2
0

init.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 .. "vector.lua")
  41. dofile(commonpath .. "strict.lua")
  42. dofile(commonpath .. "serialize.lua")
  43. dofile(commonpath .. "misc_helpers.lua")
  44. if INIT == "game" then
  45. dofile(scriptdir .. "game" .. DIR_DELIM .. "init.lua")
  46. assert(not core.get_http_api)
  47. elseif INIT == "mainmenu" then
  48. local mm_script = core.settings:get("main_menu_script")
  49. local custom_loaded = false
  50. if mm_script and mm_script ~= "" then
  51. local testfile = io.open(mm_script, "r")
  52. if testfile then
  53. testfile:close()
  54. dofile(mm_script)
  55. custom_loaded = true
  56. core.log("info", "Loaded custom main menu script: "..mm_script)
  57. else
  58. core.log("error", "Failed to load custom main menu script: "..mm_script)
  59. core.log("info", "Falling back to default main menu script")
  60. end
  61. end
  62. if not custom_loaded then
  63. dofile(core.get_mainmenu_path() .. DIR_DELIM .. "init.lua")
  64. end
  65. elseif INIT == "async" then
  66. dofile(asyncpath .. "mainmenu.lua")
  67. elseif INIT == "async_game" then
  68. dofile(commonpath .. "metatable.lua")
  69. dofile(asyncpath .. "game.lua")
  70. elseif INIT == "client" then
  71. dofile(scriptdir .. "client" .. DIR_DELIM .. "init.lua")
  72. elseif INIT == "emerge" then
  73. dofile(scriptdir .. "emerge" .. DIR_DELIM .. "init.lua")
  74. else
  75. error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT)))
  76. end