-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decompression.cs
145 lines (141 loc) · 5.04 KB
/
Decompression.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
public class Decompression
{
public static void LZ77(List<byte> cBytes, string name)
{
//Check for directory, if none then make one
if (!Directory.Exists(Environment.CurrentDirectory + "/bis/decompressed/"))
Directory.CreateDirectory(Environment.CurrentDirectory + "/bis/decompressed/");
//Init new decompressed file
FileStream decomp = new FileStream(Environment.CurrentDirectory + "/bis/decompressed/" + name, FileMode.Create);
uint dSize = 0;
uint r1 = 0;
uint currentPosition = 0;
uint endPosition = 0;
if (cBytes.Count == 0)
return;
//Read last 8 bytes of file for start position and decompressed filesize
for (int i = 1; i <= 2; i++)
{
byte[] temp = new byte[] { cBytes[cBytes.Count - (4 * i)], cBytes[cBytes.Count - (4 * i - 1)], cBytes[cBytes.Count - (4 * i - 2)], cBytes[cBytes.Count - (4 * i - 3)] };
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(temp);
}
if (i == 1)
dSize = (uint)(BitConverter.ToUInt32(temp, 0) + cBytes.Count);
else
{
r1 = BitConverter.ToUInt32(temp, 0);
currentPosition = (uint)(cBytes.Count - (r1 >> 0x18));
}
}
//Fill array first with header bytes, then all 0's
List<byte> dBytes = new List<byte>((int)dSize);
dBytes.AddRange(cBytes.GetRange(0, 0x4000));
dBytes.AddRange(Enumerable.Repeat<byte>(0x0, (int)dSize - dBytes.Count));
r1 &= 0x00FFFFFF;
//Equal to 0x4000, stops when gets to header bytes
endPosition = (uint)cBytes.Count - r1;
while (currentPosition > endPosition)
{
uint read = cBytes[(int)--currentPosition];
int r6 = 8;
loop:
if (--r6 < 0)
continue;
uint readCopy = read & 0x80;
if (readCopy != 0)
{
int temp = cBytes[(int)--currentPosition];
int temp2 = cBytes[(int)--currentPosition];
temp2 |= (temp << 8);
temp2 &= 0x0FFF;
temp2 += 2;
temp += 0x20;
while (temp >= 0)
{
byte tRead = dBytes[(int)(dSize + temp2)];
dBytes[(int)--dSize] = tRead;
temp -= 0x10;
}
}
else
{
byte temp = cBytes[(int)--currentPosition];
dBytes[(int)--dSize] = temp;
}
read <<= 1;
if (currentPosition > endPosition)
goto loop;
}
decomp.Write(dBytes.ToArray(), 0, dBytes.Count);
decomp.Close();
Console.WriteLine("Decompressed!");
}
public static void ExtractRom(string fileName)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string domain = @Environment.CurrentDirectory + @"/bis/extraction/";
startInfo.WorkingDirectory = domain;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C mdnds.exe e ../bis.nds ./decompressed";
}
else
{
//TODO
}
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
public static void BuildRom(string fileName)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
string domain = @Environment.CurrentDirectory + @"/bis/extraction/";
startInfo.WorkingDirectory = domain;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C mdnds.exe b ./decompressed ../bis.nds";
}
else
{
//TODO
}
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
public static void Decompress(string fileName, string command)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string domain = @Environment.CurrentDirectory + @"/bis/extraction/decompressed/";
startInfo.WorkingDirectory = domain;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C blz.exe {command} " + fileName;
}
else
{
//TODO
}
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
}