From ff994a34f2dd072b16f0550bbdbde2a97ca0d90d Mon Sep 17 00:00:00 2001 From: luk3yx Date: Mon, 7 Jan 2019 21:45:41 +1300 Subject: [PATCH] Add JSON-based export string format. --- README.md | 2 +- init.lua | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index efd20ed..26c4a7f 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ create HUDs on their own. - `.mrkr`: Opens a formspec allowing you to display or delete markers. - `.add_mrkr`: Adds markers. You can use `.add_mrkr x,y,z Marker name` to add markers. Markers are (currently) cross-server, and adding a marker with (exactly) the same name as another will overwrite the original marker. If you replace `x,y,z` with `here`, the marker will be set to your current position, and replacing it with `there` will set the marker to the last `.coords` position. - - `.mrkr_export`: Exports your markers to an advmarkers string. Remember to not modify the text before copying it. + - `.mrkr_export`: Exports your markers to an advmarkers string. Remember to not modify the text before copying it. You can use `.mrkr_export old` if you want an export string compatible with older versions of advmarkers (it should start with `M` instead of `J`). The old format will probably not work nicely with the planned server-side mod, however. - `.mrkr_import`: Imports your markers from an advmarkers string (`.mrkr_import `). Any markers with the same name will not be overwritten, and if they do not have the same co-ordinates, `_` will be appended to the imported one. - `.mrkrthere`: Sets a marker at the last `.coords` position. diff --git a/init.lua b/init.lua index 49ff805..77377aa 100644 --- a/init.lua +++ b/init.lua @@ -76,9 +76,12 @@ end -- Export markers function advmarkers.export(raw) local s = storage:to_table().fields - if not raw then + if raw == 'M' then s = minetest.compress(minetest.serialize(s)) s = 'M' .. minetest.encode_base64(s) + elseif not raw then + s = minetest.compress(minetest.write_json(s)) + s = 'J' .. minetest.encode_base64(s) end return s end @@ -86,11 +89,16 @@ end -- Import markers function advmarkers.import(s) if type(s) ~= 'table' then - if s:sub(1, 1) ~= 'M' then return false, 'No M' end + local ver = s:sub(1, 1) + if ver ~= 'M' and ver ~= 'J' then return end s = minetest.decode_base64(s:sub(2)) local success, msg = pcall(minetest.decompress, s) if not success then return end - s = minetest.deserialize(msg) + if ver == 'M' then + s = minetest.deserialize(msg) + else + s = minetest.parse_json(msg) + end end -- Iterate over markers to preserve existing ones and check for errors. @@ -327,12 +335,18 @@ end) -- Allow string exporting minetest.register_chatcommand('mrkr_export', { - params = '', + params = '[old]', description = 'Exports an advmarkers string containing all your markers.', func = function(param) + local export + if param == 'old' then + export = advmarkers.export('M') + else + export = advmarkers.export() + end minetest.show_formspec('advmarkers-ignore', 'field[_;Your marker export string;' .. - minetest.formspec_escape(advmarkers.export()) .. ']') + minetest.formspec_escape(export) .. ']') end })