mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-14 02:45:34 +02:00
Add @autoreleasepool blocks to the macOS specific files.
Without these blocks the strings aren't released until the thread that created them is closed, which means file accesses especially leak memory. Also refocus the game when closing the keybindings window on macOS.
This commit is contained in:
parent
a73f9ccc1f
commit
2622a84c53
4 changed files with 92 additions and 65 deletions
|
@ -97,6 +97,7 @@ typedef NSMutableArray<NSNumber*> BindingIndexArray;
|
||||||
-(void)closeWindow {
|
-(void)closeWindow {
|
||||||
[self setNotListening:true];
|
[self setNotListening:true];
|
||||||
[_window close];
|
[_window close];
|
||||||
|
SDL_RaiseWindow(shState->rtData().window);
|
||||||
}
|
}
|
||||||
|
|
||||||
-(SettingsMenu*)setWindow:(NSWindow*)window {
|
-(SettingsMenu*)setWindow:(NSWindow*)window {
|
||||||
|
|
|
@ -118,7 +118,9 @@ MKXPZTouchBar *_sharedTouchBar;
|
||||||
if (fpsLabel) {
|
if (fpsLabel) {
|
||||||
int targetFrameRate = shState->graphics().getFrameRate();
|
int targetFrameRate = shState->graphics().getFrameRate();
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
self->fpsLabel.stringValue = [NSString stringWithFormat:@"%@\n%i FPS (%i%%)", self.gameTitle, value, (int)((float)value / (float)targetFrameRate * 100)];
|
@autoreleasepool {
|
||||||
|
self->fpsLabel.stringValue = [NSString stringWithFormat:@"%@\n%i FPS (%i%%)", self.gameTitle, value, (int)((float)value / (float)targetFrameRate * 100)];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,43 +18,54 @@
|
||||||
#define NSTOPATH(str) [NSFileManager.defaultManager fileSystemRepresentationWithPath:str]
|
#define NSTOPATH(str) [NSFileManager.defaultManager fileSystemRepresentationWithPath:str]
|
||||||
|
|
||||||
bool filesystemImpl::fileExists(const char *path) {
|
bool filesystemImpl::fileExists(const char *path) {
|
||||||
BOOL isDir;
|
@autoreleasepool{
|
||||||
return [NSFileManager.defaultManager fileExistsAtPath:PATHTONS(path) isDirectory: &isDir] && !isDir;
|
BOOL isDir;
|
||||||
|
return [NSFileManager.defaultManager fileExistsAtPath:PATHTONS(path) isDirectory: &isDir] && !isDir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
|
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
|
||||||
NSString *fileContents = [NSString stringWithContentsOfFile: PATHTONS(path)];
|
@autoreleasepool {
|
||||||
if (fileContents == nil)
|
NSString *fileContents = [NSString stringWithContentsOfFile: PATHTONS(path)];
|
||||||
throw Exception(Exception::NoFileError, "Failed to read file at %s", path);
|
if (fileContents == nil)
|
||||||
|
throw Exception(Exception::NoFileError, "Failed to read file at %s", path);
|
||||||
return std::string(fileContents.UTF8String);
|
|
||||||
|
|
||||||
|
return std::string(fileContents.UTF8String);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool filesystemImpl::setCurrentDirectory(const char *path) {
|
bool filesystemImpl::setCurrentDirectory(const char *path) {
|
||||||
return [NSFileManager.defaultManager changeCurrentDirectoryPath: PATHTONS(path)];
|
@autoreleasepool {
|
||||||
|
return [NSFileManager.defaultManager changeCurrentDirectoryPath: PATHTONS(path)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getCurrentDirectory() {
|
std::string filesystemImpl::getCurrentDirectory() {
|
||||||
return std::string(NSTOPATH(NSFileManager.defaultManager.currentDirectoryPath));
|
@autoreleasepool {
|
||||||
|
return std::string(NSTOPATH(NSFileManager.defaultManager.currentDirectoryPath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool absolute) {
|
std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool absolute) {
|
||||||
NSString *nspath = [NSURL fileURLWithPath: PATHTONS(path)].URLByStandardizingPath.path;
|
@autoreleasepool {
|
||||||
NSString *pwd = [NSString stringWithFormat:@"%@/", NSFileManager.defaultManager.currentDirectoryPath];
|
NSString *nspath = [NSURL fileURLWithPath: PATHTONS(path)].URLByStandardizingPath.path;
|
||||||
if (!absolute) {
|
NSString *pwd = [NSString stringWithFormat:@"%@/", NSFileManager.defaultManager.currentDirectoryPath];
|
||||||
nspath = [nspath stringByReplacingOccurrencesOfString:pwd withString:@""];
|
if (!absolute) {
|
||||||
|
nspath = [nspath stringByReplacingOccurrencesOfString:pwd withString:@""];
|
||||||
|
}
|
||||||
|
nspath = [nspath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
|
||||||
|
return std::string(NSTOPATH(nspath));
|
||||||
}
|
}
|
||||||
nspath = [nspath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
|
|
||||||
return std::string(NSTOPATH(nspath));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getDefaultGameRoot() {
|
std::string filesystemImpl::getDefaultGameRoot() {
|
||||||
NSString *p = [NSString stringWithFormat: @"%@/%s", NSBundle.mainBundle.bundlePath, "Contents/Game"];
|
@autoreleasepool {
|
||||||
return std::string(NSTOPATH(p));
|
NSString *p = [NSString stringWithFormat: @"%@/%s", NSBundle.mainBundle.bundlePath, "Contents/Game"];
|
||||||
|
return std::string(NSTOPATH(p));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
|
NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
|
||||||
|
@ -73,51 +84,58 @@ NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getPathForAsset(const char *baseName, const char *ext) {
|
std::string filesystemImpl::getPathForAsset(const char *baseName, const char *ext) {
|
||||||
NSString *assetPath = getPathForAsset_internal(baseName, ext);
|
@autoreleasepool {
|
||||||
if (assetPath == nil)
|
NSString *assetPath = getPathForAsset_internal(baseName, ext);
|
||||||
throw Exception(Exception::NoFileError, "Failed to find the asset named %s.%s", baseName, ext);
|
if (assetPath == nil)
|
||||||
|
throw Exception(Exception::NoFileError, "Failed to find the asset named %s.%s", baseName, ext);
|
||||||
|
|
||||||
return std::string(NSTOPATH(getPathForAsset_internal(baseName, ext)));
|
return std::string(NSTOPATH(getPathForAsset_internal(baseName, ext)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::contentsOfAssetAsString(const char *baseName, const char *ext) {
|
std::string filesystemImpl::contentsOfAssetAsString(const char *baseName, const char *ext) {
|
||||||
NSString *path = getPathForAsset_internal(baseName, ext);
|
@autoreleasepool {
|
||||||
NSString *fileContents = [NSString stringWithContentsOfFile: path];
|
NSString *path = getPathForAsset_internal(baseName, ext);
|
||||||
|
NSString *fileContents = [NSString stringWithContentsOfFile: path];
|
||||||
|
|
||||||
// This should never fail
|
// This should never fail
|
||||||
if (fileContents == nil)
|
if (fileContents == nil)
|
||||||
throw Exception(Exception::MKXPError, "Failed to read file at %s", path.UTF8String);
|
throw Exception(Exception::MKXPError, "Failed to read file at %s", path.UTF8String);
|
||||||
|
|
||||||
return std::string(fileContents.UTF8String);
|
|
||||||
|
|
||||||
|
return std::string(fileContents.UTF8String);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getResourcePath() {
|
std::string filesystemImpl::getResourcePath() {
|
||||||
return std::string(NSTOPATH(NSBundle.mainBundle.resourcePath));
|
@autoreleasepool {
|
||||||
|
return std::string(NSTOPATH(NSBundle.mainBundle.resourcePath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::selectPath(SDL_Window *win, const char *msg, const char *prompt) {
|
std::string filesystemImpl::selectPath(SDL_Window *win, const char *msg, const char *prompt) {
|
||||||
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
@autoreleasepool {
|
||||||
panel.canChooseDirectories = true;
|
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
||||||
panel.canChooseFiles = false;
|
panel.canChooseDirectories = true;
|
||||||
|
panel.canChooseFiles = false;
|
||||||
|
|
||||||
if (msg) panel.message = @(msg);
|
if (msg) panel.message = @(msg);
|
||||||
if (prompt) panel.prompt = @(prompt);
|
if (prompt) panel.prompt = @(prompt);
|
||||||
//panel.directoryURL = [NSURL fileURLWithPath:NSFileManager.defaultManager.currentDirectoryPath];
|
//panel.directoryURL = [NSURL fileURLWithPath:NSFileManager.defaultManager.currentDirectoryPath];
|
||||||
|
|
||||||
SDL_SysWMinfo windowinfo{};
|
SDL_SysWMinfo windowinfo{};
|
||||||
SDL_GetWindowWMInfo(win, &windowinfo);
|
SDL_GetWindowWMInfo(win, &windowinfo);
|
||||||
|
|
||||||
[panel beginSheetModalForWindow:windowinfo.info.cocoa.window completionHandler:^(NSModalResponse res){
|
[panel beginSheetModalForWindow:windowinfo.info.cocoa.window completionHandler:^(NSModalResponse res){
|
||||||
[NSApp stopModalWithCode:res];
|
[NSApp stopModalWithCode:res];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
[NSApp runModalForWindow:windowinfo.info.cocoa.window];
|
[NSApp runModalForWindow:windowinfo.info.cocoa.window];
|
||||||
|
|
||||||
// The window needs to be brought to the front again after the OpenPanel closes
|
// The window needs to be brought to the front again after the OpenPanel closes
|
||||||
[windowinfo.info.cocoa.window makeKeyAndOrderFront:nil];
|
[windowinfo.info.cocoa.window makeKeyAndOrderFront:nil];
|
||||||
if (panel.URLs.count > 0)
|
if (panel.URLs.count > 0)
|
||||||
return std::string(NSTOPATH(panel.URLs[0].path));
|
return std::string(NSTOPATH(panel.URLs[0].path));
|
||||||
|
|
||||||
return std::string();
|
return std::string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,17 @@
|
||||||
#import "SettingsMenuController.h"
|
#import "SettingsMenuController.h"
|
||||||
|
|
||||||
std::string systemImpl::getSystemLanguage() {
|
std::string systemImpl::getSystemLanguage() {
|
||||||
NSString *languageCode = NSLocale.currentLocale.languageCode;
|
@autoreleasepool {
|
||||||
NSString *countryCode = NSLocale.currentLocale.countryCode;
|
NSString *languageCode = NSLocale.currentLocale.languageCode;
|
||||||
return std::string([NSString stringWithFormat:@"%@_%@", languageCode, countryCode].UTF8String);
|
NSString *countryCode = NSLocale.currentLocale.countryCode;
|
||||||
|
return std::string([NSString stringWithFormat:@"%@_%@", languageCode, countryCode].UTF8String);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string systemImpl::getUserName() {
|
std::string systemImpl::getUserName() {
|
||||||
return std::string(NSUserName().UTF8String);
|
@autoreleasepool {
|
||||||
|
return std::string(NSUserName().UTF8String);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int systemImpl::getScalingFactor() {
|
int systemImpl::getScalingFactor() {
|
||||||
|
@ -64,9 +68,11 @@ bool isMetalSupported() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getPlistValue(const char *key) {
|
std::string getPlistValue(const char *key) {
|
||||||
NSString *hash = [[NSBundle mainBundle] objectForInfoDictionaryKey:@(key)];
|
@autoreleasepool {
|
||||||
if (hash != nil) {
|
NSString *hash = [[NSBundle mainBundle] objectForInfoDictionaryKey:@(key)];
|
||||||
return std::string(hash.UTF8String);
|
if (hash != nil) {
|
||||||
|
return std::string(hash.UTF8String);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue