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
This commit is contained in:
UnknownShadow200 2022-09-24 15:20:03 +10:00
parent 6f3d40da20
commit 5a0505c0f9
3 changed files with 18 additions and 19 deletions

View File

@ -1562,11 +1562,6 @@ static void TexturePackScreen_FilterFiles(const cc_string* path, void* obj) {
cc_string relPath = *path; cc_string relPath = *path;
if (!String_CaselessEnds(path, &zip)) return; 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); Utils_UNSAFE_TrimFirstDirectory(&relPath);
StringsBuffer_Add((struct StringsBuffer*)obj, &relPath); StringsBuffer_Add((struct StringsBuffer*)obj, &relPath);
} }
@ -1577,17 +1572,20 @@ static void TexturePackScreen_LoadEntries(struct ListScreen* s) {
StringsBuffer_Sort(&s->entries); StringsBuffer_Sort(&s->entries);
} }
#ifdef CC_BUILD_WEB
extern void interop_UploadTexPack(const char* path); extern void interop_UploadTexPack(const char* path);
static void TexturePackScreen_UploadCallback(const cc_string* path) { static void TexturePackScreen_UploadCallback(const cc_string* path) {
#ifdef CC_BUILD_WEB
char str[NATIVE_STR_LEN]; char str[NATIVE_STR_LEN];
cc_string relPath = *path; cc_string relPath = *path;
Platform_EncodeUtf8(str, path); Platform_EncodeUtf8(str, path);
Utils_UNSAFE_GetFilename(&relPath); Utils_UNSAFE_GetFilename(&relPath);
interop_UploadTexPack(str); interop_UploadTexPack(str);
TexturePackScreen_Show(); ListScreen_Reload(&ListScreen);
TexturePack_SetDefault(&relPath); TexturePack_SetDefault(&relPath);
#else
String_Copy(&TexturePack_Path, path);
#endif
TexturePack_ExtractCurrent(true); TexturePack_ExtractCurrent(true);
} }
@ -1595,9 +1593,6 @@ static void TexturePackScreen_UploadFunc(void* s, void* w) {
static const char* const filters[] = { ".zip", NULL }; static const char* const filters[] = { ".zip", NULL };
Window_OpenFileDialog(filters, TexturePackScreen_UploadCallback); Window_OpenFileDialog(filters, TexturePackScreen_UploadCallback);
} }
#else
#define TexturePackScreen_UploadFunc NULL
#endif
void TexturePackScreen_Show(void) { void TexturePackScreen_Show(void) {
struct ListScreen* s = &ListScreen; struct ListScreen* s = &ListScreen;

View File

@ -280,15 +280,16 @@ static void UpdateCache(struct HttpRequest* req) {
/*########################################################################################################################* /*########################################################################################################################*
*-------------------------------------------------------TexturePack-------------------------------------------------------* *-------------------------------------------------------TexturePack-------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static char defTexPackBuffer[STRING_SIZE];
static char textureUrlBuffer[STRING_SIZE]; static char textureUrlBuffer[STRING_SIZE];
static cc_string texpackPath = String_FromArray(defTexPackBuffer); static char texpackPathBuffer[FILENAME_SIZE];
cc_string TexturePack_Url = String_FromArray(textureUrlBuffer);
cc_string TexturePack_Url = String_FromArray(textureUrlBuffer);
cc_string TexturePack_Path = String_FromArray(texpackPathBuffer);
static const cc_string defaultPath = String_FromConst("texpacks/default.zip"); static const cc_string defaultPath = String_FromConst("texpacks/default.zip");
void TexturePack_SetDefault(const cc_string* texPack) { void TexturePack_SetDefault(const cc_string* texPack) {
texpackPath.length = 0; TexturePack_Path.length = 0;
String_Format1(&texpackPath, "texpacks/%s", texPack); String_Format1(&TexturePack_Path, "texpacks/%s", texPack);
Options_Set(OPT_DEFAULT_TEX_PACK, 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) { 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); cc_result res = ExtractFromFile(&defaultPath);
/* override default.zip with user's default texture pack */ /* 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.ContextLost, NULL, OnContextLost);
Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
texpackPath.length = 0; TexturePack_Path.length = 0;
if (Options_UNSAFE_Get(OPT_DEFAULT_TEX_PACK, &file)) { if (Options_UNSAFE_Get(OPT_DEFAULT_TEX_PACK, &file)) {
String_Format1(&texpackPath, "texpacks/%s", &file); String_Format1(&TexturePack_Path, "texpacks/%s", &file);
} else { } else {
String_AppendString(&texpackPath, &defaultPath); String_AppendString(&TexturePack_Path, &defaultPath);
} }
Utils_EnsureDirectory("texpacks"); Utils_EnsureDirectory("texpacks");

View File

@ -51,7 +51,10 @@ CC_VAR extern struct _Atlas1DData {
GfxResourceID TexIds[ATLAS1D_MAX_ATLASES]; GfxResourceID TexIds[ATLAS1D_MAX_ATLASES];
} Atlas1D; } Atlas1D;
/* URL of the current custom texture pack, can be empty */
extern cc_string TexturePack_Url; 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_TileX(texLoc) ((texLoc) & ATLAS2D_MASK) /* texLoc % ATLAS2D_TILES_PER_ROW */
#define Atlas2D_TileY(texLoc) ((texLoc) >> ATLAS2D_SHIFT) /* texLoc / ATLAS2D_TILES_PER_ROW */ #define Atlas2D_TileY(texLoc) ((texLoc) >> ATLAS2D_SHIFT) /* texLoc / ATLAS2D_TILES_PER_ROW */