From 27881b6acc25f8e329d706f85c88a4144e6669fe Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 20 Jun 2019 09:49:27 +1000 Subject: [PATCH] simplify texture pack logic a little bit --- src/Formats.c | 4 +--- src/Game.c | 6 +----- src/InputHandler.c | 2 +- src/Protocol.c | 10 +++------- src/Server.c | 15 +++++++++++---- src/TexturePack.h | 1 + src/Utils.c | 1 + 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Formats.c b/src/Formats.c index 224bd29c8..f1649423a 100644 --- a/src/Formats.c +++ b/src/Formats.c @@ -559,9 +559,7 @@ static void Cw_Callback_4(struct NbtTag* tag) { if (IsTag(tag, "TextureURL")) { String url = NbtTag_String(tag); - if (Game_AllowServerTextures && url.length) { - Server_RetrieveTexturePack(&url); - } + if (url.length) Server_RetrieveTexturePack(&url); return; } } diff --git a/src/Game.c b/src/Game.c index 9aa8d0748..16da25352 100644 --- a/src/Game.c +++ b/src/Game.c @@ -190,11 +190,7 @@ void Game_Reset(void) { struct IGameComponent* comp; World_Reset(); Event_RaiseVoid(&WorldEvents.NewMap); - - if (World_TextureUrl.length) { - TexturePack_ExtractDefault(); - World_TextureUrl.length = 0; - } + if (World_TextureUrl.length) TexturePack_ExtractDefault(); for (comp = comps_head; comp; comp = comp->next) { if (comp->Reset) comp->Reset(); diff --git a/src/InputHandler.c b/src/InputHandler.c index a0bf9405c..4b16aa2f6 100644 --- a/src/InputHandler.c +++ b/src/InputHandler.c @@ -17,7 +17,7 @@ #include "Block.h" #include "Menus.h" #include "Gui.h" -#include "PacketHandlers.h" +#include "Protocol.h" #include "AxisLinesRenderer.h" static bool input_buttonsDown[3]; diff --git a/src/Protocol.c b/src/Protocol.c index 0c5aa9f1e..1fe61f3bb 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1,4 +1,4 @@ -#include "PacketHandlers.h" +#include "Protocol.h" #include "Deflate.h" #include "Utils.h" #include "Server.h" @@ -1206,15 +1206,11 @@ static void CPE_SetMapEnvUrl(uint8_t* data) { String_InitArray(url, urlBuffer); Protocol_ReadString(&data, &url); - if (!Game_AllowServerTextures) return; - if (!url.length) { - /* don't extract default texture pack if we can */ - if (World_TextureUrl.length) TexturePack_ExtractDefault(); - } else if (Utils_IsUrlPrefix(&url, 0)) { + if (!url.length || Utils_IsUrlPrefix(&url, 0)) { Server_RetrieveTexturePack(&url); } - Platform_Log1("Image url: %s", &url); + Platform_Log1("Tex url: %s", &url); } static void CPE_SetMapEnvProperty(uint8_t* data) { diff --git a/src/Server.c b/src/Server.c index 2a839afc7..91b304841 100644 --- a/src/Server.c +++ b/src/Server.c @@ -18,7 +18,7 @@ #include "TexturePack.h" #include "Menus.h" #include "Logger.h" -#include "PacketHandlers.h" +#include "Protocol.h" #include "Inventory.h" #include "Platform.h" #include "GameStructs.h" @@ -41,10 +41,15 @@ static void Server_ResetState(void) { } void Server_RetrieveTexturePack(const String* url) { - if (!TextureCache_HasAccepted(url) && !TextureCache_HasDenied(url)) { - Gui_ShowOverlay(TexPackOverlay_MakeInstance(url)); - } else { + 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); + } else { + Gui_ShowOverlay(TexPackOverlay_MakeInstance(url)); } } @@ -57,6 +62,8 @@ void Server_DownloadTexturePack(const String* url) { String_InitArray(etag, etagBuffer); String_InitArray(time, timeBuffer); + /* Only retrieve etag/last-modified headers if the file exists */ + /* This can occur if user decided to delete some cached files */ if (TextureCache_Has(url)) { TextureCache_GetLastModified(url, &time); TextureCache_GetETag(url, &etag); diff --git a/src/TexturePack.h b/src/TexturePack.h index e6a54e793..a85a2dcd0 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -95,6 +95,7 @@ void TextureCache_GetETag(const String* url, String* etag); void TextureCache_Set(const String* url, const uint8_t* data, uint32_t length); void TexturePack_ExtractZip_File(const String* filename); +/* Extracts user's default texture pack, then resets World_TextureUrl. */ void TexturePack_ExtractDefault(void); void TexturePack_ExtractCurrent(const String* url); void TexturePack_Extract_Req(struct HttpRequest* item); diff --git a/src/Utils.c b/src/Utils.c index f4f4c494b..9351472ca 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -317,6 +317,7 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) { if (res == ERR_END_OF_STREAM) break; if (res) { Logger_Warn2(res, "reading from", &path); break; } + /* whitespace lines are ignored (e.g. if user manually edits file) */ String_UNSAFE_TrimStart(&entry); String_UNSAFE_TrimEnd(&entry);