]> git.lizzy.rs Git - minetest.git/blob - builtin/init.lua
Use a Lua error handler that calls tostring (#11913)
[minetest.git] / builtin / init.lua
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
8 -- Initialize some very basic things
9 function core.error_handler(err, level)
10         return debug.traceback(tostring(err), level)
11 end
12 do
13         local function concat_args(...)
14                 local n, t = select("#", ...), {...}
15                 for i = 1, n do
16                         t[i] = tostring(t[i])
17                 end
18                 return table.concat(t, "\t")
19         end
20         function core.debug(...) core.log(concat_args(...)) end
21         if core.print then
22                 local core_print = core.print
23                 -- Override native print and use
24                 -- terminal if that's turned on
25                 function print(...) core_print(concat_args(...)) end
26                 core.print = nil -- don't pollute our namespace
27         end
28 end
29 math.randomseed(os.time())
30 minetest = core
31
32 -- Load other files
33 local scriptdir = core.get_builtin_path()
34 local gamepath = scriptdir .. "game" .. DIR_DELIM
35 local clientpath = scriptdir .. "client" .. DIR_DELIM
36 local commonpath = scriptdir .. "common" .. DIR_DELIM
37 local asyncpath = scriptdir .. "async" .. DIR_DELIM
38
39 dofile(commonpath .. "vector.lua")
40 dofile(commonpath .. "strict.lua")
41 dofile(commonpath .. "serialize.lua")
42 dofile(commonpath .. "misc_helpers.lua")
43
44 if INIT == "game" then
45         dofile(gamepath .. "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(asyncpath .. "game.lua")
69 elseif INIT == "client" then
70         dofile(clientpath .. "init.lua")
71 else
72         error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT)))
73 end