65 lines
1.4 KiB
Lua
Raw Normal View History

2014-12-22 14:14:46 +01:00
local load_time_start = os.clock()
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, err = loadstring(code)
if not func then -- Syntax error
return err
end
local good, err = pcall(func)
if not good then -- Runtime error
return err
end
end
2014-12-22 14:14:46 +01:00
-- path of the file
local path = minetest.get_worldpath().."/tmp.lua"
-- the file becomes checked every <step> seconds
2015-09-27 16:03:15 +02:00
local step = 4
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
local text = file:read("*all")
io.close(file)
if text == "" then
return
end
2015-09-27 16:03:15 +02:00
-- reset it
file = io.open(path, "w")
file:write("")
io.close(file)
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 timer = 0
minetest.register_globalstep(function(dtime)
timer = timer+dtime
if timer < step then
return
end
timer = 0
run_stuff()
end)
minetest.log("info", string.format("[outgame_intervention] loaded after ca. %.2fs", os.clock() - load_time_start))