added export (show .json file structure)

This commit is contained in:
Sokomine 2023-10-29 15:47:56 +01:00
parent 6346a747e0
commit cf868e0761
4 changed files with 123 additions and 0 deletions

102
fs_export.lua Normal file
View File

@ -0,0 +1,102 @@
yl_speak_up.input_fs_export = function(player, formname, fields)
if(fields and fields.back) then
return yl_speak_up.show_fs(player, "talk")
elseif(fields and fields.show_readable) then
return yl_speak_up.show_fs(player, "export", "show_readable")
elseif(fields and fields.show_export) then
return yl_speak_up.show_fs(player, "export", "show_export")
elseif(fields and fields.show_simple_dialogs) then
return yl_speak_up.show_fs(player, "export", "show_simple_dialogs")
end
end
yl_speak_up.get_fs_export = function(player, param)
local pname = player:get_player_name()
-- generic dialogs are not part of the NPC
local dialog = yl_speak_up.speak_to[pname].dialog
local n_id = yl_speak_up.speak_to[pname].n_id
local content = ""
local explanation = ""
local b1 = "button[0.2,17.2;6.0,0.9;show_readable;Human readable format]"..
"tooltip[show_readable;Shows the raw dialog data format formatted diffrently so\n"..
"that it is easier to read.]"
local b2 = "button[7.0,17.2;6.0,0.9;show_export;Export in .json format]"..
"tooltip[show_export;Shows the raw dialog data format (.json).]"
local b3 = "button[13.8,17.2;6.0,0.9;show_simple_dialogs;Show simple dialogs format]"..
"tooltip[show_simple_dialogs;Show the dialog data structure in the format used by\n"..
"the mod \"simple_dialogs\".]"
if(param and param == "show_readable") then
b1 = "label[0.2,17.6;This is human readable format]"
explanation = "This is like the raw dialog data format - except that it's written a bit "..
"diffrent so that it is easier to read."
content = minetest.write_json(dialog, true)
elseif(param and param == "show_simple_dialogs") then
b3 = "label[13.8,17.6;This is simple dialogs format]"
explanation = "This is the format used by the \"simple_dialogs\" mod. "..
"It does not cover preconditions, actions, effects and other specialities of "..
"this mod here. It only covers the raw dialogs. If a dialog line starts with "..
"\":\", \">\" or \"=\", a \" \" is added before that letter because such a "..
"line start would not be allowed in \"simple_dialogs\"."
local tmp = {}
local d_liste = {}
for d_id, v in pairs(dialog.n_dialogs) do
table.insert(d_liste, d_id)
end
table.sort(d_liste)
for i, d_id in ipairs(d_liste) do
table.insert(tmp, "===")
table.insert(tmp, tostring(d_id))
table.insert(tmp, "\n")
-- :, > and = are not allowed as line start in simple dialogs
-- just add a leading blank so that any :, > and = at the start are covered
table.insert(tmp, " ")
local t = dialog.n_dialogs[d_id].d_text or ""
t = string.gsub(t, "\n([:>=])", "\n %1")
table.insert(tmp, t)
table.insert(tmp, "\n")
for o_id, o_data in pairs(dialog.n_dialogs[d_id].d_options or {}) do
local target_dialog = nil
for r_id, r_data in pairs(o_data.o_results) do
if(r_data.r_type and r_data.r_type == "dialog") then
target_dialog = r_data.r_value
end
end
table.insert(tmp, ">")
table.insert(tmp, target_dialog or "d_1")
table.insert(tmp, ":")
table.insert(tmp, o_data.o_text_when_prerequisites_met)
table.insert(tmp, "\n")
end
table.insert(tmp, "\n")
end
content = table.concat(tmp, "")
else
b2 = "label[7.0,17.6;This is export in .json format]"
explanation = "Mark the text in the above window with your mouse and paste it into "..
"a local file on your disk. Save it as n_<id>.json (i.e. n_123.json) "..
"in the folder containing your dialog data. Then the NPCs in your "..
"local world can use this dialog."
-- TODO: import?
content = minetest.write_json(dialog, false)
end
return table.concat({"size[20,20]label[4,0.5;Export of NPC ",
minetest.formspec_escape(n_id or "- ? -"),
"dialog data in .json format]",
"button[17.8,0.2;2.0,0.9;back;Back]",
b1, b2, b3,
-- background color for the textarea below
"box[0.2,2;19.6,15;#AAAAAA]",
-- read-only
"textarea[0.2,2;19.6,15;;Current dialog of ",
minetest.formspec_escape(n_id or "- ? -"),
":;",
minetest.formspec_escape(content),
"]",
"textarea[0.2,18.2;19.6,1.8;;;",
explanation,
"]",
})
end

View File

@ -120,6 +120,11 @@ yl_speak_up.input_talk = function(player, formname, fields)
return
end
if(edit_mode and fields.button_export_dialog) then
yl_speak_up.show_fs(player, "export")
return
end
-- start edit mode (requires npc_talk_owner)
if fields.button_start_edit_mode then
-- check if this particular NPC is really owned by this player or if the player has global privs
@ -406,6 +411,12 @@ yl_speak_up.get_fs_talkdialog_main_text_in_edit_mode = function(
"Save",
"Save this dialog.",
true)
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "17.9,0.3;2,0.9", "button_export_dialog",
"Export",
"Export: Show the dialog in .json format which you can copy and store on your computer.",
true)
-- static help text instead of text input field for d_got_item
if(c_d_id == "d_got_item") then

View File

@ -233,6 +233,8 @@ yl_speak_up.reload = function(modpath, log_entry)
dofile(modpath .. "chat_commands.lua")
-- creating and maintaining quests
dofile(modpath .. "fs_quest_gui.lua")
-- export dialog for cut&paste in .json format
dofile(modpath .. "fs_export.lua")
-- show a list of all NPC the player can edit
dofile(modpath .. "fs_npc_list.lua")

View File

@ -123,6 +123,10 @@ yl_speak_up.input_handler = function(player, formname, fields)
elseif formname == "yl_speak_up:quest_gui" then
yl_speak_up.input_quest_gui(player, formname, fields)
return true
-- export to .json format for cut&paste
elseif formname == "yl_speak_up:export" then
yl_speak_up.input_fs_export(player, formname, fields)
return true
end
end
@ -434,6 +438,10 @@ yl_speak_up.show_fs = function(player, fs_name, param)
yl_speak_up.show_fs_ver(pname, "yl_speak_up:quest_gui",
yl_speak_up.get_fs_quest_gui(player, param))
elseif(fs_name == "export") then
yl_speak_up.show_fs_ver(pname, "yl_speak_up:export",
yl_speak_up.get_fs_export(player, param))
-- fallback in case of wrong call
else
minetest.chat_send_player(pname, "Error: Trying to show wrong "..