PulseCanvas / app / visionOS / VisionProjectInterchange.swift
| 1 | import Foundation |
| 2 | |
| 3 | @MainActor extension VisionProject { |
| 4 | /// Use the Mac's actual Codable model and stable effect identities, not a parallel schema. |
| 5 | static func openPortable(_ data: Data, library: ProjectMPresets = .shared) throws -> VisionProject { |
| 6 | if let legacy = try? JSONDecoder().decode(VisionProject.self, from: data) { return try legacy.validated() } |
| 7 | let source = try JSONDecoder().decode(CreatorProject.self, from: data).remapped(using: library) |
| 8 | guard source.version == 1, source.appearance.isValid, |
| 9 | [720, 1080, 2160].contains(source.resolution), [24, 30, 60, 120].contains(source.fps), |
| 10 | SocialAspect(rawValue: source.aspect) != nil, |
| 11 | StudioTimeline.valid(source.timeline ?? [], sceneCount: NativeEffects.all.count), |
| 12 | source.visualPresets?.allSatisfy({ NativeEffects.all.indices.contains($0.key) && $0.value.isValid }) ?? true, |
| 13 | source.bundledSoundtrack == nil || source.bundledSoundtrack == "pulsecanvas", |
| 14 | source.soundtrack.map(\.isFileURL) ?? true else { throw CocoaError(.fileReadCorruptFile) } |
| 15 | var project = VisionProject() |
| 16 | project.macProject = try JSONEncoder().encode(source) |
| 17 | project.cloudProjectID = source.cloudProjectID |
| 18 | project.exportAspect = source.aspect; project.exportResolution = source.resolution; project.exportFPS = source.fps |
| 19 | project.loopPlayback = source.loopPlayback ?? true |
| 20 | project.looks = (source.visualPresets ?? [:]).mapValues(VisionLook.init) |
| 21 | project.looks[source.scene] = VisionLook(source.appearance) |
| 22 | if let cuts = source.timeline, !cuts.isEmpty { |
| 23 | project.clips = cuts.map { VisionClip(scene: $0.scene, bars: $0.bars ?? source.barsPerSection ?? 4) } |
| 24 | project.importedTimeline = cuts |
| 25 | } else { project.clips = [VisionClip(scene: source.scene, bars: source.barsPerSection ?? 4)] } |
| 26 | if source.bundledSoundtrack == nil { |
| 27 | // A Mac sandbox bookmark cannot grant access on another device. |
| 28 | project.externalAudioURL = source.soundtrack ?? URL(fileURLWithPath: "/Missing soundtrack") |
| 29 | project.audioTitle = source.soundtrack?.deletingPathExtension().lastPathComponent ?? "Soundtrack" |
| 30 | } |
| 31 | return try project.validated() |
| 32 | } |
| 33 | |
| 34 | func portableData(duration: Double, scene: Int) throws -> Data { |
| 35 | let valid = try validated() |
| 36 | let saved = macProject.flatMap { try? JSONDecoder().decode(CreatorProject.self, from: $0) } |
| 37 | let appearance = valid.looks[scene] ?? VisionLook(scene: scene) |
| 38 | let project = CreatorProject(soundtrack: externalAudioURL, scene: scene, |
| 39 | appearance: appearance.preset, aspect: exportAspect ?? saved?.aspect ?? SocialAspect.wide.rawValue, |
| 40 | resolution: exportResolution ?? saved?.resolution ?? 1080, fps: exportFPS ?? saved?.fps ?? 60, |
| 41 | timeline: valid.segments(duration: duration), studioEnabled: true, |
| 42 | visualPresets: valid.looks.mapValues(\.preset), barsPerSection: valid.clips.first?.bars ?? 4, |
| 43 | effectIdentities: Dictionary(uniqueKeysWithValues: Set(valid.looks.keys).union(valid.clips.map(\.scene)).union([scene]) |
| 44 | .map { ($0, "native:\($0)") }), |
| 45 | bundledSoundtrack: externalAudioURL == nil ? "pulsecanvas" : nil, loopPlayback: loopPlayback, |
| 46 | cloudProjectID: cloudProjectID ?? saved?.cloudProjectID) |
| 47 | let encoder = JSONEncoder(); encoder.outputFormatting = [.prettyPrinted, .sortedKeys] |
| 48 | return try encoder.encode(project) |
| 49 | } |
| 50 | } |