Skip to content

Commit

Permalink
Check if app has macOS native version (PlayCover#1543)
Browse files Browse the repository at this point in the history
* MACOS_APPS list and hasMacVersion

* added error message

- Added fetchAppID() function to fetch ID by boundIeID (maybe can be improved)
- Added error message with 3 buttons: start anyway, quit ad open appstore (thats why i need ID and not bundleID)

* removed unused import + added check on install

* onInstall check

---------

Co-authored-by: Depal1 <47154119+Depal1@users.noreply.github.com>
  • Loading branch information
Catta1997 and Depal1 authored Aug 26, 2024
1 parent 0cfcffe commit 58e948f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
22 changes: 14 additions & 8 deletions PlayCover/AppInstaller/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ class DownloadApp {
return
}
}

if let wrapedURL = url {
if wrapedURL.isFileURL {
proceedInstall(url, deleteIPA: false)
} else {
let (finalURL, urlIsValid) = NetworkVM.urlAccessible(url: wrapedURL, popup: true)
if urlIsValid, let newWrappedURL = finalURL {
proceedDownload(newWrappedURL)
if let url = url, let app = app {
let ipa = IPA(url: url)
Task {
if await ipa.hasMacVersion(app: IPA.Application.store(app)) {
cancel()
} else {
if url.isFileURL {
proceedInstall(url, deleteIPA: false)
} else {
let (finalURL, urlIsValid) = NetworkVM.urlAccessible(url: url, popup: true)
if urlIsValid, let newWrappedURL = finalURL {
proceedDownload(newWrappedURL)
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions PlayCover/AppInstaller/Installer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class Installer {
try ipa.allocateTempDir()

let app = try ipa.unzip()
if await ipa.hasMacVersion(app: IPA.Application.base(app)) {
ipa.releaseTempDir()
InstallVM.shared.next(.failed, 0.95, 1.0)
returnCompletion(nil)
return
}
InstallVM.shared.next(.library, 0.5, 0.55)
try saveEntitlements(app)
let machos = resolveValidMachOs(app)
Expand Down
11 changes: 11 additions & 0 deletions PlayCover/Utils/Extensions/PlayAppExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ extension PlayApp {
func removeAlias() {
FileManager.default.delete(at: aliasURL)
}

var hasMacVersion: Bool {
PlayApp.MACOS_APPS.contains(info.bundleIdentifier)
}

static let MACOS_APPS = [
"com.innersloth.amongus",
"com.devsisters.ck",
"com.miHoYo.bh3global"
]

}
51 changes: 51 additions & 0 deletions PlayCover/Utils/IPA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,55 @@ public class IPA {
let documentsDirectory = paths[0]
return documentsDirectory
}

enum Application {
case base(BaseApp)
case store(StoreAppData)
}

@MainActor
func hasMacVersion(app: Application) async -> Bool {
let bundleID: String
let appID: Int
switch app {
case .base(let base):
bundleID = base.info.bundleIdentifier
let urlString = "https://itunes.apple.com/lookup?bundleId=\(bundleID)"
let itunes: ITunesResponse? = await getITunesData(urlString)
appID = itunes?.results.first?.trackId ?? 0
case .store(let store):
bundleID = store.bundleID
let appLookup = store.itunesLookup
let stringArray = appLookup.components(separatedBy: CharacterSet.decimalDigits.inverted)
appID = Int(stringArray.last ?? "0") ?? 0
}
let noMacAlert = UserDefaults.standard.bool(forKey: "\(bundleID).noMacAlert")
if PlayApp.MACOS_APPS.contains(bundleID), !noMacAlert {
let alert = NSAlert()
alert.messageText = NSLocalizedString("alert.error", comment: "")
alert.informativeText = String(
format: NSLocalizedString("macos.version", comment: "")
)
alert.alertStyle = .warning
alert.addButton(withTitle: NSLocalizedString("alert.install.anyway", comment: ""))
alert.addButton(withTitle: NSLocalizedString("alert.open.appstore", comment: ""))
alert.addButton(withTitle: NSLocalizedString("button.Cancel", comment: ""))
let result = alert.runModal()
switch result {
case .alertFirstButtonReturn:
UserDefaults.standard.set(true, forKey: "\(bundleID).noMacAlert")
case .alertSecondButtonReturn:
if appID != 0 {
guard let urlApp = URL(string:
"itms-apps://apps.apple.com/app/id\(appID)")
else {return true}
NSWorkspace.shared.open(urlApp)
}
return true
default:
return true
}
}
return false
}
}
3 changes: 3 additions & 0 deletions PlayCover/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,7 @@
"keycover.alert.title" = "Keychain was not unlocked";
"keycover.alert.content" = "Keychain access has been disabled for the current session";

"macos.version" = "This app can be installed from Mac App Store";
"alert.install.anyway" = "Install Anyway";
"alert.open.appstore" = "Open Mac App Store";

0 comments on commit 58e948f

Please sign in to comment.