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) 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
14template <>
15struct 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
26void 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
54int 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
69private:
71 int16_t* field;
72public:
74 void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
75 if (strcmp(name, "weapon_id") == 0)
77 else if (strcmp(name, "shield_id") == 0)
79 else if (strcmp(name, "armor_id") == 0)
81 else if (strcmp(name, "helmet_id") == 0)
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)
95 XmlReader::Read(*field, data);
96 }
97};
98
100 stream.SetHandler(new WrapperXmlHandler("Equipment", new EquipmentXmlHandler(ref)));
101}
RPG::Equipment & ref
void EndElement(XmlReader &, const char *)
void CharacterData(XmlReader &, const std::string &data)
EquipmentXmlHandler(RPG::Equipment &ref)
void StartElement(XmlReader &stream, const char *name, const char **)
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
void Skip(const struct LcfReader::Chunk &chunk_info)
Definition: reader_lcf.cpp:273
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
int16_t shield_id
Definition: rpg_equipment.h:25
int16_t accessory_id
Definition: rpg_equipment.h:28
int16_t armor_id
Definition: rpg_equipment.h:26
int16_t helmet_id
Definition: rpg_equipment.h:27
int16_t weapon_id
Definition: rpg_equipment.h:24
static void Read(T &ref, const std::string &data)
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
Definition: rpg_actor.h:26
uint32_t length
Definition: reader_lcf.h:76
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)