From 4bf5065a9cdaf55a6915e647e9b5de5281d6622f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Date: Wed, 8 Feb 2012 11:49:24 +0100 Subject: Cache textures by checksum --- src/client.cpp | 118 ++++++++++++++++++++++++--------------------------------- 1 file changed, 50 insertions(+), 68 deletions(-) (limited to 'src/client.cpp') diff --git a/src/client.cpp b/src/client.cpp index 89070d66b..3a08b25c2 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "sha1.h" #include "base64.h" #include "clientmap.h" +#include "filecache.h" #include "sound.h" static std::string getTextureCacheDir() @@ -255,6 +256,7 @@ Client::Client( m_map_seed(0), m_password(password), m_access_denied(false), + m_texture_cache(getTextureCacheDir()), m_texture_receive_progress(0), m_textures_received(false), m_itemdef_received(false), @@ -1412,7 +1414,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) //read texture from cache std::string name = deSerializeString(is); std::string sha1_texture = deSerializeString(is); - + // if name contains illegal characters, ignore the texture if(!string_allowed(name, TEXTURENAME_ALLOWED_CHARS)){ errorstream<<"Client: ignoring illegal texture name " @@ -1420,74 +1422,50 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) continue; } - std::string tpath = getTextureCacheDir() + DIR_DELIM + name; - // Read data - std::ifstream fis(tpath.c_str(), std::ios_base::binary); - - - if(fis.good() == false){ - infostream<<"Client::Texture not found in cache: " - < data_rw(tmp_os.str().c_str(), tmp_os.str().size()); + std::string digest_string = base64_encode(digest, 20); - // Create an irrlicht memory file - io::IReadFile *rfile = irrfs->createMemoryReadFile( - *data_rw, tmp_os.str().size(), "_tempreadfile"); - assert(rfile); - // Read image - video::IImage *img = vdrv->createImageFromFile(rfile); - if(!img){ - infostream<<"Client: Cannot create image from data of " - <<"received texture \""<drop(); - } - else { - m_tsrc->insertSourceImage(name, img); - img->drop(); - rfile->drop(); + if (digest_string == sha1_texture) { + // Silly irrlicht's const-incorrectness + Buffer data_rw(tmp_os.str().c_str(), tmp_os.str().size()); - texture_found = true; - } + // Create an irrlicht memory file + io::IReadFile *rfile = irrfs->createMemoryReadFile( + *data_rw, tmp_os.str().size(), "_tempreadfile"); + assert(rfile); + // Read image + video::IImage *img = vdrv->createImageFromFile(rfile); + if(!img){ + infostream<<"Client: Cannot create image from data of " + <<"received texture \""<drop(); } else { - infostream<<"Client::Texture cached sha1 hash not matching server hash: " - <"< "<insertSourceImage(name, img); + img->drop(); + rfile->drop(); - free(digest); + texture_found = true; + } } + else { + infostream<<"Client::Texture cached sha1 hash not matching server hash: " + <"< "<::Node *n; + n = m_texture_name_sha1_map.find(name); + if(n == NULL) + errorstream<<"The server sent a texture that has not been announced." + <getValue()); } m_tsrc->insertSourceImage(name, img); @@ -2382,6 +2360,10 @@ void Client::afterContentReceived() assert(m_nodedef_received); assert(m_textures_received); + // remove the information about which checksum each texture + // ought to have + m_texture_name_sha1_map.clear(); + // Rebuild inherited images and recreate textures m_tsrc->rebuildImagesAndTextures(); -- cgit v1.2.3