Duke3D/Startup: Clean up the Windows registry reading code.

There is no need to cache the result of a function called only once for each input.

git-svn-id: https://svn.eduke32.com/eduke32@5447 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
hendricks266 2015-11-25 12:08:14 +00:00
parent 6cb925a95d
commit f1a658e790

View file

@ -503,50 +503,20 @@ void G_LoadGroups(int32_t autoload)
} }
#ifdef _WIN32 #ifdef _WIN32
const char * G_GetInstallPath(int32_t insttype) static int G_ReadRegistryValue(char const * const SubKey, char const * const Value, char * const Output, DWORD * OutputSize)
{ {
static char spath[NUMINSTPATHS][BMAX_PATH]; HKEY hkey;
static int32_t success[NUMINSTPATHS] = { -1, -1, -1, -1, -1 }; LONG keygood = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ | KEY_WOW64_32KEY, &hkey);
int32_t siz = BMAX_PATH;
if (success[insttype] == -1)
{
HKEY HKLM32;
LONG keygood = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ | KEY_WOW64_32KEY, &HKLM32);
// KEY_WOW64_32KEY gets us around Wow6432Node on 64-bit builds // KEY_WOW64_32KEY gets us around Wow6432Node on 64-bit builds
if (keygood == ERROR_SUCCESS) if (keygood != ERROR_SUCCESS)
{ return 0;
switch (insttype)
{
case INSTPATH_STEAM_DUKE3D_MEGATON:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
break;
case INSTPATH_STEAM_DUKE3D_3DR:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 359850", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
break;
case INSTPATH_GOG_DUKE3D:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, spath[insttype], (LPDWORD)&siz);
break;
case INSTPATH_3DR_DUKE3D:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\3DRealms\\Duke Nukem 3D", NULL, NULL, spath[insttype], (LPDWORD)&siz);
break;
case INSTPATH_3DR_ANTH:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\3DRealms\\Anthology", NULL, NULL, spath[insttype], (LPDWORD)&siz);
break;
case INSTPATH_STEAM_NAM:
success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 329650", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
break;
}
RegCloseKey(HKLM32); LSTATUS retval = SHGetValueA(hkey, SubKey, Value, NULL, Output, OutputSize);
}
}
if (success[insttype] == ERROR_SUCCESS) RegCloseKey(hkey);
return spath[insttype];
return NULL; return retval == ERROR_SUCCESS;
} }
#endif #endif
@ -580,32 +550,29 @@ static void G_AddSteamPaths(const char *basepath)
{ {
char buf[BMAX_PATH]; char buf[BMAX_PATH];
// Duke Nukem 3D: Megaton Edition (Steam)
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot", basepath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/dc", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/dc", basepath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/nw", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/nw", basepath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/vacation", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/vacation", basepath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music", basepath);
addsearchpath(buf); addsearchpath(buf);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/nwinter", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/nwinter", basepath);
addsearchpath_user(buf, SEARCHPATH_NWINTER); addsearchpath_user(buf, SEARCHPATH_NWINTER);
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/vacation", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/vacation", basepath);
addsearchpath(buf); addsearchpath(buf);
// Duke Nukem 3D (3D Realms Anthology (Steam) / Kill-A-Ton Collection 2015)
#if defined EDUKE32_OSX #if defined EDUKE32_OSX
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/Duke Nukem 3D.app/drive_c/Program Files/Duke Nukem 3D", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/Duke Nukem 3D.app/drive_c/Program Files/Duke Nukem 3D", basepath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
#endif #endif
// NAM (Steam)
#if defined EDUKE32_OSX #if defined EDUKE32_OSX
Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/Nam.app/Contents/Resources/Nam.boxer/C.harddisk/NAM", basepath); Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/Nam.app/Contents/Resources/Nam.boxer/C.harddisk/NAM", basepath);
#else #else
@ -814,6 +781,7 @@ void G_AddSearchPaths(void)
Bsnprintf(buf, sizeof(buf), "%s/Steam/steamapps/libraryfolders.vdf", support[i]); Bsnprintf(buf, sizeof(buf), "%s/Steam/steamapps/libraryfolders.vdf", support[i]);
G_ParseSteamKeyValuesForPaths(buf); G_ParseSteamKeyValuesForPaths(buf);
// Duke Nukem 3D: Atomic Edition (GOG.com)
Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D.app/Contents/Resources/Duke Nukem 3D.boxer/C.harddisk", applications[i]); Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D.app/Contents/Resources/Duke Nukem 3D.boxer/C.harddisk", applications[i]);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
} }
@ -832,57 +800,80 @@ void G_AddSearchPaths(void)
Bfree(support[i]); Bfree(support[i]);
} }
#elif defined (_WIN32) #elif defined (_WIN32)
char buf[BMAX_PATH]; char buf[BMAX_PATH] = {0};
const char* instpath; DWORD bufsize;
if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D_MEGATON))) // Duke Nukem 3D: Megaton Edition (Steam)
bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", buf, &bufsize))
{ {
Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath); char * const suffix = buf + bufsize - 1;
addsearchpath_user(buf, SEARCHPATH_REMOVE); DWORD const remaining = sizeof(buf) - bufsize;
Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath); Bstrncpy(suffix, "/gameroot", remaining);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bstrncpy(suffix, "/gameroot/addons/dc", remaining);
Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bstrncpy(suffix, "/gameroot/addons/nw", remaining);
Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bstrncpy(suffix, "/gameroot/addons/vacation", remaining);
Bsnprintf(buf, sizeof(buf), "%s/gameroot/music", instpath); addsearchpath_user(buf, SEARCHPATH_REMOVE);
Bstrncpy(suffix, "/gameroot/music", remaining);
addsearchpath(buf); addsearchpath(buf);
Bstrncpy(suffix, "/gameroot/music/nwinter", remaining);
Bsnprintf(buf, sizeof(buf), "%s/gameroot/music/nwinter", instpath);
addsearchpath_user(buf, SEARCHPATH_NWINTER); addsearchpath_user(buf, SEARCHPATH_NWINTER);
Bstrncpy(suffix, "/gameroot/music/vacation", remaining);
Bsnprintf(buf, sizeof(buf), "%s/gameroot/music/vacation", instpath);
addsearchpath(buf); addsearchpath(buf);
} }
if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D_3DR))) // Duke Nukem 3D (3D Realms Anthology (Steam) / Kill-A-Ton Collection 2015)
bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 359850", "InstallLocation", buf, &bufsize))
{ {
Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath); char * const suffix = buf + bufsize - 1;
DWORD const remaining = sizeof(buf) - bufsize;
Bstrncpy(suffix, "/Duke Nukem 3D", remaining);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
} }
if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D))) // Duke Nukem 3D: Atomic Edition (GOG.com)
addsearchpath_user(instpath, SEARCHPATH_REMOVE); bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", buf, &bufsize))
if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
{ {
Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
} }
if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH))) // Duke Nukem 3D (3D Realms Anthology)
bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\3DRealms\\Duke Nukem 3D", NULL, buf, &bufsize))
{ {
Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath); char * const suffix = buf + bufsize - 1;
DWORD const remaining = sizeof(buf) - bufsize;
Bstrncpy(suffix, "/Duke Nukem 3D", remaining);
addsearchpath_user(buf, SEARCHPATH_REMOVE); addsearchpath_user(buf, SEARCHPATH_REMOVE);
} }
if ((instpath = G_GetInstallPath(INSTPATH_STEAM_NAM))) // 3D Realms Anthology
bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\3DRealms\\Anthology", NULL, buf, &bufsize))
{ {
Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath); char * const suffix = buf + bufsize - 1;
DWORD const remaining = sizeof(buf) - bufsize;
Bstrncpy(suffix, "/Duke Nukem 3D", remaining);
addsearchpath_user(buf, SEARCHPATH_REMOVE);
}
// NAM (Steam)
bufsize = sizeof(buf);
if (G_ReadRegistryValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 329650", "InstallLocation", buf, &bufsize))
{
char * const suffix = buf + bufsize - 1;
DWORD const remaining = sizeof(buf) - bufsize;
Bstrncpy(suffix, "/NAM", remaining);
addsearchpath_user(buf, SEARCHPATH_NAM); addsearchpath_user(buf, SEARCHPATH_NAM);
} }
#endif #endif