Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

control-panel repeats persistence #1170

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Template for new versions:
- `makeown`: set target animals to tame and domesticated
- `gui/sandbox`: created citizens can now be useful military squad members
- `gui/sandbox`: created undead now have a purple shade (after save and reload)
- `control-panel`: restore non-default values of per-save enabled/disabled settings for repeat-based commands

## Misc Improvements
- `gui/unit-info-viewer`: now displays a unit's weight, relative to either dwarves, elephants or cats
Expand Down
5 changes: 3 additions & 2 deletions control-panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ local function apply_fort_loaded_config()
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' and enabled_repeats[data.command] ~= false then
common.apply_command(data)
if data.mode == 'repeat' then
common.apply_command(data, enabled_map, enabled_repeats[data.command])
end
end
end
Expand Down
21 changes: 14 additions & 7 deletions internal/control-panel/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ end

config = config or get_config()

local function munge_repeat_name(name)
return 'control-panel/' .. name
end

local function unmunge_repeat_name(munged_name)
if munged_name:startswith('control-panel/') then
return munged_name:sub(15)
Expand Down Expand Up @@ -110,12 +114,15 @@ function get_description(data)
return helpdb.is_entry(first_word) and helpdb.get_entry_short_help(first_word) or ''
end

local function persist_enabled_repeats()
local function persist_repeats()
local cp_repeats = {}
for munged_name in pairs(repeatUtil.repeating) do
local name = unmunge_repeat_name(munged_name)
if name then
cp_repeats[name] = true
for _, data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'repeat' then
if repeatUtil.repeating[munge_repeat_name(data.command)] then
cp_repeats[data.command] = true
else
cp_repeats[data.command] = false
end
end
end
dfhack.persistent.saveSiteData(REPEATS_GLOBAL_KEY, cp_repeats)
Expand Down Expand Up @@ -147,15 +154,15 @@ function apply_command(data, enabled_map, enabled)
dfhack.run_command{enabled and 'enable' or 'disable', data.command}
end
elseif data.mode == 'repeat' then
local munged_name = 'control-panel/' .. data.command
local munged_name = munge_repeat_name(data.command)
if enabled then
local command_str = ('repeat --name %s %s\n'):
format(munged_name, table.concat(data.params, ' '))
dfhack.run_command(command_str)
else
repeatUtil.cancel(munged_name)
end
persist_enabled_repeats()
persist_repeats()
elseif data.mode == 'run' then
if enabled then
dfhack.run_command(data.command)
Expand Down