- 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

View File

@@ -6,7 +6,6 @@ public interface ISettingControl : ISettingBase
{
event Action<ISettingControl> OnDown;
KeyOrMouse Value { get; }
bool IsAssignable(KeyOrMouse value);
bool TrySetValue(KeyOrMouse value);
bool IsDown();
}

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -1,9 +0,0 @@
using System;
using Barotrauma.LuaCs.Services;
namespace Barotrauma.LuaCs.Configuration;
public interface ISettingEnum : ISettingBase, INetworkSyncEntity
{
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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; }
}

View File

@@ -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();
}

View File

@@ -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
}
}
}

View File

@@ -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();
}
}

View File

@@ -106,7 +106,7 @@ namespace Barotrauma
internal set => _isCsEnabled?.TrySetValue(value);
}
private ISettingEntry<bool> _isCsEnabled;
private SettingEntry<bool> _isCsEnabled;
/// <summary>
/// Whether the popup error GUI should be hidden/suppressed.
@@ -116,7 +116,7 @@ namespace Barotrauma
get => _disableErrorGUIOverlay?.Value ?? false;
internal set => _disableErrorGUIOverlay?.TrySetValue(value);
}
private ISettingEntry<bool> _disableErrorGUIOverlay;
private SettingEntry<bool> _disableErrorGUIOverlay;
/// <summary>
/// Whether usernames are anonymized or show in logs.
@@ -126,7 +126,7 @@ namespace Barotrauma
get => _hideUserNamesInLogs?.Value ?? false;
internal set => _hideUserNamesInLogs?.TrySetValue(value);
}
private ISettingEntry<bool> _hideUserNamesInLogs;
private SettingEntry<bool> _hideUserNamesInLogs;
/// <summary>
/// The SteamId of the Workshop LuaCs CPackage in use, if available.
@@ -136,7 +136,7 @@ namespace Barotrauma
get => _luaForBarotraumaSteamId?.Value ?? 0;
internal set => _luaForBarotraumaSteamId?.TrySetValue(value);
}
private ISettingEntry<ulong> _luaForBarotraumaSteamId;
private SettingEntry<ulong> _luaForBarotraumaSteamId;
/// <summary>
/// TODO: @evilfactory@users.noreply.github.com
@@ -146,7 +146,7 @@ namespace Barotrauma
get => _restrictMessageSize?.Value ?? false;
internal set => _restrictMessageSize?.TrySetValue(value);
}
private ISettingEntry<bool> _restrictMessageSize;
private SettingEntry<bool> _restrictMessageSize;
/// <summary>
/// The local save path for all local data storage for mods.
@@ -156,21 +156,21 @@ namespace Barotrauma
get => _localDataSavePath?.Value ?? Path.Combine(Directory.GetCurrentDirectory(), "/Data/Mods");
internal set => _localDataSavePath?.TrySetValue(value);
}
private ISettingEntry<string> _localDataSavePath;
private SettingEntry<string> _localDataSavePath;
void LoadLuaCsConfig()
{
_isCsEnabled = ConfigService.TryGetConfig<ISettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "IsCsEnabled", out var val1) ? val1
_isCsEnabled = ConfigService.TryGetConfig<SettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "IsCsEnabled", out var val1) ? val1
: throw new NullReferenceException($"{nameof(IsCsEnabled)} cannot be loaded.");
_disableErrorGUIOverlay = ConfigService.TryGetConfig<ISettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "DisableErrorGUIOverlay", out var val3) ? val3
_disableErrorGUIOverlay = ConfigService.TryGetConfig<SettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "DisableErrorGUIOverlay", out var val3) ? val3
: throw new NullReferenceException($"{nameof(DisableErrorGUIOverlay)} cannot be loaded.");
_hideUserNamesInLogs = ConfigService.TryGetConfig<ISettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "HideUserNamesInLogs", out var val4) ? val4
_hideUserNamesInLogs = ConfigService.TryGetConfig<SettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "HideUserNamesInLogs", out var val4) ? val4
: throw new NullReferenceException($"{nameof(HideUserNamesInLogs)} cannot be loaded.");
_luaForBarotraumaSteamId = ConfigService.TryGetConfig<ISettingEntry<ulong>>(ContentPackageManager.VanillaCorePackage, "LuaForBarotraumaSteamId", out var val5) ? val5
_luaForBarotraumaSteamId = ConfigService.TryGetConfig<SettingEntry<ulong>>(ContentPackageManager.VanillaCorePackage, "LuaForBarotraumaSteamId", out var val5) ? val5
: throw new NullReferenceException($"{nameof(LuaForBarotraumaSteamId)} cannot be loaded.");
_restrictMessageSize = ConfigService.TryGetConfig<ISettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "RestrictMessageSize", out var val7) ? val7
_restrictMessageSize = ConfigService.TryGetConfig<SettingEntry<bool>>(ContentPackageManager.VanillaCorePackage, "RestrictMessageSize", out var val7) ? val7
: throw new NullReferenceException($"{nameof(RestrictMessageSize)} cannot be loaded.");
_localDataSavePath = ConfigService.TryGetConfig<ISettingEntry<string>>(ContentPackageManager.VanillaCorePackage, "LocalDataSavePath", out var val8) ? val8
_localDataSavePath = ConfigService.TryGetConfig<SettingEntry<string>>(ContentPackageManager.VanillaCorePackage, "LocalDataSavePath", out var val8) ? val8
: throw new NullReferenceException($"{nameof(LocalDataSavePath)} cannot be loaded.");
}

