Add JSON-based export string format.

master
luk3yx 2019-01-07 21:45:41 +13:00
parent ca02d2b79c
commit ff994a34f2
2 changed files with 20 additions and 6 deletions

View File

@ -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 <advmarkers string>`). 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.

View File

@ -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
})