Skip to content

Commit

Permalink
Merge pull request #377 from AliceLR/add-vfs-config
Browse files Browse the repository at this point in the history
Add VFS configuration options and other improvements.
  • Loading branch information
AliceLR committed Sep 20, 2023
2 parents c69b840 + 72d2a16 commit 9caaf24
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 41 deletions.
19 changes: 16 additions & 3 deletions src/audio/audio_vorbis.c
Expand Up @@ -220,6 +220,12 @@ static int vorbis_seek_fn(void *stream, ogg_int64_t offset, int whence)
return vfseek(vf, offset, whence);
}

static int vorbis_close_fn(void *stream)
{
// Do nothing; tremor lowmem will CRASH if this function isn't provided.
return 0;
}

static long vorbis_tell_fn(void *stream)
{
vfile *vf = (vfile *)stream;
Expand All @@ -230,7 +236,7 @@ static const ov_callbacks vorbis_callbacks =
{
vorbis_read_fn,
vorbis_seek_fn,
NULL,
vorbis_close_fn,
vorbis_tell_fn,
};

Expand Down Expand Up @@ -283,8 +289,15 @@ static struct audio_stream *construct_vorbis_stream(vfile *vf,

OggVorbis_File open_file;

// FIXME: the vfile must be forced into memory for DJGPP so that filesystem
// operations will not occur during the audio interrupt.
#ifdef CONFIG_DJGPP
// DJGPP may have instability related to filesystem IO in the audio interrupt.
// TODO: this might have been entirely the tremor lowmem close function crash,
// so reenable this and use V_FORCE_CACHE in ext.c if problems continue.
/*
if(!vfile_force_to_memory(vf))
return NULL;
*/
#endif

if(ov_open_callbacks(vf, &open_file, NULL, 0, vorbis_callbacks) != 0)
return NULL;
Expand Down
74 changes: 74 additions & 0 deletions src/configure.c
Expand Up @@ -85,6 +85,8 @@

#ifdef CONFIG_3DS
#define VIDEO_RATIO_DEFAULT RATIO_CLASSIC_4_3
#define VFS_ENABLE_DEFAULT true
#define VFS_MAX_CACHE_SIZE_DEFAULT (1 << 25) /* 32 MiB */
#endif

#ifdef CONFIG_WIIU
Expand Down Expand Up @@ -112,6 +114,8 @@
#define RESAMPLE_MODE_DEFAULT RESAMPLE_MODE_NONE
#define MOD_RESAMPLE_MODE_DEFAULT RESAMPLE_MODE_NONE
#define FULLSCREEN_DEFAULT 1
#define VFS_ENABLE_DEFAULT true
#define VFS_ENABLE_AUTO_CACHE_DEFAULT false
#endif

// End arch-specific config.
Expand Down Expand Up @@ -166,6 +170,22 @@
#define VIDEO_RATIO_DEFAULT RATIO_MODERN_64_35
#endif

#ifndef VFS_ENABLE_DEFAULT
#define VFS_ENABLE_DEFAULT false
#endif

#ifndef VFS_ENABLE_AUTO_CACHE_DEFAULT
#define VFS_ENABLE_AUTO_CACHE_DEFAULT true
#endif

#ifndef VFS_MAX_CACHE_SIZE_DEFAULT
#define VFS_MAX_CACHE_SIZE_DEFAULT (1 << 24)
#endif

#ifndef VFS_MAX_CACHE_FILE_SIZE_DEFAULT
#define VFS_MAX_CACHE_FILE_SIZE_DEFAULT (VFS_MAX_CACHE_SIZE_DEFAULT >> 2)
#endif

#ifndef AUTO_DECRYPT_WORLDS
#define AUTO_DECRYPT_WORLDS true
#endif
Expand Down Expand Up @@ -244,6 +264,12 @@ static const struct config_info user_conf_default =
false, // pause_on_unfocus
1, // num_buffered_events

// Virtual filesystem options
VFS_ENABLE_DEFAULT, // vfs_enable
VFS_ENABLE_AUTO_CACHE_DEFAULT,// vfs_enable_auto_cache
VFS_MAX_CACHE_SIZE_DEFAULT, // vfs_max_cache_size_default
VFS_MAX_CACHE_FILE_SIZE_DEFAULT, // vfs_max_cache_file_size_default

// Game options
"", // startup_path
"caverns.mzx", // startup_file
Expand Down Expand Up @@ -434,6 +460,22 @@ boolean config_int(int *dest, char *value, int min, int max)
return true;
}

static boolean config_long_long(long long *dest, char *value, long long min,
long long max)
{
long long result;
int n;

if(sscanf(value, "%lld%n", &result, &n) != 1 || value[n] != 0)
return false;

if(result < min || result > max)
return false;

*dest = result;
return true;
}

#define config_enum(d, v, a) _config_enum(d, v, a, ARRAY_SIZE(a))

__editor_maybe_static
Expand Down Expand Up @@ -1095,6 +1137,34 @@ static void config_test_mode_start_board(struct config_info *conf,
conf->test_mode_start_board = result;
}

static void config_set_vfs_enable(struct config_info *conf,
char *name, char *value, char *extended_data)
{
config_boolean(&conf->vfs_enable, value);
}

static void config_set_vfs_enable_auto_cache(struct config_info *conf,
char *name, char *value, char *extended_data)
{
config_boolean(&conf->vfs_enable_auto_cache, value);
}

static void config_set_vfs_max_cache_size(struct config_info *conf,
char *name, char *value, char *extended_data)
{
long long result;
if(config_long_long(&result, value, 0, LLONG_MAX))
conf->vfs_max_cache_size = result;
}

static void config_set_vfs_max_cache_file_size(struct config_info *conf,
char *name, char *value, char *extended_data)
{
long long result;
if(config_long_long(&result, value, 0, LLONG_MAX))
conf->vfs_max_cache_file_size = result;
}

/* NOTE: This is searched as a binary tree, the nodes must be
* sorted alphabetically, or they risk being ignored.
*/
Expand Down Expand Up @@ -1176,6 +1246,10 @@ static const struct config_entry config_options[] =
{ "update_branch_pin", config_update_branch_pin, false },
{ "update_host", config_update_host, false },
#endif
{ "vfs_enable", config_set_vfs_enable, false },
{ "vfs_enable_auto_cache", config_set_vfs_enable_auto_cache, false },
{ "vfs_max_cache_file_size", config_set_vfs_max_cache_file_size, false },
{ "vfs_max_cache_size", config_set_vfs_max_cache_size, false },
{ "video_output", config_set_video_output, false },
{ "video_ratio", config_set_video_ratio, false },
{ "window_resolution", config_window_resolution, false }
Expand Down
6 changes: 6 additions & 0 deletions src/configure.h
Expand Up @@ -136,6 +136,12 @@ struct config_info
boolean pause_on_unfocus;
int num_buffered_events;

// Virtual filesystem options
boolean vfs_enable;
boolean vfs_enable_auto_cache;
long long vfs_max_cache_size;
long long vfs_max_cache_file_size;

// Game options
char startup_path[256];
char startup_file[256];
Expand Down
17 changes: 17 additions & 0 deletions src/editor/debug.c
Expand Up @@ -272,6 +272,8 @@ enum virtual_var
VIR_RAM_DEBUGGER_ROBOTS,
VIR_RAM_DEBUGGER_VARIABLES,
VIR_RAM_EXTRAM_DELTA,
VIR_RAM_VIRTUAL_FILESYSTEM,
VIR_RAM_VIRTUAL_FILESYSTEM_CACHED_ONLY,
};

