Skip to content

Commit 9983880

Browse files
committed
Stop using obsolete members; Code cleanup
1 parent 9fb7cf6 commit 9983880

4 files changed

Lines changed: 25 additions & 72 deletions

File tree

ConfigurationManager/ConfigurationManager.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class ConfigurationManager : BaseUnityPlugin
8080
private readonly ConfigEntry<bool> _showAdvanced;
8181
private readonly ConfigEntry<bool> _showKeybinds;
8282
private readonly ConfigEntry<bool> _showSettings;
83-
private readonly ConfigEntry<BepInEx.Configuration.KeyboardShortcut> _keybind;
83+
private readonly ConfigEntry<KeyboardShortcut> _keybind;
8484
private readonly ConfigEntry<bool> _hideSingleSection;
8585
private readonly ConfigEntry<bool> _pluginConfigCollapsedDefault;
8686
private bool _showDebug;
@@ -91,14 +91,14 @@ public ConfigurationManager()
9191
Logger = base.Logger;
9292
_fieldDrawer = new SettingFieldDrawer(this);
9393

94-
_showAdvanced = Config.AddSetting("Filtering", "Show advanced", false);
95-
_showKeybinds = Config.AddSetting("Filtering", "Show keybinds", true);
96-
_showSettings = Config.AddSetting("Filtering", "Show settings", true);
97-
_keybind = Config.AddSetting("General", "Show config manager", new BepInEx.Configuration.KeyboardShortcut(KeyCode.F1),
94+
_showAdvanced = Config.Bind("Filtering", "Show advanced", false);
95+
_showKeybinds = Config.Bind("Filtering", "Show keybinds", true);
96+
_showSettings = Config.Bind("Filtering", "Show settings", true);
97+
_keybind = Config.Bind("General", "Show config manager", new KeyboardShortcut(KeyCode.F1),
9898
new ConfigDescription("The shortcut used to toggle the config manager window on and off.\n" +
9999
"The key can be overridden by a game-specific plugin if necessary, in that case this setting is ignored."));
100-
_hideSingleSection = Config.AddSetting("General", "Hide single sections", false, new ConfigDescription("Show section title for plugins with only one section"));
101-
_pluginConfigCollapsedDefault = Config.AddSetting("General", "Plugin collapsed default", true, new ConfigDescription("If set to true plugins will be collapsed when opening the configuration manager window"));
100+
_hideSingleSection = Config.Bind("General", "Hide single sections", false, new ConfigDescription("Show section title for plugins with only one section"));
101+
_pluginConfigCollapsedDefault = Config.Bind("General", "Plugin collapsed default", true, new ConfigDescription("If set to true plugins will be collapsed when opening the configuration manager window"));
102102
}
103103

104104
/// <summary>
@@ -154,6 +154,9 @@ public static void RegisterCustomSettingDrawer(Type settingType, Action<SettingE
154154
SettingFieldDrawer.SettingDrawHandlers[settingType] = onGuiDrawer;
155155
}
156156

157+
/// <summary>
158+
/// Rebuild the setting list. Use to update the config manager window if config settings were removed or added while it was open.
159+
/// </summary>
157160
public void BuildSettingList()
158161
{
159162
SettingSearcher.CollectSettings(out var results, out var modsWithoutSettings, _showDebug);
@@ -434,7 +437,7 @@ private void DrawWindowHeader()
434437

435438
if (GUILayout.Button("Log", GUILayout.ExpandWidth(false)))
436439
{
437-
try { Utilities.Utils.OpenLog(); }
440+
try { Utils.OpenLog(); }
438441
catch (SystemException ex) { Logger.Log(LogLevel.Message | LogLevel.Error, ex.Message); }
439442
}
440443
}
@@ -570,20 +573,20 @@ private static void DrawDefaultButton(SettingEntryBase setting)
570573
{
571574
if (setting.HideDefaultButton) return;
572575

573-
bool DrawDefaultButton()
576+
bool DefaultButton()
574577
{
575578
GUILayout.Space(5);
576579
return GUILayout.Button("Reset", GUILayout.ExpandWidth(false));
577580
}
578581

579582
if (setting.DefaultValue != null)
580583
{
581-
if (DrawDefaultButton())
584+
if (DefaultButton())
582585
setting.Set(setting.DefaultValue);
583586
}
584587
else if (setting.SettingType.IsClass)
585588
{
586-
if (DrawDefaultButton())
589+
if (DefaultButton())
587590
setting.Set(null);
588591
}
589592
}

ConfigurationManager/SettingSearcher.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void CollectSettings(out IEnumerable<SettingEntryBase> results, ou
5252

5353
detected.RemoveAll(x => x.Browsable == false);
5454

55-
if (!detected.Any())
55+
if (detected.Count == 0)
5656
modsWithoutSettings.Add(pluginInfo.Name);
5757

5858
// Allow to enable/disable plugin if it uses any update methods ------
@@ -65,13 +65,13 @@ public static void CollectSettings(out IEnumerable<SettingEntryBase> results, ou
6565
detected.Add(enabledSetting);
6666
}
6767

68-
if (detected.Any())
68+
if (detected.Count > 0)
6969
results = results.Concat(detected);
7070
}
7171
}
7272

