Add a visible error message for invalid documents
* Also enables concurrent reading, which should be safe
This commit is contained in:
parent
b87fe7e585
commit
3fe5d9eb3a
1 changed files with 32 additions and 1 deletions
|
@ -2,6 +2,7 @@ import Cocoa
|
||||||
import ZIPFoundation
|
import ZIPFoundation
|
||||||
import CoreFoundation
|
import CoreFoundation
|
||||||
import Accelerate
|
import Accelerate
|
||||||
|
import CoreMedia
|
||||||
|
|
||||||
struct SilicaChunk {
|
struct SilicaChunk {
|
||||||
var x: Int = 0
|
var x: Int = 0
|
||||||
|
@ -86,6 +87,10 @@ class Document: NSDocument {
|
||||||
self.addWindowController(windowController)
|
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.
|
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)
|
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 {
|
override func read(from data: Data, ofType typeName: String) throws {
|
||||||
self.data = data
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let documentEntry = archive[DocumentArchivePath] else {
|
guard let documentEntry = archive[DocumentArchivePath] else {
|
||||||
|
throwError(.invalid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let documentData = readData(archive: archive, entry: documentEntry) else {
|
guard let documentData = readData(archive: archive, entry: documentEntry) else {
|
||||||
|
throwError(.invalid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var plistFormat = PropertyListSerialization.PropertyListFormat.binary
|
var plistFormat = PropertyListSerialization.PropertyListFormat.binary
|
||||||
guard let propertyList = try? PropertyListSerialization.propertyList(from: documentData, options: [], format: &plistFormat) else {
|
guard let propertyList = try? PropertyListSerialization.propertyList(from: documentData, options: [], format: &plistFormat) else {
|
||||||
|
throwError(.invalid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue