liblcf
ldb_equipment.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 "ldb_reader.h"
11 #include "ldb_chunks.h"
12 #include "reader_struct.h"
13 
14 template <>
15 struct RawStruct<RPG::Equipment> {
16  static void ReadLcf(RPG::Equipment& ref, LcfReader& stream, uint32_t length);
17  static void WriteLcf(const RPG::Equipment& ref, LcfWriter& stream);
18  static int LcfSize(const RPG::Equipment& ref, LcfWriter& stream);
19  static void WriteXml(const RPG::Equipment& ref, XmlWriter& stream);
20  static void BeginXml(RPG::Equipment& ref, XmlReader& stream);
21 };
22 
26 void RawStruct<RPG::Equipment>::ReadLcf(RPG::Equipment& ref, LcfReader& stream, uint32_t length) {
27  if (length != 10) {
28  fprintf(stderr, "Equipment has incorrect size %" PRIu32 " (expected 10)\n", length);
29 
30  LcfReader::Chunk chunk_info;
31  chunk_info.ID = 0x33;
32  chunk_info.length = length;
33 
34  stream.Skip(chunk_info);
35 
36  return;
37  }
38 
39  stream.Read(ref.weapon_id);
40  stream.Read(ref.shield_id);
41  stream.Read(ref.armor_id);
42  stream.Read(ref.helmet_id);
43  stream.Read(ref.accessory_id);
44 }
45 
47  stream.Write(ref.weapon_id);
48  stream.Write(ref.shield_id);
49  stream.Write(ref.armor_id);
50  stream.Write(ref.helmet_id);
51  stream.Write(ref.accessory_id);
52 }
53 
54 int RawStruct<RPG::Equipment>::LcfSize(const RPG::Equipment& /* ref */, LcfWriter& /* stream */) {
55  return 2 * 5;
56 }
57 
59  stream.BeginElement("Equipment");
60  stream.WriteNode<int16_t>("weapon_id", ref.weapon_id);
61  stream.WriteNode<int16_t>("shield_id", ref.shield_id);
62  stream.WriteNode<int16_t>("armor_id", ref.armor_id);
63  stream.WriteNode<int16_t>("helmet_id", ref.helmet_id);
64  stream.WriteNode<int16_t>("accessory_id", ref.accessory_id);
65  stream.EndElement("Equipment");
66 }
67 
69 private:
71  int16_t* field;
72 public:
74  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
75  if (strcmp(name, "weapon_id") == 0)
76  field = &ref.weapon_id;
77  else if (strcmp(name, "shield_id") == 0)
78  field = &ref.shield_id;
79  else if (strcmp(name, "armor_id") == 0)
80  field = &ref.armor_id;
81  else if (strcmp(name, "helmet_id") == 0)
82  field = &ref.helmet_id;
83  else if (strcmp(name, "accessory_id") == 0)
85  else {
86  stream.Error("Unrecognized field '%s'", name);
87  field = NULL;
88  }
89  }
90  void EndElement(XmlReader& /* stream */, const char* /* name */) {
91  field = NULL;
92  }
93  void CharacterData(XmlReader& /* stream*/, const std::string& data) {
94  if (field != NULL)
96  }
97 };
98 
100  stream.SetHandler(new WrapperXmlHandler("Equipment", new EquipmentXmlHandler(ref)));
101 }
LcfReader::Read
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
XmlReader::Read
static void Read(T &ref, const std::string &data)
ldb_chunks.h
RawStruct::ReadLcf
static void ReadLcf(T &ref, LcfReader &stream, uint32_t length)
RawStruct::WriteLcf
static void WriteLcf(const T &ref, LcfWriter &stream)
XmlReader::Error
void Error(const char *fmt,...)
Definition: reader_xml.cpp:59
XmlHandler
Definition: reader_xml.h:116
XmlReader
Definition: reader_xml.h:31
WrapperXmlHandler
Definition: reader_struct.h:705
EquipmentXmlHandler::field
int16_t * field
Definition: ldb_equipment.cpp:71
LcfReader::Chunk::length
uint32_t length
Definition: reader_lcf.h:76
XmlReader::SetHandler
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
RawStruct::BeginXml
static void BeginXml(T &ref, XmlReader &stream)
EquipmentXmlHandler::CharacterData
void CharacterData(XmlReader &, const std::string &data)
Definition: ldb_equipment.cpp:93
EquipmentXmlHandler::ref
RPG::Equipment & ref
Definition: ldb_equipment.cpp:70
RPG
Definition: rpg_actor.h:26
XmlWriter::EndElement
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
EquipmentXmlHandler::EquipmentXmlHandler
EquipmentXmlHandler(RPG::Equipment &ref)
Definition: ldb_equipment.cpp:73
reader_struct.h
LcfWriter
Definition: writer_lcf.h:27
XmlWriter
Definition: writer_xml.h:22
EquipmentXmlHandler
Definition: ldb_equipment.cpp:68
LcfWriter::Write
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
RPG::Equipment::armor_id
int16_t armor_id
Definition: rpg_equipment.h:26
RPG::Equipment::helmet_id
int16_t helmet_id
Definition: rpg_equipment.h:27
EquipmentXmlHandler::StartElement
void StartElement(XmlReader &stream, const char *name, const char **)
Definition: ldb_equipment.cpp:74
RPG::Equipment::shield_id
int16_t shield_id
Definition: rpg_equipment.h:25
RawStruct::WriteXml
static void WriteXml(const T &ref, XmlWriter &stream)
RPG::Equipment
Definition: rpg_equipment.h:22
LcfReader::Chunk
Definition: reader_lcf.h:70
RawStruct
Definition: reader_struct.h:97
Data::data
RPG::Database data
Definition: data.cpp:14
RawStruct::LcfSize
static int LcfSize(const T &ref, LcfWriter &stream)
LcfReader::Chunk::ID
uint32_t ID
Definition: reader_lcf.h:75
XmlWriter::WriteNode
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:155
RPG::Equipment::accessory_id
int16_t accessory_id
Definition: rpg_equipment.h:28
RPG::Equipment::weapon_id
int16_t weapon_id
Definition: rpg_equipment.h:24
LcfReader
Definition: reader_lcf.h:35
EquipmentXmlHandler::EndElement
void EndElement(XmlReader &, const char *)
Definition: ldb_equipment.cpp:90
LcfReader::Skip
void Skip(const struct LcfReader::Chunk &chunk_info)
Definition: reader_lcf.cpp:273
XmlWriter::BeginElement
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
ldb_reader.h