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