Auto reload texture pack list screen if trying to load a .zip returns file not found

This commit is contained in:
UnknownShadow200 2021-11-21 20:05:54 +11:00
parent a761f2718b
commit 79fc4cb599
3 changed files with 30 additions and 14 deletions

View File

@ -1548,10 +1548,19 @@ void SaveLevelScreen_Show(void) {
static void TexturePackScreen_EntryClick(void* screen, void* widget) { static void TexturePackScreen_EntryClick(void* screen, void* widget) {
struct ListScreen* s = (struct ListScreen*)screen; struct ListScreen* s = (struct ListScreen*)screen;
cc_string file = ListScreen_UNSAFE_GetCur(s, widget); cc_string file = ListScreen_UNSAFE_GetCur(s, widget);
cc_result res;
TexturePack_SetDefault(&file); TexturePack_SetDefault(&file);
TexturePack_Url.length = 0; 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) { static void TexturePackScreen_FilterFiles(const cc_string* path, void* obj) {

View File

@ -314,13 +314,13 @@ static cc_result ExtractPng(struct Stream* stream) {
} }
static cc_bool needReload; 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; cc_result res;
Event_RaiseVoid(&TextureEvents.PackChanged); Event_RaiseVoid(&TextureEvents.PackChanged);
/* If context is lost, then trying to load textures will just fail */ /* If context is lost, then trying to load textures will just fail */
/* So defer loading the texture pack until context is restored */ /* 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; needReload = false;
if (String_ContainsConst(path, ".zip")) { if (String_ContainsConst(path, ".zip")) {
@ -330,9 +330,10 @@ static void ExtractFrom(struct Stream* stream, const cc_string* path) {
res = ExtractPng(stream); res = ExtractPng(stream);
if (res) Logger_SysWarn2(res, "decoding", path); 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]; cc_string path; char pathBuffer[FILENAME_SIZE];
struct Stream stream; struct Stream stream;
cc_result res; cc_result res;
@ -345,41 +346,47 @@ static void ExtractFromFile(const cc_string* filename) {
/* Game shows a dialog if default.zip is missing */ /* Game shows a dialog if default.zip is missing */
Game_DefaultZipMissing |= res == ReturnCode_FileNotFound Game_DefaultZipMissing |= res == ReturnCode_FileNotFound
&& String_CaselessEquals(filename, &defaultZip); && 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 */ /* No point logging error for closing readonly file */
(void)stream.Close(&stream); (void)stream.Close(&stream);
return res;
} }
static void ExtractDefault(void) { static cc_result ExtractDefault(void) {
cc_string texPack = Game_ClassicMode ? defaultZip : defTexPack; 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 */ /* 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; static cc_bool usingDefault;
void TexturePack_ExtractCurrent(cc_bool forceReload) { cc_result TexturePack_ExtractCurrent(cc_bool forceReload) {
cc_string url = TexturePack_Url; cc_string url = TexturePack_Url;
struct Stream stream; struct Stream stream;
cc_result res; cc_result res = 0;
/* don't pointlessly load default texture pack */ /* don't pointlessly load default texture pack */
if (!usingDefault || forceReload) { if (!usingDefault || forceReload) {
ExtractDefault(); res = ExtractDefault();
usingDefault = true; usingDefault = true;
} }
if (url.length && OpenCachedData(&url, &stream)) { if (url.length && OpenCachedData(&url, &stream)) {
ExtractFrom(&stream, &url); res = ExtractFrom(&stream, &url);
usingDefault = false; usingDefault = false;
/* No point logging error for closing readonly file */ /* No point logging error for closing readonly file */
(void)stream.Close(&stream); (void)stream.Close(&stream);
} }
return res;
} }
/* Extracts and updates cache for the downloaded texture pack */ /* Extracts and updates cache for the downloaded texture pack */

View File

@ -93,7 +93,7 @@ extern int TexturePack_ReqID;
void TexturePack_SetDefault(const cc_string* texPack); void TexturePack_SetDefault(const cc_string* texPack);
/* If TexturePack_Url is empty, extracts user's default texture pack. */ /* If TexturePack_Url is empty, extracts user's default texture pack. */
/* Otherwise extracts the cached texture pack for that URL. */ /* 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. */ /* Checks if the texture pack currently being downloaded has completed. */
/* If completed, then applies the downloaded texture pack and updates cache */ /* If completed, then applies the downloaded texture pack and updates cache */
void TexturePack_CheckPending(void); void TexturePack_CheckPending(void);