X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FTypeDescription.cpp;h=c6e5d7e876498bf5a7c0aae6713ce148f956dfaf;hb=cc3d698b8c1ad09d7a3f9e3f28bc84e0ac1735ea;hp=07be7b29f165dd091e36d78e292851377546c858;hpb=ac3755adc509404528ef7de58695bf8e3bfb7dcd;p=l2e.git diff --git a/src/loader/TypeDescription.cpp b/src/loader/TypeDescription.cpp index 07be7b2..c6e5d7e 100644 --- a/src/loader/TypeDescription.cpp +++ b/src/loader/TypeDescription.cpp @@ -1,17 +1,18 @@ -/* - * TypeDescription.cpp - * - * Created on: Sep 4, 2012 - * Author: holy - */ - #include "TypeDescription.h" +#include #include +#include +#include +#include #include +using std::endl; +using std::invalid_argument; +using std::make_pair; using std::map; using std::string; +using std::stringstream; using std::vector; namespace loader { @@ -37,6 +38,20 @@ const FieldDescription &TypeDescription::GetField(const std::string &n) const { } } +void TypeDescription::Construct(void *data) const { + if (constructor) { + (*constructor)(data); + } else { + std::memset(data, 0, Size()); + } +} + +void TypeDescription::Load(void *data) const { + if (loader) { + (*loader)(data); + } +} + void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) { supertypes[id] = offset; @@ -51,31 +66,94 @@ std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const { } -vector TypeDescription::typeDescriptions; +map TypeDescription::typeDescriptions; +map TypeDescription::typeName2ID; -TypeDescription &TypeDescription::CreateOrGet(const std::string &name) { - for (vector::iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) { - if (i->name == name) { - return *i; - } +TypeDescription &TypeDescription::Create(int id, const std::string &name) { + if (typeDescriptions.count(id)) { + std::stringstream msg; + msg << "duplicate type ID " << id + << " (have " << Get(id).TypeName() << ", got " << name << ")"; + throw std::invalid_argument(msg.str()); + } + if (typeName2ID.count(name)) { + std::stringstream msg; + msg << "duplicate type name " << name + << " (have " << GetTypeId(name) << ", got " << id << ")"; + throw std::invalid_argument(msg.str()); } - typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name)); - return typeDescriptions.back(); + typeName2ID[name] = id; + return typeDescriptions.insert(make_pair(id, TypeDescription(id, name))).first->second; } int TypeDescription::GetTypeId(const std::string &name) { - for (vector::size_type i(0), end(typeDescriptions.size()); i < end; ++i) { - if (typeDescriptions[i].name == name) { - return i; - } + map::const_iterator i(typeName2ID.find(name)); + if (i != typeName2ID.end()) { + return i->second; + } else { + throw invalid_argument("unknown type name " + name); } - typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name)); - return typeDescriptions.size() - 1; } const TypeDescription &TypeDescription::Get(int id) { - assert(id >= 0 && id < int(typeDescriptions.size())); - return typeDescriptions[id]; + map::const_iterator i(typeDescriptions.find(id)); + if (i != typeDescriptions.end()) { + return i->second; + } else { + std::stringstream msg; + msg << "invalid type ID " << id; + throw invalid_argument(msg.str()); + } +} + + +void TypeDescription::WriteSourceWiki(std::ostream &out) { + vector types; + for (map::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) { + if (i->second.name != "Animation") { + types.push_back(i->second.name); + } + } + std::sort(types.begin(), types.end()); + + out << "h2. Data types" << endl << endl; + + for (vector::const_iterator i(types.begin()), end(types.end()); i != end; ++i) { + out << "* [[LoaderSource#" << *i << "|" << *i << "]]" << endl; + } + out << endl << endl; + + for (vector::const_iterator i(types.begin()), end(types.end()); i != end; ++i) { + const TypeDescription &td(Get(GetTypeId(*i))); + out << "h3. " << td.TypeName() << endl << endl; + + out << "Type ID: @" << td.TypeId() << "@" << endl << endl; + + if (td.Description()) { + out << td.Description() << endl << endl; + } + + if (td.FieldsBegin() == td.FieldsEnd()) { + out << "No properties." << endl << endl; + } else { + out << "| *Property* | *Type* | *Description* |" << endl; + for (FieldIterator field(td.FieldsBegin()); field != td.FieldsEnd(); ++field) { + const FieldDescription &fd(field->second); + out << "| " << field->first << " | "; + if (fd.IsAggregate()) { + out << "Array<" << Get(fd.TypeId()).TypeName() << ">"; + } else { + out << Get(fd.TypeId()).TypeName(); + } + out << " | "; + if (fd.Description()) { + out << fd.Description(); + } + out << " |" << endl; + } + out << endl; + } + } } }