liblcf
lmu_movecommand.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of liblcf. Copyright (c) 2020 liblcf authors.
3  * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4  *
5  * liblcf is Free/Libre Open Source Software, released under the MIT License.
6  * For the full copyright and license information, please view the COPYING
7  * file that was distributed with this source code.
8  */
9 
10 #include "rpg_movecommand.h"
11 #include "reader_struct.h"
12 #include <iostream>
13 
14 template <>
15 struct RawStruct<RPG::MoveCommand> {
16  static void ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t length);
17  static void WriteLcf(const RPG::MoveCommand& ref, LcfWriter& stream);
18  static int LcfSize(const RPG::MoveCommand& ref, LcfWriter& stream);
19  static void WriteXml(const RPG::MoveCommand& ref, XmlWriter& stream);
20  static void BeginXml(RPG::MoveCommand& ref, XmlReader& stream);
21 };
22 
23 template <>
24 struct RawStruct<std::vector<RPG::MoveCommand> > {
25  static void ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length);
26  static void WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
27  static int LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
28  static void WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream);
29  static void BeginXml(std::vector<RPG::MoveCommand>& ref, XmlReader& stream);
30 };
31 
35 void RawStruct<RPG::MoveCommand>::ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t /* length */) {
36  ref.command_id = stream.ReadInt();
37  switch (ref.command_id) {
39  stream.Read(ref.parameter_a);
40  break;
42  stream.Read(ref.parameter_a);
43  break;
45  stream.ReadString(ref.parameter_string, stream.ReadInt());
46  stream.Read(ref.parameter_a);
47  break;
49  stream.ReadString(ref.parameter_string, stream.ReadInt());
50  stream.Read(ref.parameter_a);
51  stream.Read(ref.parameter_b);
52  stream.Read(ref.parameter_c);
53  break;
54  }
55 }
56 
58  stream.WriteInt(ref.command_id);
59  switch (ref.command_id) {
61  stream.Write(ref.parameter_a);
62  break;
64  stream.Write(ref.parameter_a);
65  break;
67  stream.WriteInt(stream.Decode(ref.parameter_string).size());
68  stream.Write(ref.parameter_string);
69  stream.Write(ref.parameter_a);
70  break;
72  stream.WriteInt(stream.Decode(ref.parameter_string).size());
73  stream.Write(ref.parameter_string);
74  stream.Write(ref.parameter_a);
75  stream.Write(ref.parameter_b);
76  stream.Write(ref.parameter_c);
77  break;
78  }
79 }
80 
82  int result = 0;
83  result += LcfReader::IntSize(ref.command_id);
84  switch (ref.command_id) {
86  result += LcfReader::IntSize(ref.parameter_a);
87  break;
89  result += LcfReader::IntSize(ref.parameter_a);
90  break;
92  result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
93  result += stream.Decode(ref.parameter_string).size();
94  result += LcfReader::IntSize(ref.parameter_a);
95  break;
97  result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
98  result += stream.Decode(ref.parameter_string).size();
99  result += LcfReader::IntSize(ref.parameter_a);
100  result += LcfReader::IntSize(ref.parameter_b);
101  result += LcfReader::IntSize(ref.parameter_c);
102  break;
103  }
104  return result;
105 }
106 
108  stream.BeginElement("MoveCommand");
109  stream.WriteNode<int32_t>("command_id", ref.command_id);
110  switch (ref.command_id) {
112  stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
113  break;
115  stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
116  break;
118  stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
119  stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
120  break;
122  stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
123  stream.WriteNode<int32_t>("parameter_a", ref.parameter_a);
124  stream.WriteNode<int32_t>("parameter_b", ref.parameter_b);
125  stream.WriteNode<int32_t>("parameter_c", ref.parameter_c);
126  break;
127  }
128  stream.EndElement("MoveCommand");
129 }
130 
132 private:
134  int32_t* field;
136 public:
138  ref(ref), field(NULL), parameter_string(false) {}
139  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
140  if (strcmp(name, "command_id") == 0)
141  field = &ref.command_id;
142  else if (strcmp(name, "parameter_a") == 0)
143  field = &ref.parameter_a;
144  else if (strcmp(name, "parameter_b") == 0)
145  field = &ref.parameter_b;
146  else if (strcmp(name, "parameter_c") == 0)
147  field = &ref.parameter_c;
148  else if (strcmp(name, "parameter_string") == 0)
149  parameter_string = true;
150  else {
151  stream.Error("Unrecognized field '%s'", name);
152  field = NULL;
153  parameter_string = false;
154  }
155  }
156  void EndElement(XmlReader& /* stream */, const char* /* name */) {
157  field = NULL;
158  parameter_string = false;
159  }
160  void CharacterData(XmlReader& /* stream */, const std::string& data) {
161  if (field != NULL)
162  XmlReader::Read<int32_t>(*field, data);
163  else if (parameter_string)
164  XmlReader::Read<std::string>(ref.parameter_string, data);
165  }
166 };
167 
169  stream.SetHandler(new WrapperXmlHandler("MoveCommand", new MoveCommandXmlHandler(ref)));
170 }
171 
175 void RawStruct<std::vector<RPG::MoveCommand> >::ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length) {
176  unsigned long startpos = stream.Tell();
177  unsigned long endpos = startpos + length;
178  while (stream.Tell() != endpos) {
179  RPG::MoveCommand command;
180  RawStruct<RPG::MoveCommand>::ReadLcf(command, stream, 0);
181  ref.push_back(command);
182  }
183 }
184 
185 void RawStruct<std::vector<RPG::MoveCommand> >::WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
186  std::vector<RPG::MoveCommand>::const_iterator it;
187  for (it = ref.begin(); it != ref.end(); it++)
189 }
190 
191 int RawStruct<std::vector<RPG::MoveCommand> >::LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
192  int result = 0;
193  std::vector<RPG::MoveCommand>::const_iterator it;
194  for (it = ref.begin(); it != ref.end(); it++)
195  result += RawStruct<RPG::MoveCommand>::LcfSize(*it, stream);
196  return result;
197 }
198 
199 void RawStruct<std::vector<RPG::MoveCommand> >::WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream) {
200  std::vector<RPG::MoveCommand>::const_iterator it;
201  for (it = ref.begin(); it != ref.end(); it++)
203 }
204 
206 public:
207  MoveCommandVectorXmlHandler(std::vector<RPG::MoveCommand>& ref) : ref(ref) {}
208 
209  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
210  if (strcmp(name, "MoveCommand") != 0)
211  stream.Error("Expecting %s but got %s", "MoveCommand", name);
212  ref.resize(ref.size() + 1);
213  RPG::MoveCommand& obj = ref.back();
214  stream.SetHandler(new MoveCommandXmlHandler(obj));
215  }
216 private:
217  std::vector<RPG::MoveCommand>& ref;
218 };
219 
220 void RawStruct<std::vector<RPG::MoveCommand> >::BeginXml(std::vector<RPG::MoveCommand>& obj, XmlReader& stream) {
221  stream.SetHandler(new MoveCommandVectorXmlHandler(obj));
222 }
static int IntSize(unsigned int x)
Definition: reader_lcf.cpp:299
int ReadInt()
Definition: reader_lcf.cpp:84
uint32_t Tell()
Definition: reader_lcf.cpp:228
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:186
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
void WriteInt(int val)
Definition: writer_lcf.cpp:51
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
std::string Decode(const std::string &str_to_encode)
Definition: writer_lcf.cpp:129
MoveCommandVectorXmlHandler(std::vector< RPG::MoveCommand > &ref)
std::vector< RPG::MoveCommand > & ref
void StartElement(XmlReader &stream, const char *name, const char **)
RPG::MoveCommand & ref
MoveCommandXmlHandler(RPG::MoveCommand &ref)
void StartElement(XmlReader &stream, const char *name, const char **)
void CharacterData(XmlReader &, const std::string &data)
void EndElement(XmlReader &, const char *)
std::string parameter_string
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
void Error(const char *fmt,...)
Definition: reader_xml.cpp:59
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:155
RPG::Database data
Definition: data.cpp:14
Definition: rpg_actor.h:26
static int LcfSize(const T &ref, LcfWriter &stream)
static void WriteXml(const T &ref, XmlWriter &stream)
static void ReadLcf(T &ref, LcfReader &stream, uint32_t length)
static void WriteLcf(const T &ref, LcfWriter &stream)
static void BeginXml(T &ref, XmlReader &stream)