diff --git a/SilicaViewer/Document.swift b/SilicaViewer/Document.swift index c8bc3ab..93ea89d 100644 --- a/SilicaViewer/Document.swift +++ b/SilicaViewer/Document.swift @@ -2,6 +2,7 @@ import Cocoa import ZIPFoundation import CoreFoundation import Accelerate +import CoreMedia struct SilicaChunk { var x: Int = 0 @@ -86,6 +87,10 @@ class Document: NSDocument { self.addWindowController(windowController) } + override class func canConcurrentlyReadDocuments(ofType: String) -> Bool { + return ofType == "com.procreate" + } + /* Pass in an object from the $object array, which always contains a $class key. */ @@ -478,24 +483,50 @@ class Document: NSDocument { parseSilicaDocument(archive: archive, dict: topObjectClass) } } + + struct SilicaParsingError: Error, LocalizedError { + enum Kind { + case invalid + } + + let kind: Kind + let filename: URL? + + public var errorDescription: String? { + switch self.kind { + case .invalid: + return filename!.lastPathComponent + " is an invalid Silica Document." + } + } + } + + func throwError(_ error: SilicaParsingError.Kind) { + DispatchQueue.main.sync { + let _ = presentError(SilicaParsingError(kind: error, filename: fileURL)) + } + } override func read(from data: Data, ofType typeName: String) throws { self.data = data - guard let archive = Archive(data: data, accessMode: Archive.AccessMode.read) else { + guard let archive = Archive(data: data, accessMode: Archive.AccessMode.read) else { + throwError(.invalid) return } guard let documentEntry = archive[DocumentArchivePath] else { + throwError(.invalid) return } guard let documentData = readData(archive: archive, entry: documentEntry) else { + throwError(.invalid) return } var plistFormat = PropertyListSerialization.PropertyListFormat.binary guard let propertyList = try? PropertyListSerialization.propertyList(from: documentData, options: [], format: &plistFormat) else { + throwError(.invalid) return }