]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
9a04bd02ff9a8350e4f0ce848fa85bd760b7b24b
[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 #include "gettext.h"
26
27 int ModApiClient::l_get_current_modname(lua_State *L)
28 {
29         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
30         return 1;
31 }
32
33 // get_last_run_mod()
34 int ModApiClient::l_get_last_run_mod(lua_State *L)
35 {
36         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
37         const char *current_mod = lua_tostring(L, -1);
38         if (current_mod == NULL || current_mod[0] == '\0') {
39                 lua_pop(L, 1);
40                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
41         }
42         return 1;
43 }
44
45 // set_last_run_mod(modname)
46 int ModApiClient::l_set_last_run_mod(lua_State *L)
47 {
48         if (!lua_isstring(L, 1))
49                 return 0;
50
51         const char *mod = lua_tostring(L, 1);
52         getScriptApiBase(L)->setOriginDirect(mod);
53         lua_pushboolean(L, true);
54         return 1;
55 }
56
57 // display_chat_message(message)
58 int ModApiClient::l_display_chat_message(lua_State *L)
59 {
60         if (!lua_isstring(L, 1))
61                 return 0;
62
63         std::string message = luaL_checkstring(L, 1);
64         getClient(L)->pushToChatQueue(utf8_to_wide(message));
65         lua_pushboolean(L, true);
66         return 1;
67 }
68
69 // show_formspec(formspec)
70 int ModApiClient::l_show_formspec(lua_State *L)
71 {
72         if ( !lua_isstring(L, 1) || !lua_isstring(L, 2) )
73                 return 0;
74
75         ClientEvent event;
76         event.type = CE_SHOW_LOCAL_FORMSPEC;
77         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
78         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
79         getClient(L)->pushToEventQueue(event);
80         lua_pushboolean(L, true);
81         return 1;
82 }
83
84 // send_respawn()
85 int ModApiClient::l_send_respawn(lua_State *L)
86 {
87         getClient(L)->sendRespawn();
88         return 0;
89 }
90
91 // gettext(text)
92 int ModApiClient::l_gettext(lua_State *L)
93 {
94         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
95         lua_pushstring(L, text.c_str());
96
97         return 1;
98 }
99
100 void ModApiClient::Initialize(lua_State *L, int top)
101 {
102         API_FCT(get_current_modname);
103         API_FCT(display_chat_message);
104         API_FCT(set_last_run_mod);
105         API_FCT(get_last_run_mod);
106         API_FCT(show_formspec);
107         API_FCT(send_respawn);
108         API_FCT(gettext);
109 }