]> git.lizzy.rs Git - minetest.git/blob - src/remoteplayer.cpp
Fix mem leak in mesh cache (#5781)
[minetest.git] / src / remoteplayer.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2014-2016 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 "remoteplayer.h"
22 #include <json/json.h>
23 #include "content_sao.h"
24 #include "filesys.h"
25 #include "gamedef.h"
26 #include "porting.h"  // strlcpy
27 #include "server.h"
28 #include "settings.h"
29
30 /*
31         RemotePlayer
32 */
33 // static config cache for remoteplayer
34 bool RemotePlayer::m_setting_cache_loaded = false;
35 float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
36 u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0;
37
38 RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
39         Player(name, idef),
40         protocol_version(0),
41         m_sao(NULL),
42         m_dirty(false),
43         m_last_chat_message_sent(time(NULL)),
44         m_chat_message_allowance(5.0f),
45         m_message_rate_overhead(0),
46         hud_hotbar_image(""),
47         hud_hotbar_selected_image("")
48 {
49         if (!RemotePlayer::m_setting_cache_loaded) {
50                 RemotePlayer::m_setting_chat_message_limit_per_10sec =
51                         g_settings->getFloat("chat_message_limit_per_10sec");
52                 RemotePlayer::m_setting_chat_message_limit_trigger_kick =
53                         g_settings->getU16("chat_message_limit_trigger_kick");
54                 RemotePlayer::m_setting_cache_loaded = true;
55         }
56         movement_acceleration_default   = g_settings->getFloat("movement_acceleration_default")   * BS;
57         movement_acceleration_air       = g_settings->getFloat("movement_acceleration_air")       * BS;
58         movement_acceleration_fast      = g_settings->getFloat("movement_acceleration_fast")      * BS;
59         movement_speed_walk             = g_settings->getFloat("movement_speed_walk")             * BS;
60         movement_speed_crouch           = g_settings->getFloat("movement_speed_crouch")           * BS;
61         movement_speed_fast             = g_settings->getFloat("movement_speed_fast")             * BS;
62         movement_speed_climb            = g_settings->getFloat("movement_speed_climb")            * BS;
63         movement_speed_jump             = g_settings->getFloat("movement_speed_jump")             * BS;
64         movement_liquid_fluidity        = g_settings->getFloat("movement_liquid_fluidity")        * BS;
65         movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS;
66         movement_liquid_sink            = g_settings->getFloat("movement_liquid_sink")            * BS;
67         movement_gravity                = g_settings->getFloat("movement_gravity")                * BS;
68
69         // copy defaults
70         m_cloud_params.density = 0.4f;
71         m_cloud_params.color_bright = video::SColor(229, 240, 240, 255);
72         m_cloud_params.color_ambient = video::SColor(255, 0, 0, 0);
73         m_cloud_params.height = 120.0f;
74         m_cloud_params.thickness = 16.0f;
75         m_cloud_params.speed = v2f(0.0f, -2.0f);
76 }
77
78 void RemotePlayer::serializeExtraAttributes(std::string &output)
79 {
80         assert(m_sao);
81         Json::Value json_root;
82         const PlayerAttributes &attrs = m_sao->getExtendedAttributes();
83         for (PlayerAttributes::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
84                 json_root[(*it).first] = (*it).second;
85         }
86
87         Json::FastWriter writer;
88         output = writer.write(json_root);
89         m_sao->setExtendedAttributeModified(false);
90 }
91
92
93 void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
94                 PlayerSAO *sao)
95 {
96         Settings args;
97
98         if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
99                 throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
100         }
101
102         m_dirty = true;
103         //args.getS32("version"); // Version field value not used
104         const std::string &name = args.get("name");
105         strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
106
107         if (sao) {
108                 try {
109                         sao->setHPRaw(args.getS32("hp"));
110                 } catch(SettingNotFoundException &e) {
111                         sao->setHPRaw(PLAYER_MAX_HP);
112                 }
113
114                 try {
115                         sao->setBasePosition(args.getV3F("position"));
116                 } catch (SettingNotFoundException &e) {}
117
118                 try {
119                         sao->setPitch(args.getFloat("pitch"));
120                 } catch (SettingNotFoundException &e) {}
121                 try {
122                         sao->setYaw(args.getFloat("yaw"));
123                 } catch (SettingNotFoundException &e) {}
124
125                 try {
126                         sao->setBreath(args.getS32("breath"), false);
127                 } catch (SettingNotFoundException &e) {}
128
129                 try {
130                         const std::string &extended_attributes = args.get("extended_attributes");
131                         Json::Reader reader;
132                         Json::Value attr_root;
133                         reader.parse(extended_attributes, attr_root);
134
135                         const Json::Value::Members attr_list = attr_root.getMemberNames();
136                         for (Json::Value::Members::const_iterator it = attr_list.begin();
137                                         it != attr_list.end(); ++it) {
138                                 Json::Value attr_value = attr_root[*it];
139                                 sao->setExtendedAttribute(*it, attr_value.asString());
140                         }
141                 } catch (SettingNotFoundException &e) {}
142         }
143
144         inventory.deSerialize(is);
145
146         if (inventory.getList("craftpreview") == NULL) {
147                 // Convert players without craftpreview
148                 inventory.addList("craftpreview", 1);
149
150                 bool craftresult_is_preview = true;
151                 if(args.exists("craftresult_is_preview"))
152                         craftresult_is_preview = args.getBool("craftresult_is_preview");
153                 if(craftresult_is_preview)
154                 {
155                         // Clear craftresult
156                         inventory.getList("craftresult")->changeItem(0, ItemStack());
157                 }
158         }
159 }
160
161 void RemotePlayer::serialize(std::ostream &os)
162 {
163         // Utilize a Settings object for storing values
164         Settings args;
165         args.setS32("version", 1);
166         args.set("name", m_name);
167
168         // This should not happen
169         assert(m_sao);
170         args.setS32("hp", m_sao->getHP());
171         args.setV3F("position", m_sao->getBasePosition());
172         args.setFloat("pitch", m_sao->getPitch());
173         args.setFloat("yaw", m_sao->getYaw());
174         args.setS32("breath", m_sao->getBreath());
175
176         std::string extended_attrs = "";
177         serializeExtraAttributes(extended_attrs);
178         args.set("extended_attributes", extended_attrs);
179
180         args.writeLines(os);
181
182         os<<"PlayerArgsEnd\n";
183
184         inventory.serialize(os);
185 }
186
187 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
188 {
189         // Rate limit messages
190         u32 now = time(NULL);
191         float time_passed = now - m_last_chat_message_sent;
192         m_last_chat_message_sent = now;
193
194         // If this feature is disabled
195         if (m_setting_chat_message_limit_per_10sec <= 0.0) {
196                 return RPLAYER_CHATRESULT_OK;
197         }
198
199         m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
200         if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
201                 m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
202         }
203
204         if (m_chat_message_allowance < 1.0f) {
205                 infostream << "Player " << m_name
206                                 << " chat limited due to excessive message amount." << std::endl;
207
208                 // Kick player if flooding is too intensive
209                 m_message_rate_overhead++;
210                 if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
211                         return RPLAYER_CHATRESULT_KICK;
212                 }
213
214                 return RPLAYER_CHATRESULT_FLOODING;
215         }
216
217         // Reinit message overhead
218         if (m_message_rate_overhead > 0) {
219                 m_message_rate_overhead = 0;
220         }
221
222         m_chat_message_allowance -= 1.0f;
223         return RPLAYER_CHATRESULT_OK;
224 }