Luanti 5.15.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;
26
27class ModApiBase : protected LuaHelper {
28public:
29 static ScriptApiBase* getScriptApiBase(lua_State *L);
30 static Server* getServer(lua_State *L);
32 #if CHECK_CLIENT_BUILD()
33 static Client* getClient(lua_State *L);
34 static GUIEngine* getGuiEngine(lua_State *L);
35 #endif // !SERVER
36 static EmergeThread* getEmergeThread(lua_State *L);
37
38 static IGameDef* getGameDef(lua_State *L);
39
40 static Environment* getEnv(lua_State *L);
41
42 // When we are not loading the mod, this function returns "."
43 static std::string getCurrentModPath(lua_State *L);
44
45 // Get an arbitrary subclass of ScriptApiBase
46 // by using dynamic_cast<> on getScriptApiBase()
47 template<typename T>
48 static T* getScriptApi(lua_State *L) {
49 ScriptApiBase *scriptIface = getScriptApiBase(L);
50 T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
51 if (!scriptIfaceDowncast) {
52 throw LuaError("Requested unavailable ScriptApi - core engine bug!");
53 }
54 return scriptIfaceDowncast;
55 }
56
57 static bool registerFunction(lua_State *L,
58 const char* name,
59 lua_CFunction func,
60 int top);
61
62 template<typename T>
63 static void registerClass(lua_State *L,
64 const luaL_Reg *methods,
65 const luaL_Reg *metamethods)
66 {
67 luaL_newmetatable(L, T::className);
68 luaL_register(L, NULL, metamethods);
69 int metatable = lua_gettop(L);
70
71 lua_newtable(L);
72 luaL_register(L, NULL, methods);
73 int methodtable = lua_gettop(L);
74
75 lua_pushvalue(L, methodtable);
76 lua_setfield(L, metatable, "__index");
77
78 lua_getfield(L, metatable, "__tostring");
79 bool default_tostring = lua_isnil(L, -1);
80 lua_pop(L, 1);
81 if (default_tostring) {
82 lua_pushcfunction(L, ModApiBase::defaultToString<T>);
83 lua_setfield(L, metatable, "__tostring");
84 }
85
86 // Protect the real metatable.
87 lua_pushvalue(L, methodtable);
88 lua_setfield(L, metatable, "__metatable");
89
90 // Pop methodtable and metatable.
91 lua_pop(L, 2);
92 }
93
94 template<typename T>
95 static inline T *checkObject(lua_State *L, int narg)
96 {
97 return *reinterpret_cast<T**>(luaL_checkudata(L, narg, T::className));
98 }
99
113 static int l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func);
114
115private:
116
117 template<typename T>
118 static int defaultToString(lua_State *L)
119 {
120 auto *t = checkObject<T>(L, 1);
121 lua_pushfstring(L, "%s: %p", T::className, t);
122 return 1;
123 }
124};
Definition client.h:106
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:27
static bool registerFunction(lua_State *L, const char *name, lua_CFunction func, int top)
Definition l_base.cpp:81
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:92
static void registerClass(lua_State *L, const luaL_Reg *methods, const luaL_Reg *metamethods)
Definition l_base.h:63
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:67
static int defaultToString(lua_State *L)
Definition l_base.h:118
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:48
static EmergeThread * getEmergeThread(lua_State *L)
Definition l_base.cpp:62
static T * checkObject(lua_State *L, int narg)
Definition l_base.h:95
Definition s_base.h:63
Definition serverinventorymgr.h:17
Definition server.h:178