Luanti 5.16.0-dev
 
Loading...
Searching...
No Matches
l_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 "common/c_types.h"
8#include "common/helper.h"
9#include "config.h"
10#include "gamedef.h"
11
12extern "C" {
13#include <lua.h>
14#include <lauxlib.h>
15}
16
17#if CHECK_CLIENT_BUILD()
18class Client;
19class GUIEngine;
20#endif
21class EmergeThread;
22class ScriptApiBase;
23class Server;
24class Environment;
27
28class ModApiBase : protected LuaHelper {
29public:
30 static ScriptApiBase* getScriptApiBase(lua_State *L);
31 static Server* getServer(lua_State *L);
33 #if CHECK_CLIENT_BUILD()
34 static Client* getClient(lua_State *L);
35 static GUIEngine* getGuiEngine(lua_State *L);
36 static SSCSMEnvironment *getSSCSMEnv(lua_State *L);
37 #endif // !SERVER
38 static EmergeThread* getEmergeThread(lua_State *L);
39
40 static IGameDef* getGameDef(lua_State *L);
41
42 static Environment* getEnv(lua_State *L);
43
44 // When we are not loading the mod, this function returns "."
45 static std::string getCurrentModPath(lua_State *L);
46
47 // Get an arbitrary subclass of ScriptApiBase
48 // by using dynamic_cast<> on getScriptApiBase()
49 template<typename T>
50 static T* getScriptApi(lua_State *L) {
51 ScriptApiBase *scriptIface = getScriptApiBase(L);
52 T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
53 if (!scriptIfaceDowncast) {
54 throw LuaError("Requested unavailable ScriptApi - core engine bug!");
55 }
56 return scriptIfaceDowncast;
57 }
58
59 static bool registerFunction(lua_State *L,
60 const char* name,
61 lua_CFunction func,
62 int top);
63
64 template<typename T>
65 static void registerClass(lua_State *L,
66 const luaL_Reg *methods,
67 const luaL_Reg *metamethods)
68 {
69 luaL_newmetatable(L, T::className);
70 luaL_register(L, NULL, metamethods);
71 int metatable = lua_gettop(L);
72
73 lua_newtable(L);
74 luaL_register(L, NULL, methods);
75 int methodtable = lua_gettop(L);
76
77 lua_pushvalue(L, methodtable);
78 lua_setfield(L, metatable, "__index");
79
80 lua_getfield(L, metatable, "__tostring");
81 bool default_tostring = lua_isnil(L, -1);
82 lua_pop(L, 1);
83 if (default_tostring) {
84 lua_pushcfunction(L, ModApiBase::defaultToString<T>);
85 lua_setfield(L, metatable, "__tostring");
86 }
87
88 // Protect the real metatable.
89 lua_pushvalue(L, methodtable);
90 lua_setfield(L, metatable, "__metatable");
91
92 // Pop methodtable and metatable.
93 lua_pop(L, 2);
94 }
95
96 template<typename T>
97 static inline T *checkObject(lua_State *L, int narg)
98 {
99 return *reinterpret_cast<T**>(luaL_checkudata(L, narg, T::className));
100 }
101
115 static int l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func);
116
117private:
118
119 template<typename T>
120 static int defaultToString(lua_State *L)
121 {
122 auto *t = checkObject<T>(L, 1);
123 lua_pushfstring(L, "%s: %p", T::className, t);
124 return 1;
125 }
126};
Definition client.h:107
Definition emerge_internal.h:25
Definition environment.h:31
implementation of main menu based uppon formspecs
Definition guiEngine.h:113
Definition gamedef.h:26
Definition c_types.h:32
Definition helper.h:14
Definition l_base.h:28
static bool registerFunction(lua_State *L, const char *name, lua_CFunction func, int top)
Definition l_base.cpp:86
static int l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func)
A wrapper for deprecated functions.
Definition l_base.cpp:97
static void registerClass(lua_State *L, const luaL_Reg *methods, const luaL_Reg *metamethods)
Definition l_base.h:65
static ServerInventoryManager * getServerInventoryMgr(lua_State *L)
Definition l_base.cpp:33
static Server * getServer(lua_State *L)
Definition l_base.cpp:28
static IGameDef * getGameDef(lua_State *L)
Definition l_base.cpp:45
static std::string getCurrentModPath(lua_State *L)
Definition l_base.cpp:72
static int defaultToString(lua_State *L)
Definition l_base.h:120
static ScriptApiBase * getScriptApiBase(lua_State *L)
Definition l_base.cpp:14
static Environment * getEnv(lua_State *L)
Definition l_base.cpp:50
static T * getScriptApi(lua_State *L)
Definition l_base.h:50
static EmergeThread * getEmergeThread(lua_State *L)
Definition l_base.cpp:67
static T * checkObject(lua_State *L, int narg)
Definition l_base.h:97
The thread that runs SSCSM code.
Definition sscsm_environment.h:26
Definition s_base.h:66
Definition serverinventorymgr.h:17
Definition server.h:178