summaryrefslogtreecommitdiff
path: root/src/client.cpp
diff options
context:
space:
mode:
authorJonathan Neuschäfer <j.neuschaefer@gmx.net>2012-02-08 11:49:24 +0100
committerPerttu Ahola <celeron55@gmail.com>2012-03-25 11:51:00 +0300
commit4bf5065a9cdaf55a6915e647e9b5de5281d6622f (patch)
tree227b78da9c61bb6a14857b2e6ae291ab839d092a /src/client.cpp
parent04085cad3cc154a48c72127ef816bf25ca636f74 (diff)
downloadminetest-4bf5065a9cdaf55a6915e647e9b5de5281d6622f.tar.gz
minetest-4bf5065a9cdaf55a6915e647e9b5de5281d6622f.tar.bz2
minetest-4bf5065a9cdaf55a6915e647e9b5de5281d6622f.zip
Cache textures by checksum
Diffstat (limited to 'src/client.cpp')
-rw-r--r--src/client.cpp118
1 files changed, 50 insertions, 68 deletions
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: "
- <<name << " expected it at: "<<tpath<<std::endl;
- }
- else
- {
- std::ostringstream tmp_os(std::ios_base::binary);
- bool bad = false;
- for(;;){
- char buf[1024];
- fis.read(buf, 1024);
- std::streamsize len = fis.gcount();
- tmp_os.write(buf, len);
- if(fis.eof())
- break;
- if(!fis.good()){
- bad = true;
- break;
- }
- }
- if(bad){
- infostream<<"Client: Failed to read texture from cache\""
- <<name<<"\""<<std::endl;
- }
- else {
+ std::string sha1_decoded = base64_decode(sha1_texture);
+ std::ostringstream tmp_os(std::ios_base::binary);
+ bool tex_in_cache = m_texture_cache.loadByChecksum(name,
+ tmp_os, sha1_decoded);
+ m_texture_name_sha1_map.set(name, sha1_decoded);
- SHA1 sha1;
- sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length());
+ if(tex_in_cache) {
- unsigned char *digest = sha1.getDigest();
+ SHA1 sha1;
+ sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length());
- std::string digest_string = base64_encode(digest, 20);
+ unsigned char *digest = sha1.getDigest();
- if (digest_string == sha1_texture) {
- // Silly irrlicht's const-incorrectness
- Buffer<char> 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 \""<<name<<"\""<<std::endl;
- rfile->drop();
- }
- else {
- m_tsrc->insertSourceImage(name, img);
- img->drop();
- rfile->drop();
+ if (digest_string == sha1_texture) {
+ // Silly irrlicht's const-incorrectness
+ Buffer<char> 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 \""<<name<<"\""<<std::endl;
+ rfile->drop();
}
else {
- infostream<<"Client::Texture cached sha1 hash not matching server hash: "
- <<name << ": server ->"<<sha1_texture <<" client -> "<<digest_string<<std::endl;
- }
+ m_tsrc->insertSourceImage(name, img);
+ img->drop();
+ rfile->drop();
- free(digest);
+ texture_found = true;
+ }
}
+ else {
+ infostream<<"Client::Texture cached sha1 hash not matching server hash: "
+ <<name << ": server ->"<<sha1_texture <<" client -> "<<digest_string<<std::endl;
+ }
+
+ free(digest);
}
//add texture request
@@ -1598,15 +1576,15 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
fs::CreateAllDirs(getTextureCacheDir());
- std::string filename = getTextureCacheDir() + DIR_DELIM + name;
- std::ofstream outfile(filename.c_str(), std::ios_base::binary | std::ios_base::trunc);
-
- if (outfile.good()) {
- outfile.write(data.c_str(),data.length());
- outfile.close();
- }
- else {
- errorstream<<"Client: Unable to open cached texture file "<< filename <<std::endl;
+ {
+ core::map<std::string, std::string>::Node *n;
+ n = m_texture_name_sha1_map.find(name);
+ if(n == NULL)
+ errorstream<<"The server sent a texture that has not been announced."
+ <<std::endl;
+ else
+ m_texture_cache.updateByChecksum(name,
+ data, n->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();