Ver código fonte

emscripten: Added formal Emscripten port.

This worked previously as a generic Unix platform, but this reduces some of
the nonsense, like having to specify argv[0] to PHYSFS_init, and lets you
force a storage directory, which works nicely in cooperation with SDL3's
SDL_EMSCRIPTEN_PERSISTENT_PATH feature.

Fixes #114.
Ryan C. Gordon 1 semana atrás
pai
commit
c6ef21af2f
3 arquivos alterados com 72 adições e 1 exclusões
  1. 8 1
      CMakeLists.txt
  2. 61 0
      src/physfs_platform_emscripten.c
  3. 3 0
      src/physfs_platforms.h

+ 8 - 1
CMakeLists.txt

@@ -86,6 +86,7 @@ set(PHYSFS_SRCS
     src/physfs_platform_cafe.c
     src/physfs_platform_ctr.c
     src/physfs_platform_dos.c
+    src/physfs_platform_emscripten.c
     src/physfs_platform_posix.c
     src/physfs_platform_unix.c
     src/physfs_platform_windows.c
@@ -192,6 +193,12 @@ if(NOT PHYSFS_ARCHIVE_POD)
     add_definitions(-DPHYSFS_SUPPORTS_POD=0)
 endif()
 
+if(EMSCRIPTEN)
+    set(PHYSFS_EMSCRIPTEN_STORAGE_PATH "" CACHE STRING "Specify a path for prefdir on Emscripten")
+    if(NOT PHYSFS_EMSCRIPTEN_STORAGE_PATH STREQUAL "")
+        add_definitions(-DPHYSFS_EMSCRIPTEN_STORAGE_PATH="${PHYSFS_EMSCRIPTEN_STORAGE_PATH}")
+    endif()
+endif()
 
 option(PHYSFS_BUILD_STATIC "Build static library" TRUE)
 if(PHYSFS_BUILD_STATIC)
@@ -220,7 +227,7 @@ if(PHYSFS_BUILD_STATIC)
     list(APPEND PHYSFS_INSTALL_TARGETS "physfs-static")
 endif()
 
-if (NOT DOS)
+if (NOT DOS AND NOT EMSCRIPTEN)
     option(PHYSFS_BUILD_SHARED "Build shared library" TRUE)
 endif()
 

+ 61 - 0
src/physfs_platform_emscripten.c

@@ -0,0 +1,61 @@
+/*
+ * Emscripten support routines for PhysicsFS.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+#define __PHYSICSFS_INTERNAL__
+#include "physfs_platforms.h"
+
+#ifdef PHYSFS_PLATFORM_EMSCRIPTEN
+
+#include "physfs_internal.h"
+
+int __PHYSFS_platformInit(const char *argv0)
+{
+    return 1;  /* always succeed. */
+} /* __PHYSFS_platformInit */
+
+
+void __PHYSFS_platformDeinit(void)
+{
+    /* no-op */
+} /* __PHYSFS_platformDeinit */
+
+
+void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
+{
+    /* never CD-ROMs on Emscripten. */
+} /* __PHYSFS_platformDetectAvailableCDs */
+
+
+char *__PHYSFS_platformCalcBaseDir(const char *argv0)
+{
+    return __PHYSFS_strdup("/");
+} /* __PHYSFS_platformCalcBaseDir */
+
+
+char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
+{
+    #ifndef PHYSFS_EMSCRIPTEN_STORAGE_PATH
+    /* historically, this is where Emscripten chose by default, since it was
+       using the Unix codepath, so we'll keep it. */
+    #define PHYSFS_EMSCRIPTEN_STORAGE_PATH "/home/web_user/.local/share"
+    #endif
+
+    const char *deflt = PHYSFS_EMSCRIPTEN_STORAGE_PATH;
+    const char *envr = getenv("XDG_DATA_HOME"); /* check this just in case the app overrode it when we used the Unix code... */
+    const char *base = envr ? envr : deflt;
+    const size_t len = strlen(base) + strlen(app) + 3;
+    char *retval = (char *) allocator.Malloc(len);
+    BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
+    snprintf(retval, len, "%s/%s/", base, app);
+    return retval;
+} /* __PHYSFS_platformCalcPrefDir */
+
+#endif /* PHYSFS_PLATFORM_EMSCRIPTEN */
+
+/* end of physfs_platform_emscripten.c ... */
+

+ 3 - 0
src/physfs_platforms.h

@@ -15,6 +15,9 @@
 #if defined(TARGET_EXTENSION) && (defined(TARGET_PLAYDATE) || defined(TARGET_SIMULATOR))
 #  define PHYSFS_PLATFORM_PLAYDATE 1
 #  define PHYSFS_NO_CRUNTIME_MALLOC 1
+#elif (defined __EMSCRIPTEN__)
+#  define PHYSFS_PLATFORM_EMSCRIPTEN 1
+#  define PHYSFS_PLATFORM_POSIX 1
 #elif (defined DJGPP)
 #  define PHYSFS_PLATFORM_DOS 1
 #  define PHYSFS_PLATFORM_POSIX 1  /* close enough with djgpp */