9873dd258b
External-review findings for 3.1.0: - A starter rig can no longer be overwritten in place. Saving over a loaded starter previously stored the edited content under the rig's fixed factory id, so the next starter-catalog update (or Restore Starter Rigs) would silently revert it to the shipped version. The confirmation now offers only Save as New Rig / Cancel — a starter is managed content, and the player's changes are kept as their own rig (a new id) instead. - restoreStarterRigs advances the seed-version marker only on success, matching start(), so a failed manual restore no longer suppresses the next launch's automatic catch-up seed. - CHANGELOG: correct the starter-rig count (18, not 19). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
97 lines
4.9 KiB
Swift
97 lines
4.9 KiB
Swift
// RigLibraryBootstrap.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
import LyrebirdShoeGaze
|
|
|
|
/// Setup of the rig library for an app shell, split so nothing blocks the launch path.
|
|
///
|
|
/// ``makeStore()`` returns an auto-resolving store instantly (safe to construct in a synchronous
|
|
/// initializer and hand to the UI). ``start(_:seedFrom:)`` then does the blocking work — resolving
|
|
/// the iCloud-or-local location and the initial load — off the main thread, then performs a one-time
|
|
/// catch-up seed of any missing shipped starter rigs.
|
|
@MainActor
|
|
public enum RigLibraryBootstrap {
|
|
|
|
/// `UserDefaults` key holding the highest ``FactoryRigLibrary/seedVersion`` this device has
|
|
/// already seeded. Per-device (not synced): the seed is idempotent by stable rig id, so a device
|
|
/// that receives the rigs via iCloud sync simply finds them already present and seeds nothing.
|
|
private static let seedVersionKey: String = "net.realizedsound.shoegaze.factorySeedVersion"
|
|
|
|
/// Creates an auto-resolving store. Construct this synchronously at launch; call
|
|
/// ``start(_:seedFrom:)`` afterwards (e.g. from a SwiftUI `.task` or a `Task`).
|
|
public static func makeStore() -> RigLibraryStore {
|
|
RigLibraryStore()
|
|
}
|
|
|
|
/// Resolves + loads `store` off the main thread, then performs a one-time catch-up install of the
|
|
/// shipped starter rigs. Idempotent — safe to call once at every launch.
|
|
///
|
|
/// Installing (re)saves every factory rig by its stable id, so it **adds** missing starters and
|
|
/// **refreshes** existing ones to their current shipped definition (a corrected rig replaces the
|
|
/// stale copy in place, keeping its library position). User rigs are never touched — factory rigs
|
|
/// have their own fixed ids. It runs at most once per ``FactoryRigLibrary/seedVersion``: a first
|
|
/// launch installs every starter; a launch after a version bump refreshes/adds; and within a
|
|
/// version a starter the user deletes stays deleted (the marker has already advanced). This
|
|
/// deliberately replaces the old "only seed an empty library" rule so existing users get starters.
|
|
///
|
|
/// - Parameters:
|
|
/// - store: The store from ``makeStore()``.
|
|
/// - shoeGaze: The working session (currently unused by seeding; kept for call-site stability
|
|
/// and future working-rig seeding needs).
|
|
public static func start(
|
|
_ store: RigLibraryStore,
|
|
seedFrom shoeGaze: ShoeGaze
|
|
) async {
|
|
await store.start()
|
|
|
|
let defaults: UserDefaults = .standard
|
|
let seededVersion: Int = defaults.integer(forKey: seedVersionKey) // 0 when never seeded
|
|
guard seededVersion < FactoryRigLibrary.seedVersion else { return }
|
|
|
|
// Install the starters. Only advance the marker if it succeeded, so a transient failure
|
|
// (which should not happen for this deterministic data) is retried on the next launch.
|
|
if installFactoryRigs(into: store) != nil {
|
|
defaults.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
|
}
|
|
}
|
|
|
|
/// (Re)installs every shipped starter rig to its current definition. Safe to call anytime.
|
|
///
|
|
/// Drives the user-facing "Restore Starter Rigs" action: it re-adds starters the user deleted,
|
|
/// adds newly-shipped ones, and refreshes any stale copies — all without disturbing the user's own
|
|
/// rigs — and advances the seed marker so automatic catch-up won't fight the manual restore.
|
|
///
|
|
/// - Parameter store: The library to install into.
|
|
/// - Returns: The number of starter rigs written (0 on a build/encode failure).
|
|
@discardableResult
|
|
public static func restoreStarterRigs(into store: RigLibraryStore) -> Int {
|
|
// Advance the seed marker only on success, mirroring `start()` — a failed restore must not
|
|
// suppress the automatic catch-up seed on the next launch.
|
|
guard let written: Int = installFactoryRigs(into: store) else { return 0 }
|
|
UserDefaults.standard.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
|
return written
|
|
}
|
|
|
|
/// Builds every starter rig and saves it, overwriting any existing entry with the same stable id
|
|
/// (`store.save` upserts by id, keeping an overwritten entry's library position).
|
|
///
|
|
/// - Parameter store: The library to install into.
|
|
/// - Returns: The number of rigs written, or `nil` if building/encoding failed.
|
|
private static func installFactoryRigs(into store: RigLibraryStore) -> Int? {
|
|
do {
|
|
let entries: [RigEntry] = try FactoryRigLibrary.makeEntries()
|
|
for entry: RigEntry in entries {
|
|
try store.save(entry)
|
|
}
|
|
return entries.count
|
|
} catch {
|
|
print("[RigLibraryBootstrap] installing factory rigs failed: \(error)")
|
|
return nil
|
|
}
|
|
}
|
|
}
|