static const char * const virtual_var_names[] =
Expand All @@ -297,6 +299,8 @@ static const char * const virtual_var_names[] =
"Debug (robots)*",
"Debug (variables)*",
"ExtRAM compression delta*",
"Virtual filesystem (total)*",
"Virtual filesystem (cached only)*",
};

// We'll read off of these when we construct the tree
Expand Down Expand Up @@ -368,6 +372,8 @@ static const enum virtual_var world_ram_var_list[] =
#ifdef CONFIG_EXTRAM
VIR_RAM_EXTRAM_DELTA,
#endif
VIR_RAM_VIRTUAL_FILESYSTEM,
VIR_RAM_VIRTUAL_FILESYSTEM_CACHED_ONLY,
};

static const char *board_var_list[] =
Expand Down Expand Up @@ -518,6 +524,8 @@ struct debug_ram_data
size_t debug_variables_total_size;
size_t extram_uncompressed_size;
size_t extram_compressed_size;
size_t virtual_filesystem_size;
size_t virtual_filesystem_cached_size;
};

static struct debug_ram_data ram_data;
Expand Down Expand Up @@ -639,6 +647,9 @@ static void update_ram_usage_data(struct world *mzx_world,
}

ram_data.debug_variables_total_size = var_debug_usage;

ram_data.virtual_filesystem_size = vio_filesystem_total_memory_usage();
ram_data.virtual_filesystem_cached_size = vio_filesystem_total_cached_usage();
}

