Restore lua environment persistence and update modified timestamp correctly

See #544
develop
lethosor 2015-02-10 16:36:08 -05:00
parent e3b67b2985
commit 337598e0d6
1 changed files with 6 additions and 9 deletions

@ -444,21 +444,18 @@ function dfhack.run_script_with_env(envVars,name,...)
local perr
local time = dfhack.filesystem.mtime(file)
if time == scriptMtime[file] then
f = scripts[file].runScript
f = scripts[file].__runScript__
else
env = {}
setmetatable(env, { __index = base_env })
for x,y in pairs(envVars or {}) do
env[x] = y
end
--reload
f,perr = loadfile(file, 't', env)
f, perr = loadfile(file, 't', env)
if not f then
error(perr)
error(perr)
end
-- avoid updating mtime if the script failed to load
scriptMtime[file] = time
end
scripts[file] = env
env.runScript = f
env.__runScript__ = f
return f(...), env
end