Organize builtin into subdirectories

This commit is contained in:
ShadowNinja 2014-04-27 17:55:49 -04:00
parent fef2729fd0
commit 1cd512913e
44 changed files with 206 additions and 203 deletions

18
builtin/async/init.lua Normal file

@ -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

@ -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

@ -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

@ -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")

@ -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

@ -4,7 +4,7 @@
-- Falling stuff -- Falling stuff
-- --
minetest.register_entity("__builtin:falling_node", { minetest.register_entity(":__builtin:falling_node", {
initial_properties = { initial_properties = {
physical = true, physical = true,
collide_with_objects = false, collide_with_objects = false,

23
builtin/game/init.lua Normal file

@ -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")

@ -8,7 +8,7 @@ function minetest.spawn_item(pos, item)
return obj return obj
end end
minetest.register_entity("__builtin:item", { minetest.register_entity(":__builtin:item", {
initial_properties = { initial_properties = {
hp_max = 1, hp_max = 1,
physical = true, physical = true,

34
builtin/init.lua Normal file

@ -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

@ -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_grey = "#AAAAAA"
mt_color_blue = "#0000DD" mt_color_blue = "#0000DD"
@ -11,15 +17,6 @@ mt_color_dark_green = "#003300"
--for all other colors ask sfan5 to complete his worK! --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 = {} menu = {}
local tabbuilder = {} local tabbuilder = {}
local worldlist = nil local worldlist = nil

@ -269,7 +269,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("enable_ipv6", "true"); settings->setDefault("enable_ipv6", "true");
settings->setDefault("ipv6_server", "false"); settings->setDefault("ipv6_server", "false");
settings->setDefault("main_menu_script",""); settings->setDefault("main_menu_path", "");
settings->setDefault("main_menu_mod_mgr", "1"); settings->setDefault("main_menu_mod_mgr", "1");
settings->setDefault("main_menu_game_mgr", "0"); settings->setDefault("main_menu_game_mgr", "0");
settings->setDefault("modstore_download_url", "https://forum.minetest.net/media/"); settings->setDefault("modstore_download_url", "https://forum.minetest.net/media/");

@ -169,13 +169,12 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
m_formspecgui = new FormspecFormSource(""); m_formspecgui = new FormspecFormSource("");
/* Create menu */ /* Create menu */
m_menu = m_menu = new GUIFormSpecMenu(m_device,
new GUIFormSpecMenu( m_device,
m_parent, m_parent,
-1, -1,
m_menumanager, m_menumanager,
0 /* &client */, NULL /* &client */,
0 /* gamedef */, NULL /* gamedef */,
m_texture_source, m_texture_source,
m_formspecgui, m_formspecgui,
m_buttonhandler, m_buttonhandler,
@ -216,43 +215,21 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
/******************************************************************************/ /******************************************************************************/
bool GUIEngine::loadMainMenuScript() 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"); m_scriptdir = g_settings->get("main_menu_path");
if(menuscript != "") { if (m_scriptdir.empty()) {
m_scriptdir = fs::RemoveLastPathComponent(menuscript); m_scriptdir = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "mainmenu";
if(m_script->loadMod(menuscript, "__custommenu")) {
// custom menu script loaded
return true;
} }
else {
std::string script = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "init.lua";
if (m_script->loadScript(script)) {
// Menu script loaded
return true;
} else {
infostream infostream
<< "GUIEngine: execution of custom menu: \"" << "GUIEngine: execution of menu script in: \""
<< menuscript << "\" failed!" << m_scriptdir << "\" failed!" << std::endl;
<< std::endl
<< "\tfalling back to builtin menu"
<< std::endl;
}
}
// 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
return true;
}
else {
errorstream
<< "GUIEngine: unable to load builtin menu"
<< std::endl;
} }
return false; return false;

@ -26,6 +26,7 @@ extern "C" {
#include "lualib.h" #include "lualib.h"
} }
#include "server.h"
#include "s_async.h" #include "s_async.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
@ -233,9 +234,9 @@ AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
lua_pushstring(L, DIR_DELIM); lua_pushstring(L, DIR_DELIM);
lua_setglobal(L, "DIR_DELIM"); lua_setglobal(L, "DIR_DELIM");
lua_pushstring(L, // Push builtin initialization type
(porting::path_share + DIR_DELIM + "builtin").c_str()); lua_pushstring(L, "async");
lua_setglobal(L, "SCRIPTDIR"); lua_setglobal(L, "INIT");
jobDispatcher->prepareEnvironment(L, top); jobDispatcher->prepareEnvironment(L, top);
} }
@ -258,17 +259,16 @@ void* AsyncWorkerThread::Thread()
porting::setThreadName((std::string("AsyncWorkTh_") + number).c_str()); porting::setThreadName((std::string("AsyncWorkTh_") + number).c_str());
std::string asyncscript = porting::path_share + DIR_DELIM + "builtin" lua_State *L = getStack();
+ DIR_DELIM + "async_env.lua";
if (!loadScript(asyncscript)) { std::string script = getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua";
if (!loadScript(script)) {
errorstream errorstream
<< "AsyncWorkderThread execution of async base environment failed!" << "AsyncWorkderThread execution of async base environment failed!"
<< std::endl; << std::endl;
abort(); abort();
} }
lua_State *L = getStack();
// Main loop // Main loop
while (!StopRequested()) { while (!StopRequested()) {
// Wait for job // Wait for job

@ -88,9 +88,9 @@ ScriptApiBase::ScriptApiBase()
lua_pop(m_luastack, 1); lua_pop(m_luastack, 1);
#endif #endif
m_server = 0; m_server = NULL;
m_environment = 0; m_environment = NULL;
m_guiengine = 0; m_guiengine = NULL;
} }
ScriptApiBase::~ScriptApiBase() ScriptApiBase::~ScriptApiBase()
@ -110,17 +110,7 @@ bool ScriptApiBase::loadMod(const std::string &scriptpath,
return false; return false;
} }
bool success = false; return loadScript(scriptpath);
try{
success = loadScript(scriptpath);
}
catch(LuaError &e){
errorstream<<"Error loading mod \""<<modname
<<"\": "<<e.what()<<std::endl;
}
return success;
} }
bool ScriptApiBase::loadScript(const std::string &scriptpath) bool ScriptApiBase::loadScript(const std::string &scriptpath)

@ -885,7 +885,7 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
} }
/******************************************************************************/ /******************************************************************************/
int ModApiMainMenu::l_get_scriptdir(lua_State *L) int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
{ {
GUIEngine* engine = getGuiEngine(L); GUIEngine* engine = getGuiEngine(L);
assert(engine != 0); assert(engine != 0);
@ -1077,7 +1077,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(delete_dir); API_FCT(delete_dir);
API_FCT(copy_dir); API_FCT(copy_dir);
API_FCT(extract_zip); API_FCT(extract_zip);
API_FCT(get_scriptdir); API_FCT(get_mainmenu_path);
API_FCT(show_file_open_dialog); API_FCT(show_file_open_dialog);
API_FCT(get_version); API_FCT(get_version);
API_FCT(download_file); API_FCT(download_file);

@ -107,7 +107,7 @@ private:
//filesystem //filesystem
static int l_get_scriptdir(lua_State *L); static int l_get_mainmenu_path(lua_State *L);
static int l_get_modpath(lua_State *L); static int l_get_modpath(lua_State *L);

@ -350,12 +350,6 @@ int ModApiServer::l_get_modpath(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string modname = luaL_checkstring(L, 1); std::string modname = luaL_checkstring(L, 1);
// Do it
if(modname == "__builtin"){
std::string path = getServer(L)->getBuiltinLuaPath();
lua_pushstring(L, path.c_str());
return 1;
}
const ModSpec *mod = getServer(L)->getModSpec(modname); const ModSpec *mod = getServer(L)->getModSpec(modname);
if (!mod) { if (!mod) {
lua_pushnil(L); lua_pushnil(L);

@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_content.h" #include "common/c_content.h"
#include "cpp_api/s_async.h" #include "cpp_api/s_async.h"
#include "debug.h" #include "debug.h"
#include "porting.h"
#include "log.h" #include "log.h"
#include "tool.h" #include "tool.h"
#include "settings.h" #include "settings.h"
@ -274,6 +275,14 @@ int ModApiUtil::l_is_yes(lua_State *L)
return 1; 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) void ModApiUtil::Initialize(lua_State *L, int top)
{ {
API_FCT(debug); API_FCT(debug);
@ -294,6 +303,8 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(get_password_hash); API_FCT(get_password_hash);
API_FCT(is_yes); API_FCT(is_yes);
API_FCT(get_builtin_path);
} }
void ModApiUtil::InitializeAsync(AsyncEngine& engine) void ModApiUtil::InitializeAsync(AsyncEngine& engine)
@ -311,4 +322,7 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine)
ASYNC_API_FCT(write_json); ASYNC_API_FCT(write_json);
ASYNC_API_FCT(is_yes); ASYNC_API_FCT(is_yes);
ASYNC_API_FCT(get_builtin_path);
} }

@ -79,6 +79,9 @@ private:
// is_yes(arg) // is_yes(arg)
static int l_is_yes(lua_State *L); static int l_is_yes(lua_State *L);
// get_scriptdir()
static int l_get_builtin_path(lua_State *L);
public: public:
static void Initialize(lua_State *L, int top); static void Initialize(lua_State *L, int top);

@ -18,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
#include "scripting_game.h" #include "scripting_game.h"
#include "server.h"
#include "filesys.h"
#include "log.h" #include "log.h"
#include "cpp_api/s_internal.h" #include "cpp_api/s_internal.h"
#include "lua_api/l_base.h" #include "lua_api/l_base.h"
@ -54,6 +56,9 @@ GameScripting::GameScripting(Server* server)
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
lua_pushstring(L, DIR_DELIM);
lua_setglobal(L, "DIR_DELIM");
// Create the main minetest table // Create the main minetest table
lua_newtable(L); lua_newtable(L);
lua_setglobal(L, "minetest"); lua_setglobal(L, "minetest");
@ -70,6 +75,10 @@ GameScripting::GameScripting(Server* server)
InitializeModApi(L, top); InitializeModApi(L, top);
lua_pop(L, 1); lua_pop(L, 1);
// Push builtin initialization type
lua_pushstring(L, "game");
lua_setglobal(L, "INIT");
infostream << "SCRIPTAPI: Initialized game modules" << std::endl; infostream << "SCRIPTAPI: Initialized game modules" << std::endl;
} }

@ -18,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
#include "scripting_mainmenu.h" #include "scripting_mainmenu.h"
#include "mods.h"
#include "porting.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
#include "cpp_api/s_internal.h" #include "cpp_api/s_internal.h"
@ -58,6 +60,10 @@ MainMenuScripting::MainMenuScripting(GUIEngine* guiengine)
initializeModApi(L, top); initializeModApi(L, top);
lua_pop(L, 1); lua_pop(L, 1);
// Push builtin initialization type
lua_pushstring(L, "mainmenu");
lua_setglobal(L, "INIT");
infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl; infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl;
} }

@ -294,9 +294,6 @@ Server::Server(
errorstream << std::endl; errorstream << std::endl;
} }
// Path to builtin.lua
std::string builtinpath = getBuiltinLuaPath() + DIR_DELIM + "builtin.lua";
// Lock environment // Lock environment
JMutexAutoLock envlock(m_env_mutex); JMutexAutoLock envlock(m_env_mutex);
@ -305,16 +302,13 @@ Server::Server(
m_script = new GameScripting(this); m_script = new GameScripting(this);
std::string scriptpath = getBuiltinLuaPath() + DIR_DELIM "init.lua";
// Load and run builtin.lua if (!m_script->loadScript(scriptpath)) {
infostream<<"Server: Loading builtin.lua [\"" throw ModError("Failed to load and run " + scriptpath);
<<builtinpath<<"\"]"<<std::endl;
bool success = m_script->loadMod(builtinpath, "__builtin");
if(!success){
errorstream<<"Server: Failed to load and run "
<<builtinpath<<std::endl;
throw ModError("Failed to load and run "+builtinpath);
} }
// Print 'em // Print 'em
infostream<<"Server: Loading mods: "; infostream<<"Server: Loading mods: ";
for(std::vector<ModSpec>::iterator i = m_mods.begin(); for(std::vector<ModSpec>::iterator i = m_mods.begin();