Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

2.8 KB
Raw Blame
import Foundation

/// The provisioning profile an installed app was signed with, and when it expires: a free Apple ID's profile lasts 7
/// days, so a game installed with one stops starting unless the same app is signed again. The launcher can only tell
/// when to re-sign if it reads that date from the app the signature is in.
///
/// An App Store or paid-team profile lives a year, so the date is usually far away and nothing is shown as urgent.
package enum AppSignature {
    package static let profileName = "embedded.mobileprovision"

    /// `ExpirationDate` of the app's embedded.mobileprovision, read through `security cms -D` (the profile is a
    /// CMS-wrapped property list); nil when the app carries no profile or it cannot be read.
    package static func expiration(ofApp app: String, runner: ProcessRunner = SystemProcessRunner()) -> Date? {
        let profile = app + "/" + profileName
        guard FileManager.default.fileExists(atPath: profile) else { return nil }
        guard let out = runner.output("/usr/bin/security", ["cms", "-D", "-i", profile]) else { return nil }
        return expiration(fromProfile: Data(out.utf8))
    }

    /// The profile plist's ExpirationDate. Split out so the parse is tested without a signed app.
    package static func expiration(fromProfile data: Data) -> Date? {
        guard let plist = (try? PropertyListSerialization.propertyList(from: data, options: [], format: nil)) as? [String: Any]
        else { return nil }
        return plist["ExpirationDate"] as? Date
    }

    /// Whole days from today to the date, counted in calendar days (0 = today, negative = past): the day the profile
    /// stops working is the day iOS refuses to start the app, not 24 hours after the last start.
    package static func daysLeft(to date: Date, now: Date = Date()) -> Int {
        let calendar = Calendar(identifier: .gregorian)
        return calendar.dateComponents([.day], from: calendar.startOfDay(for: now), to: calendar.startOfDay(for: date)).day ?? 0
    }

    /// Two days or fewer: re-sign before the next game session rather than after the game stops starting.
    package static func isUrgent(_ date: Date, now: Date = Date()) -> Bool { daysLeft(to: date, now: now) <= 2 }

    /// The hero's line: the date and the days left, in the app's language.
    package static func describe(_ date: Date, _ t: (String) -> String, now: Date = Date(), locale: Locale = .current) -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
        formatter.locale = locale
        let day = formatter.string(from: date)
        let days = daysLeft(to: date, now: now)
        if days < 0 { return String(format: t("dev.expiryPast"), day, -days) }
        if days == 0 { return String(format: t("dev.expiryToday"), day) }
        return String(format: t("dev.expiry"), day, days)
    }
}