]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_base.cpp
Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[dragonfireclient.git] / src / script / lua_api / l_base.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "lua_api/l_base.h"
21 #include "lua_api/l_internal.h"
22 #include "cpp_api/s_base.h"
23 #include "content/mods.h"
24 #include "server.h"
25 #include <cmath>
26
27 ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
28 {
29         // Get server from registry
30         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
31         ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
32         lua_pop(L, 1);
33         return sapi_ptr;
34 }
35
36 Server *ModApiBase::getServer(lua_State *L)
37 {
38         return getScriptApiBase(L)->getServer();
39 }
40
41 #ifndef SERVER
42 Client *ModApiBase::getClient(lua_State *L)
43 {
44         return getScriptApiBase(L)->getClient();
45 }
46 #endif
47
48 IGameDef *ModApiBase::getGameDef(lua_State *L)
49 {
50         return getScriptApiBase(L)->getGameDef();
51 }
52
53 Environment *ModApiBase::getEnv(lua_State *L)
54 {
55         return getScriptApiBase(L)->getEnv();
56 }
57
58 GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
59 {
60         return getScriptApiBase(L)->getGuiEngine();
61 }
62
63 std::string ModApiBase::getCurrentModPath(lua_State *L)
64 {
65         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
66         std::string current_mod_name = readParam<std::string>(L, -1, "");
67         if (current_mod_name.empty())
68                 return ".";
69
70         const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
71         if (!mod)
72                 return ".";
73
74         return mod->path;
75 }
76
77
78 bool ModApiBase::registerFunction(lua_State *L, const char *name,
79                 lua_CFunction func, int top)
80 {
81         // TODO: Check presence first!
82
83         lua_pushcfunction(L, func);
84         lua_setfield(L, top, name);
85
86         return true;
87 }