-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockReader.cs
81 lines (69 loc) · 1.88 KB
/
BlockReader.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
using UnityEngine;
using System.Collections;
using System.IO;
namespace Uzu
{
public static class BlockReader
{
public static BlockFormat.Data Read (byte[] data)
{
using (MemoryStream stream = new MemoryStream (data)) {
using (BinaryReader reader = new BinaryReader (stream)) {
return ReadImpl (reader);
}
}
}
#region Implementation.
private static BlockFormat.Data ReadImpl (BinaryReader reader)
{
// Verify file integrity.
{
uint magicNumber = reader.ReadUInt32 ();
if (magicNumber != BlockFormat.MagicNumber) {
Debug.LogError ("Invalid file format (corrupt magic number): " + magicNumber);
return null;
}
}
uint version = reader.ReadUInt32 ();
// Handle support of multiple released data versions.
// Deprecate support for versions as necessary.
switch (version) {
case 0:
return ReadVersion_0 (version, reader);
default:
Debug.LogError ("Unsupported version: " + version);
break;
}
return null;
}
private static BlockFormat.Data ReadVersion_0 (uint version, BinaryReader reader)
{
BlockFormat.Header header = new BlockFormat.Header ();
{
header.version = version;
header.count = new VectorI3 (reader.ReadInt32 (), reader.ReadInt32 (), reader.ReadInt32 ());
}
BlockFormat.Data data = new BlockFormat.Data ();
{
VectorI3 xyz = header.count;
{
int totalCount = VectorI3.ElementProduct (xyz);
data._states = new bool[totalCount];
data._colors = new BlockFormat.RGB[totalCount];
}
int cnt = 0;
for (int x = 0; x < xyz.x; x++) {
for (int y = 0; y < xyz.y; y++) {
for (int z = 0; z < xyz.z; z++) {
data._states [cnt] = reader.ReadBoolean ();
data._colors [cnt] = new BlockFormat.RGB (reader.ReadByte (), reader.ReadByte (), reader.ReadByte ());
cnt++;
}
}
}
}
return data;
}
#endregion
}
}