OpenTTD Source  20241108-master-g80f628063a
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 <http://www.gnu.org/licenses/>.
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 
20 static TownID _town_index;
21 
22 static 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),
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 
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 (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 = (Town *)(size_t)_town_index;
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 
69 static const DEPTChunkHandler DEPT;
70 static const ChunkHandlerRef depot_chunk_handlers[] = {
71  DEPT,
72 };
73 
74 extern const ChunkHandlerTable _depot_chunk_handlers(depot_chunk_handlers);
Loading for depot chunks before table headers were added.
const SaveLoadCompat _depot_sl_compat[]
Original field order for _depot_desc.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
Functions/types related to saving and loading games.
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1074
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:505
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:508
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:926
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:861
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1227
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
@ SLV_141
141 19799
Definition: saveload.h:212
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:395
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
@ SLV_142
142 20003
Definition: saveload.h:213
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:882
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:600
Handlers and description of chunk.
Definition: saveload.h:459
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 Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
SaveLoad type struct.
Definition: saveload.h:711
Town data structure.
Definition: town.h:54