-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuroraFile.cs
320 lines (261 loc) · 8.02 KB
/
AuroraFile.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using KotOR_Files.AuroraParsers;
using System.Diagnostics;
namespace KotOR_Files
{
public class AuroraFile
{
public enum ResourceTypes
{
NA = 0x000F,
bmp = 1,
tga = 3,
wav = 4,
plt = 6,
ini = 7,
txt = 10,
mdl = 2002,
nss = 2009,
ncs = 2010,
are = 2012,
set = 2013,
ifo = 2014,
bic = 2015,
wok = 2016,
_2da = 2017,
txi = 2022,
git = 2023,
uti = 2025,
utc = 2027,
dlg = 2029,
itp = 2030,
utt = 2032,
dds = 2033,
uts = 2035,
ltr = 2036,
gff = 2037,
fac = 2038,
ute = 2040,
utd = 2042,
utp = 2045,
dtf = 2045,
gic = 2046,
gui = 2047,
utm = 2051,
dwk = 2052,
pwk = 2053,
jrl = 2056,
sav = 2057,
utw = 2058,
ssf = 2060,
hak = 2061,
nwm = 2062,
bik = 2063,
ptm = 2065,
ptt = 2066,
lyt = 3000,
vis = 3001,
rim = 3002,
pth = 3003,
lip = 3004,
bwm = 3005,
txb = 3006,
tpc = 3007,
mdx = 3008,
rsv = 3009,
sig = 3010,
xbx = 3011,
erf = 9997,
bif = 9998,
key = 9999
}
private FileStream fileStream;
private MemoryStream memoryStream;
private BinaryReader Reader;
private StreamReader StreamReader;
private byte[] bytes;
private String name = "";
private String ext = "";
private String path = null;
private Encoding encoding = Encoding.ASCII;
public Boolean isText = false;
public AuroraFile(String path)
{
this.ext = Path.GetExtension(path);
this.name = Path.GetFileNameWithoutExtension(path);
this.path = path;
}
public AuroraFile(byte[] bytes, String name, int restype)
{
this.bytes = bytes;
this.ext = "."+((ResourceTypes)restype).ToString();
this.name = name;
Debug.WriteLine(this.ext);
}
public String getFilename()
{
return this.name;
}
public String getExt()
{
return this.ext;
}
public String getPath()
{
return this.path;
}
public void Open()
{
if(this.path != null)
{
fileStream = new FileStream(this.getPath(), FileMode.Open);
if(isText)
StreamReader = new StreamReader(fileStream, encoding);
else
Reader = new BinaryReader(fileStream, encoding);
}
else
{
memoryStream = new MemoryStream(bytes);
Reader = new BinaryReader(memoryStream, encoding);
}
}
public void setEncoding(Encoding encoding)
{
this.encoding = encoding;
}
public void Close()
{
if (this.path != null)
{
fileStream.Dispose();
if(isText)
StreamReader.Dispose();
else
Reader.Dispose();
}
else
{
memoryStream.Dispose();
Reader.Dispose();
}
}
public FileStream getFileStream()
{
return fileStream;
}
public MemoryStream getMemoryStream()
{
return memoryStream;
}
public BinaryReader getReader()
{
return Reader;
}
public StreamReader getStreamReader()
{
return StreamReader;
}
public byte[] getContents()
{
return bytes;
}
public void Export(String export_dir)
{
if (bytes != null)
{
FileStream fs = new FileStream(Path.Combine(export_dir, getFilename() + getExt()), FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
}
public void Export(String export_dir, String filename)
{
if (bytes != null)
{
FileStream fs = new FileStream(Path.Combine(export_dir, filename + getExt()), FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
}
public void Dispose()
{
bytes = null;
fileStream = null;
memoryStream = null;
Reader = null;
}
public static void ReadValue(BinaryReader stream, out UInt32 value)
{
value = stream.ReadUInt32();
}
public static void ReadValue(BinaryReader stream, out float value)
{
value = stream.ReadSingle();
}
/*public static void ReadArray(BinaryReader stream,
UInt32 offset, UInt32 count, out values)//std::vector<T>
{
}*/
public static void ReadArray(BinaryReader stream,
UInt32 offset, UInt32 count, ref UInt32[] values)//vector<UInt32>
{
long pos = stream.BaseStream.Position;
stream.BaseStream.Position = offset;
//values.resize(count);
for (UInt32 i = 0; i < count; i++)
{
UInt32 val;
AuroraFile.ReadValue(stream, out val);
Debug.WriteLine("ReadValue: " + val);
values[i] = val;
}
stream.BaseStream.Position = pos;
}
public static void ReadArray(BinaryReader stream, UInt32 offset, UInt32 count, ref float[] values)//vector<float>
{
long pos = stream.BaseStream.Position;
stream.BaseStream.Position = offset;
//values.resize(count);
for (UInt32 i = 0; i < count; i++)
AuroraFile.ReadValue(stream, out values[i]);
stream.BaseStream.Position = pos;
}
//Gets the Array Offset & Item Count
public static void ReadArrayDef(BinaryReader stream, out UInt32 offset, out UInt32 count)
{
offset = stream.ReadUInt32();
UInt32 usedCount = stream.ReadUInt32();
UInt32 allocatedCount = stream.ReadUInt32();
if (usedCount != allocatedCount)
throw new Exception("Model::readArrayDef(): usedCount != allocatedCount ("+ usedCount + ", "+ allocatedCount + ")");
count = usedCount;
}
public static void readStrings(BinaryReader stream, uint[] offsets, uint offset, out string[] strings) {
long pos = stream.BaseStream.Position;
strings = new string[offsets.Length];
Debug.WriteLine("Reading Strings");
for (int i = 0; i != offsets.Length; i++)
{
stream.BaseStream.Position = offset + offsets[i];
string tmpName = "";
char c;
while ((int)(c = stream.ReadChar()) != 0)
tmpName = tmpName + c;
strings[i] = tmpName;
//Debug.WriteLine(tmpName);
//strings.push_back(Common::readString(mdl, Common::kEncodingASCII));
}
stream.BaseStream.Position = pos;
}
}
}