forked from BlokDust/BlokDust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serializer.ts
211 lines (153 loc) · 5.92 KB
/
Serializer.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {Effect} from './Blocks/Effect';
import {IApp} from './IApp';
import {IBlock} from './Blocks/IBlock';
import {IEffect} from './Blocks/IEffect';
import {ISource} from './Blocks/ISource';
import {SaveFile} from './SaveFile';
import {Source} from './Blocks/Source';
import {Version as Version} from './_Version';
import ObservableCollection = etch.collections.ObservableCollection;
import Point = etch.primitives.Point;
declare var App: IApp;
export class Serializer {
private static _Debug = false;
private static _SerializationDictionary: any;
private static _DeserializationDictionary: any;
public static Serialize(): string{
if (this._Debug) console.log("START SERIALIZATION");
var blocks = App.Blocks;
if (this._Debug) {
console.log("BLOCKS", blocks);
}
this._SerializationDictionary = {};
// add all block ids to the dictionary.
for (var i = 0; i < blocks.length; i++){
var b = blocks[i];
this._SerializationDictionary[b.Id] = false;
}
if (this._Debug) {
console.log("DICTIONARY", this._SerializationDictionary);
}
var json: SaveFile = <SaveFile>{
ColorThemeNo: App.ThemeManager.CurrentThemeNo,
Composition: [],
DragOffset: App.DragOffset,
Parent: (App.CompositionId) ? App.CompositionId : "",
Version: Version,
ZoomLevel: App.ZoomLevel
};
this._SerializeBlocks(json.Composition, blocks);
var result = JSON.stringify(json);
if (this._Debug) {
console.log("END SERIALIZATION", result);
}
return result;
}
private static _SerializeBlocks(list: any[], blocks: any[], parentBlock?: any): void {
for(var i = 0; i < blocks.length; i++) {
var b = this._SerializeBlock(blocks[i], parentBlock);
if (b) list.push(b);
if (this._Debug) {
console.log("DICTIONARY", this._SerializationDictionary);
console.log("SERIALIZED LIST", list);
}
}
}
private static _GetBlockSerializationType(block: IBlock): string {
return (<any>block).constructor.name;
}
private static _SerializeBlock(block: IBlock, parentBlock?: any): any {
if (this._Debug) {
console.log("SERIALIZING", block);
}
var d = this._SerializationDictionary[block.Id];
if (d) {
if (this._Debug) {
console.log("ALREADY SERIALIZED");
}
return;
}
this._SerializationDictionary[block.Id] = true;
var b: any = {};
b.Id = block.Id;
b.Type = this._GetBlockSerializationType(block);
b.Position = block.Position;
b.ZIndex = block.ZIndex;
if (block.Params) b.Params = block.Params;
// if it's a source block
if (block instanceof Source && (<ISource>block).Connections.Count){
b.Effects = [];
if (parentBlock){
b.Effects.push(parentBlock.Id);
}
this._SerializeBlocks(b.Effects, (<ISource>block).Connections.ToArray(), b);
}
// if it's an effect block
if (block instanceof Effect && (<IEffect>block).Connections.Count){
b.Sources = [];
if (parentBlock){
b.Sources.push(parentBlock.Id);
}
this._SerializeBlocks(b.Sources, (<IEffect>block).Connections.ToArray(), b);
}
return b;
}
public static Deserialize(json: string): SaveFile{
if (this._Debug) console.log("START DESERIALIZATION", json);
this._DeserializationDictionary = {};
var parsed: any = JSON.parse(json);
var saveFile = new SaveFile();
saveFile.ZoomLevel = parsed.ZoomLevel;
if (parsed.DragOffset) {
saveFile.DragOffset = new Point(parsed.DragOffset.x, parsed.DragOffset.y);
} else {
saveFile.DragOffset = new Point(0, 0);
saveFile.ZoomLevel = 1;
}
saveFile.Composition = this._DeserializeBlocks(parsed.Composition);
if (parsed.ColorThemeNo) {
saveFile.ColorThemeNo = parsed.ColorThemeNo;
} else {
saveFile.ColorThemeNo = 0;
}
return saveFile;
}
private static _DeserializeBlocks(blocks: any[]): IBlock[] {
var deserializedBlocks: IBlock[] = [];
for (var i = 0; i < blocks.length; i++) {
var b = blocks[i];
var block = this._DeserializeBlock(b);
deserializedBlocks.push(block);
}
return deserializedBlocks;
}
private static _DeserializeBlock(b: any): IBlock {
var block: IBlock;
// if it's an id and has already been deserialized, return it.
if (!(b.Id != null && b.Id.isInteger()) && Serializer._DeserializationDictionary[b]){
block = Serializer._DeserializationDictionary[b];
} else {
block = this._GetBlockDeserializationType(b);
block.Id = b.Id;
block.Position = new Point(b.Position.x, b.Position.y);
block.LastPosition = new Point(b.Position.x, b.Position.y);
block.Params = b.Params;
block.ZIndex = b.ZIndex;
Serializer._DeserializationDictionary[b.Id] = block;
}
// if it's a source block
if((<any>b).Effects){
var effects = <IEffect[]>Serializer._DeserializeBlocks(b.Effects);
(<ISource>block).Connections.AddRange(effects);
}
// if it's an effect block
if((<any>b).Sources){
var sources = <ISource[]>Serializer._DeserializeBlocks(b.Sources);
(<IEffect>block).Connections.AddRange(sources);
}
return block;
}
private static _GetBlockDeserializationType(b: any): IBlock {
return App.BlockCreator.GetBlock(b.Type);
}
}