From fb5938c3c24be4ef71bedacfe9b1aef48f2380f7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 7 Dec 2020 22:32:30 +1100 Subject: [PATCH] Webclient: Add support for uploading texture packs --- src/Menus.c | 34 +++++++++++++++++++++++++++++++--- src/TexturePack.c | 8 +------- src/TexturePack.h | 7 +++++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index 187349dac..31fa79ba2 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1526,20 +1526,49 @@ 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); } static void TexturePackScreen_LoadEntries(struct ListScreen* s) { - static const cc_string path = String_FromConst("texpacks"); + static const cc_string path = String_FromConst(TEXPACKS_DIR); Directory_Enum(&path, &s->entries, TexturePackScreen_FilterFiles); ListScreen_Sort(s); } +#ifdef CC_BUILD_WEB +#include +static void TexturePackScreen_UploadCallback(const cc_string* path) { + char str[NATIVE_STR_LEN]; + Platform_EncodeString(str, path); + + /* Move from temp into texpacks folder */ + /* TODO: This is pretty awful and should be rewritten */ + EM_ASM_({ + var name = UTF8ToString($0);; + var data = FS.readFile(name); + FS.writeFile('/texpacks/' + name.substring(1), data); + }, str); + TexturePackScreen_Show(); + TexturePack_SetDefault(path); + TexturePack_ExtractCurrent(true); +} +#else +static void TexturePackScreen_UploadCallback(const cc_string* path) { /* TODO implement */ } +#endif +static void TexturePackScreen_UploadFunc(void* s, void* w) { + Window_OpenFileDialog(".zip", TexturePackScreen_UploadCallback); +} + void TexturePackScreen_Show(void) { struct ListScreen* s = &ListScreen; s->titleText = "Select a texture pack"; - s->UploadClick = NULL; + s->UploadClick = TexturePackScreen_UploadFunc; s->LoadEntries = TexturePackScreen_LoadEntries; s->EntryClick = TexturePackScreen_EntryClick; s->DoneClick = Menu_SwitchPause; @@ -1701,7 +1730,6 @@ static void LoadLevelScreen_LoadEntries(struct ListScreen* s) { } static void LoadLevelScreen_UploadCallback(const cc_string* path) { Map_LoadFrom(path); } - static void LoadLevelScreen_UploadFunc(void* s, void* w) { Window_OpenFileDialog(".cw", LoadLevelScreen_UploadCallback); } diff --git a/src/TexturePack.c b/src/TexturePack.c index c7309d5c2..d9a19e1f6 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -338,13 +338,7 @@ static void ExtractFromFile(const cc_string* filename) { cc_result res; String_InitArray(path, pathBuffer); -#ifdef CC_BUILD_WEB - /* texpacks/default.zip must be read from memory */ - /* instead of the default filesystem */ - String_Format1(&path, "/texpacks/%s", filename); -#else - String_Format1(&path, "texpacks/%s", filename); -#endif + String_Format1(&path, TEXPACKS_DIR "/%s", filename); res = Stream_OpenFile(&stream, &path); if (res) { Logger_SysWarn2(res, "opening", &path); return; } diff --git a/src/TexturePack.h b/src/TexturePack.h index 24f5646f6..240c4843e 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -49,7 +49,14 @@ CC_VAR extern struct _Atlas1DData { /* Textures for each 1D atlas. Only Atlas1D_Count of these are valid. */ GfxResourceID TexIds[ATLAS1D_MAX_ATLASES]; } Atlas1D; + extern cc_string TexturePack_Url; +#ifdef CC_BUILD_WEB +/* texpacks must be read from memory instead of the normal filesystem */ +#define TEXPACKS_DIR "/texpacks" +#else +#define TEXPACKS_DIR "texpacks" +#endif #define Atlas2D_TileX(texLoc) ((texLoc) & ATLAS2D_MASK) /* texLoc % ATLAS2D_TILES_PER_ROW */ #define Atlas2D_TileY(texLoc) ((texLoc) >> ATLAS2D_SHIFT) /* texLoc / ATLAS2D_TILES_PER_ROW */