When reading options, perform more thorough type-checking. This should solve a crash some modders found when modifying menu.key.

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3229 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2009-10-19 01:02:22 +00:00
parent ac3b01ce12
commit 20ae0eb3fb
5 changed files with 57 additions and 32 deletions
+3 -3
View File
@@ -101,7 +101,7 @@ register_menu_controls (int index)
{ {
VCONTROL_GESTURE g; VCONTROL_GESTURE g;
snprintf (buf, 39, "menu.%s.%d", menu_res_names[index], i); snprintf (buf, 39, "menu.%s.%d", menu_res_names[index], i);
if (!res_HasKey (buf)) if (!res_IsString (buf))
break; break;
VControl_ParseGesture (&g, res_GetString (buf)); VControl_ParseGesture (&g, res_GetString (buf));
VControl_AddGestureBinding (&g, (int *)&menu_vec[index]); VControl_AddGestureBinding (&g, (int *)&menu_vec[index]);
@@ -126,7 +126,7 @@ register_flight_controls (void)
{ {
/* Copy in name */ /* Copy in name */
snprintf (buf, 39, "keys.%d.name", i+1); snprintf (buf, 39, "keys.%d.name", i+1);
if (res_HasKey (buf)) if (res_IsString (buf))
{ {
strncpy (input_templates[i].name, res_GetString (buf), 29); strncpy (input_templates[i].name, res_GetString (buf), 29);
input_templates[i].name[29] = '\0'; input_templates[i].name[29] = '\0';
@@ -141,7 +141,7 @@ register_flight_controls (void)
{ {
VCONTROL_GESTURE *g = CONTROL_PTR(i, j, k); VCONTROL_GESTURE *g = CONTROL_PTR(i, j, k);
snprintf (buf, 39, "keys.%d.%s.%d", i+1, flight_res_names[j], k+1); snprintf (buf, 39, "keys.%d.%s.%d", i+1, flight_res_names[j], k+1);
if (!res_HasKey (buf)) if (!res_IsString (buf))
{ {
g->type = VCONTROL_NONE; g->type = VCONTROL_NONE;
continue; continue;
+1
View File
@@ -102,6 +102,7 @@ extern DIRENTRY_REF LoadDirEntryTable (uio_DirHandle *dirHandle,
BOOLEAN res_HasKey (const char *key); BOOLEAN res_HasKey (const char *key);
BOOLEAN res_IsString (const char *key);
const char *res_GetString (const char *key); const char *res_GetString (const char *key);
void res_PutString (const char *key, const char *value); void res_PutString (const char *key, const char *value);
+25 -1
View File
@@ -318,13 +318,21 @@ InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun,
} }
/* These replace the mapres.c calls and probably should be split out at some point. */ /* These replace the mapres.c calls and probably should be split out at some point. */
BOOLEAN
res_IsString (const char *key)
{
RESOURCE_INDEX idx = _get_current_index_header ();
ResourceDesc *desc = lookupResourceDesc (idx, key);
return desc && !strcmp(desc->vtable->resType, "STRING");
}
const char * const char *
res_GetString (const char *key) res_GetString (const char *key)
{ {
RESOURCE_INDEX idx = _get_current_index_header (); RESOURCE_INDEX idx = _get_current_index_header ();
ResourceDesc *desc = lookupResourceDesc (idx, key); ResourceDesc *desc = lookupResourceDesc (idx, key);
if (!desc || !desc->resdata.ptr || strcmp(desc->vtable->resType, "STRING")) if (!desc || !desc->resdata.ptr || strcmp(desc->vtable->resType, "STRING"))
return NULL; return "";
/* TODO: Work out exact STRING semantics, specifically, the lifetime of /* TODO: Work out exact STRING semantics, specifically, the lifetime of
* the returned value. If caller is allowed to reference the returned * the returned value. If caller is allowed to reference the returned
* value forever, STRING has to be ref-counted. */ * value forever, STRING has to be ref-counted. */
@@ -358,6 +366,14 @@ res_PutString (const char *key, const char *value)
} }
} }
BOOLEAN
res_IsInteger (const char *key)
{
RESOURCE_INDEX idx = _get_current_index_header ();
ResourceDesc *desc = lookupResourceDesc (idx, key);
return desc && !strcmp(desc->vtable->resType, "INT32");
}
int int
res_GetInteger (const char *key) res_GetInteger (const char *key)
{ {
@@ -385,6 +401,14 @@ res_PutInteger (const char *key, int value)
desc->resdata.num = value; desc->resdata.num = value;
} }
BOOLEAN
res_IsBoolean (const char *key)
{
RESOURCE_INDEX idx = _get_current_index_header ();
ResourceDesc *desc = lookupResourceDesc (idx, key);
return desc && !strcmp(desc->vtable->resType, "BOOLEAN");
}
BOOLEAN BOOLEAN
res_GetBoolean (const char *key) res_GetBoolean (const char *key)
{ {
+27 -27
View File
@@ -215,31 +215,31 @@ main (int argc, char *argv[])
// Fill in the options struct based on uqm.cfg // Fill in the options struct based on uqm.cfg
LoadResourceIndex (configDir, "uqm.cfg", "config."); LoadResourceIndex (configDir, "uqm.cfg", "config.");
if (res_HasKey ("config.reswidth")) if (res_IsInteger ("config.reswidth"))
{ {
options.width = res_GetInteger ("config.reswidth"); options.width = res_GetInteger ("config.reswidth");
} }
if (res_HasKey ("config.resheight")) if (res_IsInteger ("config.resheight"))
{ {
options.height = res_GetInteger ("config.resheight"); options.height = res_GetInteger ("config.resheight");
} }
if (res_HasKey("config.keepaspectratio")) if (res_IsBoolean("config.keepaspectratio"))
{ {
options.keepAspectRatio = res_GetBoolean ("config.keepaspectratio"); options.keepAspectRatio = res_GetBoolean ("config.keepaspectratio");
} }
if (res_HasKey ("config.alwaysgl")) if (res_IsBoolean ("config.alwaysgl"))
{ {
if (res_GetBoolean ("config.alwaysgl")) if (res_GetBoolean ("config.alwaysgl"))
{ {
options.gfxDriver = TFB_GFXDRIVER_SDL_OPENGL; options.gfxDriver = TFB_GFXDRIVER_SDL_OPENGL;
} }
} }
if (res_HasKey ("config.usegl")) if (res_IsBoolean ("config.usegl"))
{ {
options.gfxDriver = res_GetBoolean ("config.usegl") ? options.gfxDriver = res_GetBoolean ("config.usegl") ?
TFB_GFXDRIVER_SDL_OPENGL : TFB_GFXDRIVER_SDL_PURE; TFB_GFXDRIVER_SDL_OPENGL : TFB_GFXDRIVER_SDL_PURE;
} }
if (res_HasKey ("config.scaler")) if (res_IsString ("config.scaler"))
{ {
const char *arg = res_GetString ("config.scaler"); const char *arg = res_GetString ("config.scaler");
@@ -254,67 +254,67 @@ main (int argc, char *argv[])
else if (!strcmp (arg, "hq")) else if (!strcmp (arg, "hq"))
options.gfxFlags |= TFB_GFXFLAGS_SCALE_HQXX; options.gfxFlags |= TFB_GFXFLAGS_SCALE_HQXX;
} }
if (res_HasKey ("config.scanlines") && if (res_IsBoolean ("config.scanlines") &&
res_GetBoolean ("config.scanlines")) res_GetBoolean ("config.scanlines"))
{ {
options.gfxFlags |= TFB_GFXFLAGS_SCANLINES; options.gfxFlags |= TFB_GFXFLAGS_SCANLINES;
} }
if (res_HasKey ("config.fullscreen") && if (res_IsBoolean ("config.fullscreen") &&
res_GetBoolean ("config.fullscreen")) res_GetBoolean ("config.fullscreen"))
{ {
options.gfxFlags |= TFB_GFXFLAGS_FULLSCREEN; options.gfxFlags |= TFB_GFXFLAGS_FULLSCREEN;
} }
if (res_HasKey ("config.subtitles")) if (res_IsBoolean ("config.subtitles"))
{ {
options.subTitles = res_GetBoolean ("config.subtitles"); options.subTitles = res_GetBoolean ("config.subtitles");
} }
if (res_HasKey ("config.textmenu")) if (res_IsBoolean ("config.textmenu"))
{ {
options.whichMenu = res_GetBoolean ("config.textmenu") ? options.whichMenu = res_GetBoolean ("config.textmenu") ?
OPT_PC : OPT_3DO; OPT_PC : OPT_3DO;
} }
if (res_HasKey ("config.textgradients")) if (res_IsBoolean ("config.textgradients"))
{ {
options.whichFonts = res_GetBoolean ("config.textgradients") ? options.whichFonts = res_GetBoolean ("config.textgradients") ?
OPT_PC : OPT_3DO; OPT_PC : OPT_3DO;
} }
if (res_HasKey ("config.iconicscan")) if (res_IsBoolean ("config.iconicscan"))
{ {
options.whichCoarseScan = res_GetBoolean ("config.iconicscan") ? options.whichCoarseScan = res_GetBoolean ("config.iconicscan") ?
OPT_3DO : OPT_PC; OPT_3DO : OPT_PC;
} }
if (res_HasKey ("config.smoothscroll")) if (res_IsBoolean ("config.smoothscroll"))
{ {
options.smoothScroll = res_GetBoolean ("config.smoothscroll") ? options.smoothScroll = res_GetBoolean ("config.smoothscroll") ?
OPT_3DO : OPT_PC; OPT_3DO : OPT_PC;
} }
if (res_HasKey ("config.3domusic")) if (res_IsBoolean ("config.3domusic"))
{ {
options.use3doMusic = res_GetBoolean ("config.3domusic"); options.use3doMusic = res_GetBoolean ("config.3domusic");
} }
if (res_HasKey ("config.remixmusic")) if (res_IsBoolean ("config.remixmusic"))
{ {
options.usePrecursorsMusic = res_GetBoolean ("config.remixmusic"); options.usePrecursorsMusic = res_GetBoolean ("config.remixmusic");
} }
if (res_HasKey ("config.3domovies")) if (res_IsBoolean ("config.3domovies"))
{ {
options.whichIntro = res_GetBoolean ("config.3domovies") ? options.whichIntro = res_GetBoolean ("config.3domovies") ?
OPT_3DO : OPT_PC; OPT_3DO : OPT_PC;
} }
if (res_HasKey ("config.showfps") && res_GetBoolean ("config.showfps")) if (res_IsBoolean ("config.showfps") && res_GetBoolean ("config.showfps"))
{ {
options.gfxFlags |= TFB_GFXFLAGS_SHOWFPS; options.gfxFlags |= TFB_GFXFLAGS_SHOWFPS;
} }
if (res_HasKey ("config.smoothmelee")) if (res_IsBoolean ("config.smoothmelee"))
{ {
options.meleeScale = res_GetBoolean ("config.smoothmelee") ? options.meleeScale = res_GetBoolean ("config.smoothmelee") ?
TFB_SCALE_TRILINEAR : TFB_SCALE_STEP; TFB_SCALE_TRILINEAR : TFB_SCALE_STEP;
} }
if (res_HasKey ("config.positionalsfx")) if (res_IsBoolean ("config.positionalsfx"))
{ {
options.stereoSFX = res_GetBoolean ("config.positionalsfx"); options.stereoSFX = res_GetBoolean ("config.positionalsfx");
} }
if (res_HasKey ("config.audiodriver")) if (res_IsString ("config.audiodriver"))
{ {
const char *driverstr = res_GetString ("config.audiodriver"); const char *driverstr = res_GetString ("config.audiodriver");
if (!strcmp (driverstr, "openal")) if (!strcmp (driverstr, "openal"))
@@ -335,7 +335,7 @@ main (int argc, char *argv[])
/* Can't figure it out, leave as initial default */ /* Can't figure it out, leave as initial default */
} }
} }
if (res_HasKey ("config.audioquality")) if (res_IsString ("config.audioquality"))
{ {
const char *qstr = res_GetString ("config.audioquality"); const char *qstr = res_GetString ("config.audioquality");
if (!strcmp (qstr, "low")) if (!strcmp (qstr, "low"))
@@ -361,12 +361,12 @@ main (int argc, char *argv[])
/* Can't figure it out, leave as initial default */ /* Can't figure it out, leave as initial default */
} }
} }
if (res_HasKey ("config.pulseshield")) if (res_IsBoolean ("config.pulseshield"))
{ {
options.whichShield = options.whichShield =
res_GetBoolean ("config.pulseshield") ? OPT_3DO : OPT_PC; res_GetBoolean ("config.pulseshield") ? OPT_3DO : OPT_PC;
} }
if (res_HasKey ("config.player1control")) if (res_IsInteger ("config.player1control"))
{ {
PlayerControls[0] = res_GetInteger ("config.player1control"); PlayerControls[0] = res_GetInteger ("config.player1control");
/* This is an unsigned, so no < 0 check is necessary */ /* This is an unsigned, so no < 0 check is necessary */
@@ -377,7 +377,7 @@ main (int argc, char *argv[])
PlayerControls[0] = CONTROL_TEMPLATE_KB_1; PlayerControls[0] = CONTROL_TEMPLATE_KB_1;
} }
} }
if (res_HasKey ("config.player2control")) if (res_IsInteger ("config.player2control"))
{ {
/* This is an unsigned, so no < 0 check is necessary */ /* This is an unsigned, so no < 0 check is necessary */
PlayerControls[1] = res_GetInteger ("config.player2control"); PlayerControls[1] = res_GetInteger ("config.player2control");
@@ -388,17 +388,17 @@ main (int argc, char *argv[])
PlayerControls[1] = CONTROL_TEMPLATE_JOY_1; PlayerControls[1] = CONTROL_TEMPLATE_JOY_1;
} }
} }
if (res_HasKey ("config.musicvol")) if (res_IsInteger ("config.musicvol"))
{ {
parseIntVolume (res_GetInteger ("config.musicvol"), parseIntVolume (res_GetInteger ("config.musicvol"),
&options.musicVolumeScale); &options.musicVolumeScale);
} }
if (res_HasKey ("config.sfxvol")) if (res_IsInteger ("config.sfxvol"))
{ {
parseIntVolume (res_GetInteger ("config.sfxvol"), parseIntVolume (res_GetInteger ("config.sfxvol"),
&options.sfxVolumeScale); &options.sfxVolumeScale);
} }
if (res_HasKey ("config.speechvol")) if (res_IsInteger ("config.speechvol"))
{ {
parseIntVolume (res_GetInteger ("config.speechvol"), parseIntVolume (res_GetInteger ("config.speechvol"),
&options.speechVolumeScale); &options.speechVolumeScale);
+1 -1
View File
@@ -1183,7 +1183,7 @@ GetGlobalOptions (GLOBALOPTS *opts)
break; break;
} }
if (res_HasKey ("config.alwaysgl")) if (res_IsBoolean ("config.alwaysgl"))
{ {
if (res_GetBoolean ("config.alwaysgl")) if (res_GetBoolean ("config.alwaysgl"))
{ {