This repository was archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGame.cs
284 lines (254 loc) · 10.4 KB
/
Game.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
using dnlib.DotNet;
using Il2CppInspector;
using Il2CppInspector.Cpp;
using Il2CppInspector.Reflection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using Unitor.Core.Assets;
using Unitor.Core.Reflection;
namespace Unitor.Core
{
public class Game : IDisposable
{
public string Name { get; set; }
public string Hash { get; set; }
public string GameDir { get; set; }
public string VisualName { get; set; }
public string Developer { get; set; }
public string Version { get; set; }
public BackendInfo ScriptingBackend { get; set; }
public PackingInfo Packing { get; set; }
public ObfuscationInfo Obfuscation { get; set; }
public AntiCheatInfo AntiCheat { get; set; }
public UnitorModel Model { get; set; }
public AssetModel AssetModel { get; set; }
public Game()
{
}
public Game(string path, EventHandler<string> statusCallback)
{
List<string> files = Directory.GetFiles(path).ToList();
List<string> dirs = Directory.GetDirectories(path).ToList();
string gameName = files.Where(f => Path.GetExtension(f) == ".exe" && dirs.Select(d => Path.GetFileName(d)).Contains($"{Path.GetFileNameWithoutExtension(f)}_Data")).FirstOrDefault();
if (string.IsNullOrEmpty(gameName))
{
throw new ArgumentException("Could not find game executable and Data folder pair.");
}
Name = Path.GetFileNameWithoutExtension(gameName);
var appInfo = File.ReadLines($@"{path}\{Name}_Data\app.info").ToList();
if (appInfo.Count != 2)
{
throw new ArgumentException("Malformed app.info file found in Data folder.");
}
Developer = appInfo[0].Length > 14 ? appInfo[0].Substring(0, 11) + "..." : appInfo[0];
VisualName = appInfo[1].Length > 14 ? appInfo[1].Substring(0, 11) + "..." : appInfo[1];
Version = Helpers.FromAssetFile($@"{path}\{Name}_Data\globalgamemanagers.assets").ToString();
ScriptingBackend = BackendInfo.FromPath(path, Name, statusCallback);
var binarypath = "";
if(ScriptingBackend.Def == BackendDef.Il2Cpp)
{
binarypath = files.First(f => Path.GetFileName(f) == "GameAssembly.dll");
}
else
{
binarypath = @$"{path}\{Name}_Data\Managed\Assembly-CSharp.dll";
}
//using (var cryptoProvider = new SHA1CryptoServiceProvider())
//{
// FileStream fs = File.Open(binarypath, FileMode.Open);
// Hash = BitConverter.ToString(cryptoProvider.ComputeHash(fs));
//}
Model = ScriptingBackend.Model;
Obfuscation = new ObfuscationInfo(Model);
Packing = new PackingInfo(ScriptingBackend.Il2CppStream);
AntiCheat = new AntiCheatInfo(Model);
statusCallback.Invoke(this, "Creating asset model");
//AssetModel = new AssetModel($@"{path}\{Name}_Data");
}
public void Dispose()
{
Model = null;
ScriptingBackend.Dispose();
}
}
public enum BackendDef
{
Il2Cpp,
Mono
}
public class BackendInfo : IDisposable
{
public string Name { get; set; }
public string Compiler { get; set; }
public string Version { get; set; }
public BackendDef Def { get; set; }
public UnitorModel Model { get; set; }
public IFileFormatStream Il2CppStream { get; set; }
private BackendInfo(BackendDef def, CppCompilerType? compiler, string version, UnitorModel model, IFileFormatStream stream)
{
Def = def;
Compiler = compiler.HasValue ? " - " + compiler.ToString() : "";
Name = def.ToString() + Compiler;
Version = version;
Model = model;
List<string> nspaces = new List<string>();
foreach (string nspace in model.Namespaces)
{
if (Model.Types.Any(t => t.Namespace == nspace))
{
nspaces.Add(nspace);
}
}
nspaces[0] = "<root>";
Model.Namespaces.Clear();
Model.Namespaces.AddRange(nspaces);
Il2CppStream = stream;
}
public static BackendInfo FromPath(string path, string name, EventHandler<string> statusCallback)
{
List<string> files = Directory.GetFiles(path).ToList();
BackendDef def = files.Any(f => Path.GetFileName(f) == "GameAssembly.dll") ? BackendDef.Il2Cpp : BackendDef.Mono;
switch (def)
{
case BackendDef.Il2Cpp:
string metapath = @$"{path}\{name}_Data\il2cpp_data\Metadata\global-metadata.dat";
string binarypath = files.First(f => Path.GetFileName(f) == "GameAssembly.dll");
statusCallback.Invoke(null, "Loading Il2Cpp metadata");
PluginManager.Enabled = false;
Metadata metadata = Metadata.FromStream(new MemoryStream(File.ReadAllBytes(metapath)));
statusCallback.Invoke(null, "Loading Il2Cpp binary");
IFileFormatStream stream = FileFormatStream.Load(new FileStream(binarypath, FileMode.Open, FileAccess.Read, FileShare.Read));
statusCallback.Invoke(null, "Creating Il2Cpp Type model");
var il2cpp = Il2CppInspector.Il2CppInspector.LoadFromStream(stream, metadata);
TypeModel typeModel = new TypeModel(il2cpp[0]);
statusCallback.Invoke(null, "Creating universal model");
UnitorModel model = UnitorModel.FromTypeModel(typeModel, statusCallback);
string version = il2cpp[0].Version.ToString();
statusCallback.Invoke(null, "Finding Il2Cpp informtion");
return new BackendInfo(def, CppCompiler.GuessFromImage(il2cpp[0].BinaryImage), version, model, stream);
case BackendDef.Mono:
statusCallback.Invoke(null, "Loading Assembly-CSharp.dll");
if (!File.Exists(@$"{path}\{name}_Data\Managed\Assembly-CSharp.dll"))
{
throw new ArgumentException("Cannot find Assembly-CSharp.dll");
}
ModuleContext modCtx = ModuleDef.CreateModuleContext();
ModuleDefMD module = ModuleDefMD.Load(@$"{path}\{name}_Data\Managed\Assembly-CSharp.dll", modCtx);
statusCallback.Invoke(null, "Loading Assembly-CSharp-firstpass.dll");
if (!File.Exists(@$"{path}\{name}_Data\Managed\Assembly-CSharp-firstpass.dll"))
{
throw new ArgumentException("Cannot find Assembly-CSharp-firstpass.dll");
}
ModuleContext modCtxFirstpass = ModuleDef.CreateModuleContext();
ModuleDefMD moduleFirstpass = ModuleDefMD.Load(@$"{path}\{name}_Data\Managed\Assembly-CSharp-firstpass.dll", modCtxFirstpass);
statusCallback.Invoke(null, "Creating universal model");
UnitorModel monoModel = UnitorModel.FromModuleDef(module, statusCallback);
monoModel.Add(UnitorModel.FromModuleDef(moduleFirstpass, statusCallback));
return new BackendInfo(def, null, "", monoModel, null);
default:
return new BackendInfo(def, null, "", null, null);
}
}
public void Dispose()
{
Model.TypeModel = null;
Model.AppModel = null;
Model.Types.Clear();
Model = null;
Il2CppStream = null;
}
}
public enum PackingDef
{
VMProtect,
Themida,
UPX,
None
}
public class PackingInfo
{
public string Name { get; set; }
public bool Detected { get; }
public PackingDef Def { get; set; }
public PackingInfo(IFileFormatStream stream)
{
if (stream == null)
{
Def = PackingDef.None;
Name = Def.ToString();
Detected = Def != PackingDef.None;
return;
}
List<Section> sections = stream.GetSections().ToList();
Def = PackingDef.None;
if (sections.Any(s => s.Name.Equals(".vmp3")))
{
Def = PackingDef.VMProtect;
}
else if (sections.Any(s => s.Name.Equals(".themida")))
{
Def = PackingDef.Themida;
}
else if (sections.Any(s => s.Name.Equals(".upx")))
{
Def = PackingDef.UPX;
}
Name = Def.ToString();
Detected = Def != PackingDef.None;
}
}
public enum AntiCheatDef
{
BattleEye,
EasyAntiCheat,
Riot,
CodeStage,
Custom,
None
}
public class AntiCheatInfo
{
public string Name { get; set; }
public bool Detected { get; }
public AntiCheatDef Def { get; set; }
public AntiCheatInfo(UnitorModel lookupModel)
{
Def = AntiCheatDef.None;
if (lookupModel.Namespaces.Contains("CodeStage.AntiCheat.Common"))
{
Def = AntiCheatDef.CodeStage;
}
if (lookupModel.Namespaces.Contains("Shared.Antitamper"))
{
Def = AntiCheatDef.Riot;
}
if (lookupModel.Namespaces.Contains("BattlEye"))
{
Def = AntiCheatDef.BattleEye;
}
Name = Def.ToString();
Detected = Def != AntiCheatDef.None;
}
}
public enum ObfuscatorDef
{
Beebyte,
Custom,
None
}
public class ObfuscationInfo
{
public string Name { get; }
public bool Detected { get; }
public ObfuscatorDef Def { get; set; }
public ObfuscationInfo(UnitorModel module)
{
Def = module.Namespaces.Contains("Beebyte.Obfuscator") ? ObfuscatorDef.Beebyte : ObfuscatorDef.None;
Name = Def.ToString();
Detected = Def != ObfuscatorDef.None;
}
}
}