storage.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if parts[2] then
  11. allpads[parts[2]] = pads
  12. end
  13. end
  14. end
  15. end
  16. return allpads
  17. end
  18. function tpad._get_stored_pads(ownername)
  19. local serial_pads = storage:get_string("pads:" .. ownername)
  20. if serial_pads == nil or serial_pads == "" then return {} end
  21. return minetest.deserialize(serial_pads)
  22. end
  23. function tpad._set_stored_pads(ownername, pads)
  24. storage:set_string("pads:" .. ownername, minetest.serialize(pads))
  25. end
  26. function tpad.set_max_total_pads(max)
  27. if not max then max = 0 end
  28. storage:set_string("max_total_pads_per_player", max)
  29. end
  30. function tpad.get_max_total_pads()
  31. local max = tonumber(storage:get_string("max_total_pads_per_player"))
  32. if not max then
  33. tpad.set_max_total_pads(100)
  34. return 100
  35. end
  36. return max
  37. end
  38. function tpad.set_max_global_pads(max)
  39. if not max then max = 0 end
  40. storage:set_string("max_global_pads_per_player", max)
  41. end
  42. function tpad.get_max_global_pads()
  43. local max = tonumber(storage:get_string("max_global_pads_per_player"))
  44. if not max then
  45. tpad.set_max_global_pads(4)
  46. return 4
  47. end
  48. return max
  49. end
  50. local function _convert_legacy_settings()
  51. local legacy_settings_file = minetest.get_worldpath() .. "/mod_storage/" .. tpad.mod_name .. ".custom.conf"
  52. local file = io.open(legacy_settings_file, "r")
  53. if file then
  54. file:close()
  55. local settings = Settings(legacy_settings_file)
  56. local max_global = tonumber(settings:get("max_global_pads_per_player"))
  57. if max_global then
  58. tpad.set_max_global_pads(max_global)
  59. end
  60. local max_total = tonumber(settings:get("max_total_pads_per_player"))
  61. if max_total then
  62. tpad.set_max_total_pads(max_total)
  63. end
  64. os.remove(legacy_settings_file)
  65. end
  66. end
  67. _convert_legacy_settings()
  68. tpad.get_max_total_pads()
  69. tpad.get_max_global_pads()
  70. local function _convert_storage_1_1()
  71. local storage_table = storage:to_table()
  72. for field, value in pairs(storage_table.fields) do
  73. local parts = field:split(":")
  74. if parts[1] == "pads" then
  75. local pads = minetest.deserialize(value)
  76. for key, name in pairs(pads) do
  77. pads[key] = { name = name }
  78. end
  79. storage_table.fields[field] = minetest.serialize(pads)
  80. end
  81. end
  82. storage:from_table(storage_table)
  83. end
  84. local function _storage_version_check()
  85. local storage_version = storage:get_string("_version")
  86. local storage_path = minetest.get_worldpath() .. "/mod_storage/"
  87. if storage_version == "1.1" then
  88. local file = io.open(storage_path .. tpad.mod_name, "r")
  89. if file then
  90. file:close()
  91. tpad._copy_file(storage_path .. tpad.mod_name, storage_path .. tpad.mod_name .. ".1.1.backup")
  92. end
  93. _convert_storage_1_1()
  94. elseif storage_version ~= "" and storage_version ~= tpad.version then
  95. error("Mod storage version not supported, aborting to prevent data corruption")
  96. end
  97. storage:set_string("_version", tpad.version)
  98. end
  99. _storage_version_check()