Skip to content

Commit

Permalink
Improve the formspec dev function
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Dec 23, 2023
1 parent 23fc531 commit dac7b11
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions mods/apis/ctf_gui/dev.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
local unset_function = "[f]\nreturn "

function ctf_gui.show_formspec_dev(player, formname, formspec, formcontext)
local filepath = minetest.get_worldpath().."/ctf_gui/"
local filename = filepath.."file_edit.txt"
local slower_loop = false

minetest.chat_send_all("Started formspec editing file at "..filename)

minetest.mkdir(filepath)

local file = assert(io.open(filename, "w"))

file:write(formspec)

if type(formspec) ~= "function" then
file:write(formspec)
else
file:write(unset_function)
end
file:close()

local function interval()
if formspec:sub(1, 3) == "[f]" then
local result, form = pcall(loadstring(formspec:sub(4)), formcontext)
ctf_gui.show_formspec(player, formname, result and form or "")
if type(formspec) == "function" then
ctf_gui.show_formspec(player, formname, formspec(formcontext))
elseif formspec:sub(1, 3) == "[f]" then
local result, form = pcall((loadstring(formspec:sub(4)) or function() return function() end end)(), formcontext)

ctf_gui.show_formspec(player, formname,
result and form or "size[10,10]hypertext[0,0;10,10;err;"..minetest.formspec_escape(form or "").."]"
)

slower_loop = not result
else
ctf_gui.show_formspec(player, formname, formspec)
end

minetest.after(1, function()
minetest.after(slower_loop and 3 or 1, function()
local f = assert(io.open(filename, "r"))
local new_form = f:read("*a")

formspec = f:read("*a")
if new_form ~= unset_function then
formspec = new_form
end

f:close()

if formspec:match("^exit") then
if type(formspec) == "function" or not formspec:match("^exit") then
interval()
else
minetest.request_shutdown("Formspec dev requested shutdown", true)
Expand Down

0 comments on commit dac7b11

Please sign in to comment.