Codechange: Store GS lang texts in std::strings.

This commit is contained in:
Michael Lutz
2020-05-17 23:31:56 +02:00
parent 9c2e47d03c
commit 9b6f5e3bb8
5 changed files with 189 additions and 98 deletions

View File

@@ -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]);
}
}

View File

@@ -44,6 +44,7 @@
#include "../fios.h"
#include "../error.h"
#include <atomic>
#include <string>
#include "table/strings.h"
@@ -901,6 +902,21 @@ static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType con
return len + SlGetArrayLength(len); // also include the length of the index
}
/**
* 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
@@ -980,6 +996,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
@@ -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;

View File

@@ -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,
@@ -567,6 +568,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.
@@ -621,6 +632,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.
@@ -701,6 +720,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.
@@ -738,6 +766,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.