World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
AppSettings.cpp
См. документацию.
1 #include "Platform/Win/Win.h"
2 
3 bool OS_GetAppStringRecursive(HKEY parent_key, const char *path,
4  char *out_string, int out_string_size) {
5  char current_key[128];
6  char path_tail[1024];
7 
8  const char *delimiter = strstr(path, "/");
9  if (delimiter) {
10  strncpy(current_key, path, delimiter - path);
11  current_key[delimiter - path] = '\0';
12 
13  strcpy(path_tail, delimiter + 1);
14 
15  if (!parent_key) {
16  if (!strcmpi(current_key, "HKEY_CLASSES_ROOT"))
17  parent_key = HKEY_CLASSES_ROOT;
18  else if (!strcmpi(current_key, "HKEY_CURRENT_CONFIG"))
19  parent_key = HKEY_CURRENT_CONFIG;
20  else if (!strcmpi(current_key, "HKEY_CURRENT_USER"))
21  parent_key = HKEY_CURRENT_USER;
22  else if (!strcmpi(current_key, "HKEY_LOCAL_MACHINE"))
23  parent_key = HKEY_LOCAL_MACHINE;
24  else if (!strcmpi(current_key, "HKEY_USERS"))
25  parent_key = HKEY_USERS;
26  else
27  return false;
28 
29  delimiter = strstr(path_tail, "/");
30  if (delimiter) {
31  strncpy(current_key, path_tail, delimiter - path_tail);
32  current_key[delimiter - path_tail] = '\0';
33 
34  strcpy(path_tail, delimiter + 1);
35  } else {
36  return false;
37  }
38  }
39 
40  bool result = false;
41  HKEY key;
42  if (!RegOpenKeyExA(parent_key, current_key, 0,
43  KEY_READ | KEY_WOW64_32KEY, &key)) {
44  /*int idx = 0, r;
45  do {
46  char value_name[1024];
47  DWORD value_name_size = sizeof(value_name);
48  r = RegEnumValueA(key, idx++, value_name, &value_name_size, nullptr,
49  nullptr, nullptr, nullptr);
50  __debugbreak();
51  } while (r == ERROR_SUCCESS);*/
52 
53  result = OS_GetAppStringRecursive(key, path_tail, out_string,
54  out_string_size);
55  RegCloseKey(key);
56  }
57 
58  return result;
59  } else {
60  DWORD data_size = out_string_size;
61  if (RegQueryValueExA(parent_key, path, nullptr, nullptr,
62  (LPBYTE)out_string, &data_size))
63  return false;
64  return true;
65  }
66 }
67 
68 bool OS_GetAppString(const char *path, char *out_string, int out_string_size) {
69  return OS_GetAppStringRecursive(nullptr, path, out_string, out_string_size);
70 }
71 
72 //----- (004649EF) --------------------------------------------------------
73 int OS_GetAppInt(const char *pKey, int uDefValue) {
74  DWORD cbData; // [sp+8h] [bp-20h]@1
75  LPCSTR lpValueName; // [sp+Ch] [bp-1Ch]@1
76  DWORD dwDisposition; // [sp+10h] [bp-18h]@2
77  BYTE Data[4]; // [sp+14h] [bp-14h]@5
78  HKEY hKey; // [sp+18h] [bp-10h]@1
79  HKEY phkResult; // [sp+1Ch] [bp-Ch]@1
80  HKEY v10; // [sp+20h] [bp-8h]@1
81  HKEY v11; // [sp+24h] [bp-4h]@1
82 
83  lpValueName = pKey;
84  v11 = 0;
85  v10 = 0;
86  hKey = 0;
87  phkResult = 0;
88  cbData = 4;
89  *(int *)Data = uDefValue;
90 
91  if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
92  KEY_READ | KEY_WOW64_32KEY, &hKey)) { // for 64 bit
93  if (!RegCreateKeyExA(hKey, "New World Computing", 0, "", 0,
94  KEY_ALL_ACCESS, 0, &phkResult, &dwDisposition)) {
95  if (!RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0,
96  KEY_ALL_ACCESS, 0, &v10, &dwDisposition)) {
97  if (!RegCreateKeyExA(v10, "1.0", 0, "", 0, KEY_ALL_ACCESS, 0,
98  &v11, &dwDisposition)) {
99  LSTATUS status;
100  if (status = RegQueryValueExA(v11, lpValueName, 0, 0, Data,
101  &cbData)) {
102  status;
103  GetLastError();
104  RegSetValueExA(v11, lpValueName, 0, 4, Data, 4);
105  }
106  RegCloseKey(v11);
107  }
108  RegCloseKey(v10);
109  }
110  RegCloseKey(phkResult);
111  }
112  RegCloseKey(hKey);
113  }
114  return *(int *)Data;
115 }
116 
117 //----- (00464B02) --------------------------------------------------------
118 void OS_SetAppString(const char *pKey, const char *pString) {
119  size_t v2; // eax@5
120  const char *lpValueName; // [sp+4h] [bp-1Ch]@1
121  const char *Str; // [sp+8h] [bp-18h]@1
122  DWORD dwDisposition; // [sp+Ch] [bp-14h]@2
123  HKEY hKey; // [sp+10h] [bp-10h]@1
124  HKEY phkResult; // [sp+14h] [bp-Ch]@1
125  HKEY v8; // [sp+18h] [bp-8h]@1
126  HKEY v9; // [sp+1Ch] [bp-4h]@1
127 
128  Str = pString;
129  lpValueName = pKey;
130  v9 = 0;
131  v8 = 0;
132  hKey = 0;
133  phkResult = 0;
134  if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
135  KEY_READ | KEY_WOW64_32KEY, &hKey)) {
136  if (!RegCreateKeyExA(hKey, "New World Computing", 0, "", 0,
137  KEY_ALL_ACCESS, 0, &phkResult, &dwDisposition)) {
138  if (!RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0,
139  KEY_ALL_ACCESS, 0, &v8, &dwDisposition)) {
140  if (!RegCreateKeyExA(v8, "1.0", 0, "", 0, KEY_ALL_ACCESS, 0,
141  &v9, &dwDisposition)) {
142  v2 = strlen(Str);
143  RegSetValueExA(v9, lpValueName, 0, 1, (const BYTE *)Str,
144  v2 + 1);
145  RegCloseKey(v9);
146  }
147  RegCloseKey(v8);
148  }
149  RegCloseKey(phkResult);
150  }
151  RegCloseKey(hKey);
152  }
153 }
154 
155 //----- (00464BEF) --------------------------------------------------------
156 void OS_GetAppString(const char *pKeyName, char *pOutString, int uBufLen,
157  const char *pDefaultValue) {
158  // LSTATUS (__stdcall *v4)(HKEY); // esi@1
159  LSTATUS result; // eax@7
160  DWORD Type; // [sp+Ch] [bp-24h]@5
161  LPCSTR lpValueName; // [sp+10h] [bp-20h]@1
162  DWORD cbData; // [sp+14h] [bp-1Ch]@1
163  LPBYTE Dest; // [sp+18h] [bp-18h]@1
164  DWORD dwDisposition; // [sp+1Ch] [bp-14h]@2
165  HKEY phkResult; // [sp+20h] [bp-10h]@1
166  HKEY hKey; // [sp+24h] [bp-Ch]@1
167  HKEY v13; // [sp+28h] [bp-8h]@1
168  HKEY v14; // [sp+2Ch] [bp-4h]@1
169 
170  cbData = uBufLen;
171  Dest = (LPBYTE)pOutString;
172  lpValueName = pKeyName;
173  v14 = 0;
174  v13 = 0;
175  hKey = 0;
176  phkResult = 0;
177  result = (LSTATUS)strncpy((char *)Dest, pDefaultValue, uBufLen);
178  if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
179  KEY_READ | KEY_WOW64_32KEY, &hKey)) {
180  if (!RegCreateKeyExA(hKey, "New World Computing", 0, "", 0,
181  KEY_ALL_ACCESS, 0, &phkResult, &dwDisposition)) {
182  if (!RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0,
183  KEY_ALL_ACCESS, 0, &v13, &dwDisposition)) {
184  if (!RegCreateKeyExA(v13, "1.0", 0, "", 0, KEY_ALL_ACCESS, 0,
185  &v14, &dwDisposition)) {
186  if (RegQueryValueExA(v14, lpValueName, 0, &Type, Dest,
187  &cbData))
188  GetLastError();
189  RegCloseKey(v14);
190  v14 = NULL;
191  }
192  RegCloseKey(v13);
193  v13 = NULL;
194  }
195  RegCloseKey(phkResult);
196  phkResult = NULL;
197  }
198  RegCloseKey(hKey);
199  hKey = NULL;
200  }
201 }
202 
203 //----- (00464D32) --------------------------------------------------------
204 void OS_SetAppInt(const char *pKey, int val) {
205  const char *lpValueName; // [sp+4h] [bp-1Ch]@1
206  BYTE Data[4]; // [sp+8h] [bp-18h]@1
207  DWORD dwDisposition; // [sp+Ch] [bp-14h]@2
208  HKEY hKey; // [sp+10h] [bp-10h]@1
209  HKEY phkResult; // [sp+14h] [bp-Ch]@1
210  HKEY v7; // [sp+18h] [bp-8h]@1
211  HKEY v8; // [sp+1Ch] [bp-4h]@1
212 
213  *(int *)Data = val;
214  lpValueName = pKey;
215  v8 = 0;
216  v7 = 0;
217  hKey = 0;
218  phkResult = 0;
219  if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
220  KEY_READ | KEY_WOW64_32KEY, &hKey)) {
221  if (!RegCreateKeyExA(hKey, "New World Computing", 0, "", 0,
222  KEY_ALL_ACCESS, 0, &phkResult, &dwDisposition)) {
223  if (!RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0,
224  KEY_ALL_ACCESS, 0, &v7, &dwDisposition)) {
225  if (!RegCreateKeyExA(v7, "1.0", 0, "", 0, KEY_ALL_ACCESS, 0,
226  &v8, &dwDisposition)) {
227  RegSetValueExA(v8, lpValueName, 0, 4, Data, 4);
228  RegCloseKey(v8);
229  }
230  RegCloseKey(v7);
231  }
232  RegCloseKey(phkResult);
233  }
234  RegCloseKey(hKey);
235  }
236 }
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3733
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
OS_GetAppStringRecursive
bool OS_GetAppStringRecursive(HKEY parent_key, const char *path, char *out_string, int out_string_size)
Definition: AppSettings.cpp:3
OS_GetAppString
bool OS_GetAppString(const char *path, char *out_string, int out_string_size)
Definition: AppSettings.cpp:68
OS_SetAppString
void OS_SetAppString(const char *pKey, const char *pString)
Definition: AppSettings.cpp:118
OS_SetAppInt
void OS_SetAppInt(const char *pKey, int val)
Definition: AppSettings.cpp:204
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1495
v2
GLfloat GLfloat GLfloat v2
Definition: SDL_opengl_glext.h:695
Win.h
OS_GetAppInt
int OS_GetAppInt(const char *pKey, int uDefValue)
Definition: AppSettings.cpp:73