-
Notifications
You must be signed in to change notification settings - Fork 14
/
GenericsReader.cs
69 lines (53 loc) · 1.61 KB
/
GenericsReader.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
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace CsharpVoxReader
{
public class GenericsReader {
public static byte[] ReadByteArray(BinaryReader br, ref int readsize) {
Int32 numChars = br.ReadInt32();
readsize += sizeof(Int32) + numChars;
return br.ReadBytes(numChars);
}
public static Dictionary<string, byte[]> ReadDict(BinaryReader br, ref int readsize) {
Dictionary<string, byte[]> result = new Dictionary<string, byte[]>();
Int32 numElements = br.ReadInt32();
readsize += sizeof(Int32);
for (int i=0; i < numElements; i++) {
string key = Encoding.UTF8.GetString(ReadByteArray(br, ref readsize));
byte[] value = ReadByteArray(br, ref readsize);
result[key] = value;
}
return result;
}
public static int[] ReadRotation(BinaryReader br, ref int readsize) {
byte rot = br.ReadByte();
readsize += 1;
int r0v = ((rot & 8) == 0)?1:-1;
int r1v = ((rot & 16) == 0)?1:-1;
int r2v = ((rot & 32) == 0)?1:-1;
int r0i = rot & 3;
int r1i = (rot & 12) >> 2;
/*
Truth table for the third index
r0| 0 | 1 | 2 |
r1--+---+---+---+
0 | X | 2 | 1 |
----+---+---+---+
1 | 2 | X | 0 |
----+---+---+---+
2 | 1 | 0 | X |
----+---+---+---+
Derived function
f(r0, r1) = 3 - r0 - r1
*/
int r2i = 3 - r0i - r1i;
int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
result[r0i] = r0v;
result[r1i + 3] = r1v;
result[r2i + 6] = r2v;
return result;
}
}
}