- Basic Config & Settings working (read-only, changes must be made via debug halt).

This commit is contained in:
MapleWheels
2026-02-06 14:29:25 -05:00
committed by Maplewheels
parent e75208507d
commit 771e73a798
4 changed files with 102 additions and 75 deletions
@@ -86,6 +86,7 @@ public interface ISettingRangeBase<T> : ISettingBase<T> where T : IEquatable<T>,
/// <typeparam name="T">The value type. See <see cref="ISettingBase{T}"/></typeparam>
public interface ISettingList<T> : ISettingBase<T> where T : IEquatable<T>, IConvertible
{
bool TrySetValueByIndex(int index);
IReadOnlyList<T> Options { get; }
IReadOnlyList<string> StringOptions { get; }
}
@@ -36,18 +36,11 @@ public class SettingEntry<T> : SettingBase, ISettingBase<T>, INetworkSyncEntity
try
{
Value = (T)Convert.ChangeType(ConfigInfo.Element.GetAttributeString("Value", null), typeof(T));
DefaultValue = Value;
}
catch (Exception e) when (e is InvalidCastException or ArgumentNullException)
{
Value = default(T);
}
try
{
DefaultValue = (T)Convert.ChangeType(ConfigInfo.Element.GetAttributeString("Value", null), typeof(T));
}
catch (Exception e) when (e is InvalidCastException or ArgumentNullException)
{
DefaultValue = default(T);
}
}
@@ -2,6 +2,7 @@
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs;
using OneOf;
namespace Barotrauma.LuaCs.Data;
@@ -21,33 +22,53 @@ public class SettingsEntryRegistrar : ISettingsRegistrationProvider
public void RegisterTypeProviders(IConfigService configService, Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
{
// TODO: Replace this with a proper IFactory lookup pattern.
// ISettingBase<T>
RegisterSettingEntry<bool>(configService, "bool");
RegisterSettingEntry<byte>(configService, "byte");
RegisterSettingEntry<sbyte>(configService, "sbyte");
RegisterSettingEntry<short>(configService, "short");
RegisterSettingEntry<ushort>(configService, "ushort");
RegisterSettingEntry<int>(configService, "int");
RegisterSettingEntry<uint>(configService, "uint");
RegisterSettingEntry<long>(configService, "long");
RegisterSettingEntry<ulong>(configService, "ulong");
RegisterSettingEntry<string>(configService, "string");
RegisterSettingEntry<bool>(configService, "bool", valueChangePredicate);
RegisterSettingEntry<byte>(configService, "byte", valueChangePredicate);
RegisterSettingEntry<sbyte>(configService, "sbyte", valueChangePredicate);
RegisterSettingEntry<short>(configService, "short", valueChangePredicate);
RegisterSettingEntry<ushort>(configService, "ushort", valueChangePredicate);
RegisterSettingEntry<int>(configService, "int", valueChangePredicate);
RegisterSettingEntry<uint>(configService, "uint", valueChangePredicate);
RegisterSettingEntry<long>(configService, "long", valueChangePredicate);
RegisterSettingEntry<ulong>(configService, "ulong", valueChangePredicate);
RegisterSettingEntry<string>(configService, "string", valueChangePredicate);
// ISettingRangeBase<T>
// ISettingList
configService.RegisterSettingTypeInitializer("rangeInt", cfgInfo =>
{
return new SettingRangeInt.RangeFactory().CreateInstance(cfgInfo.Info, (val) =>
IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate));
});
configService.RegisterSettingTypeInitializer("rangeFloat", cfgInfo =>
{
return new SettingRangeFloat.RangeFactory().CreateInstance(cfgInfo.Info, (val) =>
IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate));
});
// ISettingList : Not Implemented yet
}
private void RegisterSettingEntry<T>(IConfigService configService, string typeName) where T : IEquatable<T>, IConvertible
private void RegisterSettingEntry<T>(IConfigService configService, string typeName, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) where T : IEquatable<T>, IConvertible
{
configService.RegisterSettingTypeInitializer(typeName, cfgInfo =>
{
return new SettingEntry<bool>.Factory().CreateInstance(cfgInfo.Info, (val) =>
{
return !cfgInfo.Info.Element.GetAttributeBool("ReadOnly", false)
&& cfgInfo.Info.EditableStates.HasFlag(_infoProvider?.CurrentRunState ?? RunState.Running);
});
return new SettingEntry<T>.Factory().CreateInstance(cfgInfo.Info, (val) =>
IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate));
});
}
private bool IsValueChangeAllowed(IConfigInfo info, OneOf<string, XElement, object> newValue,
Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
{
return !info.Element.GetAttributeBool("ReadOnly", false)
|| !info.EditableStates.HasFlag(_infoProvider?.CurrentRunState ?? RunState.Running)
|| valueChangePredicate is null
|| valueChangePredicate.Invoke(newValue);
}
public void Dispose()
{
if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed))