diff options
author | Michael Lutz <michi@icosahedron.de> | 2020-05-17 23:31:56 +0200 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2020-05-21 20:02:34 +0200 |
commit | 9b6f5e3bb850b6cff02cc761767fcdf49c681645 (patch) | |
tree | 1989b13b0ac5f06f78df1064124486e626f8811f /src/saveload | |
parent | 9c2e47d03cbc4c7af416a34b09e0c8d0de7c3474 (diff) | |
download | openttd-9b6f5e3bb850b6cff02cc761767fcdf49c681645.tar.xz |
Codechange: Store GS lang texts in std::strings.
Diffstat (limited to 'src/saveload')
-rw-r--r-- | src/saveload/game_sl.cpp | 22 | ||||
-rw-r--r-- | src/saveload/saveload.cpp | 71 | ||||
-rw-r--r-- | src/saveload/saveload.h | 35 |
3 files changed, 117 insertions, 11 deletions
diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index 28a6c6c11..e13484385 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -113,23 +113,23 @@ static void Save_GSDT() extern GameStrings *_current_data; -static const char *_game_saveload_string; +static std::string _game_saveload_string; static uint _game_saveload_strings; static const SaveLoad _game_language_header[] = { - SLEG_STR(_game_saveload_string, SLE_STR), - SLEG_VAR(_game_saveload_strings, SLE_UINT32), - SLE_END() + SLEG_SSTR(_game_saveload_string, SLE_STR), + SLEG_VAR(_game_saveload_strings, SLE_UINT32), + SLE_END() }; static const SaveLoad _game_language_string[] = { - SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL), - SLE_END() + SLEG_SSTR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL), + SLE_END() }; static void SaveReal_GSTR(const LanguageStrings *ls) { - _game_saveload_string = ls->language; + _game_saveload_string = ls->language.c_str(); _game_saveload_strings = (uint)ls->lines.size(); SlObject(nullptr, _game_language_header); @@ -145,13 +145,13 @@ static void Load_GSTR() _current_data = new GameStrings(); while (SlIterateArray() != -1) { - _game_saveload_string = nullptr; + _game_saveload_string.clear(); SlObject(nullptr, _game_language_header); - std::unique_ptr<LanguageStrings> ls(new LanguageStrings(_game_saveload_string != nullptr ? _game_saveload_string : "")); + LanguageStrings ls(_game_saveload_string); for (uint i = 0; i < _game_saveload_strings; i++) { SlObject(nullptr, _game_language_string); - ls->lines.emplace_back(_game_saveload_string != nullptr ? _game_saveload_string : ""); + ls.lines.emplace_back(_game_saveload_string); } _current_data->raw_strings.push_back(std::move(ls)); @@ -174,7 +174,7 @@ static void Save_GSTR() for (uint i = 0; i < _current_data->raw_strings.size(); i++) { SlSetArrayIndex(i); - SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i].get()); + SlAutolength((AutolengthProc *)SaveReal_GSTR, &_current_data->raw_strings[i]); } } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index f9eebed46..7a41f4c40 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -44,6 +44,7 @@ #include "../fios.h" #include "../error.h" #include <atomic> +#include <string> #include "table/strings.h" @@ -902,6 +903,21 @@ static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType con } /** + * Calculate the gross length of the string that it + * will occupy in the savegame. This includes the real length, returned + * by SlCalcNetStringLen and the length that the index will occupy. + * @param ptr Pointer to the \c std::string. + * @return The gross length of the string. + */ +static inline size_t SlCalcStdStringLen(const void *ptr) +{ + const std::string *str = reinterpret_cast<const std::string *>(ptr); + + size_t len = str->length(); + return len + SlGetArrayLength(len); // also include the length of the index +} + +/** * Save/Load a string. * @param ptr the string being manipulated * @param length of the string (full length) @@ -981,6 +997,53 @@ static void SlString(void *ptr, size_t length, VarType conv) } /** + * Save/Load a \c std::string. + * @param ptr the string being manipulated + * @param conv must be SLE_FILE_STRING + */ +static void SlStdString(void *ptr, VarType conv) +{ + std::string *str = reinterpret_cast<std::string *>(ptr); + + switch (_sl.action) { + case SLA_SAVE: { + size_t len = str->length(); + SlWriteArrayLength(len); + SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->c_str())), len); + break; + } + + case SLA_LOAD_CHECK: + case SLA_LOAD: { + size_t len = SlReadArrayLength(); + char *buf = AllocaM(char, len + 1); + + SlCopyBytes(buf, len); + buf[len] = '\0'; // properly terminate the string + + StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK; + if ((conv & SLF_ALLOW_CONTROL) != 0) { + settings = settings | SVS_ALLOW_CONTROL_CODE; + if (IsSavegameVersionBefore(SLV_169)) { + str_fix_scc_encoded(buf, buf + len); + } + } + if ((conv & SLF_ALLOW_NEWLINE) != 0) { + settings = settings | SVS_ALLOW_NEWLINE; + } + str_validate(buf, buf + len, settings); + + // Store sanitized string. + str->assign(buf); + } + + case SLA_PTRS: break; + case SLA_NULL: break; + default: NOT_REACHED(); + } +} + +/** * Return the size in bytes of a certain type of atomic array * @param length The length of the array counted in elements * @param conv VarType type of the variable that is used in calculating the size @@ -1403,6 +1466,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) case SL_STR: case SL_LST: case SL_DEQUE: + case SL_STDSTR: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) break; @@ -1413,6 +1477,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv); case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld)); case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld->conv); + case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld)); default: NOT_REACHED(); } break; @@ -1461,6 +1526,10 @@ static bool IsVariableSizeRight(const SaveLoad *sld) /* These should be pointer sized, or fixed array. */ return sld->size == sizeof(void *) || sld->size == sld->length; + case SL_STDSTR: + /* These should be all pointers to std::string. */ + return sld->size == sizeof(std::string); + default: return true; } @@ -1482,6 +1551,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) case SL_STR: case SL_LST: case SL_DEQUE: + case SL_STDSTR: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) return false; if (SlSkipVariableOnLoad(sld)) return false; @@ -1510,6 +1580,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) case SL_STR: SlString(ptr, sld->length, sld->conv); break; case SL_LST: SlList(ptr, (SLRefType)conv); break; case SL_DEQUE: SlDeque(ptr, conv); break; + case SL_STDSTR: SlStdString(ptr, sld->conv); break; default: NOT_REACHED(); } break; diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 5065e568b..01a074bd8 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -485,6 +485,7 @@ enum SaveLoadTypes { SL_STR = 3, ///< Save/load a string. SL_LST = 4, ///< Save/load a list. SL_DEQUE = 5, ///< Save/load a deque. + SL_STDSTR = 6, ///< Save/load a \c std::string. /* non-normal save-load types */ SL_WRITEBYTE = 8, SL_VEH_INCLUDE = 9, @@ -568,6 +569,16 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to) /** + * Storage of a \c std::string in some savegame versions. + * @param base Name of the class or struct containing the string. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the string. + * @param to Last savegame version that has the string. + */ +#define SLE_CONDSSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to) + +/** * Storage of a list in some savegame versions. * @param base Name of the class or struct containing the list. * @param variable Name of the variable in the class or struct referenced by \a base. @@ -622,6 +633,14 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION) /** + * Storage of a \c std::string in every savegame version. + * @param base Name of the class or struct containing the string. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLE_SSTR(base, variable, type) SLE_CONDSSTR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION) + +/** * Storage of a list in every savegame version. * @param base Name of the class or struct containing the list. * @param variable Name of the variable in the class or struct referenced by \a base. @@ -702,6 +721,15 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to) /** + * Storage of a global \c std::string in some savegame versions. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the string. + * @param to Last savegame version that has the string. + */ +#define SLEG_CONDSSTR(variable, type, from, to) SLEG_GENERAL(SL_STDSTR, variable, type, 0, from, to) + +/** * Storage of a global list in some savegame versions. * @param variable Name of the global variable. * @param type Storage of the data in memory and in the savegame. @@ -739,6 +767,13 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, sizeof(variable), SL_MIN_VERSION, SL_MAX_VERSION) /** + * Storage of a global \c std::string in every savegame version. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLEG_SSTR(variable, type) SLEG_CONDSSTR(variable, type, SL_MIN_VERSION, SL_MAX_VERSION) + +/** * Storage of a global list in every savegame version. * @param variable Name of the global variable. * @param type Storage of the data in memory and in the savegame. |