OpenTTD Source  20240919-master-gdf0233f4c2
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);
Pool::PoolItem<&_town_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:596
DEPTChunkHandler
Definition: depot_sl.cpp:32
_depot_sl_compat
const SaveLoadCompat _depot_sl_compat[]
Original field order for _depot_desc.
Definition: depot_sl_compat.h:16
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:922
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:501
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:857
saveload.h
DEPTChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: depot_sl.cpp:60
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
SLV_142
@ SLV_142
142 20003
Definition: saveload.h:213
depot_sl_compat.h
DEPTChunkHandler::Load
void Load() const override
Load the chunk.
Definition: depot_sl.cpp:45
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:878
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:391
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:388
DEPTChunkHandler::Save
void Save() const override
Save the chunk.
Definition: depot_sl.cpp:35
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1070
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
Depot
Definition: depot_base.h:20
Town
Town data structure.
Definition: town.h:54
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1234
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
SaveLoad
SaveLoad type struct.
Definition: saveload.h:707
SLV_141
@ SLV_141
141 19799
Definition: saveload.h:212
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658