- Master recording (MasterSink ring + writer thread) and the sample recorder - CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports; platform-free parser/module/clock in core (midi_test) - Hardware keyboard and gamepads via GameController; rebindable keyboard, gamepad and MIDI CC actions; per-parameter MIDI learn - Colour themes (.szt), project bundles (.szgz), backup & restore; ZIP in core with zlib (zip_test against jar and unzip) - Persistent app settings; audio pauses in the background unless sound module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.5 KiB
Swift
72 lines
3.5 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import Foundation
|
|
|
|
/// Export and import files for the Setup tab (Foundation only).
|
|
enum SetupFiles {
|
|
/// Writes [text] to a fresh temporary folder as "<song name>.<ext>" (the name
|
|
/// the share sheet and Files show), clearing earlier exports first.
|
|
static func export(_ text: String, songName: String, ext: String) throws -> URL {
|
|
let root = FileManager.default.temporaryDirectory.appendingPathComponent("Exports", isDirectory: true)
|
|
try? FileManager.default.removeItem(at: root)
|
|
let dir = root.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
let url = dir.appendingPathComponent(fileStem(songName)).appendingPathExtension(ext)
|
|
try text.write(to: url, atomically: true, encoding: .utf8)
|
|
return url
|
|
}
|
|
|
|
/// Writes [data] to a fresh temporary folder as "<name>.<ext>".
|
|
static func export(_ data: Data, name: String, ext: String) throws -> URL {
|
|
let root = FileManager.default.temporaryDirectory.appendingPathComponent("Exports", isDirectory: true)
|
|
try? FileManager.default.removeItem(at: root)
|
|
let dir = root.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
let url = dir.appendingPathComponent(name).appendingPathExtension(ext)
|
|
try data.write(to: url)
|
|
return url
|
|
}
|
|
|
|
/// Reads a picked file (security-scoped), refusing anything over [limit] bytes.
|
|
static func readData(_ url: URL, limit: Int) throws -> Data {
|
|
let scoped = url.startAccessingSecurityScopedResource()
|
|
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
|
let size = (try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
|
|
guard size <= limit else { throw SetupFileError.tooLarge }
|
|
let data = try Data(contentsOf: url)
|
|
guard data.count <= limit else { throw SetupFileError.tooLarge }
|
|
return data
|
|
}
|
|
|
|
/// The song name as a file name: path separators replaced, blank → "untitled".
|
|
static func fileStem(_ name: String) -> String {
|
|
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let safe = trimmed.components(separatedBy: CharacterSet(charactersIn: "/\\:")).joined(separator: "_")
|
|
return safe.isEmpty || safe.hasPrefix(".") ? "untitled" + safe : safe
|
|
}
|
|
|
|
/// Reads a picked file as text (security-scoped, as .fileImporter hands it over).
|
|
static func readText(_ url: URL) throws -> String {
|
|
let scoped = url.startAccessingSecurityScopedResource()
|
|
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
|
let data = try Data(contentsOf: url)
|
|
// Song files are small text; refuse anything huge before decoding.
|
|
guard data.count <= 32 << 20 else { throw SetupFileError.tooLarge }
|
|
guard let text = String(data: data, encoding: .utf8) ?? String(data: data, encoding: .isoLatin1) else {
|
|
throw SetupFileError.notText
|
|
}
|
|
return text
|
|
}
|
|
}
|
|
|
|
enum SetupFileError: LocalizedError {
|
|
case tooLarge, notText
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .tooLarge: return "The file is too large for Sizzle."
|
|
case .notText: return "Couldn't read the selected file."
|
|
}
|
|
}
|
|
}
|