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

OpenTTD Source 1.11.0-RC1

gamelog_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 #include "../gamelog_internal.h"
12 #include "../fios.h"
13
14 #include "saveload.h"
15
16 #include "../safeguards.h"
17
18 static const SaveLoad _glog_action_desc[] = {
19 SLE_VAR(LoggedAction, tick, SLE_UINT16),
20 SLE_END()
21 };
22
23 static const SaveLoad _glog_mode_desc[] = {
24 SLE_VAR(LoggedChange, mode.mode, SLE_UINT8),
25 SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8),
26 SLE_END()
27 };
28
29 static const SaveLoad _glog_revision_desc[] = {
30 SLE_ARR(LoggedChange, revision.text, SLE_UINT8, GAMELOG_REVISION_LENGTH),
31 SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32),
32 SLE_VAR(LoggedChange, revision.slver, SLE_UINT16),
33 SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
34 SLE_END()
35 };
36
37 static const SaveLoad _glog_oldver_desc[] = {
38 SLE_VAR(LoggedChange, oldver.type, SLE_UINT32),
39 SLE_VAR(LoggedChange, oldver.version, SLE_UINT32),
40 SLE_END()
41 };
42
43 static const SaveLoad _glog_setting_desc[] = {
44 SLE_STR(LoggedChange, setting.name, SLE_STR, 128),
45 SLE_VAR(LoggedChange, setting.oldval, SLE_INT32),
46 SLE_VAR(LoggedChange, setting.newval, SLE_INT32),
47 SLE_END()
48 };
49
50 static const SaveLoad _glog_grfadd_desc[] = {
51 SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ),
52 SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16),
53 SLE_END()
54 };
55
56 static const SaveLoad _glog_grfrem_desc[] = {
57 SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32),
58 SLE_END()
59 };
60
61 static const SaveLoad _glog_grfcompat_desc[] = {
62 SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ),
63 SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16),
64 SLE_END()
65 };
66
67 static const SaveLoad _glog_grfparam_desc[] = {
68 SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32),
69 SLE_END()
70 };
71
72 static const SaveLoad _glog_grfmove_desc[] = {
73 SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32),
74 SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32),
75 SLE_END()
76 };
77
78 static const SaveLoad _glog_grfbug_desc[] = {
79 SLE_VAR(LoggedChange, grfbug.data, SLE_UINT64),
80 SLE_VAR(LoggedChange, grfbug.grfid, SLE_UINT32),
81 SLE_VAR(LoggedChange, grfbug.bug, SLE_UINT8),
82 SLE_END()
83 };
84
85 static const SaveLoad _glog_emergency_desc[] = {
86 SLE_END()
87 };
88
89 static const SaveLoad * const _glog_desc[] = {
90 _glog_mode_desc,
91 _glog_revision_desc,
92 _glog_oldver_desc,
93 _glog_setting_desc,
94 _glog_grfadd_desc,
95 _glog_grfrem_desc,
96 _glog_grfcompat_desc,
97 _glog_grfparam_desc,
98 _glog_grfmove_desc,
99 _glog_grfbug_desc,
100 _glog_emergency_desc,
101 };
102
103 static_assert(lengthof(_glog_desc) == GLCT_END);
104
105 static void Load_GLOG_common(LoggedAction *&gamelog_action, uint &gamelog_actions)
106 {
107 assert(gamelog_action == nullptr);
108 assert(gamelog_actions == 0);
109
110 GamelogActionType at;
111 while ((at = (GamelogActionType)SlReadByte()) != GLAT_NONE) {
112 gamelog_action = ReallocT(gamelog_action, gamelog_actions + 1);
113 LoggedAction *la = &gamelog_action[gamelog_actions++];
114
115 la->at = at;
116
117 SlObject(la, _glog_action_desc); // has to be saved after 'DATE'!
118 la->change = nullptr;
119 la->changes = 0;
120
121 GamelogChangeType ct;
122 while ((ct = (GamelogChangeType)SlReadByte()) != GLCT_NONE) {
123 la->change = ReallocT(la->change, la->changes + 1);
124
125 LoggedChange *lc = &la->change[la->changes++];
126 /* for SLE_STR, pointer has to be valid! so make it nullptr */
127 memset(lc, 0, sizeof(*lc));
128 lc->ct = ct;
129
130 assert((uint)ct < GLCT_END);
131
132 SlObject(lc, _glog_desc[ct]);
133 }
134 }
135 }
136
137 static void Save_GLOG()
138 {
139 const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
140 size_t length = 0;
141
142 for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
143 const LoggedChange *lcend = &la->change[la->changes];
144 for (LoggedChange *lc = la->change; lc != lcend; lc++) {
145 assert((uint)lc->ct < lengthof(_glog_desc));
146 length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1;
147 }
148 length += 4;
149 }
150 length++;
151
152 SlSetLength(length);
153
154 for (LoggedAction *la = _gamelog_action; la != laend; la++) {
155 SlWriteByte(la->at);
156 SlObject(la, _glog_action_desc);
157
158 const LoggedChange *lcend = &la->change[la->changes];
159 for (LoggedChange *lc = la->change; lc != lcend; lc++) {
160 SlWriteByte(lc->ct);
161 assert((uint)lc->ct < GLCT_END);
162 SlObject(lc, _glog_desc[lc->ct]);
163 }
164 SlWriteByte(GLCT_NONE);
165 }
166 SlWriteByte(GLAT_NONE);
167 }
168
169 static void Load_GLOG()
170 {
171 Load_GLOG_common(_gamelog_action, _gamelog_actions);
172 }
173
174 static void Check_GLOG()
175 {
176 Load_GLOG_common(_load_check_data.gamelog_action, _load_check_data.gamelog_actions);
177 }
178
179 extern const ChunkHandler _gamelog_chunk_handlers[] = {
180 { 'GLOG', Save_GLOG, Load_GLOG, nullptr, Check_GLOG, CH_RIFF | CH_LAST }
181 };
LoggedChange::newgrf
uint32 newgrf
_openttd_newgrf_version
Definition: gamelog_internal.h:45
LoggedChange::name
char * name
name of the setting
Definition: gamelog_internal.h:66
GLCT_END
@ GLCT_END
So we know how many GLCTs are there.
Definition: gamelog_internal.h:28
LoggedChange::mode
byte mode
new game mode - Editor x Game
Definition: gamelog_internal.h:40
LoggedChange::offset
int32 offset
offset, positive = move down
Definition: gamelog_internal.h:63
LoadCheckData::gamelog_action
struct LoggedAction * gamelog_action
Gamelog actions.
Definition: fios.h:46
LoggedChange::type
uint32 type
type of savegame,
Definition: gamelog_internal.h:50
_gamelog_actions
uint _gamelog_actions
number of actions
Definition: gamelog.cpp:36
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
LoggedAction::at
GamelogActionType at
Type of action.
Definition: gamelog_internal.h:83
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
LoggedChange::slver
uint16 slver
_sl_version
Definition: gamelog_internal.h:46
LoggedChange::landscape
byte landscape
landscape (temperate, arctic, ...)
Definition: gamelog_internal.h:41
SlSetLength
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:676
SLE_ARR
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:638
GLCT_NONE
@ GLCT_NONE
In savegames, end of list.
Definition: gamelog_internal.h:29
GamelogActionType
GamelogActionType
The actions we log.
Definition: gamelog.h:16
LoggedAction::changes
uint32 changes
Number of changes in this action.
Definition: gamelog_internal.h:82
SlWriteByte
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:427
LoadCheckData::gamelog_actions
uint gamelog_actions
Number of gamelog actions.
Definition: fios.h:47
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
LoggedChange::modified
byte modified
_openttd_revision_modified
Definition: gamelog_internal.h:47
LoggedChange::data
uint64 data
additional data
Definition: gamelog_internal.h:71
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
GamelogChangeType
GamelogChangeType
Type of logged change.
Definition: gamelog_internal.h:16
LoggedAction::change
LoggedChange * change
First logged change in this action.
Definition: gamelog_internal.h:81
_gamelog_action
LoggedAction * _gamelog_action
first logged action
Definition: gamelog.cpp:35
LoggedChange::grfid
uint32 grfid
ID of removed GRF.
Definition: gamelog_internal.h:55
LoggedChange::bug
byte bug
type of bug,
Definition: gamelog_internal.h:73
SlCalcObjLength
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
Calculate the size of an object.
Definition: saveload.cpp:1436
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
LoggedChange::ct
GamelogChangeType ct
Type of change logged in this struct.
Definition: gamelog_internal.h:37
GLAT_NONE
@ GLAT_NONE
No logging active; in savegames, end of list.
Definition: gamelog.h:25
SlReadByte
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:418
ReallocT
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
LoggedChange::oldval
int32 oldval
old value
Definition: gamelog_internal.h:67
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
LoggedChange::text
char text[GAMELOG_REVISION_LENGTH]
revision string, _openttd_revision
Definition: gamelog_internal.h:44
LoggedChange
Contains information about one logged change.
Definition: gamelog_internal.h:36
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
LoggedAction
Contains information about one logged action that caused at least one logged change.
Definition: gamelog_internal.h:80
LoggedChange::version
uint32 version
major and minor version OR ttdp version
Definition: gamelog_internal.h:51
LoggedChange::newval
int32 newval
new value
Definition: gamelog_internal.h:68