Skip to content

Commit 5ec1e54

Browse files
authored
persist config (globally)
1 parent ec27b39 commit 5ec1e54

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

gui/tooltips.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
local RELOAD = false -- set to true when actively working on this script
66

77
local gui = require('gui')
8+
local utils = require('utils')
89
local widgets = require('gui.widgets')
910
local overlay = require('plugins.overlay')
1011

@@ -85,6 +86,41 @@ config = config or {
8586
},
8687
}
8788

89+
--------------------------------------------------------------------------------
90+
-- config persistence
91+
local CONFIG_FILE_PATH = 'dfhack-config/tooltips.json'
92+
93+
local function load_config()
94+
local json = require('json')
95+
96+
local f = json.open(CONFIG_FILE_PATH)
97+
if f.exists then
98+
-- remove unknown or out of date entries from the loaded config
99+
-- shallow search should be enough
100+
for k in pairs(f.data) do
101+
if config[k] == nil then
102+
f.data[k] = nil
103+
end
104+
end
105+
106+
-- convert string keys into numbers - workaround json (encoder) limitations
107+
ensure_key(f.data, "happiness_levels")
108+
local t = f.data.happiness_levels
109+
for k, v in pairs(t) do
110+
t[tonumber(k)] = v
111+
t[k] = nil
112+
end
113+
114+
utils.assign(config, f.data)
115+
end
116+
117+
f.data = config -- link the config info with the file
118+
f:write() -- possibly update the stored config
119+
return f
120+
end
121+
122+
local config_file = load_config()
123+
88124
--------------------------------------------------------------------------------
89125

90126
local TITLE = "Tooltips"
@@ -182,6 +218,8 @@ function TooltipControlWindow:on_submit(index, choice)
182218
local cfg = choice.data.cfg
183219
local key = choice.data.key
184220
cfg[key] = not cfg[key]
221+
222+
config_file:write()
185223
end
186224

187225
local function GetUnitHappiness(unit)

0 commit comments

Comments
 (0)