api.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ------------------------------------------------------------------------------
  2. -- This file is registered as reloadable.
  3. ------------------------------------------------------------------------------
  4. if not minetest.global_exists("reload") then reload = {} end
  5. reload.impl = reload.impl or {}
  6. reload.impl.files = reload.impl.files or {}
  7. reload.file_registered = function(id)
  8. assert(type(id) == "string") -- Foolproof us!
  9. if reload.impl.files[id] then return true else return false end
  10. end
  11. reload.register_file = function(id, file, noload)
  12. -- Foolproof us!
  13. assert(type(id) == "string")
  14. assert(type(file) == "string")
  15. -- This file cannot already have been registered.
  16. assert(reload.impl.files[id] == nil)
  17. -- Execute the file once. Registering the file also executes it, so doing this
  18. -- is the equivalent of using 'dofile', but with the added benefit of being
  19. -- able to execute the file again and again as needed.
  20. if noload == true or noload == nil then dofile(file) end
  21. reload.impl.files[id] = file
  22. end
  23. -- Check if file exists, and register/execute it only if it does.
  24. reload.register_optional = function(id, path)
  25. -- Foolproof us!
  26. assert(type(id) == "string")
  27. assert(type(path) == "string")
  28. -- This file cannot already have been registered.
  29. assert(reload.impl.files[id] == nil)
  30. local file = io.open(path)
  31. if file then
  32. -- File exists, we can execute it.
  33. reload.impl.files[id] = path
  34. dofile(path)
  35. end
  36. end