Skip to content

Commit 9d5442b

Browse files
authored
Catch exceptions during settings search (#108)
If during the settings search an exception occurs, no settings are collected and thus displayed. Now it catches these exception and logs for what plugin this happened.
1 parent 1ca4951 commit 9d5442b

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

ConfigurationManager/SettingSearcher.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Reflection;
88
using BepInEx.Bootstrap;
9+
using UnityEngine;
910

1011
namespace ConfigurationManager
1112
{
@@ -49,39 +50,48 @@ public static void CollectSettings(out IEnumerable<SettingEntryBase> results, ou
4950

5051
foreach (var plugin in FindPlugins())
5152
{
52-
var type = plugin.GetType();
53+
try
54+
{
55+
var type = plugin.GetType();
5356

54-
var pluginInfo = plugin.Info.Metadata;
55-
var pluginName = pluginInfo?.Name ?? plugin.GetType().FullName;
57+
var pluginInfo = plugin.Info.Metadata;
58+
var pluginName = pluginInfo?.Name ?? plugin.GetType().FullName;
5659

57-
if (type.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>()
58-
.Any(x => !x.Browsable))
59-
{
60-
modsWithoutSettings.Add(pluginName);
61-
continue;
62-
}
60+
if (type.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>()
61+
.Any(x => !x.Browsable))
62+
{
63+
modsWithoutSettings.Add(pluginName);
64+
continue;
65+
}
6366

64-
var detected = new List<SettingEntryBase>();
67+
var detected = new List<SettingEntryBase>();
6568

66-
detected.AddRange(GetPluginConfig(plugin).Cast<SettingEntryBase>());
69+
detected.AddRange(GetPluginConfig(plugin).Cast<SettingEntryBase>());
6770

68-
detected.RemoveAll(x => x.Browsable == false);
71+
detected.RemoveAll(x => x.Browsable == false);
6972

70-
if (detected.Count == 0)
71-
modsWithoutSettings.Add(pluginName);
73+
if (detected.Count == 0)
74+
modsWithoutSettings.Add(pluginName);
7275

73-
// Allow to enable/disable plugin if it uses any update methods ------
74-
if (showDebug && type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(x => _updateMethodNames.Contains(x.Name)))
76+
// Allow to enable/disable plugin if it uses any update methods ------
77+
if (showDebug && type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(x => _updateMethodNames.Contains(x.Name)))
78+
{
79+
var enabledSetting = new PropertySettingEntry(plugin, type.GetProperty("enabled"), plugin);
80+
enabledSetting.DispName = "!Allow plugin to run on every frame";
81+
enabledSetting.Description = "Disabling this will disable some or all of the plugin's functionality.\nHooks and event-based functionality will not be disabled.\nThis setting will be lost after game restart.";
82+
enabledSetting.IsAdvanced = true;
83+
detected.Add(enabledSetting);
84+
}
85+
86+
if (detected.Count > 0)
87+
results = results.Concat(detected);
88+
}
89+
catch (Exception ex)
7590
{
76-
var enabledSetting = new PropertySettingEntry(plugin, type.GetProperty("enabled"), plugin);
77-
enabledSetting.DispName = "!Allow plugin to run on every frame";
78-
enabledSetting.Description = "Disabling this will disable some or all of the plugin's functionality.\nHooks and event-based functionality will not be disabled.\nThis setting will be lost after game restart.";
79-
enabledSetting.IsAdvanced = true;
80-
detected.Add(enabledSetting);
91+
string pluginName = plugin?.Info?.Metadata?.Name ?? plugin?.GetType().FullName;
92+
ConfigurationManager.Logger.LogError($"Failed to collect settings of the following plugin: {pluginName}");
93+
ConfigurationManager.Logger.LogError(ex);
8194
}
82-
83-
if (detected.Count > 0)
84-
results = results.Concat(detected);
8595
}
8696
}
8797

0 commit comments

Comments
 (0)