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

OpenTTD Source 15.3

labelmaps_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/labelmaps_sl_compat.h"
14
15#include "saveload_internal.h"
16#include "../rail.h"
17#include "../road.h"
18#include "../newgrf_railtype.h"
19#include "../newgrf_roadtype.h"
20
21#include "../safeguards.h"
22
23extern std::vector<LabelObject<RailTypeLabel>> _railtype_list;
24extern std::vector<LabelObject<RoadTypeLabel>> _roadtype_list;
25
27 void AfterLoadLabelMaps()
28{
29 ConvertRailTypes();
30 ConvertRoadTypes();
31
32 SetCurrentRailTypeLabelList();
33 SetCurrentRoadTypeLabelList();
34}
35
36 struct RAILChunkHandler : ChunkHandler {
37 RAILChunkHandler() : ChunkHandler('RAIL', CH_TABLE) {}
38
39 static inline const SaveLoad description[] = {
40 SLE_VAR(LabelObject<RailTypeLabel>, label, SLE_UINT32),
41 };
42
43 void Save() const override
44 {
45 SlTableHeader(description);
46
47 LabelObject<RailTypeLabel> lo;
48 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
49 lo.label = GetRailTypeInfo(r)->label;
50
51 SlSetArrayIndex(r);
52 SlObject(&lo, description);
53 }
54 }
55
56 void Load() const override
57 {
58 const std::vector<SaveLoad> slt = SlCompatTableHeader(description, _label_object_sl_compat);
59
60 _railtype_list.reserve(RAILTYPE_END);
61
62 LabelObject<RailTypeLabel> lo;
63
64 while (SlIterateArray() != -1) {
65 SlObject(&lo, slt);
66 _railtype_list.push_back(lo);
67 }
68 }
69};
70
71 struct ROTTChunkHandler : ChunkHandler {
72 ROTTChunkHandler() : ChunkHandler('ROTT', CH_TABLE) {}
73
74 static inline const SaveLoad description[] = {
75 SLE_VAR(LabelObject<RoadTypeLabel>, label, SLE_UINT32),
76 SLE_VAR(LabelObject<RoadTypeLabel>, subtype, SLE_UINT8),
77 };
78
79 void Save() const override
80 {
81 SlTableHeader(description);
82
83 LabelObject<RoadTypeLabel> lo;
84 for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
85 const RoadTypeInfo *rti = GetRoadTypeInfo(r);
86 lo.label = rti->label;
87 lo.subtype = GetRoadTramType(r);
88
89 SlSetArrayIndex(r);
90 SlObject(&lo, description);
91 }
92 }
93
94 void Load() const override
95 {
96 const std::vector<SaveLoad> slt = SlCompatTableHeader(description, _label_object_sl_compat);
97
98 _roadtype_list.reserve(ROADTYPE_END);
99
100 LabelObject<RoadTypeLabel> lo;
101
102 while (SlIterateArray() != -1) {
103 SlObject(&lo, slt);
104 _roadtype_list.push_back(lo);
105 }
106 }
107};
108
109static const RAILChunkHandler RAIL;
110static const ROTTChunkHandler ROTT;
111
112static const ChunkHandlerRef labelmaps_chunk_handlers[] = {
113 RAIL,
114 ROTT,
115};
116
117extern const ChunkHandlerTable _labelmaps_chunk_handlers(labelmaps_chunk_handlers);
118
RailTypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:225
RoadTypeInfo
Definition road.h:52
RoadTypeInfo::label
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition road.h:121
AfterLoadLabelMaps
void AfterLoadLabelMaps()
Perform rail type and road type conversion if necessary.
Definition labelmaps_sl.cpp:27
labelmaps_sl_compat.h
Loading of labelmaps chunks before table headers were added.
_label_object_sl_compat
const SaveLoadCompat _label_object_sl_compat[]
Original field order for _label_object_desc.
Definition labelmaps_sl_compat.h:16
ConvertRailTypes
void ConvertRailTypes()
Test if any saved rail type labels are different to the currently loaded rail types.
Definition newgrf_railtype.cpp:194
SetCurrentRailTypeLabelList
void SetCurrentRailTypeLabelList()
Populate railtype label list with current values.
Definition newgrf_railtype.cpp:244
SetCurrentRoadTypeLabelList
void SetCurrentRoadTypeLabelList()
Populate road type label list with current values.
Definition newgrf_roadtype.cpp:270
ConvertRoadTypes
void ConvertRoadTypes()
Test if any saved road type labels are different to the currently loaded road types.
Definition newgrf_roadtype.cpp:225
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:300
RailType
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
RAILTYPE_BEGIN
@ RAILTYPE_BEGIN
Used for iterations.
Definition rail_type.h:26
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:31
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:215
RoadType
RoadType
The different roadtypes we support.
Definition road_type.h:23
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition road_type.h:27
ROADTYPE_BEGIN
@ ROADTYPE_BEGIN
Used for iterations.
Definition road_type.h:24
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.
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
saveload_internal.h
Declaration of functions used in more save/load files.
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
LabelObject
Container for a label for rail or road type conversion.
Definition newgrf_commons.h:474
LabelObject::subtype
uint8_t subtype
Subtype of type (road or tram).
Definition newgrf_commons.h:476
LabelObject::label
T label
Label of rail or road type.
Definition newgrf_commons.h:475
RAILChunkHandler
Definition labelmaps_sl.cpp:36
RAILChunkHandler::Load
void Load() const override
Load the chunk.
Definition labelmaps_sl.cpp:56
RAILChunkHandler::Save
void Save() const override
Save the chunk.
Definition labelmaps_sl.cpp:43
ROTTChunkHandler
Definition labelmaps_sl.cpp:71
ROTTChunkHandler::Load
void Load() const override
Load the chunk.
Definition labelmaps_sl.cpp:94
ROTTChunkHandler::Save
void Save() const override
Save the chunk.
Definition labelmaps_sl.cpp:79
SaveLoad
SaveLoad type struct.
Definition saveload.h:732