4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file story_sl.cpp Code handling saving and loading of story pages */
12 #include "../stdafx.h"
13 #include "../story_base.h"
15 #include "saveload_buffer.h"
17 /** Called after load to trash broken pages. */
18 void AfterLoadStoryBook(const SavegameTypeVersion
*stv
)
20 if (IsOTTDSavegameVersionBefore(stv
, 185)) {
21 /* Trash all story pages and page elements because
22 * they were saved with wrong data types.
24 StoryPageElement::pool
.CleanPool();
25 StoryPage::pool
.CleanPool();
29 static const SaveLoad _story_page_elements_desc
[] = {
30 SLE_VAR(StoryPageElement
, sort_value
, SLE_FILE_U16
| SLE_VAR_U32
, , , 0, 184),
31 SLE_VAR(StoryPageElement
, sort_value
, SLE_UINT32
, 0, , 185, ),
32 SLE_VAR(StoryPageElement
, page
, SLE_UINT16
),
33 SLE_VAR(StoryPageElement
, type
, SLE_FILE_U16
| SLE_VAR_U8
, , , 0, 184),
34 SLE_VAR(StoryPageElement
, type
, SLE_UINT8
, 0, , 185, ),
35 SLE_VAR(StoryPageElement
, referenced_id
, SLE_UINT32
),
36 SLE_STR(StoryPageElement
, text
, SLS_STR
| SLS_ALLOW_CONTROL
, 0),
40 static void Save_STORY_PAGE_ELEMENT(SaveDumper
*dumper
)
43 FOR_ALL_STORY_PAGE_ELEMENTS(s
) {
44 dumper
->WriteElement(s
->index
, s
, _story_page_elements_desc
);
48 static void Load_STORY_PAGE_ELEMENT(LoadBuffer
*reader
)
51 uint32 max_sort_value
= 0;
52 while ((index
= reader
->IterateChunk()) != -1) {
53 StoryPageElement
*s
= new (index
) StoryPageElement();
54 reader
->ReadObject(s
, _story_page_elements_desc
);
55 if (s
->sort_value
> max_sort_value
) {
56 max_sort_value
= s
->sort_value
;
59 /* Update the next sort value, so that the next
60 * created page is shown after all existing pages.
62 _story_page_element_next_sort_value
= max_sort_value
+ 1;
65 static const SaveLoad _story_pages_desc
[] = {
66 SLE_VAR(StoryPage
, sort_value
, SLE_FILE_U16
| SLE_VAR_U32
, , , 0, 184),
67 SLE_VAR(StoryPage
, sort_value
, SLE_UINT32
, 0, , 185, ),
68 SLE_VAR(StoryPage
, date
, SLE_UINT32
),
69 SLE_VAR(StoryPage
, company
, SLE_FILE_U16
| SLE_VAR_U8
, , , 0, 184),
70 SLE_VAR(StoryPage
, company
, SLE_UINT8
, 0, , 185, ),
71 SLE_STR(StoryPage
, title
, SLS_STR
| SLS_ALLOW_CONTROL
, 0),
75 static void Save_STORY_PAGE(SaveDumper
*dumper
)
78 FOR_ALL_STORY_PAGES(s
) {
79 dumper
->WriteElement(s
->index
, s
, _story_pages_desc
);
83 static void Load_STORY_PAGE(LoadBuffer
*reader
)
86 uint32 max_sort_value
= 0;
87 while ((index
= reader
->IterateChunk()) != -1) {
88 StoryPage
*s
= new (index
) StoryPage();
89 reader
->ReadObject(s
, _story_pages_desc
);
90 if (s
->sort_value
> max_sort_value
) {
91 max_sort_value
= s
->sort_value
;
94 /* Update the next sort value, so that the next
95 * created page is shown after all existing pages.
97 _story_page_next_sort_value
= max_sort_value
+ 1;
100 extern const ChunkHandler _story_page_chunk_handlers
[] = {
101 { 'STPE', Save_STORY_PAGE_ELEMENT
, Load_STORY_PAGE_ELEMENT
, NULL
, NULL
, CH_ARRAY
},
102 { 'STPA', Save_STORY_PAGE
, Load_STORY_PAGE
, NULL
, NULL
, CH_ARRAY
| CH_LAST
},