Change: rework GLOG chunk to be more like the rest

Basically it is very similar to Vehicles, where there first is
a type field, followed by data of that type. So this commit makes
it looks like how Vehicles solved that.
This removes a lot of custom "keeping track of length" stuff.
pull/332/head
Patric Stout 3 years ago committed by Patric Stout
parent 88edfd4ef1
commit 4c4b55ecbd

@ -13,7 +13,7 @@
#include "newgrf_config.h" #include "newgrf_config.h"
/** The actions we log. */ /** The actions we log. */
enum GamelogActionType { enum GamelogActionType : uint8 {
GLAT_START, ///< Game created GLAT_START, ///< Game created
GLAT_LOAD, ///< Game loaded GLAT_LOAD, ///< Game loaded
GLAT_GRF, ///< GRF changed GLAT_GRF, ///< GRF changed

@ -13,7 +13,7 @@
#include "gamelog.h" #include "gamelog.h"
/** Type of logged change */ /** Type of logged change */
enum GamelogChangeType { enum GamelogChangeType : uint8 {
GLCT_MODE, ///< Scenario editor x Game, different landscape GLCT_MODE, ///< Scenario editor x Game, different landscape
GLCT_REVISION, ///< Changed game revision string GLCT_REVISION, ///< Changed game revision string
GLCT_OLDVER, ///< Loaded from savegame without logged data GLCT_OLDVER, ///< Loaded from savegame without logged data

@ -15,147 +15,317 @@
#include "../safeguards.h" #include "../safeguards.h"
static const SaveLoad _glog_action_desc[] = {
SLE_VAR(LoggedAction, tick, SLE_UINT16),
};
static const SaveLoad _glog_mode_desc[] = { class SlGamelogMode : public DefaultSaveLoadHandler<SlGamelogMode, LoggedChange> {
SLE_VAR(LoggedChange, mode.mode, SLE_UINT8), public:
SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8), inline static const SaveLoad description[] = {
}; SLE_VAR(LoggedChange, mode.mode, SLE_UINT8),
SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_MODE) return;
SlObject(lc, this->GetDescription());
}
static const SaveLoad _glog_revision_desc[] = { void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_ARR(LoggedChange, revision.text, SLE_UINT8, GAMELOG_REVISION_LENGTH), void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32), void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_VAR(LoggedChange, revision.slver, SLE_UINT16),
SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
}; };
static const SaveLoad _glog_oldver_desc[] = { class SlGamelogRevision : public DefaultSaveLoadHandler<SlGamelogRevision, LoggedChange> {
SLE_VAR(LoggedChange, oldver.type, SLE_UINT32), public:
SLE_VAR(LoggedChange, oldver.version, SLE_UINT32), 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),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_REVISION) return;
SlObject(lc, this->GetDescription());
}
static const SaveLoad _glog_setting_desc[] = { void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_STR(LoggedChange, setting.name, SLE_STR, 128), void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_VAR(LoggedChange, setting.oldval, SLE_INT32), void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
SLE_VAR(LoggedChange, setting.newval, SLE_INT32),
}; };
static const SaveLoad _glog_grfadd_desc[] = { class SlGamelogOldver : public DefaultSaveLoadHandler<SlGamelogOldver, LoggedChange> {
SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ), public:
SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16), inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, oldver.type, SLE_UINT32),
SLE_VAR(LoggedChange, oldver.version, SLE_UINT32),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_OLDVER) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_grfrem_desc[] = { class SlGamelogSetting : public DefaultSaveLoadHandler<SlGamelogSetting, LoggedChange> {
SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32), 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),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_SETTING) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_grfcompat_desc[] = { class SlGamelogGrfadd : public DefaultSaveLoadHandler<SlGamelogGrfadd, LoggedChange> {
SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ), public:
SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16), inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ),
SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_GRFADD) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_grfparam_desc[] = { class SlGamelogGrfrem : public DefaultSaveLoadHandler<SlGamelogGrfrem, LoggedChange> {
SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32), public:
inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_GRFREM) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_grfmove_desc[] = { class SlGamelogGrfcompat : public DefaultSaveLoadHandler<SlGamelogGrfcompat, LoggedChange> {
SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32), public:
SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32), inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ),
SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_GRFCOMPAT) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_grfbug_desc[] = { class SlGamelogGrfparam : public DefaultSaveLoadHandler<SlGamelogGrfparam, LoggedChange> {
SLE_VAR(LoggedChange, grfbug.data, SLE_UINT64), public:
SLE_VAR(LoggedChange, grfbug.grfid, SLE_UINT32), inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, grfbug.bug, SLE_UINT8), SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_GRFPARAM) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoad _glog_emergency_desc[] = { class SlGamelogGrfmove : public DefaultSaveLoadHandler<SlGamelogGrfmove, LoggedChange> {
SLE_CONDNULL(0, SL_MIN_VERSION, SL_MIN_VERSION), // Just an empty list, to keep the rest of the code easier. public:
inline static const SaveLoad description[] = {
SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32),
SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32),
};
void GenericSaveLoad(LoggedChange *lc) const
{
if (lc->ct != GLCT_GRFMOVE) return;
SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static const SaveLoadTable _glog_desc[] = { class SlGamelogGrfbug : public DefaultSaveLoadHandler<SlGamelogGrfbug, LoggedChange> {
_glog_mode_desc, public:
_glog_revision_desc, inline static const SaveLoad description[] = {
_glog_oldver_desc, SLE_VAR(LoggedChange, grfbug.data, SLE_UINT64),
_glog_setting_desc, SLE_VAR(LoggedChange, grfbug.grfid, SLE_UINT32),
_glog_grfadd_desc, SLE_VAR(LoggedChange, grfbug.bug, SLE_UINT8),
_glog_grfrem_desc, };
_glog_grfcompat_desc,
_glog_grfparam_desc, void GenericSaveLoad(LoggedChange *lc) const
_glog_grfmove_desc, {
_glog_grfbug_desc, if (lc->ct != GLCT_GRFBUG) return;
_glog_emergency_desc, SlObject(lc, this->GetDescription());
}
void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
}; };
static_assert(lengthof(_glog_desc) == GLCT_END); static bool _is_emergency_save = true;
static void Load_GLOG_common(LoggedAction *&gamelog_action, uint &gamelog_actions) class SlGamelogEmergency : public DefaultSaveLoadHandler<SlGamelogEmergency, LoggedChange> {
{ public:
assert(gamelog_action == nullptr); /* We need to store something, so store a "true" value. */
assert(gamelog_actions == 0); inline static const SaveLoad description[] = {
SLEG_CONDVAR(_is_emergency_save, SLE_BOOL, SLV_RIFF_TO_ARRAY, SL_MAX_VERSION),
};
byte type; void GenericSaveLoad(LoggedChange *lc) const
while ((type = SlReadByte()) != GLAT_NONE) { {
if (type >= GLAT_END) SlErrorCorrupt("Invalid gamelog action type"); if (lc->ct != GLCT_EMERGENCY) return;
GamelogActionType at = (GamelogActionType)type;
gamelog_action = ReallocT(gamelog_action, gamelog_actions + 1); _is_emergency_save = true;
LoggedAction *la = &gamelog_action[gamelog_actions++]; SlObject(lc, this->GetDescription());
}
la->at = at; void Save(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void Load(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
void LoadCheck(LoggedChange *lc) const override { this->GenericSaveLoad(lc); }
};
SlObject(la, _glog_action_desc); // has to be saved after 'DATE'! class SlGamelogAction : public DefaultSaveLoadHandler<SlGamelogAction, LoggedAction> {
la->change = nullptr; public:
la->changes = 0; inline static const SaveLoad description[] = {
SLE_SAVEBYTE(LoggedChange, ct),
SLEG_STRUCT(SlGamelogMode),
SLEG_STRUCT(SlGamelogRevision),
SLEG_STRUCT(SlGamelogOldver),
SLEG_STRUCT(SlGamelogSetting),
SLEG_STRUCT(SlGamelogGrfadd),
SLEG_STRUCT(SlGamelogGrfrem),
SLEG_STRUCT(SlGamelogGrfcompat),
SLEG_STRUCT(SlGamelogGrfparam),
SLEG_STRUCT(SlGamelogGrfmove),
SLEG_STRUCT(SlGamelogGrfbug),
SLEG_STRUCT(SlGamelogEmergency),
};
void Save(LoggedAction *la) const override
{
SlSetStructListLength(la->changes);
while ((type = SlReadByte()) != GLCT_NONE) { const LoggedChange *lcend = &la->change[la->changes];
if (type >= GLCT_END) SlErrorCorrupt("Invalid gamelog change type"); for (LoggedChange *lc = la->change; lc != lcend; lc++) {
GamelogChangeType ct = (GamelogChangeType)type; assert((uint)lc->ct < GLCT_END);
SlObject(lc, this->GetDescription());
}
}
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;
la->change = ReallocT(la->change, la->changes + 1); la->change = ReallocT(la->change, la->changes + 1);
LoggedChange *lc = &la->change[la->changes++]; LoggedChange *lc = &la->change[la->changes++];
/* for SLE_STR, pointer has to be valid! so make it nullptr */ memset(lc, 0, sizeof(*lc));
lc->ct = ct;
SlObject(lc, this->GetDescription());
}
return;
}
size_t length = SlGetStructListLength(UINT32_MAX);
la->change = ReallocT(la->change, length);
for (size_t i = 0; i < length; i++) {
LoggedChange *lc = &la->change[i];
memset(lc, 0, sizeof(*lc)); memset(lc, 0, sizeof(*lc));
lc->ct = ct;
SlObject(lc, _glog_desc[ct]); lc->ct = (GamelogChangeType)SlReadByte();
SlObject(lc, this->GetDescription());
} }
} }
}
static void Save_GLOG() void LoadCheck(LoggedAction *la) const override { this->Load(la); }
};
static const SaveLoad _gamelog_desc[] = {
SLE_CONDVAR(LoggedAction, at, SLE_UINT8, SLV_RIFF_TO_ARRAY, SL_MAX_VERSION),
SLE_VAR(LoggedAction, tick, SLE_UINT16),
SLEG_STRUCTLIST(SlGamelogAction),
};
static void Load_GLOG_common(LoggedAction *&gamelog_action, uint &gamelog_actions)
{ {
const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; assert(gamelog_action == nullptr);
size_t length = 0; assert(gamelog_actions == 0);
for (const LoggedAction *la = _gamelog_action; la != laend; la++) { if (IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY)) {
const LoggedChange *lcend = &la->change[la->changes]; byte type;
for (LoggedChange *lc = la->change; lc != lcend; lc++) { while ((type = SlReadByte()) != GLAT_NONE) {
assert((uint)lc->ct < lengthof(_glog_desc)); if (type >= GLAT_END) SlErrorCorrupt("Invalid gamelog action type");
length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1;
gamelog_action = ReallocT(gamelog_action, gamelog_actions + 1);
LoggedAction *la = &gamelog_action[gamelog_actions++];
memset(la, 0, sizeof(*la));
la->at = (GamelogActionType)type;
SlObject(la, _gamelog_desc);
} }
length += 4; return;
} }
length++;
SlSetLength(length); while (SlIterateArray() != -1) {
gamelog_action = ReallocT(gamelog_action, gamelog_actions + 1);
LoggedAction *la = &gamelog_action[gamelog_actions++];
memset(la, 0, sizeof(*la));
for (LoggedAction *la = _gamelog_action; la != laend; la++) { SlObject(la, _gamelog_desc);
SlWriteByte(la->at); }
SlObject(la, _glog_action_desc); }
const LoggedChange *lcend = &la->change[la->changes]; static void Save_GLOG()
for (LoggedChange *lc = la->change; lc != lcend; lc++) { {
SlWriteByte(lc->ct); const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
assert((uint)lc->ct < GLCT_END);
SlObject(lc, _glog_desc[lc->ct]); uint i = 0;
} for (LoggedAction *la = _gamelog_action; la != laend; la++, i++) {
SlWriteByte(GLCT_NONE); SlSetArrayIndex(i);
SlObject(la, _gamelog_desc);
} }
SlWriteByte(GLAT_NONE);
} }
static void Load_GLOG() static void Load_GLOG()
@ -169,7 +339,7 @@ static void Check_GLOG()
} }
static const ChunkHandler gamelog_chunk_handlers[] = { static const ChunkHandler gamelog_chunk_handlers[] = {
{ 'GLOG', Save_GLOG, Load_GLOG, nullptr, Check_GLOG, CH_RIFF } { 'GLOG', Save_GLOG, Load_GLOG, nullptr, Check_GLOG, CH_ARRAY }
}; };
extern const ChunkHandlerTable _gamelog_chunk_handlers(gamelog_chunk_handlers); extern const ChunkHandlerTable _gamelog_chunk_handlers(gamelog_chunk_handlers);

Loading…
Cancel
Save