- SettingsMenuService added.

- Added Settings Tab to vanilla SettingsMenu
- Fixed ISystem implementation.
This commit is contained in:
MapleWheels
2026-02-25 15:19:01 -05:00
parent d9d980122d
commit 94556fd6e7
11 changed files with 133 additions and 37 deletions
@@ -98,6 +98,7 @@ namespace Barotrauma
//serviceProvider.RegisterServiceType<IUIStylesCollection, UIStylesCollection>(ServiceLifetime.Transient); //serviceProvider.RegisterServiceType<IUIStylesCollection, UIStylesCollection>(ServiceLifetime.Transient);
serviceProvider.RegisterServiceType<IParserServiceAsync<ResourceParserInfo, IStylesResourceInfo>, ModConfigFileParserService>(ServiceLifetime.Transient); serviceProvider.RegisterServiceType<IParserServiceAsync<ResourceParserInfo, IStylesResourceInfo>, ModConfigFileParserService>(ServiceLifetime.Transient);
serviceProvider.RegisterServiceType<IUIStylesCollection.IFactory, UIStylesCollection.Factory>(ServiceLifetime.Transient); serviceProvider.RegisterServiceType<IUIStylesCollection.IFactory, UIStylesCollection.Factory>(ServiceLifetime.Transient);
serviceProvider.RegisterServiceType<ISettingsMenuSystem, SettingsMenuSystem>(ServiceLifetime.Singleton);
} }
/// <summary> /// <summary>
@@ -0,0 +1,94 @@
using System;
using Barotrauma.Extensions;
using HarmonyLib;
using Microsoft.Xna.Framework;
namespace Barotrauma.LuaCs;
public class SettingsMenuSystem : ISettingsMenuSystem
{
private GUIFrame _menuFrame;
private GUIButton _menuOpenButton;
private readonly Harmony _harmony;
private static SettingsMenuSystem _systemInstance;
public SettingsMenuSystem()
{
_systemInstance = this;
_harmony = Harmony.CreateAndPatchAll(typeof(SettingsMenuSystem));
}
[HarmonyPatch(typeof(SettingsMenu), "CreateModsTab"), HarmonyPostfix]
private static void SettingsMenu_CreateModsTab_Post(SettingsMenu __instance)
{
_systemInstance.CreateSettingsMenu(__instance);
}
private void CreateSettingsMenu(SettingsMenu __instance)
{
var tabIndex = (SettingsMenu.Tab)Enum.GetValues<SettingsMenu.Tab>().Length;
var contentFrame = CreateNewContentFrame(tabIndex);
contentFrame.RectTransform.RelativeSize = Vector2.One;
GUIFrame CreateNewContentFrame(SettingsMenu.Tab tab)
{
if (__instance.tabContents.TryGetValue(tab, out (GUIButton Button, GUIFrame Content) tabContent))
{
return tabContent.Content;
}
var contentFr = new GUIFrame(new RectTransform(Vector2.One * 0.95f, __instance.contentFrame.RectTransform, Anchor.Center, Pivot.Center), style: null);
var button = new GUIButton(new RectTransform(Vector2.One, __instance.tabber.RectTransform, Anchor.TopLeft, Pivot.TopLeft, scaleBasis: ScaleBasis.Smallest), "", style: $"SettingsMenuTab.Mods")
{
ToolTip = TextManager.Get($"LuaCsForBarotrauma.SettingsMenu.ModSettingsButton"),
OnClicked = (b, _) =>
{
__instance.SelectTab(tab);
return false;
}
};
button.RectTransform.MaxSize = RectTransform.MaxPoint;
button.Children.ForEach(c => c.RectTransform.MaxSize = RectTransform.MaxPoint);
__instance.tabContents.Add(tab, (button, contentFr));
return contentFr;
}
}
private void DisposeMenuFrame()
{
if (_menuFrame is not null)
{
_menuFrame.Parent.RemoveChild(_menuFrame);
_menuFrame = null;
}
}
#region DISPOSAL
public void Dispose()
{
if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed))
{
return;
}
DisposeMenuFrame();
GC.SuppressFinalize(this);
}
private int _isDisposed = 0;
public bool IsDisposed
{
get => ModUtils.Threading.GetBool(ref _isDisposed);
private set => ModUtils.Threading.SetBool(ref _isDisposed, value);
}
public FluentResults.Result Reset()
{
throw new NotImplementedException();
}
#endregion
}
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs;
public interface ISettingsMenuSystem : ISystem
{
}
@@ -531,7 +531,7 @@ namespace Barotrauma
} }
}; };
#endif #endif
new GUIButton(new RectTransform(new Point(300, 30), Frame.RectTransform, Anchor.TopLeft) { AbsoluteOffset = new Point(40, 50) }, /*new GUIButton(new RectTransform(new Point(300, 30), Frame.RectTransform, Anchor.TopLeft) { AbsoluteOffset = new Point(40, 50) },
$"Open LuaCs Settings", style: "MainMenuGUIButton", color: GUIStyle.Red) $"Open LuaCs Settings", style: "MainMenuGUIButton", color: GUIStyle.Red)
{ {
IgnoreLayoutGroups = true, IgnoreLayoutGroups = true,
@@ -540,7 +540,7 @@ namespace Barotrauma
LuaCsSettingsMenu.Open(Frame.RectTransform); LuaCsSettingsMenu.Open(Frame.RectTransform);
return true; return true;
} }
}; };*/
// TODO: Implement version reading. // TODO: Implement version reading.
//string version = File.Exists(LuaCsSetup.VersionFile) ? File.ReadAllText(LuaCsSetup.VersionFile) : "Github"; //string version = File.Exists(LuaCsSetup.VersionFile) ? File.ReadAllText(LuaCsSetup.VersionFile) : "Github";
@@ -33,10 +33,10 @@ namespace Barotrauma
private GameSettings.Config unsavedConfig; private GameSettings.Config unsavedConfig;
private readonly GUIFrame mainFrame; public readonly GUIFrame mainFrame;
private readonly GUILayoutGroup tabber; public readonly GUILayoutGroup tabber;
private readonly GUIFrame contentFrame; public readonly GUIFrame contentFrame;
private readonly GUILayoutGroup bottom; private readonly GUILayoutGroup bottom;
public readonly WorkshopMenu WorkshopMenu; public readonly WorkshopMenu WorkshopMenu;
@@ -103,7 +103,7 @@ namespace Barotrauma
newContent.Visible = true; newContent.Visible = true;
} }
private readonly Dictionary<Tab, (GUIButton Button, GUIFrame Content)> tabContents; public readonly Dictionary<Tab, (GUIButton Button, GUIFrame Content)> tabContents;
public void SelectTab(Tab tab) public void SelectTab(Tab tab)
{ {
@@ -149,7 +149,7 @@ namespace Barotrauma
return content; return content;
} }
private static (GUILayoutGroup Left, GUILayoutGroup Right) CreateSidebars(GUIFrame parent, bool split = false) public static (GUILayoutGroup Left, GUILayoutGroup Right) CreateSidebars(GUIFrame parent, bool split = false)
{ {
GUILayoutGroup layout = new GUILayoutGroup(new RectTransform(Vector2.One, parent.RectTransform), isHorizontal: true); GUILayoutGroup layout = new GUILayoutGroup(new RectTransform(Vector2.One, parent.RectTransform), isHorizontal: true);
GUILayoutGroup left = new GUILayoutGroup(new RectTransform((0.4875f, 1.0f), layout.RectTransform), isHorizontal: false); GUILayoutGroup left = new GUILayoutGroup(new RectTransform((0.4875f, 1.0f), layout.RectTransform), isHorizontal: false);
@@ -166,29 +166,29 @@ namespace Barotrauma
return (left, right); return (left, right);
} }
private static GUILayoutGroup CreateCenterLayout(GUIFrame parent) public static GUILayoutGroup CreateCenterLayout(GUIFrame parent)
{ {
return new GUILayoutGroup(new RectTransform((0.5f, 1.0f), parent.RectTransform, Anchor.TopCenter, Pivot.TopCenter)) { ChildAnchor = Anchor.TopCenter }; return new GUILayoutGroup(new RectTransform((0.5f, 1.0f), parent.RectTransform, Anchor.TopCenter, Pivot.TopCenter)) { ChildAnchor = Anchor.TopCenter };
} }
private static RectTransform NewItemRectT(GUILayoutGroup parent) public static RectTransform NewItemRectT(GUILayoutGroup parent)
=> new RectTransform((1.0f, 0.06f), parent.RectTransform, Anchor.CenterLeft); => new RectTransform((1.0f, 0.06f), parent.RectTransform, Anchor.CenterLeft);
private static void Spacer(GUILayoutGroup parent) public static void Spacer(GUILayoutGroup parent)
{ {
new GUIFrame(new RectTransform((1.0f, 0.03f), parent.RectTransform, Anchor.CenterLeft), style: null); new GUIFrame(new RectTransform((1.0f, 0.03f), parent.RectTransform, Anchor.CenterLeft), style: null);
} }
private static GUITextBlock Label(GUILayoutGroup parent, LocalizedString str, GUIFont font) public static GUITextBlock Label(GUILayoutGroup parent, LocalizedString str, GUIFont font)
{ {
return new GUITextBlock(NewItemRectT(parent), str, font: font); return new GUITextBlock(NewItemRectT(parent), str, font: font);
} }
private static void DropdownEnum<T>(GUILayoutGroup parent, Func<T, LocalizedString> textFunc, Func<T, LocalizedString>? tooltipFunc, T currentValue, public static void DropdownEnum<T>(GUILayoutGroup parent, Func<T, LocalizedString> textFunc, Func<T, LocalizedString>? tooltipFunc, T currentValue,
Action<T> setter) where T : Enum Action<T> setter) where T : Enum
=> Dropdown(parent, textFunc, tooltipFunc, (T[])Enum.GetValues(typeof(T)), currentValue, setter); => Dropdown(parent, textFunc, tooltipFunc, (T[])Enum.GetValues(typeof(T)), currentValue, setter);
private static GUIDropDown Dropdown<T>(GUILayoutGroup parent, Func<T, LocalizedString> textFunc, Func<T, LocalizedString>? tooltipFunc, IReadOnlyList<T> values, T currentValue, Action<T> setter) public static GUIDropDown Dropdown<T>(GUILayoutGroup parent, Func<T, LocalizedString> textFunc, Func<T, LocalizedString>? tooltipFunc, IReadOnlyList<T> values, T currentValue, Action<T> setter)
{ {
var dropdown = new GUIDropDown(NewItemRectT(parent), elementCount: values.Count); var dropdown = new GUIDropDown(NewItemRectT(parent), elementCount: values.Count);
values.ForEach(v => dropdown.AddItem(text: textFunc(v), userData: v, toolTip: tooltipFunc?.Invoke(v) ?? null)); values.ForEach(v => dropdown.AddItem(text: textFunc(v), userData: v, toolTip: tooltipFunc?.Invoke(v) ?? null));
@@ -204,7 +204,7 @@ namespace Barotrauma
return dropdown; return dropdown;
} }
private static (GUIScrollBar slider, GUITextBlock label) Slider(GUILayoutGroup parent, Vector2 range, int steps, Func<float, string> labelFunc, float currentValue, Action<float> setter, LocalizedString? tooltip = null) public static (GUIScrollBar slider, GUITextBlock label) Slider(GUILayoutGroup parent, Vector2 range, int steps, Func<float, string> labelFunc, float currentValue, Action<float> setter, LocalizedString? tooltip = null)
{ {
var layout = new GUILayoutGroup(NewItemRectT(parent), isHorizontal: true); var layout = new GUILayoutGroup(NewItemRectT(parent), isHorizontal: true);
var slider = new GUIScrollBar(new RectTransform((0.72f, 1.0f), layout.RectTransform), style: "GUISlider") var slider = new GUIScrollBar(new RectTransform((0.72f, 1.0f), layout.RectTransform), style: "GUISlider")
@@ -229,7 +229,7 @@ namespace Barotrauma
return (slider, label); return (slider, label);
} }
private static GUITickBox Tickbox(GUILayoutGroup parent, LocalizedString label, LocalizedString tooltip, bool currentValue, Action<bool> setter) public static GUITickBox Tickbox(GUILayoutGroup parent, LocalizedString label, LocalizedString tooltip, bool currentValue, Action<bool> setter)
{ {
return new GUITickBox(NewItemRectT(parent), label) return new GUITickBox(NewItemRectT(parent), label)
{ {
@@ -243,9 +243,9 @@ namespace Barotrauma
}; };
} }
private string Percentage(float v) => ToolBox.GetFormattedPercentage(v); public string Percentage(float v) => ToolBox.GetFormattedPercentage(v);
private static int Round(float v) => MathUtils.RoundToInt(v); public static int Round(float v) => MathUtils.RoundToInt(v);
private void CreateGraphicsTab() private void CreateGraphicsTab()
{ {
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<contentpackage name="LuaCsForBarotrauma"> <contentpackage name="LuaCsForBarotrauma">
<!--<Text file="%ModDir%/Texts/English.xml"/>--> <Text file="%ModDir%/Texts/English.xml"/>
<!--<Text file="%ModDir%/Texts/Portuguese.xml"/>--> <!--<Text file="%ModDir%/Texts/Portuguese.xml"/>-->
</contentpackage> </contentpackage>
@@ -7,6 +7,7 @@ using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using LightInject; using LightInject;
using Microsoft.Toolkit.Diagnostics;
namespace Barotrauma.LuaCs; namespace Barotrauma.LuaCs;
@@ -15,14 +16,11 @@ public class ServicesProvider : IServicesProvider
{ {
private ServiceContainer _serviceContainerInst; private ServiceContainer _serviceContainerInst;
private ServiceContainer ServiceContainer => _serviceContainerInst; private ServiceContainer ServiceContainer => _serviceContainerInst;
/// <summary>
/// Definition: [Key: InterfaceType, Value: ConcreteTypes]
/// </summary>
private readonly ConcurrentDictionary<Type, ConcurrentBag<Type>> _systemTypeDefs = new();
/// <summary> /// <summary>
/// Definition: [Key: ConcreteType, Value: TypeInstance] /// Definition: [Key: ConcreteType, Value: TypeInstance]
/// </summary> /// </summary>
private readonly ConcurrentDictionary<Type, ISystem> _systemInstances = new(); private ImmutableArray<ISystem> _systemInstances = ImmutableArray<ISystem>.Empty;
private readonly ReaderWriterLockSlim _serviceLock = new(); private readonly ReaderWriterLockSlim _serviceLock = new();
public ServicesProvider() public ServicesProvider()
@@ -41,7 +39,6 @@ public class ServicesProvider : IServicesProvider
if (typeof(TSvcInterface).IsAssignableTo(typeof(ISystem))) if (typeof(TSvcInterface).IsAssignableTo(typeof(ISystem)))
{ {
lifetimeInstance = new PerContainerLifetime(); lifetimeInstance = new PerContainerLifetime();
_systemTypeDefs.GetOrAdd(typeof(TSvcInterface), (type) => new ConcurrentBag<Type>());
} }
if (lifetimeInstance is null) if (lifetimeInstance is null)
@@ -87,10 +84,9 @@ public class ServicesProvider : IServicesProvider
} }
// ISystem services must run as a lifetime singleton // ISystem services must run as a lifetime singleton
if (typeof(TSvcInterface).IsAssignableTo(typeof(ISystem))) if (typeof(TService).IsAssignableTo(typeof(ISystem)))
{ {
lifetimeInstance = new PerContainerLifetime(); lifetimeInstance = new PerContainerLifetime();
_systemTypeDefs.GetOrAdd(typeof(TSvcInterface), (type) => new ConcurrentBag<Type>());
} }
if (lifetimeInstance is null) if (lifetimeInstance is null)
@@ -142,15 +138,15 @@ public class ServicesProvider : IServicesProvider
try try
{ {
_serviceLock.EnterWriteLock(); _serviceLock.EnterWriteLock();
ServiceContainer?.Compile(); ServiceContainer!.Compile();
foreach (var typeDef in _systemTypeDefs.Values.SelectMany(type => type)) if (!_systemInstances.IsDefaultOrEmpty)
{ {
if (_systemInstances.ContainsKey(typeDef)) ThrowHelper.ThrowInvalidOperationException($"Systems are already instanced!");
{
continue;
}
_systemInstances[typeDef] = (ISystem)ServiceContainer?.TryGetInstance(typeDef);
} }
_systemInstances = ServiceContainer.GetAllInstances(typeof(ISystem))
.Select(obj => (ISystem)obj)
.ToImmutableArray();
} }
finally finally
{ {
@@ -250,7 +246,7 @@ public class ServicesProvider : IServicesProvider
try try
{ {
_serviceLock.EnterWriteLock(); _serviceLock.EnterWriteLock();
foreach (var system in _systemInstances.Values) foreach (var system in _systemInstances)
{ {
try try
{ {
@@ -261,8 +257,7 @@ public class ServicesProvider : IServicesProvider
// ignored, no logging services available. // ignored, no logging services available.
} }
} }
_systemInstances.Clear(); _systemInstances = ImmutableArray<ISystem>.Empty;
_systemTypeDefs.Clear();
_serviceContainerInst?.Dispose(); _serviceContainerInst?.Dispose();
_serviceContainerInst = new ServiceContainer(); _serviceContainerInst = new ServiceContainer();
} }