From 019e7ad8e2732fadc19356e541957bd78ece8d56 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Fri, 10 Sep 2021 19:23:27 +0200 Subject: [PATCH] base lua based form def --- README.md | 2 +- mods/formspec_edit/formspec.lua | 1 + mods/formspec_edit/formspec.spec | 9 - mods/formspec_edit/game_specific.lua | 123 +++++++++++ mods/formspec_edit/init.lua | 194 ++++++++++-------- .../textures/mcl2/mcl_formspec_itemslot.png | Bin 0 -> 231 bytes .../textures/mtg/gui_furnace_arrow_bg.png | Bin 0 -> 324 bytes .../textures/mtg/gui_furnace_arrow_fg.png | Bin 0 -> 251 bytes mods/formspec_edit/textures/mtg/gui_hb_bg.png | Bin 0 -> 98 bytes .../formspec_edit/textures/mtg/gui_hotbar.png | Bin 0 -> 411 bytes .../textures/mtg/gui_hotbar_selected.png | Bin 0 -> 175 bytes 11 files changed, 238 insertions(+), 91 deletions(-) create mode 100644 mods/formspec_edit/formspec.lua delete mode 100644 mods/formspec_edit/formspec.spec create mode 100644 mods/formspec_edit/game_specific.lua create mode 100644 mods/formspec_edit/textures/mcl2/mcl_formspec_itemslot.png create mode 100644 mods/formspec_edit/textures/mtg/gui_furnace_arrow_bg.png create mode 100644 mods/formspec_edit/textures/mtg/gui_furnace_arrow_fg.png create mode 100644 mods/formspec_edit/textures/mtg/gui_hb_bg.png create mode 100644 mods/formspec_edit/textures/mtg/gui_hotbar.png create mode 100644 mods/formspec_edit/textures/mtg/gui_hotbar_selected.png diff --git a/README.md b/README.md index 74c209d..f562871 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ 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: +The file *formspec.lua* in your: ```minetest_folder/games/formspec_editor/mods/formspec_edit``` diff --git a/mods/formspec_edit/formspec.lua b/mods/formspec_edit/formspec.lua new file mode 100644 index 0000000..9b17512 --- /dev/null +++ b/mods/formspec_edit/formspec.lua @@ -0,0 +1 @@ +return "size[5,10]" \ No newline at end of file diff --git a/mods/formspec_edit/formspec.spec b/mods/formspec_edit/formspec.spec deleted file mode 100644 index 92cff8f..0000000 --- a/mods/formspec_edit/formspec.spec +++ /dev/null @@ -1,9 +0,0 @@ -size[9,8.75] -label[0,0;Ender Chest] -list[current_player;enderchest;0,0.5;9,3;] -mcl_formspec.get_itemslot_bg(0,0.5,9,3) -label[0,4.0;Inventory] -list[current_player;main;0,4.5;9,3;9] -list[current_player;main;0,7.74;9,1;] -listring[current_player;enderchest] -listring[current_player;main] \ No newline at end of file diff --git a/mods/formspec_edit/game_specific.lua b/mods/formspec_edit/game_specific.lua new file mode 100644 index 0000000..005b344 --- /dev/null +++ b/mods/formspec_edit/game_specific.lua @@ -0,0 +1,123 @@ +formspec_edit.prepend = "" +formspec_edit.inventories = { + main = {width = 8, size = 32} +} + +--Get formspec style +local styling = minetest.settings:get("formspec_editor.style") +if not styling or styling == "" then + styling = "builtin" +end + +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 + + formspec_edit.lua_env.default = {} + function formspec_edit.lua_env.default.get_hotbar_bg(x, y) + local out = "" + for i=0,7,1 do + out = out .."image["..x+i..","..y..";1,1;gui_hb_bg.png]" + end + return out + end +elseif styling == "mineclone2" then + ------------------------------------------------ + ---------------MineClone2 code------------------ + ------------------------------------------------ + --Sadly, this code doesn't support inventory slot styling (texture based) + prepend = table.concat({ + "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]", + }) + + --Add custom inventory function to lua env + formspec_edit.lua_env.mcl_formspec = {} + function formspec_edit.lua_env.mcl_formspec.get_itemslot_bg(x, y, w, h) + local out = "" + for i = 0, w - 1, 1 do + for j = 0, h - 1, 1 do + out = out .."image["..x+i..","..y+j..";1,1;mcl_formspec_itemslot.png]" + end + end + return out + end + + --Same as above, but for v4 forms + function mcl_formspec.get_itemslot_bg_v4(x, y, w, h) + local out = "" + for i = 0, w - 1, 1 do + for j = 0, h - 1, 1 do + out = out .."image["..x+i+(i*0.25)..","..y+j+(j*0.25)..";1,1;mcl_formspec_itemslot.png]" + end + end + return out + end + + --Add mcl2 color codes + formspec_edit.lua_env.mcl_colors = { + BLACK = "#000000", + DARK_BLUE = "#0000AA", + DARK_GREEN = "#00AA00", + DARK_AQUA = "#00AAAA", + DARK_RED = "#AA0000", + DARK_PURPLE = "#AA00AA", + GOLD = "#FFAA00", + GRAY = "#AAAAAA", + DARK_GRAY = "#555555", + BLUE = "#5555FF", + GREEN = "#55FF55", + AQUA = "#55FFFF", + RED = "#FF5555", + LIGHT_PURPLE = "#FF55FF", + YELLOW = "#FFFF55", + WHITE = "#FFFFFF", + background = { + BLACK = "#000000", + DARK_BLUE = "#00002A", + DARK_GREEN = "#002A00", + DARK_AQUA = "#002A2A", + DARK_RED = "#2A0000", + DARK_PURPLE = "#2A002A", + GOLD = "#2A2A00", + GRAY = "#2A2A2A", + DARK_GRAY = "#151515", + BLUE = "#15153F", + GREEN = "#153F15", + AQUA = "#153F3F", + RED = "#3F1515", + LIGHT_PURPLE = "#3F153F", + YELLOW = "#3F3F15", + WHITE = "#373501", + } + } + + --Add mcl2 inventory lists + formspec_edit.inventories = { + main = {width = 9, size = 36}, + armor = {size = 5}, + craft = {width = 3, size = 9}, + } +end + +formspec_edit.prepend = prepend \ No newline at end of file diff --git a/mods/formspec_edit/init.lua b/mods/formspec_edit/init.lua index 382a9a2..6c6c09f 100644 --- a/mods/formspec_edit/init.lua +++ b/mods/formspec_edit/init.lua @@ -3,6 +3,8 @@ --------------------------------- -----------By ExeVirus----------- +formspec_edit = {} + --Fix builtin minetest.register_alias("mapgen_stone", "air") minetest.register_alias("mapgen_water_source", "air") @@ -19,47 +21,110 @@ 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 +--Get provided modpath 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" + filepath = modpath .. "/formspec.lua" end local error_formspec = [[ formspec_version[4] size[8,2] -label[0.375,0.5;Error:formspec.spec is either ] +label[0.375,0.5;Error: formspec.lua is either] label[0.375,1;non-existent,or empty] ]] --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 +----------------------------------- +--error_handler() +----------------------------------- +local function error_handler(msg) + return string.format([[ + formspec_version[4] + size[8,2] + label[0.375,0.5;Error: %s] + ]], msg) +end + + +--Define the basic lua env (only contains safe functions) +formspec_edit.lua_env = { + print = print, --can be useful for debugging + dump = dump, + dump2 = dump2, + table = table, + string = string, + math = math, + vector = vector, + F = minetest.formspec_escape, + C = minetest.colorize, + CE = minetest.get_color_escape_sequence, + S = function(str, ...) --oversimplificated version of minetest.translate + local arg = {n=select('#', ...), ...} + local arg_index = 1 + local translated = str:gsub("@(.)", function(matched) + local c = string.byte(matched) + if string.byte("1") <= c and c <= string.byte("9") then + local a = c - string.byte("0") + if a ~= arg_index then + return str + end + if a > arg.n then + return str + end + arg_index = arg_index + 1 + return arg[a] + elseif matched == "n" then + return "\n" + else + return matched + end + end) + return translated + end, +} + +dofile(modpath.."/game_specific.lua") + --function definitions ------------------------------------ ---load_formspec() ------------------------------------ -local function load_formspec() +--------------------------------- +--check_updates() +--------------------------------- + +local cached_file = "" + +local function check_updates() + --attemp to open the file local file = io.open(filepath, "rb") if file == nil then return error_formspec else + --read file content local content = file:read("*all") file:close() + + --validate content if content == nil then return error_formspec else - return content + if content == cached_file then + return + else + cached_file = content + --load code + local func = load(content, "formspec_editor", "bt", formspec_edit.lua_env) + local state, msg = pcall(func) + if not state then + return error_handler(msg) + end + print(dump(msg)) + return msg or "size[8,2]" + end end end end @@ -68,54 +133,35 @@ 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]" + local new = check_updates() + if new then + minetest.after(0.1, function(name) + minetest.show_formspec(name, "fs", new) + end, player_name) end - player_ref:set_formspec_prepend(prepend) end + +--Registrations + ----------------------------------- ---turn_off_hud() +--on_joinplayer() ----------------------------------- -local function turn_off_hud(player_ref) - player_ref:hud_set_flags({ +minetest.register_on_joinplayer(function(player,_) + --Apply formspec prepend + player:set_formspec_prepend(formspec_edit.prepend) + + --Create inventories + local inv = player:get_inventory() + for name,def in pairs(formspec_edit.inventories) do + inv:set_size(name, def.size) + if def.width then + inv:set_width(name, def.width) + end + end + + --Hide builtin HUD elements + player:hud_set_flags({ hotbar = false, healthbar = false, crosshair = false, @@ -124,41 +170,27 @@ local function turn_off_hud(player_ref) minimap = false, minimap_radar = false, }) -end ------------------------------------ ---set_sky() ------------------------------------ -local function set_sky(player_ref) - player_ref:set_sky({ + --Update sky + player: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,_) - apply_styling(player_ref) - turn_off_hud(player_ref) - set_sky(player_ref) + player:set_stars({visible = false}) + player:set_sun({visible = false}) + player:set_moon({visible = false}) + player:override_day_night_ratio(0) end) + ----------------------------------- --on_player_receive_fields() ----------------------------------- -minetest.register_on_player_receive_fields(function(player_ref, _, fields) +minetest.register_on_player_receive_fields(function(player, _, fields) if fields.quit then minetest.request_shutdown() end - update_formspec(player_ref:get_player_name()) + update_formspec(player:get_player_name()) end) local time = 0 diff --git a/mods/formspec_edit/textures/mcl2/mcl_formspec_itemslot.png b/mods/formspec_edit/textures/mcl2/mcl_formspec_itemslot.png new file mode 100644 index 0000000000000000000000000000000000000000..84958ecd4234850d627aa868c382df5d322d0f8c GIT binary patch literal 231 zcmeAS@N?(olHy`uVBq!ia0vp^ULefD1SBVK3RMGAY)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPVvoM#DxVok4SD;Xdr;B4q#jUruH}WzVaxfVFn|k*~ zL)YKu9tTAn_c3L4&(b$ePQEKESik1onN?cXtMzTj1$%Vo{uG$^vZ}w7(f&Q#{=Tga RPC)w^JYD@<);T3K0RZ;UL=6A{ literal 0 HcmV?d00001 diff --git a/mods/formspec_edit/textures/mtg/gui_furnace_arrow_bg.png b/mods/formspec_edit/textures/mtg/gui_furnace_arrow_bg.png new file mode 100644 index 0000000000000000000000000000000000000000..046d8cda098534b25bc2dfa550ca3e705a29acf2 GIT binary patch literal 324 zcmeAS@N?(olHy`uVBq!ia0vp^h9Jzs3?v&j#pnYm^#Gp`S0GIgL`6lFUygqcRL4^i z1nwZs?dFV>hg_f193psmo7Gj+uHYL+^95}4hjoTS3j3^P6^StyP{hGr3|NFN++$3=5{P*~d z=(mEQ+xDE#kgAM3l5Y4|+@xMH!zo#Cr2tRtWi5V3WvQ+eTn9Jk=5{G%IMfQJDoSix z9;?$-bwyOL`@th+*;7sd?YETneX;xJvwW4nl|{=s_WbxTRd1!hl?A&v;x{U}bOEY1>t% z&wZ1kZhiBlU)iH0zj*pxELG)7#S2+z%_zIIkXrU99dzaFhx*Nkm+>L z3bF>|G$gkm*$NUx^#+oIacfOT)AclGW;kF03