-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathVoxelChangeSerializer.cs
244 lines (235 loc) · 11.4 KB
/
VoxelChangeSerializer.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
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
using System.Diagnostics.CodeAnalysis;
using LiteNetLib.Utils;
using Swihoni.Components;
using Swihoni.Util;
using Swihoni.Util.Math;
namespace Voxels
{
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "UnusedMember.Local")]
public static class VoxelChangeSerializer
{
// private delegate VoxelChange Deserializer(NetDataReader reader);
//
// private class DeserializerForVersionAttribute : Attribute
// {
// internal string[] Versions { get; }
//
// public DeserializerForVersionAttribute(params string[] versions) => Versions = versions;
// }
//
// [DeserializerForVersion("0.0.11")]
// private static VoxelChange _Deserialize(NetDataReader reader)
// {
// byte flags = reader.GetByte();
// VoxelChange change = default;
// if (FlagUtil.HasFlag(flags, 0)) change.id = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 1)) change.hasBlock = reader.GetByte() == 1;
// if (FlagUtil.HasFlag(flags, 2)) change.density = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 3)) change.orientation = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 4)) change.isBreakable = reader.GetBool();
// if (FlagUtil.HasFlag(flags, 5)) change.natural = reader.GetBool();
// if (FlagUtil.HasFlag(flags, 6)) change.color = reader.GetColor32();
// return change;
// }
//
// [DeserializerForVersion("0.0.12", "1.0.0.0")]
// private static VoxelChange __Deserialize(NetDataReader reader)
// {
// ushort flags = reader.GetUShort();
// VoxelChange change = default;
// if (FlagUtil.HasFlag(flags, 0)) change.id = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 1)) change.density = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 2)) change.orientation = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 3)) change.color = reader.GetColor32();
// if (FlagUtil.HasFlag(flags, 4)) change.hasBlock = FlagUtil.HasFlag(flags, 5);
// if (FlagUtil.HasFlag(flags, 6)) change.isBreakable = FlagUtil.HasFlag(flags, 7);
// if (FlagUtil.HasFlag(flags, 8)) change.natural = FlagUtil.HasFlag(flags, 9);
// return change;
// }
//
// [DeserializerForVersion("1.0.0.1")]
// private static VoxelChange ___Deserialize(NetDataReader reader)
// {
// ushort flags = reader.GetUShort();
// VoxelChange change = default;
// if (FlagUtil.HasFlag(flags, 0)) change.id = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 1)) change.density = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 2)) change.orientation = reader.GetByte();
// if (FlagUtil.HasFlag(flags, 3)) change.color = reader.GetColor32();
// if (FlagUtil.HasFlag(flags, 4)) change.magnitude = reader.GetFloat();
// if (FlagUtil.HasFlag(flags, 5)) change.yaw = reader.GetFloat();
// if (FlagUtil.HasFlag(flags, 6)) change.modifiesBlocks = FlagUtil.HasFlag(flags, 7);
// if (FlagUtil.HasFlag(flags, 8)) change.replaceGrassWithDirt = FlagUtil.HasFlag(flags, 9);
// if (FlagUtil.HasFlag(flags, 10)) change.hasBlock = FlagUtil.HasFlag(flags, 11);
// if (FlagUtil.HasFlag(flags, 12)) change.isBreakable = FlagUtil.HasFlag(flags, 13);
// if (FlagUtil.HasFlag(flags, 14)) change.natural = FlagUtil.HasFlag(flags, 15);
// return change;
// }
//
// private static readonly Dictionary<string, Deserializer> Deserializers
// = typeof(VoxelVersionSerializer).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
// .Where(method => method.GetCustomAttribute<DeserializerForVersionAttribute>() != null)
// .SelectMany(method => method.GetCustomAttribute<DeserializerForVersionAttribute>().Versions
// .Select(version => new {Method = method, Version = version}))
// .ToDictionary(pair => pair.Version,
// pair => (Deserializer) Delegate.CreateDelegate(typeof(Deserializer), null, pair.Method));
//
// public static VoxelChange Deserialize(NetDataReader reader, string version = null)
// {
// try
// {
// return version == null || version == Application.version ? DeserializeLatest(reader) : Deserializers[version](reader);
// }
// catch (KeyNotFoundException)
// {
// Debug.LogError($"No available way to convert save from version: {version}");
// throw;
// }
// }
public static VoxelChange Deserialize(NetDataReader reader, string version = null) => DeserializeLatest(reader);
public static void Serialize(in VoxelChange change, NetDataWriter writer)
{
var flags = 0u;
int flagPosition = writer.Length;
writer.Put(flags);
if (change.position is { } position)
{
FlagUtil.SetFlag(ref flags, 0);
Position3Int.Serialize(position, writer);
}
if (change.texture is { } texture)
{
FlagUtil.SetFlag(ref flags, 1);
writer.Put(texture);
}
if (change.density is { } density)
{
FlagUtil.SetFlag(ref flags, 2);
writer.Put(density);
}
if (change.orientation is { } orientation)
{
FlagUtil.SetFlag(ref flags, 3);
writer.Put(orientation);
}
if (change.color is { } color)
{
FlagUtil.SetFlag(ref flags, 4);
writer.PutColor32(color);
}
if (change.magnitude is { } magnitude)
{
FlagUtil.SetFlag(ref flags, 5);
writer.Put(magnitude);
}
if (change.yaw is { } yaw)
{
FlagUtil.SetFlag(ref flags, 6);
writer.Put(yaw);
}
if (change.form is { } form)
{
FlagUtil.SetFlag(ref flags, 7);
writer.Put((byte) form);
}
if (change.upperBound is { } upperBound)
{
FlagUtil.SetFlag(ref flags, 8);
Position3Int.Serialize(upperBound, writer);
}
if (change.isUndo) FlagUtil.SetFlag(ref flags, 12);
/* Flags */
if (change.isBreathable is { } isBreathable)
{
FlagUtil.SetFlag(ref flags, 16);
if (isBreathable) FlagUtil.SetFlag(ref flags, 17);
}
if (change.revert is { } revert)
{
FlagUtil.SetFlag(ref flags, 18);
if (revert) FlagUtil.SetFlag(ref flags, 19);
}
if (change.noRandom is { } noRandom)
{
FlagUtil.SetFlag(ref flags, 20);
if (noRandom) FlagUtil.SetFlag(ref flags, 21);
}
if (change.modifiesBlocks is { } modifiesBlocks)
{
FlagUtil.SetFlag(ref flags, 22);
if (modifiesBlocks) FlagUtil.SetFlag(ref flags, 23);
}
if (change.replace is { } replace)
{
FlagUtil.SetFlag(ref flags, 24);
if (replace) FlagUtil.SetFlag(ref flags, 25);
}
if (change.hasBlock is { } hasBlock)
{
FlagUtil.SetFlag(ref flags, 26);
if (hasBlock) FlagUtil.SetFlag(ref flags, 27);
}
if (change.isBreakable is { } isBreakable)
{
FlagUtil.SetFlag(ref flags, 28);
if (isBreakable) FlagUtil.SetFlag(ref flags, 29);
}
if (change.natural is { } natural)
{
FlagUtil.SetFlag(ref flags, 30);
if (natural) FlagUtil.SetFlag(ref flags, 31);
}
FastBitConverter.GetBytes(writer.Data, flagPosition, flags);
}
private static VoxelChange DeserializeLatest(NetDataReader reader)
{
uint flags = reader.GetUInt();
VoxelChange change = default;
if (FlagUtil.HasFlag(flags, 0)) change.position = Position3Int.Deserialize(reader);
if (FlagUtil.HasFlag(flags, 1)) change.texture = reader.GetByte();
if (FlagUtil.HasFlag(flags, 2)) change.density = reader.GetByte();
if (FlagUtil.HasFlag(flags, 3)) change.orientation = reader.GetByte();
if (FlagUtil.HasFlag(flags, 4)) change.color = reader.GetColor32();
if (FlagUtil.HasFlag(flags, 5)) change.magnitude = reader.GetFloat();
if (FlagUtil.HasFlag(flags, 6)) change.yaw = reader.GetFloat();
if (FlagUtil.HasFlag(flags, 7)) change.form = (VoxelVolumeForm) reader.GetByte();
if (FlagUtil.HasFlag(flags, 8)) change.upperBound = Position3Int.Deserialize(reader);
// switch (change.texture)
// {
// case 1:
// change.color = Voxel.Dirt;
// change.texture = VoxelTexture.Checkered;
// break;
// case 3:
// change.color = Voxel.Stone;
// change.texture = VoxelTexture.Checkered;
// break;
// case 2:
// change.color = Voxel.Grass;
// change.texture = VoxelTexture.Solid;
// break;
// case 4:
// change.color = Voxel.Wood;
// change.texture = VoxelTexture.Striped;
// break;
// }
// if (FlagUtil.HasFlag(flags, 4)) change.magnitude = reader.GetFloat();
// if (FlagUtil.HasFlag(flags, 5)) change.yaw = reader.GetFloat();
// if (FlagUtil.HasFlag(flags, 6)) change.form = (VoxelVolumeForm) reader.GetByte();
// /* Flags */
// if (FlagUtil.HasFlag(flags, 10)) change.hasBlock = FlagUtil.HasFlag(flags, 11);
// if (FlagUtil.HasFlag(flags, 12)) change.isBreakable = FlagUtil.HasFlag(flags, 13);
// if (FlagUtil.HasFlag(flags, 14)) change.natural = FlagUtil.HasFlag(flags, 15);
change.isUndo = FlagUtil.HasFlag(flags, 12);
if (FlagUtil.HasFlag(flags, 16)) change.isBreathable = FlagUtil.HasFlag(flags, 17);
if (FlagUtil.HasFlag(flags, 18)) change.revert = FlagUtil.HasFlag(flags, 19);
if (FlagUtil.HasFlag(flags, 20)) change.noRandom = FlagUtil.HasFlag(flags, 21);
if (FlagUtil.HasFlag(flags, 22)) change.modifiesBlocks = FlagUtil.HasFlag(flags, 23);
if (FlagUtil.HasFlag(flags, 24)) change.replace = FlagUtil.HasFlag(flags, 25);
if (FlagUtil.HasFlag(flags, 26)) change.hasBlock = FlagUtil.HasFlag(flags, 27);
if (FlagUtil.HasFlag(flags, 28)) change.isBreakable = FlagUtil.HasFlag(flags, 29);
if (FlagUtil.HasFlag(flags, 30)) change.natural = FlagUtil.HasFlag(flags, 31);
return change;
}
}
}