diff --git a/src/Formats.c b/src/Formats.c index 036a4f4d6..a789b15db 100644 --- a/src/Formats.c +++ b/src/Formats.c @@ -1149,7 +1149,9 @@ ReturnCode Cw_Save(struct Stream* stream) { if ((res = Stream_Write(stream, tmp, sizeof(cw_meta_cpe) + len))) return res; if ((res = Stream_Write(stream, cw_meta_defs, sizeof(cw_meta_defs)))) return res; - for (b = 1; b < BLOCK_COUNT; b++) { + /* Write block definitions in reverse order so that software that only reads byte 'ID' */ + /* still loads correct first 256 block defs when saving a map with over 256 block defs */ + for (b = BLOCK_MAX_DEFINED; b >= 1; b--) { if (!Block_IsCustomDefined(b)) continue; if ((res = Cw_WriteBockDef(stream, b))) return res; } diff --git a/src/Platform.c b/src/Platform.c index 1fa359f6d..8fcfcbf36 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -593,7 +593,13 @@ ReturnCode File_Write(FileHandle file, const uint8_t* buffer, uint32_t count, ui } ReturnCode File_Close(FileHandle file) { +#ifndef CC_BUILD_WEB return close(file) == -1 ? errno : 0; +#else + int ret = close(file) == -1 ? errno : 0; + EM_ASM( FS.syncfs(false, function(err) { if (err) console.log(err); }); ); + return ret; +#endif } ReturnCode File_Seek(FileHandle file, int offset, int seekType) { @@ -1373,9 +1379,24 @@ ReturnCode Socket_Connect(SocketHandle socket, const String* ip, int port) { } ReturnCode Socket_Read(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified) { +#ifdef CC_BUILD_WEB + /* recv only reads one WebSocket frame at most, hence call it multiple times */ + int recvCount = 0, pending; + *modified = 0; + + while (count && !Socket_Available(socket, &pending) && pending) { + recvCount = recv(socket, buffer, count, 0); + if (recvCount == -1) return Socket__Error(); + + *modified += recvCount; + buffer += recvCount; count -= recvCount; + } + return 0; +#else int recvCount = recv(socket, buffer, count, 0); if (recvCount != -1) { *modified = recvCount; return 0; } *modified = 0; return Socket__Error(); +#endif } ReturnCode Socket_Write(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified) { @@ -1929,5 +1950,11 @@ void Platform_Init(void) { TransformProcessType(&psn, kProcessTransformToForegroundApplication); } #elif defined CC_BUILD_WEB -void Platform_Init(void) { } +void Platform_Init(void) { + EM_ASM( + FS.mkdir('/classicube'); + FS.mount(IDBFS, {}, '/classicube'); + FS.syncfs(true, function(err) { if (err) console.log(err); }); + ); +} #endif diff --git a/src/Program.c b/src/Program.c index 3f2c5cfa7..adcf075de 100644 --- a/src/Program.c +++ b/src/Program.c @@ -44,10 +44,12 @@ static void Program_RunGame(void) { String title; char titleBuffer[STRING_SIZE]; int width, height; +#ifndef CC_BUILD_WEB if (!File_Exists(&defPath)) { Window_ShowDialog("Missing file", "default.zip is missing, try downloading resources first.\n\nThe game will still run, but without any textures"); } +#endif width = Options_GetInt(OPT_WINDOW_WIDTH, 0, Display_Bounds.Width, 0); height = Options_GetInt(OPT_WINDOW_HEIGHT, 0, Display_Bounds.Height, 0); @@ -65,12 +67,16 @@ static void Program_RunGame(void) { /* Attempts to set current/working directory to the directory exe file is in */ static void Program_SetCurrentDirectory(void) { -#ifndef CC_BUILD_WEB String path; char pathBuffer[FILENAME_SIZE]; int i; ReturnCode res; String_InitArray(path, pathBuffer); +#ifdef CC_BUILD_WEB + String_AppendConst(&path, "/classicube"); + res = Platform_SetCurrentDirectory(&path); + if (res) { Logger_Warn(res, "setting current directory"); return; } +#else res = Process_GetExePath(&path); if (res) { Logger_Warn(res, "getting exe path"); return; } @@ -134,9 +140,9 @@ int main(int argc, char** argv) { argsCount = Platform_GetCommandLineArgs(argc, argv, args); /* NOTE: Make sure to comment this out before pushing a commit */ - /* String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); */ + String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); /* String rawArgs = String_FromConst("UnknownShadow200"); */ - /* argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); */ + argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); if (argsCount == 0) { #ifdef CC_BUILD_WEB diff --git a/src/TexturePack.c b/src/TexturePack.c index c74f67f64..c99f0a03c 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -640,6 +640,15 @@ void TexturePack_ExtractZip_File(const String* filename) { struct Stream stream; ReturnCode res; + /* TODO: This is an ugly hack to load textures from memory. */ + /* We need to mount /classicube to IndexedDB, but texpacks folder */ + /* should be read from memory. I don't know how to do that, */ + /* since mounting /classicube/texpacks to memory doesn't work.. */ +#ifdef CC_BUILD_WEB + String memPath = String_FromConst("/"); + Platform_SetCurrentDirectory(&memPath); +#endif + String_InitArray(path, pathBuffer); String_Format1(&path, "texpacks/%s", filename); @@ -651,6 +660,11 @@ void TexturePack_ExtractZip_File(const String* filename) { res = stream.Close(&stream); if (res) { Logger_Warn2(res, "closing", &path); } + +#ifdef CC_BUILD_WEB + String idbPath = String_FromConst("/classicube"); + Platform_SetCurrentDirectory(&idbPath); +#endif } ReturnCode TexturePack_ExtractTerrainPng(struct Stream* stream) { diff --git a/src/Window.c b/src/Window.c index a14424ed7..f1bb9340e 100644 --- a/src/Window.c +++ b/src/Window.c @@ -2573,7 +2573,9 @@ static void Window_CorrectFocus(void) { } static EM_BOOL Window_MouseWheel(int type, const EmscriptenWheelEvent* ev, void* data) { - Mouse_SetWheel(Mouse_Wheel - ev->deltaY); + /* On my laptop, wheel movement of '1' is reported as '6.666666507' */ + /* TODO: Verify this is the case on other platforms */ + Mouse_SetWheel(Mouse_Wheel - ev->deltaY / 6.666666507f); Window_CorrectFocus(); return true; }