-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModManager.cs
52 lines (47 loc) · 1.56 KB
/
ModManager.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
using System.Reflection;
/// <summary>
/// Manages mods. Each mod is a dll that must conaint an 'Entry' class.
/// </summary>
class ModManager{
private static readonly List<Module> Modules = new List<Module>();
/// <summary>
/// The path to the dll is hardcoded for simplicity. In a real game, you would probably want to get the path differently (i.e. load all dlls from a 'Mods' folder).
/// </summary>
private const string modname = "../Mod/bin/Debug/net7.0/mod.dll";
/// <summary>
/// Loads the mod's dll
/// </summary>
public static void LoadMods(){
//Load Files
var file = new FileInfo(modname);
Assembly mod = Assembly.LoadFile(file.FullName);
//Look for Entry class
Type? entry = mod.GetType("Entry");
if(entry == null){
Console.WriteLine("Couldn't find mod entry!");
return;
}
Module module = (Module) Activator.CreateInstance(entry);
Modules.Add(module);
}
/// <summary>
/// Calls OnLoad() on all mods
/// </summary>
public static void InitMods(){
foreach(Module module in Modules){
Console.WriteLine("Added a mod");
module.OnLoad();
}
}
}
/// <summary>
/// Base class for mods. Each mod must have an 'Entry' class that inherits from this class.
/// </summary>
public abstract class Module{
/// <summary>
/// Called when the mod is loaded. Here you can add mod-related
/// </summary>
public virtual void OnLoad(){
Console.WriteLine("Loading mod...");
}
}