Add more citymania data to savegame

This commit is contained in:
dP
2020-06-09 02:30:07 +03:00
parent 2816c38d3d
commit 581a482692
20 changed files with 579 additions and 448 deletions
+4 -66
View File
@@ -8,16 +8,12 @@
/** @file storage_sl.cpp Code handling saving and loading of persistent storages. */
#include "../stdafx.h"
#include "../debug.h"
#include "../newgrf_storage.h"
#include "citymania_sl.h"
#include "../citymania/cm_saveload.hpp"
#include "saveload.h"
#include "../safeguards.h"
// Luukland_Citybuilder grf id actually
#define CITYMANIA_GRFID 0x534B0501U
/** Description of the data to save and load in #PersistentStorage. */
static const SaveLoad _storage_desc[] = {
SLE_CONDVAR(PersistentStorage, grfid, SLE_UINT32, SLV_6, SL_MAX_VERSION),
@@ -26,69 +22,15 @@ static const SaveLoad _storage_desc[] = {
SLE_END()
};
// static void hexdump(uint8 *data) {
// uint i;
// for (i = 0; i < 20; i++) {
// if (i) fprintf(stderr, " : ");
// fprintf(stderr, "%02x", data[i]);
// }
// fprintf(stderr, i >= 20 ? " ...\n" : "\n");
// }
/** Load persistent storage data. */
static void Load_PSAC()
{
int index;
/*
CITYMANIA_GRFID is used to hide extra data in persitant storages.
To save a bit of memory we only keep at most one PS with this
grfid and it is later discarded on save.
*/
PersistentStorage *ps = NULL;
u8vector cmdata;
uint chunk_size = IsSavegameVersionBefore(SLV_EXTEND_PERSISTENT_STORAGE) ? 64 : 1024;
DEBUG(sl, 2, "CityMania savegame data chunk size: %u", chunk_size);
while ((index = SlIterateArray()) != -1) {
if (ps == NULL) {
assert(PersistentStorage::CanAllocateItem());
ps = new (index) PersistentStorage(0, 0, 0);
}
assert(PersistentStorage::CanAllocateItem());
PersistentStorage *ps = new (index) PersistentStorage(0, 0, 0);
SlObject(ps, _storage_desc);
if (ps->grfid == CITYMANIA_GRFID) {
uint8 *data = (uint8 *)(ps->storage);
cmdata.insert(cmdata.end(), data, data + chunk_size);
} else {
ps = NULL;
}
}
CM_DecodeData(cmdata);
}
static void Save_CMDataAsPSAC() {
uint32 grfid = CITYMANIA_GRFID;
u8vector data = CM_EncodeData();
int n_chunks = (data.size() + 1023) / 1024;
data.resize(n_chunks * 1024);
uint8 *ptr = &data[0];
SaveLoadGlobVarList _desc[] = {
SLEG_CONDVAR(grfid, SLE_UINT32, SLV_6, SL_MAX_VERSION),
SLEG_CONDARR(*ptr, SLE_UINT32, 256, SLV_EXTEND_PERSISTENT_STORAGE, SL_MAX_VERSION),
SLEG_END()
};
uint index = 0;
for (PersistentStorage *ps : PersistentStorage::Iterate()) {
if (ps->grfid != CITYMANIA_GRFID)
index = max(index, ps->index + 1);
}
for (int i = 0; i < n_chunks; i++, ptr += 1024) {
_desc[1].address = (void *)ptr;
SlSetArrayIndex(index + i);
SlGlobList(_desc);
}
}
@@ -97,17 +39,13 @@ static void Save_PSAC()
{
/* Write the industries */
for (PersistentStorage *ps : PersistentStorage::Iterate()) {
if (ps->grfid == CITYMANIA_GRFID)
continue;
ps->ClearChanges();
SlSetArrayIndex(ps->index);
SlObject(ps, _storage_desc);
}
Save_CMDataAsPSAC();
}
/** Chunk handler for persistent storages. */
extern const ChunkHandler _persistent_storage_chunk_handlers[] = {
{ 'PSAC', Save_PSAC, Load_PSAC, nullptr, nullptr, CH_ARRAY | CH_LAST},
{ 'PSAC', citymania::Save_PSAC, citymania::Load_PSAC, nullptr, nullptr, CH_ARRAY | CH_LAST},
};