Bind checking case insensitive filenames from Ruby

This commit is contained in:
Struma 2020-04-19 06:44:41 -04:00 committed by Roza
parent 19389a580f
commit 89da4f3b8c
3 changed files with 25 additions and 2 deletions

View file

@ -250,6 +250,17 @@ RB_METHOD(_marshalLoad) {
} }
#endif #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() { void fileIntBindingInit() {
VALUE klass = rb_define_class("FileInt", rb_cIO); VALUE klass = rb_define_class("FileInt", rb_cIO);
#if RAPI_FULL > 187 #if RAPI_FULL > 187
@ -269,6 +280,7 @@ void fileIntBindingInit() {
#endif #endif
_rb_define_method(klass, "binmode", fileIntBinmode); _rb_define_method(klass, "binmode", fileIntBinmode);
_rb_define_method(klass, "close", fileIntClose); _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, "load_data", kernelLoadData);
_rb_define_module_function(rb_mKernel, "save_data", kernelSaveData); _rb_define_module_function(rb_mKernel, "save_data", kernelSaveData);

View file

@ -23,6 +23,7 @@
#define FILESYSTEM_H #define FILESYSTEM_H
#include <SDL_rwops.h> #include <SDL_rwops.h>
#include <string>
struct FileSystemPrivate; struct FileSystemPrivate;
class SharedFontState; class SharedFontState;
@ -69,6 +70,8 @@ public:
/* Does not perform extension supplementing */ /* Does not perform extension supplementing */
bool exists(const char *filename); bool exists(const char *filename);
const char *desensitize(const char *filename);
private: private:
FileSystemPrivate *p; FileSystemPrivate *p;
}; };

View file

@ -622,10 +622,11 @@ void FileSystem::openRead(OpenHandler &handler, const char *filename) {
throw Exception(Exception::NoFileError, "%s", 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, void FileSystem::openReadRaw(SDL_RWops &ops, const char *filename,
bool freeOnClose) { bool freeOnClose) {
PHYSFS_File *handle = PHYSFS_openRead(filename);
PHYSFS_File *handle = PHYSFS_openRead(desensitize(filename));
// assert(handle); // assert(handle);
if (!handle) if (!handle)
@ -669,3 +670,10 @@ char *FileSystem::normalize(const char *pathname, bool preferred,
bool FileSystem::exists(const char *filename) { bool FileSystem::exists(const char *filename) {
return PHYSFS_exists(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;
}