Skip to content

Commit 2b04ab8

Browse files
committedAug 13, 2015
SAPI: Track last executed mod and include in error messages
1 parent 738fbc6 commit 2b04ab8

19 files changed

+231
-45
lines changed
 

Diff for: ‎builtin/game/auth.lua

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ function core.register_authentication_handler(handler)
171171
end
172172
core.registered_auth_handler = handler
173173
core.registered_auth_handler_modname = core.get_current_modname()
174+
handler.mod_origin = core.registered_auth_handler_modname
174175
end
175176

176177
function core.get_auth_handler()

Diff for: ‎builtin/game/chatcommands.lua

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function core.register_chatcommand(cmd, def)
1010
def.params = def.params or ""
1111
def.description = def.description or ""
1212
def.privs = def.privs or {}
13+
def.mod_origin = core.get_current_modname() or "??"
1314
core.chatcommands[cmd] = def
1415
end
1516

@@ -37,6 +38,7 @@ core.register_on_chat_message(function(name, message)
3738
end
3839
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
3940
if has_privs then
41+
core.set_last_run_mod(cmd_def.mod_origin)
4042
local success, message = cmd_def.func(name, param)
4143
if message then
4244
core.chat_send_player(name, message)

Diff for: ‎builtin/game/detached_inventory.lua

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function core.create_detached_inventory(name, callbacks)
1313
stuff.on_put = callbacks.on_put
1414
stuff.on_take = callbacks.on_take
1515
end
16+
stuff.mod_origin = core.get_current_modname() or "??"
1617
core.detached_inventories[name] = stuff
1718
return core.create_detached_inventory_raw(name)
1819
end

Diff for: ‎builtin/game/item.lua

+9
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ function core.node_dig(pos, node, digger)
479479
-- Run script hook
480480
local _, callback
481481
for _, callback in ipairs(core.registered_on_dignodes) do
482+
local origin = core.callback_origins[callback]
483+
if origin then
484+
core.set_last_run_mod(origin.mod)
485+
--print("Running " .. tostring(callback) ..
486+
-- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
487+
else
488+
--print("No data associated with callback")
489+
end
490+
482491
-- Copy pos and node because callback can modify them
483492
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
484493
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}

Diff for: ‎builtin/game/misc.lua

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local function update_timers(delay)
1414
local timer = timers[index]
1515
timer.time = timer.time - delay
1616
if timer.time <= 0 then
17+
core.set_last_run_mod(timer.mod_origin)
1718
timer.func(unpack(timer.args or {}))
1819
table.remove(timers, index)
1920
sub = sub + 1
@@ -55,12 +56,22 @@ function core.after(time, func, ...)
5556
"Invalid core.after invocation")
5657
if not mintime then
5758
mintime = time
58-
timers_to_add = {{time=time+delay, func=func, args={...}}}
59+
timers_to_add = {{
60+
time = time+delay,
61+
func = func,
62+
args = {...},
63+
mod_origin = core.get_last_run_mod(),
64+
}}
5965
return
6066
end
6167
mintime = math.min(mintime, time)
6268
timers_to_add = timers_to_add or {}
63-
timers_to_add[#timers_to_add+1] = {time=time+delay, func=func, args={...}}
69+
timers_to_add[#timers_to_add+1] = {
70+
time = time+delay,
71+
func = func,
72+
args = {...},
73+
mod_origin = core.get_last_run_mod(),
74+
}
6475
end
6576

6677
function core.check_player_privs(name, privs)

Diff for: ‎builtin/game/register.lua

+38-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ end
7272
function core.register_abm(spec)
7373
-- Add to core.registered_abms
7474
core.registered_abms[#core.registered_abms+1] = spec
75+
spec.mod_origin = core.get_current_modname() or "??"
7576
end
7677

7778
function core.register_entity(name, prototype)
@@ -86,6 +87,7 @@ function core.register_entity(name, prototype)
8687

8788
-- Add to core.registered_entities
8889
core.registered_entities[name] = prototype
90+
prototype.mod_origin = core.get_current_modname() or "??"
8991
end
9092

9193
function core.register_item(name, itemdef)
@@ -147,6 +149,8 @@ function core.register_item(name, itemdef)
147149
end
148150
-- END Legacy stuff
149151

152+
itemdef.mod_origin = core.get_current_modname() or "??"
153+
150154
-- Disable all further modifications
151155
getmetatable(itemdef).__newindex = {}
152156

@@ -326,6 +330,8 @@ function core.override_item(name, redefinition)
326330
end
327331

328332

333+
core.callback_origins = {}
334+
329335
function core.run_callbacks(callbacks, mode, ...)
330336
assert(type(callbacks) == "table")
331337
local cb_len = #callbacks
@@ -338,6 +344,14 @@ function core.run_callbacks(callbacks, mode, ...)
338344
end
339345
local ret = nil
340346
for i = 1, cb_len do
347+
local origin = core.callback_origins[callbacks[i]]
348+
if origin then
349+
core.set_last_run_mod(origin.mod)
350+
--print("Running " .. tostring(callbacks[i]) ..
351+
-- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
352+
else
353+
--print("No data associated with callback")
354+
end
341355
local cb_ret = callbacks[i](...)
342356

343357
if mode == 0 and i == 1 then
@@ -370,13 +384,29 @@ end
370384

371385
local function make_registration()
372386
local t = {}
373-
local registerfunc = function(func) table.insert(t, func) end
387+
local registerfunc = function(func)
388+
table.insert(t, func)
389+
core.callback_origins[func] = {
390+
mod = core.get_current_modname() or "??",
391+
name = debug.getinfo(1, "n").name or "??"
392+
}
393+
--local origin = core.callback_origins[func]
394+
--print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
395+
end
374396
return t, registerfunc
375397
end
376398

377399
local function make_registration_reverse()
378400
local t = {}
379-
local registerfunc = function(func) table.insert(t, 1, func) end
401+
local registerfunc = function(func)
402+
table.insert(t, 1, func)
403+
core.callback_origins[func] = {
404+
mod = core.get_current_modname() or "??",
405+
name = debug.getinfo(1, "n").name or "??"
406+
}
407+
--local origin = core.callback_origins[func]
408+
--print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
409+
end
380410
return t, registerfunc
381411
end
382412

@@ -408,6 +438,7 @@ local function make_registration_wrap(reg_fn_name, clear_fn_name)
408438
end
409439

410440
core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } }
441+
411442
function core.registered_on_player_hpchange(player, hp_change)
412443
local last = false
413444
for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do
@@ -427,12 +458,17 @@ function core.registered_on_player_hpchange(player, hp_change)
427458
end
428459
return hp_change
429460
end
461+
430462
function core.register_on_player_hpchange(func, modifier)
431463
if modifier then
432464
table.insert(core.registered_on_player_hpchanges.modifiers, func)
433465
else
434466
table.insert(core.registered_on_player_hpchanges.loggers, func)
435467
end
468+
core.callback_origins[func] = {
469+
mod = core.get_current_modname() or "??",
470+
name = debug.getinfo(1, "n").name or "??"
471+
}
436472
end
437473

438474
core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes")

Diff for: ‎src/script/common/c_internal.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f)
7979
* to gather a coherent backtrace. Realistically, the best we can do here is
8080
* print which C function performed the failing pcall.
8181
*/
82-
void script_error(lua_State *L, int pcall_result, const char *fxn)
82+
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
8383
{
8484
if (pcall_result == 0)
8585
return;
@@ -99,18 +99,21 @@ void script_error(lua_State *L, int pcall_result, const char *fxn)
9999
err_type = "Unknown";
100100
}
101101

