2018-05-15 02:27:25 +02:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2018-05-15 02:27:25 +02:00
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
2024-05-16 23:30:41 +02:00
|
|
|
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
2018-05-15 02:27:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "io.h"
|
2024-05-17 04:41:28 +02:00
|
|
|
|
2018-05-15 02:27:25 +02:00
|
|
|
#include "assert.h"
|
2024-05-17 04:41:28 +02:00
|
|
|
#include "log.h"
|
2020-08-15 13:51:12 +02:00
|
|
|
#include "rwops/rwops_autobuf.h"
|
2024-05-17 04:41:28 +02:00
|
|
|
#include "stringops.h"
|
|
|
|
#include "vfs/public.h"
|
2018-05-15 02:27:25 +02:00
|
|
|
|
2022-07-12 00:31:47 +02:00
|
|
|
#ifdef TAISEI_BUILDCONF_HAVE_POSIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2020-08-15 13:51:12 +02:00
|
|
|
char *SDL_RWgets(SDL_RWops *rwops, char *buf, size_t bufsize) {
|
2018-05-15 02:27:25 +02:00
|
|
|
char c, *ptr = buf, *end = buf + bufsize - 1;
|
|
|
|
assert(end > ptr);
|
|
|
|
|
|
|
|
while((c = SDL_ReadU8(rwops)) && ptr <= end) {
|
|
|
|
if((*ptr++ = c) == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ptr == buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(ptr > end) {
|
|
|
|
*end = 0;
|
|
|
|
log_warn("Line too long (%zu bytes max): %s", bufsize, buf);
|
|
|
|
} else {
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-08-15 13:51:12 +02:00
|
|
|
char *SDL_RWgets_realloc(SDL_RWops *rwops, char **buf, size_t *bufsize) {
|
Emscripten compatibility (#161)
* Major refactoring of the main loop(s) and control flow (WIP)
run_at_fps() is gone 🦀
Instead of nested blocking event loops, there is now an eventloop API
that manages an explicit stack of scenes. This makes Taisei a lot more
portable to async environments where spinning a loop forever without
yielding control simply is not an option, and that is the entire point
of this change.
A prime example of such an environment is the Web (via emscripten).
Taisei was able to run there through a terrible hack: inserting
emscripten_sleep calls into the loop, which would yield to the browser.
This has several major drawbacks: first of all, every function that
could possibly call emscripten_sleep must be compiled into a special
kind of bytecode, which then has to be interpreted at runtime, *much*
slower than JITed WebAssembly. And that includes *everything* down the
call stack, too! For more information, see
https://emscripten.org/docs/porting/emterpreter.html
Even though that method worked well enough for experimenting, despite
suboptimal performance, there is another obvious drawback:
emscripten_sleep is implemented via setTimeout(), which can be very
imprecise and is generally not reliable for fluid animation. Browsers
actually have an API specifically for that use case:
window.requestAnimationFrame(), but Taisei's original blocking control
flow style is simply not compatible with it. Emscripten exposes this API
with its emscripten_set_main_loop(), which the eventloop backend now
uses on that platform.
Unfortunately, C is still C, with no fancy closures or coroutines.
With blocking calls into menu/scene loops gone, the control flow is
reimplemented via so-called (pun intended) "call chains". That is
basically an euphemism for callback hell. With manual memory management
and zero type-safety. Not that the menu system wasn't shitty enough
already. I'll just keep telling myself that this is all temporary and
will be replaced with scripts in v1.4.
* improve build system for emscripten + various fixes
* squish menu bugs
* improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS
Note that stock freetype does not work without
EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the
"emscripten" branch here:
https://github.com/taisei-project/freetype2/tree/emscripten
* Enable -Wcast-function-type
Calling functions through incompatible pointers is nasal demons and
doesn't work in WASM.
* webgl: workaround a crash on some browsers
* emscripten improvements:
* Persist state (config, progress, replays, ...) in local IndexDB
* Simpler HTML shell (temporary)
* Enable more optimizations
* fix build if validate_glsl=false
* emscripten: improve asset packaging, with local cache
Note that even though there are rules to build audio bundles, audio
does *not* work yet. It looks like SDL2_mixer can not work without
threads, which is a problem. Yet another reason to write an OpenAL
backend - emscripten supports that natively.
* emscripten: customize the html shell
* emscripten: force "show log" checkbox unchecked initially
* emscripten: remove quit shortcut from main menu (since there's no quit)
* emscripten: log area fixes
* emscripten/webgl: workaround for fullscreen viewport issue
* emscripten: implement frameskip
* emscripter: improve framerate limiter
* align List to at least 8 bytes (shut up warnings)
* fix non-emscripten builds
* improve fullscreen handling, mainly for emscripten
* Workaround to make audio work in chromium
emscripten-core/emscripten#6511
* emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
|
|
|
char c, *ptr = *buf, *end = *buf + *bufsize - 1;
|
|
|
|
assert(end >= ptr);
|
|
|
|
|
|
|
|
while((c = SDL_ReadU8(rwops))) {
|
|
|
|
*ptr++ = c;
|
|
|
|
|
|
|
|
if(ptr > end) {
|
|
|
|
ptrdiff_t ofs = ptr - *buf;
|
|
|
|
*bufsize *= 2;
|
2023-01-09 04:19:31 +01:00
|
|
|
*buf = mem_realloc(*buf, *bufsize);
|
Emscripten compatibility (#161)
* Major refactoring of the main loop(s) and control flow (WIP)
run_at_fps() is gone 🦀
Instead of nested blocking event loops, there is now an eventloop API
that manages an explicit stack of scenes. This makes Taisei a lot more
portable to async environments where spinning a loop forever without
yielding control simply is not an option, and that is the entire point
of this change.
A prime example of such an environment is the Web (via emscripten).
Taisei was able to run there through a terrible hack: inserting
emscripten_sleep calls into the loop, which would yield to the browser.
This has several major drawbacks: first of all, every function that
could possibly call emscripten_sleep must be compiled into a special
kind of bytecode, which then has to be interpreted at runtime, *much*
slower than JITed WebAssembly. And that includes *everything* down the
call stack, too! For more information, see
https://emscripten.org/docs/porting/emterpreter.html
Even though that method worked well enough for experimenting, despite
suboptimal performance, there is another obvious drawback:
emscripten_sleep is implemented via setTimeout(), which can be very
imprecise and is generally not reliable for fluid animation. Browsers
actually have an API specifically for that use case:
window.requestAnimationFrame(), but Taisei's original blocking control
flow style is simply not compatible with it. Emscripten exposes this API
with its emscripten_set_main_loop(), which the eventloop backend now
uses on that platform.
Unfortunately, C is still C, with no fancy closures or coroutines.
With blocking calls into menu/scene loops gone, the control flow is
reimplemented via so-called (pun intended) "call chains". That is
basically an euphemism for callback hell. With manual memory management
and zero type-safety. Not that the menu system wasn't shitty enough
already. I'll just keep telling myself that this is all temporary and
will be replaced with scripts in v1.4.
* improve build system for emscripten + various fixes
* squish menu bugs
* improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS
Note that stock freetype does not work without
EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the
"emscripten" branch here:
https://github.com/taisei-project/freetype2/tree/emscripten
* Enable -Wcast-function-type
Calling functions through incompatible pointers is nasal demons and
doesn't work in WASM.
* webgl: workaround a crash on some browsers
* emscripten improvements:
* Persist state (config, progress, replays, ...) in local IndexDB
* Simpler HTML shell (temporary)
* Enable more optimizations
* fix build if validate_glsl=false
* emscripten: improve asset packaging, with local cache
Note that even though there are rules to build audio bundles, audio
does *not* work yet. It looks like SDL2_mixer can not work without
threads, which is a problem. Yet another reason to write an OpenAL
backend - emscripten supports that natively.
* emscripten: customize the html shell
* emscripten: force "show log" checkbox unchecked initially
* emscripten: remove quit shortcut from main menu (since there's no quit)
* emscripten: log area fixes
* emscripten/webgl: workaround for fullscreen viewport issue
* emscripten: implement frameskip
* emscripter: improve framerate limiter
* align List to at least 8 bytes (shut up warnings)
* fix non-emscripten builds
* improve fullscreen handling, mainly for emscripten
* Workaround to make audio work in chromium
emscripten-core/emscripten#6511
* emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
|
|
|
end = *buf + *bufsize - 1;
|
|
|
|
ptr = *buf + ofs;
|
|
|
|
*end = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ptr == *buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(ptr <= end);
|
|
|
|
*ptr = 0;
|
|
|
|
|
|
|
|
return *buf;
|
|
|
|
}
|
|
|
|
|
2018-05-15 02:27:25 +02:00
|
|
|
size_t SDL_RWprintf(SDL_RWops *rwops, const char* fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
char *str = vstrfmt(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
size_t ret = SDL_RWwrite(rwops, str, 1, strlen(str));
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(str);
|
2018-05-15 02:27:25 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tsfprintf(FILE *out, const char *restrict fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(out, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2020-08-15 13:51:12 +02:00
|
|
|
char *try_path(const char *prefix, const char *name, const char *ext) {
|
2018-05-15 02:27:25 +02:00
|
|
|
char *p = strjoin(prefix, name, ext, NULL);
|
|
|
|
|
|
|
|
if(vfs_query(p).exists) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(p);
|
2018-05-15 02:27:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-08-15 13:51:12 +02:00
|
|
|
|
|
|
|
static void *SDL_RWreadAll_known_size(SDL_RWops *rwops, size_t file_size, size_t *out_size) {
|
2023-01-09 04:19:31 +01:00
|
|
|
char *start = mem_alloc(file_size);
|
2020-08-15 13:51:12 +02:00
|
|
|
char *end = start + file_size;
|
|
|
|
char *pbuf = start;
|
|
|
|
|
|
|
|
assert(end >= start);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
size_t read = SDL_RWread(rwops, pbuf, 1, end - pbuf);
|
|
|
|
assert(read <= end - pbuf);
|
|
|
|
|
|
|
|
if(read == 0) {
|
|
|
|
*out_size = pbuf - start;
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
pbuf += read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *SDL_RWreadAll(SDL_RWops *rwops, size_t *out_size, size_t max_size) {
|
|
|
|
ssize_t file_size = SDL_RWsize(rwops);
|
|
|
|
|
|
|
|
if(file_size >= 0) {
|
|
|
|
if(max_size && file_size > max_size) {
|
|
|
|
SDL_SetError("File is too large (%zu bytes; max is %zu)", file_size, max_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sz;
|
|
|
|
void *result = SDL_RWreadAll_known_size(rwops, file_size, &sz);
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
*out_size = sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t chunk_size = BUFSIZ;
|
|
|
|
|
|
|
|
void *buf;
|
|
|
|
SDL_RWops *autobuf = NOT_NULL(SDL_RWAutoBuffer(&buf, chunk_size));
|
2023-01-09 04:19:31 +01:00
|
|
|
void *chunk = mem_alloc(chunk_size);
|
2020-08-15 13:51:12 +02:00
|
|
|
|
|
|
|
size_t total_size = 0;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
size_t read = SDL_RWread(rwops, chunk, 1, chunk_size);
|
|
|
|
|
|
|
|
if(read == 0) {
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(chunk);
|
2020-08-15 13:51:12 +02:00
|
|
|
SDL_RWclose(rwops);
|
|
|
|
buf = memdup(buf, total_size);
|
|
|
|
SDL_RWclose(autobuf);
|
|
|
|
*out_size = total_size;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t write = SDL_RWwrite(autobuf, chunk, 1, chunk_size);
|
|
|
|
|
|
|
|
if(UNLIKELY(write != read)) {
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(chunk);
|
2020-08-15 13:51:12 +02:00
|
|
|
SDL_RWclose(rwops);
|
|
|
|
SDL_RWclose(autobuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_size += write;
|
|
|
|
|
|
|
|
if(max_size && UNLIKELY(total_size > max_size)) {
|
|
|
|
SDL_SetError("File is too large (%zu bytes read so far; max is %zu)", total_size, max_size);
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(chunk);
|
2020-08-15 13:51:12 +02:00
|
|
|
SDL_RWclose(rwops);
|
|
|
|
SDL_RWclose(autobuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 00:31:47 +02:00
|
|
|
|
|
|
|
void SDL_RWsync(SDL_RWops *rwops) {
|
|
|
|
#if HAVE_STDIO_H
|
|
|
|
if(rwops->type == SDL_RWOPS_STDFILE) {
|
|
|
|
FILE *fp = rwops->hidden.stdio.fp;
|
|
|
|
|
|
|
|
if(UNLIKELY(!fp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(fp);
|
|
|
|
|
|
|
|
#ifdef TAISEI_BUILDCONF_HAVE_POSIX
|
|
|
|
fsync(fileno(fp));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|