-
Notifications
You must be signed in to change notification settings - Fork 51
/
reader_struct.cpp
299 lines (268 loc) · 8.36 KB
/
reader_struct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* This file is part of liblcf. Copyright (c) 2017 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#include <cstring>
#include <iostream>
#include <iomanip>
#include <type_traits>
#include "ldb_reader.h"
#include "lmt_reader.h"
#include "lmu_reader.h"
#include "lsd_reader.h"
#include "reader_struct.h"
#include "rpg_save.h"
// Read/Write Struct
template <class S>
void Struct<S>::MakeFieldMap() {
if (!field_map.empty())
return;
for (int i = 0; fields[i] != NULL; i++)
field_map[fields[i]->id] = fields[i];
}
template <class S>
void Struct<S>::MakeTagMap() {
if (!tag_map.empty())
return;
for (int i = 0; fields[i] != NULL; i++)
tag_map[fields[i]->name] = fields[i];
}
template <class S>
void Struct<S>::ReadLcf(S& obj, LcfReader& stream) {
MakeFieldMap();
LcfReader::Chunk chunk_info;
while (!stream.Eof()) {
chunk_info.ID = stream.ReadInt();
if (chunk_info.ID == 0)
break;
chunk_info.length = stream.ReadInt();
if (chunk_info.length == 0)
continue;
typename field_map_type::const_iterator it = field_map.find(chunk_info.ID);
if (it != field_map.end()) {
#ifdef LCF_DEBUG_TRACE
printf("0x%02x (size: %d, pos: 0x%x): %s\n", chunk_info.ID, chunk_info.length, stream.Tell(), it->second->name);
#endif
it->second->ReadLcf(obj, stream, chunk_info.length);
}
else
stream.Skip(chunk_info);
}
}
template<typename T>
typename std::enable_if<std::is_same<T, RPG::Save>::value ||
std::is_same<T, RPG::Database>::value>::type
conditional_zero_writer(LcfWriter&) {
// no-op
}
template<typename T>
typename std::enable_if<!std::is_same<T, RPG::Save>::value &&
!std::is_same<T, RPG::Database>::value>::type
conditional_zero_writer(LcfWriter& stream) {
stream.WriteInt(0);
}
template <class S>
void Struct<S>::WriteLcf(const S& obj, LcfWriter& stream) {
S ref = S();
int last = -1;
for (int i = 0; fields[i] != NULL; i++) {
const Field<S>* field = fields[i];
if (field->id < last)
std::cerr << "field order mismatch: " << field->id
<< " after " << last
<< " in struct " << name
<< std::endl;
if (field->IsDefault(obj, ref)) {
continue;
}
stream.WriteInt(field->id);
stream.WriteInt(field->LcfSize(obj, stream));
field->WriteLcf(obj, stream);
}
// Writing a 0-byte after RPG::Database or RPG::Save breaks the parser in RPG_RT
conditional_zero_writer<S>(stream);
}
template <class S>
int Struct<S>::LcfSize(const S& obj, LcfWriter& stream) {
int result = 0;
S ref = S();
for (int i = 0; fields[i] != NULL; i++) {
const Field<S>* field = fields[i];
//printf("%s\n", field->name);
if (field->IsDefault(obj, ref))
continue;
result += LcfReader::IntSize(field->id);
int size = field->LcfSize(obj, stream);
result += LcfReader::IntSize(size);
result += size;
}
result += LcfReader::IntSize(0);
return result;
}
template <class S>
void Struct<S>::WriteXml(const S& obj, XmlWriter& stream) {
IDReader::WriteXmlTag(obj, name, stream);
for (int i = 0; fields[i] != NULL; i++) {
const Field<S>* field = fields[i];
field->WriteXml(obj, stream);
}
stream.EndElement(name);
}
template <class S>
class StructXmlHandler : public XmlHandler {
public:
StructXmlHandler(S& ref) : ref(ref), field(NULL) {
Struct<S>::MakeTagMap();
}
void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
field = Struct<S>::tag_map[name];
field->BeginXml(ref, stream);
}
void EndElement(XmlReader& /* stream */, const char* /* name */) {
field = NULL;
}
void CharacterData(XmlReader& /* stream */, const std::string& data) {
if (field != NULL)
field->ParseXml(ref, data);
}
private:
S& ref;
const Field<S>* field;
};
template <class S>
class StructFieldXmlHandler : public XmlHandler {
public:
StructFieldXmlHandler(S& ref) : ref(ref) {}
void StartElement(XmlReader& stream, const char* name, const char** atts) {
if (strcmp(name, Struct<S>::name) != 0)
stream.Error("Expecting %s but got %s", Struct<S>::name, name);
Struct<S>::IDReader::ReadIDXml(ref, atts);
stream.SetHandler(new StructXmlHandler<S>(ref));
}
private:
S& ref;
};
template <class S>
void Struct<S>::BeginXml(S& obj, XmlReader& stream) {
stream.SetHandler(new StructFieldXmlHandler<S>(obj));
}
// Read/Write std::vector<Struct>
template <class S>
void Struct<S>::ReadLcf(std::vector<S>& vec, LcfReader& stream) {
int count = stream.ReadInt();
vec.resize(count);
for (int i = 0; i < count; i++) {
IDReader::ReadID(vec[i], stream);
TypeReader<S>::ReadLcf(vec[i], stream, 0);
}
}
template <class S>
void Struct<S>::WriteLcf(const std::vector<S>& vec, LcfWriter& stream) {
int count = vec.size();
stream.WriteInt(count);
for (int i = 0; i < count; i++) {
IDReader::WriteID(vec[i], stream);
TypeReader<S>::WriteLcf(vec[i], stream);
}
}
template <class S>
int Struct<S>::LcfSize(const std::vector<S>& vec, LcfWriter& stream) {
int result = 0;
int count = vec.size();
result += LcfReader::IntSize(count);
for (int i = 0; i < count; i++) {
result += IDReader::IDSize(vec[i]);
result += TypeReader<S>::LcfSize(vec[i], stream);
}
return result;
}
template <class S>
void Struct<S>::WriteXml(const std::vector<S>& vec, XmlWriter& stream) {
int count = vec.size();
for (int i = 0; i < count; i++)
TypeReader<S>::WriteXml(vec[i], stream);
}
template <class S>
class StructVectorXmlHandler : public XmlHandler {
public:
StructVectorXmlHandler(std::vector<S>& ref) : ref(ref) {}
void StartElement(XmlReader& stream, const char* name, const char** atts) {
if (strcmp(name, Struct<S>::name) != 0)
stream.Error("Expecting %s but got %s", Struct<S>::name, name);
ref.resize(ref.size() + 1);
S& obj = ref.back();
Struct<S>::IDReader::ReadIDXml(obj, atts);
stream.SetHandler(new StructXmlHandler<S>(obj));
}
private:
std::vector<S>& ref;
};
template <class S>
void Struct<S>::BeginXml(std::vector<S>& obj, XmlReader& stream) {
stream.SetHandler(new StructVectorXmlHandler<S>(obj));
}
// Instantiate templates
#ifdef _MSC_VER
#pragma warning (disable : 4661)
#endif
template class Struct<RPG::Actor>;
template class Struct<RPG::Animation>;
template class Struct<RPG::AnimationCellData>;
template class Struct<RPG::AnimationFrame>;
template class Struct<RPG::AnimationTiming>;
template class Struct<RPG::Attribute>;
template class Struct<RPG::BattleCommand>;
template class Struct<RPG::BattleCommands>;
template class Struct<RPG::BattlerAnimation>;
template class Struct<RPG::BattlerAnimationData>;
template class Struct<RPG::BattlerAnimationExtension>;
template class Struct<RPG::Chipset>;
template class Struct<RPG::Class>;
template class Struct<RPG::CommonEvent>;
template class Struct<RPG::Database>;
template class Struct<RPG::Encounter>;
template class Struct<RPG::Enemy>;
template class Struct<RPG::EnemyAction>;
template class Struct<RPG::Event>;
template class Struct<RPG::EventPage>;
template class Struct<RPG::EventPageCondition>;
template class Struct<RPG::Item>;
template class Struct<RPG::ItemAnimation>;
template class Struct<RPG::Learning>;
template class Struct<RPG::Map>;
template class Struct<RPG::MapInfo>;
template class Struct<RPG::MoveRoute>;
template class Struct<RPG::Music>;
template class Struct<RPG::Save>;
template class Struct<RPG::SaveActor>;
template class Struct<RPG::SaveCommonEvent>;
template class Struct<RPG::SaveEventCommands>;
template class Struct<RPG::SaveEventData>;
template class Struct<RPG::SaveInventory>;
template class Struct<RPG::SaveMapEvent>;
template class Struct<RPG::SaveMapInfo>;
template class Struct<RPG::SavePartyLocation>;
template class Struct<RPG::SavePicture>;
template class Struct<RPG::SaveScreen>;
template class Struct<RPG::SaveSystem>;
template class Struct<RPG::SaveTarget>;
template class Struct<RPG::SaveTitle>;
template class Struct<RPG::SaveVehicleLocation>;
template class Struct<RPG::Skill>;
template class Struct<RPG::Sound>;
template class Struct<RPG::Start>;
template class Struct<RPG::State>;
template class Struct<RPG::Switch>;
template class Struct<RPG::System>;
template class Struct<RPG::Terms>;
template class Struct<RPG::Terrain>;
template class Struct<RPG::TestBattler>;
template class Struct<RPG::Troop>;
template class Struct<RPG::TroopMember>;
template class Struct<RPG::TroopPage>;
template class Struct<RPG::TroopPageCondition>;
template class Struct<RPG::Variable>;