diff --git a/doc/BUILD.md b/doc/BUILD.md index 4127a5e8..b8d93f4e 100644 --- a/doc/BUILD.md +++ b/doc/BUILD.md @@ -362,6 +362,31 @@ For integration and "API" usage, easiest would be checking how `vgmstream_cli.c` A cleaner API/.h and build methods is planned for the future (low priority though). +# emscripten / wasm +It's possible to build vgmstream components with emscripten (in-browser support). + +Follow emscripten's installation instructions: +- https://emscripten.org/docs/getting_started/downloads.html +- https://emscripten.org/docs/compiling/Building-Projects.html#building-projects + +Then should be buildable on Linux (Windows should be possible too but has some issues at the moment), for example: +``` +git clone https://github.com/vgmstream/vgmstream +cd vgmstream +mkdir -p build && cd build + +# quickest example, some can be enabled +emcmake cmake -DBUILD_STATIC=ON -DUSE_JANSSON=OFF -DUSE_FFMPEG=OFF -DUSE_VORBIS=OFF -DUSE_MPEG=OFF -DUSE_G7221=OFF -DUSE_G719=OFF -DUSE_ATRAC9=OFF -DUSE_SPEEX=OFF -DUSE_MPEG=OFF -S .. -B . +emmake make +``` +Or with the base makefiles (may need to rename output to .js ATM): +``` +git clone https://github.com/vgmstream/vgmstream +cd vgmstream +make vgmstream-cli CC=emcc AR=emar strip=echo +``` + + ## External libraries Support for some codecs is done with external libs, instead of copying their code in vgmstream. There are various reasons for this: - each lib may have complex or conflicting ways to compile that aren't simple to replicate diff --git a/src/streamfile.c b/src/streamfile.c index 18586cc5..c3e4f90f 100644 --- a/src/streamfile.c +++ b/src/streamfile.c @@ -7,6 +7,18 @@ #include #endif +/* For (rarely needed) +2GB file support we use fseek64/ftell64. Those are usually available + * but may depend on compiler. + * - MSVC: +VS2008 should work + * - GCC/MingW: should be available + * - GCC/Linux: should be available but some systems may need __USE_FILE_OFFSET64, + * that we (probably) don't want since that turns off_t to off64_t + * - Clang: seems only defined on Linux/GNU environments, somehow emscripten is out + * (unsure about Clang Win since apparently they define _MSC_VER) + * - Android: API +24 if not using __USE_FILE_OFFSET64 + * Not sure if fopen64 is needed in some cases. May be work adding some compiler flag to control this manually. + */ + /* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */ #if defined(__MSVCRT__) || defined(_MSC_VER) #include @@ -20,8 +32,14 @@ #endif */ - #define fseek_v _fseeki64 //fseek/fseeko - #define ftell_v _ftelli64 //ftell/ftello + #define fopen_v fopen + #if (_MSC_VER >= 1400) + #define fseek_v _fseeki64 + #define ftell_v _ftelli64 + #else + #define fseek_v fseek + #define ftell_v ftell + #endif #ifdef fileno #undef fileno @@ -34,10 +52,13 @@ #define off_t __int64 #endif -#elif defined(XBMC) +#elif defined(XBMC) || defined(__EMSCRIPTEN__) || defined (__ANDROID__) + #define fopen_v fopen #define fseek_v fseek #define ftell_v ftell + #else + #define fopen_v fopen #define fseek_v fseeko64 //fseeko #define ftell_v ftello64 //ftello #endif @@ -252,7 +273,7 @@ static STREAMFILE* open_stdio_streamfile_buffer(const char* const filename, size FILE* infile = NULL; STREAMFILE* sf = NULL; - infile = fopen(filename,"rb"); + infile = fopen_v(filename,"rb"); if (!infile) { /* allow non-existing files in some cases */ if (!vgmstream_is_virtual_filename(filename)) @@ -1471,7 +1492,7 @@ void dump_streamfile(STREAMFILE* sf, int num) { get_streamfile_filename(sf, filename, sizeof(filename)); snprintf(dumpname, sizeof(dumpname), "%s_%02i.dump", filename, num); - f = fopen(dumpname,"wb"); + f = fopen_v(dumpname,"wb"); if (!f) return; }