From 85f1b4c5c5da766cd494eb23a62040af7a10d4d9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 9 Jul 2025 18:56:08 +1000 Subject: [PATCH] Move MSDOS and webclient backend files out --- .github/workflows/build_webclient.yml | 7 ++-- Makefile | 12 ++++-- readme.md | 4 +- src/{ => msdos}/Platform_MSDOS.c | 24 ++++++------ src/{ => msdos}/Window_MSDOS.c | 16 ++++---- src/{ => webclient}/Audio_Web.c | 8 +--- src/{ => webclient}/Http_Web.c | 28 +++++++------- src/{ => webclient}/Platform_Web.c | 53 +++++++++++++-------------- src/{ => webclient}/Window_Web.c | 24 ++++++------ src/{ => webclient}/interop_web.js | 0 10 files changed, 85 insertions(+), 91 deletions(-) rename src/{ => msdos}/Platform_MSDOS.c (95%) rename src/{ => msdos}/Window_MSDOS.c (94%) rename src/{ => webclient}/Audio_Web.c (96%) rename src/{ => webclient}/Http_Web.c (85%) rename src/{ => webclient}/Platform_Web.c (90%) rename src/{ => webclient}/Window_Web.c (96%) rename src/{ => webclient}/interop_web.js (100%) diff --git a/.github/workflows/build_webclient.yml b/.github/workflows/build_webclient.yml index 9f5d10a04..01f2746dd 100644 --- a/.github/workflows/build_webclient.yml +++ b/.github/workflows/build_webclient.yml @@ -21,9 +21,10 @@ jobs: - uses: actions/checkout@v4 - name: Compiles webclient id: compile + env: + SRCS: "src/*.c src/webclient/*.c" run: | - cd src - emcc *.c -o ClassiCube.js -s WASM=0 -s NO_EXIT_RUNTIME=1 -s LEGACY_VM_SUPPORT=1 -s ALLOW_MEMORY_GROWTH=1 -s ABORTING_MALLOC=0 -s ENVIRONMENT=web -s TOTAL_STACK=256Kb --js-library interop_web.js -Os -g2 -s SINGLE_FILE + emcc ${{ env.SRCS }} -o ClassiCube.js -s WASM=0 -s NO_EXIT_RUNTIME=1 -s LEGACY_VM_SUPPORT=1 -s ALLOW_MEMORY_GROWTH=1 -s ABORTING_MALLOC=0 -s ENVIRONMENT=web -s TOTAL_STACK=256Kb --js-library src/webclient/interop_web.js -Os -g2 -s SINGLE_FILE sed -i 's#eventHandler.useCapture);#{ useCapture: eventHandler.useCapture, passive: false });#g' ClassiCube.js - uses: ./.github/actions/notify_failure @@ -35,7 +36,7 @@ jobs: - uses: ./.github/actions/upload_build if: ${{ always() && steps.compile.outcome == 'success' }} with: - SOURCE_FILE: 'src/ClassiCube.js' + SOURCE_FILE: 'ClassiCube.js' DEST_NAME: 'ClassiCube.js' diff --git a/Makefile b/Makefile index d464b72b6..e48ba190a 100644 --- a/Makefile +++ b/Makefile @@ -59,9 +59,12 @@ ifeq ($(PLAT),web) CC = emcc OEXT = .html CFLAGS = -g - LDFLAGS = -g -s WASM=1 -s NO_EXIT_RUNTIME=1 -s ABORTING_MALLOC=0 -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=256Kb --js-library $(SOURCE_DIR)/interop_web.js + LDFLAGS = -g -s WASM=1 -s NO_EXIT_RUNTIME=1 -s ABORTING_MALLOC=0 -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=256Kb --js-library $(SOURCE_DIR)/webclient/interop_web.js BUILD_DIR = build/web BEARSSL = 0 + + BUILD_DIRS += $(BUILD_DIR)/src/webclient + C_SOURCES += $(wildcard src/webclient/*.c) endif ifeq ($(PLAT),mingw) @@ -176,6 +179,9 @@ ifeq ($(PLAT),dos) OEXT = .exe BUILD_DIR = build/dos BEARSSL = 0 + + BUILD_DIRS += $(BUILD_DIR)/src/msdos + C_SOURCES += $(wildcard src/msdos/*.c) endif @@ -194,9 +200,7 @@ endif ifeq ($(BEARSSL),1) BUILD_DIRS += $(BUILD_DIR)/third_party/bearssl - BEARSSL_SOURCES = $(wildcard third_party/bearssl/*.c) - BEARSSL_OBJECTS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(BEARSSL_SOURCES)) - OBJECTS += $(BEARSSL_OBJECTS) + C_SOURCES += $(wildcard third_party/bearssl/*.c) endif ifdef RELEASE diff --git a/readme.md b/readme.md index a12917b7b..cecc55d86 100644 --- a/readme.md +++ b/readme.md @@ -206,8 +206,8 @@ NOTE: If you are distributing a modified version, **please change the bundle ID 1. Install emscripten if necessary. 2. Run either: - * `make web` or - * `emcc src/*.c -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=1Mb --js-library interop_web.js` + * `make web` - produces simple non-optimised output, easier to debug + * `make web RELEASE=1` - produces optimised output, harder to debug The generated javascript file has some issues. [See here for how to fix](doc/compile-fixes.md#webclient-patches) diff --git a/src/Platform_MSDOS.c b/src/msdos/Platform_MSDOS.c similarity index 95% rename from src/Platform_MSDOS.c rename to src/msdos/Platform_MSDOS.c index 4c8fd086c..56fc5ba13 100644 --- a/src/Platform_MSDOS.c +++ b/src/msdos/Platform_MSDOS.c @@ -1,15 +1,13 @@ -#include "Core.h" -#if defined CC_BUILD_MSDOS +#include "../_PlatformBase.h" +#include "../Stream.h" +#include "../ExtMath.h" +#include "../SystemFonts.h" +#include "../Funcs.h" +#include "../Window.h" +#include "../Utils.h" +#include "../Errors.h" +#include "../PackedCol.h" -#include "_PlatformBase.h" -#include "Stream.h" -#include "ExtMath.h" -#include "SystemFonts.h" -#include "Funcs.h" -#include "Window.h" -#include "Utils.h" -#include "Errors.h" -#include "PackedCol.h" #include #include #include @@ -39,7 +37,7 @@ cc_bool Platform_ReadonlyFilesystem; /*########################################################################################################################* *-----------------------------------------------------Main entrypoint-----------------------------------------------------* *#########################################################################################################################*/ -#include "main_impl.h" +#include "../main_impl.h" int main(int argc, char** argv) { cc_result res; @@ -450,4 +448,4 @@ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* arg cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { return 0; } -#endif + diff --git a/src/Window_MSDOS.c b/src/msdos/Window_MSDOS.c similarity index 94% rename from src/Window_MSDOS.c rename to src/msdos/Window_MSDOS.c index a17df4a0a..013f0a05c 100644 --- a/src/Window_MSDOS.c +++ b/src/msdos/Window_MSDOS.c @@ -1,12 +1,10 @@ -#include "Core.h" -#if defined CC_BUILD_MSDOS +#include "../_WindowBase.h" +#include "../String.h" +#include "../Funcs.h" +#include "../Bitmap.h" +#include "../Options.h" +#include "../Errors.h" -#include "_WindowBase.h" -#include "String.h" -#include "Funcs.h" -#include "Bitmap.h" -#include "Options.h" -#include "Errors.h" #include #include #include @@ -347,4 +345,4 @@ void Window_UpdateRawMouse(void) { void Window_DisableRawMouse(void) { DefaultDisableRawMouse(); } -#endif + diff --git a/src/Audio_Web.c b/src/webclient/Audio_Web.c similarity index 96% rename from src/Audio_Web.c rename to src/webclient/Audio_Web.c index 38dbe87b3..a7c1007b0 100644 --- a/src/Audio_Web.c +++ b/src/webclient/Audio_Web.c @@ -1,12 +1,9 @@ -#include "Core.h" - -#if defined CC_BUILD_WEBAUDIO struct AudioContext { int contextID, count; void* data; }; #define AUDIO_OVERRIDE_SOUNDS #define AUDIO_OVERRIDE_ALLOC -#include "_AudioBase.h" -#include "Funcs.h" +#include "../_AudioBase.h" +#include "../Funcs.h" extern int interop_InitAudio(void); extern int interop_AudioCreate(void); @@ -140,5 +137,4 @@ void AudioBackend_LoadSounds(void) { } } } -#endif diff --git a/src/Http_Web.c b/src/webclient/Http_Web.c similarity index 85% rename from src/Http_Web.c rename to src/webclient/Http_Web.c index c6374acec..6e15a58c1 100644 --- a/src/Http_Web.c +++ b/src/webclient/Http_Web.c @@ -1,10 +1,10 @@ -#include "Core.h" -#ifdef CC_BUILD_WEB -#include "_HttpBase.h" +#include "../_HttpBase.h" +#include "../Errors.h" #include -#include "Errors.h" + extern int interop_DownloadAsync(const char* url, int method, int reqID); extern int interop_IsHttpsOnly(void); + static struct RequestList workingReqs, queuedReqs; static cc_uint64 startTime; @@ -21,7 +21,7 @@ cc_bool Http_GetResult(int reqID, struct HttpRequest* item) { } cc_bool Http_GetCurrent(int* reqID, int* progress) { - /* TODO: Stubbed as this isn't required at the moment */ + // TODO: Stubbed as this isn't required at the moment *progress = 0; return 0; } @@ -59,7 +59,7 @@ static void Http_StartNextDownload(void) { struct HttpRequest* req; cc_result res; - /* Avoid making too many requests at once */ + // Avoid making too many requests at once if (workingReqs.count >= HTTP_MAX_CONCURRENCY) return; if (!queuedReqs.count) return; String_InitArray(url, urlBuffer); @@ -72,11 +72,11 @@ static void Http_StartNextDownload(void) { res = interop_DownloadAsync(urlStr, req->requestType, req->id); if (res) { - /* interop error code -> ClassiCube error code */ + // interop error code -> ClassiCube error code if (res == 1) res = ERR_INVALID_DATA_URL; req->result = res; - /* Invalid URL so move onto next request */ + // Invalid URL so move onto next request Http_FinishRequest(req); RequestList_RemoveAt(&queuedReqs, 0); Http_StartNextDownload(); @@ -98,7 +98,7 @@ EMSCRIPTEN_KEEPALIVE void Http_OnFinishedAsync(int reqID, void* data, int len, i int idx = RequestList_Find(&workingReqs, reqID); if (idx == -1) { - /* Shouldn't ever happen, but log a warning anyways */ + // Shouldn't ever happen, but log a warning anyways Mem_Free(data); Platform_Log1("Ignoring invalid request (%i)", &reqID); } else { @@ -108,7 +108,7 @@ EMSCRIPTEN_KEEPALIVE void Http_OnFinishedAsync(int reqID, void* data, int len, i req->statusCode = status; req->contentLength = len; - /* Usually this happens when denied by CORS */ + // Usually this happens when denied by CORS if (!status && !data) req->result = ERR_DOWNLOAD_INVALID; if (req->data) Platform_Log1("HTTP returned data: %i bytes", &req->size); @@ -118,9 +118,9 @@ EMSCRIPTEN_KEEPALIVE void Http_OnFinishedAsync(int reqID, void* data, int len, i Http_StartNextDownload(); } -/* Adds a req to the list of pending requests, waking up worker thread if needed */ +// Adds a req to the list of pending requests, waking up worker thread if needed static void HttpBackend_Add(struct HttpRequest* req, cc_uint8 flags) { - /* Add time based query string parameter to bypass browser cache */ + // Add time based query string parameter to bypass browser cache if (flags & HTTP_FLAG_NOCACHE) { cc_string url = String_FromRawArray(req->url); int lo = (int)(startTime), hi = (int)(startTime >> 32); @@ -137,7 +137,7 @@ static void HttpBackend_Add(struct HttpRequest* req, cc_uint8 flags) { *#########################################################################################################################*/ static void Http_Init(void) { Http_InitCommon(); - /* If this webpage is https://, browsers deny any http:// downloading */ + // If this webpage is https://, browsers deny any http:// downloading httpsOnly = interop_IsHttpsOnly(); startTime = DateTime_CurrentUTC(); @@ -145,4 +145,4 @@ static void Http_Init(void) { RequestList_Init(&workingReqs); RequestList_Init(&processedReqs); } -#endif + diff --git a/src/Platform_Web.c b/src/webclient/Platform_Web.c similarity index 90% rename from src/Platform_Web.c rename to src/webclient/Platform_Web.c index 7bd9509cf..a0b3b9928 100644 --- a/src/Platform_Web.c +++ b/src/webclient/Platform_Web.c @@ -1,14 +1,11 @@ -#include "Core.h" -#if defined CC_BUILD_WEB - -#include "_PlatformBase.h" -#include "Stream.h" -#include "ExtMath.h" -#include "SystemFonts.h" -#include "Funcs.h" -#include "Window.h" -#include "Utils.h" -#include "Errors.h" +#include "../_PlatformBase.h" +#include "../Stream.h" +#include "../ExtMath.h" +#include "../SystemFonts.h" +#include "../Funcs.h" +#include "../Window.h" +#include "../Utils.h" +#include "../Errors.h" #include #include @@ -22,16 +19,16 @@ #define O_EXCL 0x080 #define O_TRUNC 0x200 -/* Unfortunately, errno constants are different in some older emscripten versions */ -/* (linux errno numbers compared to WASI errno numbers) */ -/* So just use the same errono numbers as interop_web.js */ +// Unfortunately, errno constants are different in some older emscripten versions +// (linux errno numbers compared to WASI errno numbers) +// So just use the same errono numbers as interop_web.js #define _ENOENT 2 -#define _EAGAIN 6 /* same as EWOULDBLOCK */ +#define _EAGAIN 6 // same as EWOULDBLOCK #define _EEXIST 17 #define _EHOSTUNREACH 23 #define _EINPROGRESS 26 -const cc_result ReturnCode_FileShareViolation = 1000000000; /* Not used in web filesystem backend */ +const cc_result ReturnCode_FileShareViolation = 1000000000; // Not used in web filesystem backend const cc_result ReturnCode_FileNotFound = _ENOENT; const cc_result ReturnCode_SocketInProgess = _EINPROGRESS; const cc_result ReturnCode_SocketWouldBlock = _EAGAIN; @@ -424,18 +421,18 @@ cc_result Platform_GetEntropy(void* data, int len) { *#########################################################################################################################*/ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { int i, count; - argc--; argv++; /* skip executable path argument */ + argc--; argv++; // skip executable path argument // count = min(argc, GAME_MAX_CMDARGS); for (i = 0; i < count; i++) { args[i] = String_FromReadonly(argv[i]); } return count; } -#include "main_impl.h" +#include "../main_impl.h" static int _argc; static char** _argv; -/* webclient does some asynchronous initialisation first, then kickstarts the game after that */ +// webclient does some asynchronous initialisation first, then kickstarts the game after that static void web_main(void) { SetupProgram(_argc, _argv); @@ -462,11 +459,11 @@ extern void interop_AsyncDownloadTexturePack(const char* path); EMSCRIPTEN_KEEPALIVE int main(int argc, char** argv) { _argc = argc; _argv = argv; - /* Game loads resources asynchronously, then actually starts itself */ - /* main */ - /* > texture pack download (async) */ - /* > load indexedDB (async) */ - /* > web_main (game actually starts) */ + // Game loads resources asynchronously, then actually starts itself: + // main + // > texture pack download (async) + // > load indexedDB (async) + // > web_main (game actually starts) interop_FS_Init(); interop_DirectorySetWorking("/classicube"); interop_AsyncDownloadTexturePack("texpacks/default.zip"); @@ -475,12 +472,12 @@ EMSCRIPTEN_KEEPALIVE int main(int argc, char** argv) { extern void interop_LoadIndexedDB(void); extern void interop_AsyncLoadIndexedDB(void); -/* Asynchronous callback after texture pack is downloaded */ +// Asynchronous callback after texture pack is downloaded EMSCRIPTEN_KEEPALIVE void main_phase1(void) { - interop_LoadIndexedDB(); /* legacy compatibility */ + interop_LoadIndexedDB(); // legacy compatibility interop_AsyncLoadIndexedDB(); } -/* Asynchronous callback after IndexedDB is loaded */ +// Asynchronous callback after IndexedDB is loaded EMSCRIPTEN_KEEPALIVE void main_phase2(void) { web_main(); } -#endif + diff --git a/src/Window_Web.c b/src/webclient/Window_Web.c similarity index 96% rename from src/Window_Web.c rename to src/webclient/Window_Web.c index 19198253f..46f6c3b16 100644 --- a/src/Window_Web.c +++ b/src/webclient/Window_Web.c @@ -1,13 +1,13 @@ -#include "Core.h" -#if defined CC_BUILD_WEB && !defined CC_BUILD_SDL -#include "_WindowBase.h" -#include "Game.h" -#include "String.h" -#include "Funcs.h" -#include "ExtMath.h" -#include "Bitmap.h" -#include "Errors.h" -#include "Gui.h" +#include "../Core.h" +#include "../_WindowBase.h" +#include "../Game.h" +#include "../String.h" +#include "../Funcs.h" +#include "../ExtMath.h" +#include "../Bitmap.h" +#include "../Errors.h" +#include "../Gui.h" + #include #include #include @@ -754,7 +754,7 @@ void Window_DisableRawMouse(void) { *------------------------------------------------Emscripten WebGL context-------------------------------------------------* *#########################################################################################################################*/ #if CC_GFX_BACKEND_IS_GL() -#include "Graphics.h" +#include "../Graphics.h" static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx_handle; static EM_BOOL GLContext_OnLost(int eventType, const void *reserved, void *userData) { @@ -819,4 +819,4 @@ void GLContext_GetApiInfo(cc_string* info) { String_AppendUtf8(info, buffer, len); } #endif -#endif + diff --git a/src/interop_web.js b/src/webclient/interop_web.js similarity index 100% rename from src/interop_web.js rename to src/webclient/interop_web.js