twn_filewatch.c: file and directory change api, initial support for texture reload

This commit is contained in:
veclavtalica
2025-01-29 07:21:09 +03:00
parent 630c6fb5d4
commit 4b2a22bf3c
7 changed files with 246 additions and 29 deletions

View File

@ -1,5 +1,6 @@
#include "twn_loop.h"
#include "twn_engine_context_c.h"
#include "twn_filewatch_c.h"
#include "twn_input_c.h"
#include "twn_util.h"
#include "twn_util_c.h"
@ -733,8 +734,6 @@ static void clean_up(void) {
text_cache_deinit(&ctx.text_cache);
textures_cache_deinit(&ctx.texture_cache);
textures_reset_state();
arrfree(ctx.render_queue_2d);
toml_free(ctx.config_table);
@ -754,6 +753,25 @@ static void reset_state(void) {
}
static void pack_contents_modified(char const *path, enum FilewatchAction action) {
log_info("Pack contents invalidated: %s, action: %i", path, action);
reset_state();
}
/* TODO: handle .btw packs */
static bool try_mounting_root_pack(char *path) {
log_info("Mounting %s", path);
if (!PHYSFS_mount(path, NULL, true))
return false;
filewatch_add_directory(path, &pack_contents_modified);
return true;
}
int enter_loop(int argc, char **argv) {
profile_start("startup");
@ -795,10 +813,8 @@ int enter_loop(int argc, char **argv) {
return EXIT_FAILURE;
}
if (!PHYSFS_mount(argv[i+1], NULL, true)) {
CRY_PHYSFS("Data dir mount override failed.");
if (!try_mounting_root_pack(argv[i+1]))
return EXIT_FAILURE;
}
data_dir_mounted = true;
@ -823,10 +839,10 @@ int enter_loop(int argc, char **argv) {
/* try mouning data folder first, relative to executable root */
char *full_path;
SDL_asprintf(&full_path, "%sdata", ctx.base_dir);
if (!PHYSFS_mount(full_path, NULL, true)) {
if (!try_mounting_root_pack(full_path)) {
SDL_free(full_path);
SDL_asprintf(&full_path, "%sdata.btw", ctx.base_dir);
if (!PHYSFS_mount(full_path, NULL, true)) {
if (!try_mounting_root_pack(full_path)) {
SDL_free(full_path);
CRY_PHYSFS("Cannot find data.btw or data directory in root. Please create them or specify with --data-dir parameter.");
return EXIT_FAILURE;
@ -857,6 +873,7 @@ int enter_loop(int argc, char **argv) {
reset_state();
}
filewatch_poll();
main_loop();
}