Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
s_base.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <iostream>
8#include <string>
9#include <thread>
10#include <mutex>
11#include <unordered_map>
12#include "common/helper.h"
13#include "util/basic_macros.h"
14
15extern "C" {
16#include <lua.h>
17#include <lualib.h>
18}
19
20#include "irrlichttypes.h"
21#include "common/c_types.h"
22#include "common/c_internal.h"
23#include "debug.h"
24#include "config.h"
25
26#define SCRIPTAPI_LOCK_DEBUG
27
28// MUST be an invalid mod name so that mods can't
29// use that name to bypass security!
30#define BUILTIN_MOD_NAME "*builtin*"
31
32#define PCALL_RES(RES) { \
33 int result_ = (RES); \
34 if (result_ != 0) { \
35 scriptError(result_, __FUNCTION__); \
36 } \
37}
38
39#define runCallbacks(nargs, mode) \
40 runCallbacksRaw((nargs), (mode), __FUNCTION__)
41
42#define setOriginFromTable(index) \
43 setOriginFromTableRaw(index, __FUNCTION__)
44
45enum class ScriptingType: u8 {
46 Async, // either mainmenu (client) or ingame (server)
47 Client,
49 Server,
50 Emerge
51};
52
53class Server;
54#if CHECK_CLIENT_BUILD()
55class Client;
56#endif
57class EmergeThread;
58class IGameDef;
59class Environment;
60class GUIEngine;
63
64class ScriptApiBase : protected LuaHelper {
65public:
67 // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
69 {
70 FATAL_ERROR("ScriptApiBase created without ScriptingType!");
71 }
72 virtual ~ScriptApiBase();
74
75 // These throw a ModError on failure
76 void loadMod(const std::string &script_path, const std::string &mod_name);
77 void loadScript(const std::string &script_path);
78
79#if CHECK_CLIENT_BUILD()
80 void loadModFromMemory(const std::string &mod_name);
81#endif
82
83 void runCallbacksRaw(int nargs,
84 RunCallbacksMode mode, const char *fxn);
85
86 /* object */
89
91
94#if CHECK_CLIENT_BUILD()
95 Client* getClient();
96#endif
97
98 // IMPORTANT: These cannot be used for any security-related uses, they exist
99 // only to enrich error messages.
100 const std::string &getOrigin() { return m_last_run_mod; }
101 void setOriginDirect(const char *origin);
102 void setOriginFromTableRaw(int index, const char *fxn);
103
104 // Returns the currently running mod, only during init time.
105 // The reason this is "insecure" is that mods can mess with each others code,
106 // so the boundary of who is responsible is fuzzy.
107 // Note: checking this against BUILTIN_MOD_NAME is always safe (not spoofable).
108 // returns "" on error
109 static std::string getCurrentModNameInsecure(lua_State *L);
110 // Returns the currently running mod, only during init time.
111 // This checks the Lua stack to only permit direct calls in the file
112 // scope. That way it is assured that it's really the mod it claims to be.
113 // returns "" on error
114 static std::string getCurrentModName(lua_State *L);
115
116#if !CHECK_CLIENT_BUILD()
117 inline void clientOpenLibs(lua_State *L) { assert(false); }
118#else
119 void clientOpenLibs(lua_State *L);
120#endif
121
122 // Check things that should be set by the builtin mod.
123 void checkSetByBuiltin();
124
125protected:
126 friend class LuaABM;
127 friend class LuaLBM;
128 friend class InvRef;
129 friend class ObjectRef;
130 friend class NodeMetaRef;
131 friend class ModApiBase;
132 friend class ModApiEnv;
133 friend class LuaVoxelManip;
134 friend class TestMoveAction; // needs getStack()
135
136 /*
137 Subtle edge case with coroutines: If for whatever reason you have a
138 method in a subclass that's called from existing lua_CFunction
139 (any of the l_*.cpp files) then make it static and take the lua_State*
140 as an argument. This is REQUIRED because getStack() will not return the
141 correct state if called inside coroutines.
142
143 Also note that src/script/common/ is the better place for such helpers.
144 */
145 lua_State* getStack()
146 { return m_luastack; }
147
148 // Checks that stack size is sane
149 void realityCheck();
150 // Takes an error from lua_pcall and throws it as a LuaError
151 void scriptError(int result, const char *fxn);
152 // Dumps stack contents for debugging
153 void stackDump(std::ostream &o);
154
155 void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
156
158 void setEnv(Environment* env) { m_environment = env; }
159
160#if CHECK_CLIENT_BUILD()
161 GUIEngine* getGuiEngine() { return m_guiengine; }
162 void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
163#endif
164
166 void setEmergeThread(EmergeThread *emerge) { m_emerge = emerge; }
167
168 void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
169
170 void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
171
172 std::recursive_mutex m_luastackmutex;
173 std::string m_last_run_mod;
174 bool m_secure = false;
175#ifdef SCRIPTAPI_LOCK_DEBUG
177 std::thread::id m_owning_thread;
178#endif
179
180private:
181 static int luaPanic(lua_State *L);
182
183 lua_State *m_luastack = nullptr;
184
185 IGameDef *m_gamedef = nullptr;
187#if CHECK_CLIENT_BUILD()
188 GUIEngine *m_guiengine = nullptr;
189#endif
191
193};
RunCallbacksMode
Definition c_internal.h:83
Definition client.h:105
Definition emerge_internal.h:25
Definition environment.h:34
implementation of main menu based uppon formspecs
Definition guiEngine.h:113
Definition gamedef.h:36
Definition l_inventory.h:18
Definition s_env.cpp:20
Definition helper.h:14
Definition s_env.cpp:92
Definition l_vmanip.h:18
Definition l_base.h:28
Definition l_env.h:43
Definition l_nodemeta.h:19
Definition l_object.h:19
Definition s_base.h:64
static std::string getCurrentModNameInsecure(lua_State *L)
Definition s_base.cpp:220
void setOriginDirect(const char *origin)
Definition s_base.cpp:420
void addObjectReference(ServerActiveObject *cobj)
Definition s_base.cpp:446
std::recursive_mutex m_luastackmutex
Definition s_base.h:172
void setEnv(Environment *env)
Definition s_base.h:158
void stackDump(std::ostream &o)
Definition s_base.cpp:393
void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason)
Definition s_base.cpp:512
Environment * getEnv()
Definition s_base.h:157
IGameDef * getGameDef()
Definition s_base.h:92
EmergeThread * m_emerge
Definition s_base.h:190
IGameDef * m_gamedef
Definition s_base.h:185
void realityCheck()
Definition s_base.cpp:377
ScriptingType getType()
Definition s_base.h:90
DISABLE_CLASS_COPY(ScriptApiBase)
EmergeThread * getEmergeThread()
Definition s_base.h:165
int m_lock_recursion_count
Definition s_base.h:176
bool m_secure
Definition s_base.h:174
static std::string getCurrentModName(lua_State *L)
Definition s_base.cpp:228
void clientOpenLibs(lua_State *L)
Definition s_base.h:117
void setEmergeThread(EmergeThread *emerge)
Definition s_base.h:166
ScriptApiBase()
Definition s_base.h:68
ScriptingType m_type
Definition s_base.h:192
std::string m_last_run_mod
Definition s_base.h:173
void loadScript(const std::string &script_path)
Definition s_base.cpp:267
void loadMod(const std::string &script_path, const std::string &mod_name)
Definition s_base.cpp:259
void removeObjectReference(ServerActiveObject *cobj)
Definition s_base.cpp:467
Server * getServer()
Definition s_base.cpp:544
lua_State * m_luastack
Definition s_base.h:183
void setGameDef(IGameDef *gamedef)
Definition s_base.h:155
static int luaPanic(lua_State *L)
Definition s_base.cpp:163
const std::string & getOrigin()
Definition s_base.h:100
lua_State * getStack()
Definition s_base.h:145
void runCallbacksRaw(int nargs, RunCallbacksMode mode, const char *fxn)
Definition s_base.cpp:336
void checkSetByBuiltin()
Definition s_base.cpp:202
virtual ~ScriptApiBase()
Definition s_base.cpp:158
std::thread::id m_owning_thread
Definition s_base.h:177
void scriptError(int result, const char *fxn)
Definition s_base.cpp:388
void setOriginFromTableRaw(int index, const char *fxn)
Definition s_base.cpp:425
Environment * m_environment
Definition s_base.h:186
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj)
Definition s_base.cpp:491
Definition serveractiveobject.h:41
Definition server.h:167
Definition test_moveaction.cpp:12
#define FATAL_ERROR(msg)
Definition debug.h:33
ScriptingType
Definition s_base.h:45
Definition player_sao.h:227