Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions AKPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ private struct AKAppSettingsData: Codable {
var resizableAspectRatioHeight: Int?
}

private func akCurrentUserHomeDirectoryPath() -> String {
let userName = NSUserName()
if let homeDirectory = NSHomeDirectoryForUser(userName) {
return homeDirectory
private func akCurrentAccountHomeDirectoryURL() -> URL {
let configuredBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX)
var bufferSize = configuredBufferSize > 0 ? Int(configuredBufferSize) : 1_024

while true {
var password = passwd()
var result: UnsafeMutablePointer<passwd>?
var buffer = [CChar](repeating: 0, count: bufferSize)
let lookup = buffer.withUnsafeMutableBufferPointer { bufferPointer -> (Int32, String?) in
let status = getpwuid_r(getuid(), &password, bufferPointer.baseAddress, bufferPointer.count, &result)
guard status == 0, result != nil, let path = password.pw_dir else {
return (status, nil)
}
return (status, String(cString: path))
}
if lookup.0 == ERANGE {
bufferSize *= 2
continue
}
guard let homeDirectoryPath = lookup.1 else {
fatalError("Unable to locate the current account home directory")
}
return URL(fileURLWithPath: homeDirectoryPath, isDirectory: true)
}
return NSString(string: "~\(userName)").expandingTildeInPath
}

private func akSettingsURLForBundleIdentifier(_ bundleIdentifier: String) -> URL {
return URL(fileURLWithPath: akCurrentUserHomeDirectoryPath(), isDirectory: true)
return akCurrentAccountHomeDirectoryURL()
.appendingPathComponent("Library", isDirectory: true)
.appendingPathComponent("Containers", isDirectory: true)
.appendingPathComponent("io.playcover.PlayCover", isDirectory: true)
Expand Down
2 changes: 1 addition & 1 deletion PlayTools/Keymap/Keymapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Keymapping {
}

init() {
baseKeymapURL = URL(fileURLWithPath: "/Users/\(NSUserName())/Library/Containers/io.playcover.PlayCover")
baseKeymapURL = playCoverContainerBaseURL()
.appendingPathComponent("Keymapping")
.appendingPathComponent(bundleIdentifier)

Expand Down
52 changes: 19 additions & 33 deletions PlayTools/PlayLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Foundation/Foundation.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <stdatomic.h>
#include <sys/sysctl.h>

Expand Down Expand Up @@ -219,48 +220,33 @@ static OSStatus pt_SecKeyGeneratePair(CFDictionaryRef parameters, SecKeyRef *pub
static uint8_t ue_status = 0;

static void pt_copyCurrentUserHomeDirectory(char *buffer, size_t bufferSize) {
if (bufferSize == 0) {
if (buffer == NULL || bufferSize == 0) {
return;
}

buffer[0] = '\0';

char executablePath[PATH_MAX];
uint32_t executablePathSize = sizeof(executablePath);
if (_NSGetExecutablePath(executablePath, &executablePathSize) == 0) {
static char const containerMarker[] = "/Library/Containers/io.playcover.PlayCover/";
char *marker = strstr(executablePath, containerMarker);
if (marker != NULL) {
size_t homePathLength = (size_t)(marker - executablePath);
if (homePathLength > 0 && homePathLength < bufferSize) {
memcpy(buffer, executablePath, homePathLength);
buffer[homePathLength] = '\0';
return;
long configuredBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX);
size_t accountBufferSize = configuredBufferSize > 0 ? (size_t)configuredBufferSize : 1024;
char *accountBuffer = malloc(accountBufferSize);
while (accountBuffer != NULL) {
struct passwd password;
struct passwd *result = NULL;
int status = getpwuid_r(getuid(), &password, accountBuffer, accountBufferSize, &result);
if (status != ERANGE) {
if (status == 0 && result != NULL && password.pw_dir != NULL) {
snprintf(buffer, bufferSize, "%s", password.pw_dir);
}
break;
}
}

char const *homeDirectory = getenv("HOME");
if (homeDirectory != NULL && homeDirectory[0] != '\0') {
static char const containerSuffix[] = "/Library/Containers/io.playcover.PlayCover";
char const *suffix = strstr(homeDirectory, containerSuffix);
if (suffix != NULL) {
size_t homePathLength = (size_t)(suffix - homeDirectory);
if (homePathLength > 0 && homePathLength < bufferSize) {
memcpy(buffer, homeDirectory, homePathLength);
buffer[homePathLength] = '\0';
return;
}
accountBufferSize *= 2;
char *resizedBuffer = realloc(accountBuffer, accountBufferSize);
if (resizedBuffer == NULL) {
break;
}

snprintf(buffer, bufferSize, "%s", homeDirectory);
return;
}

char userName[256];
if (getlogin_r(userName, sizeof(userName)) == 0) {
snprintf(buffer, bufferSize, "/Users/%s", userName);
accountBuffer = resizedBuffer;
}
free(accountBuffer);
}

static char const* ue_fix_filename(char const* filename) {
Expand Down
30 changes: 24 additions & 6 deletions PlayTools/PlaySettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@ import UIKit

let settings = PlaySettings.shared

func playCoverUserHomeDirectoryPath() -> String {
let userName = NSUserName()
if let homeDirectory = NSHomeDirectoryForUser(userName) {
return homeDirectory
private func currentAccountHomeDirectoryURL() -> URL {
let configuredBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX)
var bufferSize = configuredBufferSize > 0 ? Int(configuredBufferSize) : 1_024

while true {
var password = passwd()
var result: UnsafeMutablePointer<passwd>?
var buffer = [CChar](repeating: 0, count: bufferSize)
let lookup = buffer.withUnsafeMutableBufferPointer { bufferPointer -> (Int32, String?) in
let status = getpwuid_r(getuid(), &password, bufferPointer.baseAddress, bufferPointer.count, &result)
guard status == 0, result != nil, let path = password.pw_dir else {
return (status, nil)
}
return (status, String(cString: path))
}
if lookup.0 == ERANGE {
bufferSize *= 2
continue
}
guard let homeDirectoryPath = lookup.1 else {
fatalError("Unable to locate the current account home directory")
}
return URL(fileURLWithPath: homeDirectoryPath, isDirectory: true)
}
return NSString(string: "~\(userName)").expandingTildeInPath
}

func playCoverContainerBaseURL() -> URL {
URL(fileURLWithPath: playCoverUserHomeDirectoryPath(), isDirectory: true)
currentAccountHomeDirectoryURL()
.appendingPathComponent("Library", isDirectory: true)
.appendingPathComponent("Containers", isDirectory: true)
.appendingPathComponent("io.playcover.PlayCover", isDirectory: true)
Expand Down
Loading