From 17e8693e62e87f1fb32817502fd2c234be738d07 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 31 Jul 2015 17:34:27 +0100 Subject: [PATCH 01/11] Initial stubs for extending the save/load version checking/upgrade code, with support for tests on individually versioned features as well as savegame versions. This is mainly for improved handling of features added to patchpacks. The current status-quo of picking a random number for the savegame version when releasing a patch breaks loading of savegames from newer trunk savegame versions, with or without the same patch applied, as savegame upgrade code is not run, which can also lead to crashes on load. Instead don't change the savegame version (but set the high bit to keep trunk versions away), but instead include the versions of individual features using a separate mechanism. --- source.list | 2 + src/saveload/extended_ver_sl.cpp | 105 +++++++++++++++++++++++++++++++ src/saveload/extended_ver_sl.h | 79 +++++++++++++++++++++++ src/saveload/saveload.cpp | 50 ++++++++++++--- src/saveload/saveload.h | 69 ++++++++++++++------ src/settings.cpp | 16 ++--- src/table/company_settings.ini | 5 +- src/table/currency_settings.ini | 7 ++- src/table/gameopt_settings.ini | 15 ++--- src/table/misc_settings.ini | 13 ++-- src/table/settings.h.preamble | 88 +++++++++++++------------- src/table/settings.ini | 27 ++++---- src/table/win32_settings.ini | 5 +- src/table/window_settings.ini | 5 +- 14 files changed, 369 insertions(+), 117 deletions(-) create mode 100644 src/saveload/extended_ver_sl.cpp create mode 100644 src/saveload/extended_ver_sl.h diff --git a/source.list b/source.list index df35cdd26e..62b1dd3f43 100644 --- a/source.list +++ b/source.list @@ -633,6 +633,8 @@ saveload/subsidy_sl.cpp saveload/town_sl.cpp saveload/vehicle_sl.cpp saveload/waypoint_sl.cpp +saveload/extended_ver_sl.h +saveload/extended_ver_sl.cpp # Tables table/airport_defaults.h diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp new file mode 100644 index 0000000000..143167d6ed --- /dev/null +++ b/src/saveload/extended_ver_sl.cpp @@ -0,0 +1,105 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file extended_ver_sl.cpp Functions related to handling save/load extended version info. */ + +#include "../stdafx.h" +#include "../debug.h" +#include "extended_ver_sl.h" + +#include "../safeguards.h" + +uint16 _sl_xv_feature_versions[XSLFI_SIZE]; ///< array of all known feature types and their current versions +std::vector _sl_xv_discardable_chunk_ids; ///< list of chunks IDs which we can discard if no chunk loader exists + +/** + * Extended save/load feature test + * + * First performs a tradional check on the provided @p savegame_version against @p savegame_version_from and @p savegame_version_to. + * Then, if the feature set in the constructor is not XSLFI_NULL, also check than the feature version is inclusively bounded by @p min_version and @p max_version, + * and return the combination of the two tests using the operator defined in the constructor. + * Otherwise just returns the result of the savegame version test + */ +bool ExtendedSaveLoadFeatureTest::IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const +{ + bool savegame_version_ok = savegame_version >= savegame_version_from && savegame_version <= savegame_version_to; + + ExtendedSaveLoadFeatureIndex feature = static_cast(GB(this->value, 0, 16)); + if (feature == XSLFI_NULL) return savegame_version_ok; + + uint16 min_version = GB(this->value, 16, 16); + uint16 max_version = GB(this->value, 32, 16); + ExtendedSaveLoadFeatureTestOperator op = static_cast(GB(this->value, 48, 16)); + bool feature_ok = SlXvIsFeaturePresent(feature, min_version, max_version); + + switch (op) { + case XSLFTO_OR: + return savegame_version_ok || feature_ok; + + case XSLFTO_AND: + return savegame_version_ok && feature_ok; + + default: + NOT_REACHED(); + return false; + } +} + +/** + * Returns true if @p feature is present and has a version inclusively bounded by @p min_version and @p max_version + */ +bool SlXvIsFeaturePresent(ExtendedSaveLoadFeatureIndex feature, uint16 min_version, uint16 max_version) +{ + assert(feature < XSLFI_SIZE); + return _sl_xv_feature_versions[feature] >= min_version && _sl_xv_feature_versions[feature] <= max_version; +} + +/** + * Resets all extended feature versions to 0 + */ +void SlXvResetState() +{ + memset(_sl_xv_feature_versions, 0, sizeof(_sl_xv_feature_versions)); +} + +/** + * Resets all extended feature versions to their currently enabled versions, i.e. versions suitable for saving + */ +void SlXvSetCurrentState() +{ + extern bool _sl_is_ext_version; + + SlXvResetState(); + _sl_is_ext_version = true; + + // TODO: set versions for currently enabled features here +} + +/** + * Check for "special" savegame versions (i.e. known patchpacks) and set correct savegame version, settings, etc. + */ +void SlXvCheckSpecialSavegameVersions() +{ + extern uint16 _sl_version; + + // TODO: check for savegame versions +} + +/** + * Return true if this chunk has been marked as discardable + */ +bool SlXvIsChunkDiscardable(uint32 id) +{ + for(size_t i = 0; i < _sl_xv_discardable_chunk_ids.size(); i++) { + if (_sl_xv_discardable_chunk_ids[i] == id) { + return true; + } + } + return false; +} diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h new file mode 100644 index 0000000000..96b0feddec --- /dev/null +++ b/src/saveload/extended_ver_sl.h @@ -0,0 +1,79 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file extended_ver_sl.h Functions/types related to handling save/load extended version info. */ + +#ifndef EXTENDED_VER_SL_H +#define EXTENDED_VER_SL_H + +#include "../core/bitmath_func.hpp" + +#include + +/** + * List of extended features, each feature has its own (16 bit) version + */ +enum ExtendedSaveLoadFeatureIndex { + XSLFI_NULL = 0, ///< Unused value, to indicate that no extended feature test is in use + + XSLFI_SIZE, ///< Total count of features, including null feature +}; + +extern uint16 _sl_xv_feature_versions[XSLFI_SIZE]; + +/** + * Operator to use when combining traditional savegame number test with an extended feature version test + */ +enum ExtendedSaveLoadFeatureTestOperator { + XSLFTO_OR = 0, ///< Test if traditional savegame version is in bounds OR extended feature is in version bounds + XSLFTO_AND ///< Test if traditional savegame version is in bounds AND extended feature is in version bounds +}; + +/** + * Structure to describe an extended feature version test, and how it combines with a traditional savegame version test + */ +struct ExtendedSaveLoadFeatureTest { + private: + uint64 value; + + public: + ExtendedSaveLoadFeatureTest() + : value(0) { } + + ExtendedSaveLoadFeatureTest(ExtendedSaveLoadFeatureTestOperator op, ExtendedSaveLoadFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF) + { + this->value = 0; + SB(this->value, 0, 16, feature); + SB(this->value, 16, 16, min_version); + SB(this->value, 32, 16, max_version); + SB(this->value, 48, 16, op); + } + + bool IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const; +}; + +bool SlXvIsFeaturePresent(ExtendedSaveLoadFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF); + +/** + * Returns true if @p feature is missing (i.e. has a version of 0) + */ +inline bool SlXvIsFeatureMissing(ExtendedSaveLoadFeatureIndex feature) +{ + return !SlXvIsFeaturePresent(feature); +} + +void SlXvResetState(); + +void SlXvSetCurrentState(); + +void SlXvCheckSpecialSavegameVersions(); + +bool SlXvIsChunkDiscardable(uint32 id); + +#endif /* EXTENDED_VER_SL_H */ diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index bd3c83d139..06836855da 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -48,9 +48,12 @@ #include "saveload_internal.h" #include "saveload_filter.h" +#include "extended_ver_sl.h" #include "../safeguards.h" +#include + /* * Previous savegame versions, the trunk revision where they were * introduced and the released version that had that particular @@ -264,12 +267,14 @@ * 194 26881 1.5.x */ extern const uint16 SAVEGAME_VERSION = 194; ///< Current savegame version of OpenTTD. +const uint16 SAVEGAME_VERSION_EXT = 0x8000; ///< Savegame extension indicator mask SavegameType _savegame_type; ///< type of savegame we are loading uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) uint16 _sl_version; ///< the major savegame version identifier byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! +bool _sl_is_ext_version; ///< is this an extended savegame version, with more info in the SLXI chunk? char _savegame_format[8]; ///< how to compress savegames bool _do_autosave; ///< are we doing an autosave at the moment? @@ -414,6 +419,7 @@ struct SaveLoadParams { static SaveLoadParams _sl; ///< Parameters used for/at saveload. /* these define the chunks */ +//extern const ChunkHandler _version_ext_chunk_handlers[]; extern const ChunkHandler _gamelog_chunk_handlers[]; extern const ChunkHandler _map_chunk_handlers[]; extern const ChunkHandler _misc_chunk_handlers[]; @@ -450,6 +456,7 @@ extern const ChunkHandler _persistent_storage_chunk_handlers[]; /** Array of all chunks in a savegame, \c NULL terminated. */ static const ChunkHandler * const _chunk_handlers[] = { +// _version_ext_chunk_handlers, // this should be first, such that it is saved first, as when loading it affects the loading of subsequent chunks _gamelog_chunk_handlers, _map_chunk_handlers, _misc_chunk_handlers, @@ -503,6 +510,7 @@ static void SlNullPointers() * during NULLing; especially those that try to get * pointers from other pools. */ _sl_version = SAVEGAME_VERSION; + SlXvSetCurrentState(); DEBUG(sl, 1, "Nulling pointers"); @@ -1414,7 +1422,7 @@ static void SlList(void *list, SLRefType conv) /** Are we going to save this object or not? */ static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld) { - if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false; + if (!sld->ext_feature_test.IsFeaturePresent(_sl_version, sld->version_from, sld->version_to)) return false; if (sld->conv & SLF_NOT_IN_SAVE) return false; return true; @@ -1692,7 +1700,7 @@ static void SlLoadChunk(const ChunkHandler *ch) /** * Load a chunk of data for checking savegames. * If the chunkhandler is NULL, the chunk is skipped. - * @param ch The chunkhandler that will be used for the operation + * @param ch The chunkhandler that will be used for the operation, this may be NULL */ static void SlLoadCheckChunk(const ChunkHandler *ch) { @@ -1706,14 +1714,14 @@ static void SlLoadCheckChunk(const ChunkHandler *ch) switch (m) { case CH_ARRAY: _sl.array_index = 0; - if (ch->load_check_proc) { + if (ch && ch->load_check_proc) { ch->load_check_proc(); } else { SlSkipArray(); } break; case CH_SPARSE_ARRAY: - if (ch->load_check_proc) { + if (ch && ch->load_check_proc) { ch->load_check_proc(); } else { SlSkipArray(); @@ -1726,7 +1734,7 @@ static void SlLoadCheckChunk(const ChunkHandler *ch) len += SlReadUint16(); _sl.obj_len = len; endoffs = _sl.reader->GetSize() + len; - if (ch->load_check_proc) { + if (ch && ch->load_check_proc) { ch->load_check_proc(); } else { SlSkipBytes(len); @@ -1840,8 +1848,16 @@ static void SlLoadChunks() DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id); ch = SlFindChunkHandler(id); - if (ch == NULL) SlErrorCorrupt("Unknown chunk type"); - SlLoadChunk(ch); + if (ch == NULL) { + if (SlXvIsChunkDiscardable(id)) { + DEBUG(sl, 1, "Discarding chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id); + SlLoadCheckChunk(NULL); + } else { + SlErrorCorrupt("Unknown chunk type"); + } + } else { + SlLoadChunk(ch); + } } } @@ -1855,7 +1871,7 @@ static void SlLoadCheckChunks() DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id); ch = SlFindChunkHandler(id); - if (ch == NULL) SlErrorCorrupt("Unknown chunk type"); + if (ch == NULL && !SlXvIsChunkDiscardable(id)) SlErrorCorrupt("Unknown chunk type"); SlLoadCheckChunk(ch); } } @@ -2511,7 +2527,7 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded) const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression); /* We have written our stuff to memory, now write it to file! */ - uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) }; + uint32 hdr[2] = { fmt->tag, TO_BE32((SAVEGAME_VERSION | SAVEGAME_VERSION_EXT) << 16) }; _sl.sf->Write((byte*)hdr, sizeof(hdr)); _sl.sf = fmt->init_write(_sl.sf, compression); @@ -2578,6 +2594,7 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded) _sl.sf = writer; _sl_version = SAVEGAME_VERSION; + SlXvSetCurrentState(); SaveViewportBeforeSaveGame(); SlSaveChunks(); @@ -2629,6 +2646,8 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check) _load_check_data.checkable = true; } + SlXvResetState(); + uint32 hdr[2]; if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); @@ -2641,6 +2660,8 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check) _sl.lf->Reset(); _sl_version = 0; _sl_minor_version = 0; + _sl_is_ext_version = false; + SlXvResetState(); /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */ fmt = _saveload_formats; @@ -2663,7 +2684,14 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check) * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */ _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF; - DEBUG(sl, 1, "Loading savegame version %d", _sl_version); + if (_sl_version & SAVEGAME_VERSION_EXT) { + _sl_version &= ~SAVEGAME_VERSION_EXT; + _sl_is_ext_version = true; + } else { + SlXvCheckSpecialSavegameVersions(); + } + + DEBUG(sl, 1, "Loading savegame version %d%s", _sl_version, _sl_is_ext_version ? " (extended)" : ""); /* Is the version higher than the current? */ if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME); @@ -2800,6 +2828,8 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo if (!LoadOldSaveGame(filename)) return SL_REINIT; _sl_version = 0; _sl_minor_version = 0; + _sl_is_ext_version = false; + SlXvResetState(); GamelogStartAction(GLAT_LOAD); if (!AfterLoadGame()) { GamelogStopAction(); diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 72c51fa69d..230203b4e7 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -14,6 +14,7 @@ #include "../fileio_type.h" #include "../strings_type.h" +#include "extended_ver_sl.h" /** Save or load result codes. */ enum SaveOrLoadResult { @@ -213,6 +214,7 @@ struct SaveLoad { * that is called to save it. address: global=true, offset: global=false */ void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536) size_t size; ///< the sizeof size. + ExtendedSaveLoadFeatureTest ext_feature_test; ///< extended feature test }; /** Same as #SaveLoad but global variables are used (for better readability); */ @@ -226,9 +228,11 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field * @note In general, it is better to use one of the SLE_* macros below. */ -#define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable), cpp_sizeof(base, variable)} +#define SLE_GENERAL_X(cmd, base, variable, type, length, from, to, extver) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable), cpp_sizeof(base, variable), extver} +#define SLE_GENERAL(cmd, base, variable, type, length, from, to) SLE_GENERAL_X(cmd, base, variable, type, length, from, to, {}) /** * Storage of a variable in some savegame versions. @@ -237,8 +241,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to) +#define SLE_CONDVAR_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_VAR, base, variable, type, 0, from, to, extver) +#define SLE_CONDVAR(base, variable, type, from, to) SLE_CONDVAR_X(base, variable, type, from, to, {}) /** * Storage of a reference in some savegame versions. @@ -247,8 +253,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Type of the reference, a value from #SLRefType. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to) +#define SLE_CONDREF_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_REF, base, variable, type, 0, from, to, extver) +#define SLE_CONDREF(base, variable, type, from, to) SLE_CONDREF_X(base, variable, type, from, to, {}) /** * Storage of an array in some savegame versions. @@ -258,8 +266,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the array. * @param from First savegame version that has the array. * @param to Last savegame version that has the array. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to) +#define SLE_CONDARR_X(base, variable, type, length, from, to, extver) SLE_GENERAL_X(SL_ARR, base, variable, type, length, from, to, extver) +#define SLE_CONDARR(base, variable, type, length, from, to) SLE_CONDARR_X(base, variable, type, length, from, to, {}) /** * Storage of a string in some savegame versions. @@ -269,8 +279,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the string (only used for fixed size buffers). * @param from First savegame version that has the string. * @param to Last savegame version that has the string. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to) +#define SLE_CONDSTR_X(base, variable, type, length, from, to, extver) SLE_GENERAL_X(SL_STR, base, variable, type, length, from, to, extver) +#define SLE_CONDSTR(base, variable, type, length, from, to) SLE_CONDSTR_X(base, variable, type, length, from, to, {}) /** * Storage of a list in some savegame versions. @@ -279,8 +291,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the list. * @param to Last savegame version that has the list. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to) +#define SLE_CONDLST_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_LST, base, variable, type, 0, from, to, extver) +#define SLE_CONDLST(base, variable, type, from, to) SLE_CONDLST_X(base, variable, type, from, to, {}) /** * Storage of a variable in every version of a savegame. @@ -335,17 +349,19 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Length of the empty space. * @param from First savegame version that has the empty space. * @param to Last savegame version that has the empty space. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have empty space */ -#define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to) +#define SLE_CONDNULL_X(length, from, to, extver) SLE_CONDARR_X(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, extver) +#define SLE_CONDNULL(length, from, to) SLE_CONDNULL_X(length, from, to, {}) /** Translate values ingame to different values in the savegame and vv. */ #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value) -#define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0} -#define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0} +#define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0, {}} +#define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0, {}} /** End marker of a struct/class save or load. */ -#define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0} +#define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0, {}} /** * Storage of global simple variables, references (pointers), and arrays. @@ -354,9 +370,11 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field * @note In general, it is better to use one of the SLEG_* macros below. */ -#define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)} +#define SLEG_GENERAL_X(cmd, variable, type, length, from, to, extver) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable), extver} +#define SLEG_GENERAL(cmd, variable, type, length, from, to) SLEG_GENERAL_X(cmd, variable, type, length, from, to, {}) /** * Storage of a global variable in some savegame versions. @@ -364,8 +382,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to) +#define SLEG_CONDVAR_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_VAR, variable, type, 0, from, to, extver) +#define SLEG_CONDVAR(variable, type, from, to) SLEG_CONDVAR_X(variable, type, from, to, {}) /** * Storage of a global reference in some savegame versions. @@ -373,8 +393,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to) +#define SLEG_CONDREF_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_REF, variable, type, 0, from, to, extver) +#define SLEG_CONDREF(variable, type, from, to) SLEG_CONDREF_X(variable, type, from, to, {}) /** * Storage of a global array in some savegame versions. @@ -383,8 +405,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the array. * @param from First savegame version that has the array. * @param to Last savegame version that has the array. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to) +#define SLEG_CONDARR_X(variable, type, length, from, to, extver) SLEG_GENERAL_X(SL_ARR, variable, type, length, from, to, extver) +#define SLEG_CONDARR(variable, type, length, from, to) SLEG_CONDARR_X(variable, type, length, from, to, {}) /** * Storage of a global string in some savegame versions. @@ -393,8 +417,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the string (only used for fixed size buffers). * @param from First savegame version that has the string. * @param to Last savegame version that has the string. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to) +#define SLEG_CONDSTR_X(variable, type, length, from, to, extver) SLEG_GENERAL_X(SL_STR, variable, type, length, from, to, extver) +#define SLEG_CONDSTR(variable, type, length, from, to) SLEG_CONDSTR_X(variable, type, length, from, to, {}) /** * Storage of a global list in some savegame versions. @@ -402,8 +428,10 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the list. * @param to Last savegame version that has the list. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field */ -#define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to) +#define SLEG_CONDLST_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_LST, variable, type, 0, from, to, extver) +#define SLEG_CONDLST(variable, type, from, to) SLEG_CONDLST_X(variable, type, from, to, {}) /** * Storage of a global variable in every savegame version. @@ -445,11 +473,12 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Length of the empty space. * @param from First savegame version that has the empty space. * @param to Last savegame version that has the empty space. + * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have empty space */ -#define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL} +#define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL, {}} /** End marker of global variables save or load. */ -#define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0} +#define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0, {}} /** * Checks whether the savegame is below \a major.\a minor. @@ -471,10 +500,10 @@ static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0) * @param version_to Highest version number that falls within the range. * @return Active savegame version falls within the given range. */ -static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to) +static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to, ExtendedSaveLoadFeatureTest ext_feature_test) { extern const uint16 SAVEGAME_VERSION; - if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false; + if (!ext_feature_test.IsFeaturePresent(SAVEGAME_VERSION, version_from, version_to)) return false; return true; } diff --git a/src/settings.cpp b/src/settings.cpp index e6754bb905..2ed2f6f26b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -486,7 +486,7 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp const SettingDescBase *sdb = &sd->desc; const SaveLoad *sld = &sd->save; - if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue; + if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to, sld->ext_feature_test)) continue; /* For settings.xx.yy load the settings from [xx] yy = ? */ s = strchr(sdb->name, '.'); @@ -585,7 +585,7 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp /* If the setting is not saved to the configuration * file, just continue with the next setting */ - if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue; + if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to, sld->ext_feature_test)) continue; if (sld->conv & SLF_NOT_IN_CONFIG) continue; /* XXX - wtf is this?? (group override?) */ @@ -1381,7 +1381,7 @@ static void HandleOldDiffCustom(bool savegame) for (uint i = 0; i < options_to_load; i++) { const SettingDesc *sd = &_settings[i]; /* Skip deprecated options */ - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) continue; void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save); Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i])); } @@ -1839,7 +1839,7 @@ CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uin const SettingDesc *sd = GetSettingDescription(p1); if (sd == NULL) return CMD_ERROR; - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) return CMD_ERROR; if (!sd->IsEditable(true)) return CMD_ERROR; @@ -2050,13 +2050,13 @@ const SettingDesc *GetSettingFromName(const char *name, uint *i) /* First check all full names */ for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) continue; if (strcmp(sd->desc.name, name) == 0) return sd; } /* Then check the shortcut variant of the name. */ for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) continue; const char *short_name = strchr(sd->desc.name, '.'); if (short_name != NULL) { short_name++; @@ -2067,7 +2067,7 @@ const SettingDesc *GetSettingFromName(const char *name, uint *i) if (strncmp(name, "company.", 8) == 0) name += 8; /* And finally the company-based settings */ for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) continue; if (strcmp(sd->desc.name, name) == 0) return sd; } @@ -2161,7 +2161,7 @@ void IConsoleListSettings(const char *prefilter) IConsolePrintF(CC_WARNING, "All settings with their current value:"); for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) { - if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; + if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to, sd->save.ext_feature_test)) continue; if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue; char value[80]; const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save); diff --git a/src/table/company_settings.ini b/src/table/company_settings.ini index 71b95cc393..615e808cb3 100644 --- a/src/table/company_settings.ini +++ b/src/table/company_settings.ini @@ -18,8 +18,8 @@ static const SettingDesc _company_settings[] = { [post-amble] }; [templates] -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDT_END = SDT_END() [defaults] @@ -34,6 +34,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/currency_settings.ini b/src/table/currency_settings.ini index dede8b0fb7..e38349f3df 100644 --- a/src/table/currency_settings.ini +++ b/src/table/currency_settings.ini @@ -11,9 +11,9 @@ static const SettingDesc _currency_settings[] = { [post-amble] }; [templates] -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_CHR = SDT_CHR($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_CHR = SDT_CHR($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDT_END = SDT_END() [defaults] @@ -28,6 +28,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/gameopt_settings.ini b/src/table/gameopt_settings.ini index 3a47c09e33..593d849ab8 100644 --- a/src/table/gameopt_settings.ini +++ b/src/table/gameopt_settings.ini @@ -41,13 +41,13 @@ static const SettingDesc _gameopt_settings[] = { [post-amble] }; [templates] -SDTG_GENERAL = SDTG_GENERAL($name, $sdt_cmd, $sle_cmd, $type, $flags, $guiflags, $var, $length, $def, $min, $max, $interval, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_NULL = SDT_NULL($length, $from, $to), -SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDTG_GENERAL = SDTG_GENERAL($name, $sdt_cmd, $sle_cmd, $type, $flags, $guiflags, $var, $length, $def, $min, $max, $interval, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_NULL = SDT_NULL($length, $from, $to, $extver), +SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDT_END = SDT_END() [defaults] @@ -62,6 +62,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini index 52ca2d16ef..26620318e6 100644 --- a/src/table/misc_settings.ini +++ b/src/table/misc_settings.ini @@ -15,12 +15,12 @@ static const SettingDescGlobVarList _misc_settings[] = { [post-amble] }; [templates] -SDTG_LIST = SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_STR = SDTG_STR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDTG_LIST = SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_STR = SDTG_STR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDTG_END = SDTG_END() [defaults] @@ -35,6 +35,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/settings.h.preamble b/src/table/settings.h.preamble index 33345bb713..7c0ca8a972 100644 --- a/src/table/settings.h.preamble +++ b/src/table/settings.h.preamble @@ -61,76 +61,76 @@ static size_t ConvertLandscape(const char *value); /* Macros for various objects to go in the configuration file. * This section is for global variables */ -#define SDTG_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, var, length, def, min, max, interval, full, str, strhelp, strval, proc, from, to, cat)\ - {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, NULL, cat), SLEG_GENERAL(sle_cmd, var, type | flags, length, from, to)} +#define SDTG_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, var, length, def, min, max, interval, full, str, strhelp, strval, proc, from, to, cat, extver)\ + {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, NULL, cat), SLEG_GENERAL_X(sle_cmd, var, type | flags, length, from, to, extver)} -#define SDTG_VAR(name, type, flags, guiflags, var, def, min, max, interval, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_NUMX, SL_VAR, type, flags, guiflags, var, 0, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTG_VAR(name, type, flags, guiflags, var, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_NUMX, SL_VAR, type, flags, guiflags, var, 0, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_BOOL(name, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, var, 0, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTG_BOOL(name, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, var, 0, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_LIST(name, type, length, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_INTLIST, SL_ARR, type, flags, guiflags, var, length, def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTG_LIST(name, type, length, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_INTLIST, SL_ARR, type, flags, guiflags, var, length, def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_STR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_STRING, SL_STR, type, flags, guiflags, var, lengthof(var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTG_STR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_STRING, SL_STR, type, flags, guiflags, var, lengthof(var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat) +#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(name, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, 0, 0, full, str, strhelp, strval, proc, from, to, cat) +#define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(name, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, 0, 0, full, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTG_NULL(length, from, to)\ - {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_NULL(length, from, to)} +#define SDTG_NULL(length, from, to, extver)\ + {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_NULL_X(length, from, to, extver)} #define SDTG_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_END()} /* Macros for various objects to go in the configuration file. * This section is for structures where their various members are saved */ -#define SDT_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, base, var, length, def, min, max, interval, full, str, strhelp, strval, proc, load, from, to, cat)\ - {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, load, cat), SLE_GENERAL(sle_cmd, base, var, type | flags, length, from, to)} +#define SDT_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, base, var, length, def, min, max, interval, full, str, strhelp, strval, proc, load, from, to, cat, extver)\ + {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, load, cat), SLE_GENERAL_X(sle_cmd, base, var, type | flags, length, from, to, extver)} -#define SDT_VAR(base, var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat)\ - SDT_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, base, var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_VAR(base, var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, base, var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_BOOL(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDT_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, base, var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_BOOL(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, base, var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_LIST(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDT_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_LIST(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_STR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDT_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_STR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_CHR(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDT_GENERAL(#var, SDT_STRING, SL_VAR, SLE_CHAR, flags, guiflags, base, var, 1, def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_CHR(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_STRING, SL_VAR, SLE_CHAR, flags, guiflags, base, var, 1, def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat)\ - SDT_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, load, from, to, cat) +#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extver)\ + SDT_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, load, from, to, cat, extver) -#define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat)\ - SDT_GENERAL(#var, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, 0, 0, full, str, strhelp, strval, proc, NULL, from, to, cat) +#define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat, extver)\ + SDT_GENERAL(#var, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, 0, 0, full, str, strhelp, strval, proc, NULL, from, to, cat, extver) -#define SDT_NULL(length, from, to)\ - {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_CONDNULL(length, from, to)} +#define SDT_NULL(length, from, to, extver)\ + {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_CONDNULL_X(length, from, to, extver)} -#define SDTC_VAR(var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTC_VAR(var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTC_BOOL(var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, _settings_client.var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTC_BOOL(var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, _settings_client.var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTC_LIST(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTC_LIST(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTC_STR(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat) +#define SDTC_STR(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) -#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat)\ - SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat) +#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extver)\ + SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver) #define SDT_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_END()} diff --git a/src/table/settings.ini b/src/table/settings.ini index f314f21e92..3256e20dab 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -64,19 +64,19 @@ const SettingDesc _settings[] = { [post-amble] }; [templates] -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTC_STR = SDTC_STR( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat), -SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_NULL = SDT_NULL($length, $from, $to), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_STR = SDTC_STR( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver), +SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_NULL = SDT_NULL($length, $from, $to, $extver), SDT_END = SDT_END() [defaults] @@ -91,6 +91,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/win32_settings.ini b/src/table/win32_settings.ini index 1e0c9ad023..71b8499d2b 100644 --- a/src/table/win32_settings.ini +++ b/src/table/win32_settings.ini @@ -17,8 +17,8 @@ static const SettingDescGlobVarList _win32_settings[] = { }; #endif /* WIN32 */ [templates] -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDTG_END = SDTG_END() [defaults] @@ -33,6 +33,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} diff --git a/src/table/window_settings.ini b/src/table/window_settings.ini index ad77423d9d..cfa2f76d5a 100644 --- a/src/table/window_settings.ini +++ b/src/table/window_settings.ini @@ -12,8 +12,8 @@ static const SettingDesc _window_settings[] = { [post-amble] }; [templates] -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), SDT_END = SDT_END() [defaults] @@ -29,6 +29,7 @@ load = NULL from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED +extver = {} From 4508cfbf9383a076d4225f108b175c804be2f222 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 31 Jul 2015 22:23:36 +0100 Subject: [PATCH 02/11] Add format-style versions of SlError and SlErrorCorrupt. --- src/saveload/saveload.cpp | 39 ++++++++++++++++++++++++++++++++++----- src/saveload/saveload.h | 8 ++++++-- src/string.cpp | 18 ++++++++++++------ src/string_func.h | 1 + 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 06836855da..dc611adf26 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -534,17 +534,22 @@ static void SlNullPointers() * @note This function does never return as it throws an exception to * break out of all the saveload code. */ -void NORETURN SlError(StringID string, const char *extra_msg) +void NORETURN SlError(StringID string, const char *extra_msg, bool already_malloced) { + char *str = NULL; + if (extra_msg != NULL) { + str = already_malloced ? const_cast(extra_msg) : stredup(extra_msg); + } + /* Distinguish between loading into _load_check_data vs. normal save/load. */ if (_sl.action == SLA_LOAD_CHECK) { _load_check_data.error = string; free(_load_check_data.error_data); - _load_check_data.error_data = (extra_msg == NULL) ? NULL : stredup(extra_msg); + _load_check_data.error_data = str; } else { _sl.error_str = string; free(_sl.extra_msg); - _sl.extra_msg = (extra_msg == NULL) ? NULL : stredup(extra_msg); + _sl.extra_msg = str; } /* We have to NULL all pointers here; we might be in a state where @@ -555,6 +560,18 @@ void NORETURN SlError(StringID string, const char *extra_msg) throw std::exception(); } +/** + * As SlError, except that it takes a format string and additional parameters + */ +void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...) +{ + va_list va; + va_start(va, msg); + char *str = str_vfmt(msg, va); + va_end(va); + SlError(string, str, true); +} + /** * Error handler for corrupt savegames. Sets everything up to show the * error message and to clean up the mess of a partial savegame load. @@ -562,9 +579,21 @@ void NORETURN SlError(StringID string, const char *extra_msg) * @note This function does never return as it throws an exception to * break out of all the saveload code. */ -void NORETURN SlErrorCorrupt(const char *msg) +void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced) +{ + SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg, already_malloced); +} + +/** + * As SlErrorCorruptFmt, except that it takes a format string and additional parameters + */ +void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...) { - SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg); + va_list va; + va_start(va, msg); + char *str = str_vfmt(msg, va); + va_end(va); + SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, str, true); } diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 230203b4e7..9c1d9615f5 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -16,6 +16,8 @@ #include "../strings_type.h" #include "extended_ver_sl.h" +#include + /** Save or load result codes. */ enum SaveOrLoadResult { SL_OK = 0, ///< completed successfully @@ -570,8 +572,10 @@ void SlGlobList(const SaveLoadGlobVarList *sldg); void SlArray(void *array, size_t length, VarType conv); void SlObject(void *object, const SaveLoad *sld); bool SlObjectMember(void *object, const SaveLoad *sld); -void NORETURN SlError(StringID string, const char *extra_msg = NULL); -void NORETURN SlErrorCorrupt(const char *msg); +void NORETURN SlError(StringID string, const char *extra_msg = NULL, bool already_malloced = false); +void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced = false); +void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...) WARN_FORMAT(2, 3); +void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...) WARN_FORMAT(1, 2); bool SaveloadCrashWithMissingNewGRFs(); diff --git a/src/string.cpp b/src/string.cpp index 6bc9319c1c..4bcd0cf805 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -131,6 +131,16 @@ char *stredup(const char *s, const char *last) return tmp; } +char *str_vfmt(const char *str, va_list va) +{ + char buf[4096]; + + int len = vseprintf(buf, lastof(buf), str, va); + char *p = MallocT(len + 1); + memcpy(p, buf, len + 1); + return p; +} + /** * Format, "printf", into a newly allocated string. * @param str The formatting string. @@ -138,15 +148,11 @@ char *stredup(const char *s, const char *last) */ char *CDECL str_fmt(const char *str, ...) { - char buf[4096]; va_list va; - va_start(va, str); - int len = vseprintf(buf, lastof(buf), str, va); + char *output = str_vfmt(str, va); va_end(va); - char *p = MallocT(len + 1); - memcpy(p, buf, len + 1); - return p; + return output; } /** diff --git a/src/string_func.h b/src/string_func.h index ff12f3747a..dd9b42600d 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -39,6 +39,7 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FO int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap); char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2); +char *str_vfmt(const char *str, va_list ap); void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK); void ValidateString(const char *str); From a3980dc6baa9b042de85ecbcb900e2e0868a0997 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 1 Aug 2015 12:20:28 +0100 Subject: [PATCH 03/11] Move SlRead/SlWrite functions to saveload.h Add SlGetBytesRead and SlGetBytesWritten functions. --- src/saveload/saveload.cpp | 51 +++++++++------------------------------ src/saveload/saveload.h | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index dc611adf26..c7abaf50dc 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -649,51 +649,24 @@ void SlWriteByte(byte b) _sl.dumper->WriteByte(b); } -static inline int SlReadUint16() -{ - int x = SlReadByte() << 8; - return x | SlReadByte(); -} - -static inline uint32 SlReadUint32() -{ - uint32 x = SlReadUint16() << 16; - return x | SlReadUint16(); -} - -static inline uint64 SlReadUint64() -{ - uint32 x = SlReadUint32(); - uint32 y = SlReadUint32(); - return (uint64)x << 32 | y; -} - -static inline void SlWriteUint16(uint16 v) -{ - SlWriteByte(GB(v, 8, 8)); - SlWriteByte(GB(v, 0, 8)); -} - -static inline void SlWriteUint32(uint32 v) -{ - SlWriteUint16(GB(v, 16, 16)); - SlWriteUint16(GB(v, 0, 16)); -} - -static inline void SlWriteUint64(uint64 x) +/** + * Returns number of bytes read so far + * May only be called during a load/load check action + */ +size_t SlGetBytesRead() { - SlWriteUint32((uint32)(x >> 32)); - SlWriteUint32((uint32)x); + assert(_sl.action == SLA_LOAD || _sl.action == SLA_LOAD_CHECK); + return _sl.reader->GetSize(); } /** - * Read in bytes from the file/data structure but don't do - * anything with them, discarding them in effect - * @param length The amount of bytes that is being treated this way + * Returns number of bytes written so far + * May only be called during a save action */ -static inline void SlSkipBytes(size_t length) +size_t SlGetBytesWritten() { - for (; length != 0; length--) SlReadByte(); + assert(_sl.action == SLA_SAVE); + return _sl.dumper->GetSize(); } /** diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 9c1d9615f5..b0da631d97 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -568,6 +568,56 @@ size_t SlCalcObjLength(const void *object, const SaveLoad *sld); byte SlReadByte(); void SlWriteByte(byte b); +static inline int SlReadUint16() +{ + int x = SlReadByte() << 8; + return x | SlReadByte(); +} + +static inline uint32 SlReadUint32() +{ + uint32 x = SlReadUint16() << 16; + return x | SlReadUint16(); +} + +static inline uint64 SlReadUint64() +{ + uint32 x = SlReadUint32(); + uint32 y = SlReadUint32(); + return (uint64)x << 32 | y; +} + +static inline void SlWriteUint16(uint16 v) +{ + SlWriteByte(GB(v, 8, 8)); + SlWriteByte(GB(v, 0, 8)); +} + +static inline void SlWriteUint32(uint32 v) +{ + SlWriteUint16(GB(v, 16, 16)); + SlWriteUint16(GB(v, 0, 16)); +} + +static inline void SlWriteUint64(uint64 x) +{ + SlWriteUint32((uint32)(x >> 32)); + SlWriteUint32((uint32)x); +} + +/** + * Read in bytes from the file/data structure but don't do + * anything with them, discarding them in effect + * @param length The amount of bytes that is being treated this way + */ +static inline void SlSkipBytes(size_t length) +{ + for (; length != 0; length--) SlReadByte(); +} + +size_t SlGetBytesRead(); +size_t SlGetBytesWritten(); + void SlGlobList(const SaveLoadGlobVarList *sldg); void SlArray(void *array, size_t length, VarType conv); void SlObject(void *object, const SaveLoad *sld); From bde094fe018f1d6765e4d517fe4afade0d78c7c6 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 1 Aug 2015 12:24:45 +0100 Subject: [PATCH 04/11] Initial implementation of SLXI chunk save/load --- src/saveload/extended_ver_sl.cpp | 249 ++++++++++++++++++++++++++++++- src/saveload/extended_ver_sl.h | 44 +++++- src/saveload/saveload.cpp | 9 +- src/saveload/saveload.h | 32 ++-- 4 files changed, 299 insertions(+), 35 deletions(-) diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp index 143167d6ed..478e6d2f34 100644 --- a/src/saveload/extended_ver_sl.cpp +++ b/src/saveload/extended_ver_sl.cpp @@ -7,17 +7,47 @@ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . */ -/** @file extended_ver_sl.cpp Functions related to handling save/load extended version info. */ +/** @file extended_ver_sl.cpp Functions related to handling save/load extended version info. + * + * Known extended features are stored in _sl_xv_feature_versions, features which are currently enabled/in use and their versions are stored in the savegame. + * On load, the list of features and their versions are loaded from the savegame. If the savegame contains a feature which is either unknown, or has too high a version, + * loading can be either aborted, or the feature can be ignored if the the feature flags in the savegame indicate that it can be ignored. The savegame may also list any additional + * chunk IDs which are associated with an extended feature, these can be discarded if the feature is discarded. + * This information is stored in the SLXI chunk, the contents of which has the following format: + * + * uint32 chunk version + * uint32 chunk flags + * uint32 number of sub chunks/features + * For each of N sub chunk/feature: + * uint32 feature flags (SlxiSubChunkFlags) + * uint16 feature version + * SLE_STR feature name + * uint32* extra data length [only present iff feature flags & XSCF_EXTRA_DATA_PRESENT] + * N bytes extra data + * uint32* chunk ID list count [only present iff feature flags & XSCF_CHUNK_ID_LIST_PRESENT] + * N x uint32 chunk ID list + */ #include "../stdafx.h" #include "../debug.h" +#include "saveload.h" #include "extended_ver_sl.h" +#include + #include "../safeguards.h" uint16 _sl_xv_feature_versions[XSLFI_SIZE]; ///< array of all known feature types and their current versions +bool _sl_is_ext_version; ///< is this an extended savegame version, with more info in the SLXI chunk? +bool _sl_is_faked_ext; ///< is this a faked extended savegame version, with no SLXI chunk? std::vector _sl_xv_discardable_chunk_ids; ///< list of chunks IDs which we can discard if no chunk loader exists +static const uint32 _sl_xv_slxi_chunk_version = 0; ///< current version os SLXI chunk + +const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = { + { XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker +}; + /** * Extended save/load feature test * @@ -26,16 +56,16 @@ std::vector _sl_xv_discardable_chunk_ids; ///< list of chunks * and return the combination of the two tests using the operator defined in the constructor. * Otherwise just returns the result of the savegame version test */ -bool ExtendedSaveLoadFeatureTest::IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const +bool SlXvFeatureTest::IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const { bool savegame_version_ok = savegame_version >= savegame_version_from && savegame_version <= savegame_version_to; - ExtendedSaveLoadFeatureIndex feature = static_cast(GB(this->value, 0, 16)); + SlXvFeatureIndex feature = static_cast(GB(this->value, 0, 16)); if (feature == XSLFI_NULL) return savegame_version_ok; uint16 min_version = GB(this->value, 16, 16); uint16 max_version = GB(this->value, 32, 16); - ExtendedSaveLoadFeatureTestOperator op = static_cast(GB(this->value, 48, 16)); + SlXvFeatureTestOperator op = static_cast(GB(this->value, 48, 16)); bool feature_ok = SlXvIsFeaturePresent(feature, min_version, max_version); switch (op) { @@ -54,7 +84,7 @@ bool ExtendedSaveLoadFeatureTest::IsFeaturePresent(uint16 savegame_version, uint /** * Returns true if @p feature is present and has a version inclusively bounded by @p min_version and @p max_version */ -bool SlXvIsFeaturePresent(ExtendedSaveLoadFeatureIndex feature, uint16 min_version, uint16 max_version) +bool SlXvIsFeaturePresent(SlXvFeatureIndex feature, uint16 min_version, uint16 max_version) { assert(feature < XSLFI_SIZE); return _sl_xv_feature_versions[feature] >= min_version && _sl_xv_feature_versions[feature] <= max_version; @@ -65,6 +95,8 @@ bool SlXvIsFeaturePresent(ExtendedSaveLoadFeatureIndex feature, uint16 min_versi */ void SlXvResetState() { + _sl_is_ext_version = false; + _sl_is_faked_ext = false; memset(_sl_xv_feature_versions, 0, sizeof(_sl_xv_feature_versions)); } @@ -73,8 +105,6 @@ void SlXvResetState() */ void SlXvSetCurrentState() { - extern bool _sl_is_ext_version; - SlXvResetState(); _sl_is_ext_version = true; @@ -103,3 +133,208 @@ bool SlXvIsChunkDiscardable(uint32 id) } return false; } + +/** + * Writes a chunk ID list string to the savegame, returns the number of chunks written + * In dry run mode, only returns the number of chunk which would have been written + */ +static uint32 WriteChunkIdList(const char *chunk_list, bool dry_run) +{ + unsigned int chunk_count = 0; // number of chunks output + unsigned int id_offset = 0; // how far are we into the ID + for (; *chunk_list != 0; chunk_list++) { + if (id_offset == 4) { + assert(*chunk_list == ','); + id_offset = 0; + } else { + if (!dry_run) { + SlWriteByte(*chunk_list); + } + if (id_offset == 3) { + chunk_count++; + } + id_offset++; + } + } + assert(id_offset == 4); + return chunk_count; +} + +static void Save_SLXI() +{ + SlXvSetCurrentState(); + + static const SaveLoad _xlsi_sub_chunk_desc[] = { + SLE_VAR(SlxiSubChunkInfo, save_version, SLE_UINT16), + SLE_STR(SlxiSubChunkInfo, name, SLE_STR, 0), + SLE_END() + }; + + // calculate lengths + uint32 item_count = 0; + uint32 length = 12; + std::vector extra_data_lengths; + std::vector chunk_counts; + extra_data_lengths.resize(XSLFI_SIZE); + chunk_counts.resize(XSLFI_SIZE); + const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos; + for (; info->index != XSLFI_NULL; ++info) { + if (info->save_version > 0) { + item_count++; + length += 4; + length += SlCalcObjLength(info, _xlsi_sub_chunk_desc); + if (info->save_proc) { + uint32 extra_data_length = info->save_proc(info, true); + if (extra_data_length) { + extra_data_lengths[info->index] = extra_data_length; + length += 4 + extra_data_length; + } + } + if (info->chunk_list) { + uint32 chunk_count = WriteChunkIdList(info->chunk_list, true); + if (chunk_count) { + chunk_counts[info->index] = chunk_count; + length += 4 * (1 + chunk_count); + } + } + } + } + + // write header + SlSetLength(length); + SlWriteUint32(_sl_xv_slxi_chunk_version); // chunk version + SlWriteUint32(0); // flags + SlWriteUint32(item_count); // item count + + // write data + info = _sl_xv_sub_chunk_infos; + for (; info->index != XSLFI_NULL; ++info) { + if (info->save_version > 0) { + SlxiSubChunkFlags flags = info->flags; + assert(!(flags & (XSCF_EXTRA_DATA_PRESENT | XSCF_CHUNK_ID_LIST_PRESENT))); + uint32 extra_data_length = extra_data_lengths[info->index]; + uint32 chunk_count = chunk_counts[info->index]; + if (extra_data_length > 0) flags |= XSCF_EXTRA_DATA_PRESENT; + if (chunk_count > 0) flags |= XSCF_CHUNK_ID_LIST_PRESENT; + SlWriteUint32(flags); + SlObject(const_cast(info), _xlsi_sub_chunk_desc); + + if (extra_data_length > 0) { + SlWriteUint32(extra_data_length); + size_t written = SlGetBytesWritten(); + info->save_proc(info, false); + assert(SlGetBytesWritten() == written + extra_data_length); + } + if (chunk_count > 0) { + SlWriteUint32(chunk_count); + size_t written = SlGetBytesWritten(); + WriteChunkIdList(info->chunk_list, false); + assert(SlGetBytesWritten() == written + (chunk_count * 4)); + } + } + } +} + +static void Load_SLXI() +{ + if (_sl_is_faked_ext || !_sl_is_ext_version) { + SlErrorCorrupt("SXLI chunk is unexpectedly present"); + } + + SlXvResetState(); + _sl_is_ext_version = true; + + uint32 version = SlReadUint32(); + if (version > _sl_xv_slxi_chunk_version) SlErrorCorruptFmt("SLXI chunk: version: %u is too new (expected max: %u)", version, _sl_xv_slxi_chunk_version); + + uint32 chunk_flags = SlReadUint32(); + // flags are not in use yet, reserve for future expansion + if (chunk_flags != 0) SlErrorCorruptFmt("SLXI chunk: unknown chunk header flags: 0x%X", chunk_flags); + + char name_buffer[256]; + const SaveLoadGlobVarList xlsi_sub_chunk_name_desc[] = { + SLEG_STR(name_buffer, SLE_STRB), + SLEG_END() + }; + + uint32 item_count = SlReadUint32(); + for (uint32 i = 0; i < item_count; i++) { + SlxiSubChunkFlags flags = static_cast(SlReadUint32()); + uint16 version = SlReadUint16(); + SlGlobList(xlsi_sub_chunk_name_desc); + + // linearly scan through feature list until found name match + bool found = false; + const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos; + for (; info->index != XSLFI_NULL; ++info) { + if (strcmp(name_buffer, info->name) == 0) { + found = true; + break; + } + } + + bool discard_chunks = false; + if (found) { + if (version > info->max_version) { + if (flags & XSCF_IGNORABLE_VERSION) { + // version too large but carry on regardless + discard_chunks = true; + if (flags & XSCF_EXTRA_DATA_PRESENT) { + SlSkipBytes(SlReadUint32()); // skip extra data field + } + DEBUG(sl, 1, "SLXI chunk: too large version for feature: '%s', version: %d, max version: %d, ignoring", name_buffer, version, info->max_version); + } else { + SlErrorCorruptFmt("SLXI chunk: too large version for feature: '%s', version: %d, max version: %d", name_buffer, version, info->max_version); + } + } else { + // success path :) + + _sl_xv_feature_versions[info->index] = version; + if (flags & XSCF_EXTRA_DATA_PRESENT) { + uint32 extra_data_size = SlReadUint32(); + if (extra_data_size) { + if (info->load_proc) { + size_t read = SlGetBytesRead(); + info->load_proc(info, extra_data_size); + if (SlGetBytesRead() != read + extra_data_size) { + SlErrorCorruptFmt("SLXI chunk: feature: %s, version: %d, extra data length mismatch", name_buffer, version); + } + } else { + SlErrorCorruptFmt("SLXI chunk: feature: %s, version: %d, unexpectedly includes extra data", name_buffer, version); + } + } + } + + DEBUG(sl, 1, "SLXI chunk: found known feature: '%s', version: %d, max version: %d", name_buffer, version, info->max_version); + } + } else { + if (flags & XSCF_IGNORABLE_UNKNOWN) { + // not found but carry on regardless + discard_chunks = true; + if (flags & XSCF_EXTRA_DATA_PRESENT) { + SlSkipBytes(SlReadUint32()); // skip extra data field + } + DEBUG(sl, 1, "SLXI chunk: unknown feature: '%s', version: %d, ignoring", name_buffer, version); + } else { + SlErrorCorruptFmt("SLXI chunk: unknown feature: %s, version: %d", name_buffer, version); + } + } + + // at this point the extra data field should have been consumed + // handle chunk ID list field + if (flags & XSCF_CHUNK_ID_LIST_PRESENT) { + uint32 chunk_count = SlReadUint32(); + for (uint32 j = 0; j < chunk_count; j++) { + uint32 chunk_id = SlReadUint32(); + if (discard_chunks) { + _sl_xv_discardable_chunk_ids.push_back(chunk_id); + DEBUG(sl, 2, "SLXI chunk: unknown feature: '%s', discarding chunk: %c%c%c%c", name_buffer, chunk_id >> 24, chunk_id >> 16, chunk_id >> 8, chunk_id); + } + } + } + } +} + +extern const ChunkHandler _version_ext_chunk_handlers[] = { + { 'SLXI', Save_SLXI, Load_SLXI, NULL, Load_SLXI, CH_RIFF | CH_LAST}, +}; diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h index 96b0feddec..a7e9496944 100644 --- a/src/saveload/extended_ver_sl.h +++ b/src/saveload/extended_ver_sl.h @@ -19,7 +19,7 @@ /** * List of extended features, each feature has its own (16 bit) version */ -enum ExtendedSaveLoadFeatureIndex { +enum SlXvFeatureIndex { XSLFI_NULL = 0, ///< Unused value, to indicate that no extended feature test is in use XSLFI_SIZE, ///< Total count of features, including null feature @@ -30,7 +30,7 @@ extern uint16 _sl_xv_feature_versions[XSLFI_SIZE]; /** * Operator to use when combining traditional savegame number test with an extended feature version test */ -enum ExtendedSaveLoadFeatureTestOperator { +enum SlXvFeatureTestOperator { XSLFTO_OR = 0, ///< Test if traditional savegame version is in bounds OR extended feature is in version bounds XSLFTO_AND ///< Test if traditional savegame version is in bounds AND extended feature is in version bounds }; @@ -38,15 +38,15 @@ enum ExtendedSaveLoadFeatureTestOperator { /** * Structure to describe an extended feature version test, and how it combines with a traditional savegame version test */ -struct ExtendedSaveLoadFeatureTest { +struct SlXvFeatureTest { private: uint64 value; public: - ExtendedSaveLoadFeatureTest() + SlXvFeatureTest() : value(0) { } - ExtendedSaveLoadFeatureTest(ExtendedSaveLoadFeatureTestOperator op, ExtendedSaveLoadFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF) + SlXvFeatureTest(SlXvFeatureTestOperator op, SlXvFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF) { this->value = 0; SB(this->value, 0, 16, feature); @@ -58,16 +58,46 @@ struct ExtendedSaveLoadFeatureTest { bool IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const; }; -bool SlXvIsFeaturePresent(ExtendedSaveLoadFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF); +bool SlXvIsFeaturePresent(SlXvFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF); /** * Returns true if @p feature is missing (i.e. has a version of 0) */ -inline bool SlXvIsFeatureMissing(ExtendedSaveLoadFeatureIndex feature) +inline bool SlXvIsFeatureMissing(SlXvFeatureIndex feature) { return !SlXvIsFeaturePresent(feature); } +/** + * sub chunk flags, this is saved as-is + * (XSCF_EXTRA_DATA_PRESENT and XSCF_CHUNK_ID_LIST_PRESENT must only be set by the save code, and read by the load code) + */ +enum SlxiSubChunkFlags { + XSCF_NULL = 0, ///< zero value + XSCF_IGNORABLE_UNKNOWN = 1 << 0, ///< the loader is free to ignore this without aborting the load if it doesn't know what it is at all + XSCF_IGNORABLE_VERSION = 1 << 1, ///< the loader is free to ignore this without aborting the load if the version is greater than the maximum that can be loaded + XSCF_EXTRA_DATA_PRESENT = 1 << 2, ///< extra data field is present, extra data in some sub-chunk/feature specific format, not used for anything yet + XSCF_CHUNK_ID_LIST_PRESENT = 1 << 3, ///< chunk ID list field is present, list of chunks which this sub-chunk/feature adds to the save game, this can be used to discard the chunks if the feature is unknown +}; +DECLARE_ENUM_AS_BIT_SET(SlxiSubChunkFlags) + +struct SlxiSubChunkInfo; + +typedef uint32 SlxiSubChunkSaveProc(const SlxiSubChunkInfo *info, bool dry_run); ///< sub chunk save procedure type, must return length and write no data when dry_run is true +typedef void SlxiSubChunkLoadProc(const SlxiSubChunkInfo *info, uint32 length); ///< sub chunk load procedure, must consume length bytes + +/** Handlers and description of chunk. */ +struct SlxiSubChunkInfo { + SlXvFeatureIndex index; ///< feature index, this is saved + SlxiSubChunkFlags flags; ///< flags, this is saved + uint16 save_version; ///< version to save + uint16 max_version; ///< maximum version to accept on load + const char *name; ///< feature name, this *IS* saved, so must be globally unique + SlxiSubChunkSaveProc *save_proc; ///< save procedure of the sub chunk, this may be NULL in which case no extra chunk data is saved + SlxiSubChunkLoadProc *load_proc; ///< load procedure of the sub chunk, this may be NULL in which case the extra chunk data must be missing or of 0 length + const char *chunk_list; ///< this is a list of chunks that this feature uses, which should be written to the savegame, this must be a comma-seperated list of 4-character IDs, with no spaces, or NULL +}; + void SlXvResetState(); void SlXvSetCurrentState(); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index c7abaf50dc..b369af5627 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -274,10 +274,11 @@ SavegameType _savegame_type; ///< type of savegame we are loading uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) uint16 _sl_version; ///< the major savegame version identifier byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! -bool _sl_is_ext_version; ///< is this an extended savegame version, with more info in the SLXI chunk? char _savegame_format[8]; ///< how to compress savegames bool _do_autosave; ///< are we doing an autosave at the moment? +extern bool _sl_is_ext_version; + /** What are we currently doing? */ enum SaveLoadAction { SLA_LOAD, ///< loading @@ -419,7 +420,7 @@ struct SaveLoadParams { static SaveLoadParams _sl; ///< Parameters used for/at saveload. /* these define the chunks */ -//extern const ChunkHandler _version_ext_chunk_handlers[]; +extern const ChunkHandler _version_ext_chunk_handlers[]; extern const ChunkHandler _gamelog_chunk_handlers[]; extern const ChunkHandler _map_chunk_handlers[]; extern const ChunkHandler _misc_chunk_handlers[]; @@ -456,7 +457,7 @@ extern const ChunkHandler _persistent_storage_chunk_handlers[]; /** Array of all chunks in a savegame, \c NULL terminated. */ static const ChunkHandler * const _chunk_handlers[] = { -// _version_ext_chunk_handlers, // this should be first, such that it is saved first, as when loading it affects the loading of subsequent chunks + _version_ext_chunk_handlers, // this should be first, such that it is saved first, as when loading it affects the loading of subsequent chunks _gamelog_chunk_handlers, _map_chunk_handlers, _misc_chunk_handlers, @@ -2662,7 +2663,6 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check) _sl.lf->Reset(); _sl_version = 0; _sl_minor_version = 0; - _sl_is_ext_version = false; SlXvResetState(); /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */ @@ -2830,7 +2830,6 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo if (!LoadOldSaveGame(filename)) return SL_REINIT; _sl_version = 0; _sl_minor_version = 0; - _sl_is_ext_version = false; SlXvResetState(); GamelogStartAction(GLAT_LOAD); if (!AfterLoadGame()) { diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index b0da631d97..711d9ac7ac 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -216,7 +216,7 @@ struct SaveLoad { * that is called to save it. address: global=true, offset: global=false */ void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536) size_t size; ///< the sizeof size. - ExtendedSaveLoadFeatureTest ext_feature_test; ///< extended feature test + SlXvFeatureTest ext_feature_test; ///< extended feature test }; /** Same as #SaveLoad but global variables are used (for better readability); */ @@ -230,7 +230,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field * @note In general, it is better to use one of the SLE_* macros below. */ #define SLE_GENERAL_X(cmd, base, variable, type, length, from, to, extver) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable), cpp_sizeof(base, variable), extver} @@ -243,7 +243,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLE_CONDVAR_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_VAR, base, variable, type, 0, from, to, extver) #define SLE_CONDVAR(base, variable, type, from, to) SLE_CONDVAR_X(base, variable, type, from, to, {}) @@ -255,7 +255,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Type of the reference, a value from #SLRefType. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLE_CONDREF_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_REF, base, variable, type, 0, from, to, extver) #define SLE_CONDREF(base, variable, type, from, to) SLE_CONDREF_X(base, variable, type, from, to, {}) @@ -268,7 +268,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the array. * @param from First savegame version that has the array. * @param to Last savegame version that has the array. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLE_CONDARR_X(base, variable, type, length, from, to, extver) SLE_GENERAL_X(SL_ARR, base, variable, type, length, from, to, extver) #define SLE_CONDARR(base, variable, type, length, from, to) SLE_CONDARR_X(base, variable, type, length, from, to, {}) @@ -281,7 +281,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the string (only used for fixed size buffers). * @param from First savegame version that has the string. * @param to Last savegame version that has the string. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLE_CONDSTR_X(base, variable, type, length, from, to, extver) SLE_GENERAL_X(SL_STR, base, variable, type, length, from, to, extver) #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_CONDSTR_X(base, variable, type, length, from, to, {}) @@ -293,7 +293,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the list. * @param to Last savegame version that has the list. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLE_CONDLST_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_LST, base, variable, type, 0, from, to, extver) #define SLE_CONDLST(base, variable, type, from, to) SLE_CONDLST_X(base, variable, type, from, to, {}) @@ -351,7 +351,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Length of the empty space. * @param from First savegame version that has the empty space. * @param to Last savegame version that has the empty space. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have empty space + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have empty space */ #define SLE_CONDNULL_X(length, from, to, extver) SLE_CONDARR_X(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, extver) #define SLE_CONDNULL(length, from, to) SLE_CONDNULL_X(length, from, to, {}) @@ -372,7 +372,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field * @note In general, it is better to use one of the SLEG_* macros below. */ #define SLEG_GENERAL_X(cmd, variable, type, length, from, to, extver) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable), extver} @@ -384,7 +384,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLEG_CONDVAR_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_VAR, variable, type, 0, from, to, extver) #define SLEG_CONDVAR(variable, type, from, to) SLEG_CONDVAR_X(variable, type, from, to, {}) @@ -395,7 +395,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the field. * @param to Last savegame version that has the field. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLEG_CONDREF_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_REF, variable, type, 0, from, to, extver) #define SLEG_CONDREF(variable, type, from, to) SLEG_CONDREF_X(variable, type, from, to, {}) @@ -407,7 +407,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the array. * @param from First savegame version that has the array. * @param to Last savegame version that has the array. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLEG_CONDARR_X(variable, type, length, from, to, extver) SLEG_GENERAL_X(SL_ARR, variable, type, length, from, to, extver) #define SLEG_CONDARR(variable, type, length, from, to) SLEG_CONDARR_X(variable, type, length, from, to, {}) @@ -419,7 +419,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Number of elements in the string (only used for fixed size buffers). * @param from First savegame version that has the string. * @param to Last savegame version that has the string. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLEG_CONDSTR_X(variable, type, length, from, to, extver) SLEG_GENERAL_X(SL_STR, variable, type, length, from, to, extver) #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_CONDSTR_X(variable, type, length, from, to, {}) @@ -430,7 +430,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param type Storage of the data in memory and in the savegame. * @param from First savegame version that has the list. * @param to Last savegame version that has the list. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have the field + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field */ #define SLEG_CONDLST_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_LST, variable, type, 0, from, to, extver) #define SLEG_CONDLST(variable, type, from, to) SLEG_CONDLST_X(variable, type, from, to, {}) @@ -475,7 +475,7 @@ typedef SaveLoad SaveLoadGlobVarList; * @param length Length of the empty space. * @param from First savegame version that has the empty space. * @param to Last savegame version that has the empty space. - * @param extver ExtendedSaveLoadFeatureTest to test (along with from and to) which savegames have empty space + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have empty space */ #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL, {}} @@ -502,7 +502,7 @@ static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0) * @param version_to Highest version number that falls within the range. * @return Active savegame version falls within the given range. */ -static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to, ExtendedSaveLoadFeatureTest ext_feature_test) +static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to, SlXvFeatureTest ext_feature_test) { extern const uint16 SAVEGAME_VERSION; if (!ext_feature_test.IsFeaturePresent(SAVEGAME_VERSION, version_from, version_to)) return false; From c57ffc64bcde78e238dc25545fbbe2a93c5e93f6 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 1 Aug 2015 12:19:18 +0100 Subject: [PATCH 05/11] Settings update: add PATX chunk to store additional settings in an unordered format which is tolerant of extra, missing or reordered settings. --- src/settings.cpp | 207 +++++++++++++++++++++++++++++++- src/settings_internal.h | 1 + src/table/company_settings.ini | 4 +- src/table/currency_settings.ini | 6 +- src/table/gameopt_settings.ini | 12 +- src/table/misc_settings.ini | 12 +- src/table/settings.h.preamble | 88 +++++++------- src/table/settings.ini | 25 ++-- src/table/win32_settings.ini | 4 +- src/table/window_settings.ini | 4 +- 10 files changed, 283 insertions(+), 80 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index 2ed2f6f26b..b8e329ef5c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -64,6 +64,8 @@ #include "roadveh.h" #include "fios.h" #include "strings_func.h" +#include "string_func.h" +#include "debug.h" #include "void_map.h" #include "station_base.h" @@ -71,6 +73,9 @@ #include "table/strings.h" #include "table/settings.h" +#include +#include + #include "safeguards.h" ClientSettings _settings_client; @@ -2180,7 +2185,7 @@ void IConsoleListSettings(const char *prefilter) } /** - * Save and load handler for settings + * Save and load handler for settings, except for those which go in the PATX chunk * @param osd SettingDesc struct containing all information * @param object can be either NULL in which case we load global variables or * a pointer to a struct which is getting saved @@ -2188,6 +2193,7 @@ void IConsoleListSettings(const char *prefilter) static void LoadSettings(const SettingDesc *osd, void *object) { for (; osd->save.cmd != SL_END; osd++) { + if (osd->patx_name != NULL) continue; const SaveLoad *sld = &osd->save; void *ptr = GetVariableAddress(object, sld); @@ -2197,7 +2203,7 @@ static void LoadSettings(const SettingDesc *osd, void *object) } /** - * Save and load handler for settings + * Save and load handler for settings, except for those which go in the PATX chunk * @param sd SettingDesc struct containing all information * @param object can be either NULL in which case we load global variables or * a pointer to a struct which is getting saved @@ -2209,16 +2215,195 @@ static void SaveSettings(const SettingDesc *sd, void *object) const SettingDesc *i; size_t length = 0; for (i = sd; i->save.cmd != SL_END; i++) { + if (i->patx_name != NULL) continue; length += SlCalcObjMemberLength(object, &i->save); } SlSetLength(length); for (i = sd; i->save.cmd != SL_END; i++) { + if (i->patx_name != NULL) continue; void *ptr = GetVariableAddress(object, &i->save); SlObjectMember(ptr, &i->save); } } +/** @file + * + * The PATX chunk stores additional settings in an unordered format + * which is tolerant of extra, missing or reordered settings. + * Additional settings generally means those that aren't in trunk. + * + * The PATX chunk contents has the following format: + * + * uint32 chunk flags + * uint32 number of settings + * For each of N settings: + * uint32 setting flags + * SLE_STR setting name + * uint32 length of setting field + * N bytes setting field + */ + +/** Sorted list of PATX settings, generated by MakeSettingsPatxList */ +static std::vector _sorted_patx_settings; + +/** + * Prepare a sorted list of settings to be potentially be loaded out of the PATX chunk + * This is to enable efficient lookup of settings by name + * This is stored in _sorted_patx_settings + */ +static void MakeSettingsPatxList(const SettingDesc *sd) +{ + static const SettingDesc *previous = NULL; + + if (sd == previous) return; + + _sorted_patx_settings.clear(); + for (const SettingDesc *desc = sd; desc->save.cmd != SL_END; desc++) { + if (desc->patx_name == NULL) continue; + _sorted_patx_settings.push_back(desc); + } + + // this makes me miss lambdas :/ + struct StringSorter { + bool operator()(const SettingDesc *a, const SettingDesc *b) + { + return strcmp(a->patx_name, b->patx_name) < 0; + } + }; + std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), StringSorter()); +} + +/** + * Load handler for settings which go in the PATX chunk + * @param osd SettingDesc struct containing all information + * @param object can be either NULL in which case we load global variables or + * a pointer to a struct which is getting saved + */ +static void LoadSettingsPatx(const SettingDesc *sd, void *object) +{ + MakeSettingsPatxList(sd); + + struct SettingsPatxLoad { + uint32 flags; + char name[256]; + uint32 setting_length; + }; + SettingsPatxLoad current_setting; + + static const SaveLoad _settings_patx_desc[] = { + SLE_VAR(SettingsPatxLoad, flags, SLE_UINT32), + SLE_STR(SettingsPatxLoad, name, SLE_STRB, 255), + SLE_VAR(SettingsPatxLoad, setting_length, SLE_UINT32), + SLE_END() + }; + + uint32 flags = SlReadUint32(); + // flags are not in use yet, reserve for future expansion + if (flags != 0) SlErrorCorruptFmt("PATX chunk: unknown chunk header flags: 0x%X", flags); + + uint32 settings_count = SlReadUint32(); + for (uint32 i = 0; i < settings_count; i++) { + SlObject(¤t_setting, _settings_patx_desc); + + // flags are not in use yet, reserve for future expansion + if (current_setting.flags != 0) SlErrorCorruptFmt("PATX chunk: unknown setting header flags: 0x%X", current_setting.flags); + + // now try to find corresponding setting, this would be much easier with C++11 support... + bool exact_match = false; + struct StringSearcher { + bool &m_exact_match; + + StringSearcher(bool &exact_match) + : m_exact_match(exact_match) { } + + bool operator()(const SettingDesc *a, const char *b) + { + int result = strcmp(a->patx_name, b); + if (result == 0) m_exact_match = true; + return result < 0; + } + }; + std::vector::iterator iter = std::lower_bound(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), current_setting.name, StringSearcher(exact_match)); + + if (exact_match) { + assert(iter != _sorted_patx_settings.end()); + // found setting + const SaveLoad *sld = &((*iter)->save); + size_t read = SlGetBytesRead(); + void *ptr = GetVariableAddress(object, sld); + SlObjectMember(ptr, sld); + if (SlGetBytesRead() != read + current_setting.setting_length) { + SlErrorCorruptFmt("PATX chunk: setting read length mismatch for setting: '%s'", current_setting.name); + } + if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, *iter, ReadValue(ptr, sld->conv)); + } else { + DEBUG(sl, 1, "PATX chunk: Could not find setting: '%s', ignoring", current_setting.name); + SlSkipBytes(current_setting.setting_length); + } + } +} + +/** + * Save handler for settings which go in the PATX chunk + * @param sd SettingDesc struct containing all information + * @param object can be either NULL in which case we load global variables or + * a pointer to a struct which is getting saved + */ +static void SaveSettingsPatx(const SettingDesc *sd, void *object) +{ + struct SettingsPatxSave { + uint32 flags; + const char *name; + uint32 setting_length; + }; + SettingsPatxSave current_setting; + + static const SaveLoad _settings_patx_desc[] = { + SLE_VAR(SettingsPatxSave, flags, SLE_UINT32), + SLE_STR(SettingsPatxSave, name, SLE_STR, 0), + SLE_VAR(SettingsPatxSave, setting_length, SLE_UINT32), + SLE_END() + }; + + struct SettingToAdd { + const SettingDesc *setting; + uint32 setting_length; + }; + std::vector settings_to_add; + + size_t length = 8; + for (const SettingDesc *desc = sd; desc->save.cmd != SL_END; desc++) { + if (desc->patx_name == NULL) continue; + uint32 setting_length = SlCalcObjMemberLength(object, &desc->save); + if (!setting_length) continue; + + current_setting.name = desc->patx_name; + + // add length of setting header + length += SlCalcObjLength(¤t_setting, _settings_patx_desc); + + // add length of actual setting + length += setting_length; + + settings_to_add.push_back({ desc, setting_length }); + } + SlSetLength(length); + + SlWriteUint32(0); // flags + SlWriteUint32(settings_to_add.size()); // settings count + + for (size_t i = 0; i < settings_to_add.size(); i++) { + const SettingDesc *desc = settings_to_add[i].setting; + current_setting.flags = 0; + current_setting.name = desc->patx_name; + current_setting.setting_length = settings_to_add[i].setting_length; + SlObject(¤t_setting, _settings_patx_desc); + void *ptr = GetVariableAddress(object, &desc->save); + SlObjectMember(ptr, &desc->save); + } +} + static void Load_OPTS() { /* Copy over default setting since some might not get loaded in @@ -2247,6 +2432,21 @@ static void Save_PATS() SaveSettings(_settings, &_settings_game); } +static void Load_PATX() +{ + LoadSettingsPatx(_settings, &_settings_game); +} + +static void Check_PATX() +{ + LoadSettingsPatx(_settings, &_load_check_data.settings); +} + +static void Save_PATX() +{ + SaveSettingsPatx(_settings, &_settings_game); +} + void CheckConfig() { /* @@ -2261,7 +2461,8 @@ void CheckConfig() extern const ChunkHandler _setting_chunk_handlers[] = { { 'OPTS', NULL, Load_OPTS, NULL, NULL, CH_RIFF}, - { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST}, + { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF}, + { 'PATX', Save_PATX, Load_PATX, NULL, Check_PATX, CH_RIFF | CH_LAST}, }; static bool IsSignedVarMemType(VarType vt) diff --git a/src/settings_internal.h b/src/settings_internal.h index 028e977e48..e356e7e5e6 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -111,6 +111,7 @@ struct SettingDescBase { struct SettingDesc { SettingDescBase desc; ///< Settings structure (going to configuration file) SaveLoad save; ///< Internal structure (going to savegame, parts to config) + const char *patx_name; ///< Name to save/load setting from in PATX chunk, if NULL save/load from PATS chunk as normal bool IsEditable(bool do_command = false) const; SettingType GetType() const; diff --git a/src/table/company_settings.ini b/src/table/company_settings.ini index 615e808cb3..5c839c2af7 100644 --- a/src/table/company_settings.ini +++ b/src/table/company_settings.ini @@ -18,8 +18,8 @@ static const SettingDesc _company_settings[] = { [post-amble] }; [templates] -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_END = SDT_END() [defaults] diff --git a/src/table/currency_settings.ini b/src/table/currency_settings.ini index e38349f3df..3c476ee5df 100644 --- a/src/table/currency_settings.ini +++ b/src/table/currency_settings.ini @@ -11,9 +11,9 @@ static const SettingDesc _currency_settings[] = { [post-amble] }; [templates] -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_CHR = SDT_CHR($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDT_CHR = SDT_CHR($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_END = SDT_END() [defaults] diff --git a/src/table/gameopt_settings.ini b/src/table/gameopt_settings.ini index 593d849ab8..9e5fa3d0ff 100644 --- a/src/table/gameopt_settings.ini +++ b/src/table/gameopt_settings.ini @@ -41,13 +41,13 @@ static const SettingDesc _gameopt_settings[] = { [post-amble] }; [templates] -SDTG_GENERAL = SDTG_GENERAL($name, $sdt_cmd, $sle_cmd, $type, $flags, $guiflags, $var, $length, $def, $min, $max, $interval, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_GENERAL = SDTG_GENERAL($name, $sdt_cmd, $sle_cmd, $type, $flags, $guiflags, $var, $length, $def, $min, $max, $interval, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_NULL = SDT_NULL($length, $from, $to, $extver), -SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver, NULL), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_END = SDT_END() [defaults] diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini index 26620318e6..11197692c8 100644 --- a/src/table/misc_settings.ini +++ b/src/table/misc_settings.ini @@ -15,12 +15,12 @@ static const SettingDescGlobVarList _misc_settings[] = { [post-amble] }; [templates] -SDTG_LIST = SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_STR = SDTG_STR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_LIST = SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_STR = SDTG_STR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDTG_END = SDTG_END() [defaults] diff --git a/src/table/settings.h.preamble b/src/table/settings.h.preamble index 7c0ca8a972..ed67d43c30 100644 --- a/src/table/settings.h.preamble +++ b/src/table/settings.h.preamble @@ -61,76 +61,76 @@ static size_t ConvertLandscape(const char *value); /* Macros for various objects to go in the configuration file. * This section is for global variables */ -#define SDTG_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, var, length, def, min, max, interval, full, str, strhelp, strval, proc, from, to, cat, extver)\ - {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, NULL, cat), SLEG_GENERAL_X(sle_cmd, var, type | flags, length, from, to, extver)} +#define SDTG_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, var, length, def, min, max, interval, full, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, NULL, cat), SLEG_GENERAL_X(sle_cmd, var, type | flags, length, from, to, extver), patxname} -#define SDTG_VAR(name, type, flags, guiflags, var, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_NUMX, SL_VAR, type, flags, guiflags, var, 0, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_VAR(name, type, flags, guiflags, var, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_NUMX, SL_VAR, type, flags, guiflags, var, 0, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTG_BOOL(name, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, var, 0, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_BOOL(name, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, var, 0, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTG_LIST(name, type, length, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_INTLIST, SL_ARR, type, flags, guiflags, var, length, def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_LIST(name, type, length, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_INTLIST, SL_ARR, type, flags, guiflags, var, length, def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTG_STR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_STRING, SL_STR, type, flags, guiflags, var, lengthof(var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_STR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_STRING, SL_STR, type, flags, guiflags, var, lengthof(var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(name, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, 0, 0, full, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(name, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, var, 0, def, 0, 0, 0, full, str, strhelp, strval, proc, from, to, cat, extver, patxname) #define SDTG_NULL(length, from, to, extver)\ - {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_NULL_X(length, from, to, extver)} + {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_NULL_X(length, from, to, extver), NULL} -#define SDTG_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_END()} +#define SDTG_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLEG_END(), NULL} /* Macros for various objects to go in the configuration file. * This section is for structures where their various members are saved */ -#define SDT_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, base, var, length, def, min, max, interval, full, str, strhelp, strval, proc, load, from, to, cat, extver)\ - {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, load, cat), SLE_GENERAL_X(sle_cmd, base, var, type | flags, length, from, to, extver)} +#define SDT_GENERAL(name, sdt_cmd, sle_cmd, type, flags, guiflags, base, var, length, def, min, max, interval, full, str, strhelp, strval, proc, load, from, to, cat, extver, patxname)\ + {NSD_GENERAL(name, def, sdt_cmd, guiflags, min, max, interval, full, str, strhelp, strval, proc, load, cat), SLE_GENERAL_X(sle_cmd, base, var, type | flags, length, from, to, extver), patxname} -#define SDT_VAR(base, var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, base, var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_VAR(base, var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, base, var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) -#define SDT_BOOL(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, base, var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_BOOL(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, base, var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) -#define SDT_LIST(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_LIST(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) -#define SDT_STR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_STR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) -#define SDT_CHR(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_STRING, SL_VAR, SLE_CHAR, flags, guiflags, base, var, 1, def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_CHR(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_STRING, SL_VAR, SLE_CHAR, flags, guiflags, base, var, 1, def, 0, 0, 0, NULL, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) -#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extver)\ - SDT_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, load, from, to, cat, extver) +#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, load, from, to, cat, extver, patxname) -#define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat, extver)\ - SDT_GENERAL(#var, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, 0, 0, full, str, strhelp, strval, proc, NULL, from, to, cat, extver) +#define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat, extver, patxname)\ + SDT_GENERAL(#var, SDT_MANYOFMANY, SL_VAR, type, flags, guiflags, base, var, 1, def, 0, 0, 0, full, str, strhelp, strval, proc, NULL, from, to, cat, extver, patxname) #define SDT_NULL(length, from, to, extver)\ - {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_CONDNULL_X(length, from, to, extver)} + {{"", NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_CONDNULL_X(length, from, to, extver), NULL} -#define SDTC_VAR(var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTC_VAR(var, type, flags, guiflags, def, min, max, interval, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(#var, SDT_NUMX, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, min, max, interval, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTC_BOOL(var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, _settings_client.var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTC_BOOL(var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, _settings_client.var, 1, def, 0, 1, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTC_LIST(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTC_LIST(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTC_STR(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTC_STR(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, NULL, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extver)\ - SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver) +#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extver, patxname)\ + SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extver, patxname) -#define SDT_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_END()} +#define SDT_END() {{NULL, NULL, {0}, {0}, 0, 0, 0, NULL, STR_NULL, STR_NULL, STR_NULL, NULL, NULL, SC_NONE}, SLE_END(), NULL} diff --git a/src/table/settings.ini b/src/table/settings.ini index 3256e20dab..0b5e55a643 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -64,18 +64,18 @@ const SettingDesc _settings[] = { [post-amble] }; [templates] -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTC_STR = SDTC_STR( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver), -SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTC_BOOL = SDTC_BOOL( $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTC_LIST = SDTC_LIST( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTC_OMANY = SDTC_OMANY( $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTC_STR = SDTC_STR( $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDTC_VAR = SDTC_VAR( $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDT_OMANY = SDT_OMANY($base, $var, $type, $flags, $guiflags, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extver, $patxname), +SDT_STR = SDT_STR($base, $var, $type, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, $patxname), SDT_NULL = SDT_NULL($length, $from, $to, $extver), SDT_END = SDT_END() @@ -92,6 +92,7 @@ from = 0 to = SL_MAX_VERSION cat = SC_ADVANCED extver = {} +patxname = NULL diff --git a/src/table/win32_settings.ini b/src/table/win32_settings.ini index 71b8499d2b..6f62db63f6 100644 --- a/src/table/win32_settings.ini +++ b/src/table/win32_settings.ini @@ -17,8 +17,8 @@ static const SettingDescGlobVarList _win32_settings[] = { }; #endif /* WIN32 */ [templates] -SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDTG_END = SDTG_END() [defaults] diff --git a/src/table/window_settings.ini b/src/table/window_settings.ini index cfa2f76d5a..4565d9b35f 100644 --- a/src/table/window_settings.ini +++ b/src/table/window_settings.ini @@ -12,8 +12,8 @@ static const SettingDesc _window_settings[] = { [post-amble] }; [templates] -SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), -SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver), +SDT_BOOL = SDT_BOOL($base, $var, $flags, $guiflags, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), +SDT_VAR = SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extver, NULL), SDT_END = SDT_END() [defaults] From 36d5b874da7bdb0c1676ef73812f1b082b6a4b71 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 1 Aug 2015 17:22:34 +0100 Subject: [PATCH 06/11] Update project files --- projects/openttd_vs100.vcxproj | 2 ++ projects/openttd_vs100.vcxproj.filters | 6 ++++++ projects/openttd_vs80.vcproj | 8 ++++++++ projects/openttd_vs90.vcproj | 8 ++++++++ 4 files changed, 24 insertions(+) diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 37f9948032..0571ef4862 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -869,6 +869,8 @@ + + diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index 06800ffdaf..0b6dc573b4 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -1836,6 +1836,12 @@ Save/Load handlers + + Save/Load handlers + + + Save/Load handlers + Tables diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index f859fcf18a..f6b938ed7d 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -2770,6 +2770,14 @@ RelativePath=".\..\src\saveload\waypoint_sl.cpp" > + + + + + + + + Date: Sat, 1 Aug 2015 18:03:07 +0100 Subject: [PATCH 07/11] Implement version setting in SlXvSetCurrentState Call in ClearSaveLoadState --- src/saveload/extended_ver_sl.cpp | 5 ++++- src/saveload/saveload.cpp | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp index 478e6d2f34..7127faf7b6 100644 --- a/src/saveload/extended_ver_sl.cpp +++ b/src/saveload/extended_ver_sl.cpp @@ -108,7 +108,10 @@ void SlXvSetCurrentState() SlXvResetState(); _sl_is_ext_version = true; - // TODO: set versions for currently enabled features here + const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos; + for (; info->index != XSLFI_NULL; ++info) { + _sl_xv_feature_versions[info->index] = info->save_version; + } } /** diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index b369af5627..55a37f8ee8 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2467,6 +2467,8 @@ static inline void ClearSaveLoadState() delete _sl.lf; _sl.lf = NULL; + + SlXvSetCurrentState(); } /** From b3e102fe52fffb278464d720e188420c3bcc4c73 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 1 Aug 2015 18:16:55 +0100 Subject: [PATCH 08/11] Fix length mismatch in PATX loader. --- src/settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/settings.cpp b/src/settings.cpp index b8e329ef5c..203dccbea0 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -2293,7 +2293,7 @@ static void LoadSettingsPatx(const SettingDesc *sd, void *object) static const SaveLoad _settings_patx_desc[] = { SLE_VAR(SettingsPatxLoad, flags, SLE_UINT32), - SLE_STR(SettingsPatxLoad, name, SLE_STRB, 255), + SLE_STR(SettingsPatxLoad, name, SLE_STRB, 256), SLE_VAR(SettingsPatxLoad, setting_length, SLE_UINT32), SLE_END() }; From 047395c663b112448250697720d1be047caa3ff0 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 2 Aug 2015 23:36:59 +0100 Subject: [PATCH 09/11] Remove unnecessary and buggy field-packing in struct SlXvFeatureTest. --- src/saveload/extended_ver_sl.cpp | 8 ++------ src/saveload/extended_ver_sl.h | 19 ++++++++----------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp index 7127faf7b6..be40cad17b 100644 --- a/src/saveload/extended_ver_sl.cpp +++ b/src/saveload/extended_ver_sl.cpp @@ -60,13 +60,9 @@ bool SlXvFeatureTest::IsFeaturePresent(uint16 savegame_version, uint16 savegame_ { bool savegame_version_ok = savegame_version >= savegame_version_from && savegame_version <= savegame_version_to; - SlXvFeatureIndex feature = static_cast(GB(this->value, 0, 16)); - if (feature == XSLFI_NULL) return savegame_version_ok; + if (this->feature == XSLFI_NULL) return savegame_version_ok; - uint16 min_version = GB(this->value, 16, 16); - uint16 max_version = GB(this->value, 32, 16); - SlXvFeatureTestOperator op = static_cast(GB(this->value, 48, 16)); - bool feature_ok = SlXvIsFeaturePresent(feature, min_version, max_version); + bool feature_ok = SlXvIsFeaturePresent(this->feature, this->min_version, this->max_version); switch (op) { case XSLFTO_OR: diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h index a7e9496944..3dffee5a7c 100644 --- a/src/saveload/extended_ver_sl.h +++ b/src/saveload/extended_ver_sl.h @@ -40,20 +40,17 @@ enum SlXvFeatureTestOperator { */ struct SlXvFeatureTest { private: - uint64 value; + uint16 min_version; + uint16 max_version; + SlXvFeatureIndex feature; + SlXvFeatureTestOperator op; public: SlXvFeatureTest() - : value(0) { } - - SlXvFeatureTest(SlXvFeatureTestOperator op, SlXvFeatureIndex feature, uint16 min_version = 1, uint16 max_version = 0xFFFF) - { - this->value = 0; - SB(this->value, 0, 16, feature); - SB(this->value, 16, 16, min_version); - SB(this->value, 32, 16, max_version); - SB(this->value, 48, 16, op); - } + : min_version(0), max_version(0), feature(XSLFI_NULL), op(XSLFTO_OR) { } + + SlXvFeatureTest(SlXvFeatureTestOperator op_, SlXvFeatureIndex feature_, uint16 min_version_ = 1, uint16 max_version_ = 0xFFFF) + : min_version(min_version_), max_version(max_version_), feature(feature_), op(op_) { } bool IsFeaturePresent(uint16 savegame_version, uint16 savegame_version_from, uint16 savegame_version_to) const; }; From 03b6c41c674fbf14efad10d79a97a1719f26e7b1 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 5 Aug 2015 21:31:17 +0100 Subject: [PATCH 10/11] Add shorthand flag in SlxiSubChunkFlags for XSCF_IGNORABLE_UNKNOWN | XSCF_IGNORABLE_VERSION. --- src/saveload/extended_ver_sl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h index 3dffee5a7c..39a03478c2 100644 --- a/src/saveload/extended_ver_sl.h +++ b/src/saveload/extended_ver_sl.h @@ -75,6 +75,8 @@ enum SlxiSubChunkFlags { XSCF_IGNORABLE_VERSION = 1 << 1, ///< the loader is free to ignore this without aborting the load if the version is greater than the maximum that can be loaded XSCF_EXTRA_DATA_PRESENT = 1 << 2, ///< extra data field is present, extra data in some sub-chunk/feature specific format, not used for anything yet XSCF_CHUNK_ID_LIST_PRESENT = 1 << 3, ///< chunk ID list field is present, list of chunks which this sub-chunk/feature adds to the save game, this can be used to discard the chunks if the feature is unknown + + XSCF_IGNORABLE_ALL = XSCF_IGNORABLE_UNKNOWN | XSCF_IGNORABLE_VERSION, ///< all "ignorable" flags }; DECLARE_ENUM_AS_BIT_SET(SlxiSubChunkFlags) From e30d7c21649aa707faa4589d5d1ee98772353a29 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 7 Aug 2015 20:12:25 +0100 Subject: [PATCH 11/11] Save/load ext: Fix SlXvSetCurrentState being called before AfterLoadGame(). --- src/saveload/saveload.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 55a37f8ee8..74f6fba1e2 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2467,8 +2467,6 @@ static inline void ClearSaveLoadState() delete _sl.lf; _sl.lf = NULL; - - SlXvSetCurrentState(); } /** @@ -2780,6 +2778,8 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check) GamelogStopAction(); } + SlXvSetCurrentState(); + return SL_OK; } @@ -2839,6 +2839,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo return SL_REINIT; } GamelogStopAction(); + SlXvSetCurrentState(); return SL_OK; }