generate_from_settingtypes.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. local concat = table.concat
  2. local insert = table.insert
  3. local sprintf = string.format
  4. local rep = string.rep
  5. local minetest_example_header = [[
  6. # This file contains a list of all available settings and their default value for minetest.conf
  7. # By default, all the settings are commented and not functional.
  8. # Uncomment settings by removing the preceding #.
  9. # minetest.conf is read by default from:
  10. # ../minetest.conf
  11. # ../../minetest.conf
  12. # Any other path can be chosen by passing the path as a parameter
  13. # to the program, eg. "minetest.exe --config ../minetest.conf.example".
  14. # Further documentation:
  15. # https://wiki.minetest.net/
  16. ]]
  17. local group_format_template = [[
  18. # %s = {
  19. # offset = %s,
  20. # scale = %s,
  21. # spread = (%s, %s, %s),
  22. # seed = %s,
  23. # octaves = %s,
  24. # persistence = %s,
  25. # lacunarity = %s,
  26. # flags =%s
  27. # }
  28. ]]
  29. local function create_minetest_conf_example(settings)
  30. local result = { minetest_example_header }
  31. for _, entry in ipairs(settings) do
  32. if entry.type == "category" then
  33. if entry.level == 0 then
  34. insert(result, "#\n# " .. entry.name .. "\n#\n\n")
  35. else
  36. insert(result, rep("#", entry.level))
  37. insert(result, "# " .. entry.name .. "\n\n")
  38. end
  39. else
  40. local group_format = false
  41. if entry.noise_params and entry.values then
  42. if entry.type == "noise_params_2d" or entry.type == "noise_params_3d" then
  43. group_format = true
  44. end
  45. end
  46. if entry.comment ~= "" then
  47. for _, comment_line in ipairs(entry.comment:split("\n", true)) do
  48. if comment_line == "" then
  49. insert(result, "#\n")
  50. else
  51. insert(result, "# " .. comment_line .. "\n")
  52. end
  53. end
  54. end
  55. if entry.type == "key" then
  56. local line = "See https://github.com/minetest/irrlicht/blob/master/include/Keycodes.h"
  57. insert(result, "# " .. line .. "\n")
  58. end
  59. insert(result, "# type: " .. entry.type)
  60. if entry.min then
  61. insert(result, " min: " .. entry.min)
  62. end
  63. if entry.max then
  64. insert(result, " max: " .. entry.max)
  65. end
  66. if entry.values and entry.noise_params == nil then
  67. insert(result, " values: " .. concat(entry.values, ", "))
  68. end
  69. if entry.possible then
  70. insert(result, " possible values: " .. concat(entry.possible, ", "))
  71. end
  72. insert(result, "\n")
  73. if group_format == true then
  74. local flags = entry.values[10]
  75. if flags ~= "" then
  76. flags = " "..flags
  77. end
  78. insert(result, sprintf(group_format_template, entry.name, entry.values[1],
  79. entry.values[2], entry.values[3], entry.values[4], entry.values[5],
  80. entry.values[6], entry.values[7], entry.values[8], entry.values[9],
  81. flags))
  82. else
  83. local append
  84. if entry.default ~= "" then
  85. append = " " .. entry.default
  86. end
  87. insert(result, sprintf("# %s =%s\n\n", entry.name, append or ""))
  88. end
  89. end
  90. end
  91. return concat(result)
  92. end
  93. local translation_file_header = [[
  94. // This file is automatically generated
  95. // It contains a bunch of fake gettext calls, to tell xgettext about the strings in config files
  96. // To update it, refer to the bottom of builtin/mainmenu/dlg_settings_advanced.lua
  97. fake_function() {]]
  98. local function create_translation_file(settings)
  99. local result = { translation_file_header }
  100. for _, entry in ipairs(settings) do
  101. if entry.type == "category" then
  102. insert(result, sprintf("\tgettext(%q);", entry.name))
  103. else
  104. if entry.readable_name then
  105. insert(result, sprintf("\tgettext(%q);", entry.readable_name))
  106. end
  107. if entry.comment ~= "" then
  108. local comment_escaped = entry.comment:gsub("\n", "\\n")
  109. comment_escaped = comment_escaped:gsub("\"", "\\\"")
  110. insert(result, "\tgettext(\"" .. comment_escaped .. "\");")
  111. end
  112. end
  113. end
  114. insert(result, "}\n")
  115. return concat(result, "\n")
  116. end
  117. local file = assert(io.open("minetest.conf.example", "w"))
  118. file:write(create_minetest_conf_example(settingtypes.parse_config_file(true, false)))
  119. file:close()
  120. file = assert(io.open("src/settings_translation_file.cpp", "w"))
  121. -- If 'minetest.conf.example' appears in the 'bin' folder, the line below may have to be
  122. -- used instead. The file will also appear in the 'bin' folder.
  123. --file = assert(io.open("settings_translation_file.cpp", "w"))
  124. -- We don't want hidden settings to be translated, so we set read_all to false.
  125. file:write(create_translation_file(settingtypes.parse_config_file(false, false)))
  126. file:close()