forked from zhangjiequan/AssetStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyLoader.cs
65 lines (61 loc) · 1.92 KB
/
AssemblyLoader.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
using Mono.Cecil;
using System.Collections.Generic;
using System.IO;
namespace AssetStudio
{
public class AssemblyLoader
{
public bool Loaded;
private Dictionary<string, ModuleDefinition> moduleDic = new Dictionary<string, ModuleDefinition>();
public void Load(string path)
{
var files = Directory.GetFiles(path, "*.dll");
var resolver = new MyAssemblyResolver();
var readerParameters = new ReaderParameters();
readerParameters.AssemblyResolver = resolver;
foreach (var file in files)
{
try
{
var assembly = AssemblyDefinition.ReadAssembly(file, readerParameters);
resolver.Register(assembly);
moduleDic.Add(assembly.MainModule.Name, assembly.MainModule);
}
catch
{
// ignored
}
}
Loaded = true;
}
public TypeDefinition GetTypeDefinition(string assemblyName, string fullName)
{
if (moduleDic.TryGetValue(assemblyName, out var module))
{
var typeDef = module.GetType(fullName);
if (typeDef == null && assemblyName == "UnityEngine.dll")
{
foreach (var pair in moduleDic)
{
typeDef = pair.Value.GetType(fullName);
if (typeDef != null)
{
break;
}
}
}
return typeDef;
}
return null;
}
public void Clear()
{
foreach (var pair in moduleDic)
{
pair.Value.Dispose();
}
moduleDic.Clear();
Loaded = false;
}
}
}