102+
if (!mod)
103+
mod = "??";
104+
105+
if (!fxn)
106+
fxn = "??";
107+
102108
const char *err_descr = lua_tostring(L, -1);
103109
if (!err_descr)
104110
err_descr = "<no description>";
105111

106-
std::string err_msg(err_type);
107-
if (fxn) {
108-
err_msg += " error in ";
109-
err_msg += fxn;
110-
err_msg += "(): ";
111-
} else {
112-
err_msg += " error: ";
113-
}
112+
char buf[256];
113+
snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
114+
err_type, mod, fxn);
115+
116+
std::string err_msg(buf);
114117
err_msg += err_descr;
115118

116119
if (pcall_result == LUA_ERRMEM) {
@@ -152,7 +155,7 @@ void script_run_callbacks_f(lua_State *L, int nargs,
152155

153156
int result = lua_pcall(L, nargs + 2, 1, errorhandler);
154157
if (result != 0)
155-
script_error(L, result, fxn);
158+
script_error(L, result, NULL, fxn);
156159

157160
lua_remove(L, -2); // Remove error handler
158161
}
@@ -176,7 +179,7 @@ void log_deprecated(lua_State *L, const std::string &message)
176179

177180
if (doerror) {
178181
if (L != NULL) {
179-
script_error(L, LUA_ERRRUN, NULL);
182+
script_error(L, LUA_ERRRUN, NULL, NULL);
180183
} else {
181184
FATAL_ERROR("Can't do a scripterror for this deprecated message, "
182185
"so exit completely!");

Diff for: ‎src/script/common/c_internal.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ extern "C" {
3434

3535
#include "common/c_types.h"
3636

37-
#define PCALL_RESL(L, RES) do { \
38-
int result_ = (RES); \
39-
if (result_ != 0) { \
40-
script_error((L), result_, __FUNCTION__); \
41-
} \
37+
#define PCALL_RESL(L, RES) do { \
38+
int result_ = (RES); \
39+
if (result_ != 0) { \
40+
script_error((L), result_, NULL, __FUNCTION__); \
41+
} \
4242
} while (0)
4343

4444
#define script_run_callbacks(L, nargs, mode) \
@@ -77,7 +77,7 @@ enum RunCallbacksMode
7777
std::string script_get_backtrace(lua_State *L);
7878
int script_error_handler(lua_State *L);
7979
int script_exception_wrapper(lua_State *L, lua_CFunction f);
80-
void script_error(lua_State *L, int pcall_result, const char *fxn);
80+
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
8181
void script_run_callbacks_f(lua_State *L, int nargs,
8282
RunCallbacksMode mode, const char *fxn);
8383
void log_deprecated(lua_State *L, const std::string &message);

Diff for: ‎src/script/cpp_api/s_base.cpp

+57-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include "cpp_api/s_internal.h"
2222
#include "cpp_api/s_security.h"
2323
#include "lua_api/l_object.h"
24+
#include "common/c_converter.h"
2425
#include "serverobject.h"
2526
#include "debug.h"
2627
#include "filesys.h"
@@ -68,9 +69,9 @@ class ModNameStorer
6869

6970
ScriptApiBase::ScriptApiBase()
7071
{
71-
#ifdef SCRIPTAPI_LOCK_DEBUG
72+
#ifdef SCRIPTAPI_LOCK_DEBUG
7273
m_locked = false;
73-
#endif
74+
#endif
7475

7576
m_luastack = luaL_newstate();
7677
FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");
@@ -154,6 +155,43 @@ bool ScriptApiBase::loadScript(const std::string &script_path, std::string *erro
154155
return true;
155156
}
156157

158+
// Push the list of callbacks (a lua table).
159+
// Then push nargs arguments.
160+
// Then call this function, which
161+
// - runs the callbacks
162+
// - replaces the table and arguments with the return value,
163+
// computed depending on mode
164+
void ScriptApiBase::runCallbacksRaw(int nargs,
165+
RunCallbacksMode mode, const char *fxn)
166+
{
167+
lua_State *L = getStack();
168+
FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
169+
170+
// Insert error handler
171+
lua_pushcfunction(L, script_error_handler);
172+
int errorhandler = lua_gettop(L) - nargs - 1;
173+
lua_insert(L, errorhandler);
174+
175+
// Insert run_callbacks between error handler and table
176+
lua_getglobal(L, "core");
177+
lua_getfield(L, -1, "run_callbacks");
178+
lua_remove(L, -2);
179+
lua_insert(L, errorhandler + 1);
180+
181+
// Insert mode after table
182+
lua_pushnumber(L, (int)mode);
183+
lua_insert(L, errorhandler + 3);
184+
185+
// Stack now looks like this:
186+
// ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
187+
188+
int result = lua_pcall(L, nargs + 2, 1, errorhandler);
189+
if (result != 0)
190+
scriptError(result, fxn);
191+
192+
lua_remove(L, -2); // Remove error handler
193+
}
194+
157195
void ScriptApiBase::realityCheck()
158196
{
159197
int top = lua_gettop(m_luastack);
@@ -167,7 +205,7 @@ void ScriptApiBase::realityCheck()
167205

168206
void ScriptApiBase::scriptError(int result, const char *fxn)
169207
{
170-
script_error(getStack(), result, fxn);
208+
script_error(getStack(), result, m_last_run_mod.c_str(), fxn);
171209
}
172210

173211
void ScriptApiBase::stackDump(std::ostream &o)
@@ -197,6 +235,22 @@ void ScriptApiBase::stackDump(std::ostream &o)
197235
o << std::endl;
198236
}
199237

238+
void ScriptApiBase::setOriginDirect(const char *origin)
239+
{
240+
m_last_run_mod = origin ? origin : "??";
241+
}
242+
243+
void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn)
244+
{
245+
#ifdef SCRIPTAPI_DEBUG
246+
lua_State *L = getStack();
247+
248+
m_last_run_mod = lua_istable(L, index) ?
249+
getstringfield_default(L, index, "mod_origin", "") : "";
250+
//printf(">>>> running %s for mod: %s\n", fxn, m_last_run_mod.c_str());
251+
#endif
252+
}
253+
200254
void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
201255
{
202256
SCRIPTAPI_PRECHECKHEADER

Diff for: ‎src/script/cpp_api/s_base.h

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
#include "common/c_internal.h"
3535

3636
#define SCRIPTAPI_LOCK_DEBUG
37+
#define SCRIPTAPI_DEBUG
3738

3839
#define SCRIPT_MOD_NAME_FIELD "current_mod_name"
3940
// MUST be an invalid mod name so that mods can't
@@ -47,6 +48,12 @@ extern "C" {
4748
} \
4849
} while (0)
4950

51+
#define runCallbacks(nargs, mode) \
52+
runCallbacksRaw((nargs), (mode), __FUNCTION__)
53+
54+
#define setOriginFromTable(index) \
55+
setOriginFromTableRaw(index, __FUNCTION__)
56+
5057
class Server;
5158
class Environment;
5259
class GUIEngine;
@@ -61,12 +68,19 @@ class ScriptApiBase {
6168
std::string *error=NULL);
6269
bool loadScript(const std::string &script_path, std::string *error=NULL);
6370

71+
void runCallbacksRaw(int nargs,
72+
RunCallbacksMode mode, const char *fxn);
73+
6474
/* object */
6575
void addObjectReference(ServerActiveObject *cobj);
6676
void removeObjectReference(ServerActiveObject *cobj);
6777

6878
Server* getServer() { return m_server; }
6979

80+
std::string getOrigin() { return m_last_run_mod; }
81+
void setOriginDirect(const char *origin);
82+
void setOriginFromTableRaw(int index, const char *fxn);
83+
7084
protected:
7185
friend class LuaABM;
7286
friend class InvRef;
@@ -95,6 +109,7 @@ class ScriptApiBase {
95109
void objectrefGet(lua_State *L, u16 id);
96110

97111
JMutex m_luastackmutex;
112+
std::string m_last_run_mod;
98113
// Stack index of Lua error handler
99114
int m_errorhandler;
100115
bool m_secure;

Diff for: ‎src/script/cpp_api/s_entity.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
9191
lua_pushvalue(L, object); // self
9292
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
9393
lua_pushinteger(L, dtime_s);
94-
// Call with 3 arguments, 0 results
94+
95+
setOriginFromTable(object);
9596
PCALL_RES(lua_pcall(L, 3, 0, m_errorhandler));
9697
} else {
9798
lua_pop(L, 1);
@@ -135,11 +136,12 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
135136
lua_pop(L, 2); // Pop entity and get_staticdata
136137
return "";
137138
}
138-
139139
luaL_checktype(L, -1, LUA_TFUNCTION);
140140
lua_pushvalue(L, object); // self
141-
// Call with 1 arguments, 1 results
141+
142+
setOriginFromTable(object);
142143
PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
144+
143145
lua_remove(L, object); // Remove object
144146

145147
size_t len = 0;
@@ -207,8 +209,10 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
207209
luaL_checktype(L, -1, LUA_TFUNCTION);
208210
lua_pushvalue(L, object); // self
209211
lua_pushnumber(L, dtime); // dtime
210-
// Call with 2 arguments, 0 results
212+
213+
setOriginFromTable(object);
211214
PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
215+
212216
lua_pop(L, 1); // Pop object
213217
}
214218

@@ -238,8 +242,10 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
238242
lua_pushnumber(L, time_from_last_punch);
239243
push_tool_capabilities(L, *toolcap);
240244
push_v3f(L, dir);
241-
// Call with 5 arguments, 0 results
245+
246+
setOriginFromTable(object);
242247
PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
248+
243249
lua_pop(L, 1); // Pop object
244250
}
245251

@@ -264,8 +270,10 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
264270
luaL_checktype(L, -1, LUA_TFUNCTION);
265271
lua_pushvalue(L, object); // self
266272
objectrefGetOrCreate(L, clicker); // Clicker reference
267-
// Call with 2 arguments, 0 results
273+
274+
setOriginFromTable(object);
268275
PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
276+
269277
lua_pop(L, 1); // Pop object
270278
}
271279

