From bd3ceb1393be694c9c8a8278bda9a394a1af72f9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 21 Jun 2019 19:49:56 +1000 Subject: [PATCH] finish texture functions rewrite --- src/Game.c | 31 ++++++++++++----------------- src/Game.h | 2 +- src/Menus.c | 18 +++++------------ src/Server.c | 14 ++----------- src/Server.h | 8 ++------ src/TexturePack.c | 50 +++++++++++++++++++---------------------------- src/TexturePack.h | 10 +++------- src/World.c | 7 +++++-- src/World.h | 5 +++-- 9 files changed, 54 insertions(+), 91 deletions(-) diff --git a/src/Game.c b/src/Game.c index 16da25352..51121907b 100644 --- a/src/Game.c +++ b/src/Game.c @@ -110,18 +110,14 @@ float Game_GetChatScale(void) { static char game_defTexPackBuffer[STRING_SIZE]; static String game_defTexPack = String_FromArray(game_defTexPackBuffer); +static const String defaultZip = String_FromConst("default.zip"); -void Game_GetDefaultTexturePack(String* texPack) { +String Game_UNSAFE_GetDefaultTexturePack(void) { String texPath; char texPathBuffer[STRING_SIZE]; - String_InitArray(texPath, texPathBuffer); - String_Format1(&texPath, "texpacks/%s", &game_defTexPack); - if (File_Exists(&texPath) && !Game_ClassicMode) { - String_AppendString(texPack, &game_defTexPack); - } else { - String_AppendConst(texPack, "default.zip"); - } + String_Format1(&texPath, "texpacks/%s", &game_defTexPack); + return File_Exists(&texPath) && !Game_ClassicMode ? game_defTexPack : defaultZip; } void Game_SetDefaultTexturePack(const String* texPack) { @@ -190,7 +186,11 @@ void Game_Reset(void) { struct IGameComponent* comp; World_Reset(); Event_RaiseVoid(&WorldEvents.NewMap); - if (World_TextureUrl.length) TexturePack_ExtractDefault(); + + if (World_TextureUrl.length) { + World_TextureUrl.length = 0; + TexturePack_ExtractCurrent(false); + } for (comp = comps_head; comp; comp = comp->next) { if (comp->Reset) comp->Reset(); @@ -331,18 +331,13 @@ static void Game_WarnFunc(const String* msg) { } static void Game_ExtractInitialTexturePack(void) { - String texPack; char texPackBuffer[STRING_SIZE]; - + String texPack; Options_Get(OPT_DEFAULT_TEX_PACK, &game_defTexPack, "default.zip"); - String_InitArray(texPack, texPackBuffer); - - String_AppendConst(&texPack, "default.zip"); - TexturePack_ExtractZip_File(&texPack); - texPack.length = 0; + TexturePack_ExtractZip_File(&defaultZip); /* in case the user's default texture pack doesn't have all required textures */ - Game_GetDefaultTexturePack(&texPack); - if (!String_CaselessEqualsConst(&texPack, "default.zip")) { + texPack = Game_UNSAFE_GetDefaultTexturePack(); + if (!String_CaselessEquals(&texPack, "default.zip")) { TexturePack_ExtractZip_File(&texPack); } } diff --git a/src/Game.h b/src/Game.h index 65c692e8c..baa15b85d 100644 --- a/src/Game.h +++ b/src/Game.h @@ -61,7 +61,7 @@ float Game_GetChatScale(void); /* Retrieves the filename of the default texture pack used. */ /* NOTE: Returns default.zip if classic mode or selected pack does not exist. */ -void Game_GetDefaultTexturePack(String* texPack); +String Game_UNSAFE_GetDefaultTexturePack(void); /* Sets the filename of the default texture pack used. */ void Game_SetDefaultTexturePack(const String* texPack); diff --git a/src/Menus.c b/src/Menus.c index 1f030cb6a..3c8859e05 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1375,7 +1375,9 @@ static void TexturePackScreen_EntryClick(void* screen, void* widget) { idx = s->currentIndex; Game_SetDefaultTexturePack(&filename); - TexturePack_ExtractDefault(); + World_TextureUrl.length = 0; + TexturePack_ExtractCurrent(true); + Elem_Recreate(s); ListScreen_SetCurrentIndex(s, idx); } @@ -2407,18 +2409,8 @@ static void GraphicsOptionsScreen_SetShadows(const String* v) { static void GraphicsOptionsScreen_GetMipmaps(String* v) { Menu_GetBool(v, Gfx.Mipmaps); } static void GraphicsOptionsScreen_SetMipmaps(const String* v) { - String url; char urlBuffer[STRING_SIZE]; - Gfx.Mipmaps = Menu_SetBool(v, OPT_MIPMAPS); - String_InitArray(url, urlBuffer); - String_Copy(&url, &World_TextureUrl); - - /* always force a reload from cache */ - World_TextureUrl.length = 0; - String_AppendConst(&World_TextureUrl, "~`#$_^*()@"); - TexturePack_ExtractCurrent(&url); - - String_Copy(&World_TextureUrl, &url); + TexturePack_ExtractCurrent(true); } static void GraphicsOptionsScreen_ContextRecreated(void* screen) { @@ -3194,7 +3186,7 @@ static void TexPackOverlay_YesClick(void* screen, void* widget) { struct TexPackOverlay* s = (struct TexPackOverlay*)screen; String url = String_UNSAFE_SubstringAt(&s->identifier, 3); - Server_DownloadTexturePack(&url); + World_ApplyTexturePack(&url); if (WarningOverlay_IsAlways(s, widget)) TextureCache_Accept(&url); Elem_Free(s); } diff --git a/src/Server.c b/src/Server.c index ff5869f75..9cdd2636b 100644 --- a/src/Server.c +++ b/src/Server.c @@ -43,23 +43,13 @@ static void Server_ResetState(void) { void Server_RetrieveTexturePack(const String* url) { if (!Game_AllowServerTextures || TextureCache_HasDenied(url)) return; - if (!url->length) { - /* Only extract default texture pack when necessary */ - if (World_TextureUrl.length) TexturePack_ExtractDefault(); - } else if (TextureCache_HasAccepted(url)) { - Server_DownloadTexturePack(url); + if (!url->length || TextureCache_HasAccepted(url)) { + World_ApplyTexturePack(url); } else { Gui_ShowOverlay(TexPackOverlay_MakeInstance(url)); } } -void Server_DownloadTexturePack(const String* url) { - static const String texPack = String_FromConst("texturePack"); - TexturePack_ExtractCurrent(url); - TexturePack_DownloadAsync(url, &texPack); - String_Copy(&World_TextureUrl, url); -} - static void Server_CheckAsyncResources(void) { static const String texPack = String_FromConst("texturePack"); struct HttpRequest item; diff --git a/src/Server.h b/src/Server.h index 70b93f644..9dd36b40f 100644 --- a/src/Server.h +++ b/src/Server.h @@ -96,13 +96,9 @@ CC_VAR extern struct _ServerConnectionData { int Port; } Server; -/* If the user hasn't previously accepted url, displays a dialog asking to confirm downloading it. */ -/* Otherwise just calls Server_DownloadTexturePack. */ +/* If user hasn't previously accepted url, displays a dialog asking to confirm downloading it. */ +/* Otherwise just calls World_ApplyTexturePack. */ void Server_RetrieveTexturePack(const String* url); -/* Queues the given URL to be asynchronously downloaded. */ -/* The texture pack will later be loaded in Server.Tick, once it has finished downloading. */ -/* NOTE: If a cached texture pack exists for the given URL, that gets immediately loaded. */ -void Server_DownloadTexturePack(const String* url); typedef void (*Net_Handler)(uint8_t* data); extern uint16_t Net_PacketSizes[OPCODE_COUNT]; diff --git a/src/TexturePack.c b/src/TexturePack.c index 859960d02..175898e01 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -656,7 +656,7 @@ static ReturnCode TexturePack_ExtractZip(struct Stream* stream) { /* Changes the current terrain atlas from a stream representing a .png image */ /* Raises TextureEvents.PackChanged, so behaves as a .zip with only terrain.png in it */ -static ReturnCode TexturePack_ExtractTerrainPng(struct Stream* stream) { +static ReturnCode TexturePack_ExtractPng(struct Stream* stream) { Bitmap bmp; ReturnCode res = Png_Decode(&bmp, stream); @@ -701,39 +701,29 @@ void TexturePack_ExtractZip_File(const String* filename) { #endif } -void TexturePack_ExtractDefault(void) { - String texPack; char texPackBuffer[STRING_SIZE]; - - String_InitArray(texPack, texPackBuffer); - Game_GetDefaultTexturePack(&texPack); - - TexturePack_ExtractZip_File(&texPack); - World_TextureUrl.length = 0; -} - -void TexturePack_ExtractCurrent(const String* url) { +static bool texturePackDefault = true; +void TexturePack_ExtractCurrent(bool forceReload) { static const String zipExt = String_FromConst(".zip"); + String url = World_TextureUrl, file; struct Stream stream; bool zip; - ReturnCode res = 0; + ReturnCode res; - if (!url->length) { - TexturePack_ExtractDefault(); - } else if (!TextureCache_Get(url, &stream)) { - /* e.g. 404 errors */ - if (World_TextureUrl.length) TexturePack_ExtractDefault(); + if (!url.length || !TextureCache_Get(&url, &stream)) { + /* don't pointlessly load default texture pack */ + if (texturePackDefault && !forceReload) return; + file = Game_UNSAFE_GetDefaultTexturePack(); + + TexturePack_ExtractZip_File(&file); + texturePackDefault = true; } else { - if (!String_Equals(url, &World_TextureUrl)) { - zip = String_ContainsString(url, &zipExt); - String_Copy(&World_TextureUrl, url); - - res = zip ? TexturePack_ExtractZip(&stream) : - TexturePack_ExtractTerrainPng(&stream); - if (res) Logger_Warn2(res, zip ? "extracting" : "decoding", url); - } + zip = String_ContainsString(&url, &zipExt); + res = zip ? TexturePack_ExtractZip(&stream) : TexturePack_ExtractPng(&stream); + if (res) Logger_Warn2(res, zip ? "extracting" : "decoding", &url); res = stream.Close(&stream); - if (res) Logger_Warn2(res, "closing cache for", url); + if (res) Logger_Warn2(res, "closing cache for", &url); + texturePackDefault = false; } } @@ -748,13 +738,13 @@ void TexturePack_Extract_Req(struct HttpRequest* item) { String_Copy(&World_TextureUrl, &url); data = item->Data; len = item->Size; - Stream_ReadonlyMemory(&mem, data, len); + png = Png_Detect(data, len); - res = png ? TexturePack_ExtractTerrainPng(&mem) - : TexturePack_ExtractZip(&mem); + res = png ? TexturePack_ExtractPng(&mem) : TexturePack_ExtractZip(&mem); if (res) Logger_Warn2(res, png ? "decoding" : "extracting", &url); + texturePackDefault = false; } void TexturePack_DownloadAsync(const String* url, const String* id) { diff --git a/src/TexturePack.h b/src/TexturePack.h index 8a0a4a0f5..1eb59744e 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -92,13 +92,9 @@ void TextureCache_Update(struct HttpRequest* req); /* Extracts a texture pack .zip from the given file. */ void TexturePack_ExtractZip_File(const String* filename); -/* Extracts user's default texture pack, then resets World_TextureUrl. */ -void TexturePack_ExtractDefault(void); -/* Extracts the current texture pack or terrain.png. 3 cases: */ -/* - Server has not set a URL, so just extract default */ -/* - URL is in texture cache, so extract cached version */ -/* - URL is not cached, so extract default for now */ -void TexturePack_ExtractCurrent(const String* url); +/* If World_TextureUrl is empty, extracts user's default texture pack. */ +/* Otherwise extracts the cached texture pack for that URL. */ +void TexturePack_ExtractCurrent(bool forceReload); /* Asynchronously downloads a texture pack. */ /* Sends ETag and Last-Modified to webserver to avoid redundant downloads. */ /* NOTE: This does not load cached textures - use TexturePack_ExtractCurrent for that. */ diff --git a/src/World.c b/src/World.c index a7b045cb6..15878dbcf 100644 --- a/src/World.c +++ b/src/World.c @@ -138,8 +138,11 @@ String World_TextureUrl = String_FromArray(World_TextureUrlBuffer); void World_ApplyTexturePack(const String* url) { static const String texPack = String_FromConst("texturePack"); - TexturePack_ExtractCurrent(url); - TexturePack_DownloadAsync(url, &texPack); + if (url->length) TexturePack_DownloadAsync(url, &texPack); + + if (String_Equals(url, &World_TextureUrl)) return; + String_Copy(&World_TextureUrl, url); + TexturePack_ExtractCurrent(false); } void Env_Reset(void) { diff --git a/src/World.h b/src/World.h index e11605055..442892957 100644 --- a/src/World.h +++ b/src/World.h @@ -123,8 +123,9 @@ extern const PackedCol Env_DefaultSunCol, Env_DefaultShadowCol; #define ENV_DEFAULT_SUNCOL_HEX "FFFFFF" #define ENV_DEFAULT_SHADOWCOL_HEX "9B9B9B" -/* Extracts texture pack cached for the given URL (or default.zip if not), */ -/* then asynchronously downloads the data for the given URL. */ +/* If url is empty, extracts default texture pack. */ +/* Else tries extracting cached texture pack for the given URL, */ +/* then asynchronously downloads the texture pack from the given URL. */ CC_API void World_ApplyTexturePack(const String* url); /* Resets all environment settings to default. */ /* NOTE: Unlike Env_Set functions, DOES NOT raise EnvVarChanged event. */