View File

@@ -11,7 +11,14 @@ public interface INetworkSyncEntity
/// <summary>
/// Network-synchronized object ID. Used for networking send/receive message events.
/// </summary>
Guid InstanceId { get; }
ulong InstanceId { get; }
/// <summary>
/// Sets the <see cref="IEntityNetworkingService"/> that is currently managing this instance. The <see cref="InstanceId"/>
/// is retrieved from here.
/// </summary>
/// <param name="networkingService">The networking service managing this instance or null to deregister.</param>
void SetNetworkOwner(IEntityNetworkingService networkingService);
/// <summary>
/// Synchronization type. See <see cref="NetSync"/> for more information.

View File

@@ -1,323 +0,0 @@
using System;
using System.Numerics;
using Barotrauma.LuaCs.Configuration;
using Barotrauma.LuaCs.Data;
using Barotrauma.Networking;
using FluentResults;
using Microsoft.Xna.Framework;
using Vector2 = Microsoft.Xna.Framework.Vector2;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Vector4 = Microsoft.Xna.Framework.Vector4;
namespace Barotrauma.LuaCs.Services;
public class ConfigInitializers : IService
{
// parameterless .ctor
public ConfigInitializers()
{
}
public void Dispose()
{
// stateless service
return;
}
// stateless service
public bool IsDisposed => false;
private Result<ISettingEntry<T>> CreateConfigEntry<T>(IConfigInfo configInfo,
Action<SettingEntry<T>, IReadMessage> readHandler,
Action<SettingEntry<T>, IWriteMessage> writeHandler)
where T : IEquatable<T>
{
throw new NotImplementedException();
}
private Result<ISettingList<T>> CreateConfigList<T>(IConfigInfo configInfo,
Action<ISettingList<T>, IReadMessage> readHandler, Action<ISettingList<T>, IWriteMessage> writeHandler)
where T : IEquatable<T>
{
throw new NotImplementedException();
}
public void RegisterTypeInitializers(IConfigService configService)
{
if (configService == null)
throw new ArgumentNullException($"{nameof(RegisterTypeInitializers)}: {nameof(IConfigService)} is null.");
/*configService.RegisterTypeInitializer<bool, ISettingEntry<bool>>(this.CreateConfigBool);
configService.RegisterTypeInitializer<sbyte, ISettingEntry<sbyte>>(this.CreateConfigSbyte);
configService.RegisterTypeInitializer<byte, ISettingEntry<byte>>(this.CreateConfigByte);
configService.RegisterTypeInitializer<short, ISettingEntry<short>>(this.CreateConfigShort);
configService.RegisterTypeInitializer<ushort, ISettingEntry<ushort>>(this.CreateConfigUShort);
configService.RegisterTypeInitializer<int, ISettingEntry<int>>(this.CreateConfigInt32);
configService.RegisterTypeInitializer<uint, ISettingEntry<uint>>(this.CreateConfigUInt32);
configService.RegisterTypeInitializer<long, ISettingEntry<long>>(this.CreateConfigInt64);
configService.RegisterTypeInitializer<ulong, ISettingEntry<ulong>>(this.CreateConfigUInt64);
configService.RegisterTypeInitializer<float, ISettingEntry<float>>(this.CreateConfigFloat32);
configService.RegisterTypeInitializer<double, ISettingEntry<double>>(this.CreateConfigFloat64);
configService.RegisterTypeInitializer<decimal, ISettingEntry<decimal>>(this.CreateConfigFloat128);
configService.RegisterTypeInitializer<char, ISettingEntry<char>>(this.CreateConfigChar);
configService.RegisterTypeInitializer<string, ISettingEntry<string>>(this.CreateConfigString);
configService.RegisterTypeInitializer<Color, ISettingEntry<Color>>(this.CreateConfigColor);
configService.RegisterTypeInitializer<Vector2, ISettingEntry<Vector2>>(this.CreateConfigVector2);
configService.RegisterTypeInitializer<Vector3, ISettingEntry<Vector3>>(this.CreateConfigVector3);
configService.RegisterTypeInitializer<Vector4, ISettingEntry<Vector4>>(this.CreateConfigVector4);*/
}
#region InitializerWrappers_NetworkInjected
private void AssignValueConditional<T>(T val, ISettingEntry<T> inst) where T : IEquatable<T>
{
#if SERVER
if (inst.SyncType is NetSync.None or NetSync.ServerAuthority)
throw new InvalidOperationException($"[Server] Tried to assign a net value to a type that does not support sync: {inst.SyncType}. Name: {inst.InternalName}, Package: {inst.OwnerPackage.Name}");
inst.TrySetValue(val);
#else
if (inst.SyncType is NetSync.None or NetSync.ClientOneWay)
throw new InvalidOperationException($"[Client] Tried to assign a net value to a type that does not support sync: {inst.SyncType}. Name: {inst.InternalName}, Package: {inst.OwnerPackage.Name}");
inst.TrySetValue(val);
#endif
}
private Result<ISettingEntry<bool>> CreateConfigBool(IConfigInfo configInfo)
{
return CreateConfigEntry<bool>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadBoolean(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteBoolean(inst.Value);
});
}
private Result<ISettingEntry<sbyte>> CreateConfigSbyte(IConfigInfo configInfo)
{
return CreateConfigEntry<sbyte>(configInfo, (inst, readMsg) =>
{
AssignValueConditional((sbyte)readMsg.ReadInt16(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteInt16((short)inst.Value);
});
}
private Result<ISettingEntry<byte>> CreateConfigByte(IConfigInfo configInfo)
{
return CreateConfigEntry<byte>(configInfo, (inst, readMsg) =>
{
AssignValueConditional((byte)readMsg.ReadUInt16(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteUInt16((byte)inst.Value);
});
}
private Result<ISettingEntry<short>> CreateConfigShort(IConfigInfo configInfo)
{
return CreateConfigEntry<short>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadInt16(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteInt16(inst.Value);
});
}
private Result<ISettingEntry<ushort>> CreateConfigUShort(IConfigInfo configInfo)
{
return CreateConfigEntry<ushort>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadUInt16(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteUInt16(inst.Value);
});
}
private Result<ISettingEntry<int>> CreateConfigInt32(IConfigInfo configInfo)
{
return CreateConfigEntry<int>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadInt32(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteInt32(inst.Value);
});
}
private Result<ISettingEntry<uint>> CreateConfigUInt32(IConfigInfo configInfo)
{
return CreateConfigEntry<uint>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadUInt32(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteUInt32(inst.Value);
});
}
private Result<ISettingEntry<long>> CreateConfigInt64(IConfigInfo configInfo)
{
return CreateConfigEntry<long>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadInt64(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteInt64(inst.Value);
});
}
private Result<ISettingEntry<ulong>> CreateConfigUInt64(IConfigInfo configInfo)
{
return CreateConfigEntry<ulong>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadUInt64(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteUInt64(inst.Value);
});
}
private Result<ISettingEntry<float>> CreateConfigFloat32(IConfigInfo configInfo)
{
return CreateConfigEntry<float>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadSingle(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteSingle(inst.Value);
});
}
private Result<ISettingEntry<double>> CreateConfigFloat64(IConfigInfo configInfo)
{
return CreateConfigEntry<double>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadDouble(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteDouble(inst.Value);
});
}
private Result<ISettingEntry<decimal>> CreateConfigFloat128(IConfigInfo configInfo)
{
return CreateConfigEntry<decimal>(configInfo, (inst, readMsg) =>
{
var decimalArr = new int[4];
decimalArr[0] = readMsg.ReadInt32();
decimalArr[1] = readMsg.ReadInt32();
decimalArr[2] = readMsg.ReadInt32();
decimalArr[3] = readMsg.ReadInt32();
AssignValueConditional(new decimal(decimalArr), inst);
}, (inst, writeMsg) =>
{
var decimalArr = Decimal.GetBits(inst.Value);
writeMsg.WriteInt32(decimalArr[0]);
writeMsg.WriteInt32(decimalArr[1]);
writeMsg.WriteInt32(decimalArr[2]);
writeMsg.WriteInt32(decimalArr[3]);
});
}
private Result<ISettingEntry<char>> CreateConfigChar(IConfigInfo configInfo)
{
return CreateConfigEntry<char>(configInfo, (inst, readMsg) =>
{
AssignValueConditional((char)readMsg.ReadUInt16(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteUInt16(inst.Value);
});
}
private Result<ISettingEntry<string>> CreateConfigString(IConfigInfo configInfo)
{
return CreateConfigEntry<string>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadString(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteString(inst.Value);
});
}
private Result<ISettingEntry<Color>> CreateConfigColor(IConfigInfo configInfo)
{
return CreateConfigEntry<Color>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(readMsg.ReadColorR8G8B8A8(), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteColorR8G8B8A8(inst.Value);
});
}
private Result<ISettingEntry<Vector2>> CreateConfigVector2(IConfigInfo configInfo)
{
return CreateConfigEntry<Vector2>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(new Vector2(readMsg.ReadSingle(), readMsg.ReadSingle()), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteSingle(inst.Value.X);
writeMsg.WriteSingle(inst.Value.Y);
});
}
private Result<ISettingEntry<Vector3>> CreateConfigVector3(IConfigInfo configInfo)
{
return CreateConfigEntry<Vector3>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(new Vector3(readMsg.ReadSingle(), readMsg.ReadSingle(), readMsg.ReadSingle()), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteSingle(inst.Value.X);
writeMsg.WriteSingle(inst.Value.Y);
writeMsg.WriteSingle(inst.Value.Z);
});
}
private Result<ISettingEntry<Vector4>> CreateConfigVector4(IConfigInfo configInfo)
{
return CreateConfigEntry<Vector4>(configInfo, (inst, readMsg) =>
{
AssignValueConditional(new Vector4(
readMsg.ReadSingle(),
readMsg.ReadSingle(),
readMsg.ReadSingle(),
readMsg.ReadSingle()), inst);
}, (inst, writeMsg) =>
{
writeMsg.WriteSingle(inst.Value.X);
writeMsg.WriteSingle(inst.Value.Y);
writeMsg.WriteSingle(inst.Value.Z);
writeMsg.WriteSingle(inst.Value.W);
});
}
#endregion
}

View File

@@ -104,6 +104,11 @@ internal partial class NetworkingService : INetworkingService
IsDisposed = true;
}
public ulong GetNetworkIdForInstance(INetworkSyncEntity entity)
{
throw new NotImplementedException();
}
public void RegisterNetVar(INetworkSyncEntity netVar)
{
throw new NotImplementedException();

View File

@@ -25,6 +25,7 @@ internal partial interface INetworkingService : IReusableService, ILuaCsNetworki
public interface IEntityNetworkingService
{
public ulong GetNetworkIdForInstance(INetworkSyncEntity entity);
public void RegisterNetVar(INetworkSyncEntity netVar);
public void SendNetVar(INetworkSyncEntity netVar);
}