OpenTTD Source 20261129-master-g72a8bdc861
group_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#include "../group.h"
12#include "../company_base.h"
13
14#include "saveload.h"
15#include "saveload_internal.h"
17
18#include "../safeguards.h"
19
21class SlGroupEngineRenews : public VectorSaveLoadHandler<SlGroupEngineRenews, Group, EngineRenew> {
22public:
29
30 static inline const SaveLoadCompatTable compat_description = {};
31
32 std::vector<EngineRenew> &GetVector(Group *g) const override { return g->engine_renew_list; }
33};
34
35static const SaveLoad _group_desc[] = {
39 SaveLoad::Variable<VarFileType::U8>("vehicle_type", SLE_OBJECT_ADDRESS(Group, vehicle_type)),
47};
48
49struct GRPSChunkHandler : ChunkHandler {
50 GRPSChunkHandler() : ChunkHandler("GRPS", ChunkType::Table) {}
51
52 void Save() const override
53 {
54 SlTableHeader(_group_desc);
55
56 for (Group *g : Group::Iterate()) {
57 SlSetArrayIndex(g->index);
58 SlObject(g, _group_desc);
59 }
60 }
61
62
63 void Load() const override
64 {
65 const std::vector<SaveLoad> slt = SlCompatTableHeader(_group_desc, _group_sl_compat);
66
67 int index;
68
69 while ((index = SlIterateArray()) != -1) {
70 Group *g = Group::CreateAtIndex(GroupID(index));
71 SlObject(g, slt);
72
74 }
75 }
76
77 void FixPointers() const override
78 {
79 for (Group *g : Group::Iterate()) {
80 SlObject(g, _group_desc);
81
83 Company *c = Company::Get(g->owner);
84 /* Migrate engine renews for this group. */
85 _old_engine_renew_saveload_pool.TransformItems(c->old_engine_renew_index, [g](const auto &item) {
86 if (item.group_id == g->index) g->engine_renew_list.emplace_back(item);
87 });
88 /* Sort by from-engine as lookups use binary-search. */
89 std::ranges::sort(g->engine_renew_list, std::less{}, &EngineRenew::from);
90 }
91 }
92 }
93};
94
95static const GRPSChunkHandler GRPS;
96static const ChunkHandlerRef group_chunk_handlers[] = {
97 GRPS,
98};
99
100extern const ChunkHandlerTable _group_chunk_handlers(group_chunk_handlers);
OldEngineRenewPool _old_engine_renew_saveload_pool
Temporary storage for conversion from old engine renew pool.
Saveload handler for group engine renew entries.
Definition group_sl.cpp:21
static const SaveLoadCompatTable compat_description
Compatibility saveload description for handler.
Definition group_sl.cpp:30
static const SaveLoad description[]
Saveload description for handler.
Definition group_sl.cpp:24
std::vector< EngineRenew > & GetVector(Group *g) const override
Get instance of vector to load/save.
Definition group_sl.cpp:32
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1338
Definition of stuff that is very close to a company, like the company struct itself.
Base class for groups and group functions.
Loading of group chunks before table headers were added.
const SaveLoadCompat _group_sl_compat[]
Original field order for _group_desc.
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:747
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.
#define SLE_OBJECT_ADDRESS(base, variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a base c...
Definition saveload.h:752
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:518
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1223
@ GroupHierarchy
Saveload version: 189, SVN revision: 26450 Hierarchical vehicle subgroups.
Definition saveload.h:274
@ ReplaceCustomNameArray
Saveload version: 84, SVN revision: 11822 Replace single fixed size array of custom names,...
Definition saveload.h:148
@ GroupLiveries
Saveload version: 205, GitHub pull request: 7108 Livery storage change and group liveries.
Definition saveload.h:294
@ EngineRenewsOwnership
Saveload version: 369, GitHub pull request: 16186 Engine renews are now owned by company or group in...
Definition saveload.h:426
@ MinVersion
First savegame version.
Definition saveload.h:35
@ GroupNumbers
Saveload version: 336, GitHub pull request: 12297 Add per-company group numbers.
Definition saveload.h:387
@ Table
An Array with a header describing the elements.
Definition saveload.h:449
Declaration of functions used in more save/load files.
Definition of base types and functions in a cross-platform compatible way.
@ AllowControlCode
Allow the special control codes.
Definition string_type.h:47
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
uint16_t old_engine_renew_index
Index of first item in old engine renew pool, required for old savegame conversion.
Information about engine autorenews and autoreplacements.
EngineID from
Engine to replace from.
void Load() const override
Load the chunk.
Definition group_sl.cpp:63
void FixPointers() const override
Fix the pointers.
Definition group_sl.cpp:77
void Save() const override
Save the chunk.
Definition group_sl.cpp:52
Group data.
Definition group.h:77
EngineRenewList engine_renew_list
Engine renewals of this group.
Definition group.h:84
GroupID parent
Parent group.
Definition group.h:91
static Pool::IterateWrapper< Group > Iterate(size_t from=0)
static T * CreateAtIndex(GroupID index, Targs &&... args)
static Company * Get(auto index)
SaveLoad type struct.
Definition saveload.h:762
static SaveLoad StructList(std::string name, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
Storage of a list of structs, optionally in some savegame versions.
Definition saveload.h:1100
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:923
static SaveLoad String(std::string name, AddressFunction< T > address_func, StringValidationSettings string_validation_settings={}, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a string in some savegame versions.
Definition saveload.h:974