Update inventory texture too
[oweals/minetest.git] / src / client.cpp
index d0e5cd405dee7a363ee86ed75dadbb41643baf07..7093a5190a2c40d176bf8279bb1437c2ba9cdba5 100644 (file)
@@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodemetadata.h"
 #include "nodedef.h"
 #include "tooldef.h"
+#include <IFileSystem.h>
 
 /*
        QueuedMeshUpdate
@@ -221,13 +222,14 @@ Client::Client(
 
        // Build main texture atlas, now that the GameDef exists (that is, us)
        if(g_settings->getBool("enable_texture_atlas"))
-               tsrc->buildMainAtlas(this);
+               m_tsrc->buildMainAtlas(this);
        else
                infostream<<"Not building texture atlas."<<std::endl;
+       
+       // Update node textures
+       m_nodedef->updateTextures(m_tsrc);
 
-       // NOTE: This should be done only after getting possible dynamic
-       // game definitions from the server, or at least shut down and
-       // restarted when doing so
+       // Start threads after setting up content definitions
        m_mesh_update_thread.Start();
 
        /*
@@ -449,7 +451,7 @@ void Client::step(float dtime)
                        snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str());
                        
                        // This should be incremented in each version
-                       writeU16(&data[51], 3);
+                       writeU16(&data[51], PROTOCOL_VERSION);
 
                        // Send as unreliable
                        Send(0, data, false);
@@ -1503,6 +1505,90 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                event.deathscreen.camera_point_target_z = camera_point_target.Z;
                m_client_event_queue.push_back(event);
        }
+       else if(command == TOCLIENT_TOOLDEF)
+       {
+               infostream<<"Client: Received tool definitions"<<std::endl;
+
+               std::string datastring((char*)&data[2], datasize-2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               // Stop threads while updating content definitions
+               m_mesh_update_thread.stop();
+
+               std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
+               m_tooldef->deSerialize(tmp_is);
+               
+               // Resume threads
+               m_mesh_update_thread.setRun(true);
+               m_mesh_update_thread.Start();
+       }
+       else if(command == TOCLIENT_TEXTURES)
+       {
+               infostream<<"Client: Received textures: packet size: "<<datasize
+                               <<std::endl;
+
+               io::IFileSystem *irrfs = m_device->getFileSystem();
+               video::IVideoDriver *vdrv = m_device->getVideoDriver();
+
+               std::string datastring((char*)&data[2], datasize-2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               // Stop threads while updating content definitions
+               m_mesh_update_thread.stop();
+               
+               /*
+                       u16 command
+                       u32 number of textures
+                       for each texture {
+                               u16 length of name
+                               string name
+                               u32 length of data
+                               data
+                       }
+               */
+               int num_textures = readU32(is);
+               infostream<<"Client: Received textures: count: "<<num_textures
+                               <<std::endl;
+               for(int i=0; i<num_textures; i++){
+                       std::string name = deSerializeString(is);
+                       std::string data = deSerializeLongString(is);
+                       // Silly irrlicht's const-incorrectness
+                       Buffer<char> data_rw(data.c_str(), data.size());
+                       // Create an irrlicht memory file
+                       io::IReadFile *rfile = irrfs->createMemoryReadFile(
+                                       *data_rw, data.size(), "_tempreadfile");
+                       assert(rfile);
+                       // Read image
+                       video::IImage *img = vdrv->createImageFromFile(rfile);
+                       if(!img){
+                               errorstream<<"Client: Cannot create image from data of "
+                                               <<"received texture \""<<name<<"\""<<std::endl;
+                               rfile->drop();
+                               continue;
+                       }
+                       m_tsrc->insertSourceImage(name, img);
+                       img->drop();
+                       rfile->drop();
+               }
+               
+               // Rebuild inherited images and recreate textures
+               m_tsrc->rebuildImagesAndTextures();
+
+               // Update texture atlas
+               if(g_settings->getBool("enable_texture_atlas"))
+                       m_tsrc->buildMainAtlas(this);
+               
+               // Update node textures
+               m_nodedef->updateTextures(m_tsrc);
+
+               // Resume threads
+               m_mesh_update_thread.setRun(true);
+               m_mesh_update_thread.Start();
+
+               ClientEvent event;
+               event.type = CE_TEXTURES_UPDATED;
+               m_client_event_queue.push_back(event);
+       }
        else
        {
                infostream<<"Client: Ignoring unknown command "