]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
7eb340d782e3a560b1abd8a1767d21f4ec6cc44b
[dragonfireclient.git] / src / script / lua_api / l_client.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "l_client.h"
22 #include "l_internal.h"
23 #include "util/string.h"
24 #include "cpp_api/s_base.h"
25
26 int ModApiClient::l_get_current_modname(lua_State *L)
27 {
28         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
29         return 1;
30 }
31
32 // get_last_run_mod()
33 int ModApiClient::l_get_last_run_mod(lua_State *L)
34 {
35         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
36         const char *current_mod = lua_tostring(L, -1);
37         if (current_mod == NULL || current_mod[0] == '\0') {
38                 lua_pop(L, 1);
39                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
40         }
41         return 1;
42 }
43
44 // set_last_run_mod(modname)
45 int ModApiClient::l_set_last_run_mod(lua_State *L)
46 {
47         const char *mod = lua_tostring(L, 1);
48         getScriptApiBase(L)->setOriginDirect(mod);
49         return 0;
50 }
51
52 // display_chat_message(message)
53 int ModApiClient::l_display_chat_message(lua_State *L)
54 {
55         NO_MAP_LOCK_REQUIRED;
56
57         std::string message = luaL_checkstring(L, 1);
58         getClient(L)->pushToChatQueue(utf8_to_wide(message));
59         return 1;
60 }
61
62 void ModApiClient::Initialize(lua_State *L, int top)
63 {
64         API_FCT(get_current_modname);
65         API_FCT(display_chat_message);
66         API_FCT(set_last_run_mod);
67         API_FCT(get_last_run_mod);
68 }