From 7b802c547dc23b022605bc7b934c31461b47622b Mon Sep 17 00:00:00 2001
From: Perttu Ahola <celeron55@gmail.com>
Date: Sat, 12 Nov 2011 18:34:04 +0200
Subject: [PATCH] Handle undefined objects better

---
 data/scripts/default.lua         |  63 -------------------------------
 data/textures/unknown_object.png | Bin 0 -> 489 bytes
 src/content_sao.cpp              |   9 +++--
 src/luaentity_common.cpp         |   2 +-
 src/scriptapi.cpp                |  22 +++++++----
 src/scriptapi.h                  |   3 +-
 6 files changed, 23 insertions(+), 76 deletions(-)
 create mode 100644 data/textures/unknown_object.png

diff --git a/data/scripts/default.lua b/data/scripts/default.lua
index 4197cd788..0b9f8b913 100644
--- a/data/scripts/default.lua
+++ b/data/scripts/default.lua
@@ -1,66 +1,3 @@
---[[function basicSerialize(o)
-	if type(o) == "number" then
-		return tostring(o)
-	else   -- assume it is a string
-		return string.format("%q", o)
-	end
-end
-
-function dump2(name, value, saved)
-	saved = saved or {}       -- initial value
-	io.write(name, " = ")
-	if type(value) == "number" or type(value) == "string" then
-		io.write(basicSerialize(value), "\n")
-	elseif type(value) == "table" then
-		if saved[value] then    -- value already saved?
-			io.write(saved[value], "\n")  -- use its previous name
-		else
-			saved[value] = name   -- save name for next time
-			io.write("{}\n")     -- create a new table
-			for k,v in pairs(value) do      -- save its fields
-				local fieldname = string.format("%s[%s]", name,
-												basicSerialize(k))
-				save(fieldname, v, saved)
-			end
-		end
-	else
-		error("cannot save a " .. type(value))
-	end
-end]]
-
---[[function dump(o, name, dumped, s)
-	name = name or "_"
-	dumped = dumped or {}
-	s = s or ""
-	s = s .. name .. " = "
-	if type(o) == "number" then
-		s = s .. tostring(o)
-	elseif type(o) == "string" then
-		s = s .. string.format("%q", o)
-	elseif type(o) == "boolean" then
-		s = s .. tostring(o)
-	elseif type(o) == "function" then
-		s = s .. "<function>"
-	elseif type(o) == "nil" then
-		s = s .. "nil"
-	elseif type(o) == "table" then
-		if dumped[o] then
-			s = s .. dumped[o]
-		else
-			dumped[o] = name
-			local t = {}
-			for k,v in pairs(o) do
-				t[#t+1] = dump(v, k, dumped)
-			end
-			s = s .. "{" .. table.concat(t, ", ") .. "}"
-		end
-	else
-		error("cannot dump a " .. type(o))
-		return nil
-	end
-	return s
-end]]
-
 function basic_dump2(o)
 	if type(o) == "number" then
 		return tostring(o)
diff --git a/data/textures/unknown_object.png b/data/textures/unknown_object.png
new file mode 100644
index 0000000000000000000000000000000000000000..4d6a98d569c16acda00e87c8ad997002ad8ce61c
GIT binary patch
literal 489
zcmV<F0T%v=P)<h;3K|Lk000e1NJLTq001BW001Be1^@s6b9#F800004b3#c}2nYxW
zd<bNS00009a7bBm000ie000ie0hKEb8vp<Si%CR5R9J<@m+P&AFbsu1x7~>j!BDVM
zrOv=km3)7IQzwK+0=FE5l0e`bKVpIa%7u6Us9vB!K#)-YfQW>9`udzl)ZQKEm!qKp
z_zEy$X`@SrNmD2q2?=bcSiM4Q<SA}`Anl`0NPFKT4;)A&WEO&A;zFcen0X+F+4Ta8
zXYX?Hp9$FIflA`Qk@J8pL9<laN7m|ae>yzUYu3-T*;O!8IxR_4oF<PgC*S7gW#8?V
z;%#;2!b4{wlww~AfudM@ac2FJhqBMBs_N@Ap6E}`hDHI6LvS#|Yw#6t0eLLohv?};
z|AbFp9dVI>G>iAF^(|sURR~JRw`xaLz*4DQ>MxD0?dbLkF!!dFFINe3V@2uc=;;IT
znt;PeSQ3sLW*#t0_avU53!|liL>70`u>@f(#gu$!QZZ{;zJxb#1_rk+PvX5sUQ6sf
zVcYU}N3<iJmY_9Ory{^_%XcPIEpd(<(oN|`g*!s-MR*~*LvRMU3GlF8CQqIW3?VuG
f3n_<i@$dN`sKM^&-}uWs00000NkvXXu0mjfz;w~d

literal 0
HcmV?d00001

diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index eeb17bd30..1096ac116 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -1568,12 +1568,13 @@ void LuaEntitySAO::addedToEnvironment(u16 id)
 	ServerActiveObject::addedToEnvironment(id);
 	
 	// Create entity from name and state
-	m_registered = true;
 	lua_State *L = m_env->getLua();
-	scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str());
+	m_registered = scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str());
 	
-	// Get properties
-	scriptapi_luaentity_get_properties(L, m_id, m_prop);
+	if(m_registered){
+		// Get properties
+		scriptapi_luaentity_get_properties(L, m_id, m_prop);
+	}
 }
 
 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
