diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/IDisplayableConfig.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/IDisplayableConfig.cs deleted file mode 100644 index 789c00ac0..000000000 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/IDisplayableConfig.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Collections.Generic; -using Barotrauma.LuaCs.Data; - -namespace Barotrauma.LuaCs.Configuration; - - -/// -/// Base type of all menu displayable types. -/// -public interface IDisplayableConfigBase : IDataInfo, IConfigDisplayInfo -{ - /// - /// Whether the current config is editable. - /// - bool IsEditable { get; } - /// - /// Used to indicate the implemented interface and targeted display logic. - /// - static virtual DisplayType DisplayOption => DisplayType.Undefined; -} - -public interface IDisplayableConfigBase : IDisplayableConfigBase -{ - void SetValue(TValue value); - TDisplay GetDisplayValue(); -} - -public interface IDisplayableConfigBool : IDisplayableConfigBase -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Boolean; -} - -public interface IDisplayableConfigText : IDisplayableConfigBase -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Text; -} - -public interface IDisplayableConfigInt : IDisplayableConfigBase -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Integer; -} - -public interface IDisplayableConfigFloat : IDisplayableConfigBase -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Float; -} - -public interface IDisplayableConfigSliderInt : IDisplayableConfigBase<(int Min, int Max, int Value, int Steps), int> -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.SliderInt; -} - -public interface IDisplayableConfigSliderFloat : IDisplayableConfigBase<(float Min, float Max, float Value, int Steps), float> -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.SliderFloat; -} - -public interface IDisplayableConfigDropdown : IDisplayableConfigBase, string> -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Dropdown; -} - -/// -/// Allows completely custom-designed UI for this configuration component. -/// -public interface IDisplayableConfigCustom : IDisplayableConfigBase -{ - static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Custom; - /// - /// Draw your menu settings option. - /// - /// Parent layout component. - void DrawComponent(GUILayoutGroup layoutGroup); - /// - /// Called when the config element is set to be disposed to allow for cleanup. - /// - void DisposeGUI(); - /// - /// Called when the UI indicates to save the current value as permanent. - /// - void OnValueSaved(); - /// - /// Called when the UI indicates to discard the currently displayed value and revert to the last saved value. - /// - void OnValueDiscarded(); -} - - - -/// -/// Indicates the intended display and feedback logic to be used by the . -///
[Important] -///
The type must implement the indicated interface for the selected option, or it will not be displayed. -///
-public enum DisplayType -{ - /// - /// Will not be displayed in menus. - /// - Undefined, - /// - /// Will be shown as a checkbox. - ///
[Requires()] - ///
- Boolean, - /// - /// Shown as an editable text input. - ///
[Requires()] - ///
- Text, - /// - /// Shown as number input (no decimal input). - ///
[Requires()] - ///
- Integer, - /// - /// Shown as a number input. - ///
[Requires()] - ///
- Float, - /// - /// Shown as a slider, values parsed as integers. - ///
[Requires()] - ///
- SliderInt, - /// - /// Shown as a slider, values parsed as single-precision decimal numbers. - ///
[Requires()] - ///
- SliderFloat, - /// - /// Shown as a menu, values parsed as strings. - ///
[Requires()] - ///
- Dropdown, - /// - /// UI Display is implemented by inheritor and actioned by a call to . - ///
[Requires()] - ///
- Custom -} diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/SettingControl.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/SettingControl.cs index b0812f425..bf18aa122 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/SettingControl.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/SettingControl.cs @@ -8,31 +8,23 @@ using OneOf; namespace Barotrauma.LuaCs.Configuration; -public class SettingControl : ISettingControl +public class SettingControl : SettingBase, ISettingControl { - public string InternalName { get; } - public ContentPackage OwnerPackage { get; } - public bool Equals(ISettingBase other) + public SettingControl(IConfigInfo configInfo) : base(configInfo) { - throw new NotImplementedException(); } - public void Dispose() + protected override void OnDispose() { - throw new NotImplementedException(); + OnValueChanged = null; } - public IConfigDisplayInfo GetDisplayInfo() - { - throw new NotImplementedException(); - } + public override Type GetValueType() => typeof(KeyOrMouse); + public override string GetStringValue() => Value.ToString(); - public Type GetValueType() => typeof(KeyOrMouse); - public string GetStringValue() => Value.ToString(); + public override string GetDefaultStringValue() => new KeyOrMouse(Keys.NumLock).ToString(); - public string GetDefaultStringValue() => new KeyOrMouse(Keys.NumLock).ToString(); - - public bool TrySetValue(OneOf value) + public override bool TrySetValue(OneOf value) { var newVal = value.Match( (string v) => GetKeyOrMouse(v), @@ -77,12 +69,8 @@ public class SettingControl : ISettingControl } - public event Action OnValueChanged; - public OneOf GetSerializableValue() - { - return Value.ToString(); - } - + public override event Action OnValueChanged; + public override OneOf GetSerializableValue() => Value.ToString(); public KeyOrMouse Value { get; private set; } = new KeyOrMouse(Keys.NumLock); public bool TrySetValue(KeyOrMouse value) diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ConfigService.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ConfigService.cs index f2294c49c..556b46757 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ConfigService.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ConfigService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Immutable; +using System.Linq; using Barotrauma.LuaCs.Configuration; using Barotrauma.LuaCs.Data; using Barotrauma.Networking; @@ -9,18 +10,14 @@ namespace Barotrauma.LuaCs; public sealed partial class ConfigService { - public ImmutableArray GetDisplayableConfigs() + public ImmutableArray GetDisplayableConfigs() { - throw new NotImplementedException(); - } + using var _ = _operationLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult(); + IService.CheckDisposed(this); - public ImmutableArray GetDisplayableConfigsForPackage(ContentPackage package) - { - throw new NotImplementedException(); - } - - public Result AddConfigControl(IConfigInfo configInfo) - { - throw new NotImplementedException(); + return _settingsInstances.Values + .Where(s => !s.IsDisposed) + .Where(s => s.GetDisplayInfo().ShowInMenus) + .ToImmutableArray(); } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ModsGameplaySettingsMenu.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ModsGameplaySettingsMenu.cs index 97a156c25..7db7ae74f 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ModsGameplaySettingsMenu.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/ModsGameplaySettingsMenu.cs @@ -1,15 +1,25 @@ -using Microsoft.Xna.Framework; +using System.Collections.Immutable; +using Microsoft.Xna.Framework; using System.Linq; +using Barotrauma.LuaCs.Data; namespace Barotrauma.LuaCs; internal sealed class ModsGameplaySettingsMenu : ModsSettingsMenu { + private readonly ImmutableArray _settingsInstancesGameplay; + public ModsGameplaySettingsMenu(GUIFrame contentFrame, IPackageManagementService packageManagementService, IConfigService configService, SettingsMenu settingsMenuInstance) : base(contentFrame, packageManagementService, configService, settingsMenuInstance) { + + _settingsInstancesGameplay = configService.GetDisplayableConfigs() + .Where(s => s is not ISettingControl) + .ToImmutableArray(); + + var mainLayoutGroup = new GUILayoutGroup(new RectTransform(new Vector2(1f, 1f), contentFrame.RectTransform, Anchor.Center), false, Anchor.TopLeft); // page title var menuTitleLayoutGroup = new GUILayoutGroup( @@ -30,7 +40,7 @@ internal sealed class ModsGameplaySettingsMenu : ModsSettingsMenu { OnTextChangedDelegate = (btn, txt) => { - // TODO: Execute filter here + GenerateDisplayFromFilter(txt); return true; } }; @@ -45,21 +55,20 @@ internal sealed class ModsGameplaySettingsMenu : ModsSettingsMenu var cpList = packageManagementService.GetAllLoadedPackages().OrderBy(cp => cp.Name == "Vanilla" ? 0 : 1).ThenBy(cp => cp.Name).ToList(); var modSelectDropDown = GUIUtil.Dropdown(modCategoryDisplayGroup, cp => cp.Name == "Vanilla" ? "All" : cp.Name, null, cpList, cpList[0], cp => { - // TODO: filter selections by adding it to the search bar + // TODO: apply filter text }, Vector2.One, 2); - - + void GenerateDisplayFromFilter(string text) { - + } - void GenerateCategoryListDisplay(GUILayoutGroup layoutGroup) + void GenerateCategoryListDisplay(GUILayoutGroup layoutGroup, ImmutableArray settings) { } - void GenerateSettingsListDisplay(GUILayoutGroup layoutGroup) + void GenerateSettingsListDisplay(GUILayoutGroup layoutGroup, ImmutableArray settings) { } diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/_Interfaces/IConfigService.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/_Interfaces/IConfigService.cs index 34b44c316..1a1ca2178 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/_Interfaces/IConfigService.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/_Interfaces/IConfigService.cs @@ -10,8 +10,5 @@ namespace Barotrauma.LuaCs; public partial interface IConfigService { - ImmutableArray GetDisplayableConfigs(); - ImmutableArray GetDisplayableConfigsForPackage(ContentPackage package); - - FluentResults.Result AddConfigControl(IConfigInfo configInfo); + ImmutableArray GetDisplayableConfigs(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs index 766a4e02f..aca15df9d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs @@ -29,6 +29,7 @@ public interface ISettingBase : IDataInfo, IEquatable, IDisposable #if CLIENT IConfigDisplayInfo GetDisplayInfo(); #endif + bool IsDisposed { get; } Type GetValueType(); string GetStringValue(); string GetDefaultStringValue(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingBase.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingBase.cs index a79c453c5..7e9ee3697 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingBase.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingBase.cs @@ -30,12 +30,14 @@ public abstract class SettingBase : ISettingBase } private int _isDisposed = 0; - protected virtual bool IsDisposed + public virtual bool IsDisposed { get => ModUtils.Threading.GetBool(ref _isDisposed); private set => ModUtils.Threading.SetBool(ref _isDisposed, value); } + protected abstract void OnDispose(); + public virtual void Dispose() { if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed)) @@ -43,8 +45,8 @@ public abstract class SettingBase : ISettingBase return; } + OnDispose(); ConfigInfo = null; - OnValueChanged = null; GC.SuppressFinalize(this); } @@ -55,6 +57,6 @@ public abstract class SettingBase : ISettingBase public abstract string GetDefaultStringValue(); public abstract bool TrySetValue(OneOf value); - public event Action OnValueChanged; + public abstract event Action OnValueChanged; public abstract OneOf GetSerializableValue(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs index e495ec43e..82c748691 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs @@ -65,7 +65,7 @@ public class SettingEntry : SettingBase, ISettingBase, INetworkSyncVar whe { return false; } - + OnValueChanged?.Invoke(this); #if CLIENT if (GameMain.IsMultiplayer && SyncType is NetSync.ClientOneWay or NetSync.TwoWay) { @@ -96,6 +96,11 @@ public class SettingEntry : SettingBase, ISettingBase, INetworkSyncVar whe return true; } + protected override void OnDispose() + { + ValueChangePredicate = null; + } + public override Type GetValueType() => typeof(T); public override string GetStringValue() => Value.ToString(); @@ -135,6 +140,8 @@ public class SettingEntry : SettingBase, ISettingBase, INetworkSyncVar whe return !isFailed && TrySetValue(typeConvertedValue); } + public override event Action OnValueChanged; + public override OneOf GetSerializableValue() => Value.ToString(); // -- Networking diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/ConfigService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/ConfigService.cs index d83690172..ec3c131c6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/ConfigService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/ConfigService.cs @@ -339,6 +339,7 @@ public sealed partial class ConfigService : IConfigService var toProcessDocs = taskResults .Where(tr => !tr.IsDefaultOrEmpty) .SelectMany(tr => tr) + .Where(icf => icf is not null) .ToImmutableArray(); var instanceQueue = new Queue<(IConfigInfo configInfo, Func<(IConfigService ConfigService, IConfigInfo Info), ISettingBase> factory)>(); @@ -604,6 +605,7 @@ public sealed partial class ConfigService : IConfigService result.WithReasons(_eventService.PublishEvent(sub => sub.OnSettingInstanceDisposed(setting)).Reasons); try { + _settingsInstances.TryRemove((setting.OwnerPackage, setting.InternalName), out _); setting.Dispose(); } catch (Exception e)