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

OpenTTD Source 14.0-RC3

storage_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/storage_sl_compat.h"
14
15 #include "../newgrf_storage.h"
16
17 #include "../safeguards.h"
18
20 static const SaveLoad _storage_desc[] = {
21 SLE_CONDVAR(PersistentStorage, grfid, SLE_UINT32, SLV_6, SL_MAX_VERSION),
22 SLE_CONDARR(PersistentStorage, storage, SLE_UINT32, 16, SLV_161, SLV_EXTEND_PERSISTENT_STORAGE),
23 SLE_CONDARR(PersistentStorage, storage, SLE_UINT32, 256, SLV_EXTEND_PERSISTENT_STORAGE, SL_MAX_VERSION),
24 };
25
27 struct PSACChunkHandler : ChunkHandler {
28 PSACChunkHandler() : ChunkHandler('PSAC', CH_TABLE) {}
29
30 void Load() const override
31 {
32 const std::vector<SaveLoad> slt = SlCompatTableHeader(_storage_desc, _storage_sl_compat);
33
34 int index;
35
36 while ((index = SlIterateArray()) != -1) {
37 assert(PersistentStorage::CanAllocateItem());
38 PersistentStorage *ps = new (index) PersistentStorage(0, 0, 0);
39 SlObject(ps, slt);
40 }
41 }
42
43 void Save() const override
44 {
45 SlTableHeader(_storage_desc);
46
47 /* Write the industries */
48 for (PersistentStorage *ps : PersistentStorage::Iterate()) {
49 ps->ClearChanges();
50 SlSetArrayIndex(ps->index);
51 SlObject(ps, _storage_desc);
52 }
53 }
54 };
55
56 static const PSACChunkHandler PSAC;
57 static const ChunkHandlerRef persistent_storage_chunk_handlers[] = {
58 PSAC,
59 };
60
61 extern const ChunkHandlerTable _persistent_storage_chunk_handlers(persistent_storage_chunk_handlers);
PSACChunkHandler::Load
void Load() const override
Load the chunk.
Definition: storage_sl.cpp:30
storage_sl_compat.h
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:491
SLE_CONDARR
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of a fixed-size array of SL_VAR elements in some savegame versions.
Definition: saveload.h:880
PSACChunkHandler
Persistent storage data.
Definition: storage_sl.cpp:27
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:848
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:445
SLV_EXTEND_PERSISTENT_STORAGE
@ SLV_EXTEND_PERSISTENT_STORAGE
201 PR#6885 Extend NewGRF persistent storages.
Definition: saveload.h:285
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:199
_storage_sl_compat
const SaveLoadCompat _storage_sl_compat[]
Original field order for _storage_desc.
Definition: storage_sl_compat.h:16
_storage_desc
static const SaveLoad _storage_desc[]
Description of the data to save and load in PersistentStorage.
Definition: storage_sl.cpp:20
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:382
Pool::PoolItem<&_persistent_storage_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
Pool::PoolItem<&_persistent_storage_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:494
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1852
SLV_161
@ SLV_161
161 22567
Definition: saveload.h:236
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1661
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1714
SaveLoad
SaveLoad type struct.
Definition: saveload.h:697
PSACChunkHandler::Save
void Save() const override
Save the chunk.
Definition: storage_sl.cpp:43
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:656