OpenTTD Source 20260711-master-g3fb3006dff
depot_sl.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
12#include "saveload.h"
14
15#include "../depot_base.h"
16#include "../town.h"
17
18#include "../safeguards.h"
19
20static TownID _town_index;
21
22static const SaveLoad _depot_desc[] = {
30};
31
32struct DEPTChunkHandler : ChunkHandler {
33 DEPTChunkHandler() : ChunkHandler('DEPT', ChunkType::Table) {}
34
35 void Save() const override
36 {
37 SlTableHeader(_depot_desc);
38
39 for (Depot *depot : Depot::Iterate()) {
40 SlSetArrayIndex(depot->index);
41 SlObject(depot, _depot_desc);
42 }
43 }
44
45 void Load() const override
46 {
47 const std::vector<SaveLoad> slt = SlCompatTableHeader(_depot_desc, _depot_sl_compat);
48
49 int index;
50
51 while ((index = SlIterateArray()) != -1) {
52 Depot *depot = Depot::CreateAtIndex(DepotID(index));
53 SlObject(depot, slt);
54
55 /* Set the town 'pointer' so we can restore it later. */
56 if (IsSavegameVersionBefore(SaveLoadVersion::UniqueDepotNames)) depot->town = reinterpret_cast<Town *>(static_cast<size_t>(_town_index.base()));
57 }
58 }
59
60 void FixPointers() const override
61 {
62 for (Depot *depot : Depot::Iterate()) {
63 SlObject(depot, _depot_desc);
64 if (IsSavegameVersionBefore(SaveLoadVersion::UniqueDepotNames)) depot->town = Town::Get((size_t)depot->town);
65 }
66 }
67};
68
69static const DEPTChunkHandler DEPT;
70static const ChunkHandlerRef depot_chunk_handlers[] = {
71 DEPT,
72};
73
74extern const ChunkHandlerTable _depot_chunk_handlers(depot_chunk_handlers);
Base for all depots (except hangars).
Loading for depot chunks before table headers were added.
const SaveLoadCompat _depot_sl_compat[]
Original field order for _depot_desc.
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
A number of safeguards to prevent using unsafe methods.
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:734
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Functions/types related to saving and loading games.
@ U32
A 32 bit unsigned int.
Definition saveload.h:684
@ U16
A 16 bit unsigned int.
Definition saveload.h:664
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:530
@ Town
Load/save a reference to a town.
Definition saveload.h:645
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:533
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:958
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:904
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
@ MultipleRoadStops
Saveload version: 6.0, SVN revision: 1721 Multi tile road stops, and some map size related fixes.
Definition saveload.h:47
@ MinVersion
First savegame version.
Definition saveload.h:31
@ UniqueDepotNames
Saveload version: 141, SVN revision: 19799 Give depots unique names.
Definition saveload.h:213
@ NewGRFDepotBuildDate
Saveload version: 142, SVN revision: 20003 Depot build date for NewGRFs.
Definition saveload.h:214
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:421
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:925
@ Table
An Array with a header describing the elements.
Definition saveload.h:475
Definition of base types and functions in a cross-platform compatible way.
void Save() const override
Save the chunk.
Definition depot_sl.cpp:35
void Load() const override
Load the chunk.
Definition depot_sl.cpp:45
void FixPointers() const override
Fix the pointers.
Definition depot_sl.cpp:60
static Pool::IterateWrapper< Depot > Iterate(size_t from=0)
static T * CreateAtIndex(DepotID index, Targs &&... args)
static Town * Get(auto index)
SaveLoad type struct.
Definition saveload.h:787
Town data structure.
Definition town.h:64
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:754
static constexpr VarType STR
Store string.
Definition saveload.h:760
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:756
static constexpr VarType I32
Store a 32 bits signed int.
Definition saveload.h:755
Base of the town class.