Codechange: Place gamelog into its own class, along with internal data.

Data is now stored in vectors to avoid manual memory management and
passing lengths around.
This commit is contained in:
Peter Nelson
2023-05-01 18:14:31 +01:00
committed by PeterN
parent 47a8d12f0e
commit 00bf42353a
18 changed files with 308 additions and 345 deletions

View File

@@ -298,12 +298,11 @@ public:
void Save(LoggedAction *la) const override
{
SlSetStructListLength(la->changes);
SlSetStructListLength(la->change.size());
const LoggedChange *lcend = &la->change[la->changes];
for (LoggedChange *lc = la->change; lc != lcend; lc++) {
assert((uint)lc->ct < GLCT_END);
SlObject(lc, this->GetDescription());
for (auto &lc : la->change) {
assert((uint)lc.ct < GLCT_END);
SlObject(&lc, this->GetDescription());
}
}
@@ -315,27 +314,20 @@ public:
if (type >= GLCT_END) SlErrorCorrupt("Invalid gamelog change type");
GamelogChangeType ct = (GamelogChangeType)type;
la->change = ReallocT(la->change, la->changes + 1);
LoggedChange *lc = &la->change[la->changes++];
memset(lc, 0, sizeof(*lc));
lc->ct = ct;
SlObject(lc, this->GetLoadDescription());
LoggedChange &lc = la->change.emplace_back();
lc.ct = ct;
SlObject(&lc, this->GetLoadDescription());
}
return;
}
size_t length = SlGetStructListLength(UINT32_MAX);
la->change = ReallocT(la->change, length);
la->changes = (uint32)length;
la->change.reserve(length);
for (size_t i = 0; i < length; i++) {
LoggedChange *lc = &la->change[i];
memset(lc, 0, sizeof(*lc));
lc->ct = (GamelogChangeType)SlReadByte();
SlObject(lc, this->GetLoadDescription());
LoggedChange &lc = la->change.emplace_back();
lc.ct = (GamelogChangeType)SlReadByte();
SlObject(&lc, this->GetLoadDescription());
}
}
@@ -352,10 +344,9 @@ static const SaveLoad _gamelog_desc[] = {
struct GLOGChunkHandler : ChunkHandler {
GLOGChunkHandler() : ChunkHandler('GLOG', CH_TABLE) {}
void LoadCommon(LoggedAction *&gamelog_action, uint &gamelog_actions) const
void LoadCommon(Gamelog &gamelog) const
{
assert(gamelog_action == nullptr);
assert(gamelog_actions == 0);
assert(gamelog.data->action.empty());
const std::vector<SaveLoad> slt = SlCompatTableHeader(_gamelog_desc, _gamelog_sl_compat);
@@ -364,22 +355,16 @@ struct GLOGChunkHandler : ChunkHandler {
while ((type = SlReadByte()) != GLAT_NONE) {
if (type >= GLAT_END) SlErrorCorrupt("Invalid gamelog action type");
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, slt);
LoggedAction &la = gamelog.data->action.emplace_back();
la.at = (GamelogActionType)type;
SlObject(&la, slt);
}
return;
}
while (SlIterateArray() != -1) {
gamelog_action = ReallocT(gamelog_action, gamelog_actions + 1);
LoggedAction *la = &gamelog_action[gamelog_actions++];
memset(la, 0, sizeof(*la));
SlObject(la, slt);
LoggedAction &la = gamelog.data->action.emplace_back();
SlObject(&la, slt);
}
}
@@ -387,23 +372,21 @@ struct GLOGChunkHandler : ChunkHandler {
{
SlTableHeader(_gamelog_desc);
const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
uint i = 0;
for (LoggedAction *la = _gamelog_action; la != laend; la++, i++) {
SlSetArrayIndex(i);
SlObject(la, _gamelog_desc);
for (LoggedAction &la : _gamelog.data->action) {
SlSetArrayIndex(i++);
SlObject(&la, _gamelog_desc);
}
}
void Load() const override
{
this->LoadCommon(_gamelog_action, _gamelog_actions);
this->LoadCommon(_gamelog);
}
void LoadCheck(size_t) const override
{
this->LoadCommon(_load_check_data.gamelog_action, _load_check_data.gamelog_actions);
this->LoadCommon(_load_check_data.gamelog);
}
};