-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timekeeper.cs
226 lines (193 loc) · 8.65 KB
/
Timekeeper.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
using NativeFileDialogSharp;
namespace Reverse1999
{
internal class Timekeeper
{
const string VERSION = "1.0.1";
static readonly byte[] UNITYFS_ID = { 0x55, 0x6E, 0x69, 0x74, 0x79, 0x46, 0x53 };
enum OperationType
{
Decrypt,
Encrypt,
None
}
class BundleDecryptor
{
/// <summary>
/// Determine what type of operation should do with the asset bundle.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
static OperationType GetOperationType(byte[] data)
{
return data[0..7].SequenceEqual(UNITYFS_ID)
? OperationType.Encrypt
: data[0..7]
.Select((b, i) => (byte)(b ^ GenXorKey(data[0], UNITYFS_ID[0])))
.SequenceEqual(UNITYFS_ID)
? OperationType.Decrypt
: OperationType.None;
}
/// <summary>
/// Generate the XOR key.
/// The key is the XOR result of the first byte of the data with the character "U (0x55)" from "UNITYFS".
/// </summary>
/// <param name="dataFirstByte"></param>
/// <returns>XOR key.</returns>
static byte GenXorKey(byte dataFirstByte, byte key)
{
byte result = (byte)(dataFirstByte ^ key);
Console.WriteLine($"XOR Key: {dataFirstByte:X2} ^ {key:X2} = {result:X2}");
return result;
}
/// <summary>
/// Xor each data chunk with the given key.
/// </summary>
/// <param name="chunk"></param>
/// <param name="key"></param>
/// <returns>Xor-ed data.</returns>
static byte[] XorDataChunk(byte[] chunk, byte key)
{
byte[] xoredChunk = new byte[chunk.Length];
for (int i = 0; i < chunk.Length; i++)
xoredChunk[i] = (byte)(chunk[i] ^ key);
return xoredChunk;
}
static string GetOutputPath(string assetPath)
{
string directory = Path.GetDirectoryName(assetPath) ?? string.Empty;
string assetFileName = Path.GetFileNameWithoutExtension(assetPath);
if (assetFileName[^4..] == "_MOD")
return Path.Combine(
directory,
$"{assetFileName}_DEC{Path.GetExtension(assetPath)}"
);
if (assetFileName[^4..] == "_DEC")
return Path.Combine(
directory,
$"{assetFileName.Replace("_DEC", "_MOD")}{Path.GetExtension(assetPath)}"
);
return Path.Combine(
directory,
$"{assetFileName}_DEC{Path.GetExtension(assetPath)}"
);
}
static void XorBundleFile(string inputPath, string outputPath)
{
try
{
byte[] inputData = File.ReadAllBytes(inputPath);
byte key = 0;
switch (GetOperationType(inputData))
{
case OperationType.Encrypt:
Console.WriteLine("Operation: Encrypt");
string originalAssetBundle = Path.Combine(Path.GetDirectoryName(inputPath) ?? inputPath, $"{Path.GetFileName(inputPath).Split('_')[0]}{Path.GetExtension(inputPath)}");
if (!File.Exists(originalAssetBundle))
throw new Exception($"No original asset bundle found: {originalAssetBundle}");
using (
FileStream fileStream =
new(originalAssetBundle, FileMode.Open, FileAccess.Read)
)
{
key = GenXorKey(inputData[0], (byte)fileStream.ReadByte());
}
break;
case OperationType.Decrypt:
Console.WriteLine("Operation: Decrypt");
key = GenXorKey(inputData[0], UNITYFS_ID[0]);
break;
case OperationType.None:
throw new Exception("Invalid asset bundle file!");
}
if (key == 0)
return;
Console.WriteLine($"Saving asset bundle as {outputPath}.");
byte[] xoredData = XorDataChunk(inputData, key);
File.WriteAllBytes(outputPath, xoredData);
}
catch (Exception ex)
{
Console.WriteLine($"Error! {ex.Message} Skipping...");
}
}
public static (TimeSpan Duration, int FilesXored) XorBundleAssets(string[] assetPaths)
{
DateTime startTime = DateTime.Now;
int filesXored = 0;
Parallel.ForEach(
assetPaths,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
assetPath =>
{
try
{
string outputPath = GetOutputPath(assetPath);
XorBundleFile(assetPath, outputPath);
filesXored++;
}
catch { }
}
);
TimeSpan duration = DateTime.Now - startTime;
return (duration, filesXored);
}
}
static void PrintHelp()
{
Console.Title = "Reverse: 1999 - Anarchist";
Console.WriteLine(
"Reverse: 1999 - Anarchist is an asset encryptor & decryptor for Reverse: 1999 game by BLUEPOCH."
);
Console.WriteLine(
"For more information, visit: https://github.com/kiraio-moe/Reverse1999-Anarchist"
);
Console.WriteLine($"Version: {VERSION}");
Console.WriteLine(
"Asset Bundle Usages: \n\t Decrypt: Just choose the ENCRYPTED asset bundle(s) to decrypt. The output file should have '_DEC' suffix. \n\t Encrypt: The encryption process requires you to put the ORIGINAL and DECRYPTED asset bundle(s) at the same directory. The DECRYPTED asset bundle(s) file name MUST follow this rule: \"Original Name_whatever you put there\". This tool will detect a file with the name before the UNDERSCORE."
);
Console.WriteLine();
}
static void Main(string[] args)
{
PrintHelp();
string? cwd = Path.GetDirectoryName(AppContext.BaseDirectory);
string[]? assetsPath = args;
//! TODO: Arguments should be adjusted to perform decryption/encryption through CLI.
PickFile:
if (args.Length < 1)
{
Console.WriteLine(
"Press SPACE BAR to perform encryption/decryption operation, X to exit."
);
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
switch (keyInfo.Key)
{
case ConsoleKey.Spacebar:
Console.WriteLine("Opening file dialog...");
DialogResult filePicker = Dialog.FileOpenMultiple(
"dat",
Path.GetDirectoryName(BadApple.GetLastOpenedFile()) ?? cwd
);
if (filePicker.IsCancelled)
{
Console.WriteLine("Canceled.");
goto PickFile;
}
assetsPath = filePicker.Paths.ToArray();
BadApple.SaveLastOpenedFile(assetsPath[0]);
break;
case ConsoleKey.X:
return;
default:
goto PickFile;
}
}
(TimeSpan duration, int filesDecrypted) = BundleDecryptor.XorBundleAssets(assetsPath);
double rps = filesDecrypted / duration.TotalSeconds;
Console.WriteLine($"Xor-ing completed in {duration}. Rate: {rps:F2} files/sec");
Console.WriteLine();
goto PickFile;
}
}
}