Codechange: rework Gamelog changes from union to classes
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "../gamelog_internal.h"
|
||||
#include "../fios.h"
|
||||
#include "../string_func.h"
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
@@ -21,8 +22,8 @@
|
||||
class SlGamelogMode : public DefaultSaveLoadHandler<SlGamelogMode, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, mode.mode, SLE_UINT8),
|
||||
SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeMode, mode, "mode.mode", SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeMode, landscape, "mode.landscape", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_mode_sl_compat;
|
||||
|
||||
@@ -43,11 +44,15 @@ public:
|
||||
|
||||
class SlGamelogRevision : public DefaultSaveLoadHandler<SlGamelogRevision, LoggedChange> {
|
||||
public:
|
||||
static const size_t GAMELOG_REVISION_LENGTH = 15;
|
||||
static char revision_text[GAMELOG_REVISION_LENGTH];
|
||||
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_ARR(LoggedChange, revision.text, SLE_UINT8, GAMELOG_REVISION_LENGTH),
|
||||
SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, revision.slver, SLE_UINT16),
|
||||
SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
|
||||
SLEG_CONDARR("revision.text", SlGamelogRevision::revision_text, SLE_UINT8, GAMELOG_REVISION_LENGTH, SL_MIN_VERSION, SLV_STRING_GAMELOG),
|
||||
SLE_CONDSSTRNAME(LoggedChangeRevision, text, "revision.text", SLE_STR, SLV_STRING_GAMELOG, SL_MAX_VERSION),
|
||||
SLE_VARNAME(LoggedChangeRevision, newgrf, "revision.newgrf", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeRevision, slver, "revision.slver", SLE_UINT16),
|
||||
SLE_VARNAME(LoggedChangeRevision, modified, "revision.modified", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_revision_sl_compat;
|
||||
|
||||
@@ -61,16 +66,23 @@ public:
|
||||
{
|
||||
if (lc->ct != GLCT_REVISION) return;
|
||||
SlObject(lc, this->GetLoadDescription());
|
||||
|
||||
if (IsSavegameVersionBefore(SLV_STRING_GAMELOG)) {
|
||||
StrMakeValidInPlace(SlGamelogRevision::revision_text, lastof(SlGamelogRevision::revision_text));
|
||||
static_cast<LoggedChangeRevision *>(lc)->text = SlGamelogRevision::revision_text;
|
||||
}
|
||||
}
|
||||
|
||||
void LoadCheck(LoggedChange *lc) const override { this->Load(lc); }
|
||||
};
|
||||
|
||||
/* static */ char SlGamelogRevision::revision_text[GAMELOG_REVISION_LENGTH];
|
||||
|
||||
class SlGamelogOldver : public DefaultSaveLoadHandler<SlGamelogOldver, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, oldver.type, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, oldver.version, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeOldVersion, type, "oldver.type", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeOldVersion, version, "oldver.version", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_oldver_sl_compat;
|
||||
|
||||
@@ -92,9 +104,9 @@ public:
|
||||
class SlGamelogSetting : public DefaultSaveLoadHandler<SlGamelogSetting, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_STR(LoggedChange, setting.name, SLE_STR, 128),
|
||||
SLE_VAR(LoggedChange, setting.oldval, SLE_INT32),
|
||||
SLE_VAR(LoggedChange, setting.newval, SLE_INT32),
|
||||
SLE_SSTRNAME(LoggedChangeSettingChanged, name, "setting.name", SLE_STR),
|
||||
SLE_VARNAME(LoggedChangeSettingChanged, oldval, "setting.oldval", SLE_INT32),
|
||||
SLE_VARNAME(LoggedChangeSettingChanged, newval, "setting.newval", SLE_INT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_setting_sl_compat;
|
||||
|
||||
@@ -116,8 +128,8 @@ public:
|
||||
class SlGamelogGrfadd : public DefaultSaveLoadHandler<SlGamelogGrfadd, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ),
|
||||
SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16),
|
||||
SLE_VARNAME(LoggedChangeGRFAdd, grfid, "grfadd.grfid", SLE_UINT32 ),
|
||||
SLE_ARRNAME(LoggedChangeGRFAdd, md5sum, "grfadd.md5sum", SLE_UINT8, 16),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfadd_sl_compat;
|
||||
|
||||
@@ -139,7 +151,7 @@ public:
|
||||
class SlGamelogGrfrem : public DefaultSaveLoadHandler<SlGamelogGrfrem, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFRemoved, grfid, "grfrem.grfid", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfrem_sl_compat;
|
||||
|
||||
@@ -161,8 +173,8 @@ public:
|
||||
class SlGamelogGrfcompat : public DefaultSaveLoadHandler<SlGamelogGrfcompat, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ),
|
||||
SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16),
|
||||
SLE_VARNAME(LoggedChangeGRFChanged, grfid, "grfcompat.grfid", SLE_UINT32 ),
|
||||
SLE_ARRNAME(LoggedChangeGRFChanged, md5sum, "grfcompat.md5sum", SLE_UINT8, 16),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfcompat_sl_compat;
|
||||
|
||||
@@ -184,7 +196,7 @@ public:
|
||||
class SlGamelogGrfparam : public DefaultSaveLoadHandler<SlGamelogGrfparam, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFParameterChanged, grfid, "grfparam.grfid", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfparam_sl_compat;
|
||||
|
||||
@@ -206,8 +218,8 @@ public:
|
||||
class SlGamelogGrfmove : public DefaultSaveLoadHandler<SlGamelogGrfmove, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32),
|
||||
SLE_VARNAME(LoggedChangeGRFMoved, grfid, "grfmove.grfid", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFMoved, offset, "grfmove.offset", SLE_INT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfmove_sl_compat;
|
||||
|
||||
@@ -229,9 +241,9 @@ public:
|
||||
class SlGamelogGrfbug : public DefaultSaveLoadHandler<SlGamelogGrfbug, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfbug.data, SLE_UINT64),
|
||||
SLE_VAR(LoggedChange, grfbug.grfid, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, grfbug.bug, SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, data, "grfbug.data", SLE_UINT64),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, grfid, "grfbug.grfid", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, bug, "grfbug.bug", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfbug_sl_compat;
|
||||
|
||||
@@ -278,6 +290,27 @@ public:
|
||||
void LoadCheck(LoggedChange *lc) const override { this->Load(lc); }
|
||||
};
|
||||
|
||||
static std::unique_ptr<LoggedChange> MakeLoggedChange(GamelogChangeType type)
|
||||
{
|
||||
switch (type) {
|
||||
case GLCT_MODE: return std::make_unique<LoggedChangeMode>();
|
||||
case GLCT_REVISION: return std::make_unique<LoggedChangeRevision>();
|
||||
case GLCT_OLDVER: return std::make_unique<LoggedChangeOldVersion>();
|
||||
case GLCT_SETTING: return std::make_unique<LoggedChangeSettingChanged>();
|
||||
case GLCT_GRFADD: return std::make_unique<LoggedChangeGRFAdd>();
|
||||
case GLCT_GRFREM: return std::make_unique<LoggedChangeGRFRemoved>();
|
||||
case GLCT_GRFCOMPAT: return std::make_unique<LoggedChangeGRFChanged>();
|
||||
case GLCT_GRFPARAM: return std::make_unique<LoggedChangeGRFParameterChanged>();
|
||||
case GLCT_GRFMOVE: return std::make_unique<LoggedChangeGRFMoved>();
|
||||
case GLCT_GRFBUG: return std::make_unique<LoggedChangeGRFBug>();
|
||||
case GLCT_EMERGENCY: return std::make_unique<LoggedChangeEmergencySave>();
|
||||
case GLCT_END:
|
||||
case GLCT_NONE:
|
||||
default:
|
||||
SlErrorCorrupt("Invalid gamelog action type");
|
||||
}
|
||||
}
|
||||
|
||||
class SlGamelogAction : public DefaultSaveLoadHandler<SlGamelogAction, LoggedAction> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
@@ -301,22 +334,25 @@ public:
|
||||
SlSetStructListLength(la->change.size());
|
||||
|
||||
for (auto &lc : la->change) {
|
||||
assert((uint)lc.ct < GLCT_END);
|
||||
SlObject(&lc, this->GetDescription());
|
||||
assert(lc->ct < GLCT_END);
|
||||
SlObject(lc.get(), this->GetDescription());
|
||||
}
|
||||
}
|
||||
|
||||
void LoadChange(LoggedAction *la, GamelogChangeType type) const
|
||||
{
|
||||
std::unique_ptr<LoggedChange> lc = MakeLoggedChange(type);
|
||||
SlObject(lc.get(), this->GetLoadDescription());
|
||||
la->change.push_back(std::move(lc));
|
||||
}
|
||||
|
||||
void Load(LoggedAction *la) const override
|
||||
{
|
||||
if (IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY)) {
|
||||
byte type;
|
||||
while ((type = SlReadByte()) != GLCT_NONE) {
|
||||
if (type >= GLCT_END) SlErrorCorrupt("Invalid gamelog change type");
|
||||
GamelogChangeType ct = (GamelogChangeType)type;
|
||||
|
||||
LoggedChange &lc = la->change.emplace_back();
|
||||
lc.ct = ct;
|
||||
SlObject(&lc, this->GetLoadDescription());
|
||||
LoadChange(la, (GamelogChangeType)type);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -325,9 +361,7 @@ public:
|
||||
la->change.reserve(length);
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
LoggedChange &lc = la->change.emplace_back();
|
||||
lc.ct = (GamelogChangeType)SlReadByte();
|
||||
SlObject(&lc, this->GetLoadDescription());
|
||||
LoadChange(la, (GamelogChangeType)SlReadByte());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -354,6 +354,7 @@ enum SaveLoadVersion : uint16 {
|
||||
SLV_EXTEND_ENTITY_MAPPING, ///< 311 PR#10672 Extend entity mapping range.
|
||||
SLV_DISASTER_VEH_STATE, ///< 312 PR#10798 Explicit storage of disaster vehicle state.
|
||||
SLV_SAVEGAME_ID, ///< 313 PR#10719 Add an unique ID to every savegame (used to deduplicate surveys).
|
||||
SLV_STRING_GAMELOG, ///< 314 PR#10801 Use std::string in gamelog.
|
||||
|
||||
SL_MAX_VERSION, ///< Highest possible saveload version
|
||||
};
|
||||
@@ -771,6 +772,18 @@ struct SaveLoadCompat {
|
||||
*/
|
||||
#define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a fixed-size array of #SL_VAR elements in some savegame versions.
|
||||
* @param base Name of the class or struct containing the array.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
* @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.
|
||||
*/
|
||||
#define SLE_CONDARRNAME(base, variable, name, type, length, from, to) SLE_GENERAL_NAME(SL_ARR, name, base, variable, type, length, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a string in some savegame versions.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
@@ -792,6 +805,17 @@ struct SaveLoadCompat {
|
||||
*/
|
||||
#define SLE_CONDSSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to, 0)
|
||||
|
||||
/**
|
||||
* 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 name Field name for table chunks.
|
||||
* @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_CONDSSTRNAME(base, variable, name, type, from, to) SLE_GENERAL_NAME(SL_STDSTR, name, base, variable, type, 0, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a list of #SL_REF elements in some savegame versions.
|
||||
* @param base Name of the class or struct containing the list.
|
||||
@@ -820,6 +844,15 @@ struct SaveLoadCompat {
|
||||
*/
|
||||
#define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a variable in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the variable.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
*/
|
||||
#define SLE_VARNAME(base, variable, name, type) SLE_CONDVARNAME(base, variable, name, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a reference in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the variable.
|
||||
@@ -837,6 +870,16 @@ struct SaveLoadCompat {
|
||||
*/
|
||||
#define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of fixed-size array of #SL_VAR elements in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the array.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
* @param length Number of elements in the array.
|
||||
*/
|
||||
#define SLE_ARRNAME(base, variable, name, type, length) SLE_CONDARRNAME(base, variable, name, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a string in every savegame version.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
@@ -854,6 +897,15 @@ struct SaveLoadCompat {
|
||||
*/
|
||||
#define SLE_SSTR(base, variable, type) SLE_CONDSSTR(base, variable, type, 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 name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
*/
|
||||
#define SLE_SSTRNAME(base, variable, name, type) SLE_CONDSSTRNAME(base, variable, name, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a list of #SL_REF elements in every savegame version.
|
||||
* @param base Name of the class or struct containing the list.
|
||||
|
Reference in New Issue
Block a user