LuaBlock: Major changes in mod storage usage

Data associated to a LuaBlock is redundantly stored in both its metadata
and in the mod storage so that it is not possible to hack a LuaBlock by
changing its metadata or copying it using WorldEdit. This commit changes
how it is done:
* Store full code instead of storing MD5 checksum, which avoids bug
caused by serializing non-ASCII strings using minetest.serialize,
* Also store owner,
* Use `minetest.hash_node_position` instead of deprecated vector_extras
set_data_to_pos etc,
* Remove mod storage data associated with the LuaBlock when destroying
it,
* Create helper functions for manipulating the mod storage in
moremesecons_utils, I will soon update other parts of MoreMesecons as well to use them.

This commit breaks backwards compatibility: previously created LuaBlocks
will have to be reset by opening the formspec and pressing the `Submit`
button.

This commit also fixes an unrelated bug: code in the formspec was not
correctly escaped using minetest.formspec_escape.
This commit is contained in:
upsilon
2020-04-09 16:13:51 +02:00
parent 2b2faeca02
commit 7cf99da1a5
12 changed files with 50 additions and 652 deletions

View File

@ -28,6 +28,30 @@ function moremesecons.setting(modname, settingname, default, min)
end
end
-- Storage helpers
function moremesecons.get_storage_data(storage, name)
return {
tab = minetest.deserialize(storage:get_string(name)) or {},
name = name,
storage = storage
}
end
function moremesecons.set_data_to_pos(sto, pos, data)
sto.tab[minetest.hash_node_position(pos)] = data
sto.storage:set_string(sto.name, minetest.serialize(sto.tab))
end
function moremesecons.get_data_from_pos(sto, pos)
return sto.tab[minetest.hash_node_position(pos)]
end
function moremesecons.remove_data_from_pos(sto, pos)
sto.tab[minetest.hash_node_position(pos)] = nil
sto.storage:set_string(sto.name, minetest.serialize(sto.tab))
end
-- Vector helpers
-- All the following functions are from the vector_extras mod (https://github.com/HybridDog/vector_extras).
-- If you enable that mod, its functions will be used instead of the ones defined below