mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
[CSM] Don't load the IO library. (#6087)
* [CSM] Don't load the IO library. * Rename the function to match the Lua API function name and add a missing `const` * Add a comment to explain some strange code and fix the other issues pointed out by shadowninja.
This commit is contained in:
parent
e7396a0c50
commit
30821ad8de
@ -198,6 +198,7 @@ void AsyncEngine::prepareEnvironment(lua_State* L, int top)
|
|||||||
AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
|
AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
|
||||||
const std::string &name) :
|
const std::string &name) :
|
||||||
Thread(name),
|
Thread(name),
|
||||||
|
ScriptApiBase(ScriptingType::Async),
|
||||||
jobDispatcher(jobDispatcher)
|
jobDispatcher(jobDispatcher)
|
||||||
{
|
{
|
||||||
lua_State *L = getStack();
|
lua_State *L = getStack();
|
||||||
|
@ -71,7 +71,8 @@ public:
|
|||||||
ScriptApiBase
|
ScriptApiBase
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ScriptApiBase::ScriptApiBase()
|
ScriptApiBase::ScriptApiBase(ScriptingType type):
|
||||||
|
m_type(type)
|
||||||
{
|
{
|
||||||
#ifdef SCRIPTAPI_LOCK_DEBUG
|
#ifdef SCRIPTAPI_LOCK_DEBUG
|
||||||
m_lock_recursion_count = 0;
|
m_lock_recursion_count = 0;
|
||||||
@ -82,6 +83,9 @@ ScriptApiBase::ScriptApiBase()
|
|||||||
|
|
||||||
lua_atpanic(m_luastack, &luaPanic);
|
lua_atpanic(m_luastack, &luaPanic);
|
||||||
|
|
||||||
|
if (m_type == ScriptingType::Client)
|
||||||
|
clientOpenLibs(m_luastack);
|
||||||
|
else
|
||||||
luaL_openlibs(m_luastack);
|
luaL_openlibs(m_luastack);
|
||||||
|
|
||||||
// Make the ScriptApiBase* accessible to ModApiBase
|
// Make the ScriptApiBase* accessible to ModApiBase
|
||||||
@ -106,6 +110,9 @@ ScriptApiBase::ScriptApiBase()
|
|||||||
lua_newtable(m_luastack);
|
lua_newtable(m_luastack);
|
||||||
lua_setglobal(m_luastack, "core");
|
lua_setglobal(m_luastack, "core");
|
||||||
|
|
||||||
|
if (m_type == ScriptingType::Client)
|
||||||
|
lua_pushstring(m_luastack, "/");
|
||||||
|
else
|
||||||
lua_pushstring(m_luastack, DIR_DELIM);
|
lua_pushstring(m_luastack, DIR_DELIM);
|
||||||
lua_setglobal(m_luastack, "DIR_DELIM");
|
lua_setglobal(m_luastack, "DIR_DELIM");
|
||||||
|
|
||||||
@ -128,6 +135,28 @@ int ScriptApiBase::luaPanic(lua_State *L)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptApiBase::clientOpenLibs(lua_State *L)
|
||||||
|
{
|
||||||
|
static const std::vector<std::pair<std::string, lua_CFunction>> m_libs = {
|
||||||
|
{ "", luaopen_base },
|
||||||
|
{ LUA_LOADLIBNAME, luaopen_package },
|
||||||
|
{ LUA_TABLIBNAME, luaopen_table },
|
||||||
|
{ LUA_OSLIBNAME, luaopen_os },
|
||||||
|
{ LUA_STRLIBNAME, luaopen_string },
|
||||||
|
{ LUA_MATHLIBNAME, luaopen_math },
|
||||||
|
{ LUA_DBLIBNAME, luaopen_debug },
|
||||||
|
#if USE_LUAJIT
|
||||||
|
{ LUA_JITLIBNAME, luaopen_jit },
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const std::pair<std::string, lua_CFunction> &lib : m_libs) {
|
||||||
|
lua_pushcfunction(L, lib.second);
|
||||||
|
lua_pushstring(L, lib.first.c_str());
|
||||||
|
lua_call(L, 1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptApiBase::loadMod(const std::string &script_path,
|
void ScriptApiBase::loadMod(const std::string &script_path,
|
||||||
const std::string &mod_name)
|
const std::string &mod_name)
|
||||||
{
|
{
|
||||||
|
@ -23,15 +23,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <unordered_map>
|
||||||
#include "util/basic_macros.h"
|
#include "util/basic_macros.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
#include <lualib.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "irrlichttypes.h"
|
#include "irrlichttypes.h"
|
||||||
#include "common/c_types.h"
|
#include "common/c_types.h"
|
||||||
#include "common/c_internal.h"
|
#include "common/c_internal.h"
|
||||||
|
#include "debug.h"
|
||||||
|
#include "cmake_config.h"
|
||||||
|
|
||||||
#define SCRIPTAPI_LOCK_DEBUG
|
#define SCRIPTAPI_LOCK_DEBUG
|
||||||
#define SCRIPTAPI_DEBUG
|
#define SCRIPTAPI_DEBUG
|
||||||
@ -54,9 +58,10 @@ extern "C" {
|
|||||||
setOriginFromTableRaw(index, __FUNCTION__)
|
setOriginFromTableRaw(index, __FUNCTION__)
|
||||||
|
|
||||||
enum class ScriptingType: u8 {
|
enum class ScriptingType: u8 {
|
||||||
|
Async,
|
||||||
Client,
|
Client,
|
||||||
Server,
|
MainMenu,
|
||||||
MainMenu
|
Server
|
||||||
};
|
};
|
||||||
|
|
||||||
class Server;
|
class Server;
|
||||||
@ -70,7 +75,12 @@ class ServerActiveObject;
|
|||||||
|
|
||||||
class ScriptApiBase {
|
class ScriptApiBase {
|
||||||
public:
|
public:
|
||||||
ScriptApiBase();
|
ScriptApiBase(ScriptingType type);
|
||||||
|
// fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
|
||||||
|
ScriptApiBase()
|
||||||
|
{
|
||||||
|
FATAL_ERROR("ScriptApiBase created without ScriptingType!");
|
||||||
|
}
|
||||||
virtual ~ScriptApiBase();
|
virtual ~ScriptApiBase();
|
||||||
DISABLE_CLASS_COPY(ScriptApiBase);
|
DISABLE_CLASS_COPY(ScriptApiBase);
|
||||||
|
|
||||||
@ -91,7 +101,6 @@ public:
|
|||||||
|
|
||||||
IGameDef *getGameDef() { return m_gamedef; }
|
IGameDef *getGameDef() { return m_gamedef; }
|
||||||
Server* getServer();
|
Server* getServer();
|
||||||
void setType(ScriptingType type) { m_type = type; }
|
|
||||||
ScriptingType getType() { return m_type; }
|
ScriptingType getType() { return m_type; }
|
||||||
#ifndef SERVER
|
#ifndef SERVER
|
||||||
Client* getClient();
|
Client* getClient();
|
||||||
@ -101,6 +110,8 @@ public:
|
|||||||
void setOriginDirect(const char *origin);
|
void setOriginDirect(const char *origin);
|
||||||
void setOriginFromTableRaw(int index, const char *fxn);
|
void setOriginFromTableRaw(int index, const char *fxn);
|
||||||
|
|
||||||
|
void clientOpenLibs(lua_State *L);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class LuaABM;
|
friend class LuaABM;
|
||||||
friend class LuaLBM;
|
friend class LuaLBM;
|
||||||
|
@ -33,10 +33,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "lua_api/l_localplayer.h"
|
#include "lua_api/l_localplayer.h"
|
||||||
#include "lua_api/l_camera.h"
|
#include "lua_api/l_camera.h"
|
||||||
|
|
||||||
ClientScripting::ClientScripting(Client *client)
|
ClientScripting::ClientScripting(Client *client):
|
||||||
|
ScriptApiBase(ScriptingType::Client)
|
||||||
{
|
{
|
||||||
setGameDef(client);
|
setGameDef(client);
|
||||||
setType(ScriptingType::Client);
|
|
||||||
|
|
||||||
SCRIPTAPI_PRECHECKHEADER
|
SCRIPTAPI_PRECHECKHEADER
|
||||||
|
|
||||||
@ -59,9 +59,6 @@ ClientScripting::ClientScripting(Client *client)
|
|||||||
lua_pushstring(L, "client");
|
lua_pushstring(L, "client");
|
||||||
lua_setglobal(L, "INIT");
|
lua_setglobal(L, "INIT");
|
||||||
|
|
||||||
lua_pushstring(L, "/");
|
|
||||||
lua_setglobal(L, "DIR_DELIM");
|
|
||||||
|
|
||||||
infostream << "SCRIPTAPI: Initialized client game modules" << std::endl;
|
infostream << "SCRIPTAPI: Initialized client game modules" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,10 +34,10 @@ extern "C" {
|
|||||||
#define MAINMENU_NUM_ASYNC_THREADS 4
|
#define MAINMENU_NUM_ASYNC_THREADS 4
|
||||||
|
|
||||||
|
|
||||||
MainMenuScripting::MainMenuScripting(GUIEngine* guiengine)
|
MainMenuScripting::MainMenuScripting(GUIEngine* guiengine):
|
||||||
|
ScriptApiBase(ScriptingType::MainMenu)
|
||||||
{
|
{
|
||||||
setGuiEngine(guiengine);
|
setGuiEngine(guiengine);
|
||||||
setType(ScriptingType::MainMenu);
|
|
||||||
|
|
||||||
SCRIPTAPI_PRECHECKHEADER
|
SCRIPTAPI_PRECHECKHEADER
|
||||||
|
|
||||||
|
@ -48,10 +48,10 @@ extern "C" {
|
|||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerScripting::ServerScripting(Server* server)
|
ServerScripting::ServerScripting(Server* server):
|
||||||
|
ScriptApiBase(ScriptingType::Server)
|
||||||
{
|
{
|
||||||
setGameDef(server);
|
setGameDef(server);
|
||||||
setType(ScriptingType::Server);
|
|
||||||
|
|
||||||
// setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
|
// setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
|
||||||
// once the environment has been created
|
// once the environment has been created
|
||||||
|
Loading…
Reference in New Issue
Block a user