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.
24 #include <IFileSystem.h>
25 #include "threading/mutex_auto_lock.h"
26 #include "util/auth.h"
27 #include "util/directiontables.h"
28 #include "util/pointedthing.h"
29 #include "util/serialize.h"
30 #include "util/string.h"
33 #include "network/clientopcodes.h"
35 #include "mapblock_mesh.h"
41 #include "clientmap.h"
42 #include "clientmedia.h"
44 #include "drawscene.h"
45 #include "database-sqlite3.h"
46 #include "serialization.h"
47 #include "guiscalingfilter.h"
48 #include "script/scripting_client.h"
51 extern gui::IGUIEnvironment* guienv;
58 IrrlichtDevice *device,
59 const char *playername,
60 const std::string &password,
61 const std::string &address_name,
62 MapDrawControl &control,
63 IWritableTextureSource *tsrc,
64 IWritableShaderSource *shsrc,
65 IWritableItemDefManager *itemdef,
66 IWritableNodeDefManager *nodedef,
68 MtEventManager *event,
70 GameUIFlags *game_ui_flags
72 m_packetcounter_timer(0.0),
73 m_connection_reinit_timer(0.1),
75 m_playerpos_send_timer(0.0),
76 m_ignore_damage_timer(0.0),
83 m_mesh_update_thread(this),
85 new ClientMap(this, control,
86 device->getSceneManager()->getRootSceneNode(),
87 device->getSceneManager(), 666),
88 device->getSceneManager(),
91 m_particle_manager(&m_env),
92 m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
93 m_address_name(address_name),
97 m_minimap_disabled_by_server(false),
98 m_server_ser_ver(SER_FMT_VER_INVALID),
101 m_inventory_updated(false),
102 m_inventory_from_server(NULL),
103 m_inventory_from_server_age(0.0),
107 m_last_chat_message_sent(time(NULL)),
108 m_chat_message_allowance(5.0f),
110 m_password(password),
111 m_chosen_auth_mech(AUTH_MECHANISM_NONE),
113 m_access_denied(false),
114 m_access_denied_reconnect(false),
115 m_itemdef_received(false),
116 m_nodedef_received(false),
117 m_media_downloader(new ClientMediaDownloader()),
118 m_time_of_day_set(false),
119 m_last_time_of_day_f(-1),
120 m_time_of_day_update_timer(0),
121 m_recommended_send_interval(0.1),
122 m_removed_sounds_check_timer(0),
126 m_mod_storage_save_timer(10.0f),
127 m_game_ui_flags(game_ui_flags),
131 m_env.setLocalPlayer(new LocalPlayer(this, playername));
133 if (g_settings->getBool("enable_minimap")) {
134 m_minimap = new Minimap(device, this);
136 m_cache_save_interval = g_settings->getU16("server_map_save_interval");
138 m_modding_enabled = g_settings->getBool("enable_client_modding");
139 m_script = new ClientScripting(this);
140 m_env.setScript(m_script);
141 m_script->setEnv(&m_env);
144 void Client::initMods()
146 m_script->loadMod(getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME);
148 // If modding is not enabled, don't load mods, just builtin
149 if (!m_modding_enabled) {
153 ClientModConfiguration modconf(getClientModsLuaPath());
154 std::vector<ModSpec> mods = modconf.getMods();
155 std::vector<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
156 // complain about mods with unsatisfied dependencies
157 if (!modconf.isConsistent()) {
158 modconf.printUnsatisfiedModsError();
162 infostream << "Client Loading mods: ";
163 for (std::vector<ModSpec>::const_iterator i = mods.begin();
164 i != mods.end(); ++i) {
165 infostream << (*i).name << " ";
168 infostream << std::endl;
169 // Load and run "mod" scripts
170 for (std::vector<ModSpec>::const_iterator it = mods.begin();
171 it != mods.end(); ++it) {
172 const ModSpec &mod = *it;
173 if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
174 throw ModError("Error loading mod \"" + mod.name +
175 "\": Mod name does not follow naming conventions: "
176 "Only characters [a-z0-9_] are allowed.");
178 std::string script_path = mod.path + DIR_DELIM + "init.lua";
179 infostream << " [" << padStringRight(mod.name, 12) << "] [\""
180 << script_path << "\"]" << std::endl;
181 m_script->loadMod(script_path, mod.name);
185 const std::string &Client::getBuiltinLuaPath()
187 static const std::string builtin_dir = porting::path_share + DIR_DELIM + "builtin";
191 const std::string &Client::getClientModsLuaPath()
193 static const std::string clientmods_dir = porting::path_share + DIR_DELIM + "clientmods";
194 return clientmods_dir;
197 const std::vector<ModSpec>& Client::getMods() const
199 static std::vector<ModSpec> client_modspec_temp;
200 return client_modspec_temp;
203 const ModSpec* Client::getModSpec(const std::string &modname) const
211 // Don't disable this part when modding is disabled, it's used in builtin
212 m_script->on_shutdown();
213 //request all client managed threads to stop
214 m_mesh_update_thread.stop();
215 // Save local server map
217 infostream << "Local map saving ended." << std::endl;
218 m_localdb->endSave();
224 bool Client::isShutdown()
226 return m_shutdown || !m_mesh_update_thread.isRunning();
234 m_mesh_update_thread.stop();
235 m_mesh_update_thread.wait();
236 while (!m_mesh_update_thread.m_queue_out.empty()) {
237 MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
242 delete m_inventory_from_server;
244 // Delete detached inventories
245 for (std::unordered_map<std::string, Inventory*>::iterator
246 i = m_detached_inventories.begin();
247 i != m_detached_inventories.end(); ++i) {
251 // cleanup 3d model meshes on client shutdown
252 while (m_device->getSceneManager()->getMeshCache()->getMeshCount() != 0) {
253 scene::IAnimatedMesh *mesh =
254 m_device->getSceneManager()->getMeshCache()->getMeshByIndex(0);
257 m_device->getSceneManager()->getMeshCache()->removeMesh(mesh);
263 void Client::connect(Address address, bool is_local_server)
265 DSTACK(FUNCTION_NAME);
267 initLocalMapSaving(address, m_address_name, is_local_server);
269 m_con.SetTimeoutMs(0);
270 m_con.Connect(address);
273 void Client::step(float dtime)
275 DSTACK(FUNCTION_NAME);
281 if(m_ignore_damage_timer > dtime)
282 m_ignore_damage_timer -= dtime;
284 m_ignore_damage_timer = 0.0;
286 m_animation_time += dtime;
287 if(m_animation_time > 60.0)
288 m_animation_time -= 60.0;
290 m_time_of_day_update_timer += dtime;
298 float &counter = m_packetcounter_timer;
304 infostream << "Client packetcounter (" << m_packetcounter_timer
306 m_packetcounter.print(infostream);
307 m_packetcounter.clear();
311 // UGLY hack to fix 2 second startup delay caused by non existent
312 // server client startup synchronization in local server or singleplayer mode
313 static bool initial_step = true;
315 initial_step = false;
317 else if(m_state == LC_Created) {
318 float &counter = m_connection_reinit_timer;
323 LocalPlayer *myplayer = m_env.getLocalPlayer();
324 FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment.");
326 u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
327 CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
329 if (proto_version_min < 25) {
330 // Send TOSERVER_INIT_LEGACY
331 // [0] u16 TOSERVER_INIT_LEGACY
332 // [2] u8 SER_FMT_VER_HIGHEST_READ
333 // [3] u8[20] player_name
334 // [23] u8[28] password (new in some version)
335 // [51] u16 minimum supported network protocol version (added sometime)
336 // [53] u16 maximum supported network protocol version (added later than the previous one)
338 char pName[PLAYERNAME_SIZE];
339 char pPassword[PASSWORD_SIZE];
340 memset(pName, 0, PLAYERNAME_SIZE * sizeof(char));
341 memset(pPassword, 0, PASSWORD_SIZE * sizeof(char));
343 std::string hashed_password = translate_password(myplayer->getName(), m_password);
344 snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName());
345 snprintf(pPassword, PASSWORD_SIZE, "%s", hashed_password.c_str());
347 sendLegacyInit(pName, pPassword);
349 if (CLIENT_PROTOCOL_VERSION_MAX >= 25)
350 sendInit(myplayer->getName());
353 // Not connected, return
358 Do stuff if connected
362 Run Map's timers and unload unused data
364 const float map_timer_and_unload_dtime = 5.25;
365 if(m_map_timer_and_unload_interval.step(dtime, map_timer_and_unload_dtime)) {
366 ScopeProfiler sp(g_profiler, "Client: map timer and unload");
367 std::vector<v3s16> deleted_blocks;
368 m_env.getMap().timerUpdate(map_timer_and_unload_dtime,
369 g_settings->getFloat("client_unload_unused_data_timeout"),
370 g_settings->getS32("client_mapblock_limit"),
375 NOTE: This loop is intentionally iterated the way it is.
378 std::vector<v3s16>::iterator i = deleted_blocks.begin();
379 std::vector<v3s16> sendlist;
381 if(sendlist.size() == 255 || i == deleted_blocks.end()) {
392 sendDeletedBlocks(sendlist);
394 if(i == deleted_blocks.end())
400 sendlist.push_back(*i);
406 Send pending messages on out chat queue
408 if (!m_out_chat_queue.empty() && canSendChatMessage()) {
409 sendChatMessage(m_out_chat_queue.front());
410 m_out_chat_queue.pop();
416 // Control local player (0ms)
417 LocalPlayer *player = m_env.getLocalPlayer();
418 assert(player != NULL);
419 player->applyControl(dtime);
423 m_sound->step(dtime);
428 while (m_env.hasClientEnvEvents()) {
429 ClientEnvEvent envEvent = m_env.getClientEnvEvent();
431 if (envEvent.type == CEE_PLAYER_DAMAGE) {
432 if (m_ignore_damage_timer <= 0) {
433 u8 damage = envEvent.player_damage.amount;
435 if (envEvent.player_damage.send_to_server)
438 // Add to ClientEvent queue
440 event.type = CE_PLAYER_DAMAGE;
441 event.player_damage.amount = damage;
442 m_client_event_queue.push(event);
445 // Protocol v29 or greater obsoleted this event
446 else if (envEvent.type == CEE_PLAYER_BREATH && m_proto_ver < 29) {
447 u16 breath = envEvent.player_breath.amount;
455 float &counter = m_avg_rtt_timer;
459 // connectedAndInitialized() is true, peer exists.
460 float avg_rtt = getRTT();
461 infostream << "Client: avg_rtt=" << avg_rtt << std::endl;
465 Send player position to server
468 float &counter = m_playerpos_send_timer;
470 if((m_state == LC_Ready) && (counter >= m_recommended_send_interval))
478 Replace updated meshes
481 int num_processed_meshes = 0;
482 while (!m_mesh_update_thread.m_queue_out.empty())
484 num_processed_meshes++;
486 MinimapMapblock *minimap_mapblock = NULL;
487 bool do_mapper_update = true;
489 MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
490 MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
492 // Delete the old mesh
493 if (block->mesh != NULL) {
499 minimap_mapblock = r.mesh->moveMinimapMapblock();
500 if (minimap_mapblock == NULL)
501 do_mapper_update = false;
503 bool is_empty = true;
504 for (int l = 0; l < MAX_TILE_LAYERS; l++)
505 if (r.mesh->getMesh(l)->getMeshBufferCount() != 0)
511 // Replace with the new mesh
512 block->mesh = r.mesh;
518 if (m_minimap && do_mapper_update)
519 m_minimap->addBlock(r.p, minimap_mapblock);
521 if (r.ack_block_to_server) {
531 if (num_processed_meshes > 0)
532 g_profiler->graphAdd("num_processed_meshes", num_processed_meshes);
538 if (m_media_downloader && m_media_downloader->isStarted()) {
539 m_media_downloader->step(this);
540 if (m_media_downloader->isDone()) {
541 delete m_media_downloader;
542 m_media_downloader = NULL;
547 If the server didn't update the inventory in a while, revert
548 the local inventory (so the player notices the lag problem
549 and knows something is wrong).
551 if(m_inventory_from_server)
553 float interval = 10.0;
554 float count_before = floor(m_inventory_from_server_age / interval);
556 m_inventory_from_server_age += dtime;
558 float count_after = floor(m_inventory_from_server_age / interval);
560 if(count_after != count_before)
562 // Do this every <interval> seconds after TOCLIENT_INVENTORY
563 // Reset the locally changed inventory to the authoritative inventory
564 LocalPlayer *player = m_env.getLocalPlayer();
565 player->inventory = *m_inventory_from_server;
566 m_inventory_updated = true;
571 Update positions of sounds attached to objects
574 for(std::unordered_map<int, u16>::iterator i = m_sounds_to_objects.begin();
575 i != m_sounds_to_objects.end(); ++i) {
576 int client_id = i->first;
577 u16 object_id = i->second;
578 ClientActiveObject *cao = m_env.getActiveObject(object_id);
581 v3f pos = cao->getPosition();
582 m_sound->updateSoundPosition(client_id, pos);
587 Handle removed remotely initiated sounds
589 m_removed_sounds_check_timer += dtime;
590 if(m_removed_sounds_check_timer >= 2.32) {
591 m_removed_sounds_check_timer = 0;
592 // Find removed sounds and clear references to them
593 std::vector<s32> removed_server_ids;
594 for (std::unordered_map<s32, int>::iterator i = m_sounds_server_to_client.begin();
595 i != m_sounds_server_to_client.end();) {
596 s32 server_id = i->first;
597 int client_id = i->second;
599 if(!m_sound->soundExists(client_id)) {
600 m_sounds_server_to_client.erase(server_id);
601 m_sounds_client_to_server.erase(client_id);
602 m_sounds_to_objects.erase(client_id);
603 removed_server_ids.push_back(server_id);
608 if(!removed_server_ids.empty()) {
609 sendRemovedSounds(removed_server_ids);
613 m_mod_storage_save_timer -= dtime;
614 if (m_mod_storage_save_timer <= 0.0f) {
615 verbosestream << "Saving registered mod storages." << std::endl;
616 m_mod_storage_save_timer = g_settings->getFloat("server_map_save_interval");
617 for (std::unordered_map<std::string, ModMetadata *>::const_iterator
618 it = m_mod_storages.begin(); it != m_mod_storages.end(); ++it) {
619 if (it->second->isModified()) {
620 it->second->save(getModStoragePath());
626 if (m_localdb && m_localdb_save_interval.step(dtime,
627 m_cache_save_interval)) {
628 m_localdb->endSave();
629 m_localdb->beginSave();
633 bool Client::loadMedia(const std::string &data, const std::string &filename)
635 // Silly irrlicht's const-incorrectness
636 Buffer<char> data_rw(data.c_str(), data.size());
640 const char *image_ext[] = {
641 ".png", ".jpg", ".bmp", ".tga",
642 ".pcx", ".ppm", ".psd", ".wal", ".rgb",
645 name = removeStringEnd(filename, image_ext);
648 verbosestream<<"Client: Attempting to load image "
649 <<"file \""<<filename<<"\""<<std::endl;
651 io::IFileSystem *irrfs = m_device->getFileSystem();
652 video::IVideoDriver *vdrv = m_device->getVideoDriver();
654 // Create an irrlicht memory file
655 io::IReadFile *rfile = irrfs->createMemoryReadFile(
656 *data_rw, data_rw.getSize(), "_tempreadfile");
658 FATAL_ERROR_IF(!rfile, "Could not create irrlicht memory file.");
661 video::IImage *img = vdrv->createImageFromFile(rfile);
663 errorstream<<"Client: Cannot create image from data of "
664 <<"file \""<<filename<<"\""<<std::endl;
669 m_tsrc->insertSourceImage(filename, img);
676 const char *sound_ext[] = {
677 ".0.ogg", ".1.ogg", ".2.ogg", ".3.ogg", ".4.ogg",
678 ".5.ogg", ".6.ogg", ".7.ogg", ".8.ogg", ".9.ogg",
681 name = removeStringEnd(filename, sound_ext);
684 verbosestream<<"Client: Attempting to load sound "
685 <<"file \""<<filename<<"\""<<std::endl;
686 m_sound->loadSoundData(name, data);
690 const char *model_ext[] = {
691 ".x", ".b3d", ".md2", ".obj",
694 name = removeStringEnd(filename, model_ext);
697 verbosestream<<"Client: Storing model into memory: "
698 <<"\""<<filename<<"\""<<std::endl;
699 if(m_mesh_data.count(filename))
700 errorstream<<"Multiple models with name \""<<filename.c_str()
701 <<"\" found; replacing previous model"<<std::endl;
702 m_mesh_data[filename] = data;
706 errorstream<<"Client: Don't know how to load file \""
707 <<filename<<"\""<<std::endl;
711 // Virtual methods from con::PeerHandler
712 void Client::peerAdded(con::Peer *peer)
714 infostream << "Client::peerAdded(): peer->id="
715 << peer->id << std::endl;
717 void Client::deletingPeer(con::Peer *peer, bool timeout)
719 infostream << "Client::deletingPeer(): "
720 "Server Peer is getting deleted "
721 << "(timeout=" << timeout << ")" << std::endl;
724 m_access_denied = true;
725 m_access_denied_reason = gettext("Connection timed out.");
731 u16 number of files requested
737 void Client::request_media(const std::vector<std::string> &file_requests)
739 std::ostringstream os(std::ios_base::binary);
740 writeU16(os, TOSERVER_REQUEST_MEDIA);
741 size_t file_requests_size = file_requests.size();
743 FATAL_ERROR_IF(file_requests_size > 0xFFFF, "Unsupported number of file requests");
745 // Packet dynamicly resized
746 NetworkPacket pkt(TOSERVER_REQUEST_MEDIA, 2 + 0);
748 pkt << (u16) (file_requests_size & 0xFFFF);
750 for(std::vector<std::string>::const_iterator i = file_requests.begin();
751 i != file_requests.end(); ++i) {
757 infostream << "Client: Sending media request list to server ("
758 << file_requests.size() << " files. packet size)" << std::endl;
761 void Client::initLocalMapSaving(const Address &address,
762 const std::string &hostname,
763 bool is_local_server)
765 if (!g_settings->getBool("enable_local_map_saving") || is_local_server) {
769 const std::string world_path = porting::path_user
770 + DIR_DELIM + "worlds"
771 + DIR_DELIM + "server_"
772 + hostname + "_" + std::to_string(address.getPort());
774 fs::CreateAllDirs(world_path);
776 m_localdb = new MapDatabaseSQLite3(world_path);
777 m_localdb->beginSave();
778 actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl;
781 void Client::ReceiveAll()
783 DSTACK(FUNCTION_NAME);
784 u64 start_ms = porting::getTimeMs();
787 // Limit time even if there would be huge amounts of data to
789 if(porting::getTimeMs() > start_ms + 100)
794 g_profiler->graphAdd("client_received_packets", 1);
796 catch(con::NoIncomingDataException &e) {
799 catch(con::InvalidIncomingDataException &e) {
800 infostream<<"Client::ReceiveAll(): "
801 "InvalidIncomingDataException: what()="
802 <<e.what()<<std::endl;
807 void Client::Receive()
809 DSTACK(FUNCTION_NAME);
815 inline void Client::handleCommand(NetworkPacket* pkt)
817 const ToClientCommandHandler& opHandle = toClientCommandTable[pkt->getCommand()];
818 (this->*opHandle.handler)(pkt);
822 sender_peer_id given to this shall be quaranteed to be a valid peer
824 void Client::ProcessData(NetworkPacket *pkt)
826 DSTACK(FUNCTION_NAME);
828 ToClientCommand command = (ToClientCommand) pkt->getCommand();
829 u32 sender_peer_id = pkt->getPeerId();
831 //infostream<<"Client: received command="<<command<<std::endl;
832 m_packetcounter.add((u16)command);
835 If this check is removed, be sure to change the queue
836 system to know the ids
838 if(sender_peer_id != PEER_ID_SERVER) {
839 infostream << "Client::ProcessData(): Discarding data not "
840 "coming from server: peer_id=" << sender_peer_id
845 // Command must be handled into ToClientCommandHandler
846 if (command >= TOCLIENT_NUM_MSG_TYPES) {
847 infostream << "Client: Ignoring unknown command "
848 << command << std::endl;
853 * Those packets are handled before m_server_ser_ver is set, it's normal
854 * But we must use the new ToClientConnectionState in the future,
857 if(toClientCommandTable[command].state == TOCLIENT_STATE_NOT_CONNECTED) {
862 if(m_server_ser_ver == SER_FMT_VER_INVALID) {
863 infostream << "Client: Server serialization"
864 " format invalid or not initialized."
865 " Skipping incoming command=" << command << std::endl;
870 Handle runtime commands
876 void Client::Send(NetworkPacket* pkt)
878 m_con.Send(PEER_ID_SERVER,
879 serverCommandFactoryTable[pkt->getCommand()].channel,
881 serverCommandFactoryTable[pkt->getCommand()].reliable);
884 // Will fill up 12 + 12 + 4 + 4 + 4 bytes
885 void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *pkt)
887 v3f pf = myplayer->getPosition() * 100;
888 v3f sf = myplayer->getSpeed() * 100;
889 s32 pitch = myplayer->getPitch() * 100;
890 s32 yaw = myplayer->getYaw() * 100;
891 u32 keyPressed = myplayer->keyPressed;
892 // scaled by 80, so that pi can fit into a u8
893 u8 fov = clientMap->getCameraFov() * 80;
894 u8 wanted_range = MYMIN(255,
895 std::ceil(clientMap->getControl().wanted_range / MAP_BLOCKSIZE));
897 v3s32 position(pf.X, pf.Y, pf.Z);
898 v3s32 speed(sf.X, sf.Y, sf.Z);
902 [0] v3s32 position*100
904 [12+12] s32 pitch*100
905 [12+12+4] s32 yaw*100
906 [12+12+4+4] u32 keyPressed
907 [12+12+4+4+4] u8 fov*80
908 [12+12+4+4+4+1] u8 ceil(wanted_range / MAP_BLOCKSIZE)
910 *pkt << position << speed << pitch << yaw << keyPressed;
911 *pkt << fov << wanted_range;
914 void Client::interact(u8 action, const PointedThing& pointed)
916 if(m_state != LC_Ready) {
917 errorstream << "Client::interact() "
918 "Canceled (not connected)"
923 LocalPlayer *myplayer = m_env.getLocalPlayer();
924 if (myplayer == NULL)
931 [5] u32 length of the next item (plen)
932 [9] serialized PointedThing
933 [9 + plen] player position information
935 0: start digging (from undersurface) or use
936 1: stop digging (all parameters ignored)
938 3: place block or item (to abovesurface)
940 5: perform secondary action of item
943 NetworkPacket pkt(TOSERVER_INTERACT, 1 + 2 + 0);
946 pkt << (u16)getPlayerItem();
948 std::ostringstream tmp_os(std::ios::binary);
949 pointed.serialize(tmp_os);
951 pkt.putLongString(tmp_os.str());
953 writePlayerPos(myplayer, &m_env.getClientMap(), &pkt);
958 void Client::deleteAuthData()
963 switch (m_chosen_auth_mech) {
964 case AUTH_MECHANISM_FIRST_SRP:
966 case AUTH_MECHANISM_SRP:
967 case AUTH_MECHANISM_LEGACY_PASSWORD:
968 srp_user_delete((SRPUser *) m_auth_data);
971 case AUTH_MECHANISM_NONE:
974 m_chosen_auth_mech = AUTH_MECHANISM_NONE;
978 AuthMechanism Client::choseAuthMech(const u32 mechs)
980 if (mechs & AUTH_MECHANISM_SRP)
981 return AUTH_MECHANISM_SRP;
983 if (mechs & AUTH_MECHANISM_FIRST_SRP)
984 return AUTH_MECHANISM_FIRST_SRP;
986 if (mechs & AUTH_MECHANISM_LEGACY_PASSWORD)
987 return AUTH_MECHANISM_LEGACY_PASSWORD;
989 return AUTH_MECHANISM_NONE;
992 void Client::sendLegacyInit(const char* playerName, const char* playerPassword)
994 NetworkPacket pkt(TOSERVER_INIT_LEGACY,
995 1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
997 u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
998 CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
1000 pkt << (u8) SER_FMT_VER_HIGHEST_READ;
1001 pkt.putRawString(playerName,PLAYERNAME_SIZE);
1002 pkt.putRawString(playerPassword, PASSWORD_SIZE);
1003 pkt << (u16) proto_version_min << (u16) CLIENT_PROTOCOL_VERSION_MAX;
1008 void Client::sendInit(const std::string &playerName)
1010 NetworkPacket pkt(TOSERVER_INIT, 1 + 2 + 2 + (1 + playerName.size()));
1012 // we don't support network compression yet
1013 u16 supp_comp_modes = NETPROTO_COMPRESSION_NONE;
1015 u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
1016 CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
1018 pkt << (u8) SER_FMT_VER_HIGHEST_READ << (u16) supp_comp_modes;
1019 pkt << (u16) proto_version_min << (u16) CLIENT_PROTOCOL_VERSION_MAX;
1025 void Client::startAuth(AuthMechanism chosen_auth_mechanism)
1027 m_chosen_auth_mech = chosen_auth_mechanism;
1029 switch (chosen_auth_mechanism) {
1030 case AUTH_MECHANISM_FIRST_SRP: {
1031 // send srp verifier to server
1032 std::string verifier;
1034 generate_srp_verifier_and_salt(getPlayerName(), m_password,
1037 NetworkPacket resp_pkt(TOSERVER_FIRST_SRP, 0);
1038 resp_pkt << salt << verifier << (u8)((m_password == "") ? 1 : 0);
1043 case AUTH_MECHANISM_SRP:
1044 case AUTH_MECHANISM_LEGACY_PASSWORD: {
1047 if (chosen_auth_mechanism == AUTH_MECHANISM_LEGACY_PASSWORD) {
1048 m_password = translate_password(getPlayerName(), m_password);
1052 std::string playername_u = lowercase(getPlayerName());
1053 m_auth_data = srp_user_new(SRP_SHA256, SRP_NG_2048,
1054 getPlayerName().c_str(), playername_u.c_str(),
1055 (const unsigned char *) m_password.c_str(),
1056 m_password.length(), NULL, NULL);
1059 SRP_Result res = srp_user_start_authentication(
1060 (struct SRPUser *) m_auth_data, NULL, NULL, 0,
1061 (unsigned char **) &bytes_A, &len_A);
1062 FATAL_ERROR_IF(res != SRP_OK, "Creating local SRP user failed.");
1064 NetworkPacket resp_pkt(TOSERVER_SRP_BYTES_A, 0);
1065 resp_pkt << std::string(bytes_A, len_A) << based_on;
1069 case AUTH_MECHANISM_NONE:
1070 break; // not handled in this method
1074 void Client::sendDeletedBlocks(std::vector<v3s16> &blocks)
1076 NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * blocks.size());
1078 pkt << (u8) blocks.size();
1081 for(std::vector<v3s16>::iterator
1083 j != blocks.end(); ++j) {
1091 void Client::sendGotBlocks(v3s16 block)
1093 NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
1094 pkt << (u8) 1 << block;
1098 void Client::sendRemovedSounds(std::vector<s32> &soundList)
1100 size_t server_ids = soundList.size();
1101 assert(server_ids <= 0xFFFF);
1103 NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4);
1105 pkt << (u16) (server_ids & 0xFFFF);
1107 for(std::vector<s32>::iterator i = soundList.begin();
1108 i != soundList.end(); ++i)
1114 void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
1115 const StringMap &fields)
1117 size_t fields_size = fields.size();
1119 FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of nodemeta fields");
1121 NetworkPacket pkt(TOSERVER_NODEMETA_FIELDS, 0);
1123 pkt << p << formname << (u16) (fields_size & 0xFFFF);
1125 StringMap::const_iterator it;
1126 for (it = fields.begin(); it != fields.end(); ++it) {
1127 const std::string &name = it->first;
1128 const std::string &value = it->second;
1130 pkt.putLongString(value);
1136 void Client::sendInventoryFields(const std::string &formname,
1137 const StringMap &fields)
1139 size_t fields_size = fields.size();
1140 FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields");
1142 NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0);
1143 pkt << formname << (u16) (fields_size & 0xFFFF);
1145 StringMap::const_iterator it;
1146 for (it = fields.begin(); it != fields.end(); ++it) {
1147 const std::string &name = it->first;
1148 const std::string &value = it->second;
1150 pkt.putLongString(value);
1156 void Client::sendInventoryAction(InventoryAction *a)
1158 std::ostringstream os(std::ios_base::binary);
1163 std::string s = os.str();
1165 NetworkPacket pkt(TOSERVER_INVENTORY_ACTION, s.size());
1166 pkt.putRawString(s.c_str(),s.size());
1171 bool Client::canSendChatMessage() const
1173 u32 now = time(NULL);
1174 float time_passed = now - m_last_chat_message_sent;
1176 float virt_chat_message_allowance = m_chat_message_allowance + time_passed *
1177 (CLIENT_CHAT_MESSAGE_LIMIT_PER_10S / 8.0f);
1179 if (virt_chat_message_allowance < 1.0f)
1185 void Client::sendChatMessage(const std::wstring &message)
1187 const s16 max_queue_size = g_settings->getS16("max_out_chat_queue_size");
1188 if (canSendChatMessage()) {
1189 u32 now = time(NULL);
1190 float time_passed = now - m_last_chat_message_sent;
1191 m_last_chat_message_sent = time(NULL);
1193 m_chat_message_allowance += time_passed * (CLIENT_CHAT_MESSAGE_LIMIT_PER_10S / 8.0f);
1194 if (m_chat_message_allowance > CLIENT_CHAT_MESSAGE_LIMIT_PER_10S)
1195 m_chat_message_allowance = CLIENT_CHAT_MESSAGE_LIMIT_PER_10S;
1197 m_chat_message_allowance -= 1.0f;
1199 NetworkPacket pkt(TOSERVER_CHAT_MESSAGE, 2 + message.size() * sizeof(u16));
1204 } else if (m_out_chat_queue.size() < (u16) max_queue_size || max_queue_size == -1) {
1205 m_out_chat_queue.push(message);
1207 infostream << "Could not queue chat message because maximum out chat queue size ("
1208 << max_queue_size << ") is reached." << std::endl;
1212 void Client::clearOutChatQueue()
1214 m_out_chat_queue = std::queue<std::wstring>();
1217 void Client::sendChangePassword(const std::string &oldpassword,
1218 const std::string &newpassword)
1220 LocalPlayer *player = m_env.getLocalPlayer();
1224 std::string playername = player->getName();
1225 if (m_proto_ver >= 25) {
1226 // get into sudo mode and then send new password to server
1227 m_password = oldpassword;
1228 m_new_password = newpassword;
1229 startAuth(choseAuthMech(m_sudo_auth_methods));
1231 std::string oldpwd = translate_password(playername, oldpassword);
1232 std::string newpwd = translate_password(playername, newpassword);
1234 NetworkPacket pkt(TOSERVER_PASSWORD_LEGACY, 2 * PASSWORD_SIZE);
1236 for (u8 i = 0; i < PASSWORD_SIZE; i++) {
1237 pkt << (u8) (i < oldpwd.length() ? oldpwd[i] : 0);
1240 for (u8 i = 0; i < PASSWORD_SIZE; i++) {
1241 pkt << (u8) (i < newpwd.length() ? newpwd[i] : 0);
1248 void Client::sendDamage(u8 damage)
1250 DSTACK(FUNCTION_NAME);
1252 NetworkPacket pkt(TOSERVER_DAMAGE, sizeof(u8));
1257 void Client::sendBreath(u16 breath)
1259 DSTACK(FUNCTION_NAME);
1261 // Protocol v29 make this obsolete
1262 if (m_proto_ver >= 29)
1265 NetworkPacket pkt(TOSERVER_BREATH, sizeof(u16));
1270 void Client::sendRespawn()
1272 DSTACK(FUNCTION_NAME);
1274 NetworkPacket pkt(TOSERVER_RESPAWN, 0);
1278 void Client::sendReady()
1280 DSTACK(FUNCTION_NAME);
1282 NetworkPacket pkt(TOSERVER_CLIENT_READY,
1283 1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(g_version_hash));
1285 pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH
1286 << (u8) 0 << (u16) strlen(g_version_hash);
1288 pkt.putRawString(g_version_hash, (u16) strlen(g_version_hash));
1292 void Client::sendPlayerPos()
1294 LocalPlayer *myplayer = m_env.getLocalPlayer();
1295 if(myplayer == NULL)
1298 ClientMap &map = m_env.getClientMap();
1300 u8 camera_fov = map.getCameraFov();
1301 u8 wanted_range = map.getControl().wanted_range;
1303 // Save bandwidth by only updating position when something changed
1304 if(myplayer->last_position == myplayer->getPosition() &&
1305 myplayer->last_speed == myplayer->getSpeed() &&
1306 myplayer->last_pitch == myplayer->getPitch() &&
1307 myplayer->last_yaw == myplayer->getYaw() &&
1308 myplayer->last_keyPressed == myplayer->keyPressed &&
1309 myplayer->last_camera_fov == camera_fov &&
1310 myplayer->last_wanted_range == wanted_range)
1313 myplayer->last_position = myplayer->getPosition();
1314 myplayer->last_speed = myplayer->getSpeed();
1315 myplayer->last_pitch = myplayer->getPitch();
1316 myplayer->last_yaw = myplayer->getYaw();
1317 myplayer->last_keyPressed = myplayer->keyPressed;
1318 myplayer->last_camera_fov = camera_fov;
1319 myplayer->last_wanted_range = wanted_range;
1321 //infostream << "Sending Player Position information" << std::endl;
1325 //MutexAutoLock lock(m_con_mutex); //bulk comment-out
1326 our_peer_id = m_con.GetPeerID();
1329 // Set peer id if not set already
1330 if(myplayer->peer_id == PEER_ID_INEXISTENT)
1331 myplayer->peer_id = our_peer_id;
1333 assert(myplayer->peer_id == our_peer_id);
1335 NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4 + 1 + 1);
1337 writePlayerPos(myplayer, &map, &pkt);
1342 void Client::sendPlayerItem(u16 item)
1344 LocalPlayer *myplayer = m_env.getLocalPlayer();
1345 if(myplayer == NULL)
1348 u16 our_peer_id = m_con.GetPeerID();
1350 // Set peer id if not set already
1351 if(myplayer->peer_id == PEER_ID_INEXISTENT)
1352 myplayer->peer_id = our_peer_id;
1353 assert(myplayer->peer_id == our_peer_id);
1355 NetworkPacket pkt(TOSERVER_PLAYERITEM, 2);
1362 void Client::removeNode(v3s16 p)
1364 std::map<v3s16, MapBlock*> modified_blocks;
1367 m_env.getMap().removeNodeAndUpdate(p, modified_blocks);
1369 catch(InvalidPositionException &e) {
1372 for(std::map<v3s16, MapBlock *>::iterator
1373 i = modified_blocks.begin();
1374 i != modified_blocks.end(); ++i) {
1375 addUpdateMeshTaskWithEdge(i->first, false, true);
1379 MapNode Client::getNode(v3s16 p, bool *is_valid_position)
1381 return m_env.getMap().getNodeNoEx(p, is_valid_position);
1384 void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
1386 //TimeTaker timer1("Client::addNode()");
1388 std::map<v3s16, MapBlock*> modified_blocks;
1391 //TimeTaker timer3("Client::addNode(): addNodeAndUpdate");
1392 m_env.getMap().addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
1394 catch(InvalidPositionException &e) {
1397 for(std::map<v3s16, MapBlock *>::iterator
1398 i = modified_blocks.begin();
1399 i != modified_blocks.end(); ++i) {
1400 addUpdateMeshTaskWithEdge(i->first, false, true);
1404 void Client::setPlayerControl(PlayerControl &control)
1406 LocalPlayer *player = m_env.getLocalPlayer();
1407 assert(player != NULL);
1408 player->control = control;
1411 void Client::selectPlayerItem(u16 item)
1413 m_playeritem = item;
1414 m_inventory_updated = true;
1415 sendPlayerItem(item);
1418 // Returns true if the inventory of the local player has been
1419 // updated from the server. If it is true, it is set to false.
1420 bool Client::getLocalInventoryUpdated()
1422 bool updated = m_inventory_updated;
1423 m_inventory_updated = false;
1427 // Copies the inventory of the local player to parameter
1428 void Client::getLocalInventory(Inventory &dst)
1430 LocalPlayer *player = m_env.getLocalPlayer();
1431 assert(player != NULL);
1432 dst = player->inventory;
1435 Inventory* Client::getInventory(const InventoryLocation &loc)
1438 case InventoryLocation::UNDEFINED:
1441 case InventoryLocation::CURRENT_PLAYER:
1443 LocalPlayer *player = m_env.getLocalPlayer();
1444 assert(player != NULL);
1445 return &player->inventory;
1448 case InventoryLocation::PLAYER:
1450 // Check if we are working with local player inventory
1451 LocalPlayer *player = m_env.getLocalPlayer();
1452 if (!player || strcmp(player->getName(), loc.name.c_str()) != 0)
1454 return &player->inventory;
1457 case InventoryLocation::NODEMETA:
1459 NodeMetadata *meta = m_env.getMap().getNodeMetadata(loc.p);
1462 return meta->getInventory();
1465 case InventoryLocation::DETACHED:
1467 if (m_detached_inventories.count(loc.name) == 0)
1469 return m_detached_inventories[loc.name];
1473 FATAL_ERROR("Invalid inventory location type.");
1479 void Client::inventoryAction(InventoryAction *a)
1482 Send it to the server
1484 sendInventoryAction(a);
1487 Predict some local inventory changes
1489 a->clientApply(this, this);
1495 float Client::getAnimationTime()
1497 return m_animation_time;
1500 int Client::getCrackLevel()
1502 return m_crack_level;
1505 v3s16 Client::getCrackPos()
1510 void Client::setCrack(int level, v3s16 pos)
1512 int old_crack_level = m_crack_level;
1513 v3s16 old_crack_pos = m_crack_pos;
1515 m_crack_level = level;
1518 if(old_crack_level >= 0 && (level < 0 || pos != old_crack_pos))
1521 addUpdateMeshTaskForNode(old_crack_pos, false, true);
1523 if(level >= 0 && (old_crack_level < 0 || pos != old_crack_pos))
1526 addUpdateMeshTaskForNode(pos, false, true);
1532 LocalPlayer *player = m_env.getLocalPlayer();
1533 assert(player != NULL);
1537 bool Client::getChatMessage(std::wstring &message)
1539 if(m_chat_queue.size() == 0)
1541 message = m_chat_queue.front();
1546 void Client::typeChatMessage(const std::wstring &message)
1548 // Discard empty line
1552 // If message was ate by script API, don't send it to server
1553 if (m_script->on_sending_message(wide_to_utf8(message))) {
1558 sendChatMessage(message);
1561 if (message[0] != L'/')
1563 // compatibility code
1564 if (m_proto_ver < 29) {
1565 LocalPlayer *player = m_env.getLocalPlayer();
1566 assert(player != NULL);
1567 std::wstring name = narrow_to_wide(player->getName());
1568 pushToChatQueue((std::wstring)L"<" + name + L"> " + message);
1573 void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
1575 // Check if the block exists to begin with. In the case when a non-existing
1576 // neighbor is automatically added, it may not. In that case we don't want
1577 // to tell the mesh update thread about it.
1578 MapBlock *b = m_env.getMap().getBlockNoCreateNoEx(p);
1582 m_mesh_update_thread.updateBlock(&m_env.getMap(), p, ack_to_server, urgent);
1585 void Client::addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server, bool urgent)
1588 addUpdateMeshTask(blockpos, ack_to_server, urgent);
1590 catch(InvalidPositionException &e){}
1593 for (int i=0;i<6;i++)
1596 v3s16 p = blockpos + g_6dirs[i];
1597 addUpdateMeshTask(p, false, urgent);
1599 catch(InvalidPositionException &e){}
1603 void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool urgent)
1607 infostream<<"Client::addUpdateMeshTaskForNode(): "
1608 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1612 v3s16 blockpos = getNodeBlockPos(nodepos);
1613 v3s16 blockpos_relative = blockpos * MAP_BLOCKSIZE;
1616 addUpdateMeshTask(blockpos, ack_to_server, urgent);
1618 catch(InvalidPositionException &e) {}
1621 if(nodepos.X == blockpos_relative.X){
1623 v3s16 p = blockpos + v3s16(-1,0,0);
1624 addUpdateMeshTask(p, false, urgent);
1626 catch(InvalidPositionException &e){}
1629 if(nodepos.Y == blockpos_relative.Y){
1631 v3s16 p = blockpos + v3s16(0,-1,0);
1632 addUpdateMeshTask(p, false, urgent);
1634 catch(InvalidPositionException &e){}
1637 if(nodepos.Z == blockpos_relative.Z){
1639 v3s16 p = blockpos + v3s16(0,0,-1);
1640 addUpdateMeshTask(p, false, urgent);
1642 catch(InvalidPositionException &e){}
1646 ClientEvent Client::getClientEvent()
1648 FATAL_ERROR_IF(m_client_event_queue.empty(),
1649 "Cannot getClientEvent, queue is empty.");
1651 ClientEvent event = m_client_event_queue.front();
1652 m_client_event_queue.pop();
1656 float Client::mediaReceiveProgress()
1658 if (m_media_downloader)
1659 return m_media_downloader->getProgress();
1661 return 1.0; // downloader only exists when not yet done
1664 typedef struct TextureUpdateArgs {
1665 IrrlichtDevice *device;
1666 gui::IGUIEnvironment *guienv;
1669 const wchar_t* text_base;
1670 ITextureSource *tsrc;
1671 } TextureUpdateArgs;
1673 void texture_update_progress(void *args, u32 progress, u32 max_progress)
1675 TextureUpdateArgs* targs = (TextureUpdateArgs*) args;
1676 u16 cur_percent = ceil(progress / (double) max_progress * 100.);
1678 // update the loading menu -- if neccessary
1679 bool do_draw = false;
1680 u64 time_ms = targs->last_time_ms;
1681 if (cur_percent != targs->last_percent) {
1682 targs->last_percent = cur_percent;
1683 time_ms = porting::getTimeMs();
1684 // only draw when the user will notice something:
1685 do_draw = (time_ms - targs->last_time_ms > 100);
1689 targs->last_time_ms = time_ms;
1690 std::basic_stringstream<wchar_t> strm;
1691 strm << targs->text_base << " " << targs->last_percent << "%...";
1692 draw_load_screen(strm.str(), targs->device, targs->guienv, targs->tsrc, 0,
1693 72 + (u16) ((18. / 100.) * (double) targs->last_percent), true);
1697 void Client::afterContentReceived(IrrlichtDevice *device)
1699 infostream<<"Client::afterContentReceived() started"<<std::endl;
1700 assert(m_itemdef_received); // pre-condition
1701 assert(m_nodedef_received); // pre-condition
1702 assert(mediaReceived()); // pre-condition
1704 const wchar_t* text = wgettext("Loading textures...");
1706 // Clear cached pre-scaled 2D GUI images, as this cache
1707 // might have images with the same name but different
1708 // content from previous sessions.
1709 guiScalingCacheClear(device->getVideoDriver());
1711 // Rebuild inherited images and recreate textures
1712 infostream<<"- Rebuilding images and textures"<<std::endl;
1713 draw_load_screen(text,device, guienv, m_tsrc, 0, 70);
1714 m_tsrc->rebuildImagesAndTextures();
1718 infostream<<"- Rebuilding shaders"<<std::endl;
1719 text = wgettext("Rebuilding shaders...");
1720 draw_load_screen(text, device, guienv, m_tsrc, 0, 71);
1721 m_shsrc->rebuildShaders();
1724 // Update node aliases
1725 infostream<<"- Updating node aliases"<<std::endl;
1726 text = wgettext("Initializing nodes...");
1727 draw_load_screen(text, device, guienv, m_tsrc, 0, 72);
1728 m_nodedef->updateAliases(m_itemdef);
1729 std::string texture_path = g_settings->get("texture_path");
1730 if (texture_path != "" && fs::IsDir(texture_path))
1731 m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
1732 m_nodedef->setNodeRegistrationStatus(true);
1733 m_nodedef->runNodeResolveCallbacks();
1736 // Update node textures and assign shaders to each tile
1737 infostream<<"- Updating node textures"<<std::endl;
1738 TextureUpdateArgs tu_args;
1739 tu_args.device = device;
1740 tu_args.guienv = guienv;
1741 tu_args.last_time_ms = porting::getTimeMs();
1742 tu_args.last_percent = 0;
1743 tu_args.text_base = wgettext("Initializing nodes");
1744 tu_args.tsrc = m_tsrc;
1745 m_nodedef->updateTextures(this, texture_update_progress, &tu_args);
1746 delete[] tu_args.text_base;
1748 // Start mesh update thread after setting up content definitions
1749 infostream<<"- Starting mesh update thread"<<std::endl;
1750 m_mesh_update_thread.start();
1755 if (g_settings->getBool("enable_client_modding")) {
1756 m_script->on_client_ready(m_env.getLocalPlayer());
1757 m_script->on_connect();
1760 text = wgettext("Done!");
1761 draw_load_screen(text, device, guienv, m_tsrc, 0, 100);
1762 infostream<<"Client::afterContentReceived() done"<<std::endl;
1766 float Client::getRTT()
1768 return m_con.getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
1771 float Client::getCurRate()
1773 return (m_con.getLocalStat(con::CUR_INC_RATE) +
1774 m_con.getLocalStat(con::CUR_DL_RATE));
1777 void Client::makeScreenshot(IrrlichtDevice *device)
1779 irr::video::IVideoDriver *driver = device->getVideoDriver();
1780 irr::video::IImage* const raw_image = driver->createScreenShot();
1785 time_t t = time(NULL);
1786 struct tm *tm = localtime(&t);
1788 char timetstamp_c[64];
1789 strftime(timetstamp_c, sizeof(timetstamp_c), "%Y%m%d_%H%M%S", tm);
1791 std::string filename_base = g_settings->get("screenshot_path")
1793 + std::string("screenshot_")
1794 + std::string(timetstamp_c);
1795 std::string filename_ext = "." + g_settings->get("screenshot_format");
1796 std::string filename;
1798 u32 quality = (u32)g_settings->getS32("screenshot_quality");
1799 quality = MYMIN(MYMAX(quality, 0), 100) / 100.0 * 255;
1801 // Try to find a unique filename
1802 unsigned serial = 0;
1804 while (serial < SCREENSHOT_MAX_SERIAL_TRIES) {
1805 filename = filename_base + (serial > 0 ? ("_" + itos(serial)) : "") + filename_ext;
1806 std::ifstream tmp(filename.c_str());
1808 break; // File did not apparently exist, we'll go with it
1812 if (serial == SCREENSHOT_MAX_SERIAL_TRIES) {
1813 infostream << "Could not find suitable filename for screenshot" << std::endl;
1815 irr::video::IImage* const image =
1816 driver->createImage(video::ECF_R8G8B8, raw_image->getDimension());
1819 raw_image->copyTo(image);
1821 std::ostringstream sstr;
1822 if (driver->writeImageToFile(image, filename.c_str(), quality)) {
1823 sstr << "Saved screenshot to '" << filename << "'";
1825 sstr << "Failed to save screenshot '" << filename << "'";
1827 pushToChatQueue(narrow_to_wide(sstr.str()));
1828 infostream << sstr.str() << std::endl;
1836 bool Client::shouldShowMinimap() const
1838 return !m_minimap_disabled_by_server;
1841 void Client::showGameChat(const bool show)
1843 m_game_ui_flags->show_chat = show;
1846 void Client::showGameHud(const bool show)
1848 m_game_ui_flags->show_hud = show;
1851 void Client::showMinimap(const bool show)
1853 m_game_ui_flags->show_minimap = show;
1856 void Client::showProfiler(const bool show)
1858 m_game_ui_flags->show_profiler_graph = show;
1861 void Client::showGameFog(const bool show)
1863 m_game_ui_flags->force_fog_off = !show;
1866 void Client::showGameDebug(const bool show)
1868 m_game_ui_flags->show_debug = show;
1871 // IGameDef interface
1873 IItemDefManager* Client::getItemDefManager()
1877 INodeDefManager* Client::getNodeDefManager()
1881 ICraftDefManager* Client::getCraftDefManager()
1884 //return m_craftdef;
1886 ITextureSource* Client::getTextureSource()
1890 IShaderSource* Client::getShaderSource()
1894 scene::ISceneManager* Client::getSceneManager()
1896 return m_device->getSceneManager();
1898 u16 Client::allocateUnknownNodeId(const std::string &name)
1900 errorstream << "Client::allocateUnknownNodeId(): "
1901 << "Client cannot allocate node IDs" << std::endl;
1902 FATAL_ERROR("Client allocated unknown node");
1904 return CONTENT_IGNORE;
1906 ISoundManager* Client::getSoundManager()
1910 MtEventManager* Client::getEventManager()
1915 ParticleManager* Client::getParticleManager()
1917 return &m_particle_manager;
1920 scene::IAnimatedMesh* Client::getMesh(const std::string &filename)
1922 StringMap::const_iterator it = m_mesh_data.find(filename);
1923 if (it == m_mesh_data.end()) {
1924 errorstream << "Client::getMesh(): Mesh not found: \"" << filename
1925 << "\"" << std::endl;
1928 const std::string &data = it->second;
1929 scene::ISceneManager *smgr = m_device->getSceneManager();
1931 // Create the mesh, remove it from cache and return it
1932 // This allows unique vertex colors and other properties for each instance
1933 Buffer<char> data_rw(data.c_str(), data.size()); // Const-incorrect Irrlicht
1934 io::IFileSystem *irrfs = m_device->getFileSystem();
1935 io::IReadFile *rfile = irrfs->createMemoryReadFile(
1936 *data_rw, data_rw.getSize(), filename.c_str());
1937 FATAL_ERROR_IF(!rfile, "Could not create/open RAM file");
1939 scene::IAnimatedMesh *mesh = smgr->getMesh(rfile);
1941 // NOTE: By playing with Irrlicht refcounts, maybe we could cache a bunch
1942 // of uniquely named instances and re-use them
1944 smgr->getMeshCache()->removeMesh(mesh);
1948 bool Client::registerModStorage(ModMetadata *storage)
1950 if (m_mod_storages.find(storage->getModName()) != m_mod_storages.end()) {
1951 errorstream << "Unable to register same mod storage twice. Storage name: "
1952 << storage->getModName() << std::endl;
1956 m_mod_storages[storage->getModName()] = storage;
1960 void Client::unregisterModStorage(const std::string &name)
1962 std::unordered_map<std::string, ModMetadata *>::const_iterator it =
1963 m_mod_storages.find(name);
1964 if (it != m_mod_storages.end()) {
1965 // Save unconditionaly on unregistration
1966 it->second->save(getModStoragePath());
1967 m_mod_storages.erase(name);
1971 std::string Client::getModStoragePath() const
1973 return porting::path_user + DIR_DELIM + "client" + DIR_DELIM + "mod_storage";