Skip to content

Commit

Permalink
Possible fix for both callbacks and the threading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Dec 28, 2023
1 parent 8fb94a1 commit 7a1d323
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 38 deletions.
23 changes: 23 additions & 0 deletions src/SpaceWarp.UI/API/UI/Settings/SettingsMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using JetBrains.Annotations;
using SpaceWarp.API.Configuration;

namespace SpaceWarp.UI.API.UI.Settings;

[PublicAPI]
public static class SettingsMenu
{
/// <summary>
/// Contains a list of all the registered config files
/// </summary>
public static Dictionary<string, IConfigFile> RegisteredConfigFiles = new();

/// <summary>
/// Register a manually created config file for the settings menu
/// </summary>
/// <param name="section">The section name for the config file</param>
/// <param name="file">The file itself</param>
public static void RegisterConfigFile(string section, IConfigFile file)
{
RegisteredConfigFiles[section] = file;
}
}
22 changes: 21 additions & 1 deletion src/SpaceWarp.UI/UI/Console/SpaceWarpConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Concurrent;
using BepInEx.Logging;
using KSP.Game;
using UitkForKsp2.API;
Expand Down Expand Up @@ -39,7 +40,7 @@ private void Start()
CreateNewLogEntry(logMessage);
}
// Binds the OnNewMessageReceived function to the OnNewMessage event
OnNewLog += CreateNewLogEntry;
OnNewLog += AddToQueue;
}


Expand Down Expand Up @@ -98,6 +99,13 @@ private void Update()
{
HideWindow();
}

if (!_isWindowVisible) return;

while (Queue.TryDequeue(out var info))
{
CreateNewLogEntry(info);
}
}

private void SetupDocument()
Expand Down Expand Up @@ -298,4 +306,16 @@ private void HideWindow()
UnbindFunctions();
}

private ConcurrentQueue<LogInfo> Queue = new();


private void AddToQueue(LogInfo info)
{
Queue.Enqueue(info);
while (Queue.Count >
Modules.UI.Instance.ConfigDebugMessageLimit.Value && Queue.TryDequeue(out _))
{
// Do nothing
}
}
}
105 changes: 68 additions & 37 deletions src/SpaceWarp.UI/UI/Settings/ModsSubMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, List<ConfigEntryBase>> 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<ConfigEntryBase> { 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);
Expand All @@ -96,7 +64,7 @@ public void Start()
GenerateDivider().transform.SetParent(transform);
}
}

foreach (var module in ModuleManager.AllSpaceWarpModules.Where(
mod => mod.ModuleConfiguration.Sections.Count > 0
))
Expand Down Expand Up @@ -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<string, List<(string name, IConfigEntry entry)>> 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<string, List<ConfigEntryBase>> 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<ConfigEntryBase> { 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()
Expand Down

0 comments on commit 7a1d323

Please sign in to comment.