1
0
Fork 0

Codechange: pass oldloader LoadgameState by reference instead of pointer

pull/13577/head
Rubidium 2025-02-16 11:18:45 +01:00 committed by rubidium42
parent 44506ebc86
commit e7595c6c85
4 changed files with 64 additions and 85 deletions

View File

@ -58,14 +58,14 @@ static inline uint8_t CalcOldVarLen(OldChunkType type)
* Reads a byte from a file (do not call yourself, use ReadByte()) * Reads a byte from a file (do not call yourself, use ReadByte())
* *
*/ */
static uint8_t ReadByteFromFile(LoadgameState *ls) static uint8_t ReadByteFromFile(LoadgameState &ls)
{ {
/* To avoid slow reads, we read BUFFER_SIZE of bytes per time /* To avoid slow reads, we read BUFFER_SIZE of bytes per time
and just return a byte per time */ and just return a byte per time */
if (ls->buffer_cur >= ls->buffer_count) { if (ls.buffer_cur >= ls.buffer_count) {
/* Read some new bytes from the file */ /* Read some new bytes from the file */
int count = static_cast<int>(fread(ls->buffer, 1, BUFFER_SIZE, *ls->file)); int count = static_cast<int>(fread(ls.buffer.data(), 1, ls.buffer.size(), *ls.file));
/* We tried to read, but there is nothing in the file anymore.. */ /* We tried to read, but there is nothing in the file anymore.. */
if (count == 0) { if (count == 0) {
@ -73,11 +73,11 @@ static uint8_t ReadByteFromFile(LoadgameState *ls)
throw std::exception(); throw std::exception();
} }
ls->buffer_count = count; ls.buffer_count = count;
ls->buffer_cur = 0; ls.buffer_cur = 0;
} }
return ls->buffer[ls->buffer_cur++]; return ls.buffer[ls.buffer_cur++];
} }
/** /**
@ -85,7 +85,7 @@ static uint8_t ReadByteFromFile(LoadgameState *ls)
* Reads a byte from the buffer and decompress if needed * Reads a byte from the buffer and decompress if needed
* *
*/ */
uint8_t ReadByte(LoadgameState *ls) uint8_t ReadByte(LoadgameState &ls)
{ {
/* Old savegames have a nice compression algorithm (RLE) /* Old savegames have a nice compression algorithm (RLE)
which means that we have a chunk, which starts with a length which means that we have a chunk, which starts with a length
@ -93,25 +93,25 @@ uint8_t ReadByte(LoadgameState *ls)
that many times ( + 1). Else, we need to read that amount of bytes. that many times ( + 1). Else, we need to read that amount of bytes.
Works pretty well if you have many zeros behind each other */ Works pretty well if you have many zeros behind each other */
if (ls->chunk_size == 0) { if (ls.chunk_size == 0) {
/* Read new chunk */ /* Read new chunk */
int8_t new_byte = ReadByteFromFile(ls); int8_t new_byte = ReadByteFromFile(ls);
if (new_byte < 0) { if (new_byte < 0) {
/* Repeat next char for new_byte times */ /* Repeat next char for new_byte times */
ls->decoding = true; ls.decoding = true;
ls->decode_char = ReadByteFromFile(ls); ls.decode_char = ReadByteFromFile(ls);
ls->chunk_size = -new_byte + 1; ls.chunk_size = -new_byte + 1;
} else { } else {
ls->decoding = false; ls.decoding = false;
ls->chunk_size = new_byte + 1; ls.chunk_size = new_byte + 1;
} }
} }
ls->total_read++; ls.total_read++;
ls->chunk_size--; ls.chunk_size--;
return ls->decoding ? ls->decode_char : ReadByteFromFile(ls); return ls.decoding ? ls.decode_char : ReadByteFromFile(ls);
} }
/** /**
@ -119,7 +119,7 @@ uint8_t ReadByte(LoadgameState *ls)
* Loads a chunk from the old savegame * Loads a chunk from the old savegame
* *
*/ */
bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks) bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks)
{ {
for (const OldChunks *chunk = chunks; chunk->type != OC_END; chunk++) { for (const OldChunks *chunk = chunks; chunk->type != OC_END; chunk++) {
if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) || if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) ||
@ -144,8 +144,8 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
break; break;
case OC_ASSERT: case OC_ASSERT:
Debug(oldloader, 4, "Assert point: 0x{:X} / 0x{:X}", ls->total_read, (uint)(size_t)chunk->ptr + _bump_assert_value); Debug(oldloader, 4, "Assert point: 0x{:X} / 0x{:X}", ls.total_read, reinterpret_cast<size_t>(chunk->ptr) + _bump_assert_value);
if (ls->total_read != (size_t)chunk->ptr + _bump_assert_value) throw std::exception(); if (ls.total_read != reinterpret_cast<size_t>(chunk->ptr) + _bump_assert_value) throw std::exception();
default: break; default: break;
} }
} else { } else {
@ -190,28 +190,6 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
return true; return true;
} }
/**
*
* Initialize some data before reading
*
*/
static void InitLoading(LoadgameState *ls)
{
ls->chunk_size = 0;
ls->total_read = 0;
ls->decoding = false;
ls->decode_char = 0;
ls->buffer_cur = 0;
ls->buffer_count = 0;
memset(ls->buffer, 0, BUFFER_SIZE);
_bump_assert_value = 0;
_settings_game.construction.freeform_edges = false; // disable so we can convert map array (SetTileType is still used)
}
/** /**
* Verifies the title has a valid checksum * Verifies the title has a valid checksum
* @param title title and checksum * @param title title and checksum
@ -253,15 +231,16 @@ static std::tuple<SavegameType, std::string> DetermineOldSavegameTypeAndName(Fil
return { SGT_INVALID, "(broken) Unknown" }; return { SGT_INVALID, "(broken) Unknown" };
} }
typedef bool LoadOldMainProc(LoadgameState *ls); typedef bool LoadOldMainProc(LoadgameState &ls);
bool LoadOldSaveGame(const std::string &file) bool LoadOldSaveGame(const std::string &file)
{ {
LoadgameState ls; LoadgameState ls{};
Debug(oldloader, 3, "Trying to load a TTD(Patch) savegame"); Debug(oldloader, 3, "Trying to load a TTD(Patch) savegame");
InitLoading(&ls); _bump_assert_value = 0;
_settings_game.construction.freeform_edges = false; // disable so we can convert map array (SetTileType is still used)
/* Open file */ /* Open file */
ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY); ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY);
@ -289,7 +268,7 @@ bool LoadOldSaveGame(const std::string &file)
bool game_loaded; bool game_loaded;
try { try {
game_loaded = proc != nullptr && proc(&ls); game_loaded = proc != nullptr && proc(ls);
} catch (...) { } catch (...) {
game_loaded = false; game_loaded = false;
} }

