Zum Inhalt

apple/PhysarumApp/Sources/Model/FactorySlots.swift

import Foundation
import PhysarumEngine

/// Loads the bundled factory manifest (`Resources/Factory/factory.json`) into a `PresetSlots.Factory`.
/// Content-agnostic: Strand ② edits the manifest, not this loader. Falls back to an empty (version-0)
/// factory if the resource is missing, so the app still launches.
enum FactorySlots {
    private struct Manifest: Decodable { var version: Int; var pinnedSlotIDs: [String]; var presets: [Preset] }

    static func load(bundle: Bundle = .main) -> PresetSlots.Factory {
        guard let url = bundle.url(forResource: "factory", withExtension: "json", subdirectory: "Factory")
                ?? bundle.url(forResource: "factory", withExtension: "json"),
              let data = try? Data(contentsOf: url),
              let m = try? JSONDecoder().decode(Manifest.self, from: data)
        else { return .init(presets: [], version: 0) }
        let presets = Array(m.presets.prefix(PresetSlots.count))   // never exceed the slot count
        return .init(presets: presets, version: m.version, pinnedSlotIDs: Set(m.pinnedSlotIDs))
    }
}