storage.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. local storage = minetest.get_mod_storage()
  2. function tpad._get_all_pads()
  3. local storage_table = storage:to_table()
  4. local allpads = {}
  5. for key, value in pairs(storage_table.fields) do
  6. local parts = key:split(":")
  7. if parts[1] == "pads" then
  8. local pads = minetest.deserialize(value)
  9. if type(pads) == "table" then
  10. allpads[parts[2]] = pads
  11. end
  12. end
  13. end
  14. return allpads
  15. end
  16. function tpad._get_stored_pads(ownername)
  17. local serial_pads = storage:get_string("pads:" .. ownername)
  18. if serial_pads == nil or serial_pads == "" then return {} end
  19. return minetest.deserialize(serial_pads)
  20. end
  21. function tpad._set_stored_pads(ownername, pads)
  22. storage:set_string("pads:" .. ownername, minetest.serialize(pads))
  23. end
  24. function tpad._storage_sanity_check()
  25. local storage_version = storage:get_string("_version")
  26. local storage_path = minetest.get_worldpath() .. "/mod_storage/"
  27. if storage_version == "1.1" then
  28. tpad._copy_file(storage_path .. tpad.mod_name, storage_path .. tpad.mod_name .. ".1.1.backup")
  29. tpad._convert_storage_1_1()
  30. elseif storage_version ~= "" and storage_version ~= tpad.version then
  31. error("Mod storage version not supported, aborting to prevent data corruption")
  32. end
  33. storage:set_string("_version", tpad.version)
  34. end
  35. function tpad._convert_storage_1_1()
  36. local storage_table = storage:to_table()
  37. for field, value in pairs(storage_table.fields) do
  38. local parts = field:split(":")
  39. if parts[1] == "pads" then
  40. local pads = minetest.deserialize(value)
  41. for key, name in pairs(pads) do
  42. pads[key] = { name = name }
  43. end
  44. storage_table.fields[field] = minetest.serialize(pads)
  45. end
  46. end
  47. storage:from_table(storage_table)
  48. end
  49. tpad._storage_sanity_check()