From 56bddaed273088e427290f7b6caec1e3bd2073ac Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Thu, 7 Apr 2016 16:12:33 +0100 Subject: [PATCH] Cache downloaded skins --- src/render/mod.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/render/mod.rs b/src/render/mod.rs index a058558..90ca10a 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -843,15 +843,32 @@ impl TextureManager { fn process_skins(recv: mpsc::Receiver, reply: mpsc::Sender<(String, Option)>) { use hyper; use std::io::Read; + use std::fs; + use std::path::Path; let client = hyper::Client::new(); loop { // TODO: Cache let hash = recv.recv().unwrap(); trace!("Fetching skin {:?}", hash); - let url = format!("http://textures.minecraft.net/texture/{}", hash); - let mut res = client.get(&url).send().unwrap(); + let path = format!("skin-cache/{}/{}.png", &hash[..2], hash); + let cache_path = Path::new(&path); + fs::create_dir_all(cache_path.parent().unwrap()).unwrap(); let mut buf = vec![]; - res.read_to_end(&mut buf).unwrap(); + if fs::metadata(cache_path).is_ok() { + debug!("Loading skin {} from cache", hash); + // We have a cached image + let mut file = fs::File::open(cache_path).unwrap(); + file.read_to_end(&mut buf).unwrap(); + } else { + debug!("Downloading skin {}", hash); + // Need to download it + let url = format!("http://textures.minecraft.net/texture/{}", hash); + let mut res = client.get(&url).send().unwrap(); + res.read_to_end(&mut buf).unwrap(); + // Save to cache + let mut file = fs::File::create(cache_path).unwrap(); + file.write_all(&buf).unwrap(); + } let img = match image::load_from_memory(&buf) { Ok(val) => val, Err(_) => {