Skip to content

Commit 30821ad

Browse files
red-001nerzhul
authored andcommittedJan 4, 2018
[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.
1 parent e7396a0 commit 30821ad

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed
 

Diff for: ‎src/script/cpp_api/s_async.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ void AsyncEngine::prepareEnvironment(lua_State* L, int top)
198198
AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
199199
const std::string &name) :
200200
Thread(name),
201+
ScriptApiBase(ScriptingType::Async),
201202
jobDispatcher(jobDispatcher)
202203
{
203204
lua_State *L = getStack();

Diff for: ‎src/script/cpp_api/s_base.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class ModNameStorer
7171
ScriptApiBase
7272
*/
7373

74-
ScriptApiBase::ScriptApiBase()
74+
ScriptApiBase::ScriptApiBase(ScriptingType type):
75+
m_type(type)
7576
{
7677
#ifdef SCRIPTAPI_LOCK_DEBUG
7778
m_lock_recursion_count = 0;
@@ -82,7 +83,10 @@ ScriptApiBase::ScriptApiBase()
8283

8384
lua_atpanic(m_luastack, &luaPanic);
8485

85-
luaL_openlibs(m_luastack);
86+
if (m_type == ScriptingType::Client)
87+
clientOpenLibs(m_luastack);
88+
else
89+
luaL_openlibs(m_luastack);
8690

8791
// Make the ScriptApiBase* accessible to ModApiBase
8892
lua_pushlightuserdata(m_luastack, this);
@@ -106,7 +110,10 @@ ScriptApiBase::ScriptApiBase()
106110
lua_newtable(m_luastack);
107111
lua_setglobal(m_luastack, "core");
108112

109-
lua_pushstring(m_luastack, DIR_DELIM);
113+
if (m_type == ScriptingType::Client)
114+
lua_pushstring(m_luastack, "/");
115+
else
116+
lua_pushstring(m_luastack, DIR_DELIM);
110117
lua_setglobal(m_luastack, "DIR_DELIM");
111118

112119
lua_pushstring(m_luastack, porting::getPlatformName());
@@ -128,6 +135,28 @@ int ScriptApiBase::luaPanic(lua_State *L)
128135
return 0;
129136
}
130137

138+
void ScriptApiBase::clientOpenLibs(lua_State *L)
139+
{
140+
static const std::vector<std::pair<std::string, lua_CFunction>> m_libs = {
141+
{ "", luaopen_base },
142+
{ LUA_LOADLIBNAME, luaopen_package },
143+
{ LUA_TABLIBNAME, luaopen_table },
144+
{ LUA_OSLIBNAME, luaopen_os },
145+
{ LUA_STRLIBNAME, luaopen_string },
146+
{ LUA_MATHLIBNAME, luaopen_math },
147+
{ LUA_DBLIBNAME, luaopen_debug },
148+
#if USE_LUAJIT
149+
{ LUA_JITLIBNAME, luaopen_jit },
150+
#endif
151+
};
152+
153+
for (const std::pair<std::string, lua_CFunction> &lib : m_libs) {
154+
lua_pushcfunction(L, lib.second);
155+
lua_pushstring(L, lib.first.c_str());
156+
lua_call(L, 1, 0);
157+
}
158+
}
159+
131160
void ScriptApiBase::loadMod(const std::string &script_path,
132161
const std::string &mod_name)
133162
{

Diff for: ‎src/script/cpp_api/s_base.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <string>
2424
#include <thread>
2525
#include <mutex>
26+
#include <unordered_map>
2627
#include "util/basic_macros.h"
2728

2829
extern "C" {
2930
#include <lua.h>
31+
#include <lualib.h>
3032
}
3133

3234
#include "irrlichttypes.h"
3335
#include "common/c_types.h"
3436
#include "common/c_internal.h"
37+
#include "debug.h"
38+
#include "cmake_config.h"
3539

3640
#define SCRIPTAPI_LOCK_DEBUG
3741
#define SCRIPTAPI_DEBUG
@@ -54,9 +58,10 @@ extern "C" {
5458
setOriginFromTableRaw(index, __FUNCTION__)
5559

5660
enum class ScriptingType: u8 {
61+
Async,
5762
Client,
58-
Server,
59-
MainMenu
63+
MainMenu,
64+
Server
6065
};
6166

6267
class Server;
@@ -70,7 +75,12 @@ class ServerActiveObject;
7075

7176
class ScriptApiBase {
7277
public:
73-
ScriptApiBase();
78+
ScriptApiBase(ScriptingType type);
79+
// fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
80+
ScriptApiBase()
81+
{
82+
FATAL_ERROR("ScriptApiBase created without ScriptingType!");
83+
}
7484
virtual ~ScriptApiBase();
7585
DISABLE_CLASS_COPY(ScriptApiBase);
7686

@@ -91,7 +101,6 @@ class ScriptApiBase {
91101

92102
IGameDef *getGameDef() { return m_gamedef; }
93103
Server* getServer();
94-
void setType(ScriptingType type) { m_type = type; }
95104
ScriptingType getType() { return m_type; }
96105
#ifndef SERVER
97106
Client* getClient();
@@ -101,6 +110,8 @@ class ScriptApiBase {
101110
void setOriginDirect(const char *origin);
102111
void setOriginFromTableRaw(int index, const char *fxn);
103112

113+
void clientOpenLibs(lua_State *L);
114+
104115
protected:
105116
friend class LuaABM;
106117
friend class LuaLBM;

Diff for: ‎src/script/scripting_client.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include "lua_api/l_localplayer.h"
3434
#include "lua_api/l_camera.h"
3535

36-
ClientScripting::ClientScripting(Client *client)
36+
ClientScripting::ClientScripting(Client *client):
37+
ScriptApiBase(ScriptingType::Client)
3738
{
3839
setGameDef(client);
39-
setType(ScriptingType::Client);
4040

4141
SCRIPTAPI_PRECHECKHEADER
4242

@@ -59,9 +59,6 @@ ClientScripting::ClientScripting(Client *client)
5959
lua_pushstring(L, "client");
6060
lua_setglobal(L, "INIT");
6161

62-
lua_pushstring(L, "/");
63-
lua_setglobal(L, "DIR_DELIM");
64-
6562
infostream << "SCRIPTAPI: Initialized client game modules" << std::endl;
6663
}
6764

Diff for: ‎src/script/scripting_mainmenu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ extern "C" {
3434
#define MAINMENU_NUM_ASYNC_THREADS 4
3535

3636

37-
MainMenuScripting::MainMenuScripting(GUIEngine* guiengine)
37+
MainMenuScripting::MainMenuScripting(GUIEngine* guiengine):
38+
ScriptApiBase(ScriptingType::MainMenu)
3839
{
3940
setGuiEngine(guiengine);
40-
setType(ScriptingType::MainMenu);
4141

4242
SCRIPTAPI_PRECHECKHEADER
4343

Diff for: ‎src/script/scripting_server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ extern "C" {
4848
#include "lualib.h"
4949
}
5050

51-
ServerScripting::ServerScripting(Server* server)
51+
ServerScripting::ServerScripting(Server* server):
52+
ScriptApiBase(ScriptingType::Server)
5253
{
5354
setGameDef(server);
54-
setType(ScriptingType::Server);
5555

5656
// setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
5757
// once the environment has been created

0 commit comments

Comments
 (0)
Please sign in to comment.