Allowed Lua scripts to share variables with run_script and corrected dfhack.findScript so that it can find save-specific scripts.

develop
expwnent 2015-01-26 21:45:31 -05:00
parent 4c5b6a5282
commit 1445b0a919
1 changed files with 15 additions and 6 deletions

@ -386,10 +386,7 @@ local scripts = internal.scripts
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
local function findScript(name) local function findScript(name)
local file = hack_path..'scripts/'..name..'.lua' local file
if dfhack.filesystem.exists(file) then
return file
end
file = dfhack.getSavePath() file = dfhack.getSavePath()
if file then if file then
file = file .. '/raw/scripts/' .. name .. '.lua' file = file .. '/raw/scripts/' .. name .. '.lua'
@ -397,7 +394,12 @@ local function findScript(name)
return file return file
end end
end end
file = hack_path..'../raw/scripts/' .. name .. '.lua' local path = dfhack.getDFPath()
file = path..'/raw/scripts/' .. name .. '.lua'
if dfhack.filesystem.exists(file) then
return file
end
file = path..'/hack/scripts/'..name..'.lua'
if dfhack.filesystem.exists(file) then if dfhack.filesystem.exists(file) then
return file return file
end end
@ -405,6 +407,10 @@ local function findScript(name)
end end
function dfhack.run_script(name,...) function dfhack.run_script(name,...)
return dfhack.run_script_with_env(nil,name,...)
end
function dfhack.run_script_with_env(envVars,name,...)
local file = findScript(name) local file = findScript(name)
if not file then if not file then
error('Could not find script ' .. name) error('Could not find script ' .. name)
@ -414,10 +420,13 @@ function dfhack.run_script(name,...)
env = {} env = {}
setmetatable(env, { __index = base_env }) setmetatable(env, { __index = base_env })
end end
for x,y in pairs(envVars or {}) do
env[x] = y
end
local f,perr = loadfile(file, 't', env) local f,perr = loadfile(file, 't', env)
if f then if f then
scripts[file] = env scripts[file] = env
return f(...) return f(...), env
end end
error(perr) error(perr)
end end