+ else if(command == TOCLIENT_PLAYERITEM)
+ {
+ infostream<<"Client: WARNING: Ignoring TOCLIENT_PLAYERITEM"<<std::endl;
+ }
+ else if(command == TOCLIENT_DEATHSCREEN)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ bool set_camera_point_target = readU8(is);
+ v3f camera_point_target = readV3F1000(is);
+
+ ClientEvent event;
+ event.type = CE_DEATHSCREEN;
+ event.deathscreen.set_camera_point_target = set_camera_point_target;
+ event.deathscreen.camera_point_target_x = camera_point_target.X;
+ event.deathscreen.camera_point_target_y = camera_point_target.Y;
+ event.deathscreen.camera_point_target_z = camera_point_target.Z;
+ m_client_event_queue.push_back(event);
+ }
+ else if(command == TOCLIENT_ANNOUNCE_MEDIA)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ // Mesh update thread must be stopped while
+ // updating content definitions
+ assert(!m_mesh_update_thread.IsRunning());
+
+ int num_files = readU16(is);
+
+ infostream<<"Client: Received media announcement: packet size: "
+ <<datasize<<std::endl;
+
+ core::list<MediaRequest> file_requests;
+
+ for(int i=0; i<num_files; i++)
+ {
+ //read file from cache
+ std::string name = deSerializeString(is);
+ std::string sha1_base64 = deSerializeString(is);
+
+ // if name contains illegal characters, ignore the file
+ if(!string_allowed(name, TEXTURENAME_ALLOWED_CHARS)){
+ errorstream<<"Client: ignoring illegal file name "
+ <<"sent by server: \""<<name<<"\""<<std::endl;
+ continue;
+ }
+
+ std::string sha1_raw = base64_decode(sha1_base64);
+ std::string sha1_hex = hex_encode(sha1_raw);
+ std::ostringstream tmp_os(std::ios_base::binary);
+ bool found_in_cache = m_media_cache.load_sha1(sha1_raw, tmp_os);
+ m_media_name_sha1_map.set(name, sha1_raw);
+
+ // If found in cache, try to load it from there
+ if(found_in_cache)
+ {
+ bool success = loadMedia(tmp_os.str(), name);
+ if(success){
+ verbosestream<<"Client: Loaded cached media: "
+ <<sha1_hex<<" \""<<name<<"\""<<std::endl;
+ continue;
+ } else{
+ infostream<<"Client: Failed to load cached media: "
+ <<sha1_hex<<" \""<<name<<"\""<<std::endl;
+ }
+ }
+ // Didn't load from cache; queue it to be requested
+ verbosestream<<"Client: Adding file to request list: \""
+ <<sha1_hex<<" \""<<name<<"\""<<std::endl;
+ file_requests.push_back(MediaRequest(name));
+ }
+
+ std::string remote_media = "";
+ try {
+ remote_media = deSerializeString(is);
+ }
+ catch(SerializationError) {
+ // not supported by server or turned off
+ }
+
+ m_media_count = file_requests.size();
+ m_media_receive_started = true;
+
+ if (remote_media == "" || !USE_CURL) {
+ request_media(file_requests);
+ } else {
+ #if USE_CURL
+ core::list<MediaFetchThread*>::Iterator cur = m_media_fetch_threads.begin();
+ for(core::list<MediaRequest>::Iterator i = file_requests.begin();
+ i != file_requests.end(); i++) {
+ (*cur)->m_file_requests.push_back(*i);
+ cur++;
+ if (cur == m_media_fetch_threads.end())
+ cur = m_media_fetch_threads.begin();
+ }
+ for (core::list<MediaFetchThread*>::Iterator i = m_media_fetch_threads.begin();
+ i != m_media_fetch_threads.end(); i++) {
+ (*i)->m_remote_url = remote_media;
+ (*i)->Start();
+ }
+ #endif
+
+ // notify server we received everything
+ std::ostringstream os(std::ios_base::binary);
+ writeU16(os, TOSERVER_RECEIVED_MEDIA);
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ Send(0, data, true);
+ }
+ ClientEvent event;
+ event.type = CE_TEXTURES_UPDATED;
+ m_client_event_queue.push_back(event);
+ }
+ else if(command == TOCLIENT_MEDIA)
+ {
+ if (m_media_count == 0)
+ return;
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ // Mesh update thread must be stopped while
+ // updating content definitions
+ assert(!m_mesh_update_thread.IsRunning());
+
+ /*
+ u16 command
+ u16 total number of file bunches
+ u16 index of this bunch
+ u32 number of files in this bunch
+ for each file {
+ u16 length of name
+ string name
+ u32 length of data
+ data
+ }
+ */
+ int num_bunches = readU16(is);
+ int bunch_i = readU16(is);
+ int num_files = readU32(is);
+ infostream<<"Client: Received files: bunch "<<bunch_i<<"/"
+ <<num_bunches<<" files="<<num_files
+ <<" size="<<datasize<<std::endl;
+ for(int i=0; i<num_files; i++){
+ m_media_received_count++;
+ std::string name = deSerializeString(is);
+ std::string data = deSerializeLongString(is);
+
+ // if name contains illegal characters, ignore the file
+ if(!string_allowed(name, TEXTURENAME_ALLOWED_CHARS)){
+ errorstream<<"Client: ignoring illegal file name "
+ <<"sent by server: \""<<name<<"\""<<std::endl;
+ continue;
+ }
+
+ bool success = loadMedia(data, name);
+ if(success){
+ verbosestream<<"Client: Loaded received media: "
+ <<"\""<<name<<"\". Caching."<<std::endl;
+ } else{
+ infostream<<"Client: Failed to load received media: "
+ <<"\""<<name<<"\". Not caching."<<std::endl;
+ continue;
+ }
+
+ bool did = fs::CreateAllDirs(getMediaCacheDir());
+ if(!did){
+ errorstream<<"Could not create media cache directory"
+ <<std::endl;
+ }
+
+ {
+ core::map<std::string, std::string>::Node *n;
+ n = m_media_name_sha1_map.find(name);
+ if(n == NULL)
+ errorstream<<"The server sent a file that has not "
+ <<"been announced."<<std::endl;
+ else
+ m_media_cache.update_sha1(data);
+ }
+ }
+
+ ClientEvent event;
+ event.type = CE_TEXTURES_UPDATED;
+ m_client_event_queue.push_back(event);
+ }
+ else if(command == TOCLIENT_TOOLDEF)
+ {
+ infostream<<"Client: WARNING: Ignoring TOCLIENT_TOOLDEF"<<std::endl;
+ }
+ else if(command == TOCLIENT_NODEDEF)
+ {
+ infostream<<"Client: Received node definitions: packet size: "
+ <<datasize<<std::endl;
+
+ // Mesh update thread must be stopped while
+ // updating content definitions
+ assert(!m_mesh_update_thread.IsRunning());
+
+ // Decompress node definitions
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
+ std::ostringstream tmp_os;
+ decompressZlib(tmp_is, tmp_os);
+
+ // Deserialize node definitions
+ std::istringstream tmp_is2(tmp_os.str());
+ m_nodedef->deSerialize(tmp_is2);
+ m_nodedef_received = true;
+ }
+ else if(command == TOCLIENT_CRAFTITEMDEF)
+ {
+ infostream<<"Client: WARNING: Ignoring TOCLIENT_CRAFTITEMDEF"<<std::endl;
+ }
+ else if(command == TOCLIENT_ITEMDEF)
+ {
+ infostream<<"Client: Received item definitions: packet size: "
+ <<datasize<<std::endl;
+
+ // Mesh update thread must be stopped while
+ // updating content definitions
+ assert(!m_mesh_update_thread.IsRunning());
+
+ // Decompress item definitions
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
+ std::ostringstream tmp_os;
+ decompressZlib(tmp_is, tmp_os);
+
+ // Deserialize node definitions
+ std::istringstream tmp_is2(tmp_os.str());
+ m_itemdef->deSerialize(tmp_is2);
+ m_itemdef_received = true;
+ }
+ else if(command == TOCLIENT_PLAY_SOUND)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ s32 server_id = readS32(is);
+ std::string name = deSerializeString(is);
+ float gain = readF1000(is);
+ int type = readU8(is); // 0=local, 1=positional, 2=object
+ v3f pos = readV3F1000(is);
+ u16 object_id = readU16(is);
+ bool loop = readU8(is);
+ // Start playing
+ int client_id = -1;
+ switch(type){
+ case 0: // local
+ client_id = m_sound->playSound(name, loop, gain);
+ break;
+ case 1: // positional
+ client_id = m_sound->playSoundAt(name, loop, gain, pos);
+ break;
+ case 2: { // object
+ ClientActiveObject *cao = m_env.getActiveObject(object_id);
+ if(cao)
+ pos = cao->getPosition();
+ client_id = m_sound->playSoundAt(name, loop, gain, pos);
+ // TODO: Set up sound to move with object
+ break; }
+ default:
+ break;
+ }
+ if(client_id != -1){
+ m_sounds_server_to_client[server_id] = client_id;
+ m_sounds_client_to_server[client_id] = server_id;
+ if(object_id != 0)
+ m_sounds_to_objects[client_id] = object_id;
+ }
+ }
+ else if(command == TOCLIENT_STOP_SOUND)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ s32 server_id = readS32(is);
+ std::map<s32, int>::iterator i =
+ m_sounds_server_to_client.find(server_id);
+ if(i != m_sounds_server_to_client.end()){
+ int client_id = i->second;
+ m_sound->stopSound(client_id);
+ }
+ }
+ else if(command == TOCLIENT_PRIVILEGES)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ m_privileges.clear();
+ infostream<<"Client: Privileges updated: ";
+ u16 num_privileges = readU16(is);
+ for(u16 i=0; i<num_privileges; i++){
+ std::string priv = deSerializeString(is);
+ m_privileges.insert(priv);
+ infostream<<priv<<" ";
+ }
+ infostream<<std::endl;
+ }
+ else if(command == TOCLIENT_INVENTORY_FORMSPEC)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ // Store formspec in LocalPlayer
+ Player *player = m_env.getLocalPlayer();
+ assert(player != NULL);
+ player->inventory_formspec = deSerializeLongString(is);
+ }
+ else if(command == TOCLIENT_DETACHED_INVENTORY)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ std::string name = deSerializeString(is);
+
+ infostream<<"Client: Detached inventory update: \""<<name<<"\""<<std::endl;
+
+ Inventory *inv = NULL;
+ if(m_detached_inventories.count(name) > 0)
+ inv = m_detached_inventories[name];
+ else{
+ inv = new Inventory(m_itemdef);
+ m_detached_inventories[name] = inv;
+ }
+ inv->deSerialize(is);
+ }
+ else if(command == TOCLIENT_SHOW_FORMSPEC)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ std::string formspec = deSerializeLongString(is);
+
+ ClientEvent event;
+ event.type = CE_SHOW_FORMSPEC;
+ // pointer is required as event is a struct only!
+ // adding a std:string to a struct isn't possible
+ event.show_formspec.formspec = new std::string(formspec);
+ m_client_event_queue.push_back(event);
+ }