diff --git a/src/luaentity_common.cpp b/src/luaentity_common.cpp
index 26cfce1c2..138e72dcb 100644
--- a/src/luaentity_common.cpp
+++ b/src/luaentity_common.cpp
@@ -29,7 +29,7 @@ LuaEntityProperties::LuaEntityProperties():
 	collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
 	visual("single_sprite")
 {
-	textures.push_back("unknown_block.png");
+	textures.push_back("unknown_object.png");
 }
 
 std::string LuaEntityProperties::dump()
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index 381664489..6d0522000 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -38,12 +38,12 @@ extern "C" {
 
 /*
 TODO:
-- Global environment step function
+- Node type definition
 - Random node triggers
 - Object visual client-side stuff
 	- Blink effect
 	- Spritesheets and animation
-- Named node types and dynamic id allocation
+- Named node types and dynamic id allocation per MapBlock
 - LuaNodeMetadata
 	blockdef.has_metadata = true/false
 	- Stores an inventory and stuff in a Settings object
@@ -558,15 +558,17 @@ void scriptapi_export(lua_State *L, Server *server)
 	// Get the main minetest table
 	lua_getglobal(L, "minetest");
 
-	// Add registered_entities table in minetest
+	// Add tables to minetest
+	
+	/*lua_newtable(L);
+	lua_setfield(L, -2, "registered_blocks");*/
+
 	lua_newtable(L);
 	lua_setfield(L, -2, "registered_entities");
 
-	// Add object_refs table in minetest
 	lua_newtable(L);
 	lua_setfield(L, -2, "object_refs");
 
-	// Add luaentities table in minetest
 	lua_newtable(L);
 	lua_setfield(L, -2, "luaentities");
 
@@ -694,7 +696,7 @@ void scriptapi_environment_step(lua_State *L, float dtime)
 	luaentity
 */
 
-void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
+bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
 		const char *init_state)
 {
 	realitycheck(L);
@@ -712,7 +714,11 @@ void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
 	lua_pushstring(L, name);
 	lua_gettable(L, -2);
 	// Should be a table, which we will use as a prototype
-	luaL_checktype(L, -1, LUA_TTABLE);
+	//luaL_checktype(L, -1, LUA_TTABLE);
+	if(lua_type(L, -1) != LUA_TTABLE){
+		errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
+		return false;
+	}
 	int prototype_table = lua_gettop(L);
 	//dump2(L, "prototype_table");
 	
@@ -750,6 +756,8 @@ void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
 	if(lua_pcall(L, 1, 0, 0))
 		script_error(L, "error running function %s:on_activate: %s\n",
 				name, lua_tostring(L, -1));*/
+
+	return true;
 }
 
 void scriptapi_luaentity_rm(lua_State *L, u16 id)
diff --git a/src/scriptapi.h b/src/scriptapi.h
index c63977fc6..de8eb6abe 100644
--- a/src/scriptapi.h
+++ b/src/scriptapi.h
@@ -39,7 +39,8 @@ void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj);
 void scriptapi_environment_step(lua_State *L, float dtime);
 
 /* luaentity */
-void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
+// Returns true if succesfully added into Lua; false otherwise.
+bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
 		const char *init_state);
 void scriptapi_luaentity_rm(lua_State *L, u16 id);
 std::string scriptapi_luaentity_get_state(lua_State *L, u16 id);
-- 
GitLab