-
Notifications
You must be signed in to change notification settings - Fork 0
/
deserialize.go
200 lines (167 loc) · 4.94 KB
/
deserialize.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
package archeserde
import (
"encoding/json"
"fmt"
"reflect"
"slices"
"github.com/mlange-42/arche/ecs"
)
// Deserialize an Arche [ecs.World] from JSON.
//
// The world must be prepared the following way:
// - The world must not contain any alive or dead entities (i.e. a new or [ecs.World.Reset] world)
// - All required component types must be registered using [ecs.ComponentID]
// - All required resources must be added as dummies using [ecs.AddResource]
//
// The options can be used to skip some or all components,
// entities entirely, and/or some or all resources.
// It only some components or resources are skipped,
// they still need to be registered to the world.
//
// # Query iteration order
//
// After deserialization, it is not guaranteed that entity iteration order in queries is the same as before.
// More precisely, it should at first be the same as before, but will likely deviate over time from what would
// happen when continuing the original, serialized run. Multiple worlds deserialized from the same source should,
// however, behave exactly the same.
func Deserialize(jsonData []byte, world *ecs.World, options ...Option) error {
opts := newSerdeOptions(options...)
deserial := deserializer{}
if err := json.Unmarshal(jsonData, &deserial); err != nil {
return err
}
if !opts.skipEntities {
world.LoadEntities(&deserial.World)
}
if err := deserializeComponents(world, &deserial, &opts); err != nil {
return err
}
if err := deserializeResources(world, &deserial, &opts); err != nil {
return err
}
return nil
}
func deserializeComponents(world *ecs.World, deserial *deserializer, opts *serdeOptions) error {
if opts.skipEntities {
return nil
}
infos := map[ecs.ID]ecs.CompInfo{}
ids := map[string]ecs.ID{}
allComps := ecs.ComponentIDs(world)
for _, id := range allComps {
if info, ok := ecs.ComponentInfo(world, id); ok {
infos[id] = info
ids[info.Type.String()] = id
}
}
for _, tp := range deserial.Types {
if _, ok := ids[tp]; !ok {
return fmt.Errorf("component type is not registered: %s", tp)
}
}
if len(deserial.Components) != len(deserial.World.Alive) {
return fmt.Errorf("found components for %d entities, but world has %d alive entities", len(deserial.Components), len(deserial.World.Alive))
}
skipComponents := ecs.Mask{}
for _, tp := range opts.skipComponents {
id := ecs.TypeID(world, tp)
skipComponents.Set(id, true)
}
for i, comps := range deserial.Components {
entity := deserial.World.Entities[deserial.World.Alive[i]]
mp := map[string]entry{}
if err := json.Unmarshal(comps.Bytes, &mp); err != nil {
return err
}
target := ecs.Entity{}
var targetComp ecs.ID
hasRelation := false
components := make([]ecs.Component, 0, len(mp))
compIDs := make([]ecs.ID, 0, len(mp))
for tpName, value := range mp {
if tpName == targetTag {
if err := json.Unmarshal(value.Bytes, &target); err != nil {
return err
}
continue
}
id := ids[tpName]
if skipComponents.Get(id) {
continue
}
info := infos[id]
if info.IsRelation {
targetComp = id
hasRelation = true
}
component := reflect.New(info.Type).Interface()
if err := json.Unmarshal(value.Bytes, &component); err != nil {
return err
}
compIDs = append(compIDs, id)
components = append(components, ecs.Component{
ID: id,
Comp: component,
})
}
if len(components) == 0 {
continue
}
if !hasRelation {
target = ecs.Entity{}
}
world.Add(entity, compIDs...)
for _, comp := range components {
assign(world, entity, comp.ID, comp.Comp)
}
if !target.IsZero() {
world.Relations().Set(entity, targetComp, target)
}
}
return nil
}
func assign(world *ecs.World, entity ecs.Entity, id ecs.ID, comp interface{}) {
dst := world.Get(entity, id)
rValue := reflect.ValueOf(comp).Elem()
valueType := rValue.Type()
valuePtr := reflect.NewAt(valueType, dst)
valuePtr.Elem().Set(rValue)
}
func deserializeResources(world *ecs.World, deserial *deserializer, opts *serdeOptions) error {
if opts.skipAllResources {
return nil
}
resTypes := map[ecs.ResID]reflect.Type{}
resIds := map[string]ecs.ResID{}
allRes := ecs.ResourceIDs(world)
skipResources := ecs.Mask{}
for _, id := range allRes {
if tp, ok := ecs.ResourceType(world, id); ok {
resTypes[id] = tp
resIds[tp.String()] = id
if slices.Contains(opts.skipResources, tp) {
skipResources.Set(ecs.ID(id), true)
}
}
}
for tpName, res := range deserial.Resources {
resID, ok := resIds[tpName]
if !ok {
return fmt.Errorf("resource type is not registered: %s", tpName)
}
if skipResources.Get(ecs.ID(resID)) {
continue
}
tp := resTypes[resID]
resLoc := world.Resources().Get(resID)
if resLoc == nil {
return fmt.Errorf("resource type registered but nil: %s", tpName)
}
ptr := reflect.ValueOf(resLoc).UnsafePointer()
value := reflect.NewAt(tp, ptr).Interface()
if err := json.Unmarshal(res.Bytes, &value); err != nil {
return err
}
}
return nil
}