| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/win32/windata.cpp $ |
| 21 | * $Revision: 8 $ |
| 22 | * $Date: 4/24/99 5:47p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * Windows registry functions |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/win32/windata.cpp $ |
| 28 | * |
| 29 | * 8 4/24/99 5:47p Samir |
| 30 | * added functions to set current win32 resource dll or exe. |
| 31 | * |
| 32 | * 7 2/15/99 1:22p Kevin |
| 33 | * Changes for GameGauge |
| 34 | * |
| 35 | * 6 9/22/98 1:55p Matt |
| 36 | * Change the registry path to "Outrage\Descent3". |
| 37 | * |
| 38 | * 5 11/16/97 2:32p Matt |
| 39 | * Allow writing of registry string variables with an empty string as the |
| 40 | * value |
| 41 | * |
| 42 | * 4 7/24/97 3:06p Matt |
| 43 | * Added functions to read & write bools & variable-length integers, and |
| 44 | * fixed a few small bugs. |
| 45 | * |
| 46 | * 3 7/22/97 7:07p Matt |
| 47 | * Changed registry path from Software\Parallax Software\Outrage |
| 48 | * Entertainment to just Software\Outrage Entertainment |
| 49 | * |
| 50 | * 6 6/11/97 1:10p Samir |
| 51 | * Fixup of database. |
| 52 | * |
| 53 | * 5 5/13/97 11:25a Samir |
| 54 | * Create_record now sets that record as the default. |
| 55 | * |
| 56 | * 4 5/08/97 1:58p Samir |
| 57 | * When initializing database, check if there is an outrage entry in the |
| 58 | * registry. |
| 59 | * |
| 60 | * 3 3/14/97 6:10 PM Jeremy |
| 61 | * added get_user_name to osWinDatabase |
| 62 | * |
| 63 | * 2 3/03/97 4:36p Samir |
| 64 | * removed instance of pserror.h. this lib shouldn't need it. |
| 65 | * |
| 66 | * 1 3/03/97 4:28p Samir |
| 67 | * |
| 68 | * $NoKeywords: $ |
| 69 | */ |
| 70 | |
| 71 | #include "AppDatabase.h" |
| 72 | |
| 73 | #include <windows.h> |
| 74 | #include <assert.h> |
| 75 | |
| 76 | #include "mono.h" |
| 77 | #include "pserror.h" |
| 78 | |
| 79 | // Construction and destruction. |
| 80 | |
| 81 | oeWin32AppDatabase::oeWin32AppDatabase() { |
| 82 | bool res; |
| 83 | |
| 84 | hCurKey = 0; |
| 85 | |
| 86 | hBaseKey = (unsigned)HKEY_CURRENT_USER; |
| 87 | |
| 88 | // create outrage entertainment key |
| 89 | lstrcpy(m_Basepath, "SOFTWARE\\Outrage"); |
| 90 | res = lookup_record(m_Basepath); |
| 91 | if (!res) { |
| 92 | res = create_record(m_Basepath); |
| 93 | if (!res) { |
| 94 | mprintf(1, "Unable to create registry directory.\n"); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | oeWin32AppDatabase::oeWin32AppDatabase(oeWin32AppDatabase *parent) { |
| 100 | hCurKey = 0; |
| 101 | hBaseKey = parent->hCurKey; |
| 102 | } |
| 103 | |
| 104 | oeWin32AppDatabase::~oeWin32AppDatabase() { |
| 105 | if (hCurKey) { |
| 106 | RegCloseKey((HKEY)hCurKey); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Record functions |
| 111 | // these are actual folders of information |
| 112 | |
| 113 | // creates an empty classification or structure where you can store information |
| 114 | bool oeWin32AppDatabase::create_record(const char *pathname) { |
| 115 | LONG lres; |
| 116 | HKEY hkey; |
| 117 | DWORD disp; |
| 118 | |
| 119 | assert(hBaseKey); |
| 120 | |
| 121 | lres = RegCreateKeyEx((HKEY)hBaseKey, pathname, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disp); |
| 122 | if (lres != ERROR_SUCCESS) { |
| 123 | mprintf(1, "Unable to create key %s (%x)\n", pathname, lres); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | hCurKey = (unsigned)hkey; |
| 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | // set current database focus to a particular record |
| 133 | bool oeWin32AppDatabase::lookup_record(const char *pathname) { |
| 134 | LONG lres; |
| 135 | HKEY hkey; |
| 136 | |
| 137 | assert(hBaseKey); |
| 138 | |
| 139 | if (hCurKey) { |
| 140 | RegCloseKey((HKEY)hCurKey); |
| 141 | } |
| 142 | |
| 143 | lres = RegOpenKeyEx((HKEY)hBaseKey, pathname, 0, KEY_READ | KEY_WRITE | KEY_EXECUTE, &hkey); |
| 144 | if (lres != ERROR_SUCCESS) { |
| 145 | mprintf(1, "Unable to open key %s (%x)\n", pathname, lres); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | hCurKey = (unsigned)hkey; |
| 150 | |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | // read either a string from the current record |
| 155 | bool oeWin32AppDatabase::read(const char *label, char *entry, int *entrylen) { |
| 156 | LONG lres; |
| 157 | DWORD type; |
| 158 | |
| 159 | assert(hBaseKey); |
| 160 | assert(label != NULL); |
| 161 | assert(entry != NULL); |
| 162 | assert(entrylen != NULL); |
| 163 | |
| 164 | lres = RegQueryValueEx((HKEY)hCurKey, label, NULL, &type, (LPBYTE)entry, (LPDWORD)entrylen); |
| 165 | |
| 166 | assert(type != REG_DWORD); |
| 167 | |
| 168 | if (lres != ERROR_SUCCESS) { |
| 169 | mprintf(1, "Unable to query str key %s (%x)\n", label, lres); |
| 170 | return 0; |
| 171 | } |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | // read a variable-sized integer from the current record |
| 176 | bool oeWin32AppDatabase::read(const char *label, void *entry, int wordsize) { |
| 177 | LONG lres; |
| 178 | DWORD len = 4; |
| 179 | DWORD type; |
| 180 | int t; |
| 181 | |
| 182 | assert(hBaseKey); |
| 183 | assert(label != NULL); |
| 184 | assert(entry != NULL); |
| 185 | |
| 186 | lres = RegQueryValueEx((HKEY)hCurKey, label, NULL, &type, (LPBYTE)&t, &len); |
| 187 | |
| 188 | assert(len == 4); |
| 189 | |
| 190 | if (lres != ERROR_SUCCESS) { |
| 191 | mprintf(1, "Unable to query int key %s (%x)\n", label, lres); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | assert(type == REG_DWORD); |
| 196 | |
| 197 | switch (wordsize) { |
| 198 | case 1: |
| 199 | *((char *)entry) = (char)t; |
| 200 | break; |
| 201 | case 2: |
| 202 | *((int16_t *)entry) = (int16_t)t; |
| 203 | break; |
| 204 | case 4: |
| 205 | *((int *)entry) = (int)t; |
| 206 | break; |
| 207 | default: |
| 208 | Int3(); |
| 209 | break; // invalid word size |
| 210 | } |
| 211 | |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | bool oeWin32AppDatabase::read(const char *label, bool *entry) { |
| 216 | int t; |
| 217 | |
| 218 | if (read(label, &t, sizeof(t))) { |
| 219 | *entry = (t != 0); |
| 220 | return 1; |
| 221 | } else |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | // write either an integer or string to a record. |
| 226 | bool oeWin32AppDatabase::write(const char *label, const char *entry, int entrylen) { |
| 227 | LONG lres; |
| 228 | |
| 229 | assert(hBaseKey); |
| 230 | assert(label != NULL); |
| 231 | assert(entry != NULL); |
| 232 | // assert(entrylen > 0); |
| 233 | |
| 234 | lres = RegSetValueEx((HKEY)hCurKey, label, 0, REG_SZ, (LPBYTE)entry, entrylen); |
| 235 | |
| 236 | if (lres != ERROR_SUCCESS) { |
| 237 | mprintf(1, "Unable to write str key %s (%x)\n", label, lres); |
| 238 | return 0; |
| 239 | } |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | bool oeWin32AppDatabase::write(const char *label, int entry) { |
| 244 | LONG lres; |
| 245 | |
| 246 | assert(hBaseKey); |
| 247 | assert(label != NULL); |
| 248 | |
| 249 | lres = RegSetValueEx((HKEY)hCurKey, label, 0, REG_DWORD, (LPBYTE)&entry, sizeof(int)); |
| 250 | |
| 251 | if (lres != ERROR_SUCCESS) { |
| 252 | mprintf(1, "Unable to write int key %s (%x)\n", label, lres); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | // get the current user's name from the os |
| 260 | void oeWin32AppDatabase::get_user_name(char *buffer, size_t *size) { GetUserName(buffer, reinterpret_cast<LPDWORD>(size)); } |
| 261 | |
| 262 | ///////////////////////////////////////////////////////////////////////////////// |
| 263 | // pass name of dll which contains desired language |
| 264 | // NULL library is the default resource DLL |
| 265 | |
| 266 | static HINSTANCE hThisResourceModule = NULL; |
| 267 | |
| 268 | bool win32_SetResourceDLL(const char *libname) { |
| 269 | if (hThisResourceModule) { |
| 270 | FreeLibrary(hThisResourceModule); |
| 271 | hThisResourceModule = NULL; |
| 272 | } |
| 273 | |
| 274 | if (libname) { |
| 275 | hThisResourceModule = LoadLibrary(libname); |
| 276 | } |
| 277 | |
| 278 | return (hThisResourceModule) ? true : false; |
| 279 | } |
| 280 | |
| 281 | // returns a string from the current resource |
| 282 | bool win32_GetStringResource(int txt_id, char *buffer, int buflen) { |
| 283 | if (LoadString(hThisResourceModule, txt_id, buffer, buflen)) { |
| 284 | buffer[buflen - 1] = 0; |
| 285 | return true; |
| 286 | } |
| 287 | strncpy(buffer, "!!!ERROR MISSING DLL STRING!!!", buflen - 1); |
| 288 | buffer[buflen - 1] = 0; |
| 289 | return false; |
| 290 | } |