View File

@ -19,16 +19,16 @@ static const uint OLD_MAP_SIZE = 256;
struct LoadgameState { struct LoadgameState {
std::optional<FileHandle> file; std::optional<FileHandle> file;
uint chunk_size; uint chunk_size = 0;
bool decoding; bool decoding = false;
uint8_t decode_char; uint8_t decode_char = 0;
uint buffer_count; uint buffer_count = 0;
uint buffer_cur; uint buffer_cur = 0;
uint8_t buffer[BUFFER_SIZE]; std::array<uint8_t, BUFFER_SIZE> buffer{};
uint total_read; uint total_read = 0;
}; };
/* OldChunk-Type */ /* OldChunk-Type */
@ -75,7 +75,7 @@ enum OldChunkType : uint32_t {
DECLARE_ENUM_AS_BIT_SET(OldChunkType) DECLARE_ENUM_AS_BIT_SET(OldChunkType)
typedef bool OldChunkProc(LoadgameState *ls, int num); typedef bool OldChunkProc(LoadgameState &ls, int num);
typedef void *OffsetProc(void *base); typedef void *OffsetProc(void *base);
struct OldChunks { struct OldChunks {
@ -88,19 +88,19 @@ struct OldChunks {
}; };
extern uint _bump_assert_value; extern uint _bump_assert_value;
uint8_t ReadByte(LoadgameState *ls); uint8_t ReadByte(LoadgameState &ls);
bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks); bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks);
bool LoadTTDMain(LoadgameState *ls); bool LoadTTDMain(LoadgameState &ls);
bool LoadTTOMain(LoadgameState *ls); bool LoadTTOMain(LoadgameState &ls);
inline uint16_t ReadUint16(LoadgameState *ls) inline uint16_t ReadUint16(LoadgameState &ls)
{ {
uint8_t x = ReadByte(ls); uint8_t x = ReadByte(ls);
return x | ReadByte(ls) << 8; return x | ReadByte(ls) << 8;
} }
inline uint32_t ReadUint32(LoadgameState *ls) inline uint32_t ReadUint32(LoadgameState &ls)
{ {
uint16_t x = ReadUint16(ls); uint16_t x = ReadUint16(ls);
return x | ReadUint16(ls) << 16; return x | ReadUint16(ls) << 16;

View File

@ -616,7 +616,7 @@ static const OldChunks town_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldTown(LoadgameState *ls, int num) static bool LoadOldTown(LoadgameState &ls, int num)
{ {
Town *t = new (num) Town(); Town *t = new (num) Town();
if (!LoadChunk(ls, t, town_chunk)) return false; if (!LoadChunk(ls, t, town_chunk)) return false;
@ -639,7 +639,7 @@ static const OldChunks order_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldOrder(LoadgameState *ls, int num) static bool LoadOldOrder(LoadgameState &ls, int num)
{ {
if (!LoadChunk(ls, nullptr, order_chunk)) return false; if (!LoadChunk(ls, nullptr, order_chunk)) return false;
@ -658,7 +658,7 @@ static bool LoadOldOrder(LoadgameState *ls, int num)
return true; return true;
} }
static bool LoadOldAnimTileList(LoadgameState *ls, int) static bool LoadOldAnimTileList(LoadgameState &ls, int)
{ {
TileIndex anim_list[256]; TileIndex anim_list[256];
const OldChunks anim_chunk[] = { const OldChunks anim_chunk[] = {
@ -683,7 +683,7 @@ static const OldChunks depot_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldDepot(LoadgameState *ls, int num) static bool LoadOldDepot(LoadgameState &ls, int num)
{ {
Depot *d = new (num) Depot(); Depot *d = new (num) Depot();
if (!LoadChunk(ls, d, depot_chunk)) return false; if (!LoadChunk(ls, d, depot_chunk)) return false;
@ -714,7 +714,7 @@ static const OldChunks goods_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldGood(LoadgameState *ls, int num) static bool LoadOldGood(LoadgameState &ls, int num)
{ {
/* for TTO games, 12th (num == 11) goods entry is created in the Station constructor */ /* for TTO games, 12th (num == 11) goods entry is created in the Station constructor */
if (_savegame_type == SGT_TTO && num == 11) return true; if (_savegame_type == SGT_TTO && num == 11) return true;
@ -771,7 +771,7 @@ static const OldChunks station_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldStation(LoadgameState *ls, int num) static bool LoadOldStation(LoadgameState &ls, int num)
{ {
Station *st = new (num) Station(); Station *st = new (num) Station();
_current_station_id = num; _current_station_id = num;
@ -853,7 +853,7 @@ static const OldChunks industry_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldIndustry(LoadgameState *ls, int num) static bool LoadOldIndustry(LoadgameState &ls, int num)
{ {
Industry *i = new (num) Industry(); Industry *i = new (num) Industry();
if (!LoadChunk(ls, i, industry_chunk)) return false; if (!LoadChunk(ls, i, industry_chunk)) return false;
@ -891,7 +891,7 @@ static const OldChunks _company_yearly_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldCompanyYearly(LoadgameState *ls, int num) static bool LoadOldCompanyYearly(LoadgameState &ls, int num)
{ {
Company *c = Company::Get(_current_company_id); Company *c = Company::Get(_current_company_id);
@ -918,7 +918,7 @@ static const OldChunks _company_economy_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldCompanyEconomy(LoadgameState *ls, int) static bool LoadOldCompanyEconomy(LoadgameState &ls, int)
{ {
Company *c = Company::Get(_current_company_id); Company *c = Company::Get(_current_company_id);
@ -979,7 +979,7 @@ static const OldChunks _company_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldCompany(LoadgameState *ls, int num) static bool LoadOldCompany(LoadgameState &ls, int num)
{ {
Company *c = new (num) Company(); Company *c = new (num) Company();
@ -1121,10 +1121,10 @@ static const OldChunks vehicle_empty_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldVehicleUnion(LoadgameState *ls, int) static bool LoadOldVehicleUnion(LoadgameState &ls, int)
{ {
Vehicle *v = Vehicle::GetIfValid(_current_vehicle_id); Vehicle *v = Vehicle::GetIfValid(_current_vehicle_id);
uint temp = ls->total_read; uint temp = ls.total_read;
bool res; bool res;
if (v == nullptr) { if (v == nullptr) {
@ -1142,7 +1142,7 @@ static bool LoadOldVehicleUnion(LoadgameState *ls, int)
} }
/* This chunk size should always be 10 bytes */ /* This chunk size should always be 10 bytes */
if (ls->total_read - temp != 10) { if (ls.total_read - temp != 10) {
Debug(oldloader, 0, "Assert failed in VehicleUnion: invalid chunk size"); Debug(oldloader, 0, "Assert failed in VehicleUnion: invalid chunk size");
return false; return false;
} }
@ -1247,7 +1247,7 @@ static const OldChunks vehicle_chunk[] = {
* @param num The number of vehicles to load. * @param num The number of vehicles to load.
* @return True iff loading went without problems. * @return True iff loading went without problems.
*/ */
bool LoadOldVehicle(LoadgameState *ls, int num) bool LoadOldVehicle(LoadgameState &ls, int num)
{ {
/* Read the TTDPatch flags, because we need some info from it */ /* Read the TTDPatch flags, because we need some info from it */
ReadTTDPatchFlags(); ReadTTDPatchFlags();
@ -1390,7 +1390,7 @@ bool LoadOldVehicle(LoadgameState *ls, int num)
* @param index The index of the loaded custom string. * @param index The index of the loaded custom string.
* @return Always true. * @return Always true.
*/ */
bool LoadOldCustomString(LoadgameState *ls, int index) bool LoadOldCustomString(LoadgameState &ls, int index)
{ {
/* /*
* Data is stored in fixed size "cells"; read these completely. * Data is stored in fixed size "cells"; read these completely.
@ -1414,7 +1414,7 @@ static const OldChunks sign_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldSign(LoadgameState *ls, int num) static bool LoadOldSign(LoadgameState &ls, int num)
{ {
Sign *si = new (num) Sign(); Sign *si = new (num) Sign();
if (!LoadChunk(ls, si, sign_chunk)) return false; if (!LoadChunk(ls, si, sign_chunk)) return false;
@ -1456,13 +1456,13 @@ static const OldChunks engine_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldEngine(LoadgameState *ls, int num) static bool LoadOldEngine(LoadgameState &ls, int num)
{ {
Engine *e = _savegame_type == SGT_TTO ? &_old_engines[num] : GetTempDataEngine(num); Engine *e = _savegame_type == SGT_TTO ? &_old_engines[num] : GetTempDataEngine(num);
return LoadChunk(ls, e, engine_chunk); return LoadChunk(ls, e, engine_chunk);
} }
static bool LoadOldEngineName(LoadgameState *ls, int num) static bool LoadOldEngineName(LoadgameState &ls, int num)
{ {
Engine *e = GetTempDataEngine(num); Engine *e = GetTempDataEngine(num);
e->name = CopyFromOldName(RemapOldStringID(ReadUint16(ls))); e->name = CopyFromOldName(RemapOldStringID(ReadUint16(ls)));
@ -1478,7 +1478,7 @@ static const OldChunks subsidy_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldSubsidy(LoadgameState *ls, int num) static bool LoadOldSubsidy(LoadgameState &ls, int num)
{ {
Subsidy *s = new (num) Subsidy(); Subsidy *s = new (num) Subsidy();
bool ret = LoadChunk(ls, s, subsidy_chunk); bool ret = LoadChunk(ls, s, subsidy_chunk);
@ -1507,7 +1507,7 @@ static const OldChunks game_difficulty_chunk[] = {
OCL_END() OCL_END()
}; };
static bool LoadOldGameDifficulty(LoadgameState *ls, int) static bool LoadOldGameDifficulty(LoadgameState &ls, int)
{ {
bool ret = LoadChunk(ls, &_settings_game.difficulty, game_difficulty_chunk); bool ret = LoadChunk(ls, &_settings_game.difficulty, game_difficulty_chunk);
_settings_game.difficulty.max_loan *= 1000; _settings_game.difficulty.max_loan *= 1000;
@ -1515,7 +1515,7 @@ static bool LoadOldGameDifficulty(LoadgameState *ls, int)
} }
static bool LoadOldMapPart1(LoadgameState *ls, int) static bool LoadOldMapPart1(LoadgameState &ls, int)
{ {
if (_savegame_type == SGT_TTO) { if (_savegame_type == SGT_TTO) {
Map::Allocate(OLD_MAP_SIZE, OLD_MAP_SIZE); Map::Allocate(OLD_MAP_SIZE, OLD_MAP_SIZE);
@ -1544,7 +1544,7 @@ static bool LoadOldMapPart1(LoadgameState *ls, int)
return true; return true;
} }
static bool LoadOldMapPart2(LoadgameState *ls, int) static bool LoadOldMapPart2(LoadgameState &ls, int)
{ {
for (auto t : Map::Iterate()) { for (auto t : Map::Iterate()) {
t.type() = ReadByte(ls); t.type() = ReadByte(ls);
@ -1556,7 +1556,7 @@ static bool LoadOldMapPart2(LoadgameState *ls, int)
return true; return true;
} }
static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int) static bool LoadTTDPatchExtraChunks(LoadgameState &ls, int)
{ {
ReadTTDPatchFlags(); ReadTTDPatchFlags();
@ -1788,7 +1788,7 @@ static const OldChunks main_chunk[] = {
OCL_END() OCL_END()
}; };
bool LoadTTDMain(LoadgameState *ls) bool LoadTTDMain(LoadgameState &ls)
{ {
Debug(oldloader, 3, "Reading main chunk..."); Debug(oldloader, 3, "Reading main chunk...");
@ -1830,7 +1830,7 @@ bool LoadTTDMain(LoadgameState *ls)
return true; return true;
} }
bool LoadTTOMain(LoadgameState *ls) bool LoadTTOMain(LoadgameState &ls)
{ {
Debug(oldloader, 3, "Reading main chunk..."); Debug(oldloader, 3, "Reading main chunk...");

View File

@ -249,7 +249,7 @@ private:
public: public:
friend void FixOldVehicles(); friend void FixOldVehicles();
friend void AfterLoadVehiclesPhase1(bool part_of_load); ///< So we can set the #previous and #first pointers while loading friend void AfterLoadVehiclesPhase1(bool part_of_load); ///< So we can set the #previous and #first pointers while loading
friend bool LoadOldVehicle(LoadgameState *ls, int num); ///< So we can set the proper next pointer while loading friend bool LoadOldVehicle(LoadgameState &ls, int num); ///< So we can set the proper next pointer while loading
/* So we can use private/protected variables in the saveload code */ /* So we can use private/protected variables in the saveload code */
friend class SlVehicleCommon; friend class SlVehicleCommon;
friend class SlVehicleDisaster; friend class SlVehicleDisaster;