--@ module = true -- 把当前界面的字段结构导出到 dfannals-dump.txt,用于摸清 UI 元素。 -- 用法:dfhack-run "dfannals/dump" local MAX_ITEMS = 400 local function is_scalar(v) local t = type(v) return t == 'number' or t == 'string' or t == 'boolean' end local function describe(obj, max_depth) local out = {} local function rec(o, prefix, depth) if depth > max_depth or #out > MAX_ITEMS then return end local ok, iter, state, ctrl = pcall(pairs, o) if not ok then return end for k, v in iter, state, ctrl do if #out > MAX_ITEMS then return end local key = tostring(k) if is_scalar(v) then out[#out + 1] = string.format('%s%s = %s', prefix, key, tostring(v)) else local t = type(v) out[#out + 1] = string.format('%s%s <%s>', prefix, key, t) if t == 'table' or t == 'userdata' then rec(v, prefix .. ' ', depth + 1) end end end end rec(obj, '', 1) return out end local f = io.open('dfannals-dump.txt', 'w') if not f then return end local function write(line) f:write(line .. '\n') end write('# focus=' .. table.concat(dfhack.gui.getCurFocus(), '/')) write('# date=' .. os.date('%Y-%m-%d %H:%M:%S')) local scr = dfhack.gui.getCurViewscreen() local depth = 0 while scr and depth < 3 do write('==== 屏幕层 ' .. depth .. ' ====') local ok, lines = pcall(describe, scr, 2) if ok then for _, l in ipairs(lines) do write(l) end else write('(失败: ' .. tostring(lines) .. ')') end scr = scr.parent depth = depth + 1 end f:close()