|
5 | 5 | local RELOAD = false -- set to true when actively working on this script |
6 | 6 |
|
7 | 7 | local gui = require('gui') |
| 8 | +local utils = require('utils') |
8 | 9 | local widgets = require('gui.widgets') |
9 | 10 | local overlay = require('plugins.overlay') |
10 | 11 |
|
@@ -85,6 +86,41 @@ config = config or { |
85 | 86 | }, |
86 | 87 | } |
87 | 88 |
|
| 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 | + |
88 | 124 | -------------------------------------------------------------------------------- |
89 | 125 |
|
90 | 126 | local TITLE = "Tooltips" |
@@ -182,6 +218,8 @@ function TooltipControlWindow:on_submit(index, choice) |
182 | 218 | local cfg = choice.data.cfg |
183 | 219 | local key = choice.data.key |
184 | 220 | cfg[key] = not cfg[key] |
| 221 | + |
| 222 | + config_file:write() |
185 | 223 | end |
186 | 224 |
|
187 | 225 | local function GetUnitHappiness(unit) |
|
0 commit comments