-
Notifications
You must be signed in to change notification settings - Fork 2
/
gob.go
312 lines (272 loc) · 6.45 KB
/
gob.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package mvm
import (
"bytes"
"encoding/gob"
"github.com/mafik/mvm/matrix"
. "github.com/mafik/mvm/vec2"
)
type Serializer interface {
Id(Gobbable) int
}
type Deserializer interface {
Get(int) Gobbable
}
type Gobbable interface {
Gob(Serializer) Gob
Connect(Deserializer, Gob)
}
type Gob interface {
Ungob() Gobbable
}
type Flattener struct {
ids map[Gobbable]int
gobbables []Gobbable
}
func (f *Flattener) Id(ble Gobbable) (id int) {
//fmt.Printf("Looking up ID for: %T %v\n", ble, ble)
id, ok := f.ids[ble]
if !ok {
id = len(f.gobbables)
//fmt.Printf("Not found - adding as %d\n", id)
f.gobbables = append(f.gobbables, ble)
f.ids[ble] = id
} else {
//fmt.Printf("Found at %d\n", id)
}
return
}
func (f *Flattener) Get(i int) Gobbable {
return f.gobbables[i]
}
func RegisterGobs() {
gob.Register(VMGob{})
gob.Register(BlueprintGob{})
gob.Register(MachineGob{})
gob.Register(FrameGob{})
gob.Register(ElementGob{})
gob.Register(ShellGob{})
for _, o := range Objects {
gob.Register(o)
}
for _, o := range Gobs {
gob.Register(o)
}
}
func Flatten(ble Gobbable) ([]byte, error) {
RegisterGobs()
f := Flattener{ids: make(map[Gobbable]int)}
f.ids[nil] = 0
f.gobbables = append(f.gobbables, nil)
f.Id(ble) // the main Gobbable is saved at 1
var gobs []Gob
gobs = append(gobs, nil)
for i := 1; i < len(f.gobbables); i++ {
b := f.gobbables[i]
//fmt.Printf("\nProcessing %d: %T %v\n", i, b, b)
gobs = append(gobs, b.Gob(&f))
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(gobs)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type Gobbables []Gobbable
func Unflatten(data []byte) (Gobbable, error) {
RegisterGobs()
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
var gobs []Gob
err := dec.Decode(&gobs)
if err != nil {
return nil, err
}
gobbables := Gobbables{}
for _, gob := range gobs {
var ble Gobbable
if gob != nil {
ble = gob.Ungob()
}
gobbables = append(gobbables, ble)
}
for i, ble := range gobbables {
//fmt.Printf("Connecting gobbable %d\n", i)
if ble != nil {
ble.Connect(gobbables, gobs[i])
}
}
return gobbables[1], nil
}
func (slice Gobbables) Get(i int) Gobbable { return slice[i] }
type VMGob struct {
ActiveIndex int
}
func (vm *VM) Gob(s Serializer) Gob {
return VMGob{ActiveIndex: s.Id(vm.root)}
}
func (gob VMGob) Ungob() Gobbable { return &VM{} }
func (vm *VM) Connect(d Deserializer, gob Gob) {
vmGob := gob.(VMGob)
vm.root = d.Get(vmGob.ActiveIndex).(*Shell)
}
type BlueprintGob struct {
Name string
Frames []int
Instances []int
Transform matrix.Matrix
Color int
}
func (blue *Blueprint) Gob(s Serializer) Gob {
gob := BlueprintGob{Name: blue.name, Transform: blue.transform, Color: blue.color}
for _, frame := range blue.frames {
gob.Frames = append(gob.Frames, s.Id(frame))
}
for instance, _ := range blue.instances {
gob.Instances = append(gob.Instances, s.Id(instance))
}
return gob
}
func (gob BlueprintGob) Ungob() Gobbable {
return &Blueprint{
name: gob.Name,
frames: nil,
instances: make(map[*Shell]bool),
transform: gob.Transform,
color: gob.Color,
}
}
func (blue *Blueprint) Connect(d Deserializer, gob Gob) {
blueGob := gob.(BlueprintGob)
blue.name = blueGob.Name
for _, i := range blueGob.Frames {
blue.frames = append(blue.frames, d.Get(i).(*Frame))
}
for _, i := range blueGob.Instances {
blue.instances[d.Get(i).(*Shell)] = true
}
}
type FrameGob struct {
Blueprint int
Pos Vec2
Size Vec2
Name string
Elems []int
Param bool
Public bool
ShowWindow bool
}
func (frame *Frame) Gob(s Serializer) Gob {
gob := FrameGob{
Blueprint: s.Id(frame.blueprint),
Pos: frame.pos,
Size: frame.size,
Name: frame.name,
Elems: nil,
Param: frame.param,
Public: frame.public,
ShowWindow: frame.ShowWindow,
}
for _, frame_element := range frame.elems {
gob.Elems = append(gob.Elems, s.Id(frame_element))
}
return gob
}
func (gob FrameGob) Ungob() Gobbable {
return &Frame{nil, gob.Pos, gob.Size, gob.Name, nil, gob.Param, gob.Public, false, gob.ShowWindow}
}
func (frame *Frame) Connect(d Deserializer, gob Gob) {
frameGob := gob.(FrameGob)
frame.blueprint = d.Get(frameGob.Blueprint).(*Blueprint)
for _, elemId := range frameGob.Elems {
frame.elems = append(frame.elems, d.Get(elemId).(*FrameElement))
}
}
type ElementGob struct {
Frame int
Name string
Target int
Stiff bool
}
func (e *FrameElement) Gob(s Serializer) Gob {
return ElementGob{
Frame: s.Id(e.frame),
Name: e.Name,
Target: s.Id(e.Target),
Stiff: e.Stiff,
}
}
func (gob ElementGob) Ungob() Gobbable {
return &FrameElement{TreeNode: TreeNode{nil, gob.Stiff}, frame: nil, Name: gob.Name}
}
func (e *FrameElement) Connect(d Deserializer, gob Gob) {
elementGob := gob.(ElementGob)
e.Target, _ = d.Get(elementGob.Target).(Target)
e.frame = d.Get(elementGob.Frame).(*Frame)
}
type MachineGob struct {
Blueprint int
Shells map[int]int
}
func (mach *Machine) Gob(s Serializer) Gob {
gob := MachineGob{Blueprint: s.Id(mach.Blueprint), Shells: make(map[int]int)}
for frame, shell := range mach.shells {
gob.Shells[s.Id(frame)] = s.Id(shell)
}
return gob
}
func (gob MachineGob) Ungob() Gobbable { return &Machine{nil, make(map[*Frame]*Shell)} }
func (m *Machine) Connect(d Deserializer, gob Gob) {
mGob := gob.(MachineGob)
m.Blueprint = d.Get(mGob.Blueprint).(*Blueprint)
for f, s := range mGob.Shells {
m.shells[d.Get(f).(*Frame)] = d.Get(s).(*Shell)
}
}
type ShellGob struct {
Parent int
Frame int
Execute bool
Object interface{}
}
func (shell *Shell) Gob(s Serializer) Gob {
gob := ShellGob{
Execute: shell.execute,
}
if gobbable, ok := shell.object.(Gobbable); ok {
gob.Object = gobbable.Gob(s)
} else {
gob.Object = shell.object
}
if shell.parent != nil {
gob.Parent = s.Id(shell.parent)
}
if shell.frame != nil {
gob.Frame = s.Id(shell.frame)
}
return gob
}
func (gob ShellGob) Ungob() Gobbable {
s := &Shell{
execute: gob.Execute,
}
if objGob, ok := gob.Object.(Gob); ok {
s.object = objGob.Ungob().(Object)
} else {
s.object = gob.Object.(Object)
}
return s
}
func (shell *Shell) Connect(d Deserializer, gob Gob) {
objGob := gob.(ShellGob)
if parent, ok := d.Get(objGob.Parent).(*Shell); ok {
shell.parent = parent
}
if frame, ok := d.Get(objGob.Frame).(*Frame); ok {
shell.frame = frame
}
if gobbable, ok := shell.object.(Gobbable); ok {
gobbable.Connect(d, objGob.Object.(Gob))
}
}