mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-11 01:15:33 +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(), ^{
|
||||||
|
@autoreleasepool {
|
||||||
self->fpsLabel.stringValue = [NSString stringWithFormat:@"%@\n%i FPS (%i%%)", self.gameTitle, value, (int)((float)value / (float)targetFrameRate * 100)];
|
self->fpsLabel.stringValue = [NSString stringWithFormat:@"%@\n%i FPS (%i%%)", self.gameTitle, value, (int)((float)value / (float)targetFrameRate * 100)];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,31 +18,39 @@
|
||||||
#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) {
|
||||||
|
@autoreleasepool{
|
||||||
BOOL isDir;
|
BOOL isDir;
|
||||||
return [NSFileManager.defaultManager fileExistsAtPath:PATHTONS(path) isDirectory: &isDir] && !isDir;
|
return [NSFileManager.defaultManager fileExistsAtPath:PATHTONS(path) isDirectory: &isDir] && !isDir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
|
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *fileContents = [NSString stringWithContentsOfFile: PATHTONS(path)];
|
NSString *fileContents = [NSString stringWithContentsOfFile: PATHTONS(path)];
|
||||||
if (fileContents == nil)
|
if (fileContents == nil)
|
||||||
throw Exception(Exception::NoFileError, "Failed to read file at %s", path);
|
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) {
|
||||||
|
@autoreleasepool {
|
||||||
return [NSFileManager.defaultManager changeCurrentDirectoryPath: PATHTONS(path)];
|
return [NSFileManager.defaultManager changeCurrentDirectoryPath: PATHTONS(path)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getCurrentDirectory() {
|
std::string filesystemImpl::getCurrentDirectory() {
|
||||||
|
@autoreleasepool {
|
||||||
return std::string(NSTOPATH(NSFileManager.defaultManager.currentDirectoryPath));
|
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) {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *nspath = [NSURL fileURLWithPath: PATHTONS(path)].URLByStandardizingPath.path;
|
NSString *nspath = [NSURL fileURLWithPath: PATHTONS(path)].URLByStandardizingPath.path;
|
||||||
NSString *pwd = [NSString stringWithFormat:@"%@/", NSFileManager.defaultManager.currentDirectoryPath];
|
NSString *pwd = [NSString stringWithFormat:@"%@/", NSFileManager.defaultManager.currentDirectoryPath];
|
||||||
if (!absolute) {
|
if (!absolute) {
|
||||||
|
@ -50,11 +58,14 @@ std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool
|
||||||
}
|
}
|
||||||
nspath = [nspath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
|
nspath = [nspath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
|
||||||
return std::string(NSTOPATH(nspath));
|
return std::string(NSTOPATH(nspath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filesystemImpl::getDefaultGameRoot() {
|
std::string filesystemImpl::getDefaultGameRoot() {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *p = [NSString stringWithFormat: @"%@/%s", NSBundle.mainBundle.bundlePath, "Contents/Game"];
|
NSString *p = [NSString stringWithFormat: @"%@/%s", NSBundle.mainBundle.bundlePath, "Contents/Game"];
|
||||||
return std::string(NSTOPATH(p));
|
return std::string(NSTOPATH(p));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
|
NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
|
||||||
|
@ -73,14 +84,17 @@ 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) {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *assetPath = getPathForAsset_internal(baseName, ext);
|
NSString *assetPath = getPathForAsset_internal(baseName, ext);
|
||||||
if (assetPath == nil)
|
if (assetPath == nil)
|
||||||
throw Exception(Exception::NoFileError, "Failed to find the asset named %s.%s", baseName, ext);
|
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) {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *path = getPathForAsset_internal(baseName, ext);
|
NSString *path = getPathForAsset_internal(baseName, ext);
|
||||||
NSString *fileContents = [NSString stringWithContentsOfFile: path];
|
NSString *fileContents = [NSString stringWithContentsOfFile: path];
|
||||||
|
|
||||||
|
@ -89,14 +103,17 @@ std::string filesystemImpl::contentsOfAssetAsString(const char *baseName, const
|
||||||
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() {
|
||||||
|
@autoreleasepool {
|
||||||
return std::string(NSTOPATH(NSBundle.mainBundle.resourcePath));
|
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) {
|
||||||
|
@autoreleasepool {
|
||||||
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
||||||
panel.canChooseDirectories = true;
|
panel.canChooseDirectories = true;
|
||||||
panel.canChooseFiles = false;
|
panel.canChooseFiles = false;
|
||||||
|
@ -120,4 +137,5 @@ std::string filesystemImpl::selectPath(SDL_Window *win, const char *msg, const c
|
||||||
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() {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *languageCode = NSLocale.currentLocale.languageCode;
|
NSString *languageCode = NSLocale.currentLocale.languageCode;
|
||||||
NSString *countryCode = NSLocale.currentLocale.countryCode;
|
NSString *countryCode = NSLocale.currentLocale.countryCode;
|
||||||
return std::string([NSString stringWithFormat:@"%@_%@", languageCode, countryCode].UTF8String);
|
return std::string([NSString stringWithFormat:@"%@_%@", languageCode, countryCode].UTF8String);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string systemImpl::getUserName() {
|
std::string systemImpl::getUserName() {
|
||||||
|
@autoreleasepool {
|
||||||
return std::string(NSUserName().UTF8String);
|
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) {
|
||||||
|
@autoreleasepool {
|
||||||
NSString *hash = [[NSBundle mainBundle] objectForInfoDictionaryKey:@(key)];
|
NSString *hash = [[NSBundle mainBundle] objectForInfoDictionaryKey:@(key)];
|
||||||
if (hash != nil) {
|
if (hash != nil) {
|
||||||
return std::string(hash.UTF8String);
|
return std::string(hash.UTF8String);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue