Save/load: Add extra cheats savegame chunk

This commit is contained in:
Jonathan G Rennison
2020-04-18 10:51:02 +01:00
parent 17f25690eb
commit 3e7618f519
6 changed files with 130 additions and 1 deletions

View File

@@ -9,11 +9,25 @@
#include "../stdafx.h"
#include "../cheat_type.h"
#include "../debug.h"
#include "saveload.h"
#include <map>
#include <string>
#include "../safeguards.h"
extern std::map<std::string, Cheat> _unknown_cheats;
struct ExtraCheatNameDesc {
const char *name;
Cheat *cht;
};
static ExtraCheatNameDesc _extra_cheat_descs[] = {
};
/**
* Save the cheat values.
*/
@@ -47,7 +61,100 @@ static void Load_CHTS()
}
}
/**
* Load the extra cheat values.
*/
static void Load_CHTX()
{
struct CheatsExtLoad {
char name[256];
Cheat cht;
};
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),
SLE_END()
};
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 (!found) {
DEBUG(sl, 1, "CHTX chunk: Could not find cheat: '%s'", current_cheat.name);
_unknown_cheats[current_cheat.name] = current_cheat.cht;
}
}
}
/**
* 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),
SLE_END()
};
SlAutolength([](void *) {
SlWriteUint32(0); // flags
SlWriteUint32(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),
SLE_END()
};
/** Chunk handlers related to cheats. */
extern const ChunkHandler _cheat_chunk_handlers[] = {
{ 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_RIFF | CH_LAST},
{ 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_RIFF},
{ 'CHTX', Save_CHTX, Load_CHTX, nullptr, nullptr, CH_RIFF | CH_LAST},
};