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;
34 #if INDIRECT_SCRIPTAPI_RIDX
35 sapi_ptr = (ScriptApiBase*) *(void**)(lua_touserdata(L, -1));
37 sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
43 Server *ModApiBase::getServer(lua_State *L)
45 return getScriptApiBase(L)->getServer();
48 ServerInventoryManager *ModApiBase::getServerInventoryMgr(lua_State *L)
50 return getScriptApiBase(L)->getServer()->getInventoryMgr();
54 Client *ModApiBase::getClient(lua_State *L)
56 return getScriptApiBase(L)->getClient();
59 Game *ModApiBase::getGame(lua_State *L)
61 return getScriptApiBase(L)->getGame();
65 IGameDef *ModApiBase::getGameDef(lua_State *L)
67 return getScriptApiBase(L)->getGameDef();
70 Environment *ModApiBase::getEnv(lua_State *L)
72 return getScriptApiBase(L)->getEnv();
76 GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
78 return getScriptApiBase(L)->getGuiEngine();
82 std::string ModApiBase::getCurrentModPath(lua_State *L)
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())
89 const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
97 bool ModApiBase::registerFunction(lua_State *L, const char *name,
98 lua_CFunction func, int top)
100 // TODO: Check presence first!
102 lua_pushcfunction(L, func);
103 lua_setfield(L, top, name);
108 int ModApiBase::l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func)
110 thread_local std::vector<u64> deprecated_logged;
112 DeprecatedHandlingMode dep_mode = get_deprecated_handling_mode();
113 if (dep_mode == DeprecatedHandlingMode::Ignore)
116 u64 start_time = porting::getTimeUs();
119 // Get caller name with line and script backtrace
120 FATAL_ERROR_IF(!lua_getstack(L, 1, &ar), "lua_getstack() failed");
121 FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
123 // Get backtrace and hash it to reduce the warning flood
124 std::string backtrace = ar.short_src;
125 backtrace.append(":").append(std::to_string(ar.currentline));
126 u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
128 if (std::find(deprecated_logged.begin(), deprecated_logged.end(), hash)
129 == deprecated_logged.end()) {
131 deprecated_logged.emplace_back(hash);
132 warningstream << "Call to deprecated function '" << bad << "', please use '"
133 << good << "' at " << backtrace << std::endl;
135 if (dep_mode == DeprecatedHandlingMode::Error)
136 script_error(L, LUA_ERRRUN, NULL, NULL);
139 u64 end_time = porting::getTimeUs();
140 g_profiler->avg("l_deprecated_function", end_time - start_time);