From 7a1d323e0c2a8d0e38f3817adb7e81e5a70cd41a Mon Sep 17 00:00:00 2001 From: Lexi Date: Thu, 28 Dec 2023 09:12:07 -0500 Subject: [PATCH] Possible fix for both callbacks and the threading issue --- .../API/UI/Settings/SettingsMenu.cs | 23 ++++ .../UI/Console/SpaceWarpConsole.cs | 22 +++- src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs | 105 ++++++++++++------ 3 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 src/SpaceWarp.UI/API/UI/Settings/SettingsMenu.cs diff --git a/src/SpaceWarp.UI/API/UI/Settings/SettingsMenu.cs b/src/SpaceWarp.UI/API/UI/Settings/SettingsMenu.cs new file mode 100644 index 00000000..49f59a8b --- /dev/null +++ b/src/SpaceWarp.UI/API/UI/Settings/SettingsMenu.cs @@ -0,0 +1,23 @@ +using JetBrains.Annotations; +using SpaceWarp.API.Configuration; + +namespace SpaceWarp.UI.API.UI.Settings; + +[PublicAPI] +public static class SettingsMenu +{ + /// + /// Contains a list of all the registered config files + /// + public static Dictionary RegisteredConfigFiles = new(); + + /// + /// Register a manually created config file for the settings menu + /// + /// The section name for the config file + /// The file itself + public static void RegisterConfigFile(string section, IConfigFile file) + { + RegisteredConfigFiles[section] = file; + } +} \ No newline at end of file diff --git a/src/SpaceWarp.UI/UI/Console/SpaceWarpConsole.cs b/src/SpaceWarp.UI/UI/Console/SpaceWarpConsole.cs index a78dd6b8..f98fd0b4 100644 --- a/src/SpaceWarp.UI/UI/Console/SpaceWarpConsole.cs +++ b/src/SpaceWarp.UI/UI/Console/SpaceWarpConsole.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Concurrent; using BepInEx.Logging; using KSP.Game; using UitkForKsp2.API; @@ -39,7 +40,7 @@ private void Start() CreateNewLogEntry(logMessage); } // Binds the OnNewMessageReceived function to the OnNewMessage event - OnNewLog += CreateNewLogEntry; + OnNewLog += AddToQueue; } @@ -98,6 +99,13 @@ private void Update() { HideWindow(); } + + if (!_isWindowVisible) return; + + while (Queue.TryDequeue(out var info)) + { + CreateNewLogEntry(info); + } } private void SetupDocument() @@ -298,4 +306,16 @@ private void HideWindow() UnbindFunctions(); } + private ConcurrentQueue Queue = new(); + + + private void AddToQueue(LogInfo info) + { + Queue.Enqueue(info); + while (Queue.Count > + Modules.UI.Instance.ConfigDebugMessageLimit.Value && Queue.TryDequeue(out _)) + { + // Do nothing + } + } } \ No newline at end of file diff --git a/src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs b/src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs index c9d2cbb1..18e60b97 100644 --- a/src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs +++ b/src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs @@ -4,7 +4,9 @@ using SpaceWarp.API.Mods; using SpaceWarp.API.UI.Settings; using SpaceWarp.Backend.Modding; +using SpaceWarp.Backend.UI.Settings; using SpaceWarp.Modules; +using SpaceWarp.UI.API.UI.Settings; using UnityEngine; namespace SpaceWarp.UI.Settings; @@ -32,46 +34,12 @@ public void Start() } // Now here is where we go through every single mod -#pragma warning disable CS0618 - foreach (var mod in BepInEx.Bootstrap.Chainloader.Plugins.Where(mod => - mod.Config.Count > 0 && mod is not ConfigurationManager.ConfigurationManager)) - { - // This is where do a "Add Name" function - GenerateTitle(mod.Info.Metadata.Name).transform.SetParent(transform); - GenerateDivider().transform.SetParent(transform); - Dictionary> modConfigCategories = new(); - foreach (var config in mod.Config) - { - var section = config.Key.Section; - var conf = config.Value; - if (modConfigCategories.TryGetValue(section, out var list)) - { - list.Add(conf); - } - else - { - modConfigCategories[section] = new List { conf }; - } - } - foreach (var config in modConfigCategories) - { - var header = GenerateSectionHeader(config.Key); - header.transform.SetParent(transform); - foreach (var drawer in config.Value.Select(ModsPropertyDrawers.Drawer).Where(drawer => drawer != null)) - { - drawer.transform.SetParent(header.transform); - } - - GenerateDivider().transform.SetParent(transform); - } - } foreach (var mod in PluginList.AllEnabledAndActivePlugins.Where(mod => mod.ConfigFile != null && mod.ConfigFile.Sections.Count > 0 && mod.ConfigFile.Sections.Any(x => - mod.ConfigFile[x].Count > 0) && mod.Plugin is not BepInExModAdapter && - mod.Plugin is not BaseSpaceWarpPlugin && mod.Plugin != null)) + mod.ConfigFile[x].Count > 0) && mod.Plugin != null)) { GenerateTitle(mod.Name).transform.SetParent(transform); GenerateDivider().transform.SetParent(transform); @@ -96,7 +64,7 @@ public void Start() GenerateDivider().transform.SetParent(transform); } } - + foreach (var module in ModuleManager.AllSpaceWarpModules.Where( mod => mod.ModuleConfiguration.Sections.Count > 0 )) @@ -126,7 +94,70 @@ public void Start() GenerateDivider().transform.SetParent(transform); } } - + + foreach (var (section, entries) in SettingsMenu.RegisteredConfigFiles) + { + GenerateTitle(section).transform.SetParent(transform); + GenerateDivider().transform.SetParent(transform); + Dictionary> modConfigCategories = new(); + foreach (var sect in entries.Sections) + { + if (entries[section].Count <= 0) continue; + var list = modConfigCategories[section] = new List<(string name, IConfigEntry entry)>(); + list.AddRange(entries[section].Select( + entry => (entry, entries[section, entry]) + )); + } + + foreach (var config in modConfigCategories) + { + var header = GenerateSectionHeader(config.Key); + header.transform.SetParent(transform); + foreach (var drawer in config.Value.Select(x => ModsPropertyDrawers.Drawer(x.name, x.entry)) + .Where(drawer => drawer != null)) + { + drawer.transform.SetParent(header.transform); + } + + GenerateDivider().transform.SetParent(transform); + } + } +#pragma warning disable CS0618 + foreach (var mod in BepInEx.Bootstrap.Chainloader.Plugins.Where(mod => + (mod is not BaseSpaceWarpPlugin || (mod is BaseSpaceWarpPlugin baseSpaceWarpPlugin && + baseSpaceWarpPlugin.SWConfiguration.Sections.Count == 0)) && + mod.Config.Count > 0 && mod is not ConfigurationManager.ConfigurationManager)) + { + // This is where do a "Add Name" function + GenerateTitle(mod.Info.Metadata.Name).transform.SetParent(transform); + GenerateDivider().transform.SetParent(transform); + Dictionary> modConfigCategories = new(); + foreach (var config in mod.Config) + { + var section = config.Key.Section; + var conf = config.Value; + if (modConfigCategories.TryGetValue(section, out var list)) + { + list.Add(conf); + } + else + { + modConfigCategories[section] = new List { conf }; + } + } + + foreach (var config in modConfigCategories) + { + var header = GenerateSectionHeader(config.Key); + header.transform.SetParent(transform); + foreach (var drawer in config.Value.Select(ModsPropertyDrawers.Drawer).Where(drawer => drawer != null)) + { + drawer.transform.SetParent(header.transform); + } + + GenerateDivider().transform.SetParent(transform); + } + } } public override void OnShow()