-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileIndex.cs
More file actions
180 lines (165 loc) · 5.27 KB
/
FileIndex.cs
File metadata and controls
180 lines (165 loc) · 5.27 KB
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
using System;
using System.Collections;
using System.IO;
namespace DoubleFine
{
public abstract class FileIndex
{
protected byte Uncompressed;
protected byte Compressed;
protected bool debug;
public int ContentSize;
public int FileNameOffset;
public int UnknownFlag1;
public byte UnknownFlag2;
public long Offset;
public int Size;
public byte FileTypeIndex;
public DoubleFine.FileTypeIndex FileType;
public byte CompressFlag;
public string FilePathString;
public FileIndex()
{
}
public FileIndex(BinaryReader r)
{
}
public abstract void Write(BinaryWriter w);
public virtual bool IsCompressed()
{
return ((int) this.CompressFlag & (int) this.Compressed) > 0;
}
public string GetFilePath()
{
if (Path.GetExtension(this.FilePathString).Length > 0 || this.FileType.Name == null || this.FileType.Name == "")
return this.FilePathString;
if (this.FileType.Extension == null || this.FileType.Extension.Length == 0)
return this.FilePathString + "." + this.FileType.Name;
return this.FilePathString + "." + this.FileType.Extension;
}
protected int toInt32(byte[] data, int byteoffset, int bitoffset, int length)
{
bool[] bits = this.getBits(data, byteoffset, bitoffset, length);
int num = 0;
foreach (bool flag in bits)
{
num <<= 1;
if (flag)
++num;
}
return num;
}
protected short toInt16(byte[] data, int byteoffset, int bitoffset, int length)
{
bool[] bits = this.getBits(data, byteoffset, bitoffset, length);
short num = 0;
foreach (bool flag in bits)
{
num <<= 1;
if (flag)
++num;
}
return num;
}
protected byte toByte(byte[] data, int byteoffset, int bitoffset, int length)
{
bool[] bits = this.getBits(data, byteoffset, bitoffset, length);
short num = 0;
foreach (bool flag in bits)
{
num <<= 1;
if (flag)
++num;
}
return (byte) num;
}
protected bool[] getBits(byte[] data, int byteoffset, int bitoffset, int length)
{
int num = byteoffset * 8 + bitoffset;
if (num < 0)
throw new Exception();
if (num >= data.Length * 8)
throw new Exception();
if (bitoffset < 0 || bitoffset > 7)
throw new Exception();
BitArray bitArray = new BitArray(data);
bool[] flagArray = new bool[length];
for (int index = 0; index < length; ++index)
flagArray[index] = bitArray.Get((num + index) / 8 * 8 + (7 - (num + index) % 8));
return flagArray;
}
protected void mapToData(byte[] buf, long val, int byteoffset, int bitoffset, int length)
{
byte[] bytes = BitConverter.GetBytes(val);
Array.Reverse((Array) bytes);
this.mapToData(buf, bytes, byteoffset, bitoffset, length);
}
protected void mapToData(byte[] buf, int val, int byteoffset, int bitoffset, int length)
{
byte[] bytes = BitConverter.GetBytes(val);
Array.Reverse((Array) bytes);
this.mapToData(buf, bytes, byteoffset, bitoffset, length);
}
protected void mapToData(byte[] buf, short val, int byteoffset, int bitoffset, int length)
{
byte[] bytes = BitConverter.GetBytes(val);
Array.Reverse((Array) bytes);
this.mapToData(buf, bytes, byteoffset, bitoffset, length);
}
protected void mapToData(byte[] buf, byte[] val, int byteoffset, int bitoffset, int length)
{
BitArray bitArray = new BitArray(val);
if (bitArray.Length - length < 0 || length < 0)
throw new Exception();
if (byteoffset * 8 + bitoffset < 0 || byteoffset * 8 + bitoffset + length > buf.Length * 8)
throw new Exception();
int num1 = byteoffset * 8 + bitoffset;
int num2 = val.Length * 8 - length;
for (int index1 = 0; index1 < length; ++index1)
{
if (bitArray.Get((num2 + index1) / 8 * 8 + (7 - (num2 + index1) % 8)))
{
int index2 = (num1 + index1) / 8;
switch ((num1 + index1) % 8)
{
case 0:
buf[index2] = (byte) ((uint) buf[index2] + 128U);
continue;
case 1:
buf[index2] = (byte) ((uint) buf[index2] + 64U);
continue;
case 2:
buf[index2] = (byte) ((uint) buf[index2] + 32U);
continue;
case 3:
buf[index2] = (byte) ((uint) buf[index2] + 16U);
continue;
case 4:
buf[index2] = (byte) ((uint) buf[index2] + 8U);
continue;
case 5:
buf[index2] = (byte) ((uint) buf[index2] + 4U);
continue;
case 6:
buf[index2] = (byte) ((uint) buf[index2] + 2U);
continue;
case 7:
buf[index2] = (byte) ((uint) buf[index2] + 1U);
continue;
default:
continue;
}
}
}
}
protected void debugBitData(byte[] data)
{
using (StreamWriter streamWriter = new StreamWriter("debug.log", true))
{
foreach (bool bit in this.getBits(data, 0, 0, 128))
streamWriter.Write(bit ? "1" : "0");
streamWriter.WriteLine();
}
}
}
}