OpenTTD Source 20260711-master-g3fb3006dff
story_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
12#include "saveload.h"
14
15#include "../story_base.h"
16
17#include "../safeguards.h"
18
21{
23 /* Trash all story pages and page elements because
24 * they were saved with wrong data types.
25 */
26 _story_page_element_pool.CleanPool();
27 _story_page_pool.CleanPool();
28 }
29}
30
31static const SaveLoad _story_page_elements_desc[] = {
37 SLE_VAR(StoryPageElement, referenced_id, VarTypes::U32),
39};
40
41struct STPEChunkHandler : ChunkHandler {
42 STPEChunkHandler() : ChunkHandler('STPE', ChunkType::Table) {}
43
44 void Save() const override
45 {
46 SlTableHeader(_story_page_elements_desc);
47
49 SlSetArrayIndex(s->index);
50 SlObject(s, _story_page_elements_desc);
51 }
52 }
53
54 void Load() const override
55 {
56 const std::vector<SaveLoad> slt = SlCompatTableHeader(_story_page_elements_desc, _story_page_elements_sl_compat);
57
58 int index;
59 uint32_t max_sort_value = 0;
60 while ((index = SlIterateArray()) != -1) {
62 SlObject(s, slt);
63 if (s->sort_value > max_sort_value) {
64 max_sort_value = s->sort_value;
65 }
66 }
67 /* Update the next sort value, so that the next
68 * created page is shown after all existing pages.
69 */
70 _story_page_element_next_sort_value = max_sort_value + 1;
71 }
72};
73
74static const SaveLoad _story_pages_desc[] = {
81};
82
83struct STPAChunkHandler : ChunkHandler {
84 STPAChunkHandler() : ChunkHandler('STPA', ChunkType::Table) {}
85
86 void Save() const override
87 {
88 SlTableHeader(_story_pages_desc);
89
90 for (StoryPage *s : StoryPage::Iterate()) {
91 SlSetArrayIndex(s->index);
92 SlObject(s, _story_pages_desc);
93 }
94 }
95
96 void Load() const override
97 {
98 const std::vector<SaveLoad> slt = SlCompatTableHeader(_story_pages_desc, _story_pages_sl_compat);
99
100 int index;
101 uint32_t max_sort_value = 0;
102 while ((index = SlIterateArray()) != -1) {
104 SlObject(s, slt);
105 if (s->sort_value > max_sort_value) {
106 max_sort_value = s->sort_value;
107 }
108 }
109 /* Update the next sort value, so that the next
110 * created page is shown after all existing pages.
111 */
112 _story_page_next_sort_value = max_sort_value + 1;
113 }
114};
115
116static const STPEChunkHandler STPE;
117static const STPAChunkHandler STPA;
118static const ChunkHandlerRef story_page_chunk_handlers[] = {
119 STPE,
120 STPA,
121};
122
123extern const ChunkHandlerTable _story_page_chunk_handlers(story_page_chunk_handlers);
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:734
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.
@ U8
A 8 bit unsigned int.
Definition saveload.h:680
@ U32
A 32 bit unsigned int.
Definition saveload.h:684
@ U16
A 16 bit unsigned int.
Definition saveload.h:664
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:530
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:533
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:904
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1061
@ MinVersion
First savegame version.
Definition saveload.h:31
@ Storybooks
Saveload version: 185, SVN revision: 25620 Storybooks.
Definition saveload.h:266
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:421
@ Table
An Array with a header describing the elements.
Definition saveload.h:475
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
Definition of base types and functions in a cross-platform compatible way.
StoryPage base class.
void AfterLoadStoryBook()
Called after load to trash broken pages.
Definition story_sl.cpp:20
Loading for story chunks before table headers were added.
const SaveLoadCompat _story_page_elements_sl_compat[]
Original field order for _story_page_elements_desc.
const SaveLoadCompat _story_pages_sl_compat[]
Original field order for _story_pages_desc.
PoolID< uint16_t, struct StoryPageIDTag, 64000, 0xFFFF > StoryPageID
ID of a story page.
Definition story_type.h:16
PoolID< uint16_t, struct StoryPageElementIDTag, 64000, 0xFFFF > StoryPageElementID
ID of a story page element.
Definition story_type.h:15
@ AllowControlCode
Allow the special control codes.
Definition string_type.h:47
static Pool::IterateWrapper< StoryPageElement > Iterate(size_t from=0)
static T * CreateAtIndex(StoryPageElementID index, Targs &&... args)
void Load() const override
Load the chunk.
Definition story_sl.cpp:96
void Save() const override
Save the chunk.
Definition story_sl.cpp:86
void Load() const override
Load the chunk.
Definition story_sl.cpp:54
void Save() const override
Save the chunk.
Definition story_sl.cpp:44
SaveLoad type struct.
Definition saveload.h:787
Struct about story page elements.
Definition story_base.h:143
uint32_t sort_value
A number that increases for every created story page element. Used for sorting. The id of a story pag...
Definition story_base.h:144
Struct about stories, current and completed.
Definition story_base.h:162
uint32_t sort_value
A number that increases for every created story page. Used for sorting. The id of a story page is the...
Definition story_base.h:163
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:754
static constexpr VarType U8
Store a 8 bits unsigned int.
Definition saveload.h:752
static constexpr VarType STR
Store string.
Definition saveload.h:760
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:756