Skip to content

Commit

Permalink
Add names to NodeMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron55 committed Nov 29, 2011
1 parent 697ff92 commit 3b3ca65
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/content_nodemeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/content_nodemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SignNodeMetadata : public NodeMetadata
//~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);
Expand All @@ -51,6 +53,8 @@ class ChestNodeMetadata : public NodeMetadata
~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);
Expand All @@ -70,6 +74,8 @@ class LockingChestNodeMetadata : public NodeMetadata
~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);
Expand All @@ -93,6 +99,8 @@ class FurnaceNodeMetadata : public NodeMetadata
~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);
Expand Down
50 changes: 44 additions & 6 deletions src/nodemetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
core::map<std::string, NodeMetadata::Factory> NodeMetadata::m_names;

NodeMetadata::NodeMetadata(IGameDef *gamedef):
m_gamedef(gamedef)
Expand All @@ -41,6 +42,36 @@ NodeMetadata::~NodeMetadata()
{
}

NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
{
// Find factory function
core::map<std::string, Factory>::Node *n;
n = m_names.find(name);
if(n == NULL)
{
// If factory is not found, just return.
errorstream<<"WARNING: NodeMetadata: No factory for name=\""
<<name<<"\""<<std::endl;
return NULL;
}

// Try to load the metadata. If it fails, just return.
try
{
std::istringstream iss("", std::ios_base::binary);

Factory f = n->getValue();
NodeMetadata *meta = (*f)(iss, gamedef);
return meta;
}
catch(SerializationError &e)
{
errorstream<<"NodeMetadata: SerializationError "
<<"while creating name=\""<<name<<"\""<<std::endl;
return NULL;
}
}

NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
{
// Read id
Expand Down Expand Up @@ -89,13 +120,20 @@ void NodeMetadata::serialize(std::ostream &os)
os<<serializeString(oss.str());
}

void NodeMetadata::registerType(u16 id, Factory f)
void NodeMetadata::registerType(u16 id, const std::string &name, Factory f)
{
core::map<u16, Factory>::Node *n;
n = m_types.find(id);
if(n)
return;
m_types.insert(id, f);
{ // typeId
core::map<u16, Factory>::Node *n;
n = m_types.find(id);
if(!n)
m_types.insert(id, f);
}
{ // typeName
core::map<std::string, Factory>::Node *n;
n = m_names.find(name);
if(!n)
m_names.insert(name, f);
}
}

/*
Expand Down
8 changes: 5 additions & 3 deletions src/nodemetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>
#include <iostream>

Expand All @@ -44,11 +44,12 @@ class NodeMetadata
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 "";}
Expand All @@ -69,10 +70,11 @@ class NodeMetadata
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<u16, Factory> m_types;
static core::map<std::string, Factory> m_names;
};

/*
Expand Down

0 comments on commit 3b3ca65

Please sign in to comment.