// Made by MarC0 / ManlyMarco // Copyright 2018 GNU General Public License v3.0 using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; using BepInEx; using BepInEx.Logging; using UnityEngine; using BepInEx.Configuration; namespace ConfigurationManager { /// /// An easy way to let user configure how a plugin behaves without the need to make your own GUI. The user can change any of the settings you expose, even keyboard shortcuts. /// https://github.com/ManlyMarco/BepInEx.ConfigurationManager /// [BepInPlugin(GUID, "Configuration Manager", Version)] [Browsable(false)] public class ConfigurationManager : BaseUnityPlugin { /// /// GUID of this plugin /// public const string GUID = "com.bepis.bepinex.configurationmanager"; /// /// Version constant /// public const string Version = "16.2"; internal static new ManualLogSource Logger; private static SettingFieldDrawer _fieldDrawer; private static readonly Color _advancedSettingColor = new Color(1f, 0.95f, 0.67f, 1f); private const int WindowId = -68; private const string SearchBoxName = "searchBox"; private bool _focusSearchBox; private string _searchString = string.Empty; /// /// Event fired every time the manager window is shown or hidden. /// public event EventHandler> DisplayingWindowChanged; /// /// Disable the hotkey check used by config manager. If enabled you have to set to show the manager. /// public bool OverrideHotkey; private bool _displayingWindow; private bool _obsoleteCursor; private string _modsWithoutSettings; private List _allSettings; private List _filteredSetings = new List(); internal Rect SettingWindowRect { get; private set; } private Rect _screenRect; private Vector2 _settingWindowScrollPos; private int _tipsHeight; private PropertyInfo _curLockState; private PropertyInfo _curVisible; private int _previousCursorLockState; private bool _previousCursorVisible; internal static Texture2D TooltipBg { get; private set; } internal static Texture2D WindowBackground { get; private set; } internal int LeftColumnWidth { get; private set; } internal int RightColumnWidth { get; private set; } private readonly ConfigEntry _showAdvanced; private readonly ConfigEntry _showKeybinds; private readonly ConfigEntry _showSettings; private readonly ConfigEntry _keybind; private readonly ConfigEntry _hideSingleSection; private readonly ConfigEntry _pluginConfigCollapsedDefault; private bool _showDebug; /// 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), 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")); } /// /// Is the config manager main window displayed on screen /// public bool DisplayingWindow { get => _displayingWindow; set { if (_displayingWindow == value) return; _displayingWindow = value; SettingFieldDrawer.ClearCache(); if (_displayingWindow) { CalculateWindowRect(); BuildSettingList(); _focusSearchBox = true; // Do through reflection for unity 4 compat if (_curLockState != null) { _previousCursorLockState = _obsoleteCursor ? Convert.ToInt32((bool)_curLockState.GetValue(null, null)) : (int)_curLockState.GetValue(null, null); _previousCursorVisible = (bool)_curVisible.GetValue(null, null); } } else { if (!_previousCursorVisible || _previousCursorLockState != 0) // 0 = CursorLockMode.None SetUnlockCursor(_previousCursorLockState, _previousCursorVisible); } DisplayingWindowChanged?.Invoke(this, new ValueChangedEventArgs(value)); } } /// /// Register a custom setting drawer for a given type. The action is ran in OnGui in a single setting slot. /// Do not use any Begin / End layout methods, and avoid raising height from standard. /// public static void RegisterCustomSettingDrawer(Type settingType, Action onGuiDrawer) { if (settingType == null) throw new ArgumentNullException(nameof(settingType)); if (onGuiDrawer == null) throw new ArgumentNullException(nameof(onGuiDrawer)); if (SettingFieldDrawer.SettingDrawHandlers.ContainsKey(settingType)) Logger.LogWarning("Tried to add a setting drawer for type " + settingType.FullName + " while one already exists."); else SettingFieldDrawer.SettingDrawHandlers[settingType] = onGuiDrawer; } public void BuildSettingList() { SettingSearcher.CollectSettings(out var results, out var modsWithoutSettings, _showDebug); _modsWithoutSettings = string.Join(", ", modsWithoutSettings.Select(x => x.TrimStart('!')).OrderBy(x => x).ToArray()); _allSettings = results.ToList(); BuildFilteredSettingList(); } private void BuildFilteredSettingList() { IEnumerable results = _allSettings; var searchStrings = SearchString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (searchStrings.Length > 0) { results = results.Where(x => ContainsSearchString(x, searchStrings)); } else { if (!_showAdvanced.Value) results = results.Where(x => x.IsAdvanced != true); if (!_showKeybinds.Value) results = results.Where(x => !IsKeyboardShortcut(x)); if (!_showSettings.Value) results = results.Where(x => x.IsAdvanced == true || IsKeyboardShortcut(x)); } const string shortcutsCatName = "Keyboard shortcuts"; string GetCategory(SettingEntryBase eb) { #pragma warning disable 618 // Disable obsolete warning // Legacy behavior if (eb.SettingType == typeof(BepInEx.KeyboardShortcut)) return shortcutsCatName; #pragma warning restore 618 return eb.Category; } var settingsAreCollapsed = _pluginConfigCollapsedDefault.Value; var nonDefaultCollpasingStateByPluginName = new HashSet(); foreach (var pluginSetting in _filteredSetings) { if (pluginSetting.Collapsed != settingsAreCollapsed) { nonDefaultCollpasingStateByPluginName.Add(pluginSetting.Info.Name); } } _filteredSetings = results .GroupBy(x => x.PluginInfo) .Select(pluginSettings => { var categories = pluginSettings .GroupBy(GetCategory) .OrderBy(x => string.Equals(x.Key, shortcutsCatName, StringComparison.Ordinal)) .ThenBy(x => x.Key) .Select(x => new PluginSettingsData.PluginSettingsGroupData { Name = x.Key, Settings = x.OrderByDescending(set => set.Order).ThenBy(set => set.DispName).ToList() }); return new PluginSettingsData { Info = pluginSettings.Key, Categories = categories.ToList(), Collapsed = nonDefaultCollpasingStateByPluginName.Contains(pluginSettings.Key.Name) ? !settingsAreCollapsed : settingsAreCollapsed }; }) .OrderBy(x => x.Info.Name) .ToList(); } private static bool IsKeyboardShortcut(SettingEntryBase x) { #pragma warning disable 618 // Disable obsolete warning return x.SettingType == typeof(BepInEx.KeyboardShortcut) || x.SettingType == typeof(BepInEx.Configuration.KeyboardShortcut); #pragma warning restore 618 } private static bool ContainsSearchString(SettingEntryBase setting, string[] searchStrings) { var combinedSearchTarget = setting.PluginInfo.Name + "\n" + setting.PluginInfo.GUID + "\n" + setting.DispName + "\n" + setting.Category + "\n" + setting.Description + "\n" + setting.DefaultValue + "\n" + setting.Get(); return searchStrings.All(s => combinedSearchTarget.IndexOf(s, StringComparison.InvariantCultureIgnoreCase) >= 0); } private void CalculateWindowRect() { var width = Mathf.Min(Screen.width, 650); var height = Screen.height < 560 ? Screen.height : Screen.height - 100; var offsetX = Mathf.RoundToInt((Screen.width - width) / 2f); var offsetY = Mathf.RoundToInt((Screen.height - height) / 2f); SettingWindowRect = new Rect(offsetX, offsetY, width, height); _screenRect = new Rect(0, 0, Screen.width, Screen.height); LeftColumnWidth = Mathf.RoundToInt(SettingWindowRect.width / 2.5f); RightColumnWidth = (int)SettingWindowRect.width - LeftColumnWidth - 115; } private void OnGUI() { if (DisplayingWindow) { if (Event.current.type == EventType.KeyUp && Event.current.keyCode == _keybind.Value.MainKey) { DisplayingWindow = false; return; } SetUnlockCursor(0, true); if (GUI.Button(_screenRect, string.Empty, GUI.skin.box) && !SettingWindowRect.Contains(Input.mousePosition)) DisplayingWindow = false; GUI.Box(SettingWindowRect, GUIContent.none, new GUIStyle { normal = new GUIStyleState { background = WindowBackground } }); GUILayout.Window(WindowId, SettingWindowRect, SettingsWindow, "Plugin / mod settings"); if (!SettingFieldDrawer.SettingKeyboardShortcut) Input.ResetInputAxes(); } } private static void DrawTooltip(Rect area) { if (!string.IsNullOrEmpty(GUI.tooltip)) { var currentEvent = Event.current; var style = new GUIStyle { normal = new GUIStyleState { textColor = Color.white, background = TooltipBg }, wordWrap = true, alignment = TextAnchor.MiddleCenter }; const int width = 400; var height = style.CalcHeight(new GUIContent(GUI.tooltip), 400) + 10; var x = currentEvent.mousePosition.x + width > area.width ? area.width - width : currentEvent.mousePosition.x; var y = currentEvent.mousePosition.y + 25 + height > area.height ? currentEvent.mousePosition.y - height : currentEvent.mousePosition.y + 25; GUI.Box(new Rect(x, y, width, height), GUI.tooltip, style); } } private void SettingsWindow(int id) { DrawWindowHeader(); _settingWindowScrollPos = GUILayout.BeginScrollView(_settingWindowScrollPos, false, true); var scrollPosition = _settingWindowScrollPos.y; var scrollHeight = SettingWindowRect.height; GUILayout.BeginVertical(); { if (string.IsNullOrEmpty(SearchString)) { DrawTips(); if (_tipsHeight == 0 && Event.current.type == EventType.Repaint) _tipsHeight = (int)GUILayoutUtility.GetLastRect().height; } var currentHeight = _tipsHeight; foreach (var plugin in _filteredSetings) { var visible = plugin.Height == 0 || currentHeight + plugin.Height >= scrollPosition && currentHeight <= scrollPosition + scrollHeight; if (visible) { try { DrawSinglePlugin(plugin); } catch (ArgumentException) { // Needed to avoid GUILayout: Mismatched LayoutGroup.Repaint crashes on large lists } if (plugin.Height == 0 && Event.current.type == EventType.Repaint) plugin.Height = (int)GUILayoutUtility.GetLastRect().height; } else { try { GUILayout.Space(plugin.Height); } catch (ArgumentException) { // Needed to avoid GUILayout: Mismatched LayoutGroup.Repaint crashes on large lists } } currentHeight += plugin.Height; } if (_showDebug) { GUILayout.Space(10); GUILayout.Label("Plugins with no options available: " + _modsWithoutSettings); } else { // Always leave some space in case there's a dropdown box at the very bottom of the list GUILayout.Space(70); } } GUILayout.EndVertical(); GUILayout.EndScrollView(); if (!SettingFieldDrawer.DrawCurrentDropdown()) DrawTooltip(SettingWindowRect); } private void DrawTips() { GUILayout.BeginHorizontal(); { GUILayout.Label("Tip: Click plugin names to expand. Click setting and group names to see their descriptions."); GUILayout.FlexibleSpace(); if (GUILayout.Button(_pluginConfigCollapsedDefault.Value ? "Expand" : "Collapse", GUILayout.ExpandWidth(false))) { var newValue = !_pluginConfigCollapsedDefault.Value; _pluginConfigCollapsedDefault.Value = newValue; foreach (var plugin in _filteredSetings) plugin.Collapsed = newValue; } } GUILayout.EndHorizontal(); } private void DrawWindowHeader() { GUILayout.BeginHorizontal(GUI.skin.box); { GUILayout.Label("Show: ", GUILayout.ExpandWidth(false)); GUI.enabled = SearchString == string.Empty; var newVal = GUILayout.Toggle(_showSettings.Value, "Normal settings"); if (_showSettings.Value != newVal) { _showSettings.Value = newVal; BuildFilteredSettingList(); } newVal = GUILayout.Toggle(_showKeybinds.Value, "Keyboard shortcuts"); if (_showKeybinds.Value != newVal) { _showKeybinds.Value = newVal; BuildFilteredSettingList(); } var origColor = GUI.color; GUI.color = _advancedSettingColor; newVal = GUILayout.Toggle(_showAdvanced.Value, "Advanced settings"); if (_showAdvanced.Value != newVal) { _showAdvanced.Value = newVal; BuildFilteredSettingList(); } GUI.color = origColor; GUI.enabled = true; newVal = GUILayout.Toggle(_showDebug, "Debug mode"); if (_showDebug != newVal) { _showDebug = newVal; BuildSettingList(); } if (GUILayout.Button("Log", GUILayout.ExpandWidth(false))) { try { Utilities.Utils.OpenLog(); } catch (SystemException ex) { Logger.Log(LogLevel.Message | LogLevel.Error, ex.Message); } } } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(GUI.skin.box); { GUILayout.Label("Search settings: ", GUILayout.ExpandWidth(false)); GUI.SetNextControlName(SearchBoxName); SearchString = GUILayout.TextField(SearchString, GUILayout.ExpandWidth(true)); if (_focusSearchBox) { GUI.FocusWindow(WindowId); GUI.FocusControl(SearchBoxName); _focusSearchBox = false; } if (GUILayout.Button("Clear", GUILayout.ExpandWidth(false))) SearchString = string.Empty; } GUILayout.EndHorizontal(); } /// /// String currently entered into the search box /// public string SearchString { get => _searchString; private set { if (value == null) value = string.Empty; if (_searchString == value) return; _searchString = value; BuildFilteredSettingList(); } } private void DrawSinglePlugin(PluginSettingsData plugin) { GUILayout.BeginVertical(GUI.skin.box); var categoryHeader = _showDebug ? new GUIContent($"{plugin.Info.Name.TrimStart('!')} {plugin.Info.Version}", "GUID: " + plugin.Info.GUID) : new GUIContent($"{plugin.Info.Name.TrimStart('!')} {plugin.Info.Version}"); var isSearching = !string.IsNullOrEmpty(SearchString); if (SettingFieldDrawer.DrawPluginHeader(categoryHeader, plugin.Collapsed && !isSearching) && !isSearching) plugin.Collapsed = !plugin.Collapsed; if (isSearching || !plugin.Collapsed) { foreach (var category in plugin.Categories) { if (!string.IsNullOrEmpty(category.Name)) { if (plugin.Categories.Count > 1 || !_hideSingleSection.Value) SettingFieldDrawer.DrawCategoryHeader(category.Name); } foreach (var setting in category.Settings) { DrawSingleSetting(setting); GUILayout.Space(2); } } } GUILayout.EndVertical(); } private void DrawSingleSetting(SettingEntryBase setting) { GUILayout.BeginHorizontal(); { try { DrawSettingName(setting); _fieldDrawer.DrawSettingValue(setting); DrawDefaultButton(setting); } catch (Exception ex) { Logger.Log(LogLevel.Error, $"Failed to draw setting {setting.DispName} - {ex}"); GUILayout.Label("Failed to draw this field, check log for details."); } } GUILayout.EndHorizontal(); } private void DrawSettingName(SettingEntryBase setting) { if (setting.HideSettingName) return; var origColor = GUI.color; if (setting.IsAdvanced == true) GUI.color = _advancedSettingColor; GUILayout.Label(new GUIContent(setting.DispName.TrimStart('!'), setting.Description), GUILayout.Width(LeftColumnWidth), GUILayout.MaxWidth(LeftColumnWidth)); GUI.color = origColor; } private static void DrawDefaultButton(SettingEntryBase setting) { if (setting.HideDefaultButton) return; bool DrawDefaultButton() { GUILayout.Space(5); return GUILayout.Button("Reset", GUILayout.ExpandWidth(false)); } if (setting.DefaultValue != null) { if (DrawDefaultButton()) setting.Set(setting.DefaultValue); } else if (setting is LegacySettingEntry legacySetting && legacySetting.Wrapper != null) { var method = legacySetting.Wrapper.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public); if (method != null && DrawDefaultButton()) method.Invoke(legacySetting.Wrapper, null); } else if (setting.SettingType.IsClass) { if (DrawDefaultButton()) setting.Set(null); } } private void Start() { var background = new Texture2D(1, 1, TextureFormat.ARGB32, false); background.SetPixel(0, 0, Color.black); background.Apply(); TooltipBg = background; var windowBackground = new Texture2D(1, 1, TextureFormat.ARGB32, false); windowBackground.SetPixel(0, 0, new Color(0.5f, 0.5f, 0.5f, 1)); windowBackground.Apply(); WindowBackground = windowBackground; // Use reflection to keep compatibility with unity 4.x since it doesn't have Cursor var tCursor = typeof(Cursor); _curLockState = tCursor.GetProperty("lockState", BindingFlags.Static | BindingFlags.Public); _curVisible = tCursor.GetProperty("visible", BindingFlags.Static | BindingFlags.Public); if (_curLockState == null && _curVisible == null) { _obsoleteCursor = true; _curLockState = typeof(Screen).GetProperty("lockCursor", BindingFlags.Static | BindingFlags.Public); _curVisible = typeof(Screen).GetProperty("showCursor", BindingFlags.Static | BindingFlags.Public); } // Check if user has permissions to write config files to disk try { Config.Save(); } catch (IOException ex) { Logger.Log(LogLevel.Message | LogLevel.Warning, "WARNING: Failed to write to config directory, expect issues!\nError message:" + ex.Message); } catch (UnauthorizedAccessException ex) { Logger.Log(LogLevel.Message | LogLevel.Warning, "WARNING: Permission denied to write to config directory, expect issues!\nError message:" + ex.Message); } } private void Update() { if (DisplayingWindow) SetUnlockCursor(0, true); if (OverrideHotkey) return; if (!DisplayingWindow && _keybind.Value.IsUp()) { DisplayingWindow = true; } } private void LateUpdate() { if (DisplayingWindow) SetUnlockCursor(0, true); } private void SetUnlockCursor(int lockState, bool cursorVisible) { if (_curLockState != null) { // Do through reflection for unity 4 compat //Cursor.lockState = CursorLockMode.None; //Cursor.visible = true; if(_obsoleteCursor) _curLockState.SetValue(null, Convert.ToBoolean(lockState), null); else _curLockState.SetValue(null, lockState, null); _curVisible.SetValue(null, cursorVisible, null); } } private sealed class PluginSettingsData { public BepInPlugin Info; public List Categories; private bool _collapsed; public bool Collapsed { get => _collapsed; set { _collapsed = value; Height = 0; } } public sealed class PluginSettingsGroupData { public string Name; public List Settings; } public int Height { get; set; } } } }