forked from alexguirre/EmergencyV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddonsManager.cs
104 lines (92 loc) · 3.98 KB
/
AddonsManager.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
namespace EmergencyV
{
// System
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
// RPH
using Rage;
internal class AddonsManager
{
private static AddonsManager instance;
public static AddonsManager Instance
{
get
{
if (instance == null)
instance = new AddonsManager();
return instance;
}
}
public List<Assembly> LoadedAssemblies = new List<Assembly>();
public List<API.Addon> CurrentAddons = new List<API.Addon>();
private AddonsManager()
{
}
public void LoadAddons()
{
Game.LogTrivial($"{this.GetType().Name}: Loading addons...");
string[] files = Directory.GetFiles(Plugin.AddonsFolder, "*.dll", SearchOption.TopDirectoryOnly);
UnloadAddons();
if (files.Length >= 1)
{
foreach (string file in files)
{
try
{
// may need to check if it's a valid .NET dll
//Assembly assembly = Assembly.LoadFrom(file); // LoadFrom locks the files,
Assembly assembly = Assembly.Load(File.ReadAllBytes(file));
if (!LoadedAssemblies.Contains(assembly))
LoadedAssemblies.Add(assembly);
Type[] addonsTypes = assembly.GetTypes().Where(t => !t.IsAbstract &&
t.IsSubclassOf(typeof(API.Addon))).ToArray();
Game.LogTrivial($"{this.GetType().Name}: - Loading assembly {assembly} from {file}");
if (addonsTypes.Length > 0)
{
foreach (Type t in addonsTypes)
{
Game.LogTrivial($"{this.GetType().Name}: - Creating addon instance from {t.FullName}");
API.Addon instance = (API.Addon)Activator.CreateInstance(t);
CurrentAddons.Add(instance);
Game.LogTrivial($"{this.GetType().Name}: Addon({instance.GetType().FullName}) - OnStart()");
instance.OnStart();
}
}
else
{
Game.LogTrivial($"{this.GetType().Name}: - The assembly doesn't contain any class that inherits from {nameof(API.Addon)}.");
}
}
catch (BadImageFormatException ex)
{
Game.LogTrivial($"{this.GetType().Name}: Can't load {file}, it isn't a valid .NET assembly");
Game.LogTrivial($"{this.GetType().Name}: Exception: {ex}");
}
catch (Exception ex)
{
Game.LogTrivial($"{this.GetType().Name}: Failed to load file as addon: {file}");
Game.LogTrivial($"{this.GetType().Name}: Exception: {ex}");
}
Game.LogTrivial($"{this.GetType().Name}: ");
}
Game.LogTrivial($"{this.GetType().Name}: Loaded " + CurrentAddons.Count + " addons");
}
}
public void UnloadAddons()
{
if (CurrentAddons.Count >= 1)
{
Game.LogTrivial($"{this.GetType().Name}: Unloading addons...");
foreach (API.Addon a in CurrentAddons)
{
Game.LogTrivial($"{this.GetType().Name}: Addon({a.GetType().FullName}) - OnCleanUp()");
a.OnCleanUp();
}
CurrentAddons.Clear();
}
}
}
}