Only reload and recompile Lua scripts if they have moved or been updated.

develop
expwnent 2015-02-02 01:46:01 -05:00
parent 0f01583972
commit 712892fb71
1 changed files with 31 additions and 5 deletions

@ -381,8 +381,13 @@ end
local internal = dfhack.internal local internal = dfhack.internal
internal.scripts = internal.scripts or {} internal.scripts = internal.scripts or {}
internal.scriptPath = internal.scriptPath or {}
internal.scriptMtime = internal.scriptMtime or {}
local scripts = internal.scripts local scripts = internal.scripts
local scriptPath = internal.scriptPath
local scriptMtime = internal.scriptMtime
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
function dfhack.findScript(name) function dfhack.findScript(name)
@ -420,6 +425,13 @@ function dfhack.run_script_with_env(envVars,name,...)
if not file then if not file then
error('Could not find script ' .. name) error('Could not find script ' .. name)
end end
if scriptPath[name] and scriptPath[name] ~= file then
--new file path: must have loaded a different save or unloaded
scriptPath[name] = file
scriptMtime[file] = dfhack.filesystem.mtime(file)
--it is the responsibility of the script to clear its own data on unload so it's safe for us to not delete it here
end
local env = scripts[file] local env = scripts[file]
if env == nil then if env == nil then
env = {} env = {}
@ -428,12 +440,26 @@ function dfhack.run_script_with_env(envVars,name,...)
for x,y in pairs(envVars or {}) do for x,y in pairs(envVars or {}) do
env[x] = y env[x] = y
end end
local f,perr = loadfile(file, 't', env) local f
if f then local perr
scripts[file] = env local time = dfhack.filesystem.mtime(file)
return f(...), env if time == scriptMtime[file] then
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)
if not f then
error(perr)
end
end end
error(perr) scripts[file] = env
env.runScript = f
return f(...), env
end end
local function _run_command(...) local function _run_command(...)