forked from solidDoWant/Planetbase-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modloader.cs
171 lines (148 loc) · 5.93 KB
/
Modloader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace PlanetbaseFramework
{
/*
* This is the core class behind loading all mods. The patcher injects calls to loadMods() and updateMods() into the native PB code, which then
* calls the methods in this file. This allows for minimal changes to PB's native code, while still allowing it to be extended.
*/
public class ModLoader
{
/// <summary>
/// A list of all mods that have been initialized
/// </summary>
public static List<ModBase> ModList = new List<ModBase>();
/// <summary>
/// Called by the game manager on startup to load in mods
/// </summary>
public static void LoadMods()
{
Debug.Log("Loading mods...");
var modDLLs = new List<string>();
modDLLs.Add(Assembly.GetExecutingAssembly().Location);
if (Directory.Exists(ModBase.BasePath))
{
modDLLs.AddRange(Directory.GetFiles(ModBase.BasePath, "*.dll"));
}
else
{
Debug.Log($"Mod directory does not exist, creating at \"{ModBase.BasePath}\"");
Directory.CreateDirectory(ModBase.BasePath);
//Create the planetbase mod folder and extract the assets
}
Debug.Log($"Found {modDLLs.Count} mods");
foreach (var file in modDLLs)
{
Type[] types;
try
{
types = Assembly.LoadFile(file).GetTypes();
}
catch (ReflectionTypeLoadException e)
{
Utils.LogException(e);
foreach (var loaderException in e.LoaderExceptions)
{
Utils.LogException(loaderException);
}
Debug.Log(
"************************ Note to modders: If you're seeing this exception, you probably are using a a post .Net 2.0.5.0 function.\r\n" +
"For convenience I've made it so you can use mods compiled after 2.0.5.0, however modern features are not available. ************************"
);
continue;
}
catch (Exception e)
{
Utils.LogException(e);
continue;
}
foreach (var type in types)
{
//Skip if the type isn't a mod
if (!typeof(ModBase).IsAssignableFrom(type) || type.IsAbstract || !type.IsPublic ||
Attribute.IsDefined(type, typeof(ModLoaderIgnoreAttribute))) continue;
var typeName = type.Name;
Debug.Log($"Loading mod \"{typeName}\" from file \"{file}\"");
ModBase mod = null;
try
{
mod = Activator.CreateInstance(type) as ModBase;
}
catch (Exception e)
{
Debug.Log(
$"Error loading mod from file: \"{file}\" of type: \"{typeName}\". Exception thrown:");
Utils.LogException(e);
}
if (mod != null)
{
var modName = mod.ModName;
try
{
mod.Init();
ModList.Add(mod);
Debug.Log($"Loaded mod \"{modName}\"");
}
catch (Exception e)
{
Debug.Log(
$"Error initializing mod \"{modName}\" from file: \"{file}\" of type: {typeName}"
);
Utils.LogException(e);
}
}
else
{
Debug.Log($"Failed to load mod \"{typeName}\" from file \"{file}\"");
}
}
}
Debug.Log($"Successfully loaded {modDLLs.Count} mods");
}
/// <summary>
/// Update the mods in the order they were loaded on each game tick.
/// </summary>
public static void UpdateMods()
{
foreach(var mod in ModList)
{
try
{
mod.Update();
}
catch (Exception e)
{
Debug.Log($"Error updating mod {mod.ModName}");
Utils.LogException(e);
}
}
}
/// <summary>
/// Utility method to get mods that match the provided type
/// </summary>
/// <typeparam name="T">The type of the mod to look for</typeparam>
/// <returns>A list of instantiated mods matching the type</returns>
public static List<T> GetModByType<T>() where T : ModBase => GetModByType(typeof(T)).Cast<T>().ToList();
/// <summary>
/// Utility method to get mods that match the provided type
/// </summary>
/// <param name="modType">The type of the mod to look for</param>
/// <returns>A list of instantiated mods matching the type</returns>
public static List<ModBase> GetModByType(Type modType)
{
var matchedMods = new List<ModBase>(); //oh boy, sure wish I could use linq right about now
foreach (var mod in ModList)
{
if (mod.GetType() == modType/*.Compare(modType)*/)
{
matchedMods.Add(mod);
}
}
return matchedMods;
}
}
}