Diff for: ‎src/script/cpp_api/s_env.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
3838
push_v3s16(L, minp);
3939
push_v3s16(L, maxp);
4040
lua_pushnumber(L, blockseed);
41-
script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
41+
runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
4242
}
4343

4444
void ScriptApiEnv::environment_Step(float dtime)
@@ -52,7 +52,7 @@ void ScriptApiEnv::environment_Step(float dtime)
5252
// Call callbacks
5353
lua_pushnumber(L, dtime);
5454
try {
55-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
55+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
5656
} catch (LuaError &e) {
5757
getServer()->setAsyncFatalError(e.what());
5858
}
@@ -73,7 +73,7 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
7373
objectrefGetOrCreate(L, player); // player
7474
lua_pushstring(L,type.c_str()); // event type
7575
try {
76-
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
76+
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
7777
} catch (LuaError &e) {
7878
getServer()->setAsyncFatalError(e.what());
7979
}

Diff for: ‎src/script/cpp_api/s_inventory.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
209209
lua_pop(L, 1);
210210
return false;
211211
}
212+
213+
setOriginFromTable(-1);
214+
212215
lua_getfield(L, -1, callbackname);
213216
lua_remove(L, -2);
214217
// Should be a function or nil

Diff for: ‎src/script/cpp_api/s_item.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
193193
lua_remove(L, -2);
194194
luaL_checktype(L, -1, LUA_TTABLE);
195195
}
196+
197+
setOriginFromTable(-1);
198+
196199
lua_getfield(L, -1, callbackname);
197200
lua_remove(L, -2); // Remove item def
198201
// Should be a function or nil

