Skip to content

Commit e075162

Browse files
committed
ScriptEngine - Better handle exception during plugin load
1 parent 6bb1a73 commit e075162

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

src/ScriptEngine/ScriptEngine.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ void Awake()
3232
LoadOnStart = Config.Bind("General", "LoadOnStart", false, new ConfigDescription("Load all plugins from the scripts folder when starting the application"));
3333
ReloadKey = Config.Bind("General", "ReloadKey", new KeyboardShortcut(KeyCode.F6), new ConfigDescription("Press this key to reload all the plugins from the scripts folder"));
3434

35-
if(LoadOnStart.Value)
35+
if (LoadOnStart.Value)
3636
ReloadPlugins();
3737
}
3838

3939
void Update()
4040
{
41-
if(ReloadKey.Value.IsDown())
41+
if (ReloadKey.Value.IsDown())
4242
ReloadPlugins();
4343
}
4444

@@ -50,9 +50,9 @@ void ReloadPlugins()
5050
DontDestroyOnLoad(scriptManager);
5151

5252
var files = Directory.GetFiles(ScriptDirectory, "*.dll");
53-
if(files.Length > 0)
53+
if (files.Length > 0)
5454
{
55-
foreach(string path in Directory.GetFiles(ScriptDirectory, "*.dll"))
55+
foreach (string path in Directory.GetFiles(ScriptDirectory, "*.dll"))
5656
LoadDLL(path, scriptManager);
5757

5858
Logger.LogMessage("Reloaded all plugins!");
@@ -72,30 +72,47 @@ void LoadDLL(string path, GameObject obj)
7272

7373
Logger.Log(LogLevel.Info, $"Loading plugins from {path}");
7474

75-
using(var dll = AssemblyDefinition.ReadAssembly(path, new ReaderParameters { AssemblyResolver = defaultResolver }))
75+
using (var dll = AssemblyDefinition.ReadAssembly(path, new ReaderParameters { AssemblyResolver = defaultResolver }))
7676
{
7777
dll.Name.Name = $"{dll.Name.Name}-{DateTime.Now.Ticks}";
7878

79-
using(var ms = new MemoryStream())
79+
using (var ms = new MemoryStream())
8080
{
8181
dll.Write(ms);
8282
var ass = Assembly.Load(ms.ToArray());
8383

8484
foreach (Type type in GetTypesSafe(ass))
8585
{
86-
if (typeof(BaseUnityPlugin).IsAssignableFrom(type))
86+
try
8787
{
88-
var metadata = MetadataHelper.GetMetadata(type);
89-
if (metadata != null)
88+
if (typeof(BaseUnityPlugin).IsAssignableFrom(type))
9089
{
91-
var typeDefinition = dll.MainModule.Types.First(x => x.FullName == type.FullName);
92-
var typeInfo = Chainloader.ToPluginInfo(typeDefinition);
93-
Chainloader.PluginInfos[metadata.GUID] = typeInfo;
94-
95-
Logger.Log(LogLevel.Info, $"Loading {metadata.GUID}");
96-
StartCoroutine(DelayAction(() => obj.AddComponent(type)));
90+
var metadata = MetadataHelper.GetMetadata(type);
91+
if (metadata != null)
92+
{
93+
var typeDefinition = dll.MainModule.Types.First(x => x.FullName == type.FullName);
94+
var typeInfo = Chainloader.ToPluginInfo(typeDefinition);
95+
Chainloader.PluginInfos[metadata.GUID] = typeInfo;
96+
97+
Logger.Log(LogLevel.Info, $"Loading {metadata.GUID}");
98+
StartCoroutine(DelayAction(() =>
99+
{
100+
try
101+
{
102+
obj.AddComponent(type);
103+
}
104+
catch (Exception e)
105+
{
106+
Logger.LogError($"Failed to load plugin {metadata.GUID} because of exception: {e}");
107+
}
108+
}));
109+
}
97110
}
98111
}
112+
catch (Exception e)
113+
{
114+
Logger.LogError($"Failed to load plugin {type.Name} because of exception: {e}");
115+
}
99116
}
100117
}
101118
}

0 commit comments

Comments
 (0)