Add Ruby functions for mounting/unmounting paths

This commit is contained in:
Struma 2021-01-09 00:49:31 -05:00 committed by Roza
parent f25acdd7e9
commit 94657eaed5
4 changed files with 83 additions and 4 deletions

View file

@ -127,6 +127,9 @@ RB_METHOD(mkxpPowerState);
RB_METHOD(mkxpSettingsMenu); RB_METHOD(mkxpSettingsMenu);
RB_METHOD(mkxpCpuCount); RB_METHOD(mkxpCpuCount);
RB_METHOD(mkxpSystemMemory); RB_METHOD(mkxpSystemMemory);
RB_METHOD(mkxpReloadPathCache);
RB_METHOD(mkxpAddPath);
RB_METHOD(mkxpRemovePath);
RB_METHOD(mriRgssMain); RB_METHOD(mriRgssMain);
RB_METHOD(mriRgssStop); RB_METHOD(mriRgssStop);
@ -205,6 +208,9 @@ static void mriBindingInit() {
_rb_define_module_function(mod, "power_state", mkxpPowerState); _rb_define_module_function(mod, "power_state", mkxpPowerState);
_rb_define_module_function(mod, "nproc", mkxpCpuCount); _rb_define_module_function(mod, "nproc", mkxpCpuCount);
_rb_define_module_function(mod, "memory", mkxpSystemMemory); _rb_define_module_function(mod, "memory", mkxpSystemMemory);
_rb_define_module_function(mod, "reload_cache", mkxpReloadPathCache);
_rb_define_module_function(mod, "mount", mkxpAddPath);
_rb_define_module_function(mod, "unmount", mkxpRemovePath);
/* Load global constants */ /* Load global constants */
rb_gv_set("MKXP", Qtrue); rb_gv_set("MKXP", Qtrue);
@ -379,6 +385,46 @@ RB_METHOD(mkxpSystemMemory) {
return INT2NUM(SDL_GetSystemRAM()); return INT2NUM(SDL_GetSystemRAM());
} }
RB_METHOD(mkxpReloadPathCache) {
RB_UNUSED_PARAM;
shState->fileSystem().reloadPathCache();
return Qnil;
}
RB_METHOD(mkxpAddPath) {
RB_UNUSED_PARAM;
VALUE path, mountpoint;
rb_scan_args(argc, argv, "11", &path, &mountpoint);
SafeStringValue(path);
if (mountpoint != Qnil) SafeStringValue(mountpoint);
const char *mp = (mountpoint == Qnil) ? 0 : RSTRING_PTR(mountpoint);
try {
shState->fileSystem().addPath(RSTRING_PTR(path), mp, 1);
} catch (Exception &e) {
raiseRbExc(e);
}
return path;
}
RB_METHOD(mkxpRemovePath) {
RB_UNUSED_PARAM;
VALUE path;
rb_scan_args(argc, argv, "1", &path);
SafeStringValue(path);
try {
shState->fileSystem().removePath(RSTRING_PTR(path), 1);
} catch (Exception &e) {
raiseRbExc(e);
}
return path;
}
static VALUE rgssMainCb(VALUE block) { static VALUE rgssMainCb(VALUE block) {
rb_funcall2(block, rb_intern("call"), 0, 0); rb_funcall2(block, rb_intern("call"), 0, 0);
return Qnil; return Qnil;

View file

@ -318,16 +318,33 @@ FileSystem::~FileSystem() {
Debug() << "PhyFS failed to deinit."; Debug() << "PhyFS failed to deinit.";
} }
void FileSystem::addPath(const char *path) { void FileSystem::addPath(const char *path, const char *mountpoint, bool reload) {
/* Try the normal mount first */ /* Try the normal mount first */
if (!PHYSFS_mount(path, 0, 1)) { int state = PHYSFS_mount(path, mountpoint, 1);
if (!state) {
/* If it didn't work, try mounting via a wrapped /* If it didn't work, try mounting via a wrapped
* SDL_RWops */ * SDL_RWops */
PHYSFS_Io *io = createSDLRWIo(path); PHYSFS_Io *io = createSDLRWIo(path);
if (io) if (io)
PHYSFS_mountIo(io, path, 0, 1); state = PHYSFS_mountIo(io, path, 0, 1);
} }
if (!state) {
PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
throw Exception(Exception::PHYSFSError, "Failed to mount %s (%s)", path, PHYSFS_getErrorByCode(err));
}
if (reload) reloadPathCache();
}
void FileSystem::removePath(const char *path, bool reload) {
if (!PHYSFS_unmount(path)) {
PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
throw Exception(Exception::PHYSFSError, "Failed to unmount %s (%s)", path, PHYSFS_getErrorByCode(err));
}
if (reload) reloadPathCache();
} }
struct CacheEnumData { struct CacheEnumData {
@ -424,6 +441,14 @@ void FileSystem::createPathCache() {
p->havePathCache = true; p->havePathCache = true;
} }
void FileSystem::reloadPathCache() {
if (!p->havePathCache) return;
p->fileLists.clear();
p->pathCache.clear();
createPathCache();
}
struct FontSetsCBData { struct FontSetsCBData {
FileSystemPrivate *p; FileSystemPrivate *p;
SharedFontState *sfs; SharedFontState *sfs;

View file

@ -39,10 +39,13 @@ public:
bool allowSymlinks); bool allowSymlinks);
~FileSystem(); ~FileSystem();
void addPath(const char *path); void addPath(const char *path, const char *mountpoint = 0, bool reload = false);
void removePath(const char *path, bool reload = false);
/* Call these after the last 'addPath()' */ /* Call these after the last 'addPath()' */
void createPathCache(); void createPathCache();
void reloadPathCache();
/* Scans "Fonts/" and creates inventory of /* Scans "Fonts/" and creates inventory of
* available font assets */ * available font assets */

View file

@ -92,6 +92,11 @@ public:
{ {
return p.cend(); return p.cend();
} }
inline void clear()
{
p.clear();
}
}; };
template<typename K> template<typename K>