7373
/// <summary>
74-
/// Bepinex 5 config
74+
/// Get entries for all core BepInEx settings
7575
/// </summary>
7676
private static IEnumerable<SettingEntryBase> GetBepInExCoreConfig()
7777
{
@@ -81,17 +81,15 @@ private static IEnumerable<SettingEntryBase> GetBepInExCoreConfig()
8181
var coreConfig = (ConfigFile)coreConfigProp.GetValue(null, null);
8282
var bepinMeta = new BepInPlugin("BepInEx", "BepInEx", typeof(BepInEx.Bootstrap.Chainloader).Assembly.GetName().Version.ToString());
8383

84-
return coreConfig.GetConfigEntries()
85-
.Select(x => new ConfigSettingEntry(x, null) { IsAdvanced = true, PluginInfo = bepinMeta })
86-
.Cast<SettingEntryBase>();
84+
return coreConfig.Select(kvp => (SettingEntryBase)new ConfigSettingEntry(kvp.Value, null) { IsAdvanced = true, PluginInfo = bepinMeta });
8785
}
8886

8987
/// <summary>
90-
/// Used by bepinex 5 plugins
88+
/// Get entries for all settings of a plugin
9189
/// </summary>
9290
private static IEnumerable<ConfigSettingEntry> GetPluginConfig(BaseUnityPlugin plugin)
9391
{
94-
return plugin.Config.GetConfigEntries().Select(x => new ConfigSettingEntry(x, plugin));
92+
return plugin.Config.Select(kvp => new ConfigSettingEntry(kvp.Value, plugin));
9593
}
9694
}
9795
}

ConfigurationManager/Utilities/ComboBox.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ internal class ComboBox
1212
{
1313
private static bool forceToUnShow;
1414
private static int useControlID = -1;
15-
private readonly string boxStyle;
1615
private readonly string buttonStyle;
1716
private bool isClickedComboButton;
1817
private readonly GUIContent[] listContent;
@@ -25,22 +24,10 @@ public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, G
2524
ButtonContent = buttonContent;
2625
this.listContent = listContent;
2726
buttonStyle = "button";
28-
boxStyle = "box";
2927
this.listStyle = listStyle;
3028
_windowYmax = (int)windowYmax;
3129
}
3230

33-
public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle,
34-
string boxStyle, GUIStyle listStyle)
35-
{
36-
Rect = rect;
37-
ButtonContent = buttonContent;
38-
this.listContent = listContent;
39-
this.buttonStyle = buttonStyle;
40-
this.boxStyle = boxStyle;
41-
this.listStyle = listStyle;
42-
}
43-
4431
public Rect Rect { get; set; }
4532

4633
public GUIContent ButtonContent { get; set; }

ConfigurationManager/Utilities/Utilities.cs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using BepInEx;
55
using System;
66
using System.Collections.Generic;
7-
using System.ComponentModel;
87
using System.Diagnostics;
98
using System.IO;
109
using System.Linq;
@@ -35,44 +34,9 @@ public static string ToProperCase(this string str)
3534
return result;
3635
}
3736

38-
/// <summary>
39-
/// Return items with browsable attribute same as expectedBrowsable, and optionally items with no browsable attribute
40-
/// </summary>
41-
public static IEnumerable<T> FilterBrowsable<T>(this IEnumerable<T> props, bool expectedBrowsable,
42-
bool includeNotSet = false) where T : MemberInfo
43-
{
44-
if (includeNotSet)
45-
return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().All(x => x.Browsable == expectedBrowsable));
46-
47-
return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().Any(x => x.Browsable == expectedBrowsable));
48-
}
49-
50-
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
51-
{
52-
while (toCheck != null && toCheck != typeof(object))
53-
{
54-
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
55-
if (generic == cur)
56-
return true;
57-
toCheck = toCheck.BaseType;
58-
}
59-
return false;
60-
}
61-
62-
// Additionally search for BaseUnityPlugin to find dynamically loaded plugins
63-
public static BaseUnityPlugin[] FindPlugins() => BepInEx.Bootstrap.Chainloader.Plugins.Concat(Object.FindObjectsOfType(typeof(BaseUnityPlugin)).Cast<BaseUnityPlugin>()).Distinct().ToArray();
64-
65-
public static bool IsNumber(this object value) => value is sbyte
66-
|| value is byte
67-
|| value is short
68-
|| value is ushort
69-
|| value is int
70-
|| value is uint
71-
|| value is long
72-
|| value is ulong
73-
|| value is float
74-
|| value is double
75-
|| value is decimal;
37+
// Search for instances of BaseUnityPlugin to also find dynamically loaded plugins. Doing this makes checking Chainloader.PluginInfos redundant.
38+
// Have to use FindObjectsOfType(Type) instead of FindObjectsOfType<T> because the latter is not available in some older unity versions.
39+
public static BaseUnityPlugin[] FindPlugins() => Array.ConvertAll(Object.FindObjectsOfType(typeof(BaseUnityPlugin)), input => (BaseUnityPlugin)input);
7640

7741
public static string AppendZero(this string s)
7842
{
@@ -148,6 +112,7 @@ bool TryOpen(string path)
148112

149113
public static string GetWebsite(BaseUnityPlugin bepInPlugin)
150114
{
115+
if (bepInPlugin == null) return null;
151116
try
152117
{
153118
var fileName = bepInPlugin.GetType().Assembly.Location;
@@ -164,7 +129,7 @@ public static string GetWebsite(BaseUnityPlugin bepInPlugin)
164129
}
165130
catch (Exception e)
166131
{
167-
ConfigurationManager.Logger.LogWarning("Failed to get Uri info - " + e.Message);
132+
ConfigurationManager.Logger.LogWarning($"Failed to get URI for {bepInPlugin?.Info?.Metadata?.Name} - {e.Message}");
168133
return null;
169134
}
170135
}

0 commit comments

Comments
 (0)