extract entry from texcache before doing async resource check. avoids 1-2 seconds of pink textures

This commit is contained in:
UnknownShadow200 2017-01-30 14:47:38 +11:00
parent 7a96cac27e
commit fb95a6a8b4
2 changed files with 57 additions and 50 deletions

View File

@ -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

View File

@ -51,8 +51,8 @@ namespace ClassicalSharp.Textures {
}
internal static void ExtractTerrainPng(Game game, string url, DownloadedItem item) {
if (item != null && item.Data != null) {
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();
@ -62,10 +62,12 @@ namespace ClassicalSharp.Textures {
TextureCache.Add(item.Url, bmp);
TextureCache.AddETag(item.Url, item.ETag, game.ETags);
TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified);
} else {
}
internal static void ExtractCachedTerrainPng(Game game, string url) {
FileStream data = TextureCache.GetStream(url);
if (data == null) { // e.g. 404 errors
ExtractDefault(game);
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; }
@ -80,12 +82,12 @@ namespace ClassicalSharp.Textures {
data.Dispose();
}
}
}
internal static void ExtractTexturePack(Game game, string url, DownloadedItem item) {
if (item != null && item.Data != null) {
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);
@ -94,17 +96,19 @@ namespace ClassicalSharp.Textures {
TextureCache.Add(item.Url, data);
TextureCache.AddETag(item.Url, item.ETag, game.ETags);
TextureCache.AdddLastModified(item.Url, item.LastModified, game.LastModified);
} else {
}
internal static void ExtractCachedTexturePack(Game game, string url) {
FileStream data = TextureCache.GetStream(url);
if (data == null) { // e.g. 404 errors
ExtractDefault(game);
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 {