diff --git a/ClassicalSharp/Network/IServerConnection.cs b/ClassicalSharp/Network/IServerConnection.cs index dd78e2e42..361a48e83 100644 --- a/ClassicalSharp/Network/IServerConnection.cs +++ b/ClassicalSharp/Network/IServerConnection.cs @@ -118,21 +118,24 @@ namespace ClassicalSharp { DateTime lastModified = TextureCache.GetLastModified(url, game.LastModified); string etag = TextureCache.GetETag(url, game.ETags); - if (url.Contains(".zip")) + if (url.Contains(".zip")) { + TexturePack.ExtractCachedTexturePack(game, url); game.AsyncDownloader.DownloadData(url, true, "texturePack", lastModified, etag); - else + } else { + TexturePack.ExtractCachedTerrainPng(game, url); game.AsyncDownloader.DownloadImage(url, true, "terrain", lastModified, etag); + } } protected void CheckAsyncResources() { DownloadedItem item; if (game.AsyncDownloader.TryGetItem("terrain", out item)) { - TexturePack.ExtractTerrainPng(game, item.Url, item); + TexturePack.ExtractTerrainPng(game, item); } if (game.AsyncDownloader.TryGetItem("texturePack", out item)) { - TexturePack.ExtractTexturePack(game, item.Url, item); + TexturePack.ExtractTexturePack(game, item); } } #endregion diff --git a/ClassicalSharp/TexturePack/TexturePack.cs b/ClassicalSharp/TexturePack/TexturePack.cs index 122c5e10f..5fc40ed7e 100644 --- a/ClassicalSharp/TexturePack/TexturePack.cs +++ b/ClassicalSharp/TexturePack/TexturePack.cs @@ -51,60 +51,64 @@ namespace ClassicalSharp.Textures { } - internal static void ExtractTerrainPng(Game game, string url, DownloadedItem item) { - if (item != null && item.Data != null) { - Bitmap bmp = (Bitmap)item.Data; - game.World.TextureUrl = item.Url; + internal static void ExtractTerrainPng(Game game, DownloadedItem item) { + if (item.Data == null) return; + Bitmap bmp = (Bitmap)item.Data; + game.World.TextureUrl = item.Url; + game.Events.RaiseTexturePackChanged(); + + if (!game.ChangeTerrainAtlas(bmp, null)) { bmp.Dispose(); return; } + + TextureCache.Add(item.Url, bmp); + TextureCache.AddETag(item.Url, item.ETag, game.ETags); + TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified); + } + + internal static void ExtractCachedTerrainPng(Game game, string url) { + FileStream data = TextureCache.GetStream(url); + if (data == null) { // e.g. 404 errors + if (game.World.TextureUrl != null) ExtractDefault(game); + } else if (url != game.World.TextureUrl) { + Bitmap bmp = GetBitmap(game.Drawer2D, data); + if (bmp == null) { data.Dispose(); return; } + + game.World.TextureUrl = url; game.Events.RaiseTexturePackChanged(); + if (game.ChangeTerrainAtlas(bmp, data)) return; - if (!game.ChangeTerrainAtlas(bmp, null)) { bmp.Dispose(); return; } - - TextureCache.Add(item.Url, bmp); - TextureCache.AddETag(item.Url, item.ETag, game.ETags); - TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified); + bmp.Dispose(); + data.Dispose(); } else { - FileStream data = TextureCache.GetStream(url); - if (data == null) { // e.g. 404 errors - ExtractDefault(game); - } else if (url != game.World.TextureUrl) { - Bitmap bmp = GetBitmap(game.Drawer2D, data); - if (bmp == null) { data.Dispose(); return; } - - game.World.TextureUrl = url; - game.Events.RaiseTexturePackChanged(); - if (game.ChangeTerrainAtlas(bmp, data)) return; - - bmp.Dispose(); - data.Dispose(); - } else { - data.Dispose(); - } + data.Dispose(); } } - internal static void ExtractTexturePack(Game game, string url, DownloadedItem item) { - if (item != null && item.Data != null) { - game.World.TextureUrl = item.Url; - byte[] data = (byte[])item.Data; - TexturePack extractor = new TexturePack(); - using (Stream ms = new MemoryStream(data)) { - extractor.Extract(ms, game); - } - - TextureCache.Add(item.Url, data); - TextureCache.AddETag(item.Url, item.ETag, game.ETags); - TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified); - } else { - FileStream data = TextureCache.GetStream(url); - if (data == null) { // e.g. 404 errors - ExtractDefault(game); - } else if (url != game.World.TextureUrl) { - game.World.TextureUrl = url; - TexturePack extractor = new TexturePack(); - extractor.Extract(data, game); - } + internal static void ExtractTexturePack(Game game, DownloadedItem item) { + if (item.Data == null) return; + game.World.TextureUrl = item.Url; + byte[] data = (byte[])item.Data; + + TexturePack extractor = new TexturePack(); + using (Stream ms = new MemoryStream(data)) { + extractor.Extract(ms, game); } + + TextureCache.Add(item.Url, data); + TextureCache.AddETag(item.Url, item.ETag, game.ETags); + TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified); } + + internal static void ExtractCachedTexturePack(Game game, string url) { + FileStream data = TextureCache.GetStream(url); + if (data == null) { // e.g. 404 errors + if (game.World.TextureUrl != null) ExtractDefault(game); + } else if (url != game.World.TextureUrl) { + game.World.TextureUrl = url; + TexturePack extractor = new TexturePack(); + extractor.Extract(data, game); + } + } + static Bitmap GetBitmap(IDrawer2D drawer, Stream src) { try {