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

OpenTTD Source 14.0-RC3

league_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
14 #include "../league_base.h"
15
16 #include "../safeguards.h"
17
18 static const SaveLoad _league_table_elements_desc[] = {
19 SLE_VAR(LeagueTableElement, table, SLE_UINT8),
20 SLE_CONDVAR(LeagueTableElement, rating, SLE_FILE_U64 | SLE_VAR_I64, SL_MIN_VERSION, SLV_LINKGRAPH_EDGES),
21 SLE_CONDVAR(LeagueTableElement, rating, SLE_INT64, SLV_LINKGRAPH_EDGES, SL_MAX_VERSION),
22 SLE_VAR(LeagueTableElement, company, SLE_UINT8),
23 SLE_SSTR(LeagueTableElement, text, SLE_STR | SLF_ALLOW_CONTROL),
24 SLE_SSTR(LeagueTableElement, score, SLE_STR | SLF_ALLOW_CONTROL),
25 SLE_VAR(LeagueTableElement, link.type, SLE_UINT8),
26 SLE_VAR(LeagueTableElement, link.target, SLE_UINT32),
27 };
28
29 struct LEAEChunkHandler : ChunkHandler {
30 LEAEChunkHandler() : ChunkHandler('LEAE', CH_TABLE) {}
31
32 void Save() const override
33 {
34 SlTableHeader(_league_table_elements_desc);
35
36 for (LeagueTableElement *lte : LeagueTableElement::Iterate()) {
37 SlSetArrayIndex(lte->index);
38 SlObject(lte, _league_table_elements_desc);
39 }
40 }
41
42 void Load() const override
43 {
44 const std::vector<SaveLoad> slt = SlTableHeader(_league_table_elements_desc);
45
46 int index;
47 while ((index = SlIterateArray()) != -1) {
48 LeagueTableElement *lte = new (index) LeagueTableElement();
49 SlObject(lte, slt);
50 }
51 }
52 };
53
54 static const SaveLoad _league_tables_desc[] = {
55 SLE_SSTR(LeagueTable, title, SLE_STR | SLF_ALLOW_CONTROL),
56 SLE_SSTR(LeagueTable, header, SLE_STR | SLF_ALLOW_CONTROL),
57 SLE_SSTR(LeagueTable, footer, SLE_STR | SLF_ALLOW_CONTROL),
58 };
59
60 struct LEATChunkHandler : ChunkHandler {
61 LEATChunkHandler() : ChunkHandler('LEAT', CH_TABLE) {}
62
63 void Save() const override
64 {
65 SlTableHeader(_league_tables_desc);
66
67 for (LeagueTable *lt : LeagueTable::Iterate()) {
68 SlSetArrayIndex(lt->index);
69 SlObject(lt, _league_tables_desc);
70 }
71 }
72
73 void Load() const override
74 {
75 const std::vector<SaveLoad> slt = SlTableHeader(_league_tables_desc);
76
77 int index;
78 while ((index = SlIterateArray()) != -1) {
79 LeagueTable *lt = new (index) LeagueTable();
80 SlObject(lt, slt);
81 }
82 }
83 };
84
85 static const LEAEChunkHandler LEAE;
86 static const LEATChunkHandler LEAT;
87 static const ChunkHandlerRef league_chunk_handlers[] = {
88 LEAE,
89 LEAT,
90 };
91
92 extern const ChunkHandlerTable _league_chunk_handlers(league_chunk_handlers);
LeagueTableElement
Struct about league table elements.
Definition: league_base.h:31
LEAEChunkHandler
Definition: league_sl.cpp:29
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:491
LEATChunkHandler::Load
void Load() const override
Load the chunk.
Definition: league_sl.cpp:73
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:848
LeagueTable
Struct about custom league tables.
Definition: league_base.h:52
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:445
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:670
LEAEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: league_sl.cpp:42
LEAEChunkHandler::Save
void Save() const override
Save the chunk.
Definition: league_sl.cpp:32
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:382
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:952
Pool::PoolItem<&_league_table_element_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
LEATChunkHandler
Definition: league_sl.cpp:60
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:494
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:996
LEATChunkHandler::Save
void Save() const override
Save the chunk.
Definition: league_sl.cpp:63
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
SLV_LINKGRAPH_EDGES
@ SLV_LINKGRAPH_EDGES
304 PR#10314 Explicitly store link graph edges destination, PR#10471 int64_t instead of uint64_t leag...
Definition: saveload.h:344
SaveLoad
SaveLoad type struct.
Definition: saveload.h:697
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:656