-
Notifications
You must be signed in to change notification settings - Fork 51
/
lmu_movecommand.cpp
221 lines (205 loc) · 7.92 KB
/
lmu_movecommand.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
/*
* 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 "rpg_movecommand.h"
#include "reader_struct.h"
template <>
struct RawStruct<RPG::MoveCommand> {
static void ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t length);
static void WriteLcf(const RPG::MoveCommand& ref, LcfWriter& stream);
static int LcfSize(const RPG::MoveCommand& ref, LcfWriter& stream);
static void WriteXml(const RPG::MoveCommand& ref, XmlWriter& stream);
static void BeginXml(RPG::MoveCommand& ref, XmlReader& stream);
};
template <>
struct RawStruct<std::vector<RPG::MoveCommand> > {
static void ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length);
static void WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
static int LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
static void WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream);
static void BeginXml(std::vector<RPG::MoveCommand>& ref, XmlReader& stream);
};
/**
* Reads Move Command.
*/
void RawStruct<RPG::MoveCommand>::ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t /* length */) {
ref.command_id = stream.ReadInt();
switch (ref.command_id) {
case RPG::MoveCommand::Code::switch_on:
stream.Read(ref.parameter_a);
break;
case RPG::MoveCommand::Code::switch_off:
stream.Read(ref.parameter_a);
break;
case RPG::MoveCommand::Code::change_graphic:
stream.ReadString(ref.parameter_string, stream.ReadInt());
stream.Read(ref.parameter_a);
break;
case RPG::MoveCommand::Code::play_sound_effect:
stream.ReadString(ref.parameter_string, stream.ReadInt());
stream.Read(ref.parameter_a);
stream.Read(ref.parameter_b);
stream.Read(ref.parameter_c);
break;
}
}
void RawStruct<RPG::MoveCommand>::WriteLcf(const RPG::MoveCommand& ref, LcfWriter& stream) {
stream.WriteInt(ref.command_id);
switch (ref.command_id) {
case RPG::MoveCommand::Code::switch_on:
stream.Write(ref.parameter_a);
break;
case RPG::MoveCommand::Code::switch_off:
stream.Write(ref.parameter_a);
break;
case RPG::MoveCommand::Code::change_graphic:
stream.WriteInt(stream.Decode(ref.parameter_string).size());
stream.Write(ref.parameter_string);
stream.Write(ref.parameter_a);
break;
case RPG::MoveCommand::Code::play_sound_effect:
stream.WriteInt(stream.Decode(ref.parameter_string).size());
stream.Write(ref.parameter_string);
stream.Write(ref.parameter_a);
stream.Write(ref.parameter_b);
stream.Write(ref.parameter_c);
break;
}
}
int RawStruct<RPG::MoveCommand>::LcfSize(const RPG::MoveCommand& ref, LcfWriter& stream) {
int result = 0;
result += LcfReader::IntSize(ref.command_id);
switch (ref.command_id) {
case RPG::MoveCommand::Code::switch_on:
result += LcfReader::IntSize(ref.parameter_a);
break;
case RPG::MoveCommand::Code::switch_off:
result += LcfReader::IntSize(ref.parameter_a);
break;
case RPG::MoveCommand::Code::change_graphic:
result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
result += stream.Decode(ref.parameter_string).size();
result += LcfReader::IntSize(ref.parameter_a);
break;
case RPG::MoveCommand::Code::play_sound_effect:
result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
result += stream.Decode(ref.parameter_string).size();
result += LcfReader::IntSize(ref.parameter_a);
result += LcfReader::IntSize(ref.parameter_b);
result += LcfReader::IntSize(ref.parameter_c);
break;
}
return result;
}
void RawStruct<RPG::MoveCommand>::WriteXml(const RPG::MoveCommand& ref, XmlWriter& stream) {
stream.BeginElement("MoveCommand");
stream.WriteNode<int32_t>("command_id", ref.command_id);
switch (ref.command_id) {
case RPG::MoveCommand::Code::switch_on:
stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
break;
case RPG::MoveCommand::Code::switch_off:
stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
break;
case RPG::MoveCommand::Code::change_graphic:
stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
break;
case RPG::MoveCommand::Code::play_sound_effect:
stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
stream.WriteNode<int32_t>("parameter_b", ref.parameter_b);
stream.WriteNode<int32_t>("parameter_c", ref.parameter_c);
break;
}
stream.EndElement("MoveCommand");
}
class MoveCommandXmlHandler : public XmlHandler {
private:
RPG::MoveCommand& ref;
int32_t* field;
bool parameter_string;
public:
MoveCommandXmlHandler(RPG::MoveCommand& ref) :
ref(ref), field(NULL), parameter_string(false) {}
void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
if (strcmp(name, "command_id") == 0)
field = &ref.command_id;
else if (strcmp(name, "parameter_a") == 0)
field = &ref.parameter_a;
else if (strcmp(name, "parameter_b") == 0)
field = &ref.parameter_b;
else if (strcmp(name, "parameter_c") == 0)
field = &ref.parameter_c;
else if (strcmp(name, "parameter_string") == 0)
parameter_string = true;
else {
stream.Error("Unrecognized field '%s'", name);
field = NULL;
parameter_string = false;
}
}
void EndElement(XmlReader& /* stream */, const char* /* name */) {
field = NULL;
parameter_string = false;
}
void CharacterData(XmlReader& /* stream */, const std::string& data) {
if (field != NULL)
XmlReader::Read<int32_t>(*field, data);
else if (parameter_string)
XmlReader::Read<std::string>(ref.parameter_string, data);
}
};
void RawStruct<RPG::MoveCommand>::BeginXml(RPG::MoveCommand& ref, XmlReader& stream) {
stream.SetHandler(new WrapperXmlHandler("MoveCommand", new MoveCommandXmlHandler(ref)));
}
/**
* Reads Move Commands.
*/
void RawStruct<std::vector<RPG::MoveCommand> >::ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length) {
unsigned long startpos = stream.Tell();
unsigned long endpos = startpos + length;
do {
RPG::MoveCommand command;
RawStruct<RPG::MoveCommand>::ReadLcf(command, stream, 0);
ref.push_back(command);
} while (stream.Tell() != endpos);
}
void RawStruct<std::vector<RPG::MoveCommand> >::WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
std::vector<RPG::MoveCommand>::const_iterator it;
for (it = ref.begin(); it != ref.end(); it++)
RawStruct<RPG::MoveCommand>::WriteLcf(*it, stream);
}
int RawStruct<std::vector<RPG::MoveCommand> >::LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
int result = 0;
std::vector<RPG::MoveCommand>::const_iterator it;
for (it = ref.begin(); it != ref.end(); it++)
result += RawStruct<RPG::MoveCommand>::LcfSize(*it, stream);
return result;
}
void RawStruct<std::vector<RPG::MoveCommand> >::WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream) {
std::vector<RPG::MoveCommand>::const_iterator it;
for (it = ref.begin(); it != ref.end(); it++)
RawStruct<RPG::MoveCommand>::WriteXml(*it, stream);
}
class MoveCommandVectorXmlHandler : public XmlHandler {
public:
MoveCommandVectorXmlHandler(std::vector<RPG::MoveCommand>& ref) : ref(ref) {}
void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
if (strcmp(name, "MoveCommand") != 0)
stream.Error("Expecting %s but got %s", "MoveCommand", name);
ref.resize(ref.size() + 1);
RPG::MoveCommand& obj = ref.back();
stream.SetHandler(new MoveCommandXmlHandler(obj));
}
private:
std::vector<RPG::MoveCommand>& ref;
};
void RawStruct<std::vector<RPG::MoveCommand> >::BeginXml(std::vector<RPG::MoveCommand>& obj, XmlReader& stream) {
stream.SetHandler(new MoveCommandVectorXmlHandler(obj));
}