3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
29 ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
31 // Get server from registry
32 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
33 ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
38 Server *ModApiBase::getServer(lua_State *L)
40 return getScriptApiBase(L)->getServer();
44 Client *ModApiBase::getClient(lua_State *L)
46 return getScriptApiBase(L)->getClient();
50 IGameDef *ModApiBase::getGameDef(lua_State *L)
52 return getScriptApiBase(L)->getGameDef();
55 Environment *ModApiBase::getEnv(lua_State *L)
57 return getScriptApiBase(L)->getEnv();
60 GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
62 return getScriptApiBase(L)->getGuiEngine();
65 std::string ModApiBase::getCurrentModPath(lua_State *L)
67 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
68 std::string current_mod_name = readParam<std::string>(L, -1, "");
69 if (current_mod_name.empty())
72 const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
80 bool ModApiBase::registerFunction(lua_State *L, const char *name,
81 lua_CFunction func, int top)
83 // TODO: Check presence first!
85 lua_pushcfunction(L, func);
86 lua_setfield(L, top, name);
91 std::unordered_map<std::string, luaL_Reg> ModApiBase::m_deprecated_wrappers;
92 bool ModApiBase::m_error_deprecated_calls = false;
94 int ModApiBase::l_deprecated_function(lua_State *L)
96 thread_local std::vector<u64> deprecated_logged;
98 u64 start_time = porting::getTimeUs();
101 // Get function name for lookup
102 FATAL_ERROR_IF(!lua_getstack(L, 0, &ar), "lua_getstack() failed");
103 FATAL_ERROR_IF(!lua_getinfo(L, "n", &ar), "lua_getinfo() failed");
105 // Combine name with line and script backtrace
106 FATAL_ERROR_IF(!lua_getstack(L, 1, &ar), "lua_getstack() failed");
107 FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
109 // Get parent class to get the wrappers map
110 luaL_checktype(L, 1, LUA_TUSERDATA);
111 void *ud = lua_touserdata(L, 1);
112 ModApiBase *o = *(ModApiBase**)ud;
114 // New function and new function name
115 auto it = o->m_deprecated_wrappers.find(ar.name);
117 // Get backtrace and hash it to reduce the warning flood
118 std::string backtrace = ar.short_src;
119 backtrace.append(":").append(std::to_string(ar.currentline));
120 u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
122 if (std::find(deprecated_logged.begin(), deprecated_logged.end(), hash)
123 == deprecated_logged.end()) {
125 deprecated_logged.emplace_back(hash);
126 warningstream << "Call to deprecated function '" << ar.name << "', please use '"
127 << it->second.name << "' at " << backtrace << std::endl;
129 if (m_error_deprecated_calls)
130 script_error(L, LUA_ERRRUN, NULL, NULL);
133 u64 end_time = porting::getTimeUs();
134 g_profiler->avg("l_deprecated_function", end_time - start_time);
136 return it->second.func(L);
139 void ModApiBase::markAliasDeprecated(luaL_Reg *reg)
141 std::string value = g_settings->get("deprecated_lua_api_handling");
142 m_error_deprecated_calls = value == "error";
144 if (!m_error_deprecated_calls && value != "log")
147 const char *last_name = nullptr;
148 lua_CFunction last_func = nullptr;
150 // ! Null termination !
152 if (last_func == reg->func) {
154 std::pair<std::string, luaL_Reg> entry(
156 { .name = last_name, .func = reg->func }
158 m_deprecated_wrappers.emplace(entry);
159 reg->func = l_deprecated_function;
162 last_func = reg->func;
163 last_name = reg->name;