diff --git a/.editorconfig b/.editorconfig new file mode 100755 index 0000000..b58478e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +[*] +end_of_line = lf + +[*.{lua,spec}] +charset = utf8 +indent_size = 4 +indent_style = tab +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eba1110 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..b955d14 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,25 @@ +name: luacheck + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + + workflow_dispatch: + +jobs: + main: + runs-on: ubuntu-latest + + steps: + # Checks-out your repository under $GITHUB_WORKSPACE + - uses: actions/checkout@v2 + - name: luacheck + uses: nebularg/actions-luacheck@v1.1.0 + #with: + #files: # optional, default is . + #path: # optional, default is ${{ github.workspace }} + #args: # optional + #config: # optional + #annotate: # optional, default is none \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b683fc0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.mods/formspec_edit/formspec.spec \ No newline at end of file diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..0ae26d3 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,47 @@ +max_line_length = false + +read_globals = { + "DIR_DELIM", "INIT", + + "minetest", "core", + "dump", "dump2", + + "Raycast", + "Settings", + "PseudoRandom", + "PerlinNoise", + "VoxelManip", + "SecureRandom", + "VoxelArea", + "PerlinNoiseMap", + "PcgRandom", + "ItemStack", + "AreaStore", + + "vector", + + table = { + fields = { + "copy", + "indexof", + "insert_all", + "key_value_swap", + "shuffle", + } + }, + + string = { + fields = { + "split", + "trim", + } + }, + + math = { + fields = { + "hypot", + "sign", + "factorial" + } + }, +} \ No newline at end of file diff --git a/README.md b/README.md index b1fd341..74c209d 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,21 @@ ## Getting Started +This mod needs access to an insecure environment in order to work. + +Add `formspec_edit` to `secure.trusted_mods` in your `minetest.conf` file. + The file *formspec.spec* in your: ```minetest_folder/games/formspec_editor/mods/formspec_edit``` -contains a formspec you can edit and see updates of in real time. -Simply add the game to MT, add ```formspec_edit``` to your ```secure.trusted_mods``` settings, -load up a level of *Formspec Editor*, and you will be greeted with the *formspec.spec* formspec. +contains a formspec you can edit and see updates of in real time. (You can also specify a file location with the `formspec_editor.file_path` setting.) +Simply add the game to MT, load up a level of *Formspec Editor*, and you will be greeted with the formspec. -- To make edits, open the file (formspec.spec) in your editor of choice and make changes as you see fit. When you hit save, the formspec will auto-update. Best when used side by side. +- To make edits, open the file in your editor of choice and make changes as you see fit. When you hit save, the formspec will auto-update. Best when used side by side. - To exit just hit or use a button_exit[] button. Both send the fields.quit message. - You can test with images if you want, adding a "textures" folder to the formspec_edit gamemod folder, otherwise images will default to random colors. - +- You can show the formspec with builtin, minetest_game or mineclone2 styling (see the `formspec_editor.style` setting) +- You can change the refresh rate by changing the `formspec_editor.update_time` setting diff --git a/mods/formspec_edit/init.lua b/mods/formspec_edit/init.lua index 484acd5..7467797 100644 --- a/mods/formspec_edit/init.lua +++ b/mods/formspec_edit/init.lua @@ -9,111 +9,165 @@ minetest.register_alias("mapgen_water_source", "air") --Variables local modpath = minetest.get_modpath("formspec_edit") + local insecure_env = minetest.request_insecure_environment() -local auto_update_time = 0.2 --seconds +if not insecure_env then + error("[formspec_editor] Cannot access insecure environment!") +end + +local io = insecure_env.io +local update_time = tonumber(minetest.settings:get("formspec_editor.update_time")) or 0.2 + +--Load provided file if present +local filepath = minetest.settings:get("formspec_editor.file_path") +if not filepath or filepath == "" then + filepath = modpath .. "/formspec.spec" +end + +--Get styling presets +local styling = minetest.settings:get("formspec_editor.style") +if not styling or styling == "" then + styling = "builtin" +end + local error_formspec = [[ formspec_version[4] size[8,2] -position[0.5,0.5] label[0.375,0.5;Error:formspec.spec is either ] label[0.375,1;non-existent,or empty] ]] ---function declarations -local update_formspec = nil -local load_formspec = nil -local auto_update = nil -local turn_off_hud = nil +--Crash if not singleplayer +--TODO: hide the 'Host server' checkbox in main menu then possible +if not minetest.is_singleplayer() then + error("[formspec_editor] This game doesn't work in multiplayer!") +end + +--function definitions + +----------------------------------- +--load_formspec() +----------------------------------- +local function load_formspec() + local file = io.open(filepath, "rb") + if file == nil then + return error_formspec + else + local content = file:read("*all") + file:close() + if content == nil then + return error_formspec + else + return content + end + end +end + +----------------------------------- +--update_formspec() +----------------------------------- +local function update_formspec(player_name) + minetest.after(0.1, function(name) + minetest.show_formspec(name, "fs", load_formspec()) + end, player_name) +end + +----------------------------------- +--apply_styling() +----------------------------------- +local function apply_styling(player_ref) + local prepend = "" + if styling == "minetest_game" then + ------------------------------------------------ + ---------------Minetest Game code--------------- + ------------------------------------------------ + prepend = [[ + bgcolor[#080808BB;true] + listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF] + ]] + local name = player_ref:get_player_name() + local info = minetest.get_player_information(name) + if info.formspec_version > 1 then + prepend = prepend .. "background9[5,5;1,1;mtg_gui_formbg.png;true;10]" + else + prepend = prepend .. "background[5,5;1,1;mtg_gui_formbg.png;true]" + end + elseif styling == "mineclone2" then + ------------------------------------------------ + ---------------MineClone2 code------------------ + ------------------------------------------------ + --Sadly, this code doesn't support inventory slot styling (texture based) + prepend = "listcolors[#9990;#FFF7;#FFF0;#000;#FFF]".. + "style_type[image_button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]".. + "style_type[button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]".. + "style_type[field;textcolor=#323232]".. + "style_type[label;textcolor=#323232]".. + "style_type[textarea;textcolor=#323232]".. + "style_type[checkbox;textcolor=#323232]".. + "bgcolor[#00000000]".. + "background9[1,1;1,1;mcl_base_textures_background9.png;true;7]" + end + player_ref:set_formspec_prepend(prepend) +end + +----------------------------------- +--turn_off_hud() +----------------------------------- +local function turn_off_hud(player_ref) + player_ref:hud_set_flags({ + hotbar = false, + healthbar = false, + crosshair = false, + wielditem = false, + breathbar = false, + minimap = false, + minimap_radar = false, + }) +end + +----------------------------------- +--set_sky() +----------------------------------- +local function set_sky(player_ref) + player_ref:set_sky({ + base_color = "#AAF", + type = "plain", + clouds = false, + }) + player_ref:set_stars({visible = false}) + player_ref:set_sun({visible = false}) + player_ref:set_moon({visible = false}) + player_ref:override_day_night_ratio(0) +end --Registrations ----------------------------------- --on_joinplayer() ----------------------------------- -minetest.register_on_joinplayer( -function(player_ref,_) - auto_update(player_ref:get_player_name()) - turn_off_hud(player_ref) - set_sky(player_ref) -end -) +minetest.register_on_joinplayer(function(player_ref,_) + apply_styling(player_ref) + turn_off_hud(player_ref) + set_sky(player_ref) +end) ----------------------------------- --on_player_receive_fields() ----------------------------------- -minetest.register_on_player_receive_fields( -function(player_ref, _, fields) - if(fields.quit) then - minetest.request_shutdown() - end - update_formspec(player_ref:get_player_name()) -end -) +minetest.register_on_player_receive_fields(function(player_ref, _, fields) + if fields.quit then + minetest.request_shutdown() + end + update_formspec(player_ref:get_player_name()) +end) ---function definitions - ------------------------------------ ---auto_update() ------------------------------------ -auto_update = function(player_name) - update_formspec(player_name) - minetest.after(auto_update_time,auto_update,player_name) -end - ------------------------------------ ---update_formspec() ------------------------------------ -update_formspec = function(player_name) - minetest.after(0.1, - function(player_name) - minetest.show_formspec(player_name, "fs", load_formspec()) - end, - player_name) -end - ------------------------------------ ---load_formspec() ------------------------------------ -load_formspec = function() - local io = insecure_env.io - local file = io.open(modpath .. "/formspec.spec", "rb") - if file == nil then - return error_formspec - else - local content = file:read("*all") - file:close() - if content == nil then - return error_formspec - else - return content - end - end -end - ------------------------------------ ---turn_off_hud() ------------------------------------ -turn_off_hud = function(player_ref) - local flags = { - hotbar = false, - healthbar = false, - crosshair = false, - wielditem = false, - breathbar = false, - minimap = false, - minimap_radar = false, - } - player_ref:hud_set_flags(flags) -end - ------------------------------------ ---set_sky() ------------------------------------ -set_sky = function(player_ref) - local sky = { - base_color = "#AAF", - type = "plain", - clouds = false, - } - player_ref:set_sky(sky) - player_ref:override_day_night_ratio(0) -end \ No newline at end of file +local time = 0 +minetest.register_globalstep(function(dtime) + time = time + dtime + if time >= update_time then + local player = minetest.get_connected_players()[1] --The game isn't supposed to work in multiplayer + if player then + update_formspec(player:get_player_name()) + end + time = 0 + end +end) diff --git a/mods/formspec_edit/textures/CREDITS.md b/mods/formspec_edit/textures/CREDITS.md new file mode 100644 index 0000000..15fba50 --- /dev/null +++ b/mods/formspec_edit/textures/CREDITS.md @@ -0,0 +1,9 @@ +These textures are copied from Minetest Game and MineClone2 projects + +# Minetest Game (CC BY-SA 3.0) +mtg_gui_formbg.png + +# MineClone2 (CC BY-SA 3.0) +mcl_inventory_button9.png +mcl_inventory_button9_pressed.png +mcl_base_textures_background9.png \ No newline at end of file diff --git a/mods/formspec_edit/textures/mcl2/mcl_base_textures_background9.png b/mods/formspec_edit/textures/mcl2/mcl_base_textures_background9.png new file mode 100644 index 0000000..e5d693e Binary files /dev/null and b/mods/formspec_edit/textures/mcl2/mcl_base_textures_background9.png differ diff --git a/mods/formspec_edit/textures/mcl2/mcl_inventory_button9.png b/mods/formspec_edit/textures/mcl2/mcl_inventory_button9.png new file mode 100644 index 0000000..aab1601 Binary files /dev/null and b/mods/formspec_edit/textures/mcl2/mcl_inventory_button9.png differ diff --git a/mods/formspec_edit/textures/mcl2/mcl_inventory_button9_pressed.png b/mods/formspec_edit/textures/mcl2/mcl_inventory_button9_pressed.png new file mode 100644 index 0000000..aeaea13 Binary files /dev/null and b/mods/formspec_edit/textures/mcl2/mcl_inventory_button9_pressed.png differ diff --git a/mods/formspec_edit/textures/mtg/mtg_gui_formbg.png b/mods/formspec_edit/textures/mtg/mtg_gui_formbg.png new file mode 100644 index 0000000..e259fec Binary files /dev/null and b/mods/formspec_edit/textures/mtg/mtg_gui_formbg.png differ diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..fe15730 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,8 @@ +# Formspec update time (in seconds) +formspec_editor.update_time (Update time) float 0.2 + +# Path of the formspec file +formspec_editor.file_path (File path) filepath + +# Style type +formspec_editor.style (Formspec style) enum builtin builtin,minetest_game,mineclone2 \ No newline at end of file