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 function filter_settings(settings, searchstring)
348 if not searchstring or searchstring == "" then
352 -- Setup the keyword list
354 for word in searchstring:lower():gmatch("%S+") do
355 table.insert(keywords, word)
359 local category_stack = {}
360 local current_level = 0
361 local best_setting = nil
362 for _, entry in pairs(settings) do
363 if entry.type == "category" then
364 -- Remove all settingless categories
365 while #category_stack > 0 and entry.level <= current_level do
366 table.remove(category_stack, #category_stack)
367 if #category_stack > 0 then
368 current_level = category_stack[#category_stack].level
374 -- Push category onto stack
375 category_stack[#category_stack + 1] = entry
376 current_level = entry.level
378 -- See if setting matches keywords
379 local setting_score = 0
380 for k = 1, #keywords do
381 local keyword = keywords[k]
383 if string.find(entry.name:lower(), keyword, 1, true) then
384 setting_score = setting_score + 1
387 if entry.readable_name and
388 string.find(fgettext(entry.readable_name):lower(), keyword, 1, true) then
389 setting_score = setting_score + 1
393 string.find(fgettext_ne(entry.comment):lower(), keyword, 1, true) then
394 setting_score = setting_score + 1
398 -- Add setting to results if match
399 if setting_score > 0 then
400 -- Add parent categories
401 for _, category in pairs(category_stack) do
402 result[#result + 1] = category
407 result[#result + 1] = entry
408 entry.score = setting_score
410 if not best_setting or
411 setting_score > result[best_setting].score then
412 best_setting = #result
417 return result, best_setting or -1
420 local full_settings = parse_config_file(false, true)
421 local search_string = ""
422 local settings = full_settings
423 local selected_setting = 1
425 local function get_current_value(setting)
426 local value = core.settings:get(setting.name)
428 value = setting.default
433 local function create_change_setting_formspec(dialogdata)
434 local setting = settings[selected_setting]
435 local formspec = "size[10,5.2,true]" ..
436 "button[5,4.5;2,1;btn_done;" .. fgettext("Save") .. "]" ..
437 "button[3,4.5;2,1;btn_cancel;" .. fgettext("Cancel") .. "]" ..
438 "tablecolumns[color;text]" ..
439 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
440 "table[0,0;10,3;info;"
442 if setting.readable_name then
443 formspec = formspec .. "#FFFF00," .. fgettext(setting.readable_name)
444 .. " (" .. core.formspec_escape(setting.name) .. "),"
446 formspec = formspec .. "#FFFF00," .. core.formspec_escape(setting.name) .. ","
449 formspec = formspec .. ",,"
451 local comment_text = ""
453 if setting.comment == "" then
454 comment_text = fgettext_ne("(No description of setting given)")
456 comment_text = fgettext_ne(setting.comment)
458 for _, comment_line in ipairs(comment_text:split("\n", true)) do
459 formspec = formspec .. "," .. core.formspec_escape(comment_line) .. ","
462 if setting.type == "flags" then
463 formspec = formspec .. ",,"
464 .. "," .. fgettext("Please enter a comma seperated list of flags.") .. ","
465 .. "," .. fgettext("Possible values are: ")
466 .. core.formspec_escape(setting.possible:gsub(",", ", ")) .. ","
467 elseif setting.type == "noise_params" then
468 formspec = formspec .. ",,"
469 .. "," .. fgettext("Format: <offset>, <scale>, (<spreadX>, <spreadY>, <spreadZ>), <seed>, <octaves>, <persistence>") .. ","
470 .. "," .. fgettext("Optionally the lacunarity can be appended with a leading comma.") .. ","
471 elseif setting.type == "v3f" then
472 formspec = formspec .. ",,"
473 .. "," .. fgettext_ne("Format is 3 numbers separated by commas and inside brackets.") .. ","
476 formspec = formspec:sub(1, -2) -- remove trailing comma
478 formspec = formspec .. ";1]"
480 if setting.type == "bool" then
482 if core.is_yes(get_current_value(setting)) then
487 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
488 .. fgettext("Disabled") .. "," .. fgettext("Enabled") .. ";"
489 .. selected_index .. "]"
491 elseif setting.type == "enum" then
492 local selected_index = 0
493 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
494 for index, value in ipairs(setting.values) do
495 -- translating value is not possible, since it's the value
496 -- that we set the setting to
497 formspec = formspec .. core.formspec_escape(value) .. ","
498 if get_current_value(setting) == value then
499 selected_index = index
502 if #setting.values > 0 then
503 formspec = formspec:sub(1, -2) -- remove trailing comma
505 formspec = formspec .. ";" .. selected_index .. "]"
507 elseif setting.type == "path" then
508 local current_value = dialogdata.selected_path
509 if not current_value then
510 current_value = get_current_value(setting)
512 formspec = formspec .. "field[0.5,4;7.5,1;te_setting_value;;"
513 .. core.formspec_escape(current_value) .. "]"
514 .. "button[8,3.75;2,1;btn_browser_path;" .. fgettext("Browse") .. "]"
517 -- TODO: fancy input for float, int, flags, noise_params, v3f
519 local text = get_current_value(setting)
520 if dialogdata.error_message then
521 formspec = formspec .. "tablecolumns[color;text]" ..
522 "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
523 "table[5,3.9;5,0.6;error_message;#FF0000,"
524 .. core.formspec_escape(dialogdata.error_message) .. ";0]"
526 if dialogdata.entered_text then
527 text = dialogdata.entered_text
530 formspec = formspec .. "field[0.5,4;" .. width .. ",1;te_setting_value;;"
531 .. core.formspec_escape(text) .. "]"
536 local function handle_change_setting_buttons(this, fields)
537 if fields["btn_done"] or fields["key_enter"] then
538 local setting = settings[selected_setting]
539 if setting.type == "bool" then
540 local new_value = fields["dd_setting_value"]
541 -- Note: new_value is the actual (translated) value shown in the dropdown
542 core.settings:set_bool(setting.name, new_value == fgettext("Enabled"))
544 elseif setting.type == "enum" then
545 local new_value = fields["dd_setting_value"]
546 core.settings:set(setting.name, new_value)
548 elseif setting.type == "int" then
549 local new_value = tonumber(fields["te_setting_value"])
550 if not new_value or math.floor(new_value) ~= new_value then
551 this.data.error_message = fgettext_ne("Please enter a valid integer.")
552 this.data.entered_text = fields["te_setting_value"]
553 core.update_formspec(this:get_formspec())
556 if setting.min and new_value < setting.min then
557 this.data.error_message = fgettext_ne("The value must be at least $1.", setting.min)
558 this.data.entered_text = fields["te_setting_value"]
559 core.update_formspec(this:get_formspec())
562 if setting.max and new_value > setting.max then
563 this.data.error_message = fgettext_ne("The value must not be larger than $1.", setting.max)
564 this.data.entered_text = fields["te_setting_value"]
565 core.update_formspec(this:get_formspec())
568 core.settings:set(setting.name, new_value)
570 elseif setting.type == "float" then
571 local new_value = tonumber(fields["te_setting_value"])
572 if not new_value then
573 this.data.error_message = fgettext_ne("Please enter a valid number.")
574 this.data.entered_text = fields["te_setting_value"]
575 core.update_formspec(this:get_formspec())
578 core.settings:set(setting.name, new_value)
580 elseif setting.type == "flags" then
581 local new_value = fields["te_setting_value"]
582 for _,value in ipairs(new_value:split(",", true)) do
584 local possible = "," .. setting.possible .. ","
585 if not possible:find("," .. value .. ",", 0, true) then
586 this.data.error_message = fgettext_ne("\"$1\" is not a valid flag.", value)
587 this.data.entered_text = fields["te_setting_value"]
588 core.update_formspec(this:get_formspec())
592 core.settings:set(setting.name, new_value)
595 local new_value = fields["te_setting_value"]
596 core.settings:set(setting.name, new_value)
598 core.settings:write()
603 if fields["btn_cancel"] then
608 if fields["btn_browser_path"] then
609 core.show_file_open_dialog("dlg_browse_path", fgettext_ne("Select path"))
612 if fields["dlg_browse_path_accepted"] then
613 this.data.selected_path = fields["dlg_browse_path_accepted"]
614 core.update_formspec(this:get_formspec())
620 local function create_settings_formspec(tabview, name, tabdata)
621 local formspec = "size[12,6.5;true]" ..
622 "tablecolumns[color;tree;text,width=32;text]" ..
623 "tableoptions[background=#00000000;border=false]" ..
624 "field[0.3,0.1;10.2,1;search_string;;" .. core.formspec_escape(search_string) .. "]" ..
625 "field_close_on_enter[search_string;false]" ..
626 "button[10.2,-0.2;2,1;search;" .. fgettext("Search") .. "]" ..
627 "table[0,0.8;12,4.5;list_settings;"
629 local current_level = 0
630 for _, entry in ipairs(settings) do
632 if not core.settings:get_bool("main_menu_technical_settings") and entry.readable_name then
633 name = fgettext_ne(entry.readable_name)
638 if entry.type == "category" then
639 current_level = entry.level
640 formspec = formspec .. "#FFFF00," .. current_level .. "," .. fgettext(name) .. ",,"
642 elseif entry.type == "bool" then
643 local value = get_current_value(entry)
644 if core.is_yes(value) then
645 value = fgettext("Enabled")
647 value = fgettext("Disabled")
649 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
652 elseif entry.type == "key" then
653 -- ignore key settings, since we have a special dialog for them
656 formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
657 .. core.formspec_escape(get_current_value(entry)) .. ","
661 if #settings > 0 then
662 formspec = formspec:sub(1, -2) -- remove trailing comma
664 formspec = formspec .. ";" .. selected_setting .. "]" ..
665 "button[0,6;4,1;btn_back;".. fgettext("< Back to Settings page") .. "]" ..
666 "button[10,6;2,1;btn_edit;" .. fgettext("Edit") .. "]" ..
667 "button[7,6;3,1;btn_restore;" .. fgettext("Restore Default") .. "]" ..
668 "checkbox[0,5.3;cb_tech_settings;" .. fgettext("Show technical names") .. ";"
669 .. dump(core.settings:get_bool("main_menu_technical_settings")) .. "]"
674 local function handle_settings_buttons(this, fields, tabname, tabdata)
675 local list_enter = false
676 if fields["list_settings"] then
677 selected_setting = core.get_table_index("list_settings")
678 if core.explode_table_event(fields["list_settings"]).type == "DCL" then
679 -- Directly toggle booleans
680 local setting = settings[selected_setting]
681 if setting and setting.type == "bool" then
682 local current_value = get_current_value(setting)
683 core.settings:set_bool(setting.name, not core.is_yes(current_value))
684 core.settings:write()
694 if fields.search or fields.key_enter_field == "search_string" then
695 if search_string == fields.search_string then
696 if selected_setting > 0 then
697 -- Go to next result on enter press
698 local i = selected_setting + 1
700 while i > #settings or settings[i].type == "category" do
702 if i > #settings then
703 -- Stop infinte looping
712 core.update_formspec(this:get_formspec())
716 -- Search for setting
717 search_string = fields.search_string
718 settings, selected_setting = filter_settings(full_settings, search_string)
719 core.update_formspec(this:get_formspec())
724 if fields["btn_edit"] or list_enter then
725 local setting = settings[selected_setting]
726 if setting and setting.type ~= "category" then
727 local edit_dialog = dialog_create("change_setting", create_change_setting_formspec,
728 handle_change_setting_buttons)
729 edit_dialog:set_parent(this)
736 if fields["btn_restore"] then
737 local setting = settings[selected_setting]
738 if setting and setting.type ~= "category" then
739 core.settings:set(setting.name, setting.default)
740 core.settings:write()
741 core.update_formspec(this:get_formspec())
746 if fields["btn_back"] then
751 if fields["cb_tech_settings"] then
752 core.settings:set("main_menu_technical_settings", fields["cb_tech_settings"])
753 core.settings:write()
754 core.update_formspec(this:get_formspec())
761 function create_adv_settings_dlg()
762 local dlg = dialog_create("settings_advanced",
763 create_settings_formspec,
764 handle_settings_buttons,
770 -- Generate minetest.conf.example and settings_translation_file.cpp
772 --assert(loadfile(core.get_builtin_path()..DIR_DELIM.."mainmenu"..DIR_DELIM.."generate_from_settingtypes.lua"))(parse_config_file(true, false))