]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.cpp
19974fb52cced072a31509df6453cf84117f46d6
[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 int TypeDescription::GetMaxSize() {
111         int max = 0;
112         for (map<int, TypeDescription>::const_iterator
113                         i(typeDescriptions.begin()),
114                         end(typeDescriptions.end());
115                         i != end; ++i) {
116                 if (i->second.Size() > max) {
117                         max = i->second.Size();
118                 }
119         }
120         return max;
121 }
122
123
124 void TypeDescription::WriteSourceWiki(std::ostream &out) {
125         vector<string> types;
126         for (map<int, TypeDescription>::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) {
127                 if (i->second.name != "Animation") {
128                         types.push_back(i->second.name);
129                 }
130         }
131         std::sort(types.begin(), types.end());
132
133         out << "h2. Data types" << endl << endl;
134
135         for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
136                 out << "* [[LoaderSource#" << *i << "|" << *i << "]]" << endl;
137         }
138         out << endl << endl;
139
140         for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
141                 const TypeDescription &td(Get(GetTypeId(*i)));
142                 out << "h3. " << td.TypeName() << endl << endl;
143
144                 out << "Type ID: @" << td.TypeId() << "@" << endl << endl;
145
146                 if (td.Description()) {
147                         out << td.Description() << endl << endl;
148                 }
149
150                 if (td.FieldsBegin() == td.FieldsEnd()) {
151                         out << "No properties." << endl << endl;
152                 } else {
153                         out << "| *Property* | *Type* | *Description* |" << endl;
154                         for (FieldIterator field(td.FieldsBegin()); field != td.FieldsEnd(); ++field) {
155                                 const FieldDescription &fd(field->second);
156                                 out << "| " << field->first << " | ";
157                                 if (fd.IsAggregate()) {
158                                         out << "Array<" << Get(fd.TypeId()).TypeName() << ">";
159                                 } else {
160                                         out << Get(fd.TypeId()).TypeName();
161                                 }
162                                 out << " | ";
163                                 if (fd.Description()) {
164                                         out << fd.Description();
165                                 }
166                                 out << " |" << endl;
167                         }
168                         out << endl;
169                 }
170         }
171 }
172
173 }