diff --git a/src/saveload/oldloader.cpp b/src/saveload/oldloader.cpp index 6b77904aaa..7f2fd62929 100644 --- a/src/saveload/oldloader.cpp +++ b/src/saveload/oldloader.cpp @@ -58,14 +58,14 @@ static inline uint8_t CalcOldVarLen(OldChunkType type) * 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 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 */ - int count = static_cast(fread(ls->buffer, 1, BUFFER_SIZE, *ls->file)); + int count = static_cast(fread(ls.buffer.data(), 1, ls.buffer.size(), *ls.file)); /* We tried to read, but there is nothing in the file anymore.. */ if (count == 0) { @@ -73,11 +73,11 @@ static uint8_t ReadByteFromFile(LoadgameState *ls) throw std::exception(); } - ls->buffer_count = count; - ls->buffer_cur = 0; + ls.buffer_count = count; + 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 * */ -uint8_t ReadByte(LoadgameState *ls) +uint8_t ReadByte(LoadgameState &ls) { /* Old savegames have a nice compression algorithm (RLE) 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. Works pretty well if you have many zeros behind each other */ - if (ls->chunk_size == 0) { + if (ls.chunk_size == 0) { /* Read new chunk */ int8_t new_byte = ReadByteFromFile(ls); if (new_byte < 0) { /* Repeat next char for new_byte times */ - ls->decoding = true; - ls->decode_char = ReadByteFromFile(ls); - ls->chunk_size = -new_byte + 1; + ls.decoding = true; + ls.decode_char = ReadByteFromFile(ls); + ls.chunk_size = -new_byte + 1; } else { - ls->decoding = false; - ls->chunk_size = new_byte + 1; + ls.decoding = false; + ls.chunk_size = new_byte + 1; } } - ls->total_read++; - ls->chunk_size--; + ls.total_read++; + 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 * */ -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++) { if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) || @@ -144,8 +144,8 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks) break; case OC_ASSERT: - Debug(oldloader, 4, "Assert point: 0x{:X} / 0x{:X}", ls->total_read, (uint)(size_t)chunk->ptr + _bump_assert_value); - if (ls->total_read != (size_t)chunk->ptr + _bump_assert_value) throw std::exception(); + Debug(oldloader, 4, "Assert point: 0x{:X} / 0x{:X}", ls.total_read, reinterpret_cast(chunk->ptr) + _bump_assert_value); + if (ls.total_read != reinterpret_cast(chunk->ptr) + _bump_assert_value) throw std::exception(); default: break; } } else { @@ -190,28 +190,6 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks) 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 * @param title title and checksum @@ -253,15 +231,16 @@ static std::tuple DetermineOldSavegameTypeAndName(Fil return { SGT_INVALID, "(broken) Unknown" }; } -typedef bool LoadOldMainProc(LoadgameState *ls); +typedef bool LoadOldMainProc(LoadgameState &ls); bool LoadOldSaveGame(const std::string &file) { - LoadgameState ls; + LoadgameState ls{}; 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 */ ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY); @@ -289,7 +268,7 @@ bool LoadOldSaveGame(const std::string &file) bool game_loaded; try { - game_loaded = proc != nullptr && proc(&ls); + game_loaded = proc != nullptr && proc(ls); } catch (...) { game_loaded = false; } diff --git a/src/saveload/oldloader.h b/src/saveload/oldloader.h index 2c9fdbaca3..4a8784a293 100644 --- a/src/saveload/oldloader.h +++ b/src/saveload/oldloader.h @@ -19,16 +19,16 @@ static const uint OLD_MAP_SIZE = 256; struct LoadgameState { std::optional file; - uint chunk_size; + uint chunk_size = 0; - bool decoding; - uint8_t decode_char; + bool decoding = false; + uint8_t decode_char = 0; - uint buffer_count; - uint buffer_cur; - uint8_t buffer[BUFFER_SIZE]; + uint buffer_count = 0; + uint buffer_cur = 0; + std::array buffer{}; - uint total_read; + uint total_read = 0; }; /* OldChunk-Type */ @@ -75,7 +75,7 @@ enum OldChunkType : uint32_t { 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); struct OldChunks { @@ -88,19 +88,19 @@ struct OldChunks { }; extern uint _bump_assert_value; -uint8_t ReadByte(LoadgameState *ls); -bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks); +uint8_t ReadByte(LoadgameState &ls); +bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks); -bool LoadTTDMain(LoadgameState *ls); -bool LoadTTOMain(LoadgameState *ls); +bool LoadTTDMain(LoadgameState &ls); +bool LoadTTOMain(LoadgameState &ls); -inline uint16_t ReadUint16(LoadgameState *ls) +inline uint16_t ReadUint16(LoadgameState &ls) { uint8_t x = ReadByte(ls); return x | ReadByte(ls) << 8; } -inline uint32_t ReadUint32(LoadgameState *ls) +inline uint32_t ReadUint32(LoadgameState &ls) { uint16_t x = ReadUint16(ls); return x | ReadUint16(ls) << 16; diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index b3e3fdd48c..a25b489ae8 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -616,7 +616,7 @@ static const OldChunks town_chunk[] = { OCL_END() }; -static bool LoadOldTown(LoadgameState *ls, int num) +static bool LoadOldTown(LoadgameState &ls, int num) { Town *t = new (num) Town(); if (!LoadChunk(ls, t, town_chunk)) return false; @@ -639,7 +639,7 @@ static const OldChunks order_chunk[] = { OCL_END() }; -static bool LoadOldOrder(LoadgameState *ls, int num) +static bool LoadOldOrder(LoadgameState &ls, int num) { if (!LoadChunk(ls, nullptr, order_chunk)) return false; @@ -658,7 +658,7 @@ static bool LoadOldOrder(LoadgameState *ls, int num) return true; } -static bool LoadOldAnimTileList(LoadgameState *ls, int) +static bool LoadOldAnimTileList(LoadgameState &ls, int) { TileIndex anim_list[256]; const OldChunks anim_chunk[] = { @@ -683,7 +683,7 @@ static const OldChunks depot_chunk[] = { OCL_END() }; -static bool LoadOldDepot(LoadgameState *ls, int num) +static bool LoadOldDepot(LoadgameState &ls, int num) { Depot *d = new (num) Depot(); if (!LoadChunk(ls, d, depot_chunk)) return false; @@ -714,7 +714,7 @@ static const OldChunks goods_chunk[] = { 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 */ if (_savegame_type == SGT_TTO && num == 11) return true; @@ -771,7 +771,7 @@ static const OldChunks station_chunk[] = { OCL_END() }; -static bool LoadOldStation(LoadgameState *ls, int num) +static bool LoadOldStation(LoadgameState &ls, int num) { Station *st = new (num) Station(); _current_station_id = num; @@ -853,7 +853,7 @@ static const OldChunks industry_chunk[] = { OCL_END() }; -static bool LoadOldIndustry(LoadgameState *ls, int num) +static bool LoadOldIndustry(LoadgameState &ls, int num) { Industry *i = new (num) Industry(); if (!LoadChunk(ls, i, industry_chunk)) return false; @@ -891,7 +891,7 @@ static const OldChunks _company_yearly_chunk[] = { OCL_END() }; -static bool LoadOldCompanyYearly(LoadgameState *ls, int num) +static bool LoadOldCompanyYearly(LoadgameState &ls, int num) { Company *c = Company::Get(_current_company_id); @@ -918,7 +918,7 @@ static const OldChunks _company_economy_chunk[] = { OCL_END() }; -static bool LoadOldCompanyEconomy(LoadgameState *ls, int) +static bool LoadOldCompanyEconomy(LoadgameState &ls, int) { Company *c = Company::Get(_current_company_id); @@ -979,7 +979,7 @@ static const OldChunks _company_chunk[] = { OCL_END() }; -static bool LoadOldCompany(LoadgameState *ls, int num) +static bool LoadOldCompany(LoadgameState &ls, int num) { Company *c = new (num) Company(); @@ -1121,10 +1121,10 @@ static const OldChunks vehicle_empty_chunk[] = { OCL_END() }; -static bool LoadOldVehicleUnion(LoadgameState *ls, int) +static bool LoadOldVehicleUnion(LoadgameState &ls, int) { Vehicle *v = Vehicle::GetIfValid(_current_vehicle_id); - uint temp = ls->total_read; + uint temp = ls.total_read; bool res; if (v == nullptr) { @@ -1142,7 +1142,7 @@ static bool LoadOldVehicleUnion(LoadgameState *ls, int) } /* 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"); return false; } @@ -1247,7 +1247,7 @@ static const OldChunks vehicle_chunk[] = { * @param num The number of vehicles to load. * @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 */ ReadTTDPatchFlags(); @@ -1390,7 +1390,7 @@ bool LoadOldVehicle(LoadgameState *ls, int num) * @param index The index of the loaded custom string. * @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. @@ -1414,7 +1414,7 @@ static const OldChunks sign_chunk[] = { OCL_END() }; -static bool LoadOldSign(LoadgameState *ls, int num) +static bool LoadOldSign(LoadgameState &ls, int num) { Sign *si = new (num) Sign(); if (!LoadChunk(ls, si, sign_chunk)) return false; @@ -1456,13 +1456,13 @@ static const OldChunks engine_chunk[] = { 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); return LoadChunk(ls, e, engine_chunk); } -static bool LoadOldEngineName(LoadgameState *ls, int num) +static bool LoadOldEngineName(LoadgameState &ls, int num) { Engine *e = GetTempDataEngine(num); e->name = CopyFromOldName(RemapOldStringID(ReadUint16(ls))); @@ -1478,7 +1478,7 @@ static const OldChunks subsidy_chunk[] = { OCL_END() }; -static bool LoadOldSubsidy(LoadgameState *ls, int num) +static bool LoadOldSubsidy(LoadgameState &ls, int num) { Subsidy *s = new (num) Subsidy(); bool ret = LoadChunk(ls, s, subsidy_chunk); @@ -1507,7 +1507,7 @@ static const OldChunks game_difficulty_chunk[] = { OCL_END() }; -static bool LoadOldGameDifficulty(LoadgameState *ls, int) +static bool LoadOldGameDifficulty(LoadgameState &ls, int) { bool ret = LoadChunk(ls, &_settings_game.difficulty, game_difficulty_chunk); _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) { Map::Allocate(OLD_MAP_SIZE, OLD_MAP_SIZE); @@ -1544,7 +1544,7 @@ static bool LoadOldMapPart1(LoadgameState *ls, int) return true; } -static bool LoadOldMapPart2(LoadgameState *ls, int) +static bool LoadOldMapPart2(LoadgameState &ls, int) { for (auto t : Map::Iterate()) { t.type() = ReadByte(ls); @@ -1556,7 +1556,7 @@ static bool LoadOldMapPart2(LoadgameState *ls, int) return true; } -static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int) +static bool LoadTTDPatchExtraChunks(LoadgameState &ls, int) { ReadTTDPatchFlags(); @@ -1788,7 +1788,7 @@ static const OldChunks main_chunk[] = { OCL_END() }; -bool LoadTTDMain(LoadgameState *ls) +bool LoadTTDMain(LoadgameState &ls) { Debug(oldloader, 3, "Reading main chunk..."); @@ -1830,7 +1830,7 @@ bool LoadTTDMain(LoadgameState *ls) return true; } -bool LoadTTOMain(LoadgameState *ls) +bool LoadTTOMain(LoadgameState &ls) { Debug(oldloader, 3, "Reading main chunk..."); diff --git a/src/vehicle_base.h b/src/vehicle_base.h index f425122667..b692a93be6 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -249,7 +249,7 @@ private: public: friend void FixOldVehicles(); 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 */ friend class SlVehicleCommon; friend class SlVehicleDisaster;