end
core.registered_auth_handler = handler
core.registered_auth_handler_modname = core.get_current_modname()
+ handler.mod_origin = core.registered_auth_handler_modname
end
function core.get_auth_handler()
def.params = def.params or ""
def.description = def.description or ""
def.privs = def.privs or {}
+ def.mod_origin = core.get_current_modname() or "??"
core.chatcommands[cmd] = def
end
end
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
if has_privs then
+ core.set_last_run_mod(cmd_def.mod_origin)
local success, message = cmd_def.func(name, param)
if message then
core.chat_send_player(name, message)
stuff.on_put = callbacks.on_put
stuff.on_take = callbacks.on_take
end
+ stuff.mod_origin = core.get_current_modname() or "??"
core.detached_inventories[name] = stuff
return core.create_detached_inventory_raw(name)
end
-- Run script hook
local _, callback
for _, callback in ipairs(core.registered_on_dignodes) do
+ local origin = core.callback_origins[callback]
+ if origin then
+ core.set_last_run_mod(origin.mod)
+ --print("Running " .. tostring(callback) ..
+ -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
+ else
+ --print("No data associated with callback")
+ end
+
-- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
local timer = timers[index]
timer.time = timer.time - delay
if timer.time <= 0 then
+ core.set_last_run_mod(timer.mod_origin)
timer.func(unpack(timer.args or {}))
table.remove(timers, index)
sub = sub + 1
"Invalid core.after invocation")
if not mintime then
mintime = time
- timers_to_add = {{time=time+delay, func=func, args={...}}}
+ timers_to_add = {{
+ time = time+delay,
+ func = func,
+ args = {...},
+ mod_origin = core.get_last_run_mod(),
+ }}
return
end
mintime = math.min(mintime, time)
timers_to_add = timers_to_add or {}
- timers_to_add[#timers_to_add+1] = {time=time+delay, func=func, args={...}}
+ timers_to_add[#timers_to_add+1] = {
+ time = time+delay,
+ func = func,
+ args = {...},
+ mod_origin = core.get_last_run_mod(),
+ }
end
function core.check_player_privs(name, privs)
function core.register_abm(spec)
-- Add to core.registered_abms
core.registered_abms[#core.registered_abms+1] = spec
+ spec.mod_origin = core.get_current_modname() or "??"
end
function core.register_entity(name, prototype)
-- Add to core.registered_entities
core.registered_entities[name] = prototype
+ prototype.mod_origin = core.get_current_modname() or "??"
end
function core.register_item(name, itemdef)
end
-- END Legacy stuff
+ itemdef.mod_origin = core.get_current_modname() or "??"
+
-- Disable all further modifications
getmetatable(itemdef).__newindex = {}
end
+core.callback_origins = {}
+
function core.run_callbacks(callbacks, mode, ...)
assert(type(callbacks) == "table")
local cb_len = #callbacks
end
local ret = nil
for i = 1, cb_len do
+ local origin = core.callback_origins[callbacks[i]]
+ if origin then
+ core.set_last_run_mod(origin.mod)
+ --print("Running " .. tostring(callbacks[i]) ..
+ -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
+ else
+ --print("No data associated with callback")
+ end
local cb_ret = callbacks[i](...)
if mode == 0 and i == 1 then
local function make_registration()
local t = {}
- local registerfunc = function(func) table.insert(t, func) end
+ local registerfunc = function(func)
+ table.insert(t, func)
+ core.callback_origins[func] = {
+ mod = core.get_current_modname() or "??",
+ name = debug.getinfo(1, "n").name or "??"
+ }
+ --local origin = core.callback_origins[func]
+ --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
+ end
return t, registerfunc
end
local function make_registration_reverse()
local t = {}
- local registerfunc = function(func) table.insert(t, 1, func) end
+ local registerfunc = function(func)
+ table.insert(t, 1, func)
+ core.callback_origins[func] = {
+ mod = core.get_current_modname() or "??",
+ name = debug.getinfo(1, "n").name or "??"
+ }
+ --local origin = core.callback_origins[func]
+ --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
+ end
return t, registerfunc
end
end
core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } }
+
function core.registered_on_player_hpchange(player, hp_change)
local last = false
for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do
end
return hp_change
end
+
function core.register_on_player_hpchange(func, modifier)
if modifier then
table.insert(core.registered_on_player_hpchanges.modifiers, func)
else
table.insert(core.registered_on_player_hpchanges.loggers, func)
end
+ core.callback_origins[func] = {
+ mod = core.get_current_modname() or "??",
+ name = debug.getinfo(1, "n").name or "??"
+ }
end
core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes")
* to gather a coherent backtrace. Realistically, the best we can do here is
* print which C function performed the failing pcall.
*/
-void script_error(lua_State *L, int pcall_result, const char *fxn)
+void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
{
if (pcall_result == 0)
return;
err_type = "Unknown";
}
+ if (!mod)
+ mod = "??";
+
+ if (!fxn)
+ fxn = "??";
+
const char *err_descr = lua_tostring(L, -1);
if (!err_descr)
err_descr = "<no description>";
- std::string err_msg(err_type);
- if (fxn) {
- err_msg += " error in ";
- err_msg += fxn;
- err_msg += "(): ";
- } else {
- err_msg += " error: ";
- }
+ char buf[256];
+ snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
+ err_type, mod, fxn);
+
+ std::string err_msg(buf);
err_msg += err_descr;
if (pcall_result == LUA_ERRMEM) {
int result = lua_pcall(L, nargs + 2, 1, errorhandler);
if (result != 0)
- script_error(L, result, fxn);
+ script_error(L, result, NULL, fxn);
lua_remove(L, -2); // Remove error handler
}
if (doerror) {
if (L != NULL) {
- script_error(L, LUA_ERRRUN, NULL);
+ script_error(L, LUA_ERRRUN, NULL, NULL);
} else {
FATAL_ERROR("Can't do a scripterror for this deprecated message, "
"so exit completely!");
#include "common/c_types.h"
-#define PCALL_RESL(L, RES) do { \
- int result_ = (RES); \
- if (result_ != 0) { \
- script_error((L), result_, __FUNCTION__); \
- } \
+#define PCALL_RESL(L, RES) do { \
+ int result_ = (RES); \
+ if (result_ != 0) { \
+ script_error((L), result_, NULL, __FUNCTION__); \
+ } \
} while (0)
#define script_run_callbacks(L, nargs, mode) \
std::string script_get_backtrace(lua_State *L);
int script_error_handler(lua_State *L);
int script_exception_wrapper(lua_State *L, lua_CFunction f);
-void script_error(lua_State *L, int pcall_result, const char *fxn);
+void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
void script_run_callbacks_f(lua_State *L, int nargs,
RunCallbacksMode mode, const char *fxn);
void log_deprecated(lua_State *L, const std::string &message);
#include "cpp_api/s_internal.h"
#include "cpp_api/s_security.h"
#include "lua_api/l_object.h"
+#include "common/c_converter.h"
#include "serverobject.h"
#include "debug.h"
#include "filesys.h"
ScriptApiBase::ScriptApiBase()
{
- #ifdef SCRIPTAPI_LOCK_DEBUG
+#ifdef SCRIPTAPI_LOCK_DEBUG
m_locked = false;
- #endif
+#endif
m_luastack = luaL_newstate();
FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");
return true;
}
+// Push the list of callbacks (a lua table).
+// Then push nargs arguments.
+// Then call this function, which
+// - runs the callbacks
+// - replaces the table and arguments with the return value,
+// computed depending on mode
+void ScriptApiBase::runCallbacksRaw(int nargs,
+ RunCallbacksMode mode, const char *fxn)
+{
+ lua_State *L = getStack();
+ FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
+
+ // Insert error handler
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L) - nargs - 1;
+ lua_insert(L, errorhandler);
+
+ // Insert run_callbacks between error handler and table
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "run_callbacks");
+ lua_remove(L, -2);
+ lua_insert(L, errorhandler + 1);
+
+ // Insert mode after table
+ lua_pushnumber(L, (int)mode);
+ lua_insert(L, errorhandler + 3);
+
+ // Stack now looks like this:
+ // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
+
+ int result = lua_pcall(L, nargs + 2, 1, errorhandler);
+ if (result != 0)
+ scriptError(result, fxn);
+
+ lua_remove(L, -2); // Remove error handler
+}
+
void ScriptApiBase::realityCheck()
{
int top = lua_gettop(m_luastack);
void ScriptApiBase::scriptError(int result, const char *fxn)
{
- script_error(getStack(), result, fxn);
+ script_error(getStack(), result, m_last_run_mod.c_str(), fxn);
}
void ScriptApiBase::stackDump(std::ostream &o)
o << std::endl;
}
+void ScriptApiBase::setOriginDirect(const char *origin)
+{
+ m_last_run_mod = origin ? origin : "??";
+}
+
+void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn)
+{
+#ifdef SCRIPTAPI_DEBUG
+ lua_State *L = getStack();
+
+ m_last_run_mod = lua_istable(L, index) ?
+ getstringfield_default(L, index, "mod_origin", "") : "";
+ //printf(">>>> running %s for mod: %s\n", fxn, m_last_run_mod.c_str());
+#endif
+}
+
void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
{
SCRIPTAPI_PRECHECKHEADER
#include "common/c_internal.h"
#define SCRIPTAPI_LOCK_DEBUG
+#define SCRIPTAPI_DEBUG
#define SCRIPT_MOD_NAME_FIELD "current_mod_name"
// MUST be an invalid mod name so that mods can't
} \
} while (0)
+#define runCallbacks(nargs, mode) \
+ runCallbacksRaw((nargs), (mode), __FUNCTION__)
+
+#define setOriginFromTable(index) \
+ setOriginFromTableRaw(index, __FUNCTION__)
+
class Server;
class Environment;
class GUIEngine;
std::string *error=NULL);
bool loadScript(const std::string &script_path, std::string *error=NULL);
+ void runCallbacksRaw(int nargs,
+ RunCallbacksMode mode, const char *fxn);
+
/* object */
void addObjectReference(ServerActiveObject *cobj);
void removeObjectReference(ServerActiveObject *cobj);
Server* getServer() { return m_server; }
+ std::string getOrigin() { return m_last_run_mod; }
+ void setOriginDirect(const char *origin);
+ void setOriginFromTableRaw(int index, const char *fxn);
+
protected:
friend class LuaABM;
friend class InvRef;
void objectrefGet(lua_State *L, u16 id);
JMutex m_luastackmutex;
+ std::string m_last_run_mod;
// Stack index of Lua error handler
int m_errorhandler;
bool m_secure;
lua_pushvalue(L, object); // self
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
lua_pushinteger(L, dtime_s);
- // Call with 3 arguments, 0 results
+
+ setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 3, 0, m_errorhandler));
} else {
lua_pop(L, 1);
lua_pop(L, 2); // Pop entity and get_staticdata
return "";
}
-
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
- // Call with 1 arguments, 1 results
+
+ setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
+
lua_remove(L, object); // Remove object
size_t len = 0;
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
lua_pushnumber(L, dtime); // dtime
- // Call with 2 arguments, 0 results
+
+ setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
+
lua_pop(L, 1); // Pop object
}
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
- // Call with 5 arguments, 0 results
+
+ setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
+
lua_pop(L, 1); // Pop object
}
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(L, clicker); // Clicker reference
- // Call with 2 arguments, 0 results
+
+ setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
+
lua_pop(L, 1); // Pop object
}
push_v3s16(L, minp);
push_v3s16(L, maxp);
lua_pushnumber(L, blockseed);
- script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiEnv::environment_Step(float dtime)
// Call callbacks
lua_pushnumber(L, dtime);
try {
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
} catch (LuaError &e) {
getServer()->setAsyncFatalError(e.what());
}
objectrefGetOrCreate(L, player); // player
lua_pushstring(L,type.c_str()); // event type
try {
- script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
} catch (LuaError &e) {
getServer()->setAsyncFatalError(e.what());
}
lua_pop(L, 1);
return false;
}
+
+ setOriginFromTable(-1);
+
lua_getfield(L, -1, callbackname);
lua_remove(L, -2);
// Should be a function or nil
lua_remove(L, -2);
luaL_checktype(L, -1, LUA_TTABLE);
}
+
+ setOriginFromTable(-1);
+
lua_getfield(L, -1, callbackname);
lua_remove(L, -2); // Remove item def
// Should be a function or nil
lua_getfield(L, -1, "registered_on_newplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
lua_getfield(L, -1, "registered_on_dieplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
lua_pushnumber(L, damage);
- script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
+ runCallbacks(6, RUN_CALLBACKS_MODE_OR);
return lua_toboolean(L, -1);
}
lua_getfield(L, -1, "registered_on_respawnplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR);
bool positioning_handled_by_some = lua_toboolean(L, -1);
return positioning_handled_by_some;
}
lua_getfield(L, -1, "registered_on_prejoinplayers");
lua_pushstring(L, name.c_str());
lua_pushstring(L, ip.c_str());
- script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
if (lua_isstring(L, -1)) {
reason->assign(lua_tostring(L, -1));
return true;
lua_getfield(L, -1, "registered_on_joinplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
lua_getfield(L, -1, "registered_on_leaveplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
- script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
lua_newtable(L);
lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
lua_setfield(L, -2, "type");
- script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
- script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
+ runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
}
ScriptApiPlayer::~ScriptApiPlayer()
lua_pop(L, 1);
lua_getfield(L, -1, "builtin_auth_handler");
}
+
+ setOriginFromTable(-1);
+
lua_remove(L, -2); // Remove core
if (lua_type(L, -1) != LUA_TTABLE)
throw LuaError("Authentication handler table not valid");
// Call callbacks
lua_pushstring(L, name.c_str());
lua_pushstring(L, message.c_str());
- script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
bool ate = lua_toboolean(L, -1);
return ate;
}
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_shutdown");
// Call callbacks
- script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
+ runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
}
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");
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
}
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)
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
// 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);