configuration.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. --[[
  2. Configuration from default, moddir and worlddir, in that order.
  3. See init.lua for license.
  4. ]]
  5. -- change these for other mods
  6. local M = thirsty
  7. local modname = 'thirsty'
  8. local fileroot = modname
  9. -- make sure config exists; keep constant reference to it
  10. local C = M.config or {}
  11. M.config = C
  12. local function try_config_file(filename)
  13. --print("Config from "..filename)
  14. local file, err = io.open(filename, 'r')
  15. if file then
  16. file:close() -- was just for checking existance
  17. local confcode, err = loadfile(filename)
  18. if confcode then
  19. confcode()
  20. if C ~= M.config then
  21. -- M.config was overriden, merge
  22. for key, value in pairs(M.config) do
  23. if type(value) == 'table' and type(C[key]) == 'table' and not value.CLEAR then
  24. for k, v in pairs(value) do
  25. C[key][k] = value[k]
  26. end
  27. else
  28. -- copy (not a table, or asked to clear)
  29. C[key] = value
  30. end
  31. end
  32. else
  33. -- no override? Empty, or file knows what it is doing.
  34. end
  35. else
  36. minetest.log("error", "Could not load " .. filename .. ": " .. err)
  37. end
  38. end
  39. end
  40. -- read starting configuration from <modname>.default.conf
  41. try_config_file(minetest.get_modpath(modname) .. "/" .. fileroot .. ".default.conf")
  42. -- next, install-specific copy in modpath
  43. try_config_file(minetest.get_modpath(modname) .. "/" .. fileroot .. ".conf")
  44. -- last, world-specific copy in worldpath
  45. try_config_file(minetest.get_worldpath() .. "/" .. fileroot .. ".conf")
  46. -- remove any special keys from tables
  47. for key, value in pairs(C) do
  48. if type(value) == 'table' then
  49. value.CLEAR = nil
  50. end
  51. end
  52. -- write back
  53. M.config = C