-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.cs
137 lines (123 loc) · 4.83 KB
/
Module.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using SDG.Framework.Modules;
using SDG.Unturned;
namespace FixLibrary;
[UsedImplicitly]
public class Module : IModuleNexus
{
private ConcurrentDictionary<string, Assembly>? _loadedAssemblies;
private List<Library>? _libraries;
private const string DiscordSupportUrl = "https://discord.gg/2yG5t869uq";
private const string GitHubUrl = "https://github.com/UnturnedCommunity/FixLibrary";
public void initialize()
{
var assembly = typeof(Module).Assembly;
var assemblyName = assembly.GetName();
CommandWindow.Log($"Loading FixLibrary... {assemblyName.Name} {assemblyName.Version} this could take for a while!");
CommandWindow.Log($"Discord Support: {DiscordSupportUrl}");
CommandWindow.Log($"GitHub: {GitHubUrl}");
var moduleDirectory = string.Empty;
var assemblyFileName = string.Empty;
foreach (var module in ModuleHook.modules)
{
// ReSharper disable once InvertIf
if (module.assemblies is { Length: > 0 } && module.assemblies[0] == assembly)
{
moduleDirectory = Path.GetFullPath(module.config.DirectoryPath);
assemblyFileName = Path.GetFileName(module.config.Assemblies[0].Path);
break;
}
}
if (string.IsNullOrEmpty(moduleDirectory))
{
throw new Exception("Failed to get FixLibrary module directory");
}
_loadedAssemblies = [];
_libraries = Directory
.GetFiles(moduleDirectory, "*.dll", SearchOption.TopDirectoryOnly)
.Where(x => !Path.GetFileName(x).Equals(assemblyFileName, StringComparison.OrdinalIgnoreCase))
.Select(GetLibrary)
.ToList();
ModuleHook.PreVanillaAssemblyResolve += UnturnedPreVanillaAssemblyResolve;
foreach (var library in _libraries)
{
if (_loadedAssemblies.ContainsKey(library.Name))
{
// Loaded on Assembly Resolve
continue;
}
CommandWindow.Log($"LOADED: {library.Name} {library.Path}");
var loadedAssembly = Assembly.Load(library.Content, library.Symbols);
_loadedAssemblies.TryAdd(loadedAssembly.GetName().Name, loadedAssembly);
}
CommandWindow.Log("Loaded FixLibrary!");
}
public void shutdown()
{
_libraries?.Clear();
_libraries = null;
_loadedAssemblies?.Clear();
_loadedAssemblies = null;
ModuleHook.PreVanillaAssemblyResolve -= UnturnedPreVanillaAssemblyResolve;
}
private static Library GetLibrary(string assemblyFilePath)
{
var fileName = Path.GetFileNameWithoutExtension(assemblyFilePath);
var symbolsFilePath = Path.ChangeExtension(assemblyFilePath, "pdb");
var symbols = File.Exists(symbolsFilePath) ? File.ReadAllBytes(symbolsFilePath) : null;
var content = File.ReadAllBytes(assemblyFilePath);
return new Library
{
Path = assemblyFilePath,
Name = fileName,
Content = content,
Symbols = symbols,
};
}
private Assembly? UnturnedPreVanillaAssemblyResolve(object sender, ResolveEventArgs args)
{
var assembly = FindLoadedAssembly(args)
?? TryLoadNotLoadedAssembly(args);
return assembly;
}
private Assembly? FindLoadedAssembly(ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
CommandWindow.Log($"FindLoadedAssembly: {assemblyName.Name}");
return _loadedAssemblies!.TryGetValue(assemblyName.Name, out var loadedAssembly)
? loadedAssembly
: null;
}
/// <summary>
/// This way we solve a problem when Library dependent on another Library.
/// And its very important to load it on first Assembly Resolve because
/// somehow if you don't resolve it first time it will break everything.
/// </summary>
private Assembly? TryLoadNotLoadedAssembly(ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
var library = _libraries!.FirstOrDefault(x =>
assemblyName.Name == x.Name);
if (library != null)
{
CommandWindow.Log($"TryLoadNotLoadedAssembly: {library.Name} {library.Path}");
var assembly = Assembly.Load(library.Content);
_loadedAssemblies!.TryAdd(assemblyName.Name, assembly);
return assembly;
}
return null;
}
private class Library
{
public string Path { get; set; }
public string Name { get; set; }
public byte[] Content { get; set; }
public byte[]? Symbols { get; set; }
}
}