#define match_counter(_name) (strlen(_name) == len && !strcasecmp(name, _name))
Expand Down Expand Up @@ -947,6 +958,12 @@ static void get_var_value(struct world *mzx_world, struct debug_var *v,
value = (int64_t)ram_data.extram_compressed_size -
(int64_t)ram_data.extram_uncompressed_size;
break;
case VIR_RAM_VIRTUAL_FILESYSTEM:
value = ram_data.virtual_filesystem_size;
break;
case VIR_RAM_VIRTUAL_FILESYSTEM_CACHED_ONLY:
value = ram_data.virtual_filesystem_cached_size;
break;
}
*long_value = value;
break;
Expand Down
27 changes: 25 additions & 2 deletions src/io/fsafeopen.c
Expand Up @@ -30,7 +30,20 @@

#include "../util.h"

#ifndef __WIN32__
#if defined(CONFIG_3DS)
// Enable hacks to reduce the number of filesystem operations on platforms
// with extremely slow filesystem access.
#define SLOW_FILESYSTEM_HACKS
#endif

#if defined(SLOW_FILESYSTEM_HACKS) || defined(_WIN32) || defined(CONFIG_DJGPP)
// Skip the extra all caps/lower case checks, either because the current
// platform will always be case-insensitive, or because they are slower than
// just scanning the directory.
#define NO_EXTRA_CASE_CHECKS
#endif

#ifndef _WIN32
#define ENABLE_DOS_COMPAT_TRANSLATIONS
#endif

Expand Down Expand Up @@ -306,7 +319,7 @@ static int case5(char *path, size_t buffer_len, char *string, boolean check_sfn)
newpath[dirlen + 2 - 1] = 0;
}

wd = vdir_open(newpath);
wd = vdir_open_ext(newpath, VDIR_FAST);
if(wd)
{
const char *string_cmp = string;
Expand Down Expand Up @@ -437,6 +450,11 @@ static int match(char *path, size_t buffer_len)
if(vstat(path, &inode) == 0)
break;

#ifdef NO_EXTRA_CASE_CHECKS
// Skip the "normal" cases.
i = 4;
#endif

// try normal cases, then try brute force
switch(i)
{
Expand Down Expand Up @@ -475,6 +493,11 @@ static int match(char *path, size_t buffer_len)
if(vstat(path, &inode) == 0)
break;

#ifdef NO_EXTRA_CASE_CHECKS
// Skip the "normal" cases.
i = 2;
#endif

// try normal cases, then try brute force
switch(i)
{
Expand Down

0 comments on commit 9caaf24

Please sign in to comment.