OpenTTD Source: src/saveload/depot_sl.cpp Source File

OpenTTD Source 15.3

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
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/depot_sl_compat.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[] = {
23 SLE_CONDVAR(Depot, xy, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
24 SLE_CONDVAR(Depot, xy, SLE_UINT32, SLV_6, SL_MAX_VERSION),
25 SLEG_CONDVAR("town_index", _town_index, SLE_UINT16, SL_MIN_VERSION, SLV_141),
26 SLE_CONDREF(Depot, town, REF_TOWN, SLV_141, SL_MAX_VERSION),
27 SLE_CONDVAR(Depot, town_cn, SLE_UINT16, SLV_141, SL_MAX_VERSION),
28 SLE_CONDSSTR(Depot, name, SLE_STR, SLV_141, SL_MAX_VERSION),
29 SLE_CONDVAR(Depot, build_date, SLE_INT32, SLV_142, SL_MAX_VERSION),
30};
31
32 struct DEPTChunkHandler : ChunkHandler {
33 DEPTChunkHandler() : ChunkHandler('DEPT', CH_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 = new (DepotID(index)) Depot();
53 SlObject(depot, slt);
54
55 /* Set the town 'pointer' so we can restore it later. */
56 if (IsSavegameVersionBefore(SLV_141)) 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(SLV_141)) 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);
depot_sl_compat.h
Loading for depot chunks before table headers were added.
_depot_sl_compat
const SaveLoadCompat _depot_sl_compat[]
Original field order for _depot_desc.
Definition depot_sl_compat.h:16
DepotID
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
saveload.h
Functions/types related to saving and loading games.
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition saveload.h:618
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:948
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition saveload.h:46
SLV_141
@ SLV_141
141 19799
Definition saveload.h:212
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_142
@ SLV_142
142 20003
Definition saveload.h:213
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:904
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
DEPTChunkHandler
Definition depot_sl.cpp:32
DEPTChunkHandler::Save
void Save() const override
Save the chunk.
Definition depot_sl.cpp:35
DEPTChunkHandler::Load
void Load() const override
Load the chunk.
Definition depot_sl.cpp:45
DEPTChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition depot_sl.cpp:60
Depot
Definition depot_base.h:20
PoolID< uint16_t, struct TownIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_depot_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Pool::PoolItem<&_town_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
SaveLoad
SaveLoad type struct.
Definition saveload.h:732
Town
Town data structure.
Definition town.h:63