rewire FileSystem::normalize()

This commit is contained in:
Struma 2020-02-10 17:31:01 -05:00 committed by Roza
parent 1bf5f4f3da
commit 1ba86afbb4
2 changed files with 26 additions and 43 deletions

View file

@ -1,4 +1,4 @@
project('mkxp-z', 'cpp', 'objc', 'objcpp', version: '1.2.0', meson_version: '>=0.47.0', default_options: ['cpp_std=c++11', 'objcpp_std=c++11','buildtype=release'])
project('mkxp-z', 'cpp', 'objc', 'objcpp', version: '1.2.0', meson_version: '>=0.47.0', default_options: ['cpp_std=c++11', 'buildtype=release'])
minimum_macos_version = get_option('macos_min_version')

View file

@ -47,6 +47,10 @@
#import <iconv.h>
#endif
#ifdef __WINDOWS__
#import <direct.h>
#endif
struct SDLRWIoContext {
SDL_RWops *ops;
std::string filename;
@ -630,31 +634,6 @@ void FileSystem::openReadRaw(SDL_RWops &ops, const char *filename,
initReadOps(handle, ops, freeOnClose);
}
// A bit obtuse, but it doesn't work unless I do it this way
char *fixSeparators(const char *in, bool preferred) {
char *ret = new char[512];
OFString *inStr = @(in);
OFString *out;
#ifdef __WINDOWS__
if (preferred) {
out = [inStr stringByReplacingOccurrencesOfString:@"/"
withString:@"\\"
options:0
range:{inStr.length, 0}];
} else
#endif
{
out = [inStr stringByReplacingOccurrencesOfString:@"\\"
withString:@"/"
options:0
range:{inStr.length, 0}];
}
strncpy(ret, [out UTF8String], 512);
return ret;
}
// RGSS normalizes paths for at least audio
// Essentials kind of takes this for granted
// (`Audio/BGM/../../Audio/ME/[file]`)
@ -663,24 +642,28 @@ char *fixSeparators(const char *in, bool preferred) {
char *FileSystem::normalize(const char *pathname, bool preferred,
bool absolute) {
@autoreleasepool {
id str = [OFString stringWithString:@(pathname)];
if (absolute) {
@try {
OFURL *base = [OFURL
fileURLWithPath:OFFileManager.defaultManager.currentDirectoryPath];
OFURL *purl = [OFURL fileURLWithPath:str];
OFString *path =
[OFURL URLWithString:purl.string relativeToURL:base].path;
str = [path copy];
#ifdef __WINDOWS__
str = [OFString
stringWithString:[str substringWithRange:{1, ([str length] - 1)}]];
#endif
} @catch (...) {
}
OFMutableString *str = @(pathname).mutableCopy;
if (absolute && !str.absolutePath) {
[str prependString:@"/"];
[str prependString:OFFileManager.defaultManager.currentDirectoryPath];
}
return fixSeparators([str UTF8String], preferred);
}
str = str.stringByStandardizingPath.mutableCopy;
#ifdef __WINDOWS__
if (preferred) {
[str replaceOccurrencesOfString:@"/" withString:@"\\"];
} else {
[str replaceOccurrencesOfString:@"\\" withString:@"/"];
}
#else
[str replaceOccurrencesOfString:@"\\" withString:@"/"];
#endif
[str makeImmutable];
char *ret = new char[300];
strncpy(ret, str.UTF8String, 300);
return ret;
};
}
bool FileSystem::exists(const char *filename) {