init.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --
  2. -- This file contains built-in stuff in Minetest implemented in Lua.
  3. --
  4. -- It is always loaded and executed after registration of the C API,
  5. -- before loading and running any mods.
  6. --
  7. -- Initialize some very basic things
  8. function core.error_handler(err, level)
  9. return debug.traceback(tostring(err), level)
  10. end
  11. do
  12. local function concat_args(...)
  13. local n, t = select("#", ...), {...}
  14. for i = 1, n do
  15. t[i] = tostring(t[i])
  16. end
  17. return table.concat(t, "\t")
  18. end
  19. function core.debug(...) core.log(concat_args(...)) end
  20. if core.print then
  21. local core_print = core.print
  22. -- Override native print and use
  23. -- terminal if that's turned on
  24. function print(...) core_print(concat_args(...)) end
  25. core.print = nil -- don't pollute our namespace
  26. end
  27. end
  28. do
  29. -- Note that PUC Lua just calls srand() which is already initialized by C++,
  30. -- but we don't want to rely on this implementation detail.
  31. local seed = 1048576 * (os.time() % 1048576)
  32. seed = seed + core.get_us_time() % 1048576
  33. math.randomseed(seed)
  34. end
  35. minetest = core
  36. -- Load other files
  37. local scriptdir = core.get_builtin_path()
  38. local commonpath = scriptdir .. "common" .. DIR_DELIM
  39. local asyncpath = scriptdir .. "async" .. DIR_DELIM
  40. dofile(commonpath .. "math.lua")
  41. dofile(commonpath .. "vector.lua")
  42. dofile(commonpath .. "strict.lua")
  43. dofile(commonpath .. "serialize.lua")
  44. dofile(commonpath .. "misc_helpers.lua")
  45. if INIT == "game" then
  46. dofile(scriptdir .. "game" .. DIR_DELIM .. "init.lua")
  47. assert(not core.get_http_api)
  48. elseif INIT == "mainmenu" then
  49. local mm_script = core.settings:get("main_menu_script")
  50. local custom_loaded = false
  51. if mm_script and mm_script ~= "" then
  52. local testfile = io.open(mm_script, "r")
  53. if testfile then
  54. testfile:close()
  55. dofile(mm_script)
  56. custom_loaded = true
  57. core.log("info", "Loaded custom main menu script: "..mm_script)
  58. else
  59. core.log("error", "Failed to load custom main menu script: "..mm_script)
  60. core.log("info", "Falling back to default main menu script")
  61. end
  62. end
  63. if not custom_loaded then
  64. dofile(core.get_mainmenu_path() .. DIR_DELIM .. "init.lua")
  65. end
  66. elseif INIT == "async" then
  67. dofile(asyncpath .. "mainmenu.lua")
  68. elseif INIT == "async_game" then
  69. dofile(commonpath .. "metatable.lua")
  70. dofile(asyncpath .. "game.lua")
  71. elseif INIT == "client" then
  72. dofile(scriptdir .. "client" .. DIR_DELIM .. "init.lua")
  73. elseif INIT == "emerge" then
  74. dofile(scriptdir .. "emerge" .. DIR_DELIM .. "init.lua")
  75. else
  76. error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT)))
  77. end