From 1cd512913e4d4ad1fb43d4b6e3d7971bb6c67528 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sun, 27 Apr 2014 17:55:49 -0400 Subject: [PATCH] Organize builtin into subdirectories --- builtin/async/init.lua | 18 +++++ builtin/async_env.lua | 21 ------ builtin/async_event.lua | 45 ------------- builtin/builtin.lua | 32 --------- builtin/common/async_event.lua | 42 ++++++++++++ builtin/{ => common}/misc_helpers.lua | 0 builtin/{ => common}/serialize.lua | 0 builtin/{ => common}/vector.lua | 0 builtin/{ => game}/auth.lua | 0 builtin/{ => game}/chatcommands.lua | 0 builtin/{ => game}/deprecated.lua | 0 builtin/{ => game}/detached_inventory.lua | 0 builtin/{ => game}/falling.lua | 2 +- builtin/{ => game}/features.lua | 0 builtin/{ => game}/forceloading.lua | 0 builtin/game/init.lua | 23 +++++++ builtin/{ => game}/item.lua | 0 builtin/{ => game}/item_entity.lua | 2 +- builtin/{ => game}/misc.lua | 0 builtin/{ => game}/privileges.lua | 0 .../{misc_register.lua => game/register.lua} | 0 builtin/{ => game}/statbars.lua | 0 builtin/{ => game}/static_spawn.lua | 0 builtin/{ => game}/voxelarea.lua | 0 builtin/init.lua | 34 ++++++++++ builtin/{ => mainmenu}/filterlist.lua | 0 builtin/{ => mainmenu}/gamemgr.lua | 0 builtin/{mainmenu.lua => mainmenu/init.lua} | 23 +++---- .../{mm_menubar.lua => mainmenu/menubar.lua} | 0 builtin/{ => mainmenu}/modmgr.lua | 0 builtin/{ => mainmenu}/modstore.lua | 0 .../textures.lua} | 0 src/defaultsettings.cpp | 6 +- src/guiEngine.cpp | 65 ++++++------------- src/script/cpp_api/s_async.cpp | 14 ++-- src/script/cpp_api/s_base.cpp | 20 ++---- src/script/lua_api/l_mainmenu.cpp | 4 +- src/script/lua_api/l_mainmenu.h | 2 +- src/script/lua_api/l_server.cpp | 8 +-- src/script/lua_api/l_util.cpp | 14 ++++ src/script/lua_api/l_util.h | 3 + src/script/scripting_game.cpp | 9 +++ src/script/scripting_mainmenu.cpp | 6 ++ src/server.cpp | 16 ++--- 44 files changed, 206 insertions(+), 203 deletions(-) create mode 100644 builtin/async/init.lua delete mode 100644 builtin/async_env.lua delete mode 100644 builtin/async_event.lua delete mode 100644 builtin/builtin.lua create mode 100644 builtin/common/async_event.lua rename builtin/{ => common}/misc_helpers.lua (100%) rename builtin/{ => common}/serialize.lua (100%) rename builtin/{ => common}/vector.lua (100%) rename builtin/{ => game}/auth.lua (100%) rename builtin/{ => game}/chatcommands.lua (100%) rename builtin/{ => game}/deprecated.lua (100%) rename builtin/{ => game}/detached_inventory.lua (100%) rename builtin/{ => game}/falling.lua (99%) rename builtin/{ => game}/features.lua (100%) rename builtin/{ => game}/forceloading.lua (100%) create mode 100644 builtin/game/init.lua rename builtin/{ => game}/item.lua (100%) rename builtin/{ => game}/item_entity.lua (98%) rename builtin/{ => game}/misc.lua (100%) rename builtin/{ => game}/privileges.lua (100%) rename builtin/{misc_register.lua => game/register.lua} (100%) rename builtin/{ => game}/statbars.lua (100%) rename builtin/{ => game}/static_spawn.lua (100%) rename builtin/{ => game}/voxelarea.lua (100%) create mode 100644 builtin/init.lua rename builtin/{ => mainmenu}/filterlist.lua (100%) rename builtin/{ => mainmenu}/gamemgr.lua (100%) rename builtin/{mainmenu.lua => mainmenu/init.lua} (98%) rename builtin/{mm_menubar.lua => mainmenu/menubar.lua} (100%) rename builtin/{ => mainmenu}/modmgr.lua (100%) rename builtin/{ => mainmenu}/modstore.lua (100%) rename builtin/{mm_textures.lua => mainmenu/textures.lua} (100%) diff --git a/builtin/async/init.lua b/builtin/async/init.lua new file mode 100644 index 00000000..79b1a0a6 --- /dev/null +++ b/builtin/async/init.lua @@ -0,0 +1,18 @@ +engine.log("info", "Initializing Asynchronous environment") + +local core = engine or minetest + +function core.job_processor(serialized_func, serialized_param) + local func = loadstring(serialized_func) + local param = core.deserialize(serialized_param) + local retval = nil + + if type(func) == "function" then + retval = core.serialize(func(param)) + else + core.log("error", "ASYNC WORKER: Unable to deserialize function") + end + + return retval or core.serialize(nil) +end + diff --git a/builtin/async_env.lua b/builtin/async_env.lua deleted file mode 100644 index cdcb82ee..00000000 --- a/builtin/async_env.lua +++ /dev/null @@ -1,21 +0,0 @@ -engine.log("info", "Initializing Asynchronous environment") -local tbl = engine or minetest - -minetest = tbl -dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua") -dofile(SCRIPTDIR .. DIR_DELIM .. "misc_helpers.lua") - -function tbl.job_processor(serialized_func, serialized_param) - local func = loadstring(serialized_func) - local param = tbl.deserialize(serialized_param) - local retval = nil - - if type(func) == "function" then - retval = tbl.serialize(func(param)) - else - tbl.log("error", "ASYNC WORKER: Unable to deserialize function") - end - - return retval or tbl.serialize(nil) -end - diff --git a/builtin/async_event.lua b/builtin/async_event.lua deleted file mode 100644 index 2c3fb8fa..00000000 --- a/builtin/async_event.lua +++ /dev/null @@ -1,45 +0,0 @@ -local tbl = engine or minetest - -local SCRIPTDIR = SCRIPTDIR or tbl.get_scriptdir() -minetest = tbl -dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua") - -tbl.async_jobs = {} - -local function handle_job(jobid, serialized_retval) - local retval = tbl.deserialize(serialized_retval) - assert(type(tbl.async_jobs[jobid]) == "function") - tbl.async_jobs[jobid](retval) - tbl.async_jobs[jobid] = nil -end - -if engine ~= nil then - tbl.async_event_handler = handle_job -else - minetest.register_globalstep(function(dtime) - for i, job in ipairs(tbl.get_finished_jobs()) do - handle_job(job.jobid, job.retval) - end - end) -end - -function tbl.handle_async(func, parameter, callback) - -- Serialize function - local serialized_func = string.dump(func) - - assert(serialized_func ~= nil) - - -- Serialize parameters - local serialized_param = tbl.serialize(parameter) - - if serialized_param == nil then - return false - end - - local jobid = tbl.do_async_callback(serialized_func, serialized_param) - - tbl.async_jobs[jobid] = callback - - return true -end - diff --git a/builtin/builtin.lua b/builtin/builtin.lua deleted file mode 100644 index 1babe006..00000000 --- a/builtin/builtin.lua +++ /dev/null @@ -1,32 +0,0 @@ --- --- This file contains built-in stuff in Minetest implemented in Lua. --- --- It is always loaded and executed after registration of the C API, --- before loading and running any mods. --- - --- Initialize some very basic things -print = minetest.debug -math.randomseed(os.time()) -os.setlocale("C", "numeric") - --- Load other files -local modpath = minetest.get_modpath("__builtin") -dofile(modpath.."/serialize.lua") -dofile(modpath.."/misc_helpers.lua") -dofile(modpath.."/item.lua") -dofile(modpath.."/misc_register.lua") -dofile(modpath.."/item_entity.lua") -dofile(modpath.."/deprecated.lua") -dofile(modpath.."/misc.lua") -dofile(modpath.."/privileges.lua") -dofile(modpath.."/auth.lua") -dofile(modpath.."/chatcommands.lua") -dofile(modpath.."/static_spawn.lua") -dofile(modpath.."/detached_inventory.lua") -dofile(modpath.."/falling.lua") -dofile(modpath.."/features.lua") -dofile(modpath.."/voxelarea.lua") -dofile(modpath.."/vector.lua") -dofile(modpath.."/forceloading.lua") -dofile(modpath.."/statbars.lua") diff --git a/builtin/common/async_event.lua b/builtin/common/async_event.lua new file mode 100644 index 00000000..ef4bf435 --- /dev/null +++ b/builtin/common/async_event.lua @@ -0,0 +1,42 @@ + +local core = engine or minetest + +core.async_jobs = {} + +local function handle_job(jobid, serialized_retval) + local retval = core.deserialize(serialized_retval) + assert(type(core.async_jobs[jobid]) == "function") + core.async_jobs[jobid](retval) + core.async_jobs[jobid] = nil +end + +if engine ~= nil then + core.async_event_handler = handle_job +else + minetest.register_globalstep(function(dtime) + for i, job in ipairs(core.get_finished_jobs()) do + handle_job(job.jobid, job.retval) + end + end) +end + +function core.handle_async(func, parameter, callback) + -- Serialize function + local serialized_func = string.dump(func) + + assert(serialized_func ~= nil) + + -- Serialize parameters + local serialized_param = core.serialize(parameter) + + if serialized_param == nil then + return false + end + + local jobid = core.do_async_callback(serialized_func, serialized_param) + + core.async_jobs[jobid] = callback + + return true +end + diff --git a/builtin/misc_helpers.lua b/builtin/common/misc_helpers.lua similarity index 100% rename from builtin/misc_helpers.lua rename to builtin/common/misc_helpers.lua diff --git a/builtin/serialize.lua b/builtin/common/serialize.lua similarity index 100% rename from builtin/serialize.lua rename to builtin/common/serialize.lua diff --git a/builtin/vector.lua b/builtin/common/vector.lua similarity index 100% rename from builtin/vector.lua rename to builtin/common/vector.lua diff --git a/builtin/auth.lua b/builtin/game/auth.lua similarity index 100% rename from builtin/auth.lua rename to builtin/game/auth.lua diff --git a/builtin/chatcommands.lua b/builtin/game/chatcommands.lua similarity index 100% rename from builtin/chatcommands.lua rename to builtin/game/chatcommands.lua diff --git a/builtin/deprecated.lua b/builtin/game/deprecated.lua similarity index 100% rename from builtin/deprecated.lua rename to builtin/game/deprecated.lua diff --git a/builtin/detached_inventory.lua b/builtin/game/detached_inventory.lua similarity index 100% rename from builtin/detached_inventory.lua rename to builtin/game/detached_inventory.lua diff --git a/builtin/falling.lua b/builtin/game/falling.lua similarity index 99% rename from builtin/falling.lua rename to builtin/game/falling.lua index 7ac348bd..93d17221 100644 --- a/builtin/falling.lua +++ b/builtin/game/falling.lua @@ -4,7 +4,7 @@ -- Falling stuff -- -minetest.register_entity("__builtin:falling_node", { +minetest.register_entity(":__builtin:falling_node", { initial_properties = { physical = true, collide_with_objects = false, diff --git a/builtin/features.lua b/builtin/game/features.lua similarity index 100% rename from builtin/features.lua rename to builtin/game/features.lua diff --git a/builtin/forceloading.lua b/builtin/game/forceloading.lua similarity index 100% rename from builtin/forceloading.lua rename to builtin/game/forceloading.lua diff --git a/builtin/game/init.lua b/builtin/game/init.lua new file mode 100644 index 00000000..b878a266 --- /dev/null +++ b/builtin/game/init.lua @@ -0,0 +1,23 @@ + +local scriptpath = minetest.get_builtin_path()..DIR_DELIM +local commonpath = scriptpath.."common"..DIR_DELIM +local gamepath = scriptpath.."game"..DIR_DELIM + +dofile(commonpath.."vector.lua") + +dofile(gamepath.."item.lua") +dofile(gamepath.."register.lua") +dofile(gamepath.."item_entity.lua") +dofile(gamepath.."deprecated.lua") +dofile(gamepath.."misc.lua") +dofile(gamepath.."privileges.lua") +dofile(gamepath.."auth.lua") +dofile(gamepath.."chatcommands.lua") +dofile(gamepath.."static_spawn.lua") +dofile(gamepath.."detached_inventory.lua") +dofile(gamepath.."falling.lua") +dofile(gamepath.."features.lua") +dofile(gamepath.."voxelarea.lua") +dofile(gamepath.."forceloading.lua") +dofile(gamepath.."statbars.lua") + diff --git a/builtin/item.lua b/builtin/game/item.lua similarity index 100% rename from builtin/item.lua rename to builtin/game/item.lua diff --git a/builtin/item_entity.lua b/builtin/game/item_entity.lua similarity index 98% rename from builtin/item_entity.lua rename to builtin/game/item_entity.lua index 0dcc2dc2..8150e6da 100644 --- a/builtin/item_entity.lua +++ b/builtin/game/item_entity.lua @@ -8,7 +8,7 @@ function minetest.spawn_item(pos, item) return obj end -minetest.register_entity("__builtin:item", { +minetest.register_entity(":__builtin:item", { initial_properties = { hp_max = 1, physical = true, diff --git a/builtin/misc.lua b/builtin/game/misc.lua similarity index 100% rename from builtin/misc.lua rename to builtin/game/misc.lua diff --git a/builtin/privileges.lua b/builtin/game/privileges.lua similarity index 100% rename from builtin/privileges.lua rename to builtin/game/privileges.lua diff --git a/builtin/misc_register.lua b/builtin/game/register.lua similarity index 100% rename from builtin/misc_register.lua rename to builtin/game/register.lua diff --git a/builtin/statbars.lua b/builtin/game/statbars.lua similarity index 100% rename from builtin/statbars.lua rename to builtin/game/statbars.lua diff --git a/builtin/static_spawn.lua b/builtin/game/static_spawn.lua similarity index 100% rename from builtin/static_spawn.lua rename to builtin/game/static_spawn.lua diff --git a/builtin/voxelarea.lua b/builtin/game/voxelarea.lua similarity index 100% rename from builtin/voxelarea.lua rename to builtin/game/voxelarea.lua diff --git a/builtin/init.lua b/builtin/init.lua new file mode 100644 index 00000000..9969111b --- /dev/null +++ b/builtin/init.lua @@ -0,0 +1,34 @@ +-- +-- This file contains built-in stuff in Minetest implemented in Lua. +-- +-- It is always loaded and executed after registration of the C API, +-- before loading and running any mods. +-- + +local core = minetest or engine +minetest = core + +-- Initialize some very basic things +print = core.debug +math.randomseed(os.time()) +os.setlocale("C", "numeric") + +-- Load other files +local scriptdir = core.get_builtin_path()..DIR_DELIM +local gamepath = scriptdir.."game"..DIR_DELIM +local commonpath = scriptdir.."common"..DIR_DELIM +local asyncpath = scriptdir.."async"..DIR_DELIM + +dofile(commonpath.."serialize.lua") +dofile(commonpath.."misc_helpers.lua") + +if INIT == "game" then + dofile(gamepath.."init.lua") +elseif INIT == "mainmenu" then + dofile(core.get_mainmenu_path()..DIR_DELIM.."init.lua") +elseif INIT == "async" then + dofile(asyncpath.."init.lua") +else + error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT))) +end + diff --git a/builtin/filterlist.lua b/builtin/mainmenu/filterlist.lua similarity index 100% rename from builtin/filterlist.lua rename to builtin/mainmenu/filterlist.lua diff --git a/builtin/gamemgr.lua b/builtin/mainmenu/gamemgr.lua similarity index 100% rename from builtin/gamemgr.lua rename to builtin/mainmenu/gamemgr.lua diff --git a/builtin/mainmenu.lua b/builtin/mainmenu/init.lua similarity index 98% rename from builtin/mainmenu.lua rename to builtin/mainmenu/init.lua index 67e0113b..d78a668c 100644 --- a/builtin/mainmenu.lua +++ b/builtin/mainmenu/init.lua @@ -1,8 +1,14 @@ -print = engine.debug -math.randomseed(os.time()) -os.setlocale("C", "numeric") -local scriptpath = engine.get_scriptdir() +local menupath = engine.get_mainmenu_path()..DIR_DELIM +local commonpath = engine.get_builtin_path()..DIR_DELIM.."common"..DIR_DELIM + +dofile(menupath.."filterlist.lua") +dofile(menupath.."modmgr.lua") +dofile(menupath.."modstore.lua") +dofile(menupath.."gamemgr.lua") +dofile(menupath.."textures.lua") +dofile(menupath.."menubar.lua") +dofile(commonpath.."async_event.lua") mt_color_grey = "#AAAAAA" mt_color_blue = "#0000DD" @@ -11,15 +17,6 @@ mt_color_dark_green = "#003300" --for all other colors ask sfan5 to complete his worK! -dofile(scriptpath .. DIR_DELIM .. "misc_helpers.lua") -dofile(scriptpath .. DIR_DELIM .. "filterlist.lua") -dofile(scriptpath .. DIR_DELIM .. "modmgr.lua") -dofile(scriptpath .. DIR_DELIM .. "modstore.lua") -dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua") -dofile(scriptpath .. DIR_DELIM .. "mm_textures.lua") -dofile(scriptpath .. DIR_DELIM .. "mm_menubar.lua") -dofile(scriptpath .. DIR_DELIM .. "async_event.lua") - menu = {} local tabbuilder = {} local worldlist = nil diff --git a/builtin/mm_menubar.lua b/builtin/mainmenu/menubar.lua similarity index 100% rename from builtin/mm_menubar.lua rename to builtin/mainmenu/menubar.lua diff --git a/builtin/modmgr.lua b/builtin/mainmenu/modmgr.lua similarity index 100% rename from builtin/modmgr.lua rename to builtin/mainmenu/modmgr.lua diff --git a/builtin/modstore.lua b/builtin/mainmenu/modstore.lua similarity index 100% rename from builtin/modstore.lua rename to builtin/mainmenu/modstore.lua diff --git a/builtin/mm_textures.lua b/builtin/mainmenu/textures.lua similarity index 100% rename from builtin/mm_textures.lua rename to builtin/mainmenu/textures.lua diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 3c17e650..945804ba 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -269,9 +269,9 @@ void set_default_settings(Settings *settings) settings->setDefault("enable_ipv6", "true"); settings->setDefault("ipv6_server", "false"); - settings->setDefault("main_menu_script",""); - settings->setDefault("main_menu_mod_mgr","1"); - settings->setDefault("main_menu_game_mgr","0"); + settings->setDefault("main_menu_path", ""); + settings->setDefault("main_menu_mod_mgr", "1"); + settings->setDefault("main_menu_game_mgr", "0"); settings->setDefault("modstore_download_url", "https://forum.minetest.net/media/"); settings->setDefault("modstore_listmods_url", "https://forum.minetest.net/mmdb/mods/"); settings->setDefault("modstore_details_url", "https://forum.minetest.net/mmdb/mod/*/"); diff --git a/src/guiEngine.cpp b/src/guiEngine.cpp index e32d629d..ef018021 100644 --- a/src/guiEngine.cpp +++ b/src/guiEngine.cpp @@ -169,17 +169,16 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, m_formspecgui = new FormspecFormSource(""); /* Create menu */ - m_menu = - new GUIFormSpecMenu( m_device, - m_parent, - -1, - m_menumanager, - 0 /* &client */, - 0 /* gamedef */, - m_texture_source, - m_formspecgui, - m_buttonhandler, - NULL); + m_menu = new GUIFormSpecMenu(m_device, + m_parent, + -1, + m_menumanager, + NULL /* &client */, + NULL /* gamedef */, + m_texture_source, + m_formspecgui, + m_buttonhandler, + NULL); m_menu->allowClose(false); m_menu->lockSize(true,v2u32(800,600)); @@ -216,43 +215,21 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, /******************************************************************************/ bool GUIEngine::loadMainMenuScript() { - // Try custom menu script (main_menu_script) + // Try custom menu script (main_menu_path) - std::string menuscript = g_settings->get("main_menu_script"); - if(menuscript != "") { - m_scriptdir = fs::RemoveLastPathComponent(menuscript); - - if(m_script->loadMod(menuscript, "__custommenu")) { - // custom menu script loaded - return true; - } - else { - infostream - << "GUIEngine: execution of custom menu: \"" - << menuscript << "\" failed!" - << std::endl - << "\tfalling back to builtin menu" - << std::endl; - } + m_scriptdir = g_settings->get("main_menu_path"); + if (m_scriptdir.empty()) { + m_scriptdir = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "mainmenu"; } - // Try builtin menu script (main_menu_script) - - std::string builtin_menuscript = - porting::path_share + DIR_DELIM + "builtin" - + DIR_DELIM + "mainmenu.lua"; - - m_scriptdir = fs::RemoveRelativePathComponents( - fs::RemoveLastPathComponent(builtin_menuscript)); - - if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) { - // builtin menu script loaded + std::string script = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "init.lua"; + if (m_script->loadScript(script)) { + // Menu script loaded return true; - } - else { - errorstream - << "GUIEngine: unable to load builtin menu" - << std::endl; + } else { + infostream + << "GUIEngine: execution of menu script in: \"" + << m_scriptdir << "\" failed!" << std::endl; } return false; diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 4feed3e5..64260fb3 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -26,6 +26,7 @@ extern "C" { #include "lualib.h" } +#include "server.h" #include "s_async.h" #include "log.h" #include "filesys.h" @@ -233,9 +234,9 @@ AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher, lua_pushstring(L, DIR_DELIM); lua_setglobal(L, "DIR_DELIM"); - lua_pushstring(L, - (porting::path_share + DIR_DELIM + "builtin").c_str()); - lua_setglobal(L, "SCRIPTDIR"); + // Push builtin initialization type + lua_pushstring(L, "async"); + lua_setglobal(L, "INIT"); jobDispatcher->prepareEnvironment(L, top); } @@ -258,17 +259,16 @@ void* AsyncWorkerThread::Thread() porting::setThreadName((std::string("AsyncWorkTh_") + number).c_str()); - std::string asyncscript = porting::path_share + DIR_DELIM + "builtin" - + DIR_DELIM + "async_env.lua"; + lua_State *L = getStack(); - if (!loadScript(asyncscript)) { + std::string script = getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua"; + if (!loadScript(script)) { errorstream << "AsyncWorkderThread execution of async base environment failed!" << std::endl; abort(); } - lua_State *L = getStack(); // Main loop while (!StopRequested()) { // Wait for job diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 774b3f51..1a172fd3 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -88,9 +88,9 @@ ScriptApiBase::ScriptApiBase() lua_pop(m_luastack, 1); #endif - m_server = 0; - m_environment = 0; - m_guiengine = 0; + m_server = NULL; + m_environment = NULL; + m_guiengine = NULL; } ScriptApiBase::~ScriptApiBase() @@ -103,24 +103,14 @@ bool ScriptApiBase::loadMod(const std::string &scriptpath, { ModNameStorer modnamestorer(getStack(), modname); - if(!string_allowed(modname, MODNAME_ALLOWED_CHARS)){ + if (!string_allowed(modname, MODNAME_ALLOWED_CHARS)) { errorstream<<"Error loading mod \""<getBuiltinLuaPath(); - lua_pushstring(L, path.c_str()); - return 1; - } const ModSpec *mod = getServer(L)->getModSpec(modname); - if(!mod){ + if (!mod) { lua_pushnil(L); return 1; } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index f26a88a9..b30bab29 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_content.h" #include "cpp_api/s_async.h" #include "debug.h" +#include "porting.h" #include "log.h" #include "tool.h" #include "settings.h" @@ -274,6 +275,14 @@ int ModApiUtil::l_is_yes(lua_State *L) return 1; } +int ModApiUtil::l_get_builtin_path(lua_State *L) +{ + std::string path = porting::path_share + DIR_DELIM + "builtin"; + lua_pushstring(L, path.c_str()); + return 1; +} + + void ModApiUtil::Initialize(lua_State *L, int top) { API_FCT(debug); @@ -294,6 +303,8 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_password_hash); API_FCT(is_yes); + + API_FCT(get_builtin_path); } void ModApiUtil::InitializeAsync(AsyncEngine& engine) @@ -311,4 +322,7 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(write_json); ASYNC_API_FCT(is_yes); + + ASYNC_API_FCT(get_builtin_path); } + diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 13357587..d72978dc 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -79,6 +79,9 @@ private: // is_yes(arg) static int l_is_yes(lua_State *L); + // get_scriptdir() + static int l_get_builtin_path(lua_State *L); + public: static void Initialize(lua_State *L, int top); diff --git a/src/script/scripting_game.cpp b/src/script/scripting_game.cpp index b2c2150c..fccd1072 100644 --- a/src/script/scripting_game.cpp +++ b/src/script/scripting_game.cpp @@ -18,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "scripting_game.h" +#include "server.h" +#include "filesys.h" #include "log.h" #include "cpp_api/s_internal.h" #include "lua_api/l_base.h" @@ -54,6 +56,9 @@ GameScripting::GameScripting(Server* server) SCRIPTAPI_PRECHECKHEADER + lua_pushstring(L, DIR_DELIM); + lua_setglobal(L, "DIR_DELIM"); + // Create the main minetest table lua_newtable(L); lua_setglobal(L, "minetest"); @@ -70,6 +75,10 @@ GameScripting::GameScripting(Server* server) InitializeModApi(L, top); lua_pop(L, 1); + // Push builtin initialization type + lua_pushstring(L, "game"); + lua_setglobal(L, "INIT"); + infostream << "SCRIPTAPI: Initialized game modules" << std::endl; } diff --git a/src/script/scripting_mainmenu.cpp b/src/script/scripting_mainmenu.cpp index dbf1fc45..9afddd15 100644 --- a/src/script/scripting_mainmenu.cpp +++ b/src/script/scripting_mainmenu.cpp @@ -18,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "scripting_mainmenu.h" +#include "mods.h" +#include "porting.h" #include "log.h" #include "filesys.h" #include "cpp_api/s_internal.h" @@ -58,6 +60,10 @@ MainMenuScripting::MainMenuScripting(GUIEngine* guiengine) initializeModApi(L, top); lua_pop(L, 1); + // Push builtin initialization type + lua_pushstring(L, "mainmenu"); + lua_setglobal(L, "INIT"); + infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl; } diff --git a/src/server.cpp b/src/server.cpp index ed99a721..288f254e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -294,9 +294,6 @@ Server::Server( errorstream << std::endl; } - // Path to builtin.lua - std::string builtinpath = getBuiltinLuaPath() + DIR_DELIM + "builtin.lua"; - // Lock environment JMutexAutoLock envlock(m_env_mutex); @@ -305,16 +302,13 @@ Server::Server( m_script = new GameScripting(this); + std::string scriptpath = getBuiltinLuaPath() + DIR_DELIM "init.lua"; - // Load and run builtin.lua - infostream<<"Server: Loading builtin.lua [\"" - <loadMod(builtinpath, "__builtin"); - if(!success){ - errorstream<<"Server: Failed to load and run " - <loadScript(scriptpath)) { + throw ModError("Failed to load and run " + scriptpath); } + + // Print 'em infostream<<"Server: Loading mods: "; for(std::vector::iterator i = m_mods.begin(); -- 2.30.2