From 5a0505c0f9fb23a75c910fe79ca2f4f5446125da Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 24 Sep 2022 15:20:03 +1000 Subject: [PATCH] Add 'load file' button to texture packs list menu, to allow loading a texture pack .zip from anywhere on disc Note that unlike clicking on an entry in the texture packs list, this change is only temporary (lasts until you close the game) and isn't saved to options.txt --- src/Menus.c | 15 +++++---------- src/TexturePack.c | 19 ++++++++++--------- src/TexturePack.h | 3 +++ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index aff242241..1e456f1e4 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1562,11 +1562,6 @@ static void TexturePackScreen_FilterFiles(const cc_string* path, void* obj) { cc_string relPath = *path; if (!String_CaselessEnds(path, &zip)) return; -#ifdef CC_BUILD_WEB - /* Web client texture pack dir starts with /, so need to get rid of that */ - if (relPath.buffer[0] == '/') { relPath.buffer++; relPath.length--; } -#endif - Utils_UNSAFE_TrimFirstDirectory(&relPath); StringsBuffer_Add((struct StringsBuffer*)obj, &relPath); } @@ -1577,17 +1572,20 @@ static void TexturePackScreen_LoadEntries(struct ListScreen* s) { StringsBuffer_Sort(&s->entries); } -#ifdef CC_BUILD_WEB extern void interop_UploadTexPack(const char* path); static void TexturePackScreen_UploadCallback(const cc_string* path) { +#ifdef CC_BUILD_WEB char str[NATIVE_STR_LEN]; cc_string relPath = *path; Platform_EncodeUtf8(str, path); Utils_UNSAFE_GetFilename(&relPath); interop_UploadTexPack(str); - TexturePackScreen_Show(); + ListScreen_Reload(&ListScreen); TexturePack_SetDefault(&relPath); +#else + String_Copy(&TexturePack_Path, path); +#endif TexturePack_ExtractCurrent(true); } @@ -1595,9 +1593,6 @@ static void TexturePackScreen_UploadFunc(void* s, void* w) { static const char* const filters[] = { ".zip", NULL }; Window_OpenFileDialog(filters, TexturePackScreen_UploadCallback); } -#else -#define TexturePackScreen_UploadFunc NULL -#endif void TexturePackScreen_Show(void) { struct ListScreen* s = &ListScreen; diff --git a/src/TexturePack.c b/src/TexturePack.c index 7d1b42f64..8d1a5b0e0 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -280,15 +280,16 @@ static void UpdateCache(struct HttpRequest* req) { /*########################################################################################################################* *-------------------------------------------------------TexturePack-------------------------------------------------------* *#########################################################################################################################*/ -static char defTexPackBuffer[STRING_SIZE]; static char textureUrlBuffer[STRING_SIZE]; -static cc_string texpackPath = String_FromArray(defTexPackBuffer); -cc_string TexturePack_Url = String_FromArray(textureUrlBuffer); +static char texpackPathBuffer[FILENAME_SIZE]; + +cc_string TexturePack_Url = String_FromArray(textureUrlBuffer); +cc_string TexturePack_Path = String_FromArray(texpackPathBuffer); static const cc_string defaultPath = String_FromConst("texpacks/default.zip"); void TexturePack_SetDefault(const cc_string* texPack) { - texpackPath.length = 0; - String_Format1(&texpackPath, "texpacks/%s", texPack); + TexturePack_Path.length = 0; + String_Format1(&TexturePack_Path, "texpacks/%s", texPack); Options_Set(OPT_DEFAULT_TEX_PACK, texPack); } @@ -356,7 +357,7 @@ static cc_result ExtractFromFile(const cc_string* path) { } static cc_result ExtractDefault(void) { - cc_string path = Game_ClassicMode ? defaultPath : texpackPath; + cc_string path = Game_ClassicMode ? defaultPath : TexturePack_Path; cc_result res = ExtractFromFile(&defaultPath); /* override default.zip with user's default texture pack */ @@ -478,11 +479,11 @@ static void OnInit(void) { Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); - texpackPath.length = 0; + TexturePack_Path.length = 0; if (Options_UNSAFE_Get(OPT_DEFAULT_TEX_PACK, &file)) { - String_Format1(&texpackPath, "texpacks/%s", &file); + String_Format1(&TexturePack_Path, "texpacks/%s", &file); } else { - String_AppendString(&texpackPath, &defaultPath); + String_AppendString(&TexturePack_Path, &defaultPath); } Utils_EnsureDirectory("texpacks"); diff --git a/src/TexturePack.h b/src/TexturePack.h index 3a0d85cce..ec5206b1c 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -51,7 +51,10 @@ CC_VAR extern struct _Atlas1DData { GfxResourceID TexIds[ATLAS1D_MAX_ATLASES]; } Atlas1D; +/* URL of the current custom texture pack, can be empty */ extern cc_string TexturePack_Url; +/* Path to the default texture pack to use */ +extern cc_string TexturePack_Path; #define Atlas2D_TileX(texLoc) ((texLoc) & ATLAS2D_MASK) /* texLoc % ATLAS2D_TILES_PER_ROW */ #define Atlas2D_TileY(texLoc) ((texLoc) >> ATLAS2D_SHIFT) /* texLoc / ATLAS2D_TILES_PER_ROW */