This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
forked from Slaynash/VRCModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModManager.cs
178 lines (155 loc) · 6.9 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
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
172
173
174
175
176
177
178
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
using VRLoader.Attributes;
using VRLoader.Modules;
namespace VRCModLoader
{
public static class ModManager
{
public static readonly string VERSION = "2.0.0-pre1";
internal static ModuleManager moduleManager;
public static List<VRCMod> _Mods = null;
internal static List<VRCModController> ModControllers = null;
public static List<VRModule> _Modules = null;
private static List<Assembly> loadedAssemblies = new List<Assembly>();
public static Coroutine StartCoroutine(IEnumerator routine)
{
return ModComponent.Instance.StartCoroutine(routine);
}
public static List<VRCMod> Mods
{
get { return _Mods; }
set { _Mods = value; }
}
public static List<VRModule> Modules
{
get { return _Modules; }
set { _Modules = value; }
}
internal static void LoadMods()
{
VRCModLogger.Log("Looking for mods");
string modDirectory = "";
if (Application.platform == RuntimePlatform.WindowsPlayer)
modDirectory = Path.Combine(Environment.CurrentDirectory, "Mods");
if (Application.platform == RuntimePlatform.Android)
modDirectory = "/sdcard/VRCTools/Mods";
Mods = new List<VRCMod>();
ModControllers = new List<VRCModController>();
Modules = new List<VRModule>();
if (moduleManager == null)
{
moduleManager = new ModuleManager();
ModComponent.Instance.gameObject.AddComponent<VRLoader.VRLoader>();
}
if (!Directory.Exists(modDirectory)) return;
string[] files = Directory.GetFiles(modDirectory, "*.dll");
foreach (string s in files)
{
if (!File.Exists(s) || !s.EndsWith(".dll", true, null))
return;
VRCModLogger.Log("Loading " + s);
try
{
byte[] data = File.ReadAllBytes(s);
Assembly a = Assembly.Load(data);
loadedAssemblies.Add(a);
}
catch (Exception e)
{
VRCModLogger.LogError("Unable to load assembly " + s + ":\n" + e);
}
}
foreach (Assembly a in loadedAssemblies)
{
VRCModLogger.Log("Loading mods from " + a.GetName());
LoadModsFromAssembly(a);
}
// DEBUG
VRCModLogger.Log("Running on Unity " + Application.unityVersion + ", using VRCModLoader " + VERSION);
VRCModLogger.Log("-----------------------------");
VRCModLogger.Log("Loading mods from " + modDirectory + " and found " + Mods.Count + " VRCMods and " + Modules.Count + " VRModules.");
VRCModLogger.Log("-----------------------------");
foreach (var mod in Mods)
VRCModLogger.Log(" " + mod.Name + " (" + mod.Version + ") by " + mod.Author + (mod.DownloadLink != null ? " (" + mod.DownloadLink + ")" : ""));
VRCModLogger.Log("-----------------------------");
foreach (var mod in Modules)
VRCModLogger.Log(" " + mod.Name + " (" + mod.Version + ") by " + mod.Author);
VRCModLogger.Log("-----------------------------");
}
private static void LoadModsFromAssembly(Assembly assembly)
{
try
{
foreach (Type t in assembly.GetLoadableTypes())
{
if (t.IsSubclassOf(typeof(VRCMod)))
{
try
{
VRCMod modInstance = Activator.CreateInstance(t) as VRCMod;
Mods.Add(modInstance);
ModControllers.Add(new VRCModController(modInstance));
VRCModInfoAttribute modInfoAttribute = modInstance.GetType().GetCustomAttributes(typeof(VRCModInfoAttribute), true).FirstOrDefault() as VRCModInfoAttribute;
if (modInfoAttribute != null)
{
modInstance.Name = modInfoAttribute.Name;
modInstance.Version = modInfoAttribute.Version;
modInstance.Author = modInfoAttribute.Author;
modInstance.DownloadLink = modInfoAttribute.DownloadLink;
}
}
catch (Exception e)
{
VRCModLogger.Log("[WARN] [ModManager] Could not load mod " + t.FullName + " in " + assembly.GetName() + "! " + e);
}
}
if (t.IsSubclassOf(typeof(VRModule)))
{
try
{
ModuleInfoAttribute moduleInfo;
if ((moduleInfo = (t.GetCustomAttributes(typeof(ModuleInfoAttribute), true).FirstOrDefault() as ModuleInfoAttribute)) != null)
{
VRCModLogger.Log("Adding component " + t);
VRModule vrmodule = ModComponent.modulesGameObject.gameObject.AddComponent(t) as VRModule;
Modules.Add(vrmodule);
vrmodule.Initialize(moduleInfo, moduleManager);
}
}
catch (Exception e)
{
VRCModLogger.Log("[WARN] [ModManager] Could not load module " + t.FullName + " in " + assembly.GetName() + "! " + e);
}
}
}
}
catch (Exception e)
{
VRCModLogger.LogError("[ModManager] Could not load " + assembly.GetName() + "! " + e);
}
}
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException(nameof(assembly));
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
VRCModLogger.LogError("[ModManager] An error occured while getting types from assembly " + assembly.GetName().Name + ". Returning types from error.\n" + e);
return e.Types.Where(t => t != null);
}
}
}
}