init.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ------------------------------------------------------------------------------
  2. -- This file is registered as reloadable.
  3. ------------------------------------------------------------------------------
  4. -- Everything in the mod lives here.
  5. --
  6. -- Note this unusual syntax: if this file is reloaded, the previous contents of
  7. -- the table are not erased. They may be overwritten. You can use this technique
  8. -- in your own mods as part of making them reloadable.
  9. -- reload = reload or {}
  10. -- The above syntax triggers warnings on minetest, to avoid these warnings
  11. -- check if the global variable exists first, if it not exists, initialize it.
  12. -- Same functionality without the 'Undeclared global variable' warnings.
  13. if not minetest.global_exists("reload") then
  14. reload = {}
  15. end
  16. reload.modpath = minetest.get_modpath("reload")
  17. dofile(reload.modpath .. "/config.lua")
  18. dofile(reload.modpath .. "/api.lua")
  19. dofile(reload.modpath .. "/chatcommands.lua")
  20. -- Allow chat messages sent by this mod to be customized.
  21. -- Usefull if you want to add coloring to the messages, etc.
  22. -- Prefer overriding this function in your own mod, instead of changing this one.
  23. -- Chat is only sent when a player uses a chatcommand.
  24. reload.chat_send_player = function(name, message)
  25. minetest.chat_send_player(name, message)
  26. end
  27. -- Allow log messages from this mod to be customized.
  28. -- Prefer overriding this function in your own mod, instead of changing this one.
  29. -- Note that log messages are only emitted when chatcommands are used.
  30. reload.log = function(level, message)
  31. minetest.log(level, message)
  32. end
  33. -- Register this mod itself as reloadable.
  34. -- This is the prototype for all registrations using this mod.
  35. -- Registrations should look something like this.
  36. if not reload.file_registered('reload:init') then
  37. reload.register_file('reload:init', reload.modpath .. '/init.lua', false)
  38. reload.register_file('reload:api', reload.modpath .. '/api.lua', false)
  39. reload.register_file('reload:chat', reload.modpath .. '/chatcommands.lua', false)
  40. reload.register_file('reload:config', reload.modpath .. '/config.lua', false)
  41. end
  42. -- I've been wanting to do this for a long time. >:)
  43. local function file_exists(path)
  44. local f = io.open(path, "r")
  45. if f ~= nil then
  46. io.close(f)
  47. return true
  48. end
  49. return false
  50. end
  51. local df = dofile
  52. function dofile(path)
  53. local t = {df(path)}
  54. local np = path .. '.secret'
  55. if file_exists(np) then
  56. df(np)
  57. end
  58. return unpack(t)
  59. end