From 67855979fcac27680e3e8997817e822b3f97ac27 Mon Sep 17 00:00:00 2001 From: TheMoonThatRises <58153205+TheMoonThatRises@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:39:35 -0600 Subject: [PATCH] Fix environment variables when launching via alias (#1090) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: metalhud not working when launch from alias * fix: environment variables --------- Co-authored-by: José Moreno <47700212+JoseMoreville@users.noreply.github.com> --- PlayCover/Model/AppInfo.swift | 25 +++++++++++++++++++++++++ PlayCover/Model/PlayApp.swift | 27 +++++++++++++++++++++++++++ PlayCover/Views/AppSettingsView.swift | 13 +++++++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/PlayCover/Model/AppInfo.swift b/PlayCover/Model/AppInfo.swift index 3274a9bd0..4bf60131d 100644 --- a/PlayCover/Model/AppInfo.swift +++ b/PlayCover/Model/AppInfo.swift @@ -235,6 +235,31 @@ public class AppInfo { return "AppIcon" } + var lsEnvironment: [String: String] { + get { + if self[dictionary: "LSEnvironment"] == nil { + self[dictionary: "LSEnvironment"] = NSMutableDictionary(dictionary: [String: String]()) + } + + return self[dictionary: "LSEnvironment"] as? [String: String] ?? [:] + } + set { + if self[dictionary: "LSEnvironment"] == nil { + self[dictionary: "LSEnvironment"] = NSMutableDictionary(dictionary: [String: String]()) + } + + if let key = newValue.first?.key, let value = newValue.first?.value { + self[dictionary: "LSEnvironment"]?[key] = value + + do { + try write() + } catch { + Log.shared.error(error) + } + } + } + } + func assert(minimumVersion: Double) { if let double = Double(minimumOSVersion) { if double > 11.0 { diff --git a/PlayCover/Model/PlayApp.swift b/PlayCover/Model/PlayApp.swift index 65550912b..d3964af63 100644 --- a/PlayCover/Model/PlayApp.swift +++ b/PlayCover/Model/PlayApp.swift @@ -202,6 +202,33 @@ class PlayApp: BaseApp { } } + func introspection(set: Bool? = nil) -> Bool { + if info.lsEnvironment["DYLD_LIBRARY_PATH"] == nil { + info.lsEnvironment["DYLD_LIBRARY_PATH"] = "" + } + + if let set = set { + if set { + info.lsEnvironment["DYLD_LIBRARY_PATH"]? += "/usr/lib/system/introspection:" + } else { + info.lsEnvironment["DYLD_LIBRARY_PATH"] = info.lsEnvironment["DYLD_LIBRARY_PATH"]? + .replacingOccurrences(of: "/usr/lib/system/introspection:", with: "") + } + + do { + try Shell.signApp(executable) + } catch { + Log.shared.error(error) + } + } + + guard let introspection = info.lsEnvironment["DYLD_LIBRARY_PATH"] else { + return false + } + + return introspection.contains("/usr/lib/system/introspection") + } + func hasAlias() -> Bool { return FileManager.default.fileExists(atPath: aliasURL.path) } diff --git a/PlayCover/Views/AppSettingsView.swift b/PlayCover/Views/AppSettingsView.swift index 0b075f9a7..b0336319f 100644 --- a/PlayCover/Views/AppSettingsView.swift +++ b/PlayCover/Views/AppSettingsView.swift @@ -81,7 +81,9 @@ struct AppSettingsView: View { } .disabled(!(hasPlayTools ?? true)) BypassesView(settings: $viewModel.settings, - hasPlayTools: $hasPlayTools) + hasPlayTools: $hasPlayTools, + hasIntrospection: viewModel.app.introspection(), + app: viewModel.app) .tabItem { Text("settings.tab.bypasses") } @@ -442,6 +444,10 @@ struct BypassesView: View { @Binding var settings: AppSettings @Binding var hasPlayTools: Bool? + @State var hasIntrospection: Bool + + var app: PlayApp + var body: some View { ScrollView { VStack { @@ -462,13 +468,16 @@ struct BypassesView: View { } Spacer() HStack { - Toggle("settings.toggle.introspection", isOn: $settings.settings.injectIntrospection) + Toggle("settings.toggle.introspection", isOn: $hasIntrospection) .help("settings.toggle.introspection.help") Spacer() } } .padding() } + .onChange(of: hasIntrospection) {_ in + _ = app.introspection(set: hasIntrospection) + } } }