nodecore-cd2025/mods/nc_api/util_settings.lua
Aaron Suen 8a8bed8e5d Major settings cleanup
- Run all settings through a common API.
- Use modname prefix consistently for setting
  keys instead of "nodecore".
- Add automatic dumping of settingtypes.txt
  metadata for maintenance.
- Include initial settingtypes.txt for game for
  main menu use.
- Remove per-recipe tuning for pummel recipes,
  as there were way too many of those to be
  possibly useful and it was clogging up the
  settings menu.  Use tool rate adjustments to
  control it instead.
- Remove vestigial enable_damage setting.
2021-07-10 10:04:03 -04:00

96 lines
2.8 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, nodecore, pairs, string, table, tonumber,
tostring
= ipairs, minetest, nodecore, pairs, string, table, tonumber,
tostring
local string_format, string_gmatch, string_match, string_sub,
table_sort
= string.format, string.gmatch, string.match, string.sub,
table.sort
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local known_settings = {}
local known_dirty
local prefix = "nc_"
local function setting_learn(key, title, comment, typename, typedata)
if string_sub(key, 1, #prefix) ~= prefix then return end
if known_settings[key] then return end
known_settings[key] = {
title = title,
comment = comment,
typename = typename,
typedata = typedata
}
known_dirty = true
end
function nodecore.setting_string(key, default, title, comment)
setting_learn(key, title, comment, "string",
default and string_format("%q", default))
local s = minetest.settings:get(key)
return s == "" and default or s
end
function nodecore.setting_float(key, default, title, comment)
setting_learn(key, title, comment, "float",
default and tostring(default) or "")
return tonumber(minetest.settings:get(key)) or default
end
function nodecore.setting_bool(key, default, title, comment)
setting_learn(key, title, comment, "bool",
default ~= nil and (default and "true" or "false") or "")
return minetest.settings:get_bool(key, default)
end
------------------------------------------------------------------------
function nodecore.infodump()
return nodecore.setting_bool(
minetest.get_current_modname() .. "_infodump",
false,
"Write info dumps to world path",
[[Write out after startup (and possibly maintain while running)
text files to the world path containing template metadata for
development use.]]
)
end
if nodecore.infodump() then
minetest.register_globalstep(function()
if not known_dirty then return end
known_dirty = nil
local keys = {}
for k in pairs(known_settings) do keys[#keys + 1] = k end
table_sort(keys)
local dump = "# ===== Automatically generated by " .. modname .. "\n"
for _, k in ipairs(keys) do
dump = dump .. "\n"
local data = known_settings[k]
if data.comment and string_match(data.comment, "%S") then
local txt = ""
for w in string_gmatch(data.comment, "(%S+)") do
if #txt + #w + 1 > 71 then
dump = dump .. "#" .. txt .. "\n"
txt = ""
end
txt = txt .. " " .. w
end
if txt ~= "" then
dump = dump .. "#" .. txt .. "\n"
end
end
dump = dump .. string_format("%s (%s) %s %s\n", k,
data.title or k, data.typename, data.typedata or "")
end
local p = minetest.get_worldpath() .. "/settingtypes.txt"
return minetest.safe_file_write(p, dump)
end)
end