2014-06-26 06:36:57 -06:00
|
|
|
-- repeat.lua
|
2014-06-26 23:40:36 -06:00
|
|
|
-- repeatedly calls a lua script, eg "repeat -time 1 months -command cleanowned"; to disable "repeat -cancel cleanowned"
|
|
|
|
-- repeat -help for details
|
2014-06-26 06:36:57 -06:00
|
|
|
-- author expwnent
|
|
|
|
-- vaguely based on a script by Putnam
|
|
|
|
|
2014-06-27 04:23:04 -06:00
|
|
|
local repeatUtil = require 'repeatUtil'
|
2014-06-26 06:36:57 -06:00
|
|
|
|
|
|
|
local args = {...}
|
|
|
|
if args[1] == '-cancel' then
|
|
|
|
repeatUtil.cancel(args[2])
|
|
|
|
return
|
|
|
|
elseif args[1] == '-help' then
|
|
|
|
print([[repeat.lua
|
|
|
|
repeat -help
|
|
|
|
print this help message
|
|
|
|
repeat -cancel bob
|
|
|
|
cancels the repetition with the name bob
|
|
|
|
repeat -name jim -time delay timeUnits -printResult true -command printArgs 3 1 2
|
|
|
|
except for -command, arguments can go in any order
|
|
|
|
-name sets the name for the purposes of cancelling and making sure you don't schedule the same repeating event twice
|
|
|
|
if not specified, it's set to the first argument after -command
|
|
|
|
-time delay timeUnits
|
|
|
|
delay is some positive integer
|
|
|
|
timeUnits is some valid time unit for dfhack.timeout(delay,timeUnits,function)
|
|
|
|
-printResult true
|
|
|
|
print the results of the command
|
|
|
|
-printResult false
|
|
|
|
suppress the results of the command
|
2014-06-27 04:23:04 -06:00
|
|
|
-later
|
|
|
|
make it happen later instead of now (every 5 ticks starting now vs every 5 ticks starting in 5 ticks)
|
2014-06-26 06:36:57 -06:00
|
|
|
-command ...
|
|
|
|
specify the command to be run
|
|
|
|
]])
|
|
|
|
end
|
|
|
|
|
|
|
|
local name=nil
|
|
|
|
local time
|
|
|
|
local timeUnits
|
|
|
|
local i=1
|
|
|
|
local command={}
|
|
|
|
local printResult=true
|
2014-06-27 04:23:04 -06:00
|
|
|
local later = false
|
2014-06-26 06:36:57 -06:00
|
|
|
while i <= #args do
|
|
|
|
if args[i] == '-name' then
|
|
|
|
name = args[i+1]
|
|
|
|
i = i + 2
|
2014-06-27 04:23:04 -06:00
|
|
|
elseif args[i] == '-later' then
|
|
|
|
later = true
|
|
|
|
i = i+1
|
2014-06-26 06:36:57 -06:00
|
|
|
elseif args[i] == '-time' then
|
|
|
|
time = tonumber(args[i+1])
|
|
|
|
timeUnits = args[i+2]
|
|
|
|
i = i+3
|
|
|
|
elseif args[i] == '-command' then
|
|
|
|
name = name or args[i+1]
|
|
|
|
for j=i+1,#args,1 do
|
|
|
|
table.insert(command,args[j])
|
|
|
|
end
|
|
|
|
break
|
|
|
|
elseif args[i] == '-printResult' then
|
|
|
|
if args[i+1] == "true" then
|
|
|
|
printOutput = true
|
|
|
|
elseif args[i+1] == "false" then
|
|
|
|
printOutput = false
|
|
|
|
else
|
|
|
|
qerror("repeat -printResult " .. args[i+1] .. ": expected true or false")
|
|
|
|
end
|
|
|
|
i = i+2
|
2014-06-26 23:46:00 -06:00
|
|
|
else
|
|
|
|
qerror('Improper arguments to repeat.')
|
2014-06-26 06:36:57 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-27 04:23:04 -06:00
|
|
|
local callCommand = function()
|
|
|
|
local result = dfhack.run_command(table.unpack(command))
|
2014-06-26 06:36:57 -06:00
|
|
|
if printResult then
|
|
|
|
print(result)
|
|
|
|
end
|
2014-06-27 04:23:04 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local func
|
|
|
|
if later then
|
|
|
|
local didOnce
|
|
|
|
func = function()
|
|
|
|
if didOnce then
|
|
|
|
callCommand()
|
|
|
|
else
|
|
|
|
didOnce = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
func = callCommand
|
|
|
|
end
|
|
|
|
|
|
|
|
repeatUtil.scheduleEvery(name,time,timeUnits,func)
|
2014-06-26 06:36:57 -06:00
|
|
|
|