-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockWriter.cs
91 lines (75 loc) · 1.98 KB
/
BlockWriter.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
using UnityEngine;
using System.Collections;
using System.IO;
namespace Uzu
{
public static class BlockWriter
{
public static byte[] Write (BlockContainer blocks)
{
using (MemoryStream stream = new MemoryStream ()) {
using (BinaryWriter writer = new BinaryWriter (stream)) {
WriteImpl (writer, blocks);
return stream.ToArray ();
}
}
}
#region Implementation
private static void WriteImpl (BinaryWriter writer, BlockContainer blocks)
{
BlockFormat.Header header = PrepareHeader (blocks);
BlockFormat.Data data = PrepareData (blocks);
// Embed at beginning of file.
writer.Write (BlockFormat.MagicNumber);
{
writer.Write (header.version);
writer.Write (header.count.x);
writer.Write (header.count.y);
writer.Write (header.count.z);
}
{
for (int i = 0; i < data._states.Length; i++) {
writer.Write (data._states [i]);
BlockFormat.RGB rgb = data._colors [i];
writer.Write (rgb.r);
writer.Write (rgb.g);
writer.Write (rgb.b);
}
}
}
private static BlockFormat.Header PrepareHeader (BlockContainer blocks)
{
BlockFormat.Header header = new BlockFormat.Header ();
{
header.version = BlockFormat.CURRENT_VERSION;
header.count = blocks.CountXYZ;
}
return header;
}
private static BlockFormat.Data PrepareData (BlockContainer blocks)
{
BlockFormat.Data data = new BlockFormat.Data ();
VectorI3 xyz = blocks.CountXYZ;
{
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++) {
BlockType blockType = blocks [cnt].Type;
if (blockType != BlockType.EMPTY) {
data._states [cnt] = true;
data._colors [cnt] = new BlockFormat.RGB (blocks [cnt].Color);
}
cnt++;
}
}
}
return data;
}
#endregion
}
}