From 2b04ab874d75711bc021a0cd8dc7fca68f4e6929 Mon Sep 17 00:00:00 2001
From: kwolekr <kwolekr@minetest.net>
Date: Tue, 11 Aug 2015 22:27:54 -0400
Subject: SAPI: Track last executed mod and include in error messages

---
 src/script/lua_api/l_env.cpp    |  6 +++++-
 src/script/lua_api/l_server.cpp | 27 +++++++++++++++++++++++++++
 src/script/lua_api/l_server.h   |  6 ++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

(limited to 'src/script/lua_api')

diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index 48c46c079..9d1936769 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -68,6 +68,8 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
 		FATAL_ERROR("");
 	lua_remove(L, -2); // Remove registered_abms
 
+	scriptIface->setOriginFromTable(-1);
+
 	// Call action
 	luaL_checktype(L, -1, LUA_TTABLE);
 	lua_getfield(L, -1, "action");
@@ -78,7 +80,9 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
 	lua_pushnumber(L, active_object_count);
 	lua_pushnumber(L, active_object_count_wider);
 
-	PCALL_RESL(L, lua_pcall(L, 4, 0, errorhandler));
+	int result = lua_pcall(L, 4, 0, errorhandler);
+	if (result)
+		scriptIface->scriptError(result, "LuaABM::trigger");
 
 	lua_pop(L, 1); // Pop error handler
 }
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index 96c0327df..73eca9d60 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -438,6 +438,31 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
 	return 0;
 }
 
+// get_last_run_mod()
+int ModApiServer::l_get_last_run_mod(lua_State *L)
+{
+	NO_MAP_LOCK_REQUIRED;
+	lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
+	const char *current_mod = lua_tostring(L, -1);
+	if (current_mod == NULL || current_mod[0] == '\0') {
+		lua_pop(L, 1);
+		lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
+	}
+	return 1;
+}
+
+// set_last_run_mod(modname)
+int ModApiServer::l_set_last_run_mod(lua_State *L)
+{
+	NO_MAP_LOCK_REQUIRED;
+#ifdef SCRIPTAPI_DEBUG
+	const char *mod = lua_tostring(L, 1);
+	getScriptApiBase(L)->setOriginDirect(mod);
+	//printf(">>>> last mod set from Lua: %s\n", mod);
+#endif
+	return 0;
+}
+
 #ifndef NDEBUG
 // cause_error(type_of_error)
 int ModApiServer::l_cause_error(lua_State *L)
@@ -495,6 +520,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
 	API_FCT(unban_player_or_ip);
 	API_FCT(notify_authentication_modified);
 
+	API_FCT(get_last_run_mod);
+	API_FCT(set_last_run_mod);
 #ifndef NDEBUG
 	API_FCT(cause_error);
 #endif
diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h
index e14bef043..df31f325f 100644
--- a/src/script/lua_api/l_server.h
+++ b/src/script/lua_api/l_server.h
@@ -88,6 +88,12 @@ private:
 	// notify_authentication_modified(name)
 	static int l_notify_authentication_modified(lua_State *L);
 
+	// get_last_run_mod()
+	static int l_get_last_run_mod(lua_State *L);
+
+	// set_last_run_mod(modname)
+	static int l_set_last_run_mod(lua_State *L);
+
 #ifndef NDEBUG
 	//  cause_error(type_of_error)
 	static int l_cause_error(lua_State *L);
-- 
cgit v1.2.3