-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathECS_Serializer.cs
189 lines (163 loc) · 8.09 KB
/
ECS_Serializer.cs
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using UnityEngine;
using VodeoECS.Internal;
namespace VodeoECS
{
/// <summary>
/// ECS Serializer class. For saving and loading the ECS World state as a json string
/// </summary>
public class ECS_Serializer
{
private World world;
private ScheduleQueueManager queuesManager;
private JsonSerializer serializer;
private EventEmitter<WorldLoadedEvent> eventEmitter;
/// <summary>
/// Constructs an ECS Serializer.
/// </summary>
/// <param name="world">The World to serialize.</param>
public ECS_Serializer ( World world )
{
this.world = world;
this.queuesManager = world.ScheduleQueues;
this.serializer = world.Prototypes.GetSerializer();
this.eventEmitter = world.Events.GetEmitter<WorldLoadedEvent>( null );
}
/// <summary>
/// Serializes the ECS World state to a json string.
/// </summary>
/// <returns>The serialized World state as a json string.</returns>
public string SerializeWorld ( )
{
StringBuilder sb = new StringBuilder( );
StringWriter sw = new StringWriter( sb );
JsonWriter writer = new JsonTextWriter( sw );
writer.Formatting = Formatting.Indented;
writer.WriteStartObject( );
{
writer.WritePropertyName( "ECS_Entities" );
writer.WriteStartArray( );
{
SerializedWorldData data = world.SerializeToBytes( );
serializer.Serialize( writer, this.Compress( data.entities ) );
serializer.Serialize( writer, data.nextfree );
serializer.Serialize( writer, data.recyclenext );
serializer.Serialize( writer, Time.time + world.timeOffset );
}
writer.WriteEndArray( );
NamedRegistry<Type> types = world.GetComponentTypeRegistry( );
foreach ( RegistryIndex<Type> type in types.ByIndex )
{
writer.WritePropertyName( types.GetName( type ) );
IComponentPool pool = world.GetComponentPoolDynamic( type );
SerializedPoolData bytes = pool.SerializeToBytes( );
writer.WriteStartArray( );
{
if ( bytes.filterIndices != null )
{
serializer.Serialize( writer, this.Compress( bytes.filterIndices ) );
}
if ( bytes.elementCounts != null )
{
serializer.Serialize( writer, this.Compress( bytes.elementCounts ) );
}
serializer.Serialize( writer, this.Compress( bytes.entities ) );
serializer.Serialize( writer, this.Compress( bytes.components ) );
}
writer.WriteEndArray( );
}
writer.WritePropertyName( "ECS_ScheduleQueues" );
writer.WriteStartObject( );
{
NamedRegistry<NativePriorityQueue<Entity>> queues = queuesManager.GetQueueRegistry( );
foreach ( RegistryIndex<NativePriorityQueue<Entity>> index in queues.ByIndex )
{
writer.WritePropertyName( queues.GetName( index ) );
serializer.Serialize( writer, this.Compress( queues[index].SerializeToBytes( ) ) );
}
}
writer.WriteEndObject( );
}
writer.WriteEndObject( );
return sb.ToString( );
}
/// <summary>
/// Deserializes an ECS World state from a json string.
/// </summary>
/// <param name="data">The serialized ECS World state as json string to deserialize.</param>
public void DeserializeWorld ( string data )
{
JObject json = JObject.Parse( data );
SerializedWorldData worldData = new SerializedWorldData( );
JsonReader worldReader = json["ECS_Entities"][0].CreateReader( );
worldData.entities = this.Decompress( serializer.Deserialize<byte[]>( worldReader ) );
worldData.nextfree = serializer.Deserialize<int>( json["ECS_Entities"][1].CreateReader( ) );
worldData.recyclenext = serializer.Deserialize<int>( json["ECS_Entities"][2].CreateReader( ) );
world.timeOffset = serializer.Deserialize<float>( json["ECS_Entities"][3].CreateReader( ) ) - Time.time;
world.DeserializeFromBytes( worldData );
NamedRegistry<Type> types = world.GetComponentTypeRegistry( );
foreach ( RegistryIndex<Type> type in types.ByIndex )
{
IComponentPool pool = world.GetComponentPoolDynamic( type );
SerializedPoolData bytes = new SerializedPoolData( );
JArray jarray = ( JArray )json[types.GetName( type )];
Type poolType = pool.GetType( );
if ( poolType.GetGenericTypeDefinition( ) == typeof( DataComponentPool<> ) )
{
bytes.entities = this.Decompress( serializer.Deserialize<byte[]>( jarray[0].CreateReader( ) ) );
bytes.components = this.Decompress( serializer.Deserialize<byte[]>( jarray[1].CreateReader( ) ) );
}
else if ( poolType.GetGenericTypeDefinition( ) == typeof( FilterComponentPool<> ) )
{
bytes.filterIndices = this.Decompress( serializer.Deserialize<byte[]>( jarray[0].CreateReader( ) ) );
bytes.entities = this.Decompress( serializer.Deserialize<byte[]>( jarray[1].CreateReader( ) ) );
bytes.components = this.Decompress( serializer.Deserialize<byte[]>( jarray[2].CreateReader( ) ) );
}
else if ( poolType.GetGenericTypeDefinition( ) == typeof( ListComponentPool<> ) )
{
bytes.elementCounts = this.Decompress( serializer.Deserialize<byte[]>( jarray[0].CreateReader( ) ) );
bytes.entities = this.Decompress( serializer.Deserialize<byte[]>( jarray[1].CreateReader( ) ) );
bytes.components = this.Decompress( serializer.Deserialize<byte[]>( jarray[2].CreateReader( ) ) );
}
else
{
throw new Exception( "Unknown pool type" );
}
pool.DeserializeFromBytes( bytes );
}
NamedRegistry<NativePriorityQueue<Entity>> queues = queuesManager.GetQueueRegistry( );
foreach ( RegistryIndex<NativePriorityQueue<Entity>> index in queues.ByIndex )
{
JsonReader queueReader = json["ECS_ScheduleQueues"][queues.GetName( index )].CreateReader( );
byte[] bytes = this.Decompress( serializer.Deserialize<byte[]>( queueReader ) );
queues[index].DeserializeFromBytes( bytes );
}
eventEmitter.CreateEvent( new WorldLoadedEvent( ) );
}
private byte[] Compress ( byte[] data )
{
MemoryStream stream = new MemoryStream( data.Length );
DeflateStream zipper = new DeflateStream( stream, System.IO.Compression.CompressionLevel.Fastest );
zipper.Write( data, 0, data.Length );
zipper.Dispose( );
stream.Dispose( );
return stream.ToArray( );
}
private byte[] Decompress ( byte[] data )
{
MemoryStream input = new MemoryStream( data );
MemoryStream output = new MemoryStream( data.Length );
DeflateStream unzipper = new DeflateStream( input, CompressionMode.Decompress );
unzipper.CopyTo( output );
unzipper.Dispose( );
input.Dispose( );
output.Dispose( );
return output.ToArray( );
}
}
}