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 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.
|
||||
*/
|
||||
|
@ -479,23 +484,49 @@ class Document: NSDocument {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue