]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.cpp
c2ce0d4145abf20ee1b90c291ad7965d2e5446aa
[l2e.git] / src / loader / Interpreter.cpp
1 /*
2  * Interpreter.cpp
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #include "Interpreter.h"
9
10 #include "ParsedSource.h"
11 #include "../battle/Hero.h"
12 #include "../battle/Monster.h"
13 #include "../battle/PartyLayout.h"
14 #include "../battle/Resources.h"
15 #include "../common/Ikari.h"
16 #include "../common/Item.h"
17 #include "../common/Script.h"
18 #include "../common/Spell.h"
19 #include "../common/Stats.h"
20 #include "../common/TargetingMode.h"
21 #include "../graphics/ComplexAnimation.h"
22 #include "../graphics/Font.h"
23 #include "../graphics/Frame.h"
24 #include "../graphics/Gauge.h"
25 #include "../graphics/Menu.h"
26 #include "../graphics/SimpleAnimation.h"
27 #include "../graphics/Sprite.h"
28
29 #include <algorithm>
30 #include <cstring>
31 #include <SDL_image.h>
32
33 using battle::Hero;
34 using battle::Monster;
35 using battle::PartyLayout;
36 using common::Ikari;
37 using common::Item;
38 using common::Script;
39 using common::Spell;
40 using common::Stats;
41 using common::TargetingMode;
42 using graphics::Animation;
43 using graphics::Color;
44 using graphics::Font;
45 using graphics::Frame;
46 using graphics::Gauge;
47 using graphics::ComplexAnimation;
48 using graphics::SimpleAnimation;
49 using graphics::Sprite;
50 using geometry::Vector;
51 using std::make_pair;
52 using std::set;
53 using std::string;
54 using std::vector;
55
56 namespace loader {
57
58 Interpreter::~Interpreter() {
59         for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
60                 SDL_FreeSurface(i->second);
61         }
62 }
63
64
65 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
66         return parsedDefinitions.at(identifier);
67 }
68
69
70 void *Interpreter::GetObject(int typeId, const std::string &name) {
71         std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
72         if (i != parsedDefinitions.end()) {
73                 const TypeDescription &requested(TypeDescription::Get(typeId));
74                 const TypeDescription &actual(TypeDescription::Get(i->second.type));
75                 if (requested.TypeId() == actual.TypeId()) {
76                         return values[actual.TypeId()][i->second.id];
77                 } else if (actual.IsSubtypeOf(requested)) {
78                         char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
79                         std::ptrdiff_t offset(actual.SupertypeOffset(requested));
80                         return sub - offset;
81                 } else {
82                         throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
83                 }
84         } else {
85                 throw Error("access to undefined object " + name);
86         }
87 }
88
89
90 void Interpreter::ReadSource() {
91         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
92                 ReadDefinition(source.GetDefinition(*i));
93         }
94 }
95
96 void Interpreter::ReadDefinition(const Definition &dfn) {
97         if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
98                 return;
99         }
100         if (dfn.HasLiteralValue()) {
101                 ReadLiteral(dfn);
102         } else {
103                 ReadObject(dfn);
104         }
105 }
106
107 void Interpreter::ReadLiteral(const Definition &dfn) {
108         const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
109         int typeId(TypeDescription::GetTypeId(typeName));
110         int id(values[typeId].size());
111         const TypeDescription &td(TypeDescription::Get(typeId));
112         int size(
113                         (dfn.GetLiteral()->GetType() == Literal::PATH
114                                         || dfn.GetLiteral()->GetType() == Literal::STRING)
115                         ? dfn.GetLiteral()->GetString().size() : td.Size());
116         char *object(alloc.Alloc(size));
117         if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
118                 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
119         } else {
120                 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
121         }
122         values[typeId].push_back(object);
123         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
124 }
125
126 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
127         switch (literal.GetType()) {
128                 case Literal::ARRAY_VALUES:
129                         throw Error("named value arrays are not supported, sorry");
130                         break;
131                 case Literal::ARRAY_PROPS:
132                         throw Error("named property list arrays are not supported, sorry");
133                         break;
134                 case Literal::BOOLEAN:
135                         new (object) bool(literal.GetBoolean());
136                         break;
137                 case Literal::COLOR:
138                         new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
139                         break;
140                 case Literal::NUMBER:
141                         new (object) int(literal.GetNumber());
142                         break;
143                 case Literal::PATH:
144                         std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
145                         object[literal.GetString().size()] = '\0';
146                         break;
147                 case Literal::SCRIPT:
148                         new (object) Script;
149                         ReadScript(literal.GetScript(), reinterpret_cast<Script *>(object));
150                         break;
151                 case Literal::STRING:
152                         std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
153                         object[literal.GetString().size()] = '\0';
154                         break;
155                 case Literal::VECTOR:
156                         new (object) Vector<int>(literal.GetX(), literal.GetY());
157                         break;
158                 case Literal::OBJECT:
159                         throw Error("illogical branch: read literal object as non-object literal");
160         }
161 }
162
163
164 void *Interpreter::GetObject(int typeId, const Value &v) {
165         if (v.IsLiteral()) {
166                 if (v.GetLiteral().IsObject()) {
167                         int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
168                         const TypeDescription &td(TypeDescription::Get(typeId));
169                         char *object(alloc.Alloc(td.Size()));
170                         td.Construct(object);
171                         int id(values[typeId].size());
172                         values[typeId].push_back(object);
173                         ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
174                         return object;
175                 } else {
176                         int typeId(0), id(0);
177                         switch (v.GetLiteral().GetType()) {
178                                 case Literal::ARRAY_VALUES:
179                                         throw Error("cannot copy value arrays, sorry");
180                                         break;
181                                 case Literal::ARRAY_PROPS:
182                                         throw Error("cannot copy property list arrays, sorry");
183                                         break;
184                                 case Literal::BOOLEAN:
185                                         {
186                                                 typeId = TypeDescription::GetTypeId("Boolean");
187                                                 id = values[typeId].size();
188                                                 const TypeDescription &td(TypeDescription::Get(typeId));
189                                                 char *object(alloc.Alloc(td.Size()));
190                                                 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
191                                         }
192                                         break;
193                                 case Literal::COLOR:
194                                         {
195                                                 typeId = TypeDescription::GetTypeId("Color");
196                                                 id = values[typeId].size();
197                                                 const TypeDescription &td(TypeDescription::Get(typeId));
198                                                 char *object(alloc.Alloc(td.Size()));
199                                                 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
200                                         }
201                                         break;
202                                 case Literal::NUMBER:
203                                         {
204                                                 typeId = TypeDescription::GetTypeId("Number");
205                                                 id = values[typeId].size();
206                                                 const TypeDescription &td(TypeDescription::Get(typeId));
207                                                 char *object(alloc.Alloc(td.Size()));
208                                                 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
209                                         }
210                                         break;
211                                 case Literal::PATH:
212                                         {
213                                                 typeId = TypeDescription::GetTypeId("Path");
214                                                 id = values[typeId].size();
215                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
216                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
217                                                 str[v.GetLiteral().GetString().size()] = '\0';
218                                                 values[typeId].push_back(str);
219                                         }
220                                         break;
221                                 case Literal::SCRIPT:
222                                         {
223                                                 typeId = TypeDescription::GetTypeId("Script");
224                                                 id = values[typeId].size();
225                                                 values[typeId].push_back(ReadScript(v.GetLiteral().GetScript()));
226                                         }
227                                         break;
228                                 case Literal::STRING:
229                                         {
230                                                 typeId = TypeDescription::GetTypeId("String");
231                                                 id = values[typeId].size();
232                                                 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
233                                                 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
234                                                 str[v.GetLiteral().GetString().size()] = '\0';
235                                                 values[typeId].push_back(str);
236                                         }
237                                         break;
238                                 case Literal::VECTOR:
239                                         {
240                                                 typeId = TypeDescription::GetTypeId("Vector");
241                                                 id = values[typeId].size();
242                                                 const TypeDescription &td(TypeDescription::Get(typeId));
243                                                 char *object(alloc.Alloc(td.Size()));
244                                                 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
245                                         }
246                                         break;
247                                 case Literal::OBJECT:
248                                         {
249                                                 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
250                                                 const TypeDescription &td(TypeDescription::Get(typeId));
251                                                 id = values[typeId].size();
252                                                 char *object(alloc.Alloc(td.Size()));
253                                                 td.Construct(object);
254                                                 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
255                                         }
256                                         break;
257                         }
258                         return values[typeId][id];
259                 }
260         } else {
261                 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
262                 return GetObject(typeId, v.GetIdentifier());
263         }
264 }
265
266
267 void Interpreter::ReadObject(const Definition &dfn) {
268         int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
269         const TypeDescription &td(TypeDescription::Get(typeId));
270         int id(values[typeId].size());
271         char *object(alloc.Alloc(td.Size()));
272         td.Construct(object);
273         values[typeId].push_back(object);
274         ReadObject(typeId, id, object, *dfn.GetProperties());
275         parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
276 }
277
278
279 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
280         const TypeDescription &td(TypeDescription::Get(typeId));
281         for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
282                 const FieldDescription &fd(td.GetField(i->first));
283                 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
284                 if (CanLink(*i->second)) {
285                         char *dest(object + fd.Offset());
286                         if (fd.IsAggregate()) {
287                                 int arraySize(i->second->GetLiteral().ArraySize());
288                                 char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
289                                 char *iter(aggregate);
290                                 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
291                                         const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
292                                         for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
293                                                 fieldType.Construct(iter);
294                                                 ReadObject(fieldType.TypeId(), -1, iter, **j);
295                                         }
296                                 } else {
297                                         const vector<Value *> &list(i->second->GetLiteral().GetValues());
298                                         for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
299                                                 fieldType.Construct(iter);
300                                                 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
301                                         }
302                                 }
303                                 if (fd.IsReferenced()) {
304                                         std::memcpy(dest, &aggregate, sizeof(char *));
305                                         dest += sizeof(char *);
306                                         std::memcpy(dest, &arraySize, sizeof(int));
307                                 } else {
308                                         throw Error("aggregate type fields must be referenced");
309                                 }
310                         } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
311                                 // inline literals
312                                 if (i->second->GetLiteral().IsObject()) {
313                                         ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
314                                 } else {
315                                         ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
316                                 }
317                         } else {
318                                 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
319                                 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
320                                         src = reinterpret_cast<char *>(GetImage(src));
321                                 }
322                                 if (fd.IsReferenced()) {
323                                         std::memcpy(dest, &src, sizeof(char *));
324                                 } else {
325                                         std::memcpy(dest, src, fieldType.Size());
326                                 }
327                         }
328                 } else {
329                         Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
330                 }
331         }
332         td.Load(object);
333 }
334
335
336 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
337         int size(0);
338         for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
339                 if ((*i)->GetType() != ScriptToken::COMMAND) {
340                         throw Error("unexpected script token");
341                 }
342                 ++size;
343                 const string &cmd((*i)->CommandName());
344                 if (cmd == "move") {
345                         ++i;
346                         if (i == end) {
347                                 throw Error("unexpected script end after move");
348                         }
349                         const string &reg((*i)->RegisterName());
350                         switch (reg[0]) {
351                                 case 'a':
352                                         size += sizeof(void *);
353                                         break;
354                                 case 'i':
355                                         size += sizeof(int);
356                                         break;
357                                 case 'v':
358                                         size += sizeof(Vector<int>);
359                                         break;
360                                 default:
361                                         throw Error("unknown register " + reg);
362                         }
363                         ++i;
364                         if (i == end) {
365                                 throw Error("unexpected script end after move");
366                         }
367                 } else if (cmd == "add") {
368                         ++i;
369                         if (i == end) {
370                                 throw Error("unexpected script end after add");
371                         }
372                         const string &reg((*i)->RegisterName());
373                         switch (reg[0]) {
374                                 case 'i':
375                                         size += sizeof(int);
376                                         break;
377                                 case 'v':
378                                         size += sizeof(Vector<int>);
379                                         break;
380                                 default:
381                                         throw Error("expected register after add " + reg);
382                         }
383                         ++i;
384                         if (i == end) {
385                                 throw Error("unexpected script end after add");
386                         }
387                 } else if (cmd == "mod") {
388                         ++i;
389                         if (i == end) {
390                                 throw Error("unexpected script end after mod");
391                         }
392                         size += sizeof(int);
393                         ++i;
394                         if (i == end) {
395                                 throw Error("unexpected script end after mod");
396                         }
397                 } else if (cmd == "rand") {
398                         ++i;
399                         if (i == end) {
400                                 throw Error("unexpected script end after rand");
401                         }
402                         size += sizeof(int);
403                         ++i;
404                         if (i == end) {
405                                 throw Error("unexpected script end after rand");
406                         }
407                 } else if (cmd == "sysc") {
408
409                 } else {
410                         throw Error("unknown command " + cmd);
411                 }
412         }
413
414         unsigned char *text(reinterpret_cast<unsigned char *>(alloc.Alloc(size)));
415         int cursor(0);
416         for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
417                 if ((*i)->GetType() != ScriptToken::COMMAND) {
418                         throw Error("unexpected script token");
419                 }
420                 const string &cmd((*i)->CommandName());
421                 if (cmd == "move") {
422                         ++i;
423                         const string &reg((*i)->RegisterName());
424                         ++i;
425                         if (reg == "a0") {
426                                 text[cursor] = Script::CODE_MOVE_A0;
427                                 ++cursor;
428                                 ReadScriptAddress(**i, text + cursor);
429                                 cursor += sizeof(void *);
430                         } else if (reg == "a1") {
431                                 text[cursor] = Script::CODE_MOVE_A1;
432                                 ++cursor;
433                                 ReadScriptAddress(**i, text + cursor);
434                                 cursor += sizeof(void *);
435                         } else if (reg == "i0") {
436                                 text[cursor] = Script::CODE_MOVE_I0;
437                                 ++cursor;
438                                 ReadScriptInteger(**i, text + cursor);
439                                 cursor += sizeof(int);
440                         } else if (reg == "i1") {
441                                 text[cursor] = Script::CODE_MOVE_I1;
442                                 ++cursor;
443                                 ReadScriptInteger(**i, text + cursor);
444                                 cursor += sizeof(int);
445                         } else if (reg == "v0") {
446                                 text[cursor] = Script::CODE_MOVE_V0;
447                                 ++cursor;
448                                 ReadScriptVector(**i, text + cursor);
449                                 cursor += sizeof(Vector<int>);
450                         } else if (reg == "v1") {
451                                 text[cursor] = Script::CODE_MOVE_V1;
452                                 ++cursor;
453                                 ReadScriptVector(**i, text + cursor);
454                                 cursor += sizeof(Vector<int>);
455                         } else {
456                                 throw Error("unknown register " + reg);
457                         }
458                 } else if (cmd == "add") {
459                         ++i;
460                         const string &reg((*i)->RegisterName());
461                         ++i;
462                         if (reg == "i0") {
463                                 text[cursor] = Script::CODE_ADD_I0;
464                                 ++cursor;
465                                 ReadScriptInteger(**i, text + cursor);
466                                 cursor += sizeof(int);
467                         } else if (reg == "i1") {
468                                 text[cursor] = Script::CODE_ADD_I1;
469                                 ++cursor;
470                                 ReadScriptInteger(**i, text + cursor);
471                                 cursor += sizeof(int);
472                         } else if (reg == "v0") {
473                                 text[cursor] = Script::CODE_ADD_V0;
474                                 ++cursor;
475                                 ReadScriptVector(**i, text + cursor);
476                                 cursor += sizeof(Vector<int>);
477                         } else if (reg == "v1") {
478                                 text[cursor] = Script::CODE_ADD_V1;
479                                 ++cursor;
480                                 ReadScriptVector(**i, text + cursor);
481                                 cursor += sizeof(Vector<int>);
482                         } else {
483                                 throw Error("unexpected register " + reg);
484                         }
485                 } else if (cmd == "mod") {
486                         ++i;
487                         const string &reg((*i)->RegisterName());
488                         ++i;
489                         if (reg == "i0") {
490                                 text[cursor] = Script::CODE_MOD_I0;
491                                 ++cursor;
492                                 ReadScriptInteger(**i, text + cursor);
493                                 cursor += sizeof(int);
494                         } else if (reg == "i1") {
495                                 text[cursor] = Script::CODE_MOD_I1;
496                                 ++cursor;
497                                 ReadScriptInteger(**i, text + cursor);
498                                 cursor += sizeof(int);
499                         } else {
500                                 throw Error("unexpected register " + reg);
501                         }
502                 } else if (cmd == "rand") {
503                         ++i;
504                         const string &reg((*i)->RegisterName());
505                         if (reg == "i0") {
506                                 text[cursor] = Script::CODE_RAND_I0;
507                                 ++cursor;
508                         } else if (reg == "i1") {
509                                 text[cursor] = Script::CODE_RAND_I1;
510                                 ++cursor;
511                         } else {
512                                 throw Error("unexpected register " + reg);
513                         }
514                 } else if (cmd == "sysc") {
515                         text[cursor] = Script::CODE_SYSCALL;
516                         ++cursor;
517                 } else {
518                         throw Error("unknown command " + cmd);
519                 }
520         }
521
522         script->text = text;
523         script->textlen = size;
524 }
525
526 char *Interpreter::ReadScript(const vector<ScriptToken *> &s) {
527         char *mem(alloc.Alloc(sizeof(Script)));
528         new (mem) Script;
529         Script *script(reinterpret_cast<Script *>(mem));
530         ReadScript(s, script);
531         return mem;
532 }
533
534 void Interpreter::ReadScriptAddress(const ScriptToken &t, unsigned char *dest) {
535         if (t.GetType() != ScriptToken::IDENTIFIER) {
536                 throw Error("expected identifier for address");
537         }
538         if (source.IsDefined(t.GetIdentifier())) {
539                 const ParsedDefinition &def(GetDefinition(t.GetIdentifier()));
540                 void *addr(GetObject(def.type, t.GetIdentifier()));
541                 *reinterpret_cast<void **>(dest) = addr;
542         } else {
543                 throw Error("postponing values in scripts not implemented");
544         }
545 }
546
547 void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) {
548         if (t.GetType() == ScriptToken::IDENTIFIER) {
549                 if (source.IsDefined(t.GetIdentifier())) {
550                         void *num(GetObject(TypeDescription::GetTypeId("Number"), t.GetIdentifier()));
551                         *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
552                 } else {
553                         throw Error("postponing values in scripts not implemented");
554                 }
555         } else if (t.GetType() == ScriptToken::LITERAL) {
556                 *reinterpret_cast<int *>(dest) = t.GetLiteral()->GetNumber();
557         } else {
558                 throw Error("expected identifier or integer literal");
559         }
560 }
561
562 void Interpreter::ReadScriptVector(const ScriptToken &t, unsigned char *dest) {
563         if (t.GetType() == ScriptToken::IDENTIFIER) {
564                 if (source.IsDefined(t.GetIdentifier())) {
565                         void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.GetIdentifier()));
566                         *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
567                 } else {
568                         throw Error("postponing values in scripts not implemented");
569                 }
570         } else if (t.GetType() == ScriptToken::LITERAL) {
571                 *reinterpret_cast<Vector<int> *>(dest) = Vector<int>(t.GetLiteral()->GetX(), t.GetLiteral()->GetY());
572         } else {
573                 throw Error("expected identifier or vector literal");
574         }
575 }
576
577
578 SDL_Surface *Interpreter::GetImage(const string &path) {
579         std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
580         if (result != imageCache.end()) {
581                 return result->second;
582         } else {
583                 SDL_Surface *image(IMG_Load(path.c_str()));
584                 imageCache.insert(make_pair(path, image));
585                 return image;
586         }
587 }
588
589
590 bool Interpreter::CanLink(const Value &v) const {
591         return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
592 }
593
594 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
595         char *str(alloc.Alloc(identifier.size() + 1));
596         std::memcpy(str, identifier.c_str(), identifier.size());
597         str[identifier.size()] = '\0';
598         postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
599 }
600
601
602 void Interpreter::CreateTypeDescriptions() {
603         {
604                 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
605                 td.SetDescription("Logical value which can be either true or false.");
606                 td.SetSize(sizeof(bool));
607         }
608         {
609                 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
610                 td.SetDescription(
611                                 "A color in RGB format with an optional alpha channel.\n"
612                                 "Components range from 0 to 255.\n"
613                                 "Alpha defaults to 255 if omitted.");
614                 td.SetSize(sizeof(Color));
615         }
616         {
617                 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
618                 td.SetDescription("Path to a PNG file with image data.");
619                 td.SetSize(sizeof(SDL_Surface));
620         }
621         {
622                 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
623                 td.SetDescription("A signed integer.");
624                 td.SetSize(sizeof(int));
625         }
626         {
627                 int stringId(TypeDescription::GetTypeId("String"));
628                 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
629                 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
630                 td.SetSize(1);
631                 td.AddSupertype(stringId, 0);
632         }
633         {
634                 TypeDescription &td(TypeDescription::CreateOrGet("Script"));
635                 td.SetDescription("Collection of commands that define a behaviour.");
636                 td.SetSize(sizeof(Script));
637         }
638         {
639                 TypeDescription &td(TypeDescription::CreateOrGet("String"));
640                 td.SetDescription("Some characters.");
641                 td.SetSize(1);
642         }
643         {
644                 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
645                 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
646                 td.SetSize(sizeof(Vector<int>));
647         }
648 }
649
650 }