forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautounsuspend.lua
More file actions
66 lines (52 loc) · 1.45 KB
/
autounsuspend.lua
File metadata and controls
66 lines (52 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Automate periodic running of the unsuspend script
--@module = true
--@enable = true
local json = require('json')
local persist = require('persist-table')
local GLOBAL_KEY = 'autounsuspend' -- used for state change hooks and persistence
enabled = enabled or false
function isEnabled()
return enabled
end
local function persist_state()
persist.GlobalTable[GLOBAL_KEY] = json.encode({enabled=enabled})
end
local function event_loop()
if enabled then
dfhack.run_script('unsuspend', '--quiet')
dfhack.timeout(1, 'days', event_loop)
end
end
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_UNLOADED then
enabled = false
return
end
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
return
end
local persisted_data = json.decode(persist.GlobalTable[GLOBAL_KEY] or '')
enabled = (persisted_data or {enabled=false})['enabled']
event_loop()
end
if dfhack_flags.module then
return
end
if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
dfhack.printerr('autounsuspend needs a loaded fortress map to work')
return
end
local args = {...}
if dfhack_flags and dfhack_flags.enable then
args = {dfhack_flags.enable_state and 'enable' or 'disable'}
end
local command = args[1]
if command == "enable" then
enabled = true
elseif command == "disable" then
enabled = false
else
return
end
event_loop()
persist_state()