]> git.lizzy.rs Git - minetest.git/blobdiff - src/nodemetadata.cpp
Fix and tune stars
[minetest.git] / src / nodemetadata.cpp
index ef6ea1cd2062b75ca248c8dd80b90f815edf9657..410b4e2ea78bcb12cf89020dba9451aeb2336312 100644 (file)
@@ -30,8 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        NodeMetadata
 */
 
-core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
-
 NodeMetadata::NodeMetadata(IGameDef *gamedef):
        m_gamedef(gamedef)
 {
@@ -41,6 +39,34 @@ NodeMetadata::~NodeMetadata()
 {
 }
 
+NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
+{
+       // Find factory function
+       core::map<std::string, Factory2>::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
+       {
+               Factory2 f2 = n->getValue();
+               NodeMetadata *meta = (*f2)(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
@@ -89,13 +115,21 @@ 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,
+               Factory2 f2)
 {
-       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, Factory2>::Node *n;
+               n = m_names.find(name);
+               if(!n)
+                       m_names.insert(name, f2);
+       }
 }
 
 /*