Move script internals to a single table

develop
lethosor 2015-03-10 16:53:32 -04:00
parent d0ba6d7019
commit e73a2f8778
1 changed files with 15 additions and 19 deletions

@ -386,15 +386,14 @@ end
local internal = dfhack.internal local internal = dfhack.internal
internal.scripts = internal.scripts or {} Script = defclass(Script)
internal.scriptPath = internal.scriptPath or {} function Script:init(path)
internal.scriptMtime = internal.scriptMtime or {} self.path = path
internal.scriptRun = internal.scriptRun or {} self.mtime = dfhack.filesystem.mtime(path)
end
internal.scripts = internal.scripts or {}
local scripts = internal.scripts local scripts = internal.scripts
local scriptPath = internal.scriptPath
local scriptMtime = internal.scriptMtime
local scriptRun = internal.scriptRun
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
@ -433,14 +432,11 @@ 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] if scripts[file] == nil then
scripts[file] = Script(file)
end
local env = scripts[file].env
if env == nil then if env == nil then
env = {} env = {}
setmetatable(env, { __index = base_env }) setmetatable(env, { __index = base_env })
@ -451,8 +447,8 @@ 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] and scriptRun[file] then if time == scripts[file].mtime and scripts[file].run then
f = scriptRun[file] f = scripts[file].run
else else
--reload --reload
f, perr = loadfile(file, 't', env) f, perr = loadfile(file, 't', env)
@ -460,10 +456,10 @@ function dfhack.run_script_with_env(envVars,name,...)
error(perr) error(perr)
end end
-- avoid updating mtime if the script failed to load -- avoid updating mtime if the script failed to load
scriptMtime[file] = time scripts[file].mtime = time
end end
scripts[file] = env scripts[file].env = env
scriptRun[file] = f scripts[file].run = f
return f(...), env return f(...), env
end end