-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathShaderManiaDocument.swift
75 lines (61 loc) · 2.11 KB
/
ShaderManiaDocument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// ShaderManiaDocument.swift
// Shared
//
// Created by Markus Moenig on 19/11/20.
//
import SwiftUI
import UniformTypeIdentifiers
import Combine
extension UTType {
static var shaderManiaShader: UTType {
UTType(exportedAs: "com.ShaderMania.shader")
}
}
struct ShaderManiaDocument: FileDocument {
var core = Core()
var updated = false
let exportImage = PassthroughSubject<Void, Never>()
let help = PassthroughSubject<Void, Never>()
init() {
}
static var readableContentTypes: [UTType] { [.shaderManiaShader] }
static var writableContentTypes: [UTType] { [.shaderManiaShader, .png] }
/*
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}*/
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let folder = try? JSONDecoder().decode(AssetFolder.self, from: data)
else {
throw CocoaError(.fileReadCorruptFile)
}
if data.isEmpty == false {
core.assetFolder = folder
core.assetFolder.core = core
// Make sure there is a selected asset
if core.assetFolder.assets.count > 0 {
core.assetFolder.current = core.assetFolder.assets[0]
}
}
}
/*
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = text.data(using: .utf8)!
return .init(regularFileWithContents: data)
}*/
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
var data = Data()
let encodedData = try? JSONEncoder().encode(core.assetFolder)
if let json = String(data: encodedData!, encoding: .utf8) {
data = json.data(using: .utf8)!
}
return .init(regularFileWithContents: data)
}
}