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_%-%.]",
23 INTEGER = "[+-]?[%d]",
24 FLOAT = "[+-]?[%d%.]",
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
68 .. CHAR_CLASSES.SPACE .. "*"
69 .. "%(([^%)]*)%)" -- readable name
70 .. CHAR_CLASSES.SPACE .. "*"
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 = current_comment,
114 if setting_type == "string" or setting_type == "noise_params"
115 or setting_type == "key" or setting_type == "v3f" 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 = 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 = 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 = current_comment,
177 if setting_type == "enum" then
178 local default, values = remaining_line:match("^"
179 -- first value (default) may be empty (i.e. is optional)
180 .. "(" .. CHAR_CLASSES.VARIABLE .. "*)" .. CHAR_CLASSES.SPACE .. "*"
181 .. "(" .. CHAR_CLASSES.FLAGS .. "+)"
184 if not default or values == "" then
185 return "Invalid enum setting"
188 table.insert(settings, {
190 readable_name = readable_name,
193 values = values:split(",", true),
194 comment = current_comment,
199 if setting_type == "path" then
200 local default = remaining_line:match("^(.*)$")
203 return "Invalid path setting"
206 table.insert(settings, {
208 readable_name = readable_name,
211 comment = current_comment,
216 if setting_type == "flags" then
217 local default, possible = remaining_line:match("^"
218 -- first value (default) may be empty (i.e. is optional)
219 -- this is implemented by making the last value optional, and
220 -- swapping them around if it turns out empty.
221 .. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. "*"
222 .. "(" .. CHAR_CLASSES.FLAGS .. "*)"
225 if not default or not possible then
226 return "Invalid flags setting"
229 if possible == "" then
234 table.insert(settings, {
236 readable_name = readable_name,
240 comment = current_comment,
245 return "Invalid setting type \"" .. setting_type .. "\""
248 local function parse_single_file(file, filepath, read_all, result, base_level, allow_secure)
249 -- store this helper variable in the table so it's easier to pass to parse_setting_line()
250 result.current_comment = ""
252 local line = file:read("*line")
254 local error_msg = parse_setting_line(result, line, read_all, base_level, allow_secure)
256 core.log("error", error_msg .. " in " .. filepath .. " \"" .. line .. "\"")
258 line = file:read("*line")
261 result.current_comment = nil
264 -- read_all: whether to ignore certain setting types for GUI or not
265 -- parse_mods: whether to parse settingtypes.txt in mods and games
266 local function parse_config_file(read_all, parse_mods)
267 local builtin_path = core.get_builtin_path() .. DIR_DELIM .. FILENAME
268 local file = io.open(builtin_path, "r")
271 core.log("error", "Can't load " .. FILENAME)
275 parse_single_file(file, builtin_path, read_all, settings, 0, true)
281 local games_category_initialized = false
283 local game = gamemgr.get_game(index)
285 local path = game.path .. DIR_DELIM .. FILENAME
286 local file = io.open(path, "r")
288 if not games_category_initialized then
289 local translation = fgettext_ne("Games"), -- not used, but needed for xgettext
290 table.insert(settings, {
295 games_category_initialized = true
298 table.insert(settings, {
304 parse_single_file(file, path, read_all, settings, 2, false)
310 game = gamemgr.get_game(index)
314 local mods_category_initialized = false
316 get_mods(core.get_modpath(), mods)
317 for _, mod in ipairs(mods) do
318 local path = mod.path .. DIR_DELIM .. FILENAME
319 local file = io.open(path, "r")
321 if not mods_category_initialized then
322 local translation = fgettext_ne("Mods"), -- not used, but needed for xgettext
323 table.insert(settings, {
328 mods_category_initialized = true
331 table.insert(settings, {
337 parse_single_file(file, path, read_all, settings, 2, false)
347 local settings = parse_config_file(false, true)
348 local selected_setting = 1
350 local function get_current_value(setting)
351 local value = core.setting_get(setting.name)
353 value = setting.default
358 local function create_change_setting_formspec(dialogdata)
359 local setting = settings[selected_setting]
360 local formspec = "size[10,5.2,true]" ..
361 "button[5,4.5;2,1;btn_done;" .. fgettext("Save") .. "]" ..
362 "button[3,4.5;2,1;btn_cancel;" .. fgettext("Cancel") .. "]" ..
363 "tablecolumns[color;text]" ..
364 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
365 "table[0,0;10,3;info;"
367 if setting.readable_name then
368 formspec = formspec .. "#FFFF00," .. fgettext(setting.readable_name)
369 .. " (" .. core.formspec_escape(setting.name) .. "),"
371 formspec = formspec .. "#FFFF00," .. core.formspec_escape(setting.name) .. ","
374 formspec = formspec .. ",,"
376 local comment_text = ""
378 if setting.comment == "" then
379 comment_text = fgettext_ne("(No description of setting given)")
381 comment_text = fgettext_ne(setting.comment)
383 for _, comment_line in ipairs(comment_text:split("\n", true)) do
384 formspec = formspec .. "," .. core.formspec_escape(comment_line) .. ","
387 if setting.type == "flags" then
388 formspec = formspec .. ",,"
389 .. "," .. fgettext("Please enter a comma seperated list of flags.") .. ","
390 .. "," .. fgettext("Possible values are: ")
391 .. core.formspec_escape(setting.possible:gsub(",", ", ")) .. ","
392 elseif setting.type == "noise_params" then
393 formspec = formspec .. ",,"
394 .. "," .. fgettext("Format: <offset>, <scale>, (<spreadX>, <spreadY>, <spreadZ>), <seed>, <octaves>, <persistence>") .. ","
395 .. "," .. fgettext("Optionally the lacunarity can be appended with a leading comma.") .. ","
396 elseif setting.type == "v3f" then
397 formspec = formspec .. ",,"
398 .. "," .. fgettext_ne("Format is 3 numbers separated by commas and inside brackets.") .. ","
401 formspec = formspec:sub(1, -2) -- remove trailing comma
403 formspec = formspec .. ";1]"
405 if setting.type == "bool" then
407 if core.is_yes(get_current_value(setting)) then
412 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
413 .. fgettext("Disabled") .. "," .. fgettext("Enabled") .. ";"
414 .. selected_index .. "]"
416 elseif setting.type == "enum" then
417 local selected_index = 0
418 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
419 for index, value in ipairs(setting.values) do
420 -- translating value is not possible, since it's the value
421 -- that we set the setting to
422 formspec = formspec .. core.formspec_escape(value) .. ","
423 if get_current_value(setting) == value then
424 selected_index = index
427 if #setting.values > 0 then
428 formspec = formspec:sub(1, -2) -- remove trailing comma
430 formspec = formspec .. ";" .. selected_index .. "]"
432 elseif setting.type == "path" then
433 local current_value = dialogdata.selected_path
434 if not current_value then
435 current_value = get_current_value(setting)
437 formspec = formspec .. "field[0.5,4;7.5,1;te_setting_value;;"
438 .. core.formspec_escape(current_value) .. "]"
439 .. "button[8,3.75;2,1;btn_browser_path;" .. fgettext("Browse") .. "]"
442 -- TODO: fancy input for float, int, flags, noise_params, v3f
444 local text = get_current_value(setting)
445 if dialogdata.error_message then
446 formspec = formspec .. "tablecolumns[color;text]" ..
447 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
448 "table[5,3.9;5,0.6;error_message;#FF0000,"
449 .. core.formspec_escape(dialogdata.error_message) .. ";0]"
451 if dialogdata.entered_text then
452 text = dialogdata.entered_text
455 formspec = formspec .. "field[0.5,4;" .. width .. ",1;te_setting_value;;"
456 .. core.formspec_escape(text) .. "]"
461 local function handle_change_setting_buttons(this, fields)
462 if fields["btn_done"] or fields["key_enter"] then
463 local setting = settings[selected_setting]
464 if setting.type == "bool" then
465 local new_value = fields["dd_setting_value"]
466 -- Note: new_value is the actual (translated) value shown in the dropdown
467 core.setting_setbool(setting.name, new_value == fgettext("Enabled"))
469 elseif setting.type == "enum" then
470 local new_value = fields["dd_setting_value"]
471 core.setting_set(setting.name, new_value)
473 elseif setting.type == "int" then
474 local new_value = tonumber(fields["te_setting_value"])
475 if not new_value or math.floor(new_value) ~= new_value then
476 this.data.error_message = fgettext_ne("Please enter a valid integer.")
477 this.data.entered_text = fields["te_setting_value"]
478 core.update_formspec(this:get_formspec())
481 if setting.min and new_value < setting.min then
482 this.data.error_message = fgettext_ne("The value must be greater than $1.", setting.min)
483 this.data.entered_text = fields["te_setting_value"]
484 core.update_formspec(this:get_formspec())
487 if setting.max and new_value > setting.max then
488 this.data.error_message = fgettext_ne("The value must be lower than $1.", setting.max)
489 this.data.entered_text = fields["te_setting_value"]
490 core.update_formspec(this:get_formspec())
493 core.setting_set(setting.name, new_value)
495 elseif setting.type == "float" then
496 local new_value = tonumber(fields["te_setting_value"])
497 if not new_value then
498 this.data.error_message = fgettext_ne("Please enter a valid number.")
499 this.data.entered_text = fields["te_setting_value"]
500 core.update_formspec(this:get_formspec())
503 core.setting_set(setting.name, new_value)
505 elseif setting.type == "flags" then
506 local new_value = fields["te_setting_value"]
507 for _,value in ipairs(new_value:split(",", true)) do
509 local possible = "," .. setting.possible .. ","
510 if not possible:find("," .. value .. ",", 0, true) then
511 this.data.error_message = fgettext_ne("\"$1\" is not a valid flag.", value)
512 this.data.entered_text = fields["te_setting_value"]
513 core.update_formspec(this:get_formspec())
517 core.setting_set(setting.name, new_value)
520 local new_value = fields["te_setting_value"]
521 core.setting_set(setting.name, new_value)
528 if fields["btn_cancel"] then
533 if fields["btn_browser_path"] then
534 core.show_file_open_dialog("dlg_browse_path", fgettext_ne("Select path"))
537 if fields["dlg_browse_path_accepted"] then
538 this.data.selected_path = fields["dlg_browse_path_accepted"]
539 core.update_formspec(this:get_formspec())
545 local function create_settings_formspec(tabview, name, tabdata)
546 local formspec = "size[12,6.5;true]" ..
547 "tablecolumns[color;tree;text;text]" ..
548 "tableoptions[background=#00000000;border=false]" ..
549 "table[0,0;12,5.5;list_settings;"
551 local current_level = 0
552 for _, entry in ipairs(settings) do
554 if not core.setting_getbool("main_menu_technical_settings") and entry.readable_name then
555 name = fgettext_ne(entry.readable_name)
560 if entry.type == "category" then
561 current_level = entry.level
562 formspec = formspec .. "#FFFF00," .. current_level .. "," .. fgettext(name) .. ",,"
564 elseif entry.type == "bool" then
565 local value = get_current_value(entry)
566 if core.is_yes(value) then
567 value = fgettext("Enabled")
569 value = fgettext("Disabled")
571 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
574 elseif entry.type == "key" then
575 -- ignore key settings, since we have a special dialog for them
578 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
579 .. core.formspec_escape(get_current_value(entry)) .. ","
583 if #settings > 0 then
584 formspec = formspec:sub(1, -2) -- remove trailing comma
586 formspec = formspec .. ";" .. selected_setting .. "]" ..
587 "button[0,6;4,1;btn_back;".. fgettext("< Back to Settings page") .. "]" ..
588 "button[10,6;2,1;btn_edit;" .. fgettext("Edit") .. "]" ..
589 "button[7,6;3,1;btn_restore;" .. fgettext("Restore Default") .. "]" ..
590 "checkbox[0,5.3;cb_tech_settings;" .. fgettext("Show technical names") .. ";"
591 .. dump(core.setting_getbool("main_menu_technical_settings")) .. "]"
596 local function handle_settings_buttons(this, fields, tabname, tabdata)
597 local list_enter = false
598 if fields["list_settings"] then
599 selected_setting = core.get_table_index("list_settings")
600 if core.explode_table_event(fields["list_settings"]).type == "DCL" then
601 -- Directly toggle booleans
602 local setting = settings[selected_setting]
603 if setting.type == "bool" then
604 local current_value = get_current_value(setting)
605 core.setting_setbool(setting.name, not core.is_yes(current_value))
616 if fields["btn_edit"] or list_enter then
617 local setting = settings[selected_setting]
618 if setting.type ~= "category" then
619 local edit_dialog = dialog_create("change_setting", create_change_setting_formspec,
620 handle_change_setting_buttons)
621 edit_dialog:set_parent(this)
628 if fields["btn_restore"] then
629 local setting = settings[selected_setting]
630 if setting.type ~= "category" then
631 core.setting_set(setting.name, setting.default)
633 core.update_formspec(this:get_formspec())
638 if fields["btn_back"] then
643 if fields["cb_tech_settings"] then
644 core.setting_set("main_menu_technical_settings", fields["cb_tech_settings"])
646 core.update_formspec(this:get_formspec())
653 function create_adv_settings_dlg()
654 local dlg = dialog_create("settings_advanced",
655 create_settings_formspec,
656 handle_settings_buttons,
662 local function create_minetest_conf_example()
663 local result = "# This file contains a list of all available settings and their default value for minetest.conf\n" ..
665 "# By default, all the settings are commented and not functional.\n" ..
666 "# Uncomment settings by removing the preceding #.\n" ..
668 "# minetest.conf is read by default from:\n" ..
669 "# ../minetest.conf\n" ..
670 "# ../../minetest.conf\n" ..
671 "# Any other path can be chosen by passing the path as a parameter\n" ..
672 "# to the program, eg. \"minetest.exe --config ../minetest.conf.example\".\n" ..
674 "# Further documentation:\n" ..
675 "# http://wiki.minetest.net/\n" ..
678 local settings = parse_config_file(true, false)
679 for _, entry in ipairs(settings) do
680 if entry.type == "category" then
681 if entry.level == 0 then
682 result = result .. "#\n# " .. entry.name .. "\n#\n\n"
684 for i = 1, entry.level do
685 result = result .. "#"
687 result = result .. "# " .. entry.name .. "\n\n"
690 if entry.comment ~= "" then
691 for _, comment_line in ipairs(entry.comment:split("\n", true)) do
692 result = result .."# " .. comment_line .. "\n"
695 result = result .. "# type: " .. entry.type
697 result = result .. " min: " .. entry.min
700 result = result .. " max: " .. entry.max
703 result = result .. " values: " .. table.concat(entry.values, ", ")
705 if entry.possible then
706 result = result .. " possible values: " .. entry.possible:gsub(",", ", ")
708 result = result .. "\n"
710 if entry.default ~= "" then
711 append = " " .. entry.default
713 result = result .. "# " .. entry.name .. " =" .. append .. "\n\n"
719 local function create_translation_file()
720 local result = "// This file is automatically generated\n" ..
721 "// It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files\n" ..
722 "// To update it, refer to the bottom of builtin/mainmenu/tab_settings.lua\n\n" ..
723 "fake_function() {\n"
725 local settings = parse_config_file(true, false)
726 for _, entry in ipairs(settings) do
727 if entry.type == "category" then
728 local name_escaped = entry.name:gsub("\"", "\\\"")
729 result = result .. "\tgettext(\"" .. name_escaped .. "\");\n"
731 if entry.readable_name then
732 local readable_name_escaped = entry.readable_name:gsub("\"", "\\\"")
733 result = result .. "\tgettext(\"" .. readable_name_escaped .. "\");\n"
735 if entry.comment ~= "" then
736 local comment_escaped = entry.comment:gsub("\n", "\\n")
737 comment_escaped = comment_escaped:gsub("\"", "\\\"")
738 result = result .. "\tgettext(\"" .. comment_escaped .. "\");\n"
742 result = result .. "}\n"
747 local file = io.open("minetest.conf.example", "w")
749 file:write(create_minetest_conf_example())
755 local file = io.open("src/settings_translation_file.cpp", "w")
757 file:write(create_translation_file())