2 --Copyright (C) 2015 PilzAdam
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 local FILENAME = "settingtypes.txt"
20 local CHAR_CLASSES = {
22 VARIABLE = "[%w_%-%.]",
28 -- returns error message, or nil
29 local function parse_setting_line(settings, line, read_all, base_level, allow_secure)
31 local comment = line:match("^#" .. CHAR_CLASSES.SPACE .. "*(.*)$")
33 if settings.current_comment == "" then
34 settings.current_comment = comment
36 settings.current_comment = settings.current_comment .. "\n" .. comment
41 -- clear current_comment so only comments directly above a setting are bound to it
42 -- but keep a local reference to it for variables in the current line
43 local current_comment = settings.current_comment
44 settings.current_comment = ""
47 if line:match("^" .. CHAR_CLASSES.SPACE .. "*$") then
52 local stars, category = line:match("^%[([%*]*)([^%]]+)%]$")
54 table.insert(settings, {
56 level = stars:len() + base_level,
63 local first_part, name, readable_name, setting_type = line:match("^"
64 -- this first capture group matches the whole first part,
65 -- so we can later strip it from the rest of the line
67 .. "([" .. CHAR_CLASSES.VARIABLE .. "+)" -- variable name
69 .. "%(([^%)]*)%)" -- readable name
71 .. "(" .. CHAR_CLASSES.VARIABLE .. "+)" -- type
72 .. CHAR_CLASSES.SPACE .. "?"
75 if not first_part then
79 if name:match("secure%.[.]*") and not allow_secure then
80 return "Tried to add \"secure.\" setting"
83 if readable_name == "" then
86 local remaining_line = line:sub(first_part:len() + 1)
88 if setting_type == "int" then
89 local default, min, max = remaining_line:match("^"
90 -- first int is required, the last 2 are optional
91 .. "(" .. CHAR_CLASSES.INTEGER .. "+)" .. CHAR_CLASSES.SPACE .. "?"
92 .. "(" .. CHAR_CLASSES.INTEGER .. "*)" .. CHAR_CLASSES.SPACE .. "?"
93 .. "(" .. CHAR_CLASSES.INTEGER .. "*)"
96 if not default or not tonumber(default) then
97 return "Invalid integer setting"
102 table.insert(settings, {
104 readable_name = readable_name,
109 comment = settings.current_comment,
114 if setting_type == "string" or setting_type == "noise_params"
115 or setting_type == "key" then
116 local default = remaining_line:match("^(.*)$")
119 return "Invalid string setting"
121 if setting_type == "key" and not read_all then
122 -- ignore key type if read_all is false
126 table.insert(settings, {
128 readable_name = readable_name,
131 comment = settings.current_comment,
136 if setting_type == "bool" then
137 if remaining_line ~= "false" and remaining_line ~= "true" then
138 return "Invalid boolean setting"
141 table.insert(settings, {
143 readable_name = readable_name,
145 default = remaining_line,
146 comment = settings.current_comment,
151 if setting_type == "float" then
152 local default, min, max = remaining_line:match("^"
153 -- first float is required, the last 2 are optional
154 .. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "?"
155 .. "(" .. CHAR_CLASSES.FLOAT .. "*)" .. CHAR_CLASSES.SPACE .. "?"
156 .. "(" .. CHAR_CLASSES.FLOAT .. "*)"
159 if not default or not tonumber(default) then
160 return "Invalid float setting"
165 table.insert(settings, {
167 readable_name = readable_name,
172 comment = settings.current_comment,
177 if setting_type == "enum" then
178 local default, values = remaining_line:match("^(.+)" .. CHAR_CLASSES.SPACE .. "(.+)$")
180 if not default or values == "" then
181 return "Invalid enum setting"
184 table.insert(settings, {
186 readable_name = readable_name,
189 values = values:split(",", true),
190 comment = settings.current_comment,
195 if setting_type == "path" then
196 local default = remaining_line:match("^(.*)$")
199 return "Invalid path setting"
202 table.insert(settings, {
204 readable_name = readable_name,
207 comment = settings.current_comment,
212 if setting_type == "flags" then
213 local default, possible = remaining_line:match("^"
214 .. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. ""
215 .. "(" .. CHAR_CLASSES.FLAGS .. "+)"
218 if not default or not possible then
219 return "Invalid flags setting"
222 table.insert(settings, {
224 readable_name = readable_name,
228 comment = settings.current_comment,
233 return "Invalid setting type \"" .. setting_type .. "\""
236 local function parse_single_file(file, filepath, read_all, result, base_level, allow_secure)
237 -- store this helper variable in the table so it's easier to pass to parse_setting_line()
238 result.current_comment = ""
240 local line = file:read("*line")
242 local error_msg = parse_setting_line(result, line, read_all, base_level, allow_secure)
244 core.log("error", error_msg .. " in " .. filepath .. " \"" .. line .. "\"")
246 line = file:read("*line")
249 result.current_comment = nil
252 -- read_all: whether to ignore certain setting types for GUI or not
253 -- parse_mods: whether to parse settingtypes.txt in mods and games
254 local function parse_config_file(read_all, parse_mods)
255 local builtin_path = core.get_builtin_path() .. DIR_DELIM .. FILENAME
256 local file = io.open(builtin_path, "r")
259 core.log("error", "Can't load " .. FILENAME)
263 parse_single_file(file, builtin_path, read_all, settings, 0, true)
269 local games_category_initialized = false
271 local game = gamemgr.get_game(index)
273 local path = game.path .. DIR_DELIM .. FILENAME
274 local file = io.open(path, "r")
276 if not games_category_initialized then
277 local translation = fgettext_ne("Games"), -- not used, but needed for xgettext
278 table.insert(settings, {
283 games_category_initialized = true
286 table.insert(settings, {
292 parse_single_file(file, path, read_all, settings, 2, false)
298 game = gamemgr.get_game(index)
302 local mods_category_initialized = false
304 get_mods(core.get_modpath(), mods)
305 for _, mod in ipairs(mods) do
306 local path = mod.path .. DIR_DELIM .. FILENAME
307 local file = io.open(path, "r")
309 if not mods_category_initialized then
310 local translation = fgettext_ne("Mods"), -- not used, but needed for xgettext
311 table.insert(settings, {
316 mods_category_initialized = true
319 table.insert(settings, {
325 parse_single_file(file, path, read_all, settings, 2, false)
335 local settings = parse_config_file(false, true)
336 local selected_setting = 1
338 local function get_current_value(setting)
339 local value = core.setting_get(setting.name)
341 value = setting.default
346 local function create_change_setting_formspec(dialogdata)
347 local setting = settings[selected_setting]
348 local formspec = "size[10,5.2,true]" ..
349 "button[5,4.5;2,1;btn_done;" .. fgettext("Save") .. "]" ..
350 "button[3,4.5;2,1;btn_cancel;" .. fgettext("Cancel") .. "]" ..
351 "tablecolumns[color;text]" ..
352 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
353 "table[0,0;10,3;info;"
355 if setting.readable_name then
356 formspec = formspec .. "#FFFF00," .. fgettext(setting.readable_name)
357 .. " (" .. core.formspec_escape(setting.name) .. "),"
359 formspec = formspec .. "#FFFF00," .. core.formspec_escape(setting.name) .. ","
362 formspec = formspec .. ",,"
364 local comment_text = ""
366 if setting.comment == "" then
367 comment_text = fgettext_ne("(No description of setting given)")
369 comment_text = fgettext_ne(setting.comment)
371 for _, comment_line in ipairs(comment_text:split("\n", true)) do
372 formspec = formspec .. "," .. core.formspec_escape(comment_line) .. ","
375 if setting.type == "flags" then
376 formspec = formspec .. ",,"
377 .. "," .. fgettext("Please enter a comma seperated list of flags.") .. ","
378 .. "," .. fgettext("Possible values are: ")
379 .. core.formspec_escape(setting.possible:gsub(",", ", ")) .. ","
380 elseif setting.type == "noise_params" then
381 formspec = formspec .. ",,"
382 .. "," .. fgettext("Format: <offset>, <scale>, (<spreadX>, <spreadY>, <spreadZ>), <seed>, <octaves>, <persistence>") .. ","
383 .. "," .. fgettext("Optionally the lacunarity can be appended with a leading comma.") .. ","
386 formspec = formspec:sub(1, -2) -- remove trailing comma
388 formspec = formspec .. ";1]"
390 if setting.type == "bool" then
392 if core.is_yes(get_current_value(setting)) then
397 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
398 .. fgettext("Disabled") .. "," .. fgettext("Enabled") .. ";"
399 .. selected_index .. "]"
401 elseif setting.type == "enum" then
402 local selected_index = 0
403 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
404 for index, value in ipairs(setting.values) do
405 -- translating value is not possible, since it's the value
406 -- that we set the setting to
407 formspec = formspec .. core.formspec_escape(value) .. ","
408 if get_current_value(setting) == value then
409 selected_index = index
412 if #setting.values > 0 then
413 formspec = formspec:sub(1, -2) -- remove trailing comma
415 formspec = formspec .. ";" .. selected_index .. "]"
417 elseif setting.type == "path" then
418 local current_value = dialogdata.selected_path
419 if not current_value then
420 current_value = get_current_value(setting)
422 formspec = formspec .. "field[0.5,4;7.5,1;te_setting_value;;"
423 .. core.formspec_escape(current_value) .. "]"
424 .. "button[8,3.75;2,1;btn_browser_path;" .. fgettext("Browse") .. "]"
427 -- TODO: fancy input for float, int, flags, noise_params
429 local text = get_current_value(setting)
430 if dialogdata.error_message then
431 formspec = formspec .. "tablecolumns[color;text]" ..
432 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
433 "table[5,4;5,1;error_message;#FF0000,"
434 .. core.formspec_escape(dialogdata.error_message) .. ";0]"
436 if dialogdata.entered_text then
437 text = dialogdata.entered_text
440 formspec = formspec .. "field[0.5,4;" .. width .. ",1;te_setting_value;;"
441 .. core.formspec_escape(text) .. "]"
446 local function handle_change_setting_buttons(this, fields)
447 if fields["btn_done"] or fields["key_enter"] then
448 local setting = settings[selected_setting]
449 if setting.type == "bool" then
450 local new_value = fields["dd_setting_value"]
451 -- Note: new_value is the actual (translated) value shown in the dropdown
452 core.setting_setbool(setting.name, new_value == fgettext("Enabled"))
454 elseif setting.type == "enum" then
455 local new_value = fields["dd_setting_value"]
456 core.setting_set(setting.name, new_value)
458 elseif setting.type == "int" then
459 local new_value = tonumber(fields["te_setting_value"])
460 if not new_value or math.floor(new_value) ~= new_value then
461 this.data.error_message = fgettext_ne("Please enter a valid integer.")
462 this.data.entered_text = fields["te_setting_value"]
463 core.update_formspec(this:get_formspec())
466 if setting.min and new_value < setting.min then
467 this.data.error_message = fgettext_ne("The value must be greater than $1.", setting.min)
468 this.data.entered_text = fields["te_setting_value"]
469 core.update_formspec(this:get_formspec())
472 if setting.max and new_value > setting.max then
473 this.data.error_message = fgettext_ne("The value must be lower than $1.", setting.max)
474 this.data.entered_text = fields["te_setting_value"]
475 core.update_formspec(this:get_formspec())
478 core.setting_set(setting.name, new_value)
480 elseif setting.type == "float" then
481 local new_value = tonumber(fields["te_setting_value"])
482 if not new_value then
483 this.data.error_message = fgettext_ne("Please enter a valid number.")
484 this.data.entered_text = fields["te_setting_value"]
485 core.update_formspec(this:get_formspec())
488 core.setting_set(setting.name, new_value)
490 elseif setting.type == "flags" then
491 local new_value = fields["te_setting_value"]
492 for _,value in ipairs(new_value:split(",", true)) do
494 if not value:match(CHAR_CLASSES.FLAGS .. "+")
495 or not setting.possible:match("[,]?" .. value .. "[,]?") then
496 this.data.error_message = fgettext_ne("\"" .. value .. "\" is not a valid flag.")
497 this.data.entered_text = fields["te_setting_value"]
498 core.update_formspec(this:get_formspec())
502 core.setting_set(setting.name, new_value)
505 local new_value = fields["te_setting_value"]
506 core.setting_set(setting.name, new_value)
513 if fields["btn_cancel"] then
518 if fields["btn_browser_path"] then
519 core.show_file_open_dialog("dlg_browse_path", fgettext_ne("Select path"))
522 if fields["dlg_browse_path_accepted"] then
523 this.data.selected_path = fields["dlg_browse_path_accepted"]
524 core.update_formspec(this:get_formspec())
530 local function create_settings_formspec(tabview, name, tabdata)
531 local formspec = "tablecolumns[color;tree;text;text]" ..
532 "tableoptions[background=#00000000;border=false]" ..
533 "table[0,0;12,4.5;list_settings;"
535 local current_level = 0
536 for _, entry in ipairs(settings) do
538 if not core.setting_getbool("main_menu_technical_settings") and entry.readable_name then
539 name = fgettext_ne(entry.readable_name)
544 if entry.type == "category" then
545 current_level = entry.level
546 formspec = formspec .. "#FFFF00," .. current_level .. "," .. fgettext(name) .. ",,"
548 elseif entry.type == "bool" then
549 local value = get_current_value(entry)
550 if core.is_yes(value) then
551 value = fgettext("Enabled")
553 value = fgettext("Disabled")
555 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
558 elseif entry.type == "key" then
559 -- ignore key settings, since we have a special dialog for them
562 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
563 .. core.formspec_escape(get_current_value(entry)) .. ","
567 if #settings > 0 then
568 formspec = formspec:sub(1, -2) -- remove trailing comma
570 formspec = formspec .. ";" .. selected_setting .. "]" ..
571 "button[4,4.5;3,1;btn_change_keys;".. fgettext("Change keys") .. "]" ..
572 "button[10,4.5;2,1;btn_edit;" .. fgettext("Edit") .. "]" ..
573 "button[7,4.5;3,1;btn_restore;" .. fgettext("Restore Default") .. "]" ..
574 "checkbox[0,4.5;cb_tech_settings;" .. fgettext("Show technical names") .. ";"
575 .. dump(core.setting_getbool("main_menu_technical_settings")) .. "]"
580 local function handle_settings_buttons(this, fields, tabname, tabdata)
581 local list_enter = false
582 if fields["list_settings"] then
583 selected_setting = core.get_table_index("list_settings")
584 if core.explode_table_event(fields["list_settings"]).type == "DCL" then
585 -- Directly toggle booleans
586 local setting = settings[selected_setting]
587 if setting.type == "bool" then
588 local current_value = get_current_value(setting)
589 core.setting_setbool(setting.name, not core.is_yes(current_value))
600 if fields["btn_edit"] or list_enter then
601 local setting = settings[selected_setting]
602 if setting.type ~= "category" then
603 local edit_dialog = dialog_create("change_setting", create_change_setting_formspec,
604 handle_change_setting_buttons)
605 edit_dialog:set_parent(this)
612 if fields["btn_restore"] then
613 local setting = settings[selected_setting]
614 if setting.type ~= "category" then
615 core.setting_set(setting.name, setting.default)
617 core.update_formspec(this:get_formspec())
622 if fields["btn_change_keys"] then
623 core.show_keys_menu()
627 if fields["cb_tech_settings"] then
628 core.setting_set("main_menu_technical_settings", fields["cb_tech_settings"])
630 core.update_formspec(this:get_formspec())
639 caption = fgettext("Settings"),
640 cbf_formspec = create_settings_formspec,
641 cbf_button_handler = handle_settings_buttons,
644 local function create_minetest_conf_example()
645 local result = "# This file contains a list of all available settings and their default value for minetest.conf\n" ..
647 "# By default, all the settings are commented and not functional.\n" ..
648 "# Uncomment settings by removing the preceding #.\n" ..
650 "# minetest.conf is read by default from:\n" ..
651 "# ../minetest.conf\n" ..
652 "# ../../minetest.conf\n" ..
653 "# Any other path can be chosen by passing the path as a parameter\n" ..
654 "# to the program, eg. \"minetest.exe --config ../minetest.conf.example\".\n" ..
656 "# Further documentation:\n" ..
657 "# http://wiki.minetest.net/\n" ..
660 local settings = parse_config_file(true, false)
661 for _, entry in ipairs(settings) do
662 if entry.type == "category" then
663 if entry.level == 0 then
664 result = result .. "#\n# " .. entry.name .. "\n#\n\n"
666 for i = 1, entry.level do
667 result = result .. "#"
669 result = result .. "# " .. entry.name .. "\n\n"
672 if entry.comment ~= "" then
673 for _, comment_line in ipairs(entry.comment:split("\n", true)) do
674 result = result .."# " .. comment_line .. "\n"
677 result = result .. "# type: " .. entry.type
679 result = result .. " min: " .. entry.min
682 result = result .. " max: " .. entry.max
685 result = result .. " values: " .. table.concat(entry.values, ", ")
687 if entry.possible then
688 result = result .. " possible values: " .. entry.possible:gsub(",", ", ")
690 result = result .. "\n"
691 result = result .. "# " .. entry.name .. " = ".. entry.default .. "\n\n"
697 local function create_translation_file()
698 local result = "// This file is automatically generated\n" ..
699 "// It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files\n" ..
700 "// To update it, refer to the bottom of builtin/mainmenu/tab_settings.lua\n\n" ..
701 "fake_function() {\n"
703 local settings = parse_config_file(true, false)
704 for _, entry in ipairs(settings) do
705 if entry.type == "category" then
706 result = result .. "\tgettext(\"" .. entry.name .. "\");\n"
708 if entry.readable_name then
709 result = result .. "\tgettext(\"" .. entry.readable_name .. "\");\n"
711 if entry.comment ~= "" then
712 local comment = entry.comment:gsub("\n", "\\n")
713 result = result .. "\tgettext(\"" .. comment .. "\");\n"
717 result = result .. "}\n"
722 local file = io.open("minetest.conf.example", "w")
724 file:write(create_minetest_conf_example())
730 local file = io.open("src/settings_translation_file.cpp", "w")
732 file:write(create_translation_file())