From 89da4f3b8ccf513e94a3887e68eef4a95d2a6be2 Mon Sep 17 00:00:00 2001 From: Struma Date: Sun, 19 Apr 2020 06:44:41 -0400 Subject: [PATCH] Bind checking case insensitive filenames from Ruby --- binding/filesystem-binding.cpp | 12 ++++++++++++ src/filesystem.h | 3 +++ src/filesystem.mm | 12 ++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/binding/filesystem-binding.cpp b/binding/filesystem-binding.cpp index a58b8c27..4e8765d2 100644 --- a/binding/filesystem-binding.cpp +++ b/binding/filesystem-binding.cpp @@ -250,6 +250,17 @@ RB_METHOD(_marshalLoad) { } #endif +RB_METHOD(fileIntDesensitize) { + RB_UNUSED_PARAM; + + VALUE filename; + rb_scan_args(argc, argv, "1", &filename); + SafeStringValue(filename); + + return rb_str_new_cstr( + shState->fileSystem().desensitize(RSTRING_PTR(filename))); +} + void fileIntBindingInit() { VALUE klass = rb_define_class("FileInt", rb_cIO); #if RAPI_FULL > 187 @@ -269,6 +280,7 @@ void fileIntBindingInit() { #endif _rb_define_method(klass, "binmode", fileIntBinmode); _rb_define_method(klass, "close", fileIntClose); + rb_define_singleton_method(klass, "desensitize", RUBY_METHOD_FUNC(fileIntDesensitize), -1); _rb_define_module_function(rb_mKernel, "load_data", kernelLoadData); _rb_define_module_function(rb_mKernel, "save_data", kernelSaveData); diff --git a/src/filesystem.h b/src/filesystem.h index d78c84a6..76781e74 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -23,6 +23,7 @@ #define FILESYSTEM_H #include +#include struct FileSystemPrivate; class SharedFontState; @@ -69,6 +70,8 @@ public: /* Does not perform extension supplementing */ bool exists(const char *filename); + const char *desensitize(const char *filename); + private: FileSystemPrivate *p; }; diff --git a/src/filesystem.mm b/src/filesystem.mm index 04b7933c..0eaadbe6 100644 --- a/src/filesystem.mm +++ b/src/filesystem.mm @@ -622,10 +622,11 @@ void FileSystem::openRead(OpenHandler &handler, const char *filename) { throw Exception(Exception::NoFileError, "%s", filename); } -// FIXME: This is (a) slower than RGSS and (b) case-sensitive +// FIXME: This is slower than RGSS void FileSystem::openReadRaw(SDL_RWops &ops, const char *filename, bool freeOnClose) { - PHYSFS_File *handle = PHYSFS_openRead(filename); + + PHYSFS_File *handle = PHYSFS_openRead(desensitize(filename)); // assert(handle); if (!handle) @@ -669,3 +670,10 @@ char *FileSystem::normalize(const char *pathname, bool preferred, bool FileSystem::exists(const char *filename) { return PHYSFS_exists(filename); } + +const char *FileSystem::desensitize(const char *filename) { + OFString *fn_lower = @(filename).lowercaseString; + if (p->havePathCache && p->pathCache.contains(fn_lower.UTF8String)) + return p->pathCache[fn_lower.UTF8String].c_str(); + return filename; +}