Webclient: Add support for uploading texture packs

This commit is contained in:
UnknownShadow200 2020-12-07 22:32:30 +11:00
parent 3811e25314
commit fb5938c3c2
3 changed files with 39 additions and 10 deletions

View File

@ -1526,20 +1526,49 @@ 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);
} }
static void TexturePackScreen_LoadEntries(struct ListScreen* s) { 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); Directory_Enum(&path, &s->entries, TexturePackScreen_FilterFiles);
ListScreen_Sort(s); ListScreen_Sort(s);
} }
#ifdef CC_BUILD_WEB
#include <emscripten.h>
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) { void TexturePackScreen_Show(void) {
struct ListScreen* s = &ListScreen; struct ListScreen* s = &ListScreen;
s->titleText = "Select a texture pack"; s->titleText = "Select a texture pack";
s->UploadClick = NULL; s->UploadClick = TexturePackScreen_UploadFunc;
s->LoadEntries = TexturePackScreen_LoadEntries; s->LoadEntries = TexturePackScreen_LoadEntries;
s->EntryClick = TexturePackScreen_EntryClick; s->EntryClick = TexturePackScreen_EntryClick;
s->DoneClick = Menu_SwitchPause; 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_UploadCallback(const cc_string* path) { Map_LoadFrom(path); }
static void LoadLevelScreen_UploadFunc(void* s, void* w) { static void LoadLevelScreen_UploadFunc(void* s, void* w) {
Window_OpenFileDialog(".cw", LoadLevelScreen_UploadCallback); Window_OpenFileDialog(".cw", LoadLevelScreen_UploadCallback);
} }

View File

@ -338,13 +338,7 @@ static void ExtractFromFile(const cc_string* filename) {
cc_result res; cc_result res;
String_InitArray(path, pathBuffer); String_InitArray(path, pathBuffer);
#ifdef CC_BUILD_WEB String_Format1(&path, TEXPACKS_DIR "/%s", filename);
/* 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
res = Stream_OpenFile(&stream, &path); res = Stream_OpenFile(&stream, &path);
if (res) { Logger_SysWarn2(res, "opening", &path); return; } if (res) { Logger_SysWarn2(res, "opening", &path); return; }

View File

@ -49,7 +49,14 @@ CC_VAR extern struct _Atlas1DData {
/* Textures for each 1D atlas. Only Atlas1D_Count of these are valid. */ /* Textures for each 1D atlas. Only Atlas1D_Count of these are valid. */
GfxResourceID TexIds[ATLAS1D_MAX_ATLASES]; GfxResourceID TexIds[ATLAS1D_MAX_ATLASES];
} Atlas1D; } Atlas1D;
extern cc_string TexturePack_Url; 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_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 */