Diff for: ‎src/script/cpp_api/s_player.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
3232
lua_getfield(L, -1, "registered_on_newplayers");
3333
// Call callbacks
3434
objectrefGetOrCreate(L, player);
35-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
35+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
3636
}
3737

3838
void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
@@ -44,7 +44,7 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
4444
lua_getfield(L, -1, "registered_on_dieplayers");
4545
// Call callbacks
4646
objectrefGetOrCreate(L, player);
47-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
47+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
4848
}
4949

5050
bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
@@ -65,7 +65,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
6565
push_tool_capabilities(L, *toolcap);
6666
push_v3f(L, dir);
6767
lua_pushnumber(L, damage);
68-
script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
68+
runCallbacks(6, RUN_CALLBACKS_MODE_OR);
6969
return lua_toboolean(L, -1);
7070
}
7171

@@ -96,7 +96,7 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
9696
lua_getfield(L, -1, "registered_on_respawnplayers");
9797
// Call callbacks
9898
objectrefGetOrCreate(L, player);
99-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
99+
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
100100
bool positioning_handled_by_some = lua_toboolean(L, -1);
101101
return positioning_handled_by_some;
102102
}
@@ -113,7 +113,7 @@ bool ScriptApiPlayer::on_prejoinplayer(
113113
lua_getfield(L, -1, "registered_on_prejoinplayers");
114114
lua_pushstring(L, name.c_str());
115115
lua_pushstring(L, ip.c_str());
116-
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
116+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
117117
if (lua_isstring(L, -1)) {
118118
reason->assign(lua_tostring(L, -1));
119119
return true;
@@ -130,7 +130,7 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
130130
lua_getfield(L, -1, "registered_on_joinplayers");
131131
// Call callbacks
132132
objectrefGetOrCreate(L, player);
133-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
133+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
134134
}
135135

136136
void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
@@ -142,7 +142,7 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
142142
lua_getfield(L, -1, "registered_on_leaveplayers");
143143
// Call callbacks
144144
objectrefGetOrCreate(L, player);
145-
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
145+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
146146
}
147147

