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

OpenTTD Source 13.1

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_VAR(LeagueTableElement, rating, SLE_FILE_U64 | SLE_VAR_I64),
21 SLE_VAR(LeagueTableElement, company, SLE_UINT8),
22 SLE_SSTR(LeagueTableElement, text, SLE_STR | SLF_ALLOW_CONTROL),
23 SLE_SSTR(LeagueTableElement, score, SLE_STR | SLF_ALLOW_CONTROL),
24 SLE_VAR(LeagueTableElement, link.type, SLE_UINT8),
25 SLE_VAR(LeagueTableElement, link.target, SLE_UINT32),
26 };
27
28 struct LEAEChunkHandler : ChunkHandler {
29 LEAEChunkHandler() : ChunkHandler('LEAE', CH_TABLE) {}
30
31 void Save() const override
32 {
33 SlTableHeader(_league_table_elements_desc);
34
35 for (LeagueTableElement *lte : LeagueTableElement::Iterate()) {
36 SlSetArrayIndex(lte->index);
37 SlObject(lte, _league_table_elements_desc);
38 }
39 }
40
41 void Load() const override
42 {
43 const std::vector<SaveLoad> slt = SlTableHeader(_league_table_elements_desc);
44
45 int index;
46 while ((index = SlIterateArray()) != -1) {
47 LeagueTableElement *lte = new (index) LeagueTableElement();
48 SlObject(lte, slt);
49 }
50 }
51 };
52
53 static const SaveLoad _league_tables_desc[] = {
54 SLE_SSTR(LeagueTable, title, SLE_STR | SLF_ALLOW_CONTROL),
55 SLE_SSTR(LeagueTable, header, SLE_STR | SLF_ALLOW_CONTROL),
56 SLE_SSTR(LeagueTable, footer, SLE_STR | SLF_ALLOW_CONTROL),
57 };
58
59 struct LEATChunkHandler : ChunkHandler {
60 LEATChunkHandler() : ChunkHandler('LEAT', CH_TABLE) {}
61
62 void Save() const override
63 {
64 SlTableHeader(_league_tables_desc);
65
66 for (LeagueTable *lt : LeagueTable::Iterate()) {
67 SlSetArrayIndex(lt->index);
68 SlObject(lt, _league_tables_desc);
69 }
70 }
71
72 void Load() const override
73 {
74 const std::vector<SaveLoad> slt = SlTableHeader(_league_tables_desc);
75
76 int index;
77 while ((index = SlIterateArray()) != -1) {
78 LeagueTable *lt = new (index) LeagueTable();
79 SlObject(lt, slt);
80 }
81 }
82 };
83
84 static const LEAEChunkHandler LEAE;
85 static const LEATChunkHandler LEAT;
86 static const ChunkHandlerRef league_chunk_handlers[] = {
87 LEAE,
88 LEAT,
89 };
90
91 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:28
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:449
LEATChunkHandler::Load
void Load() const override
Load the chunk.
Definition: league_sl.cpp:72
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:804
LeagueTable
Struct about custom league tables.
Definition: league_base.h:52
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:412
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:631
LEAEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: league_sl.cpp:41
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
LEAEChunkHandler::Save
void Save() const override
Save the chunk.
Definition: league_sl.cpp:31
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:778
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:386
LEATChunkHandler
Definition: league_sl.cpp:59
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:812
LEATChunkHandler::Save
void Save() const override
Save the chunk.
Definition: league_sl.cpp:62
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1839
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1892
SaveLoad
SaveLoad type struct.
Definition: saveload.h:659
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:671