Allows admins to run code snippets without crashing the server as often.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

persistence.lua 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. --
  2. -- Persistent snippets
  3. --
  4. -- Get storage
  5. local storage = ...
  6. assert(storage)
  7. -- Load persistent snippets
  8. local register_snippet_raw = snippets.register_snippet
  9. do
  10. for name, def in pairs(storage:to_table().fields) do
  11. if name:sub(1, 1) == '>' then
  12. def = minetest.deserialize(def)
  13. if def then
  14. def.persistent = true
  15. register_snippet_raw(name:sub(2), def)
  16. end
  17. end
  18. end
  19. end
  20. -- Override snippets.register_snippet so it accepts the "persistent" field.
  21. function snippets.register_snippet(name, def)
  22. if def == nil and type(name) == 'table' then
  23. name, def = name.name, name
  24. end
  25. -- Fix tracebacks
  26. local good, msg = pcall(register_snippet_raw, name, def)
  27. if not good then error(msg, 2) end
  28. if not msg then return msg end
  29. -- Check for def.persistent
  30. def = snippets.registered_snippets[name]
  31. if type(def) == 'table' and def.persistent and def.code then
  32. print('Saving snippet', name)
  33. storage:set_string('>' .. name, minetest.serialize({
  34. code = def.code,
  35. owner = def.owner,
  36. }))
  37. end
  38. -- Return the same value as register_snippet_raw.
  39. return msg
  40. end
  41. -- Override snippets.unregister_snippet
  42. local unregister_snippet_raw = snippets.unregister_snippet
  43. function snippets.unregister_snippet(name)
  44. local def = snippets.registered_snippets[name]
  45. if def and def.persistent then
  46. storage:set_string('>' .. name, '')
  47. end
  48. return unregister_snippet_raw(name)
  49. end