-
Notifications
You must be signed in to change notification settings - Fork 2
/
BTBinFile.cs
226 lines (211 loc) · 6.17 KB
/
BTBinFile.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
/****************************************************************************
* *
* BK2BT - An N64 Graphics Microcode Converter *
* https://www.YouTube.com/Trenavix/ *
* Copyright (C) 2017 Trenavix. All rights reserved. *
* *
* License: *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
* *
****************************************************************************/
using System;
using System.IO;
public class BTBinFile
{
private byte[] CurrentBin;
public BTBinFile(byte[] newBin)
{
this.CurrentBin = newBin;
}
public UInt32 getGeoAddr()
{
return ReadFourBytes(0x04);
}
public UInt32 getF3DEX2SetupAddr()
{
return ReadFourBytes(0x0C);
}
public UInt32 getDLAddr()
{
return ReadFourBytes(0x0C)+0x08;
}
public void updateF3DEX2SetupAddr(UInt32 newAddr)
{
WriteFourBytes(getF3DEX2SetupAddr(), newAddr);
}
public UInt16 getTextureSetupAddr()
{
return ReadTwoBytes(0x08);
}
public UInt16 getTextureCount()
{
return ReadTwoBytes((uint)(getTextureSetupAddr()+0x04));
}
public uint getTextureDataAddr()
{
return (uint)(getTextureSetupAddr() + 0x8+(getTextureCount() * 0x10));
}
public UInt32 getVTXSetupAddr()
{
return ReadFourBytes(0x10);
}
public UInt32 getCollisionSetupAddr()
{
return ReadFourBytes(0x1C);
}
public UInt16 getVertexCount()
{
return (UInt16)((getCollisionSetupAddr() - (getVTXSetupAddr() + 0x18))/0x10);
}
public byte[][] getVTXArray()
{
byte[][] array = new byte[getVertexCount()][];
for (int i = 0; i < array.Length; i++)
{
array[i] = new byte[16];
for (int j = 0; j < 16; j++)
{
array[i][j] = getByte((uint)(getVTXSetupAddr() + 0x18+(0x10*i)+j));
}
}
return array;
}
public UInt32 getVTXAddr()
{
return ReadFourBytes(0x10)+0x18;
}
public uint F3DCommandsLength()
{
return ReadFourBytes(getF3DEX2SetupAddr());
}
public byte[] getF3DSegment()
{
byte[] F3DSeg = new byte[F3DCommandsLength()];
for (int i = 0; i < F3DCommandsLength()*8; i++)
{
F3DSeg[i] = CurrentBin[getF3DEX2SetupAddr()+8+i];
}
return F3DSeg;
}
public byte[] getCurrentBin()
{
return CurrentBin;
}
public uint getEndBinAddr()
{
return (uint)(CurrentBin.Length-1);
}
public UInt16 ReadTwoBytes(uint offset)
{
UInt16 value = getByte(offset);
for (uint i = offset; i < offset + 2; i++)
{
value = (UInt16)((value << 8) | CurrentBin[i]);
}
return value;
}
public UInt16 ReadTwoSignedBytes(uint offset)
{
return (UInt16)((getByte(offset + 1) << 8) | getByte(offset));
}
public UInt32 ReadFourBytes(uint offset)
{
UInt32 value = getByte(offset);
for (uint i = offset; i < offset + 4; i++)
{
value = (value << 8) | CurrentBin[i];
}
return value;
}
public UInt64 ReadEightBytes(uint offset)
{
UInt64 value = getByte(offset);
for (uint i = offset; i < offset + 8; i++)
{
value = (value << 8) | CurrentBin[i];
}
return value;
}
public void WriteFourBytes(uint offset, UInt32 bytes)
{
byte[] currentbyte = BitConverter.GetBytes(bytes);
for (uint i = offset; i > offset - 4; i--)
{
CurrentBin[i + 3] = currentbyte[offset - i];
}
}
public void WriteTwoBytes(uint offset, UInt16 bytes)
{
byte[] currentbyte = BitConverter.GetBytes(bytes);
for (uint i = offset; i > offset - 2; i--)
{
CurrentBin[i + 1] = currentbyte[offset - i];
}
}
public void WriteEightBytes(uint offset, UInt64 bytes)
{
byte[] currentbyte = BitConverter.GetBytes(bytes);
for (uint i = offset; i > offset - 8; i--)
{
CurrentBin[i + 7] = currentbyte[offset - i];
}
}
public byte getByte(uint offset)
{
return CurrentBin[offset];
}
public void changeByte(uint offset, byte newbyte)
{
if (offset > getEndBinAddr())
{
Array.Resize(ref CurrentBin, (int)offset + 1);
}
CurrentBin[offset] = newbyte;
}
public void copyBytes(uint srcAddr, uint destAddr, uint size)
{
byte[] tempbuffer = new byte[size];
for (uint i = 0; i < size; i++)
{
tempbuffer[i] = CurrentBin[srcAddr + i];
}
for (uint i = 0; i < size; i++)
{
changeByte(destAddr + i, tempbuffer[i]);
}
}
public void cutBytes(uint srcAddr, uint destAddr, uint size)
{
byte[] tempbuffer = new byte[size];
for (uint i = 0; i < size; i++)
{
tempbuffer[i] = CurrentBin[srcAddr + i];
}
copyBytes(srcAddr, srcAddr - size, getEndBinAddr() - srcAddr); //Copybytes backward to "cut"
copyBytes(destAddr, destAddr + size, getEndBinAddr() - size - destAddr);
for (uint i = 0; i < size; i++)
{
changeByte(destAddr + i, tempbuffer[i]);
}
}
public byte[] copyBytestoArray(uint srcAddr, uint size)
{
byte[] newarray = new byte[size];
for (int i = 0; i < size; i++)
{
newarray[i] = CurrentBin[srcAddr + i];
}
return newarray;
}
public void writeByteArray(uint offset, byte[] array)
{
for (uint i = 0; i < array.Length; i++)
{
changeByte(offset + i, array[i]);
}
}
public void changeEndBinAddr(uint newsize)
{
Array.Resize(ref CurrentBin, (int)newsize);
}
}