From 3b3ca65562375b3cfdf8919acae8c3259df05c91 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 25 Nov 2011 15:17:54 +0200 Subject: [PATCH] Add names to NodeMetadata --- src/content_nodemeta.cpp | 8 +++---- src/content_nodemeta.h | 8 +++++++ src/nodemetadata.cpp | 50 +++++++++++++++++++++++++++++++++++----- src/nodemetadata.h | 8 ++++--- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/content_nodemeta.cpp b/src/content_nodemeta.cpp index ab984966..25ad3f0f 100644 --- a/src/content_nodemeta.cpp +++ b/src/content_nodemeta.cpp @@ -38,7 +38,7 @@ SignNodeMetadata::SignNodeMetadata(IGameDef *gamedef, std::string text): NodeMetadata(gamedef), m_text(text) { - NodeMetadata::registerType(typeId(), create); + NodeMetadata::registerType(typeId(), typeName(), create); } u16 SignNodeMetadata::typeId() const { @@ -72,7 +72,7 @@ ChestNodeMetadata proto_ChestNodeMetadata(NULL); ChestNodeMetadata::ChestNodeMetadata(IGameDef *gamedef): NodeMetadata(gamedef) { - NodeMetadata::registerType(typeId(), create); + NodeMetadata::registerType(typeId(), typeName(), create); m_inventory = new Inventory(); m_inventory->addList("0", 8*4); @@ -135,7 +135,7 @@ LockingChestNodeMetadata proto_LockingChestNodeMetadata(NULL); LockingChestNodeMetadata::LockingChestNodeMetadata(IGameDef *gamedef): NodeMetadata(gamedef) { - NodeMetadata::registerType(typeId(), create); + NodeMetadata::registerType(typeId(), typeName(), create); m_inventory = new Inventory(); m_inventory->addList("0", 8*4); @@ -200,7 +200,7 @@ FurnaceNodeMetadata proto_FurnaceNodeMetadata(NULL); FurnaceNodeMetadata::FurnaceNodeMetadata(IGameDef *gamedef): NodeMetadata(gamedef) { - NodeMetadata::registerType(typeId(), create); + NodeMetadata::registerType(typeId(), typeName(), create); m_inventory = new Inventory(); m_inventory->addList("fuel", 1); diff --git a/src/content_nodemeta.h b/src/content_nodemeta.h index 86df882e..2535e985 100644 --- a/src/content_nodemeta.h +++ b/src/content_nodemeta.h @@ -31,6 +31,8 @@ public: //~SignNodeMetadata(); virtual u16 typeId() const; + virtual const char* typeName() const + { return "sign"; } static NodeMetadata* create(std::istream &is, IGameDef *gamedef); virtual NodeMetadata* clone(IGameDef *gamedef); virtual void serializeBody(std::ostream &os); @@ -51,6 +53,8 @@ public: ~ChestNodeMetadata(); virtual u16 typeId() const; + virtual const char* typeName() const + { return "chest"; } static NodeMetadata* create(std::istream &is, IGameDef *gamedef); virtual NodeMetadata* clone(IGameDef *gamedef); virtual void serializeBody(std::ostream &os); @@ -70,6 +74,8 @@ public: ~LockingChestNodeMetadata(); virtual u16 typeId() const; + virtual const char* typeName() const + { return "locked_chest"; } static NodeMetadata* create(std::istream &is, IGameDef *gamedef); virtual NodeMetadata* clone(IGameDef *gamedef); virtual void serializeBody(std::ostream &os); @@ -93,6 +99,8 @@ public: ~FurnaceNodeMetadata(); virtual u16 typeId() const; + virtual const char* typeName() const + { return "furnace"; } virtual NodeMetadata* clone(IGameDef *gamedef); static NodeMetadata* create(std::istream &is, IGameDef *gamedef); virtual void serializeBody(std::ostream &os); diff --git a/src/nodemetadata.cpp b/src/nodemetadata.cpp index ef6ea1cd..2f47aba2 100644 --- a/src/nodemetadata.cpp +++ b/src/nodemetadata.cpp @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ core::map NodeMetadata::m_types; +core::map NodeMetadata::m_names; NodeMetadata::NodeMetadata(IGameDef *gamedef): m_gamedef(gamedef) @@ -41,6 +42,36 @@ NodeMetadata::~NodeMetadata() { } +NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef) +{ + // Find factory function + core::map::Node *n; + n = m_names.find(name); + if(n == NULL) + { + // If factory is not found, just return. + errorstream<<"WARNING: NodeMetadata: No factory for name=\"" + <getValue(); + NodeMetadata *meta = (*f)(iss, gamedef); + return meta; + } + catch(SerializationError &e) + { + errorstream<<"NodeMetadata: SerializationError " + <<"while creating name=\""<::Node *n; - n = m_types.find(id); - if(n) - return; - m_types.insert(id, f); + { // typeId + core::map::Node *n; + n = m_types.find(id); + if(!n) + m_types.insert(id, f); + } + { // typeName + core::map::Node *n; + n = m_names.find(name); + if(!n) + m_names.insert(name, f); + } } /* diff --git a/src/nodemetadata.h b/src/nodemetadata.h index 9eb08678..67b80b64 100644 --- a/src/nodemetadata.h +++ b/src/nodemetadata.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef NODEMETADATA_HEADER #define NODEMETADATA_HEADER -#include "common_irrlicht.h" +#include "irrlichttypes.h" #include #include @@ -44,11 +44,12 @@ public: NodeMetadata(IGameDef *gamedef); virtual ~NodeMetadata(); + static NodeMetadata* create(const std::string &name, IGameDef *gamedef); static NodeMetadata* deSerialize(std::istream &is, IGameDef *gamedef); void serialize(std::ostream &os); - // This usually is the CONTENT_ value virtual u16 typeId() const = 0; + virtual const char* typeName() const = 0; virtual NodeMetadata* clone(IGameDef *gamedef) = 0; virtual void serializeBody(std::ostream &os) = 0; virtual std::string infoText() {return "";} @@ -69,10 +70,11 @@ public: virtual std::string getText(){ return ""; } virtual void setText(const std::string &t){} protected: - static void registerType(u16 id, Factory f); + static void registerType(u16 id, const std::string &name, Factory f); IGameDef *m_gamedef; private: static core::map m_types; + static core::map m_names; }; /* -- 2.30.2