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 gamelog_sl.cpp Code handling saving and loading of gamelog data */
12 #include "../stdafx.h"
13 #include "../gamelog_internal.h"
18 #include "../safeguards.h"
20 static const SaveLoad _glog_action_desc
[] = {
21 SLE_VAR(LoggedAction
, tick
, SLE_UINT16
),
25 static const SaveLoad _glog_mode_desc
[] = {
26 SLE_VAR(LoggedChange
, mode
.mode
, SLE_UINT8
),
27 SLE_VAR(LoggedChange
, mode
.landscape
, SLE_UINT8
),
31 static const SaveLoad _glog_revision_desc
[] = {
32 SLE_ARR(LoggedChange
, revision
.text
, SLE_UINT8
, NETWORK_REVISION_LENGTH
),
33 SLE_VAR(LoggedChange
, revision
.newgrf
, SLE_UINT32
),
34 SLE_VAR(LoggedChange
, revision
.slver
, SLE_UINT16
),
35 SLE_VAR(LoggedChange
, revision
.modified
, SLE_UINT8
),
39 static const SaveLoad _glog_oldver_desc
[] = {
40 SLE_VAR(LoggedChange
, oldver
.type
, SLE_UINT32
),
41 SLE_VAR(LoggedChange
, oldver
.version
, SLE_UINT32
),
45 static const SaveLoad _glog_setting_desc
[] = {
46 SLE_STR(LoggedChange
, setting
.name
, SLE_STR
, 128),
47 SLE_VAR(LoggedChange
, setting
.oldval
, SLE_INT32
),
48 SLE_VAR(LoggedChange
, setting
.newval
, SLE_INT32
),
52 static const SaveLoad _glog_grfadd_desc
[] = {
53 SLE_VAR(LoggedChange
, grfadd
.grfid
, SLE_UINT32
),
54 SLE_ARR(LoggedChange
, grfadd
.md5sum
, SLE_UINT8
, 16),
58 static const SaveLoad _glog_grfrem_desc
[] = {
59 SLE_VAR(LoggedChange
, grfrem
.grfid
, SLE_UINT32
),
63 static const SaveLoad _glog_grfcompat_desc
[] = {
64 SLE_VAR(LoggedChange
, grfcompat
.grfid
, SLE_UINT32
),
65 SLE_ARR(LoggedChange
, grfcompat
.md5sum
, SLE_UINT8
, 16),
69 static const SaveLoad _glog_grfparam_desc
[] = {
70 SLE_VAR(LoggedChange
, grfparam
.grfid
, SLE_UINT32
),
74 static const SaveLoad _glog_grfmove_desc
[] = {
75 SLE_VAR(LoggedChange
, grfmove
.grfid
, SLE_UINT32
),
76 SLE_VAR(LoggedChange
, grfmove
.offset
, SLE_INT32
),
80 static const SaveLoad _glog_grfbug_desc
[] = {
81 SLE_VAR(LoggedChange
, grfbug
.data
, SLE_UINT64
),
82 SLE_VAR(LoggedChange
, grfbug
.grfid
, SLE_UINT32
),
83 SLE_VAR(LoggedChange
, grfbug
.bug
, SLE_UINT8
),
87 static const SaveLoad _glog_emergency_desc
[] = {
91 static const SaveLoad
* const _glog_desc
[] = {
102 _glog_emergency_desc
,
105 assert_compile(lengthof(_glog_desc
) == GLCT_END
);
107 static void Load_GLOG_common(LoggedAction
*&gamelog_action
, uint
&gamelog_actions
)
109 assert(gamelog_action
== NULL
);
110 assert(gamelog_actions
== 0);
112 GamelogActionType at
;
113 while ((at
= (GamelogActionType
)SlReadByte()) != GLAT_NONE
) {
114 gamelog_action
= ReallocT(gamelog_action
, gamelog_actions
+ 1);
115 LoggedAction
*la
= &gamelog_action
[gamelog_actions
++];
119 SlObject(la
, _glog_action_desc
); // has to be saved after 'DATE'!
123 GamelogChangeType ct
;
124 while ((ct
= (GamelogChangeType
)SlReadByte()) != GLCT_NONE
) {
125 la
->change
= ReallocT(la
->change
, la
->changes
+ 1);
127 LoggedChange
*lc
= &la
->change
[la
->changes
++];
128 /* for SLE_STR, pointer has to be valid! so make it NULL */
129 memset(lc
, 0, sizeof(*lc
));
132 assert((uint
)ct
< GLCT_END
);
134 SlObject(lc
, _glog_desc
[ct
]);
139 static void Save_GLOG()
141 const LoggedAction
*laend
= &_gamelog_action
[_gamelog_actions
];
144 for (const LoggedAction
*la
= _gamelog_action
; la
!= laend
; la
++) {
145 const LoggedChange
*lcend
= &la
->change
[la
->changes
];
146 for (LoggedChange
*lc
= la
->change
; lc
!= lcend
; lc
++) {
147 assert((uint
)lc
->ct
< lengthof(_glog_desc
));
148 length
+= SlCalcObjLength(lc
, _glog_desc
[lc
->ct
]) + 1;
156 for (LoggedAction
*la
= _gamelog_action
; la
!= laend
; la
++) {
158 SlObject(la
, _glog_action_desc
);
160 const LoggedChange
*lcend
= &la
->change
[la
->changes
];
161 for (LoggedChange
*lc
= la
->change
; lc
!= lcend
; lc
++) {
163 assert((uint
)lc
->ct
< GLCT_END
);
164 SlObject(lc
, _glog_desc
[lc
->ct
]);
166 SlWriteByte(GLCT_NONE
);
168 SlWriteByte(GLAT_NONE
);
171 static void Load_GLOG()
173 Load_GLOG_common(_gamelog_action
, _gamelog_actions
);
176 static void Check_GLOG()
178 Load_GLOG_common(_load_check_data
.gamelog_action
, _load_check_data
.gamelog_actions
);
181 extern const ChunkHandler _gamelog_chunk_handlers
[] = {
182 { 'GLOG', Save_GLOG
, Load_GLOG
, NULL
, Check_GLOG
, CH_RIFF
| CH_LAST
}