74 lines
1.5 KiB
Lua
Raw Normal View History

local load_time_start = minetest.get_us_time()
-- path of the file
local path = minetest.get_worldpath().."/tmp.lua"
-- the file becomes checked every <step> seconds
local step = 4
2014-12-22 14:14:46 +01:00
2015-09-22 17:17:12 +02:00
-- copied from worldedit/worldedit/code.lua
--- Executes `code` as a Lua chunk in the global namespace.
-- @return An error message if the code fails, or nil on success.
local function run_lua_text(code)
local func, syntax_err = loadstring(code)
if not func then
return syntax_err
2015-09-22 17:17:12 +02:00
end
local good, runtime_err = pcall(func)
if not good then
return runtime_err
2015-09-22 17:17:12 +02:00
end
end
2014-12-22 14:14:46 +01:00
local function run_stuff()
-- search file
local file = io.open(path, "r")
if not file then
return
end
-- test if it contains something
if file:seek("end") == 0 then
file:close()
2014-12-22 14:14:46 +01:00
return
end
-- get the text
file:seek("set")
local text = file:read("*all")
file:close()
2015-09-27 16:03:15 +02:00
-- reset it
file = io.open(path, "w")
file:write("")
file:close()
2015-09-27 16:03:15 +02:00
2014-12-22 14:14:46 +01:00
-- run it
2015-09-22 17:17:12 +02:00
local err = run_lua_text(text)
if err then
minetest.log("action", "[outgame_intervention] error executing file: "..err)
return
end
2014-12-22 14:14:46 +01:00
-- inform that it worked
2015-09-22 17:17:12 +02:00
minetest.log("info", "[outgame_intervention] file successfully executed.")
2014-12-22 14:14:46 +01:00
return true
end
local function do_step()
2014-12-22 14:14:46 +01:00
run_stuff()
minetest.after(step, do_step)
end
minetest.after(step, do_step)
2014-12-22 14:14:46 +01:00
local time = (minetest.get_us_time() - load_time_start) / 1000000
local msg = "[outgame_intervention] loaded after ca. " .. time .. " seconds."
if time > 0.01 then
print(msg)
else
minetest.log("info", msg)
end