From 79fc4cb599ab6643c9eccdf0487db101816f143b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 21 Nov 2021 20:05:54 +1100 Subject: [PATCH] Auto reload texture pack list screen if trying to load a .zip returns file not found --- src/Menus.c | 11 ++++++++++- src/TexturePack.c | 31 +++++++++++++++++++------------ src/TexturePack.h | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index a1abe7b38..282a9a64f 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1548,10 +1548,19 @@ void SaveLevelScreen_Show(void) { static void TexturePackScreen_EntryClick(void* screen, void* widget) { struct ListScreen* s = (struct ListScreen*)screen; cc_string file = ListScreen_UNSAFE_GetCur(s, widget); + cc_result res; TexturePack_SetDefault(&file); TexturePack_Url.length = 0; - TexturePack_ExtractCurrent(true); + res = TexturePack_ExtractCurrent(true); + + /* Load error may be because user deleted .zips from disc */ + if (res != ReturnCode_FileNotFound) return; + + Chat_AddRaw("&eReloading texture pack list as it may be out of date"); + ListScreen_Free(s); + s->LoadEntries(s); + ListScreen_SetCurrentIndex(s, s->currentIndex); } static void TexturePackScreen_FilterFiles(const cc_string* path, void* obj) { diff --git a/src/TexturePack.c b/src/TexturePack.c index 57b3b91c7..4b379dd2b 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -314,13 +314,13 @@ static cc_result ExtractPng(struct Stream* stream) { } static cc_bool needReload; -static void ExtractFrom(struct Stream* stream, const cc_string* path) { +static cc_result ExtractFrom(struct Stream* stream, const cc_string* path) { cc_result res; Event_RaiseVoid(&TextureEvents.PackChanged); /* If context is lost, then trying to load textures will just fail */ /* So defer loading the texture pack until context is restored */ - if (Gfx.LostContext) { needReload = true; return; } + if (Gfx.LostContext) { needReload = true; return 0; } needReload = false; if (String_ContainsConst(path, ".zip")) { @@ -330,9 +330,10 @@ static void ExtractFrom(struct Stream* stream, const cc_string* path) { res = ExtractPng(stream); if (res) Logger_SysWarn2(res, "decoding", path); } + return res; } -static void ExtractFromFile(const cc_string* filename) { +static cc_result ExtractFromFile(const cc_string* filename) { cc_string path; char pathBuffer[FILENAME_SIZE]; struct Stream stream; cc_result res; @@ -345,41 +346,47 @@ static void ExtractFromFile(const cc_string* filename) { /* Game shows a dialog if default.zip is missing */ Game_DefaultZipMissing |= res == ReturnCode_FileNotFound && String_CaselessEquals(filename, &defaultZip); - Logger_SysWarn2(res, "opening", &path); return; + Logger_SysWarn2(res, "opening", &path); + return res; } - ExtractFrom(&stream, &path); + res = ExtractFrom(&stream, &path); /* No point logging error for closing readonly file */ (void)stream.Close(&stream); + return res; } -static void ExtractDefault(void) { +static cc_result ExtractDefault(void) { cc_string texPack = Game_ClassicMode ? defaultZip : defTexPack; - ExtractFromFile(&defaultZip); + cc_result res = ExtractFromFile(&defaultZip); /* in case the user's default texture pack doesn't have all required textures */ - if (!String_CaselessEquals(&texPack, &defaultZip)) ExtractFromFile(&texPack); + if (!String_CaselessEquals(&texPack, &defaultZip)) { + res = ExtractFromFile(&texPack); + } + return res; } static cc_bool usingDefault; -void TexturePack_ExtractCurrent(cc_bool forceReload) { +cc_result TexturePack_ExtractCurrent(cc_bool forceReload) { cc_string url = TexturePack_Url; struct Stream stream; - cc_result res; + cc_result res = 0; /* don't pointlessly load default texture pack */ if (!usingDefault || forceReload) { - ExtractDefault(); + res = ExtractDefault(); usingDefault = true; } if (url.length && OpenCachedData(&url, &stream)) { - ExtractFrom(&stream, &url); + res = ExtractFrom(&stream, &url); usingDefault = false; /* No point logging error for closing readonly file */ (void)stream.Close(&stream); } + return res; } /* Extracts and updates cache for the downloaded texture pack */ diff --git a/src/TexturePack.h b/src/TexturePack.h index d5f3e0b88..918ceeaec 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -93,7 +93,7 @@ extern int TexturePack_ReqID; void TexturePack_SetDefault(const cc_string* texPack); /* If TexturePack_Url is empty, extracts user's default texture pack. */ /* Otherwise extracts the cached texture pack for that URL. */ -void TexturePack_ExtractCurrent(cc_bool forceReload); +cc_result TexturePack_ExtractCurrent(cc_bool forceReload); /* Checks if the texture pack currently being downloaded has completed. */ /* If completed, then applies the downloaded texture pack and updates cache */ void TexturePack_CheckPending(void);