Skip to content

Commit 266f6e9

Browse files
committed
migrate enabled status from cpp nestboxes
1 parent 0aacfa7 commit 266f6e9

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

eggwatch.lua

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local utils = require("utils")
66

77
local GLOBAL_KEY = "eggwatch"
88
local default_table = {10, false, false}
9-
local stringtoboolean = {["true"] = true, ["false"] = false, ["1"] = true, ["0"] = false, ["Y"] = true, ["N"] = false}
9+
local string_or_int_to_boolean = {["true"] = true, ["false"] = false, ["1"] = true, ["0"] = false, ["Y"] = true, ["N"] = false, [1] = true, [0] = false}
1010

1111
function dump(o)
1212
if type(o) == "table" then
@@ -30,6 +30,7 @@ local function get_default_state()
3030
default = default_table,
3131
EVENT_FREQ = 7,
3232
split_stacks = true,
33+
migration_from_cpp_to_lua_done = false,
3334
target_eggs_count_per_race = {}
3435
}
3536
end
@@ -60,20 +61,20 @@ local function format_target_count_row(header, row)
6061
row[1] .. "; count children: " .. tostring(row[2]) .. "; count adults: " .. tostring(row[3])
6162
end
6263
local function print_status()
63-
print_local(("eggwatch is currently %s."):format(state.enabled and "enabled" or "disabled"))
64+
print_local((GLOBAL_KEY .. " is currently %s."):format(state.enabled and "enabled" or "disabled"))
6465
print_local(("egg stack splitting is %s"):format(state.split_stacks and "enabled" or "disabled"))
6566
print_local(format_target_count_row("Default", state.default))
6667
if state.target_eggs_count_per_race ~= nil then
6768
for k, v in pairs(state.target_eggs_count_per_race) do
6869
print_local(format_target_count_row(df.global.world.raws.creatures.all[k].creature_id, v))
6970
end
7071
end
71-
print_details("eggwatch is in verbose mode")
72+
print_details("verbose mode enabled")
7273
print_details(dump(state))
7374
end
7475

7576
local function persist_state()
76-
print_details(("start load_state"))
77+
print_details(("start persist_state"))
7778
local state_to_persist = {}
7879
state_to_persist = utils.clone(state)
7980
state_to_persist.target_eggs_count_per_race = {}
@@ -83,7 +84,7 @@ local function persist_state()
8384
end
8485
end
8586
dfhack.persistent.saveSiteData(GLOBAL_KEY, state_to_persist)
86-
print_details(("end load_state"))
87+
print_details(("end persist_state"))
8788
end
8889

8990
--- Load the saved state of the script
@@ -101,8 +102,25 @@ local function load_state()
101102
end
102103
end
103104
end
105+
104106
state = get_default_state()
105107
utils.assign(state, processed_persisted_data)
108+
109+
if not state.migration_from_cpp_to_lua_done then
110+
print_local("About to attempt migration from cpp to lua")
111+
local nestboxes_status = dfhack.run_command_silent("nestboxes migrate_enabled_status")
112+
if nestboxes_status ~= nil and string_or_int_to_boolean[nestboxes_status] then
113+
print_local(("Migrating status %s from cpp nestboxes to lua"):format(string_or_int_to_boolean[nestboxes_status] and "enabled" or "disabled"))
114+
state.enabled = string_or_int_to_boolean[nestboxes_status]
115+
state.migration_from_cpp_to_lua_done = true
116+
dfhack.persistent['deleteSiteData']("nestboxes/config")
117+
persist_state()
118+
else
119+
print_local("Did not get valid response from cpp nestboxes")
120+
end
121+
print_local("Migrating from cpp to lua done")
122+
end
123+
106124
print_details(("end load_state"))
107125
end
108126

@@ -456,15 +474,15 @@ local function set_target(target_race, target_count, count_children, count_adult
456474
if target_race_upper == "DEFAULT" then
457475
state.default = {
458476
tonumber(target_count),
459-
stringtoboolean[count_children] or false,
460-
stringtoboolean[count_adult] or false
477+
string_or_int_to_boolean[count_children] or false,
478+
string_or_int_to_boolean[count_adult] or false
461479
}
462480
elseif race >= 0 then
463481
print(race)
464482
state.target_eggs_count_per_race[race] = {
465483
tonumber(target_count),
466-
stringtoboolean[count_children] or false,
467-
stringtoboolean[count_adult] or false
484+
string_or_int_to_boolean[count_children] or false,
485+
string_or_int_to_boolean[count_adult] or false
468486
}
469487
else
470488
handle_error("must specify DEFAULT or valid creature_id")
@@ -473,16 +491,12 @@ local function set_target(target_race, target_count, count_children, count_adult
473491
end
474492

475493
if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
476-
dfhack.printerr("eggwatch needs a loaded fortress to work")
494+
dfhack.printerr(GLOBAL_KEY .. " needs a loaded fortress to work")
477495
return
478496
end
479497

480498
load_state()
481499
local args, opts = {...}, {}
482-
if dfhack_flags and dfhack_flags.enable then
483-
args = {dfhack_flags.enable_state and "enable" or "disable"}
484-
end
485-
486500
local positionals =
487501
argparse.processArgsGetopt(
488502
args,
@@ -500,10 +514,8 @@ local positionals =
500514
if dfhack_flags.enable then
501515
if dfhack_flags.enable_state then
502516
do_enable()
503-
print_status()
504517
else
505518
do_disable()
506-
print_status()
507519
end
508520
end
509521

@@ -515,17 +527,17 @@ elseif command == "target" then
515527
set_target(positionals[2], positionals[3], positionals[4], positionals[5])
516528
print_status()
517529
elseif command == "verbose" then
518-
state.verbose = not state.verbose
530+
state.verbose = string_or_int_to_boolean[positionals[2]]
519531
print_status()
520532
elseif command == "clear" then
521533
state = get_default_state()
522534
update_event_listener()
523535
elseif command == "split_stacks" then
524-
state.split_stacks = stringtoboolean[positionals[2]]
536+
state.split_stacks = string_or_int_to_boolean[positionals[2]]
525537
print_status()
526538
elseif not command or command == "status" then
527539
print_status()
528-
elseif (command ~= "enable" or command ~= "disable") and not dfhack_flags.enable then
540+
else
529541
handle_error(("Command '% s' is not recognized"):format(command))
530542
end
531543
persist_state()

0 commit comments

Comments
 (0)