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

@@ -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;