Luanti 5.11.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
112 static std::string getCurrentModNameInsecure(lua_State *L);
113
114#if !CHECK_CLIENT_BUILD()
115 inline void clientOpenLibs(lua_State *L) { assert(false); }
116#else
117 void clientOpenLibs(lua_State *L);
118#endif
119
120 // Check things that should be set by the builtin mod.
121 void checkSetByBuiltin();
122
123protected:
124 friend class LuaABM;
125 friend class LuaLBM;
126 friend class InvRef;
127 friend class ObjectRef;
128 friend class NodeMetaRef;
129 friend class ModApiBase;
130 friend class ModApiEnv;
131 friend class LuaVoxelManip;
132 friend class TestMoveAction; // needs getStack()
133
134 /*
135 Subtle edge case with coroutines: If for whatever reason you have a
136 method in a subclass that's called from existing lua_CFunction
137 (any of the l_*.cpp files) then make it static and take the lua_State*
138 as an argument. This is REQUIRED because getStack() will not return the
139 correct state if called inside coroutines.
140
141 Also note that src/script/common/ is the better place for such helpers.
142 */
143 lua_State* getStack()
144 { return m_luastack; }
145
146 // Checks that stack size is sane
147 void realityCheck();
148 // Takes an error from lua_pcall and throws it as a LuaError
149 void scriptError(int result, const char *fxn);
150 // Dumps stack contents for debugging
151 void stackDump(std::ostream &o);
152
153 void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
154
156 void setEnv(Environment* env) { m_environment = env; }
157
158#if CHECK_CLIENT_BUILD()
159 GUIEngine* getGuiEngine() { return m_guiengine; }
160 void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
161#endif
162
164 void setEmergeThread(EmergeThread *emerge) { m_emerge = emerge; }
165
166 void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
167
168 void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
169
170 std::recursive_mutex m_luastackmutex;
171 std::string m_last_run_mod;
172
173#ifdef SCRIPTAPI_LOCK_DEBUG
175 std::thread::id m_owning_thread;
176#endif
177
178private:
179 static int luaPanic(lua_State *L);
180
181 lua_State *m_luastack = nullptr;
182
183 IGameDef *m_gamedef = nullptr;
185#if CHECK_CLIENT_BUILD()
186 GUIEngine *m_guiengine = nullptr;
187#endif
189
191};
RunCallbacksMode
Definition c_internal.h:84
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)
Returns the currently running mod, only during init time.
Definition s_base.cpp:220
void setOriginDirect(const char *origin)
Definition s_base.cpp:389
void addObjectReference(ServerActiveObject *cobj)
Definition s_base.cpp:415
std::recursive_mutex m_luastackmutex
Definition s_base.h:170
void setEnv(Environment *env)
Definition s_base.h:156
void stackDump(std::ostream &o)
Definition s_base.cpp:362
void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason)
Definition s_base.cpp:481
Environment * getEnv()
Definition s_base.h:155
IGameDef * getGameDef()
Definition s_base.h:92
EmergeThread * m_emerge
Definition s_base.h:188
IGameDef * m_gamedef
Definition s_base.h:183
void realityCheck()
Definition s_base.cpp:346
ScriptingType getType()
Definition s_base.h:90
DISABLE_CLASS_COPY(ScriptApiBase)
EmergeThread * getEmergeThread()
Definition s_base.h:163
int m_lock_recursion_count
Definition s_base.h:174
void clientOpenLibs(lua_State *L)
Definition s_base.h:115
void setEmergeThread(EmergeThread *emerge)
Definition s_base.h:164
ScriptApiBase()
Definition s_base.h:68
ScriptingType m_type
Definition s_base.h:190
std::string m_last_run_mod
Definition s_base.h:171
void loadScript(const std::string &script_path)
Definition s_base.cpp:236
void loadMod(const std::string &script_path, const std::string &mod_name)
Definition s_base.cpp:228
void removeObjectReference(ServerActiveObject *cobj)
Definition s_base.cpp:436
Server * getServer()
Definition s_base.cpp:513
lua_State * m_luastack
Definition s_base.h:181
void setGameDef(IGameDef *gamedef)
Definition s_base.h:153
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:143
void runCallbacksRaw(int nargs, RunCallbacksMode mode, const char *fxn)
Definition s_base.cpp:305
void checkSetByBuiltin()
Definition s_base.cpp:202
virtual ~ScriptApiBase()
Definition s_base.cpp:158
std::thread::id m_owning_thread
Definition s_base.h:175
void scriptError(int result, const char *fxn)
Definition s_base.cpp:357
void setOriginFromTableRaw(int index, const char *fxn)
Definition s_base.cpp:394
Environment * m_environment
Definition s_base.h:184
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj)
Definition s_base.cpp:460
Definition serveractiveobject.h:41
Definition server.h:166
Definition test_moveaction.cpp:12
#define FATAL_ERROR(msg)
Definition debug.h:33
ScriptingType
Definition s_base.h:45
Definition player_sao.h:227