]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_base.cpp
script: Put getGuiEngine() inside a client-only #ifdef
[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 "profiler.h"
25 #include "server.h"
26 #include <algorithm>
27 #include <cmath>
28
29 ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
30 {
31         // Get server from registry
32         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
33         ScriptApiBase *sapi_ptr;
34 #if INDIRECT_SCRIPTAPI_RIDX
35         sapi_ptr = (ScriptApiBase*) *(void**)(lua_touserdata(L, -1));
36 #else
37         sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
38 #endif
39         lua_pop(L, 1);
40         return sapi_ptr;
41 }
42
43 Server *ModApiBase::getServer(lua_State *L)
44 {
45         return getScriptApiBase(L)->getServer();
46 }
47
48 #ifndef SERVER
49 Client *ModApiBase::getClient(lua_State *L)
50 {
51         return getScriptApiBase(L)->getClient();
52 }
53 #endif
54
55 IGameDef *ModApiBase::getGameDef(lua_State *L)
56 {
57         return getScriptApiBase(L)->getGameDef();
58 }
59
60 Environment *ModApiBase::getEnv(lua_State *L)
61 {
62         return getScriptApiBase(L)->getEnv();
63 }
64
65 #ifndef SERVER
66 GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
67 {
68         return getScriptApiBase(L)->getGuiEngine();
69 }
70 #endif
71
72 std::string ModApiBase::getCurrentModPath(lua_State *L)
73 {
74         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
75         std::string current_mod_name = readParam<std::string>(L, -1, "");
76         if (current_mod_name.empty())
77                 return ".";
78
79         const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
80         if (!mod)
81                 return ".";
82
83         return mod->path;
84 }
85
86
87 bool ModApiBase::registerFunction(lua_State *L, const char *name,
88                 lua_CFunction func, int top)
89 {
90         // TODO: Check presence first!
91
92         lua_pushcfunction(L, func);
93         lua_setfield(L, top, name);
94
95         return true;
96 }
97
98 std::unordered_map<std::string, luaL_Reg> ModApiBase::m_deprecated_wrappers;
99 bool ModApiBase::m_error_deprecated_calls = false;
100
101 int ModApiBase::l_deprecated_function(lua_State *L)
102 {
103         thread_local std::vector<u64> deprecated_logged;
104
105         u64 start_time = porting::getTimeUs();
106         lua_Debug ar;
107
108         // Get function name for lookup
109         FATAL_ERROR_IF(!lua_getstack(L, 0, &ar), "lua_getstack() failed");
110         FATAL_ERROR_IF(!lua_getinfo(L, "n", &ar), "lua_getinfo() failed");
111
112         // Combine name with line and script backtrace
113         FATAL_ERROR_IF(!lua_getstack(L, 1, &ar), "lua_getstack() failed");
114         FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
115
116         // Get parent class to get the wrappers map
117         luaL_checktype(L, 1, LUA_TUSERDATA);
118         void *ud = lua_touserdata(L, 1);
119         ModApiBase *o = *(ModApiBase**)ud;
120
121         // New function and new function name
122         auto it = o->m_deprecated_wrappers.find(ar.name);
123
124         // Get backtrace and hash it to reduce the warning flood
125         std::string backtrace = ar.short_src;
126         backtrace.append(":").append(std::to_string(ar.currentline));
127         u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
128
129         if (std::find(deprecated_logged.begin(), deprecated_logged.end(), hash)
130                         == deprecated_logged.end()) {
131
132                 deprecated_logged.emplace_back(hash);
133                 warningstream << "Call to deprecated function '"  << ar.name << "', please use '"
134                         << it->second.name << "' at " << backtrace << std::endl;
135
136                 if (m_error_deprecated_calls)
137                         script_error(L, LUA_ERRRUN, NULL, NULL);
138         }
139
140         u64 end_time = porting::getTimeUs();
141         g_profiler->avg("l_deprecated_function", end_time - start_time);
142
143         return it->second.func(L);
144 }
145
146 void ModApiBase::markAliasDeprecated(luaL_Reg *reg)
147 {
148         std::string value = g_settings->get("deprecated_lua_api_handling");
149         m_error_deprecated_calls = value == "error";
150
151         if (!m_error_deprecated_calls && value != "log")
152                 return;
153
154         const char *last_name = nullptr;
155         lua_CFunction last_func = nullptr;
156
157         // ! Null termination !
158         while (reg->func) {
159                 if (last_func == reg->func) {
160                         // Duplicate found
161                         luaL_Reg original_reg;
162                         // Do not inline struct. Breaks MSVC or is error-prone
163                         original_reg.name = last_name;
164                         original_reg.func = reg->func;
165                         m_deprecated_wrappers.emplace(
166                                 std::pair<std::string, luaL_Reg>(reg->name, original_reg));
167                         reg->func = l_deprecated_function;
168                 }
169
170                 last_func = reg->func;
171                 last_name = reg->name;
172                 ++reg;
173         }
174 }