forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
control-panel.lua
242 lines (212 loc) · 7.22 KB
/
control-panel.lua
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
--@module = true
local argparse = require('argparse')
local common = reqscript('internal/control-panel/common')
local registry = reqscript('internal/control-panel/registry')
local utils = require('utils')
local GLOBAL_KEY = 'control-panel'
-- state change hooks
local function apply_system_config()
local enabled_map = common.get_enabled_map()
for _, data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'system_enable' or data.mode == 'tweak' then
common.apply_command(data, enabled_map)
end
end
for _, data in ipairs(registry.PREFERENCES_BY_IDX) do
local value = safe_index(common.config.data.preferences, data.name, 'val')
if value ~= nil then
data.set_fn(value)
end
end
end
local function apply_autostart_config()
local enabled_map =common.get_enabled_map()
for _, data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'enable' or data.mode == 'run' or data.mode == 'repeat' then
common.apply_command(data, enabled_map)
end
end
end
local function apply_fort_loaded_config()
local state = dfhack.persistent.getSiteData(GLOBAL_KEY, {})
if not state.autostart_done then
apply_autostart_config()
dfhack.persistent.saveSiteData(GLOBAL_KEY, {autostart_done=true})
end
local enabled_map = common.get_enabled_map()
local enabled_repeats = dfhack.persistent.getSiteData(common.REPEATS_GLOBAL_KEY, {})
for _, data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'repeat' then
common.apply_command(data, enabled_map, enabled_repeats[data.command])
end
end
end
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_CORE_INITIALIZED then
apply_system_config()
elseif sc == SC_MAP_LOADED and dfhack.world.isFortressMode() then
apply_fort_loaded_config()
end
end
local function get_command_data(name_or_idx)
if type(name_or_idx) == 'number' then
return registry.COMMANDS_BY_IDX[name_or_idx]
end
return registry.COMMANDS_BY_NAME[name_or_idx]
end
local function get_autostart_internal(data)
local default_value = not not data.default
local current_value = safe_index(common.config.data.commands, data.command, 'autostart')
if current_value == nil then
current_value = default_value
end
return current_value, default_value
end
-- API
-- returns current, default
function get_autostart(command)
local data = get_command_data(command)
if not data then return end
return get_autostart_internal(data)
end
-- CLI
local function print_header(header)
print()
print(header)
print(('-'):rep(#header))
end
local function list_command_group(group, filter_strs, enabled_map)
local header = ('Group: %s'):format(group)
for idx, data in ipairs(registry.COMMANDS_BY_IDX) do
if not common.command_passes_filters(data, group, filter_strs) then
goto continue
end
if header then
print_header(header)
---@diagnostic disable-next-line: cast-local-type
header = nil
end
local extra = ''
if data.mode == 'system_enable' or data.mode == 'tweak' then
extra = ' (global)'
end
print(('%d) %s%s'):format(idx, data.command, extra))
local desc = common.get_description(data)
if #desc > 0 then
print((' %s'):format(desc))
end
print((' autostart enabled: %s (default: %s)'):format(get_autostart_internal(data)))
if enabled_map[data.command] ~= nil then
print((' currently enabled: %s'):format(enabled_map[data.command]))
end
print()
::continue::
end
if not header then
end
end
local function list_preferences(filter_strs)
local header = 'Preferences'
for _, data in ipairs(registry.PREFERENCES_BY_IDX) do
local search_key = ('%s %s %s'):format(data.name, data.label, data.desc)
if not utils.search_text(search_key, filter_strs) then goto continue end
if header then
print_header(header)
---@diagnostic disable-next-line: cast-local-type
header = nil
end
print(('%s) %s'):format(data.name, data.label))
print((' %s'):format(data.desc))
print((' current: %s (default: %s)'):format(data.get_fn(), data.default))
if data.min then
print((' minimum: %s'):format(data.min))
end
print()
::continue::
end
end
local function do_list(filter_strs)
local enabled_map = common.get_enabled_map()
list_command_group('automation', filter_strs, enabled_map)
list_command_group('bugfix', filter_strs, enabled_map)
list_command_group('gameplay', filter_strs, enabled_map)
list_preferences(filter_strs)
end
local function do_enable_disable(which, entries)
local enabled_map =common.get_enabled_map()
for _, entry in ipairs(entries) do
local data = get_command_data(entry)
if data.mode ~= 'system_enable' and not dfhack.world.isFortressMode() then
qerror('must have a loaded fortress to enable '..data.name)
end
if common.apply_command(data, enabled_map, which == 'en') then
print(('%sabled %s'):format(which, entry))
end
end
end
local function do_enable(entries)
do_enable_disable('en', entries)
end
local function do_disable(entries)
do_enable_disable('dis', entries)
end
local function do_autostart_noautostart(which, entries)
for _, entry in ipairs(entries) do
local data = get_command_data(entry)
if not data then
qerror(('autostart command or index not found: "%s"'):format(entry))
else
common.set_autostart(data, which == 'en')
print(('%sabled autostart for: %s'):format(which, entry))
end
end
common.config:write()
end
local function do_autostart(entries)
do_autostart_noautostart('en', entries)
end
local function do_noautostart(entries)
do_autostart_noautostart('dis', entries)
end
local function do_set(params)
local name, value = params[1], params[2]
local data = registry.PREFERENCES_BY_NAME[name]
if not data then
qerror(('preference name not found: "%s"'):format(name))
end
common.set_preference(data, value)
common.config:write()
end
local function do_reset(params)
local name = params[1]
local data = registry.PREFERENCES_BY_NAME[name]
if not data then
qerror(('preference name not found: "%s"'):format(name))
end
common.set_preference(data, data.default)
common.config:write()
end
local command_switch = {
list=do_list,
enable=do_enable,
disable=do_disable,
autostart=do_autostart,
noautostart=do_noautostart,
set=do_set,
reset=do_reset,
}
local function main(args)
local help = false
local positionals = argparse.processArgsGetopt(args, {
{'h', 'help', handler=function() help = true end},
})
local command = table.remove(positionals, 1)
if help or not command or not command_switch[command] then
print(dfhack.script_help())
return
end
command_switch[command](positionals)
end
if not dfhack_flags.module then
main{...}
end