-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mix.cs
364 lines (349 loc) · 13.3 KB
/
Mix.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
//using System.Xml;
using Bitmap = System.Drawing.Bitmap;
namespace Noxico
{
public static class Mix
{
private class MixFileEntry
{
public string MixFile, Filename;
public int Offset, Length;
public bool IsCompressed;
}
private static Dictionary<string, MixFileEntry> fileList;
private static Dictionary<string, string> stringCache;
private static Dictionary<string, Bitmap> bitmapCache;
/// <summary>
/// Populates the Mix database for usage
/// </summary>
/// <param name="mainFile">The primary archive's base name.</param>
public static void Initialize(string mainFile = "Noxico")
{
Program.WriteLine("Mix.Initialize()");
fileList = new Dictionary<string, MixFileEntry>();
stringCache = new Dictionary<string, string>();
bitmapCache = new Dictionary<string, Bitmap>();
var mixfiles = new List<string>() { mainFile + ".nox" };
mixfiles.AddRange(Directory.EnumerateFiles(".", "*.nox").Select(x => x.Substring(2)).Where(x => !x.Equals(mainFile + ".nox", StringComparison.OrdinalIgnoreCase)));
Program.WriteLine("Mixfiles enumerated. Indexing contents...");
foreach (var mixfile in mixfiles)
{
var mixHash = mixfile.GetHashCode();
if (!File.Exists(mixfile))
{
Program.WriteLine("Mixfile \"{0}\" in list but nonexistent.", mixfile);
continue;
}
using (var mStream = new BinaryReader(File.Open(mixfile, FileMode.Open)))
{
//This is not the "proper" way to do it. Fuck that.
while (true)
{
var header = mStream.ReadBytes(4);
if (header[0] != 'P' || header[1] != 'K' || header[2] != 3 || header[3] != 4)
{
if (header[2] == 1 && header[3] == 2) //reached the Central Directory
break;
throw new FileLoadException(string.Format("MIX file '{0}' has an incorrect header.", mixfile));
}
mStream.BaseStream.Seek(4, SeekOrigin.Current);
var method = mStream.ReadInt16();
mStream.BaseStream.Seek(8, SeekOrigin.Current);
var moto = mStream.ReadBytes(4);
var compressedSize = (moto[3] << 24) | (moto[2] << 16) | (moto[1] << 8) | moto[0]; //0x000000F8
moto = mStream.ReadBytes(4);
var uncompressedSize = (moto[3] << 24) | (moto[2] << 16) | (moto[1] << 8) | moto[0]; //0x00000197
moto = mStream.ReadBytes(2);
var filenameLength = (moto[1] << 8) | moto[0];
mStream.BaseStream.Seek(2, SeekOrigin.Current);
var filename = new string(mStream.ReadChars(filenameLength)).Replace('/', '\\');
var offset = (int)mStream.BaseStream.Position;
mStream.BaseStream.Seek(compressedSize, SeekOrigin.Current);
if (filename.EndsWith("\\"))
continue;
if (filename.Contains("missions"))
continue;
var entry = new MixFileEntry()
{
Offset = offset,
Length = compressedSize,
IsCompressed = method == 8,
Filename = filename,
MixFile = mixfile,
};
if (filename.EndsWith(".patch"))
filename = mixHash + "#" + filename;
fileList[filename] = entry;
}
}
}
if (!Directory.Exists("mods"))
return;
foreach (var dataDir in Directory.EnumerateDirectories("mods"))
{
foreach (var dataFile in Directory.EnumerateFiles(dataDir, "*.*", SearchOption.AllDirectories))
{
var baseName = dataFile.Remove(0, dataDir.Length + 1);
var entry = new MixFileEntry()
{
Offset = -1,
Length = -1,
IsCompressed = false,
Filename = dataFile,
MixFile = null,
};
if (baseName.EndsWith(".patch"))
baseName = dataFile;
fileList[baseName] = entry;
}
}
}
/// <summary>
/// Checks whether a given file exists, be it in the data folder or in any loaded archives.
/// </summary>
/// <param name="fileName">The file to check for.</param>
/// <returns>True if the file exists.</returns>
public static bool FileExists(string fileName)
{
//if (File.Exists(Path.Combine("data", fileName)))
// return true;
return (fileList.ContainsKey(fileName));
}
/// <summary>
/// Looks up the given file in the Mix database and returns a <see cref="MemoryStream"/> for it.
/// </summary>
/// <param name="fileName">The file to find.</param>
/// <returns>Returns a <see cref="MemoryStream"/> if found, null otherwise.</returns>
public static Stream GetStream(string fileName)
{
Program.WriteLine("Mix.GetStream({0})", fileName);
//if (File.Exists(Path.Combine("data", fileName)))
// return new MemoryStream(File.ReadAllBytes(Path.Combine("data", fileName)));
if (!fileList.ContainsKey(fileName))
throw new FileNotFoundException("File " + fileName + " was not found in the MIX files.");
MemoryStream ret;
var entry = fileList[fileName];
if (entry.MixFile == null)
return new MemoryStream(File.ReadAllBytes(entry.Filename));
using (var mStream = new BinaryReader(File.Open(entry.MixFile, FileMode.Open)))
{
mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
ret = new MemoryStream(mStream.ReadBytes(entry.Length));
}
return ret;
}
/// <summary>
/// Looks up the given file in the Mix database and returns its contents as a <see cref="string"/>.
/// </summary>
/// <param name="fileName">The file to find.</param>
/// <param name="cache">If true, stores the resulting string in cache for quicker reuse.</param>
/// <returns>Returns a <see cref="string"/> with the file's contents if found, null otherwise.</returns>
public static string GetString(string fileName, bool cache = true)
{
if (cache && stringCache.ContainsKey(fileName))
return stringCache[fileName];
Program.WriteLine("Mix.GetString({0})", fileName);
//if (File.Exists(Path.Combine("data", fileName)))
// return File.ReadAllText(Path.Combine("data", fileName));
if (!fileList.ContainsKey(fileName))
throw new FileNotFoundException("File " + fileName + " was not found in the MIX files.");
var bytes = GetBytes(fileName);
var ret = Encoding.UTF8.GetString(bytes);
if (cache)
stringCache[fileName] = ret;
return ret;
}
/// <summary>
/// Looks up the given file in the Mix database and returns its contents as a token tree.
/// </summary>
/// <param name="fileName">The file to find.</param>
/// <param name="cache">Should the original file contents be cached?</param>
/// <returns>Returns a token tree representing the file's contents.</returns>
public static List<Token> GetTokenTree(string fileName, bool cache = false)
{
var tml = GetString(fileName, cache);
var carrier = new TokenCarrier();
carrier.Tokenize(tml);
var patches = fileList.Keys.Where(e => e.EndsWith(fileName + ".patch")).ToList();
/* if (Directory.Exists("data"))
{
var externals = Directory.GetFiles("data", "*.tml.patch", SearchOption.AllDirectories).Select(e => e.Substring(5)).Where(e => e.EndsWith(fileName + ".patch"));
patches.AddRange(externals);
} */
if (patches.Count > 0)
{
Program.WriteLine("{0} has patches!", fileName);
foreach (var p in patches)
{
var patch = GetString(p);
carrier.Patch(patch);
}
}
return carrier.Tokens;
}
/// <summary>
/// Looks up the given file in the Mix database and returns its contents as a <see cref="Bitmap"/>.
/// </summary>
/// <param name="fileName">The file to find.</param>
/// <returns>Returns a <see cref="Bitmap"/> with the file's contents if found, null otherwise.</returns>
public static Bitmap GetBitmap(string fileName, bool cache = true)
{
if (cache && bitmapCache.ContainsKey(fileName))
return bitmapCache[fileName];
Program.WriteLine("Mix.GetBitmap({0})", fileName);
//if (File.Exists(Path.Combine("data", fileName)))
// return new Bitmap(Path.Combine("data", fileName));
var raw = GetBytes(fileName);
using (var str = new MemoryStream(raw))
{
var ret = (Bitmap)Bitmap.FromStream(str);
if (cache)
bitmapCache[fileName] = ret;
return ret;
}
}
/// <summary>
/// Looks up the given file in the Mix database and returns its contents as a <see cref="Byte"/> array.
/// </summary>
/// <param name="fileName">The file to find.</param>
/// <returns>Returns a <see cref="Byte"/> array with the file's contents if found, null otherwise.</returns>
public static byte[] GetBytes(string fileName)
{
Program.WriteLine("Mix.GetBytes({0})", fileName);
//if (File.Exists(Path.Combine("data", fileName)))
// return File.ReadAllBytes(Path.Combine("data", fileName));
if (!fileList.ContainsKey(fileName))
throw new FileNotFoundException("File " + fileName + " was not found in the MIX files.");
byte[] ret;
var entry = fileList[fileName];
if (entry.MixFile == null)
return File.ReadAllBytes(entry.Filename);
using (var mStream = new BinaryReader(File.Open(entry.MixFile, FileMode.Open)))
{
mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
if (!entry.IsCompressed)
{
ret = mStream.ReadBytes(entry.Length);
}
else
{
var cStream = new MemoryStream(mStream.ReadBytes(entry.Length));
var decompressor = new System.IO.Compression.DeflateStream(cStream, System.IO.Compression.CompressionMode.Decompress);
var outStream = new MemoryStream();
var buffer = new byte[1024];
var recieved = 1;
while (recieved > 0)
{
recieved = decompressor.Read(buffer, 0, buffer.Length);
outStream.Write(buffer, 0, recieved);
}
ret = new byte[outStream.Length];
outStream.Seek(0, SeekOrigin.Begin);
outStream.Read(ret, 0, ret.Length);
}
}
return ret;
}
/// <summary>
/// Returns a list of all the files in a given path.
/// </summary>
/// <param name="path">The path to look in.</param>
/// <returns>A string array with all the files found, which may be empty.</returns>
public static string[] GetFilesInPath(string path)
{
var ret = new List<string>();
foreach (var entry in fileList.Values.Where(x => x.Filename.StartsWith(path)))
ret.Add(entry.Filename);
/* if (Directory.Exists(Path.Combine("data", path)))
{
var getFiles = Directory.GetFiles(Path.Combine("data", path), "*", SearchOption.AllDirectories);
ret.AddRange(getFiles.Select(x => x.Substring("data\\".Length)).Where(x => !ret.Contains(x)));
} */
return ret.ToArray();
}
public static void GetFileRange(string fileName, out int offset, out int length, out string mixFile)
{
offset = -1;
length = -1;
mixFile = string.Empty;
if (!fileList.ContainsKey(fileName))
return;
var entry = fileList[fileName];
offset = entry.Offset;
length = entry.Length;
mixFile = entry.MixFile;
}
public static string[] GetFilesWithPattern(string pattern)
{
var ret = new List<string>();
var regex = new System.Text.RegularExpressions.Regex(pattern.Replace("*", "(.*)").Replace("\\", "\\\\"));
foreach (var entry in fileList)
{
if (regex.IsMatch(entry.Value.Filename))
ret.Add(entry.Key);
}
//foreach (var entry in fileList.Values.Where(x => regex.IsMatch(x.Filename)))
// ret.Add(entry.Filename);
/* if (Directory.Exists("data"))
{
if (pattern.Contains('\\'))
{
if (!Directory.Exists(Path.Combine("data", Path.GetDirectoryName(pattern))))
return ret.ToArray();
}
var getFiles = Directory.GetFiles("data", pattern, SearchOption.AllDirectories);
ret.AddRange(getFiles.Select(f => f.Substring(5)).Where(f => !ret.Contains(f)));
} */
return ret.ToArray();
}
public static void SpreadEm()
{
Program.WriteLine("Spreadin' em...");
if (fileList == null || fileList.Count == 0)
Initialize();
foreach (var entry in fileList.Values)
{
if (entry.Length == 0)
{
Program.WriteLine("* Bogus entry with zero length: \"{0}\", offset {1}", entry.Filename, entry.Offset);
continue;
}
var targetPath = Path.Combine("data", /* entry.MixFile.Remove(entry.MixFile.Length - 4),*/ entry.Filename);
var targetDir = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
using (var mStream = new BinaryReader(File.Open(entry.MixFile, FileMode.Open)))
{
mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
if (!entry.IsCompressed)
{
File.WriteAllBytes(targetPath, mStream.ReadBytes(entry.Length));
}
else
{
var cStream = new MemoryStream(mStream.ReadBytes(entry.Length));
var decompressor = new System.IO.Compression.GZipStream(cStream, System.IO.Compression.CompressionMode.Decompress);
var outStream = new MemoryStream();
var buffer = new byte[1024];
var recieved = 1;
while (recieved > 0)
{
recieved = decompressor.Read(buffer, 0, buffer.Length);
outStream.Write(buffer, 0, recieved);
}
var unpacked = new byte[outStream.Length];
outStream.Seek(0, SeekOrigin.Begin);
outStream.Read(unpacked, 0, unpacked.Length);
File.WriteAllBytes(targetPath, unpacked);
}
}
}
Program.WriteLine("All entries in mix files extracted. Happy Hacking.");
}
}
}