Skip to content

Commit

Permalink
Stop using obsolete members; Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jun 4, 2022
1 parent 9fb7cf6 commit 9983880
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 72 deletions.
25 changes: 14 additions & 11 deletions ConfigurationManager/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ConfigurationManager : BaseUnityPlugin
private readonly ConfigEntry<bool> _showAdvanced;
private readonly ConfigEntry<bool> _showKeybinds;
private readonly ConfigEntry<bool> _showSettings;
private readonly ConfigEntry<BepInEx.Configuration.KeyboardShortcut> _keybind;
private readonly ConfigEntry<KeyboardShortcut> _keybind;
private readonly ConfigEntry<bool> _hideSingleSection;
private readonly ConfigEntry<bool> _pluginConfigCollapsedDefault;
private bool _showDebug;
Expand All @@ -91,14 +91,14 @@ public ConfigurationManager()
Logger = base.Logger;
_fieldDrawer = new SettingFieldDrawer(this);

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

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

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

if (GUILayout.Button("Log", GUILayout.ExpandWidth(false)))
{
try { Utilities.Utils.OpenLog(); }
try { Utils.OpenLog(); }
catch (SystemException ex) { Logger.Log(LogLevel.Message | LogLevel.Error, ex.Message); }
}
}
Expand Down Expand Up @@ -570,20 +573,20 @@ private static void DrawDefaultButton(SettingEntryBase setting)
{
if (setting.HideDefaultButton) return;

bool DrawDefaultButton()
bool DefaultButton()
{
GUILayout.Space(5);
return GUILayout.Button("Reset", GUILayout.ExpandWidth(false));
}

if (setting.DefaultValue != null)
{
if (DrawDefaultButton())
if (DefaultButton())
setting.Set(setting.DefaultValue);
}
else if (setting.SettingType.IsClass)
{
if (DrawDefaultButton())
if (DefaultButton())
setting.Set(null);
}
}
Expand Down
14 changes: 6 additions & 8 deletions ConfigurationManager/SettingSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void CollectSettings(out IEnumerable<SettingEntryBase> results, ou

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

if (!detected.Any())
if (detected.Count == 0)
modsWithoutSettings.Add(pluginInfo.Name);

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

if (detected.Any())
if (detected.Count > 0)
results = results.Concat(detected);
}
}

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

return coreConfig.GetConfigEntries()
.Select(x => new ConfigSettingEntry(x, null) { IsAdvanced = true, PluginInfo = bepinMeta })
.Cast<SettingEntryBase>();
return coreConfig.Select(kvp => (SettingEntryBase)new ConfigSettingEntry(kvp.Value, null) { IsAdvanced = true, PluginInfo = bepinMeta });
}

/// <summary>
/// Used by bepinex 5 plugins
/// Get entries for all settings of a plugin
/// </summary>
private static IEnumerable<ConfigSettingEntry> GetPluginConfig(BaseUnityPlugin plugin)
{
return plugin.Config.GetConfigEntries().Select(x => new ConfigSettingEntry(x, plugin));
return plugin.Config.Select(kvp => new ConfigSettingEntry(kvp.Value, plugin));
}
}
}
13 changes: 0 additions & 13 deletions ConfigurationManager/Utilities/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ internal class ComboBox
{
private static bool forceToUnShow;
private static int useControlID = -1;
private readonly string boxStyle;
private readonly string buttonStyle;
private bool isClickedComboButton;
private readonly GUIContent[] listContent;
Expand All @@ -25,22 +24,10 @@ public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, G
ButtonContent = buttonContent;
this.listContent = listContent;
buttonStyle = "button";
boxStyle = "box";
this.listStyle = listStyle;
_windowYmax = (int)windowYmax;
}

public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle,
string boxStyle, GUIStyle listStyle)
{
Rect = rect;
ButtonContent = buttonContent;
this.listContent = listContent;
this.buttonStyle = buttonStyle;
this.boxStyle = boxStyle;
this.listStyle = listStyle;
}

public Rect Rect { get; set; }

public GUIContent ButtonContent { get; set; }
Expand Down
45 changes: 5 additions & 40 deletions ConfigurationManager/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using BepInEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -35,44 +34,9 @@ public static string ToProperCase(this string str)
return result;
}

/// <summary>
/// Return items with browsable attribute same as expectedBrowsable, and optionally items with no browsable attribute
/// </summary>
public static IEnumerable<T> FilterBrowsable<T>(this IEnumerable<T> props, bool expectedBrowsable,
bool includeNotSet = false) where T : MemberInfo
{
if (includeNotSet)
return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().All(x => x.Browsable == expectedBrowsable));

return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().Any(x => x.Browsable == expectedBrowsable));
}

public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
return true;
toCheck = toCheck.BaseType;
}
return false;
}

// Additionally search for BaseUnityPlugin to find dynamically loaded plugins
public static BaseUnityPlugin[] FindPlugins() => BepInEx.Bootstrap.Chainloader.Plugins.Concat(Object.FindObjectsOfType(typeof(BaseUnityPlugin)).Cast<BaseUnityPlugin>()).Distinct().ToArray();

public static bool IsNumber(this object value) => value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
// Search for instances of BaseUnityPlugin to also find dynamically loaded plugins. Doing this makes checking Chainloader.PluginInfos redundant.
// Have to use FindObjectsOfType(Type) instead of FindObjectsOfType<T> because the latter is not available in some older unity versions.
public static BaseUnityPlugin[] FindPlugins() => Array.ConvertAll(Object.FindObjectsOfType(typeof(BaseUnityPlugin)), input => (BaseUnityPlugin)input);

public static string AppendZero(this string s)
{
Expand Down Expand Up @@ -148,6 +112,7 @@ bool TryOpen(string path)

public static string GetWebsite(BaseUnityPlugin bepInPlugin)
{
if (bepInPlugin == null) return null;
try
{
var fileName = bepInPlugin.GetType().Assembly.Location;
Expand All @@ -164,7 +129,7 @@ public static string GetWebsite(BaseUnityPlugin bepInPlugin)
}
catch (Exception e)
{
ConfigurationManager.Logger.LogWarning("Failed to get Uri info - " + e.Message);
ConfigurationManager.Logger.LogWarning($"Failed to get URI for {bepInPlugin?.Info?.Metadata?.Name} - {e.Message}");
return null;
}
}
Expand Down

0 comments on commit 9983880

Please sign in to comment.
  翻译: