diff --git a/saveload.c b/saveload.c index 433229a722..9383b55f9b 100644 --- a/saveload.c +++ b/saveload.c @@ -751,7 +751,7 @@ void SlObject(void *object, const SaveLoad *sld) } for (; sld->cmd != SL_END; sld++) { - void *ptr = (byte*)object + (ptrdiff_t)sld->address; + void *ptr = GetVariableAddress(object, sld); SlObjectMember(ptr, sld); } } diff --git a/saveload.h b/saveload.h index 3a0a59010a..56c3a314dc 100644 --- a/saveload.h +++ b/saveload.h @@ -271,6 +271,15 @@ static inline VarType GetVarFileType(VarType type) return type & 0xF; // GB(type, 0, 4); } +/** Get the address of the variable. Which one to pick depends on the object + * pointer. If it is NULL we are dealing with global variables so the address + * is taken. If non-null only the offset is stored in the union and we need + * to add this to the address of the object */ +static inline void *GetVariableAddress(const void *object, const SaveLoad *sld) +{ + return (byte*)object + (ptrdiff_t)sld->address; +} + int64 ReadValue(const void *ptr, VarType conv); void WriteValue(void *ptr, VarType conv, int64 val); diff --git a/settings.c b/settings.c index 3de21068d0..2672f202a3 100644 --- a/settings.c +++ b/settings.c @@ -690,7 +690,7 @@ static void ini_load_settings(IniFile *ini, const SettingDesc *sd, const char *g item = ini_getitem(group, s, false); p = (item == NULL) ? sdb->def : string_to_val(sdb, item->value); - ptr = ini_get_variable(sld, object); + ptr = GetVariableAddress(object, sld); switch (sdb->cmd) { case SDT_BOOLX: /* All four are various types of (integer) numbers */ @@ -766,7 +766,7 @@ static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *g } item = ini_getitem(group, s, true); - ptr = ini_get_variable(sld, object); + ptr = GetVariableAddress(object, sld); if (item->value != NULL) { // check if the value is the same as the old value @@ -1582,7 +1582,7 @@ int32 CmdChangePatchSetting(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (flags & DC_EXEC) { Patches *patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches; - void *var = ini_get_variable(&sd->save, patches_ptr); + void *var = GetVariableAddress(patches_ptr, &sd->save); Write_ValidateSetting(var, sd, (int32)p2); if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv)); @@ -1607,11 +1607,11 @@ bool SetPatchValue(uint index, const Patches *object, int32 value) * of patches because changing a player-based setting in a game also * changes its defaults. At least that is the convention we have chosen */ if (sd->save.conv & SLF_NETWORK_NO) { - void *var = ini_get_variable(&sd->save, object); + void *var = GetVariableAddress(object, &sd->save); Write_ValidateSetting(var, sd, value); if (_game_mode != GM_MENU) { - void *var2 = ini_get_variable(&sd->save, &_patches_newgame); + void *var2 = GetVariableAddress(&_patches_newgame, &sd->save); Write_ValidateSetting(var2, sd, value); } if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv)); @@ -1654,7 +1654,7 @@ bool IConsoleSetPatchSetting(const char *name, int32 value) } patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches; - ptr = ini_get_variable(&sd->save, patches_ptr); + ptr = GetVariableAddress(patches_ptr, &sd->save); success = SetPatchValue(index, patches_ptr, value); return success; @@ -1672,7 +1672,7 @@ void IConsoleGetPatchSetting(const char *name) return; } - ptr = ini_get_variable(&sd->save, (_game_mode == GM_MENU) ? &_patches_newgame : &_patches); + ptr = GetVariableAddress((_game_mode == GM_MENU) ? &_patches_newgame : &_patches, &sd->save); if (sd->desc.cmd == SDT_BOOLX) { snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off"); @@ -1692,7 +1692,7 @@ static void LoadSettings(const SettingDesc *osd, void *object) { for (; osd->save.cmd != SL_END; osd++) { const SaveLoad *sld = &osd->save; - void *ptr = ini_get_variable(sld, object); + void *ptr = GetVariableAddress(object, sld); if (!SlObjectMember(ptr, sld)) continue; } @@ -1722,7 +1722,7 @@ static void SaveSettings(const SettingDesc *sd, void *object) SlSetLength(length); for (i = sd; i->save.cmd != SL_END; i++) { - void *ptr = ini_get_variable(&i->save, object); + void *ptr = GetVariableAddress(object, &i->save); SlObjectMember(ptr, &i->save); } } diff --git a/settings.h b/settings.h index 1eb26e2a07..0ab3ecef32 100644 --- a/settings.h +++ b/settings.h @@ -66,15 +66,6 @@ typedef enum { IGT_LIST = 1, ///< a list of values, seperated by \n and terminated by the next group block } IniGroupType; -/** Get the address of the variable. Which one to pick depends on the object - * pointer. If it is NULL we are dealing with global variables so the address - * is taken. If non-null only the offset is stored in the union and we need - * to add this to the address of the object */ -static inline void *ini_get_variable(const SaveLoad *sld, const void *object) -{ - return (object == NULL) ? sld->address : (byte*)object + (ptrdiff_t)sld->address; -} - /** The patch values that are used for new games and/or modified in config file */ extern Patches _patches_newgame; diff --git a/settings_gui.c b/settings_gui.c index eff7dedc17..ebdc58df60 100644 --- a/settings_gui.c +++ b/settings_gui.c @@ -719,7 +719,7 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e) for (i = 0; i != page->num; i++) { const SettingDesc *sd = page->entries[i].setting; const SettingDescBase *sdb = &sd->desc; - const void *var = ini_get_variable(&sd->save, patches_ptr); + const void *var = GetVariableAddress(patches_ptr, &sd->save); bool editable = true; bool disabled = false; @@ -788,7 +788,7 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e) if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return; if (!(sd->save.conv & SLF_NETWORK_NO) && _networking && !_network_server) return; - var = ini_get_variable(&sd->save, patches_ptr); + var = GetVariableAddress(patches_ptr, &sd->save); value = (int32)ReadValue(var, sd->save.conv); /* clicked on the icon on the left side. Either scroller or bool on/off */