]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.cpp
c6e5d7e876498bf5a7c0aae6713ce148f956dfaf
[l2e.git] / src / loader / TypeDescription.cpp
1 #include "TypeDescription.h"
2
3 #include <algorithm>
4 #include <cassert>
5 #include <cstring>
6 #include <ostream>
7 #include <sstream>
8 #include <stdexcept>
9
10 using std::endl;
11 using std::invalid_argument;
12 using std::make_pair;
13 using std::map;
14 using std::string;
15 using std::stringstream;
16 using std::vector;
17
18 namespace loader {
19
20 void TypeDescription::AddField(const std::string &n, const FieldDescription &f) {
21         if (HasField(n)) {
22                 throw std::invalid_argument("duplicate definition of field " + n + " of type " + name);
23         } else {
24                 fields.insert(std::make_pair(n, f));
25         }
26 }
27
28 bool TypeDescription::HasField(const std::string &name) const {
29         return fields.count(name);
30 }
31
32 const FieldDescription &TypeDescription::GetField(const std::string &n) const {
33         map<string, FieldDescription>::const_iterator result(fields.find(n));
34         if (result != fields.end()) {
35                 return result->second;
36         } else {
37                 throw std::invalid_argument("undefined field " + n + " of type " + name);
38         }
39 }
40
41 void TypeDescription::Construct(void *data) const {
42         if (constructor) {
43                 (*constructor)(data);
44         } else {
45                 std::memset(data, 0, Size());
46         }
47 }
48
49 void TypeDescription::Load(void *data) const {
50         if (loader) {
51                 (*loader)(data);
52         }
53 }
54
55
56 void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) {
57         supertypes[id] = offset;
58 }
59
60 bool TypeDescription::IsSubtypeOf(int id) const {
61         return supertypes.count(id);
62 }
63
64 std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const {
65         return supertypes.at(id);
66 }
67
68
69 map<int, TypeDescription> TypeDescription::typeDescriptions;
70 map<string, int> TypeDescription::typeName2ID;
71
72 TypeDescription &TypeDescription::Create(int id, const std::string &name) {
73         if (typeDescriptions.count(id)) {
74                 std::stringstream msg;
75                 msg << "duplicate type ID " << id
76                                 << " (have " << Get(id).TypeName() << ", got " << name << ")";
77                 throw std::invalid_argument(msg.str());
78         }
79         if (typeName2ID.count(name)) {
80                 std::stringstream msg;
81                 msg << "duplicate type name " << name
82                                 << " (have " << GetTypeId(name) << ", got " << id << ")";
83                 throw std::invalid_argument(msg.str());
84         }
85         typeName2ID[name] = id;
86         return typeDescriptions.insert(make_pair(id, TypeDescription(id, name))).first->second;
87 }
88
89 int TypeDescription::GetTypeId(const std::string &name) {
90         map<string, int>::const_iterator i(typeName2ID.find(name));
91         if (i != typeName2ID.end()) {
92                 return i->second;
93         } else {
94                 throw invalid_argument("unknown type name " + name);
95         }
96 }
97
98 const TypeDescription &TypeDescription::Get(int id) {
99         map<int, TypeDescription>::const_iterator i(typeDescriptions.find(id));
100         if (i != typeDescriptions.end()) {
101                 return i->second;
102         } else {
103                 std::stringstream msg;
104                 msg << "invalid type ID " << id;
105                 throw invalid_argument(msg.str());
106         }
107 }
108
109
110 void TypeDescription::WriteSourceWiki(std::ostream &out) {
111         vector<string> types;
112         for (map<int, TypeDescription>::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) {
113                 if (i->second.name != "Animation") {
114                         types.push_back(i->second.name);
115                 }
116         }
117         std::sort(types.begin(), types.end());
118
119         out << "h2. Data types" << endl << endl;
120
121         for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
122                 out << "* [[LoaderSource#" << *i << "|" << *i << "]]" << endl;
123         }
124         out << endl << endl;
125
126         for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
127                 const TypeDescription &td(Get(GetTypeId(*i)));
128                 out << "h3. " << td.TypeName() << endl << endl;
129
130                 out << "Type ID: @" << td.TypeId() << "@" << endl << endl;
131
132                 if (td.Description()) {
133                         out << td.Description() << endl << endl;
134                 }
135
136                 if (td.FieldsBegin() == td.FieldsEnd()) {
137                         out << "No properties." << endl << endl;
138                 } else {
139                         out << "| *Property* | *Type* | *Description* |" << endl;
140                         for (FieldIterator field(td.FieldsBegin()); field != td.FieldsEnd(); ++field) {
141                                 const FieldDescription &fd(field->second);
142                                 out << "| " << field->first << " | ";
143                                 if (fd.IsAggregate()) {
144                                         out << "Array<" << Get(fd.TypeId()).TypeName() << ">";
145                                 } else {
146                                         out << Get(fd.TypeId()).TypeName();
147                                 }
148                                 out << " | ";
149                                 if (fd.Description()) {
150                                         out << fd.Description();
151                                 }
152                                 out << " |" << endl;
153                         }
154                         out << endl;
155                 }
156         }
157 }
158
159 }