From c942d507d1be2594493518547eee4e7acf35ddef Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 1 Dec 2021 21:07:25 +1100 Subject: [PATCH] Also reload map list if file not found error --- src/Formats.c | 9 +++++---- src/Formats.h | 2 +- src/Menus.c | 22 ++++++++++++++++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Formats.c b/src/Formats.c index 616665813..15124eee8 100644 --- a/src/Formats.c +++ b/src/Formats.c @@ -54,28 +54,29 @@ IMapImporter Map_FindImporter(const cc_string* path) { return NULL; } -void Map_LoadFrom(const cc_string* path) { +cc_result Map_LoadFrom(const cc_string* path) { IMapImporter importer; struct Stream stream; cc_result res; Game_Reset(); res = Stream_OpenFile(&stream, path); - if (res) { Logger_SysWarn2(res, "opening", path); return; } + if (res) { Logger_SysWarn2(res, "opening", path); return res; } importer = Map_FindImporter(path); if (!importer) { - Logger_SysWarn2(ERR_NOT_SUPPORTED, "decoding", path); + res = ERR_NOT_SUPPORTED; } else if ((res = importer(&stream))) { World_Reset(); - Logger_SysWarn2(res, "decoding", path); } /* No point logging error for closing readonly file */ stream.Close(&stream); + if (res) Logger_SysWarn2(res, "decoding", path); World_SetNewMap(World.Blocks, World.Width, World.Height, World.Length); LocalPlayer_MoveToSpawn(); + return res; } diff --git a/src/Formats.h b/src/Formats.h index b4ef3c2cb..d47771e32 100644 --- a/src/Formats.h +++ b/src/Formats.h @@ -13,7 +13,7 @@ typedef cc_result (*IMapImporter)(struct Stream* stream); CC_API IMapImporter Map_FindImporter(const cc_string* path); /* Attempts to import the map from the given file. */ /* NOTE: Uses Map_FindImporter to import based on filename. */ -CC_API void Map_LoadFrom(const cc_string* path); +CC_API cc_result Map_LoadFrom(const cc_string* path); /* Imports a world from a .lvl MCSharp server map file. */ /* Used by MCSharp/MCLawl/MCForge/MCDzienny/MCGalaxy. */ diff --git a/src/Menus.c b/src/Menus.c index 687d29f68..1b6cec38c 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -413,6 +413,12 @@ static void ListScreen_ContextRecreated(void* screen) { ButtonWidget_SetConst(&s->upload, "Upload", &s->font); } +static void ListScreen_Reload(struct ListScreen* s) { + ListScreen_Free(s); + s->LoadEntries(s); + ListScreen_SetCurrentIndex(s, s->currentIndex); +} + static const struct ScreenVTABLE ListScreen_VTABLE = { ListScreen_Init, Screen_NullUpdate, ListScreen_Free, ListScreen_Render, Screen_BuildMesh, @@ -1553,13 +1559,11 @@ static void TexturePackScreen_EntryClick(void* screen, void* widget) { TexturePack_SetDefault(&file); TexturePack_Url.length = 0; res = TexturePack_ExtractCurrent(true); - if (res != ReturnCode_FileNotFound) return; - /* FileNotFound error may be because user deleted.zips from disc */ + /* FileNotFound 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); + ListScreen_Reload(s); } static void TexturePackScreen_FilterFiles(const cc_string* path, void* obj) { @@ -1745,11 +1749,17 @@ void HotkeyListScreen_Show(void) { static void LoadLevelScreen_EntryClick(void* screen, void* widget) { cc_string path; char pathBuffer[FILENAME_SIZE]; struct ListScreen* s = (struct ListScreen*)screen; + cc_result res; cc_string relPath = ListScreen_UNSAFE_GetCur(s, widget); String_InitArray(path, pathBuffer); String_Format1(&path, "maps/%s", &relPath); - Map_LoadFrom(&path); + res = Map_LoadFrom(&path); + + /* FileNotFound error may be because user deleted maps from disc */ + if (res != ReturnCode_FileNotFound) return; + Chat_AddRaw("&eReloading level list as it may be out of date"); + ListScreen_Reload(s); } static void LoadLevelScreen_FilterFiles(const cc_string* path, void* obj) {