148148
void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
@@ -158,7 +158,7 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
158158
lua_newtable(L);
159159
lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
160160
lua_setfield(L, -2, "type");
161-
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
161+
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
162162
}
163163

164164
void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
@@ -185,7 +185,7 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
185185
lua_pushlstring(L, value.c_str(), value.size());
186186
lua_settable(L, -3);
187187
}
188-
script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
188+
runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
189189
}
190190

191191
ScriptApiPlayer::~ScriptApiPlayer()

Diff for: ‎src/script/cpp_api/s_server.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ void ScriptApiServer::getAuthHandler()
6767
lua_pop(L, 1);
6868
lua_getfield(L, -1, "builtin_auth_handler");
6969
}
70+
71+
setOriginFromTable(-1);
72+
7073
lua_remove(L, -2); // Remove core
7174
if (lua_type(L, -1) != LUA_TTABLE)
7275
throw LuaError("Authentication handler table not valid");
@@ -133,7 +136,7 @@ bool ScriptApiServer::on_chat_message(const std::string &name,
133136
// Call callbacks
134137
lua_pushstring(L, name.c_str());
135138
lua_pushstring(L, message.c_str());
136-
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
139+
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
137140
bool ate = lua_toboolean(L, -1);
138141
return ate;
139142
}
@@ -146,6 +149,6 @@ void ScriptApiServer::on_shutdown()
146149
lua_getglobal(L, "core");
147150
lua_getfield(L, -1, "registered_on_shutdown");
148151
// Call callbacks
149-
script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
152+
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
150153
}
151154

