Rise
The Vieneo Province
config.cpp
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include "Cryptography.h"
4 
5 #include <io.h> // For _open, _read, _close, _lseek (eof, write)
6 #include <cassert> // For assert
7 #include <fcntl.h> // For _O_RDWR, _O_BINARY, _O_RDONLY (O_TEXT)
8 #include <sys/stat.h> // For _S_IWRITE, _S_IREAD
9 #include <direct.h> // For _mkdir
10 #include <cstdio> // For SEEK_SET and SecureZeroMemory
11 
13 {
14  initialized = true;
15  _mkdir("Save"); // for config, messages, waypoints
17  LoadPatchDate();
18  Loader();
19 }
20 
22 {
23  int handle;
24  _sopen_s(&handle, "patchdate.txt", _O_RDONLY | _O_TEXT | _O_SEQUENTIAL, _SH_DENYNO, _S_IWRITE);
25  if (handle == -1)
26  {
27  char msg[199];
28  sprintf_s(msg, 199, "Could not open patchdate.txt for reading: %i", errno);
29  MessageBoxA(nullptr, msg, "Fatal Exception", MB_ICONERROR | MB_OK);
30  exit(errno);
31  }
32  const int bytes = _read(handle, patchdate, 24);
33  patchdate[bytes - 1] = 0;
34  if (patchdate[bytes - 2] == '\n')
35  patchdate[bytes - 2] = 0;
36  _close(handle);
37 }
38 
40 {
41  int handle;
42  _sopen_s(&handle, "patchdate.txt", _O_WRONLY | _O_CREAT | _O_TRUNC | _O_TEXT | _O_SEQUENTIAL, _SH_SECURE, _S_IWRITE);
43  if (handle == -1)
44  {
45  char msg[199];
46  sprintf_s(msg, 199, "Could not open patchdate.txt for writing: %i", errno);
47  MessageBoxA(nullptr, msg, "Fatal Exception", MB_ICONERROR | MB_OK);
48  exit(errno);
49  }
50  char newdate[24];
51  sprintf_s(newdate, 24, "%s\n", patchdate);
52  _write(handle, newdate, strlen(newdate) + 1); // include null terminator
53  _close(handle);
54 }
55 
57 {
58  assert(initialized);
59 
60  SecureZeroMemory(oldvphelp, 128);
61 
62  int config;
63  _sopen_s(&config, "Save\\client.cfg", _O_RDONLY | _O_BINARY | _O_SEQUENTIAL, _SH_DENYNO, _S_IWRITE);
64  if (config == -1)
65  {
66  // try moving from . to Save
67  if (rename("client.cfg", "Save\\client.cfg") == 0)
68  {
69  rename("waypoint.dat", "Save\\waypoint.dat");
70  _sopen_s(&config, "Save\\client.cfg", _O_RDONLY | _O_BINARY | _O_SEQUENTIAL, _SH_DENYNO, _S_IWRITE);
71  }
72  }
73 
74  unsigned char unused;
75  if (config != -1)
76  {
77  _lseek(config, 0L, SEEK_SET);
78  _read(config, &unused, 1); // 0
79  _read(config, &language, 1); // 1
80  _read(config, &unused, 1); // 2
81  _read(config, &unused, 1); // 3
82  _read(config, &unused, 1); // 4
83  _read(config, &unused, 1); // 5
84  _read(config, &configver, 1); // 6
85  if (configver > cfgverC)
86  {
87  _close(config);
88  config = -1;
89  MessageBox(nullptr, L"Your \"Save\\client.cfg\" was corrupt and has been rebuilt. Please report this error.", L"Version error!", MB_ICONERROR | MB_OK);
90  }
91  }
92 
93  if (config != -1)
94  {
95  g_bCheckSteamLanguage = false;
96  if (configver == 0)
97  {
98  configver = 1;
99  }
100 
101  if (configver == 1)
102  {
103  configver = 2;
104  defaultuser[0] = 0;
105  defaultpass[0] = 0;
106  }
107  else
108  {
109  _lseek(config, 8L, SEEK_SET);
110  _read(config, defaultuser, 13); // 8
111  }
112 
113  if (configver == 2)
114  {
115  configver = 3;
116  g_bAutoLogin = false;
117  }
118  else
119  {
120  _lseek(config, 40L, SEEK_SET);
121  _read(config, &g_bAutoLogin, 1); // 40
122  }
123 
124  if (configver == 3 || configver == 4)
125  {
126  configver = 5;
127  g_bStartFullscreen = false;
128  }
129  else
130  {
131  _lseek(config, 42L, SEEK_SET);
132  _read(config, &g_bStartFullscreen, 1); // 42
133  }
134 
135  if (configver == 5 || configver == 6 || configver == 7 || configver == 8 || configver == 9 || configver == 10 || configver == 11)
136  {
137  configver = 12;
138  language = 0; // engish
139  }
140 
141  if (configver == 12)
142  {
143  configver = 13;
144  _lseek(config, 24L, SEEK_SET);
145  _read(config, defaultpass, 16); // 24
146  char encryptpass[16];
148  _lseek(config, 24L, SEEK_SET);
149  _write(config, encryptpass, 16); // 24
150  }
151  else
152  {
153  char encryptpass[16];
154  _lseek(config, 24L, SEEK_SET);
155  _read(config, encryptpass, 16); // 24
157  }
158 
159  // these are all deleted values
160  if (configver == 13 || configver == 14 || configver == 15 || configver == 16 || configver == 17 || configver == 18 || configver == 19)
161  {
162  configver = 20;
163  }
164 
165  if (configver == 20)
166  {
167  configver = 21;
171  }
172  else
173  {
174  _lseek(config, 700L, SEEK_SET);
175  _read(config, &soundConfigComputer, 32); // 700
176  _read(config, &soundConfigComms, 32); // 732
177  _read(config, &soundConfigLegacy, 32); // 764
178  }
179 
180  if (configver == 21)
181  {
182  configver = 22;
183  g_bSoundIntroMusic = true;
184  }
185  else
186  {
187  _lseek(config, 75L, SEEK_SET); // 75
188  _read(config, &g_bSoundIntroMusic, 1); // 75
189  }
190 
191  if (configver == 22)
192  {
193  configver = 23;
200  }
201  else
202  {
203  _lseek(config, 500L, SEEK_SET); // 500
204  _read(config, &inputConfigPitch, 32); // 500
205  _read(config, &inputConfigRoll, 32); // 532
206  _read(config, &inputConfigYaw, 32); // 564
207  _read(config, &inputConfigThrustLeft, 32); // 596
208  _read(config, &inputConfigSteer, 32); // 628
209  _read(config, &inputConfigGas, 32); // 660
210  }
211 
212  if (configver == 23)
213  {
214  configver = 24;
218  }
219  else
220  {
221  _lseek(config, 400L, SEEK_SET); // 400
222  _read(config, &inputConfigBrake, 32); // 400
223  _read(config, &inputConfigLeftToeBrake, 32); // 432
224  _read(config, &inputConfigRightToeBrake, 32); // 464
225  }
226 
227  if (configver == 24)
228  {
229  configver = 25;
231  }
232  else
233  {
234  _lseek(config, 796L, SEEK_SET);
235  _read(config, &soundConfigRadio, 32); // 764
236  }
237 
238  if (configver == 25)
239  {
240  configver = 26;
241  }
242 
243  if (configver == 26)
244  {
245  configver = 27;
247  }
248  else
249  {
250  _lseek(config, 900L, SEEK_SET); // 900
251  _read(config, &inputConfigThrustRight, 32); // 900-931
252  }
253 
254  if (configver == 27)
255  {
256  configver = 28;
257  scale = 0.0f;
258  }
259  else
260  {
261  _lseek(config, 43L, SEEK_SET);
262  _read(config, &scale, 4);
263  }
264 
265  if (configver == 28)
266  {
267  _unlink("Save/gui.cfg");
268  configver = 29;
269  }
270 
271  if (configver == 29)
272  {
273  // zero out the padding for future use
274  RtlZeroMemory(inputConfigPitch.padding, sizeof InputConfig::padding);
275  RtlZeroMemory(inputConfigRoll.padding, sizeof InputConfig::padding);
276  RtlZeroMemory(inputConfigYaw.padding, sizeof InputConfig::padding);
277  RtlZeroMemory(inputConfigThrustLeft.padding, sizeof InputConfig::padding);
278  RtlZeroMemory(inputConfigThrustRight.padding, sizeof InputConfig::padding);
281  RtlZeroMemory(inputConfigSteer.padding, sizeof InputConfig::padding);
282  RtlZeroMemory(inputConfigGas.padding, sizeof InputConfig::padding);
283  RtlZeroMemory(inputConfigBrake.padding, sizeof InputConfig::padding);
284 
285  // because padding wasn't initialized previously we have to set these
286  inputConfigPitch.split = false;
287  inputConfigRoll.split = false;
288  inputConfigYaw.split = false;
293  inputConfigSteer.split = false;
294  inputConfigGas.split = false;
295  inputConfigBrake.split = false;
296 
297  configver = 30;
298  }
299 
300  if (configver == 30)
301  {
302  configver = 31;
306  }
307  else
308  {
309  _lseek(config, 932L, SEEK_SET); // 932
310  _read(config, &inputConfigLookLateral, 32);
311  _read(config, &inputConfigLookVertical, 32);
312  _read(config, &inputConfigPitchTrim, 32);
313  }
314 
315  if (configver == 31)
316  {
317  configver = 32;
320  }
321  else
322  {
323  _lseek(config, 828L, SEEK_SET); // 828
324  _read(config, &inputConfigWalkLateral, 32);
325  _read(config, &inputConfigWalkVertical, 32);
326  }
327 
328  if (configver == 32)
329  {
330  configver = 32; // we hold it here ... the GameClient will check for this version and increment it to 33 after sending oldvphelp to server
331  _lseek(config, 128L, SEEK_SET); // 128
332  _read(config, oldvphelp, 128); // 128-255
333  }
334 
335  _close(config);
336 
337  if (!g_bAutoLogin)
338  defaultuser[0] = defaultpass[0] = 0;
339  }
340  else // config==-1
341  {
342  g_bCheckSteamLanguage = true;
343  // This is for InnoSetup installer support
344  if (_access("Languages\\chinese.default", 0) != -1) language = 1; // @todo steam differentiates traditional and simplified
345  else if (_access("Languages\\dutch.default", 0) != -1) language = 2;
346  else if (_access("Languages\\french.default", 0) != -1) language = 3;
347  else if (_access("Languages\\german.default", 0) != -1) language = 4;
348  else if (_access("Languages\\greek.default", 0) != -1) language = 5;
349  else if (_access("Languages\\italian.default", 0) != -1) language = 6;
350  else if (_access("Languages\\japanese.default", 0) != -1) language = 7;
351  else if (_access("Languages\\korean.default", 0) != -1) language = 8;
352  else if (_access("Languages\\portuguese.default", 0) != -1) language = 9; // @todo steam differentiates brazillian vs. portuguese
353  else if (_access("Languages\\russian.default", 0) != -1) language = 10;
354  else if (_access("Languages\\spanish.default", 0) != -1) language = 11;
355  else language = 0; // english
356 
357  configver = cfgverC;
358 
359  Save();
360  }
361 }
362 
364 {
365  assert(initialized);
366  int config;
367  _sopen_s(&config, "Save\\client.cfg", _O_WRONLY | O_CREAT | _O_BINARY | _O_SEQUENTIAL, _SH_SECURE, _S_IWRITE);
368  if (config == -1)
369  {
370  MessageBox(nullptr, L"Could not save to the \"Save\\client.cfg\" file!", L"File system error!", MB_ICONERROR | MB_OK);
371  return;
372  }
373 
374  _lseek(config, 0L, SEEK_SET);
375  unsigned char unused[24];
376  ZeroMemory(unused, 24);
377  _write(config, &unused, 1); // 0
378  _write(config, &language, 1); // 1
379  _write(config, &unused, 1); // 2
380  _write(config, &unused, 1); // 3
381  _write(config, &unused, 1); // 4
382  _write(config, &unused, 1); // 5
383  _write(config, &configver, 1); // 6
384  _write(config, &unused, 1); // 7
385  _write(config, defaultuser, 13); // 8
386  _write(config, &unused, 1); // 21
387  _write(config, &unused, 1); // 22
388  _write(config, &unused, 1); // 23
389  char encryptpass[16];
391  _write(config, encryptpass, 16); // 24
392  _write(config, &g_bAutoLogin, 1); // 40
393  _write(config, &unused, 1); // 41
394  _write(config, &g_bStartFullscreen, 1); // 42
395  _write(config, &scale, 4); // 43
396  _write(config, &unused, 1); // 47
397  _write(config, &unused, 1); // 48
398  _write(config, &unused, 1); // 49
399  _write(config, &unused, 24); // 50
400  _write(config, &unused, 1); // 74
401  _write(config, &g_bSoundIntroMusic, 1); // 75
402 
403  _lseek(config, 400L, SEEK_SET); // 400
404  _write(config, &inputConfigBrake, 32); // 400
405  _write(config, &inputConfigLeftToeBrake, 32); // 432
406  _write(config, &inputConfigRightToeBrake, 32); // 464-496
407 
408  _lseek(config, 500L, SEEK_SET); // 500
409  _write(config, &inputConfigPitch, 32); // 500
410  _write(config, &inputConfigRoll, 32); // 532
411  _write(config, &inputConfigYaw, 32); // 564
412  _write(config, &inputConfigThrustLeft, 32); // 596
413  _write(config, &inputConfigSteer, 32); // 628
414  _write(config, &inputConfigGas, 32); // 660-692
415 
416  _lseek(config, 700L, SEEK_SET); // 700
417  _write(config, &soundConfigComputer, 32); // 700
418  _write(config, &soundConfigComms, 32); // 732
419  _write(config, &soundConfigLegacy, 32); // 764
420  _write(config, &soundConfigRadio, 32); // 796
421  _write(config, &inputConfigWalkLateral, 32); // 828
422  _write(config, &inputConfigWalkVertical, 32); // 860-892
423 
424  _lseek(config, 900L, SEEK_SET); // 900
425  _write(config, &inputConfigThrustRight, 32); // 900-931
426  _write(config, &inputConfigLookLateral, 32); // 932-963
427  _write(config, &inputConfigLookVertical, 32); // 964-995
428  _write(config, &inputConfigPitchTrim, 32); // 996-1027
429 
430  _close(config);
431 }
InputConfig inputConfigLeftToeBrake
Definition: config.h:35
void SavePatchDate() const
Definition: config.cpp:39
bool g_bCheckSteamLanguage
Definition: config.h:13
Config()
Definition: config.cpp:12
bool oldvphelp[128]
Definition: config.h:18
unsigned char language
Definition: config.h:12
static bool EncryptString(char *szPassword, char *szEncryptPwd, char *szKey)
bool g_bStartFullscreen
Definition: config.h:23
SoundConfig soundConfigLegacy
Definition: config.h:45
InputConfig inputConfigGas
Definition: config.h:33
InputConfig inputConfigRoll
Definition: config.h:27
InputConfig inputConfigLookVertical
Definition: config.h:38
static bool DecryptString(char *szEncryptPwd, char *szPassword, char *szKey)
char defaultpass[16]
Definition: config.h:16
void Save()
Definition: config.cpp:363
char padding[11]
Definition: InputConfig.h:18
bool initialized
Definition: config.h:50
void LoadPatchDate()
Definition: config.cpp:21
InputConfig inputConfigThrustLeft
Definition: config.h:29
InputConfig inputConfigThrustRight
Definition: config.h:30
static bool SetupCryptoClient()
unsigned char configver
Definition: config.h:14
char defaultuser[13]
Definition: config.h:15
InputConfig inputConfigPitchTrim
Definition: config.h:39
InputConfig inputConfigPitch
Definition: config.h:26
SoundConfig soundConfigComms
Definition: config.h:44
#define cfgverC
Definition: config.h:3
bool g_bSoundIntroMusic
Definition: config.h:19
InputConfig inputConfigBrake
Definition: config.h:34
InputConfig inputConfigRightToeBrake
Definition: config.h:36
float scale
Definition: config.h:24
SoundConfig soundConfigRadio
Definition: config.h:46
bool g_bAutoLogin
Definition: config.h:20
InputConfig inputConfigWalkVertical
Definition: config.h:41
InputConfig inputConfigWalkLateral
Definition: config.h:40
InputConfig inputConfigYaw
Definition: config.h:28
SoundConfig soundConfigComputer
Definition: config.h:43
void Loader()
Definition: config.cpp:56
InputConfig inputConfigSteer
Definition: config.h:32
InputConfig inputConfigLookLateral
Definition: config.h:37
#define MY_CRYPTKEY
Definition: Cryptography.h:8
char patchdate[24]
Definition: config.h:17