Codechange: Use ChunkHandlers sub-classes

This commit is contained in:
glx22
2021-06-07 23:24:37 +02:00
committed by Loïc Guilloux
parent c1a9fe6fbd
commit 2c941cd8b3
35 changed files with 1953 additions and 1665 deletions

View File

@@ -57,77 +57,81 @@ static void SaveReal_AIPL(int *index_ptr)
if (Company::IsValidAiID(index)) AI::Save(index);
}
static void Load_AIPL()
{
const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat);
struct AIPLChunkHandler : ChunkHandler {
AIPLChunkHandler() : ChunkHandler('AIPL', CH_TABLE) {}
/* Free all current data */
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr);
}
void Load() const override
{
const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat);
CompanyID index;
while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
_ai_saveload_is_random = false;
_ai_saveload_version = -1;
SlObject(nullptr, slt);
if (_networking && !_network_server) {
if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
continue;
/* Free all current data */
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr);
}
AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
if (_ai_saveload_name.empty()) {
/* A random AI. */
config->Change(nullptr, -1, false, true);
} else {
config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random);
if (!config->HasScript()) {
/* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */
config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random);
CompanyID index;
while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
_ai_saveload_is_random = false;
_ai_saveload_version = -1;
SlObject(nullptr, slt);
if (_networking && !_network_server) {
if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
continue;
}
AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
if (_ai_saveload_name.empty()) {
/* A random AI. */
config->Change(nullptr, -1, false, true);
} else {
config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random);
if (!config->HasScript()) {
if (_ai_saveload_name.compare("%_dummy") != 0) {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "A random other AI will be loaded in its place.");
/* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */
config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random);
if (!config->HasScript()) {
if (_ai_saveload_name.compare("%_dummy") != 0) {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "A random other AI will be loaded in its place.");
} else {
Debug(script, 0, "The savegame had no AIs available at the time of saving.");
Debug(script, 0, "A random available AI will be loaded now.");
}
} else {
Debug(script, 0, "The savegame had no AIs available at the time of saving.");
Debug(script, 0, "A random available AI will be loaded now.");
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
}
} else {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
/* Make sure the AI doesn't get the saveload data, as it was not the
* writer of the saveload data in the first place */
_ai_saveload_version = -1;
}
/* Make sure the AI doesn't get the saveload data, as it was not the
* writer of the saveload data in the first place */
_ai_saveload_version = -1;
}
config->StringToSettings(_ai_saveload_settings);
/* Start the AI directly if it was active in the savegame */
if (Company::IsValidAiID(index)) {
AI::StartNew(index, false);
AI::Load(index, _ai_saveload_version);
}
}
}
config->StringToSettings(_ai_saveload_settings);
void Save() const override
{
SlTableHeader(_ai_company_desc);
/* Start the AI directly if it was active in the savegame */
if (Company::IsValidAiID(index)) {
AI::StartNew(index, false);
AI::Load(index, _ai_saveload_version);
for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
SlSetArrayIndex(i);
SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
}
}
}
};
static void Save_AIPL()
{
SlTableHeader(_ai_company_desc);
for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
SlSetArrayIndex(i);
SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
}
}
static const ChunkHandler AIPL{ 'AIPL', Save_AIPL, Load_AIPL, nullptr, nullptr, CH_TABLE };
static const AIPLChunkHandler AIPL;
static const ChunkHandlerRef ai_chunk_handlers[] = {
AIPL,
};