Merge pull request #547 from lethosor/fix-lua-env-persistence

Fix lua environment persistence
develop
Lethosor 2015-02-11 14:54:35 -05:00
commit 4c2354280c
1 changed files with 9 additions and 10 deletions

@ -383,10 +383,12 @@ local internal = dfhack.internal
internal.scripts = internal.scripts or {} internal.scripts = internal.scripts or {}
internal.scriptPath = internal.scriptPath or {} internal.scriptPath = internal.scriptPath or {}
internal.scriptMtime = internal.scriptMtime or {} internal.scriptMtime = internal.scriptMtime or {}
internal.scriptRun = internal.scriptRun or {}
local scripts = internal.scripts local scripts = internal.scripts
local scriptPath = internal.scriptPath local scriptPath = internal.scriptPath
local scriptMtime = internal.scriptMtime local scriptMtime = internal.scriptMtime
local scriptRun = internal.scriptRun
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
@ -443,22 +445,19 @@ function dfhack.run_script_with_env(envVars,name,...)
local f local f
local perr local perr
local time = dfhack.filesystem.mtime(file) local time = dfhack.filesystem.mtime(file)
if time == scriptMtime[file] then if time == scriptMtime[file] and scriptRun[file] then
f = scripts[file].runScript f = scriptRun[file]
else else
env = {}
setmetatable(env, { __index = base_env })
for x,y in pairs(envVars or {}) do
env[x] = y
end
--reload --reload
f,perr = loadfile(file, 't', env) f, perr = loadfile(file, 't', env)
if not f then if not f then
error(perr) error(perr)
end end
-- avoid updating mtime if the script failed to load
scriptMtime[file] = time
end end
scripts[file] = env scripts[file] = env
env.runScript = f scriptRun[file] = f
return f(...), env return f(...), env
end end