Move upstream saveload to src/saveload/, move jgrpp saveload to src/sl/

Leave afterload in src/saveload/
This commit is contained in:
Jonathan G Rennison
2023-06-07 23:29:52 +01:00
parent 3c9ce6f9a5
commit ac2f9a21e8
181 changed files with 16887 additions and 16888 deletions

View File

@@ -8,29 +8,15 @@
/** @file cheat_sl.cpp Code handling saving and loading of cheats */
#include "../stdafx.h"
#include "../cheat_type.h"
#include "../debug.h"
#include "saveload.h"
#include "compat/cheat_sl_compat.h"
#include <map>
#include <string>
#include "../cheat_type.h"
#include "../safeguards.h"
extern std::map<std::string, Cheat> _unknown_cheats;
struct ExtraCheatNameDesc {
const char *name;
Cheat *cht;
};
static ExtraCheatNameDesc _extra_cheat_descs[] = {
{ "inflation_cost", &_extra_cheats.inflation_cost },
{ "inflation_income", &_extra_cheats.inflation_income },
{ "station_rating", &_extra_cheats.station_rating },
{ "town_rating", &_extra_cheats.town_rating },
};
namespace upstream_sl {
static const SaveLoad _cheats_desc[] = {
SLE_VAR(Cheats, magic_bulldozer.been_used, SLE_BOOL),
@@ -41,146 +27,60 @@ static const SaveLoad _cheats_desc[] = {
SLE_VAR(Cheats, money.value, SLE_BOOL),
SLE_VAR(Cheats, crossing_tunnels.been_used, SLE_BOOL),
SLE_VAR(Cheats, crossing_tunnels.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, no_jetcrash.been_used, SLE_BOOL),
SLE_VAR(Cheats, no_jetcrash.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, change_date.been_used, SLE_BOOL),
SLE_VAR(Cheats, change_date.value, SLE_BOOL),
SLE_VAR(Cheats, setup_prod.been_used, SLE_BOOL),
SLE_VAR(Cheats, setup_prod.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, edit_max_hl.been_used, SLE_BOOL),
SLE_VAR(Cheats, edit_max_hl.value, SLE_BOOL),
};
/**
* Save the cheat values.
*/
static void Save_CHTS()
{
SlSetLength(std::size(_cheats_desc));
SlObject(&_cheats, _cheats_desc);
}
/**
* Load the cheat values.
*/
static void Load_CHTS()
{
size_t count = SlGetFieldLength();
std::vector<SaveLoad> slt;
struct CHTSChunkHandler : ChunkHandler {
CHTSChunkHandler() : ChunkHandler('CHTS', CH_TABLE) {}
/* Cheats were added over the years without a savegame bump. They are
* stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
* are stored for this savegame. So read only "count" SLE_BOOLs (and in
* result "count / 2" cheats). */
for (auto &sld : _cheats_desc) {
count--;
slt.push_back(sld);
void Save() const override
{
SlTableHeader(_cheats_desc);
if (count == 0) break;
SlSetArrayIndex(0);
SlObject(&_cheats, _cheats_desc);
}
SlObject(&_cheats, slt);
}
void Load() const override
{
std::vector<SaveLoad> slt = SlCompatTableHeader(_cheats_desc, _cheats_sl_compat);
/**
* Load the extra cheat values.
*/
static void Load_CHTX()
{
struct CheatsExtLoad {
char name[256];
Cheat cht;
};
if (IsSavegameVersionBefore(SLV_TABLE_CHUNKS)) {
size_t count = SlGetFieldLength();
std::vector<SaveLoad> oslt;
static const SaveLoad _cheats_ext_load_desc[] = {
SLE_STR(CheatsExtLoad, name, SLE_STRB, 256),
SLE_VAR(CheatsExtLoad, cht.been_used, SLE_BOOL),
SLE_VAR(CheatsExtLoad, cht.value, SLE_BOOL),
};
/* Cheats were added over the years without a savegame bump. They are
* stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
* are stored for this savegame. So read only "count" SLE_BOOLs (and in
* result "count / 2" cheats). */
for (auto &sld : slt) {
count--;
oslt.push_back(sld);
CheatsExtLoad current_cheat;
uint32 chunk_flags = SlReadUint32();
// flags are not in use yet, reserve for future expansion
if (chunk_flags != 0) SlErrorCorruptFmt("CHTX chunk: unknown chunk header flags: 0x%X", chunk_flags);
uint32 cheat_count = SlReadUint32();
for (uint32 i = 0; i < cheat_count; i++) {
SlObject(&current_cheat, _cheats_ext_load_desc);
bool found = false;
for (uint j = 0; j < lengthof(_extra_cheat_descs); j++) {
const ExtraCheatNameDesc &desc = _extra_cheat_descs[j];
if (strcmp(desc.name, current_cheat.name) == 0) {
*(desc.cht) = current_cheat.cht;
found = true;
break;
if (count == 0) break;
}
slt = oslt;
}
if (!found) {
DEBUG(sl, 1, "CHTX chunk: Could not find cheat: '%s'", current_cheat.name);
_unknown_cheats[current_cheat.name] = current_cheat.cht;
}
if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
SlObject(&_cheats, slt);
if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries");
}
}
/**
* Save the extra cheat values.
*/
static void Save_CHTX()
{
struct CheatsExtSave {
const char *name;
Cheat cht;
};
static const SaveLoad _cheats_ext_save_desc[] = {
SLE_STR(CheatsExtSave, name, SLE_STR, 0),
SLE_VAR(CheatsExtSave, cht.been_used, SLE_BOOL),
SLE_VAR(CheatsExtSave, cht.value, SLE_BOOL),
};
SlAutolength([](void *) {
SlWriteUint32(0); // flags
SlWriteUint32((uint32)(lengthof(_extra_cheat_descs) + _unknown_cheats.size())); // cheat count
for (uint j = 0; j < lengthof(_extra_cheat_descs); j++) {
CheatsExtSave save = { _extra_cheat_descs[j].name, *(_extra_cheat_descs[j].cht) };
SlObject(&save, _cheats_ext_save_desc);
}
for (const auto &iter : _unknown_cheats) {
CheatsExtSave save = { iter.first.c_str(), iter.second };
SlObject(&save, _cheats_ext_save_desc);
}
}, nullptr);
}
/**
* Internal structure used in SaveSettingsPatx() and SaveSettingsPlyx()
*/
struct SettingsExtSave {
uint32 flags;
const char *name;
uint32 setting_length;
};
static const SaveLoad _settings_ext_save_desc[] = {
SLE_VAR(SettingsExtSave, flags, SLE_UINT32),
SLE_STR(SettingsExtSave, name, SLE_STR, 0),
SLE_VAR(SettingsExtSave, setting_length, SLE_UINT32),
};
/** Chunk handlers related to cheats. */
static const ChunkHandler cheat_chunk_handlers[] = {
{ 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_RIFF },
{ 'CHTX', Save_CHTX, Load_CHTX, nullptr, nullptr, CH_RIFF },
static const CHTSChunkHandler CHTS;
static const ChunkHandlerRef cheat_chunk_handlers[] = {
CHTS,
};
extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers);
}