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