liblcf
Loading...
Searching...
No Matches
ldb_equipment.cpp
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) 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 "lcf/ldb/reader.h"
11#include "lcf/ldb/chunks.h"
12#include "reader_struct.h"
13
14namespace lcf {
15
16template <>
17struct RawStruct<rpg::Equipment> {
18 static void ReadLcf(rpg::Equipment& ref, LcfReader& stream, uint32_t length);
19 static void WriteLcf(const rpg::Equipment& ref, LcfWriter& stream);
20 static int LcfSize(const rpg::Equipment& ref, LcfWriter& stream);
21 static void WriteXml(const rpg::Equipment& ref, XmlWriter& stream);
22 static void BeginXml(rpg::Equipment& ref, XmlReader& stream);
23};
24
28void RawStruct<rpg::Equipment>::ReadLcf(rpg::Equipment& ref, LcfReader& stream, uint32_t length) {
29 if (length != 10) {
30 fprintf(stderr, "Equipment has incorrect size %" PRIu32 " (expected 10)\n", length);
31
32 LcfReader::Chunk chunk_info;
33 chunk_info.ID = 0x33;
34 chunk_info.length = length;
35
36 stream.Skip(chunk_info, "Equipment");
37
38 return;
39 }
40
41 stream.Read(ref.weapon_id);
42 stream.Read(ref.shield_id);
43 stream.Read(ref.armor_id);
44 stream.Read(ref.helmet_id);
45 stream.Read(ref.accessory_id);
46}
47
48void RawStruct<rpg::Equipment>::WriteLcf(const rpg::Equipment& ref, LcfWriter& stream) {
49 stream.Write(ref.weapon_id);
50 stream.Write(ref.shield_id);
51 stream.Write(ref.armor_id);
52 stream.Write(ref.helmet_id);
53 stream.Write(ref.accessory_id);
54}
55
56int RawStruct<rpg::Equipment>::LcfSize(const rpg::Equipment& /* ref */, LcfWriter& /* stream */) {
57 return 2 * 5;
58}
59
60void RawStruct<rpg::Equipment>::WriteXml(const rpg::Equipment& ref, XmlWriter& stream) {
61 stream.BeginElement("Equipment");
62 stream.WriteNode<int16_t>("weapon_id", ref.weapon_id);
63 stream.WriteNode<int16_t>("shield_id", ref.shield_id);
64 stream.WriteNode<int16_t>("armor_id", ref.armor_id);
65 stream.WriteNode<int16_t>("helmet_id", ref.helmet_id);
66 stream.WriteNode<int16_t>("accessory_id", ref.accessory_id);
67 stream.EndElement("Equipment");
68}
69
70class EquipmentXmlHandler : public XmlHandler {
71private:
72 rpg::Equipment& ref;
73 int16_t* field;
74public:
75 EquipmentXmlHandler(rpg::Equipment& ref) : ref(ref), field(NULL) {}
76 void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
77 if (strcmp(name, "weapon_id") == 0)
78 field = &ref.weapon_id;
79 else if (strcmp(name, "shield_id") == 0)
80 field = &ref.shield_id;
81 else if (strcmp(name, "armor_id") == 0)
82 field = &ref.armor_id;
83 else if (strcmp(name, "helmet_id") == 0)
84 field = &ref.helmet_id;
85 else if (strcmp(name, "accessory_id") == 0)
86 field = &ref.accessory_id;
87 else {
88 stream.Error("Unrecognized field '%s'", name);
89 field = NULL;
90 }
91 }
92 void EndElement(XmlReader& /* stream */, const char* /* name */) {
93 field = NULL;
94 }
95 void CharacterData(XmlReader& /* stream*/, const std::string& data) {
96 if (field != NULL)
97 XmlReader::Read(*field, data);
98 }
99};
100
101void RawStruct<rpg::Equipment>::BeginXml(rpg::Equipment& ref, XmlReader& stream) {
102 stream.SetHandler(new WrapperXmlHandler("Equipment", new EquipmentXmlHandler(ref)));
103}
104
105} //namespace lcf
void CharacterData(XmlReader &, const std::string &data)
void StartElement(XmlReader &stream, const char *name, const char **)
EquipmentXmlHandler(rpg::Equipment &ref)
void EndElement(XmlReader &, const char *)
Definition: dbarray.cpp:13
static void WriteXml(const T &ref, XmlWriter &stream)
static void BeginXml(T &ref, XmlReader &stream)
static void ReadLcf(T &ref, LcfReader &stream, uint32_t length)
static void WriteLcf(const T &ref, LcfWriter &stream)
static int LcfSize(const T &ref, LcfWriter &stream)