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/>.
8 /** @file story_sl.cpp Code handling saving and loading of story pages */
10 #include "../stdafx.h"
13 #include "compat/story_sl_compat.h"
15 #include "../story_base.h"
17 #include "../safeguards.h"
19 /** Called after load to trash broken pages. */
20 void AfterLoadStoryBook()
22 if (IsSavegameVersionBefore(SLV_185
)) {
23 /* Trash all story pages and page elements because
24 * they were saved with wrong data types.
26 _story_page_element_pool
.CleanPool();
27 _story_page_pool
.CleanPool();
31 static 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
),
41 struct STPEChunkHandler
: ChunkHandler
{
42 STPEChunkHandler() : ChunkHandler('STPE', CH_TABLE
) {}
44 void Save() const override
46 SlTableHeader(_story_page_elements_desc
);
48 for (StoryPageElement
*s
: StoryPageElement::Iterate()) {
49 SlSetArrayIndex(s
->index
);
50 SlObject(s
, _story_page_elements_desc
);
54 void Load() const override
56 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_story_page_elements_desc
, _story_page_elements_sl_compat
);
59 uint32_t max_sort_value
= 0;
60 while ((index
= SlIterateArray()) != -1) {
61 StoryPageElement
*s
= new (index
) StoryPageElement();
63 if (s
->sort_value
> max_sort_value
) {
64 max_sort_value
= s
->sort_value
;
67 /* Update the next sort value, so that the next
68 * created page is shown after all existing pages.
70 _story_page_element_next_sort_value
= max_sort_value
+ 1;
74 static 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
),
83 struct STPAChunkHandler
: ChunkHandler
{
84 STPAChunkHandler() : ChunkHandler('STPA', CH_TABLE
) {}
86 void Save() const override
88 SlTableHeader(_story_pages_desc
);
90 for (StoryPage
*s
: StoryPage::Iterate()) {
91 SlSetArrayIndex(s
->index
);
92 SlObject(s
, _story_pages_desc
);
96 void Load() const override
98 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_story_pages_desc
, _story_pages_sl_compat
);
101 uint32_t max_sort_value
= 0;
102 while ((index
= SlIterateArray()) != -1) {
103 StoryPage
*s
= new (index
) StoryPage();
105 if (s
->sort_value
> max_sort_value
) {
106 max_sort_value
= s
->sort_value
;
109 /* Update the next sort value, so that the next
110 * created page is shown after all existing pages.
112 _story_page_next_sort_value
= max_sort_value
+ 1;
116 static const STPEChunkHandler STPE
;
117 static const STPAChunkHandler STPA
;
118 static const ChunkHandlerRef story_page_chunk_handlers
[] = {
123 extern const ChunkHandlerTable
_story_page_chunk_handlers(story_page_chunk_handlers
);