diff --git a/minetest.lua b/minetest.lua index d88075f..8c6d268 100644 --- a/minetest.lua +++ b/minetest.lua @@ -1,4 +1,42 @@ --- MT extension +function override(function_name, function_builder) + local func = minetest[function_name] + minetest["original_" .. function_name] = func + minetest[function_name] = function_builder(func) +end + +-- TODO fix modlib.minetest.get_gametime() messing up responsible "mod" determined by engine on crash +get_gametime = minetest.get_gametime +local get_gametime_initialized +local function get_gametime_init(dtime) + if get_gametime_initialized then + -- if the profiler is being used, the globalstep can't be unregistered + return + end + get_gametime_initialized = true + assert(dtime == 0) + local gametime = minetest.get_gametime() + assert(gametime) + function modlib.minetest.get_gametime() + local imprecise_gametime = minetest.get_gametime() + if imprecise_gametime > gametime then + minetest.log("warning", "modlib.minetest.get_gametime(): Called after increment and before first globalstep") + return imprecise_gametime + end + return gametime + end + for index, globalstep in pairs(minetest.registered_globalsteps) do + if globalstep == get_gametime_init then + table.remove(minetest.registered_globalsteps, index) + break + end + end + -- globalsteps of mods which depend on modlib will execute after this + minetest.register_globalstep(function(dtime) + gametime = gametime + dtime + end) +end +minetest.register_globalstep(get_gametime_init) + delta_times={} delays={} callbacks={}