SAPI: Track last executed mod and include in error messages

This commit is contained in:
kwolekr 2015-08-11 22:27:54 -04:00 committed by Nils Dagsson Moskopp
parent 777a203e5f
commit 3120d9e8e2
Signed by: erle
GPG Key ID: A3BC671C35191080
6 changed files with 64 additions and 4 deletions

View File

@ -171,6 +171,7 @@ function core.register_authentication_handler(handler)
end end
core.registered_auth_handler = handler core.registered_auth_handler = handler
core.registered_auth_handler_modname = core.get_current_modname() core.registered_auth_handler_modname = core.get_current_modname()
handler.mod_origin = core.registered_auth_handler_modname
end end
function core.get_auth_handler() function core.get_auth_handler()

View File

@ -10,6 +10,7 @@ function core.register_chatcommand(cmd, def)
def.params = def.params or "" def.params = def.params or ""
def.description = def.description or "" def.description = def.description or ""
def.privs = def.privs or {} def.privs = def.privs or {}
def.mod_origin = core.get_current_modname() or "??"
core.chatcommands[cmd] = def core.chatcommands[cmd] = def
end end
@ -37,6 +38,7 @@ core.register_on_chat_message(function(name, message)
end end
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs) local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
if has_privs then if has_privs then
core.set_last_run_mod(cmd_def.mod_origin)
local success, message = cmd_def.func(name, param) local success, message = cmd_def.func(name, param)
if message then if message then
core.chat_send_player(name, message) core.chat_send_player(name, message)

View File

@ -13,6 +13,7 @@ function core.create_detached_inventory(name, callbacks)
stuff.on_put = callbacks.on_put stuff.on_put = callbacks.on_put
stuff.on_take = callbacks.on_take stuff.on_take = callbacks.on_take
end end
stuff.mod_origin = core.get_current_modname() or "??"
core.detached_inventories[name] = stuff core.detached_inventories[name] = stuff
return core.create_detached_inventory_raw(name) return core.create_detached_inventory_raw(name)
end end

View File

@ -479,6 +479,15 @@ function core.node_dig(pos, node, digger)
-- Run script hook -- Run script hook
local _, callback local _, callback
for _, callback in ipairs(core.registered_on_dignodes) do 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 -- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z} 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 node_copy = {name=node.name, param1=node.param1, param2=node.param2}

View File

@ -14,6 +14,7 @@ local function update_timers(delay)
local timer = timers[index] local timer = timers[index]
timer.time = timer.time - delay timer.time = timer.time - delay
if timer.time <= 0 then if timer.time <= 0 then
core.set_last_run_mod(timer.mod_origin)
timer.func(unpack(timer.args or {})) timer.func(unpack(timer.args or {}))
table.remove(timers, index) table.remove(timers, index)
sub = sub + 1 sub = sub + 1
@ -55,12 +56,22 @@ function core.after(time, func, ...)
"Invalid core.after invocation") "Invalid core.after invocation")
if not mintime then if not mintime then
mintime = time 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 return
end end
mintime = math.min(mintime, time) mintime = math.min(mintime, time)
timers_to_add = timers_to_add or {} 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 end
function core.check_player_privs(name, privs) function core.check_player_privs(name, privs)

View File

@ -72,6 +72,7 @@ end
function core.register_abm(spec) function core.register_abm(spec)
-- Add to core.registered_abms -- Add to core.registered_abms
core.registered_abms[#core.registered_abms+1] = spec core.registered_abms[#core.registered_abms+1] = spec
spec.mod_origin = core.get_current_modname() or "??"
end end
function core.register_entity(name, prototype) function core.register_entity(name, prototype)
@ -86,6 +87,7 @@ function core.register_entity(name, prototype)
-- Add to core.registered_entities -- Add to core.registered_entities
core.registered_entities[name] = prototype core.registered_entities[name] = prototype
prototype.mod_origin = core.get_current_modname() or "??"
end end
function core.register_item(name, itemdef) function core.register_item(name, itemdef)
@ -147,6 +149,8 @@ function core.register_item(name, itemdef)
end end
-- END Legacy stuff -- END Legacy stuff
itemdef.mod_origin = core.get_current_modname() or "??"
-- Disable all further modifications -- Disable all further modifications
getmetatable(itemdef).__newindex = {} getmetatable(itemdef).__newindex = {}
@ -326,6 +330,8 @@ function core.override_item(name, redefinition)
end end
core.callback_origins = {}
function core.run_callbacks(callbacks, mode, ...) function core.run_callbacks(callbacks, mode, ...)
assert(type(callbacks) == "table") assert(type(callbacks) == "table")
local cb_len = #callbacks local cb_len = #callbacks
@ -338,6 +344,14 @@ function core.run_callbacks(callbacks, mode, ...)
end end
local ret = nil local ret = nil
for i = 1, cb_len do 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](...) local cb_ret = callbacks[i](...)
if mode == 0 and i == 1 then if mode == 0 and i == 1 then
@ -370,13 +384,29 @@ end
local function make_registration() local function make_registration()
local t = {} 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 return t, registerfunc
end end
local function make_registration_reverse() local function make_registration_reverse()
local t = {} 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 return t, registerfunc
end end
@ -408,6 +438,7 @@ local function make_registration_wrap(reg_fn_name, clear_fn_name)
end end
core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } } core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } }
function core.registered_on_player_hpchange(player, hp_change) function core.registered_on_player_hpchange(player, hp_change)
local last = false local last = false
for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do 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 end
return hp_change return hp_change
end end
function core.register_on_player_hpchange(func, modifier) function core.register_on_player_hpchange(func, modifier)
if modifier then if modifier then
table.insert(core.registered_on_player_hpchanges.modifiers, func) table.insert(core.registered_on_player_hpchanges.modifiers, func)
else else
table.insert(core.registered_on_player_hpchanges.loggers, func) table.insert(core.registered_on_player_hpchanges.loggers, func)
end end
core.callback_origins[func] = {
mod = core.get_current_modname() or "??",
name = debug.getinfo(1, "n").name or "??"
}
end end
core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes") core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes")