- Some work on config service.

This commit is contained in:
MapleWheels
2026-02-04 21:52:29 -05:00
committed by Maplewheels
parent 9cc20a03c0
commit 863ee23583
15 changed files with 420 additions and 549 deletions
@@ -1,18 +0,0 @@
using System;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs.Configuration;
public partial interface ISettingBase : IDataInfo, IEquatable<ISettingBase>, IDisposable
{
Type GetValueType();
string GetStringValue();
bool TrySetValue(OneOf.OneOf<string, XElement> value);
bool IsAssignable(OneOf.OneOf<string, XElement> value);
event Func<OneOf.OneOf<string, XElement>, bool> IsNewValueValid;
event Action<ISettingBase> OnValueChanged;
OneOf.OneOf<string, XElement> GetSerializableValue();
}
@@ -1,12 +0,0 @@
using System;
using Barotrauma.LuaCs.Services;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingEntry<T> : ISettingBase, INetworkSyncEntity where T : IEquatable<T>
{
T Value { get; }
bool TrySetValue(T value);
bool IsAssignable(T value);
new event Action<ISettingEntry<T>> OnValueChanged;
}
@@ -1,9 +0,0 @@
using System;
using Barotrauma.LuaCs.Services;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingEnum : ISettingBase, INetworkSyncEntity
{
}
@@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
using Barotrauma.LuaCs.Services;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingList<T> : ISettingEntry<T>, INetworkSyncEntity where T : IEquatable<T>
{
IReadOnlyList<T> Options { get; }
new event Action<ISettingList<T>> OnValueChanged;
}
@@ -1,13 +0,0 @@
using System;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingRangeEntry<T> : ISettingEntry<T> where T : IConvertible, IEquatable<T>
{
T MinValue { get; }
T MaxValue { get; }
int GetStepCount();
float GetRangeMin();
float GetRangeMax();
}
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingBase : IDataInfo, IEquatable<ISettingBase>, IDisposable
{
/// <summary>
/// Settings production factory. Should be implemented by all types and registered with the Dependency Injector.
/// </summary>
/// <typeparam name="T">An interface type derived from <see cref="ISettingBase"/>.</typeparam>
public interface IFactory<out T> where T : ISettingBase
{
/// <summary>
/// Creates an instance of the given <see cref="ISettingBase"/> type.
/// </summary>
/// <param name="configInfo">Configuration information.</param>
/// <param name="valueChangePredicate">Called before a new value is assigned. Returns a boolean whether to allow
/// the value to be changed to the one given.</param>
/// <returns></returns>
T CreateInstance([NotNull]IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate);
}
#if CLIENT
IConfigDisplayInfo GetDisplayInfo();
#endif
Type GetValueType();
string GetStringValue();
string GetDefaultStringValue();
bool TrySetValue(OneOf.OneOf<string, XElement> value);
event Action<ISettingBase> OnValueChanged;
OneOf.OneOf<string, XElement> GetSerializableValue();
}
/// <summary>
/// Creates a setting representing a value of the given <see cref="Type"/>. Must be a compatible listed type. <br/>
/// </summary>
/// <typeparam name="T">
/// <b>Compatible Types:</b><br/>
/// Any primitive type:<br/>
/// - <see cref="byte"/><br/>
/// - <see cref="sbyte"/><br/>
/// - <see cref="ushort"/><br/>
/// - <see cref="short"/><br/>
/// - <see cref="int"/><br/>
/// - <see cref="uint"/><br/>
/// - <see cref="long"/><br/>
/// - <see cref="ulong"/><br/>
/// - <see cref="float"/><br/>
/// - <see cref="double"/><br/>
/// Extension types and Enums: <br/>
/// - <see cref="string"/><br/>
/// - <see cref="Enum"/><br/>
/// </typeparam>
public interface ISettingBase<T> : ISettingBase where T : IEquatable<T>, IConvertible
{
[NotNull]
T Value { get; }
[NotNull]
T DefaultValue { get; }
bool TrySetValue(T value);
}
/// <summary>
/// Creates a setting representing a value of the given <see cref="Type"/> with a minimum and maximum value.
/// Must be a type compatible with <see cref="ISettingBase{T}"/>.
/// </summary>
/// <typeparam name="T">The value type. See <see cref="ISettingBase{T}"/></typeparam>
public interface ISettingRangeBase<T> : ISettingBase<T> where T : IEquatable<T>, IConvertible
{
T MinValue { get; }
T MaxValue { get; }
int IncrementalSteps { get; }
}
/// <summary>
/// Creates a setting representing a value of the given <see cref="Type"/> with a distinct list of selectable values.
/// Must be a type compatible with <see cref="ISettingBase{T}"/>.
/// </summary>
/// <typeparam name="T">The value type. See <see cref="ISettingBase{T}"/></typeparam>
public interface ISettingList<T> : ISettingBase<T> where T : IEquatable<T>, IConvertible
{
IReadOnlyList<T> Options { get; }
IReadOnlyList<string> StringOptions { get; }
}
@@ -0,0 +1,60 @@
using System;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using OneOf;
namespace Barotrauma.LuaCs.Configuration;
public abstract class SettingBase : ISettingBase
{
protected SettingBase(IConfigInfo configInfo)
{
ConfigInfo = configInfo;
}
protected IConfigInfo ConfigInfo { get; private set; }
public string InternalName => ConfigInfo.InternalName;
public ContentPackage OwnerPackage => ConfigInfo.OwnerPackage;
#if CLIENT
public IConfigDisplayInfo GetDisplayInfo() => ConfigInfo;
#endif
public virtual bool Equals(ISettingBase other)
{
return other is not null && (
ReferenceEquals(this, other) || !IsDisposed &&
OwnerPackage == other.OwnerPackage &&
InternalName.Equals(other.InternalName));
}
private int _isDisposed = 0;
protected virtual bool IsDisposed
{
get => ModUtils.Threading.GetBool(ref _isDisposed);
private set => ModUtils.Threading.SetBool(ref _isDisposed, value);
}
public virtual void Dispose()
{
if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed))
{
return;
}
ConfigInfo = null;
OnValueChanged = null;
GC.SuppressFinalize(this);
}
// -- Must be implemented
public abstract Type GetValueType();
public abstract string GetStringValue();
public abstract string GetDefaultStringValue();
public abstract bool TrySetValue(OneOf<string, XElement> value);
public event Action<ISettingBase> OnValueChanged;
public abstract OneOf<string, XElement> GetSerializableValue();
}
@@ -1,85 +1,274 @@
using System;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
using Microsoft.Toolkit.Diagnostics;
using OneOf;
namespace Barotrauma.LuaCs.Configuration;
public class SettingEntry<T> : ISettingEntry<T> where T : IEquatable<T>
public class SettingEntry<T> : SettingBase, ISettingBase<T>, INetworkSyncEntity where T : IEquatable<T>, IConvertible
{
public string InternalName { get; }
public ContentPackage OwnerPackage { get; }
public bool Equals(ISettingBase other)
public class Factory : ISettingBase.IFactory<ISettingBase<T>>
{
throw new NotImplementedException();
public ISettingBase<T> CreateInstance(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
{
Guard.IsNotNull(configInfo, nameof(configInfo));
return new SettingEntry<T>(configInfo, valueChangePredicate);
}
}
public SettingEntry(IConfigInfo configInfo,
Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
: base(configInfo)
{
if (!(
typeof(T).IsEnum ||
typeof(T).IsPrimitive ||
typeof(T) == typeof(string)))
{
ThrowHelper.ThrowArgumentException($"{nameof(ISettingBase<T>)}: The type of {nameof(T)} is not an allowed type.");
}
ValueChangePredicate = valueChangePredicate;
try
{
Value = (T)Convert.ChangeType(ConfigInfo.Element.GetAttributeString("Value", null), typeof(T));
}
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);
}
}
public void Dispose()
protected Func<OneOf<string, XElement, object>, bool> ValueChangePredicate;
public T Value { get; protected set; }
public T DefaultValue { get; protected set; }
public virtual bool TrySetValue(T value)
{
throw new NotImplementedException();
if (value is null)
{
return false;
}
if (ValueChangePredicate != null && !ValueChangePredicate(value))
{
return false;
}
Value = value;
return true;
}
public Type GetValueType()
public override Type GetValueType() => typeof(T);
public override string GetStringValue() => Value.ToString();
public override string GetDefaultStringValue() => DefaultValue.ToString();
public override bool TrySetValue(OneOf<string, XElement> value)
{
throw new NotImplementedException();
bool isFailed = false;
var typeConvertedValue = value.Match<T>(
(string val) =>
{
try
{
return (T)Convert.ChangeType(val, typeof(T));
}
catch (Exception e)
{
// ignored
isFailed = true;
return default(T);
}
},
(XElement val) =>
{
try
{
return (T)Convert.ChangeType(val.GetAttributeString("Value", null), typeof(T));
}
catch (Exception e)
{
isFailed = true;
return default(T);
}
});
return isFailed || TrySetValue(typeConvertedValue);
}
public string GetStringValue()
public override OneOf<string, XElement> GetSerializableValue() => Value.ToString();
// -- Networking
protected IEntityNetworkingService NetworkingService;
public ulong InstanceId => NetworkingService?.GetNetworkIdForInstance(this) ?? 0ul;
public void SetNetworkOwner(IEntityNetworkingService networkingService)
{
throw new NotImplementedException();
NetworkingService = networkingService;
if (NetworkingService is null)
{
return;
}
NetworkingService.RegisterNetVar(this);
}
public bool TrySetValue(OneOf<string, XElement> value)
{
throw new NotImplementedException();
}
public bool IsAssignable(OneOf<string, XElement> value)
{
throw new NotImplementedException();
}
public event Func<OneOf<string, XElement>, bool> IsNewValueValid;
public T Value { get; }
public bool TrySetValue(T value)
{
throw new NotImplementedException();
}
public bool IsAssignable(T value)
{
throw new NotImplementedException();
}
event Action<ISettingEntry<T>> ISettingEntry<T>.OnValueChanged
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
event Action<ISettingBase> ISettingBase.OnValueChanged
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
public OneOf<string, XElement> GetSerializableValue()
{
throw new NotImplementedException();
}
public Guid InstanceId { get; }
public NetSync SyncType { get; }
public ClientPermissions WritePermissions { get; }
public NetSync SyncType => ConfigInfo.NetSync;
// needs to be added IConfigInfo
public ClientPermissions WritePermissions => throw new NotImplementedException();
public void ReadNetMessage(IReadMessage message)
{
throw new NotImplementedException();
if (SyncType == NetSync.None || NetworkingService is null)
{
return;
}
try
{
if (typeof(T).IsEnum)
{
TrySetValue((T)(object)message.ReadInt32());
}
// No...there's no better way to do this...
var typeCode = Type.GetTypeCode(typeof(T));
switch (typeCode)
{
case TypeCode.Boolean:
TrySetValue((T)Convert.ChangeType(message.ReadBoolean(), typeCode));
return;
case TypeCode.Byte:
TrySetValue((T)Convert.ChangeType(message.ReadByte(), typeCode));
return;
// SByte not supported by interface
case TypeCode.SByte:
TrySetValue((T)Convert.ChangeType(message.ReadInt16(), typeCode));
return;
case TypeCode.Int16:
TrySetValue((T)Convert.ChangeType(message.ReadInt16(), typeCode));
return;
case TypeCode.Char:
case TypeCode.UInt16:
TrySetValue((T)Convert.ChangeType(message.ReadUInt16(), typeCode));
return;
case TypeCode.Int32:
TrySetValue((T)Convert.ChangeType(message.ReadInt32(), typeCode));
return;
case TypeCode.UInt32:
TrySetValue((T)Convert.ChangeType(message.ReadUInt32(), typeCode));
return;
case TypeCode.Int64:
TrySetValue((T)Convert.ChangeType(message.ReadInt64(), typeCode));
return;
case TypeCode.UInt64:
TrySetValue((T)Convert.ChangeType(message.ReadUInt64(), typeCode));
return;
case TypeCode.Single:
TrySetValue((T)Convert.ChangeType(message.ReadSingle(), typeCode));
return;
case TypeCode.Double:
TrySetValue((T)Convert.ChangeType(message.ReadDouble(), typeCode));
return;
case TypeCode.String:
TrySetValue((T)Convert.ChangeType(message.ReadString(), typeCode));
return;
case TypeCode.Decimal:
default:
ThrowHelper.ThrowNotSupportedException($"{nameof(SettingEntry<T>)}: The type {typeof(T).Name} is not supported.");
break;
}
}
catch (Exception e)
{
// Suppress unless we're testing.
#if DEBUG
throw;
#endif
}
}
public void WriteNetMessage(IWriteMessage message)
{
throw new NotImplementedException();
if (SyncType == NetSync.None || NetworkingService is null)
{
return;
}
try
{
if (typeof(T).IsEnum)
{
message.WriteInt32((int)((IConvertible)Value));
}
// No...there's no better way to do this...
var typeCode = Type.GetTypeCode(typeof(T));
switch (typeCode)
{
case TypeCode.Boolean:
message.WriteBoolean((bool)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Byte:
message.WriteByte((byte)Convert.ChangeType(Value, typeCode)!);
return;
// SByte not supported by interface
case TypeCode.SByte:
message.WriteInt16((short)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Int16:
message.WriteInt16((short)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Char:
case TypeCode.UInt16:
message.WriteUInt16((ushort)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Int32:
message.WriteInt32((int)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.UInt32:
message.WriteUInt32((uint)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Int64:
message.WriteInt64((long)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.UInt64:
message.WriteUInt64((ulong)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Single:
message.WriteSingle((float)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Double:
message.WriteDouble((double)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.String:
message.WriteString((string)Convert.ChangeType(Value, typeCode)!);
return;
case TypeCode.Decimal:
default:
ThrowHelper.ThrowNotSupportedException($"{nameof(SettingEntry<T>)}: The type {typeof(T).Name} is not supported.");
break;
}
}
catch (Exception e)
{
// Suppress unless we're testing.
#if DEBUG
throw;
#endif
}
}
}
@@ -1,94 +0,0 @@
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
using OneOf;
namespace Barotrauma.LuaCs.Configuration;
public class SettingList<T> : ISettingList<T> where T : IEquatable<T>
{
public string InternalName { get; }
public ContentPackage OwnerPackage { get; }
public bool Equals(ISettingBase other)
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
public Type GetValueType()
{
throw new NotImplementedException();
}
public string GetStringValue()
{
throw new NotImplementedException();
}
public bool TrySetValue(OneOf<string, XElement> value)
{
throw new NotImplementedException();
}
public bool IsAssignable(OneOf<string, XElement> value)
{
throw new NotImplementedException();
}
public event Func<OneOf<string, XElement>, bool> IsNewValueValid;
public T Value { get; }
public bool TrySetValue(T value)
{
throw new NotImplementedException();
}
public bool IsAssignable(T value)
{
throw new NotImplementedException();
}
event Action<ISettingList<T>> ISettingList<T>.OnValueChanged
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
public IReadOnlyList<T> Options { get; }
event Action<ISettingEntry<T>> ISettingEntry<T>.OnValueChanged
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
event Action<ISettingBase> ISettingBase.OnValueChanged
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
public OneOf<string, XElement> GetSerializableValue()
{
throw new NotImplementedException();
}
public Guid InstanceId { get; }
public NetSync SyncType { get; }
public ClientPermissions WritePermissions { get; }
public void ReadNetMessage(IReadMessage message)
{
throw new NotImplementedException();
}
public void WriteNetMessage(IWriteMessage message)
{
throw new NotImplementedException();
}
}