From: kwolekr Date: Mon, 4 May 2015 02:05:24 +0000 (-0400) Subject: ObjDefManager: Set replacement object's handle info after calling set() X-Git-Tag: 0.4.13~316 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5b237b4d94cc3fa4a95f951654dfbe37dcf2e2f9;p=oweals%2Fminetest.git ObjDefManager: Set replacement object's handle info after calling set() Make gamedef optional when constructing an ObjDefManager Add note about object ownership --- diff --git a/src/mapgen.cpp b/src/mapgen.cpp index d8eb8068a..1de7fca81 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -434,7 +434,7 @@ void GenerateNotifier::getEvents( ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type) { m_objtype = type; - m_ndef = gamedef->getNodeDefManager(); + m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL; } @@ -471,7 +471,16 @@ ObjDef *ObjDefManager::get(ObjDefHandle handle) const ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj) { u32 index = validateHandle(handle); - return (index != OBJDEF_INVALID_INDEX) ? setRaw(index, obj) : NULL; + if (index == OBJDEF_INVALID_INDEX) + return NULL; + + ObjDef *oldobj = setRaw(index, obj); + + obj->uid = oldobj->uid; + obj->index = oldobj->index; + obj->handle = oldobj->handle; + + return oldobj; } diff --git a/src/mapgen.h b/src/mapgen.h index ee7e8227e..5be004e9f 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -206,12 +206,15 @@ public: std::string name; }; +// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is +// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted, +// so the same ObjDef instance must not be referenced multiple class ObjDefManager { public: ObjDefManager(IGameDef *gamedef, ObjDefType type); virtual ~ObjDefManager(); - virtual const char *getObjectTitle() const = 0; + virtual const char *getObjectTitle() const { return "ObjDef"; } virtual void clear(); virtual ObjDef *getByName(const std::string &name) const;