From 3120d9e8e2e69ff2c1625b8cd6a435d2e02fae1a Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 11 Aug 2015 22:27:54 -0400 Subject: [PATCH] SAPI: Track last executed mod and include in error messages --- builtin/game/auth.lua | 1 + builtin/game/chatcommands.lua | 2 ++ builtin/game/detached_inventory.lua | 1 + builtin/game/item.lua | 9 +++++++ builtin/game/misc.lua | 15 +++++++++-- builtin/game/register.lua | 40 +++++++++++++++++++++++++++-- 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/builtin/game/auth.lua b/builtin/game/auth.lua index 93b0099..423eb31 100644 --- a/builtin/game/auth.lua +++ b/builtin/game/auth.lua @@ -171,6 +171,7 @@ function core.register_authentication_handler(handler) 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() diff --git a/builtin/game/chatcommands.lua b/builtin/game/chatcommands.lua index 6c8ca86..5d317de 100644 --- a/builtin/game/chatcommands.lua +++ b/builtin/game/chatcommands.lua @@ -10,6 +10,7 @@ function core.register_chatcommand(cmd, def) 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 @@ -37,6 +38,7 @@ core.register_on_chat_message(function(name, message) 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) diff --git a/builtin/game/detached_inventory.lua b/builtin/game/detached_inventory.lua index e8f03b5..b5d106b 100644 --- a/builtin/game/detached_inventory.lua +++ b/builtin/game/detached_inventory.lua @@ -13,6 +13,7 @@ function core.create_detached_inventory(name, callbacks) 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 diff --git a/builtin/game/item.lua b/builtin/game/item.lua index d25f4ef..6628a40 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -479,6 +479,15 @@ function core.node_dig(pos, node, digger) -- 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} diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 7fa9574..e3b7d82 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -14,6 +14,7 @@ local function update_timers(delay) 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 @@ -55,12 +56,22 @@ function core.after(time, func, ...) "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) diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 3a13abf..d0e04bf 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -72,6 +72,7 @@ end 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) @@ -86,6 +87,7 @@ 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) @@ -147,6 +149,8 @@ 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 = {} @@ -326,6 +330,8 @@ function core.override_item(name, redefinition) end +core.callback_origins = {} + function core.run_callbacks(callbacks, mode, ...) assert(type(callbacks) == "table") local cb_len = #callbacks @@ -338,6 +344,14 @@ function core.run_callbacks(callbacks, mode, ...) 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 @@ -370,13 +384,29 @@ end 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 @@ -408,6 +438,7 @@ local function make_registration_wrap(reg_fn_name, clear_fn_name) 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 @@ -427,12 +458,17 @@ function core.registered_on_player_hpchange(player, hp_change) 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")