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

OpenTTD Source 15.0-RC2

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
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/story_sl_compat.h"
14
15#include "../story_base.h"
16
17#include "../safeguards.h"
18
20 void AfterLoadStoryBook()
21{
22 if (IsSavegameVersionBefore(SLV_185)) {
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[] = {
32 SLE_CONDVAR(StoryPageElement, sort_value, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_185),
33 SLE_CONDVAR(StoryPageElement, sort_value, SLE_UINT32, SLV_185, SL_MAX_VERSION),
34 SLE_VAR(StoryPageElement, page, SLE_UINT16),
35 SLE_CONDVAR(StoryPageElement, type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_185),
36 SLE_CONDVAR(StoryPageElement, type, SLE_UINT8, SLV_185, SL_MAX_VERSION),
37 SLE_VAR(StoryPageElement, referenced_id, SLE_UINT32),
38 SLE_SSTR(StoryPageElement, text, SLE_STR | SLF_ALLOW_CONTROL),
39};
40
41 struct STPEChunkHandler : ChunkHandler {
42 STPEChunkHandler() : ChunkHandler('STPE', CH_TABLE) {}
43
44 void Save() const override
45 {
46 SlTableHeader(_story_page_elements_desc);
47
48 for (StoryPageElement *s : StoryPageElement::Iterate()) {
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) {
61 StoryPageElement *s = new (StoryPageElementID(index)) StoryPageElement();
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[] = {
75 SLE_CONDVAR(StoryPage, sort_value, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_185),
76 SLE_CONDVAR(StoryPage, sort_value, SLE_UINT32, SLV_185, SL_MAX_VERSION),
77 SLE_VAR(StoryPage, date, SLE_UINT32),
78 SLE_CONDVAR(StoryPage, company, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_185),
79 SLE_CONDVAR(StoryPage, company, SLE_UINT8, SLV_185, SL_MAX_VERSION),
80 SLE_SSTR(StoryPage, title, SLE_STR | SLF_ALLOW_CONTROL),
81};
82
83 struct STPAChunkHandler : ChunkHandler {
84 STPAChunkHandler() : ChunkHandler('STPA', CH_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) {
103 StoryPage *s = new (StoryPageID(index)) StoryPage();
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);
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.
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:702
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_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1061
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_185
@ SLV_185
185 25620 Storybooks
Definition saveload.h:265
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
AfterLoadStoryBook
void AfterLoadStoryBook()
Called after load to trash broken pages.
Definition story_sl.cpp:20
story_sl_compat.h
Loading for story chunks before table headers were added.
_story_page_elements_sl_compat
const SaveLoadCompat _story_page_elements_sl_compat[]
Original field order for _story_page_elements_desc.
Definition story_sl_compat.h:16
_story_pages_sl_compat
const SaveLoadCompat _story_pages_sl_compat[]
Original field order for _story_pages_desc.
Definition story_sl_compat.h:25
StoryPageID
PoolID< uint16_t, struct StoryPageIDTag, 64000, 0xFFFF > StoryPageID
ID of a story page.
Definition story_type.h:16
StoryPageElementID
PoolID< uint16_t, struct StoryPageElementIDTag, 64000, 0xFFFF > StoryPageElementID
ID of a story page element.
Definition story_type.h:15
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
Pool::PoolItem<&_story_page_element_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Pool::CleanPool
void CleanPool() override
Virtual method that deletes all items in the pool.
STPAChunkHandler
Definition story_sl.cpp:83
STPAChunkHandler::Load
void Load() const override
Load the chunk.
Definition story_sl.cpp:96
STPAChunkHandler::Save
void Save() const override
Save the chunk.
Definition story_sl.cpp:86
STPEChunkHandler
Definition story_sl.cpp:41
STPEChunkHandler::Load
void Load() const override
Load the chunk.
Definition story_sl.cpp:54
STPEChunkHandler::Save
void Save() const override
Save the chunk.
Definition story_sl.cpp:44
SaveLoad
SaveLoad type struct.
Definition saveload.h:732
StoryPageElement
Struct about story page elements.
Definition story_base.h:145
StoryPageElement::sort_value
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:146
StoryPage
Struct about stories, current and completed.
Definition story_base.h:167
StoryPage::sort_value
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:168