Diff for: ‎src/script/lua_api/l_env.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
6868
FATAL_ERROR("");
6969
lua_remove(L, -2); // Remove registered_abms
7070

71+
scriptIface->setOriginFromTable(-1);
72+
7173
// Call action
7274
luaL_checktype(L, -1, LUA_TTABLE);
7375
lua_getfield(L, -1, "action");
@@ -78,7 +80,9 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
7880
lua_pushnumber(L, active_object_count);
7981
lua_pushnumber(L, active_object_count_wider);
8082

81-
PCALL_RESL(L, lua_pcall(L, 4, 0, errorhandler));
83+
int result = lua_pcall(L, 4, 0, errorhandler);
84+
if (result)
85+
scriptIface->scriptError(result, "LuaABM::trigger");
8286

8387
lua_pop(L, 1); // Pop error handler
8488
}

Diff for: ‎src/script/lua_api/l_server.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,31 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
438438
return 0;
439439
}
440440

441+
// get_last_run_mod()
442+
int ModApiServer::l_get_last_run_mod(lua_State *L)
443+
{
444+
NO_MAP_LOCK_REQUIRED;
445+
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
446+
const char *current_mod = lua_tostring(L, -1);
447+
if (current_mod == NULL || current_mod[0] == '\0') {
448+
lua_pop(L, 1);
449+
lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
450+
}
451+
return 1;
452+
}
453+
454+
// set_last_run_mod(modname)
455+
int ModApiServer::l_set_last_run_mod(lua_State *L)
456+
{
457+
NO_MAP_LOCK_REQUIRED;
458+
#ifdef SCRIPTAPI_DEBUG
459+
const char *mod = lua_tostring(L, 1);
460+
getScriptApiBase(L)->setOriginDirect(mod);
461+
//printf(">>>> last mod set from Lua: %s\n", mod);
462+
#endif
463+
return 0;
464+
}
465+
441466
#ifndef NDEBUG
442467
// cause_error(type_of_error)
443468
int ModApiServer::l_cause_error(lua_State *L)
@@ -495,6 +520,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
495520
API_FCT(unban_player_or_ip);
496521
API_FCT(notify_authentication_modified);
497522

523+
API_FCT(get_last_run_mod);
524+
API_FCT(set_last_run_mod);
498525
#ifndef NDEBUG
499526
API_FCT(cause_error);
500527
#endif

Diff for: ‎src/script/lua_api/l_server.h

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ class ModApiServer : public ModApiBase {
8888
// notify_authentication_modified(name)
8989
static int l_notify_authentication_modified(lua_State *L);
9090

91+
// get_last_run_mod()
92+
static int l_get_last_run_mod(lua_State *L);
93+
94+
// set_last_run_mod(modname)
95+
static int l_set_last_run_mod(lua_State *L);
96+
9197
#ifndef NDEBUG
9298
// cause_error(type_of_error)
9399
static int l_cause_error(lua_State *L);

0 commit comments

Comments
 (0)
Please sign in to comment.