2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
#include "scripting_mainmenu.h"
|
2018-04-17 14:54:50 +01:00
|
|
|
#include "content/mods.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "cpp_api/s_internal.h"
|
|
|
|
#include "lua_api/l_base.h"
|
2020-06-06 17:17:08 +01:00
|
|
|
#include "lua_api/l_http.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "lua_api/l_mainmenu.h"
|
2023-06-16 20:15:21 +02:00
|
|
|
#include "lua_api/l_mainmenu_sound.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "lua_api/l_util.h"
|
2013-09-09 22:50:25 +02:00
|
|
|
#include "lua_api/l_settings.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "log.h"
|
2024-11-03 16:53:01 +01:00
|
|
|
#include "filesys.h"
|
|
|
|
#include "porting.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lualib.h"
|
|
|
|
}
|
2024-11-03 16:53:01 +01:00
|
|
|
|
|
|
|
#define MAINMENU_NUM_ASYNC_THREADS 2
|
2014-04-15 13:41:07 -04:00
|
|
|
|
|
|
|
|
2018-01-04 07:21:12 +00:00
|
|
|
MainMenuScripting::MainMenuScripting(GUIEngine* guiengine):
|
|
|
|
ScriptApiBase(ScriptingType::MainMenu)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
setGuiEngine(guiengine);
|
|
|
|
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2024-11-03 16:53:01 +01:00
|
|
|
initializeSecurity();
|
|
|
|
|
2014-04-27 21:02:48 -04:00
|
|
|
lua_getglobal(L, "core");
|
2014-04-15 13:41:07 -04:00
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_setglobal(L, "gamedata");
|
|
|
|
|
|
|
|
// Initialize our lua_api modules
|
2014-04-15 15:10:30 -04:00
|
|
|
initializeModApi(L, top);
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2014-04-27 17:55:49 -04:00
|
|
|
// Push builtin initialization type
|
|
|
|
lua_pushstring(L, "mainmenu");
|
|
|
|
lua_setglobal(L, "INIT");
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl;
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-15 15:10:30 -04:00
|
|
|
void MainMenuScripting::initializeModApi(lua_State *L, int top)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
2014-12-12 14:49:19 -05:00
|
|
|
registerLuaClasses(L, top);
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
// Initialize mod API modules
|
2013-08-11 04:09:45 +02:00
|
|
|
ModApiMainMenu::Initialize(L, top);
|
|
|
|
ModApiUtil::Initialize(L, top);
|
2023-06-16 20:15:21 +02:00
|
|
|
ModApiMainMenuSound::Initialize(L, top);
|
2020-06-06 17:17:08 +01:00
|
|
|
ModApiHttp::Initialize(L, top);
|
2013-09-09 22:50:25 +02:00
|
|
|
|
2014-12-12 14:49:19 -05:00
|
|
|
asyncEngine.registerStateInitializer(registerLuaClasses);
|
|
|
|
asyncEngine.registerStateInitializer(ModApiMainMenu::InitializeAsync);
|
|
|
|
asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
|
2020-06-22 14:40:04 +01:00
|
|
|
asyncEngine.registerStateInitializer(ModApiHttp::InitializeAsync);
|
2013-11-26 18:15:31 +01:00
|
|
|
|
|
|
|
// Initialize async environment
|
|
|
|
//TODO possibly make number of async threads configurable
|
2014-04-15 15:10:30 -04:00
|
|
|
asyncEngine.initialize(MAINMENU_NUM_ASYNC_THREADS);
|
2013-11-26 18:15:31 +01:00
|
|
|
}
|
|
|
|
|
2014-12-12 14:49:19 -05:00
|
|
|
void MainMenuScripting::registerLuaClasses(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
LuaSettings::Register(L);
|
2023-06-16 20:15:21 +02:00
|
|
|
MainMenuSoundHandle::Register(L);
|
2014-12-12 14:49:19 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 16:53:01 +01:00
|
|
|
bool MainMenuScripting::mayModifyPath(const std::string &path)
|
|
|
|
{
|
2024-12-03 16:51:34 +01:00
|
|
|
std::string path_temp = fs::AbsolutePathPartial(fs::TempPath());
|
|
|
|
if (fs::PathStartsWith(path, path_temp))
|
2024-11-03 16:53:01 +01:00
|
|
|
return true;
|
|
|
|
|
2024-12-03 16:51:34 +01:00
|
|
|
std::string path_user = fs::AbsolutePathPartial(porting::path_user);
|
2024-11-03 16:53:01 +01:00
|
|
|
|
|
|
|
if (fs::PathStartsWith(path, path_user + DIR_DELIM "client"))
|
|
|
|
return true;
|
|
|
|
if (fs::PathStartsWith(path, path_user + DIR_DELIM "games"))
|
|
|
|
return true;
|
|
|
|
if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods"))
|
|
|
|
return true;
|
|
|
|
if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
|
|
|
|
return true;
|
|
|
|
if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds"))
|
|
|
|
return true;
|
|
|
|
|
2024-12-03 16:51:34 +01:00
|
|
|
if (fs::PathStartsWith(path, fs::AbsolutePathPartial(porting::path_cache)))
|
2024-11-03 16:53:01 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainMenuScripting::checkPathAccess(const std::string &abs_path, bool write_required,
|
|
|
|
bool *write_allowed)
|
|
|
|
{
|
|
|
|
if (mayModifyPath(abs_path)) {
|
|
|
|
if (write_allowed)
|
|
|
|
*write_allowed = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// TODO?: global read access sounds too broad
|
|
|
|
return !write_required;
|
|
|
|
}
|
|
|
|
|
2024-03-30 11:06:28 +01:00
|
|
|
void MainMenuScripting::beforeClose()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "on_before_close");
|
|
|
|
|
|
|
|
PCALL_RES(lua_pcall(L, 0, 0, error_handler));
|
|
|
|
|
|
|
|
lua_pop(L, 2); // Pop core, error handler
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:12:52 +02:00
|
|
|
void MainMenuScripting::step()
|
|
|
|
{
|
2015-08-25 07:44:53 +02:00
|
|
|
asyncEngine.step(getStack());
|
2013-11-26 18:15:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 12:15:12 +02:00
|
|
|
u32 MainMenuScripting::queueAsync(std::string &&serialized_func,
|
|
|
|
std::string &&serialized_param)
|
2017-04-20 00:12:52 +02:00
|
|
|
{
|
2021-08-28 12:15:12 +02:00
|
|
|
return asyncEngine.queueAsyncJob(std::move(serialized_func), std::move(serialized_param));
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
2014-04-15 13:41:07 -04:00
|
|
|
|