From 863ee23583c9fea4399135f49ddb77e29ea7e4d7 Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Wed, 4 Feb 2026 21:52:29 -0500 Subject: [PATCH] - Some work on config service. --- .../LuaCs/Configuration/ISettingControl.cs | 1 - .../LuaCs/Configuration/ISettingBase.cs | 18 - .../LuaCs/Configuration/ISettingEntry.cs | 12 - .../LuaCs/Configuration/ISettingEnum.cs | 9 - .../LuaCs/Configuration/ISettingList.cs | 11 - .../LuaCs/Configuration/ISettingRangeEntry.cs | 13 - .../LuaCs/Configuration/ISettingTypeDef.cs | 90 +++++ .../LuaCs/Configuration/SettingBase.cs | 60 ++++ .../LuaCs/Configuration/SettingEntry.cs | 299 +++++++++++++--- .../LuaCs/Configuration/SettingList.cs | 94 ----- .../SharedSource/LuaCs/LuaCsSetup.cs | 24 +- .../LuaCs/Networking/INetworkSyncEntity.cs | 9 +- .../LuaCs/Services/ConfigInitializers.cs | 323 ------------------ .../LuaCs/Services/NetworkingService.cs | 5 + .../_Interfaces/INetworkingService.cs | 1 + 15 files changed, 420 insertions(+), 549 deletions(-) delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingBase.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEntry.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEnum.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingList.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingRangeEntry.cs create mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingTypeDef.cs create mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingBase.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingList.cs delete mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigInitializers.cs diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/ISettingControl.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/ISettingControl.cs index 9907af9cc..f478dc50b 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/ISettingControl.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/Configuration/ISettingControl.cs @@ -6,7 +6,6 @@ public interface ISettingControl : ISettingBase { event Action OnDown; KeyOrMouse Value { get; } - bool IsAssignable(KeyOrMouse value); bool TrySetValue(KeyOrMouse value); bool IsDown(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingBase.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingBase.cs deleted file mode 100644 index 6adc49f31..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingBase.cs +++ /dev/null @@ -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, IDisposable -{ - Type GetValueType(); - string GetStringValue(); - bool TrySetValue(OneOf.OneOf value); - bool IsAssignable(OneOf.OneOf value); - event Func, bool> IsNewValueValid; - event Action OnValueChanged; - OneOf.OneOf GetSerializableValue(); -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEntry.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEntry.cs deleted file mode 100644 index a6e545543..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEntry.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using Barotrauma.LuaCs.Services; - -namespace Barotrauma.LuaCs.Configuration; - -public interface ISettingEntry : ISettingBase, INetworkSyncEntity where T : IEquatable -{ - T Value { get; } - bool TrySetValue(T value); - bool IsAssignable(T value); - new event Action> OnValueChanged; -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEnum.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEnum.cs deleted file mode 100644 index 83e9604f0..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingEnum.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; -using Barotrauma.LuaCs.Services; - -namespace Barotrauma.LuaCs.Configuration; - -public interface ISettingEnum : ISettingBase, INetworkSyncEntity -{ - -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingList.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingList.cs deleted file mode 100644 index ee74412cb..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingList.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using Barotrauma.LuaCs.Services; - -namespace Barotrauma.LuaCs.Configuration; - -public interface ISettingList : ISettingEntry, INetworkSyncEntity where T : IEquatable -{ - IReadOnlyList Options { get; } - new event Action> OnValueChanged; -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingRangeEntry.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingRangeEntry.cs deleted file mode 100644 index 57eb47fab..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingRangeEntry.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace Barotrauma.LuaCs.Configuration; - -public interface ISettingRangeEntry : ISettingEntry where T : IConvertible, IEquatable -{ - T MinValue { get; } - T MaxValue { get; } - - int GetStepCount(); - float GetRangeMin(); - float GetRangeMax(); -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingTypeDef.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingTypeDef.cs new file mode 100644 index 000000000..0f2534db6 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/ISettingTypeDef.cs @@ -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, IDisposable +{ + /// + /// Settings production factory. Should be implemented by all types and registered with the Dependency Injector. + /// + /// An interface type derived from . + public interface IFactory where T : ISettingBase + { + /// + /// Creates an instance of the given type. + /// + /// Configuration information. + /// Called before a new value is assigned. Returns a boolean whether to allow + /// the value to be changed to the one given. + /// + T CreateInstance([NotNull]IConfigInfo configInfo, Func, bool> valueChangePredicate); + } + + #if CLIENT + IConfigDisplayInfo GetDisplayInfo(); + #endif + Type GetValueType(); + string GetStringValue(); + string GetDefaultStringValue(); + bool TrySetValue(OneOf.OneOf value); + event Action OnValueChanged; + OneOf.OneOf GetSerializableValue(); +} + +/// +/// Creates a setting representing a value of the given . Must be a compatible listed type.
+///
+/// +/// Compatible Types:
+/// Any primitive type:
+/// -
+/// -
+/// -
+/// -
+/// -
+/// -
+/// -
+/// -
+/// -
+/// -
+/// Extension types and Enums:
+/// -
+/// -
+///
+public interface ISettingBase : ISettingBase where T : IEquatable, IConvertible +{ + [NotNull] + T Value { get; } + [NotNull] + T DefaultValue { get; } + bool TrySetValue(T value); +} + +/// +/// Creates a setting representing a value of the given with a minimum and maximum value. +/// Must be a type compatible with . +/// +/// The value type. See +public interface ISettingRangeBase : ISettingBase where T : IEquatable, IConvertible +{ + T MinValue { get; } + T MaxValue { get; } + int IncrementalSteps { get; } +} + +/// +/// Creates a setting representing a value of the given with a distinct list of selectable values. +/// Must be a type compatible with . +/// +/// The value type. See +public interface ISettingList : ISettingBase where T : IEquatable, IConvertible +{ + IReadOnlyList Options { get; } + IReadOnlyList StringOptions { get; } +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingBase.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingBase.cs new file mode 100644 index 000000000..ca7fbdcc0 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingBase.cs @@ -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 value); + public event Action OnValueChanged; + public abstract OneOf GetSerializableValue(); +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingEntry.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingEntry.cs index 4c1193e2d..040a3a065 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingEntry.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingEntry.cs @@ -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 : ISettingEntry where T : IEquatable +public class SettingEntry : SettingBase, ISettingBase, INetworkSyncEntity where T : IEquatable, IConvertible { - public string InternalName { get; } - public ContentPackage OwnerPackage { get; } - public bool Equals(ISettingBase other) + public class Factory : ISettingBase.IFactory> { - throw new NotImplementedException(); + public ISettingBase CreateInstance(IConfigInfo configInfo, Func, bool> valueChangePredicate) + { + Guard.IsNotNull(configInfo, nameof(configInfo)); + return new SettingEntry(configInfo, valueChangePredicate); + } + } + + public SettingEntry(IConfigInfo configInfo, + Func, bool> valueChangePredicate) + : base(configInfo) + { + if (!( + typeof(T).IsEnum || + typeof(T).IsPrimitive || + typeof(T) == typeof(string))) + { + ThrowHelper.ThrowArgumentException($"{nameof(ISettingBase)}: 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, 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 value) { - throw new NotImplementedException(); + bool isFailed = false; + var typeConvertedValue = value.Match( + (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 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 value) - { - throw new NotImplementedException(); - } - - public bool IsAssignable(OneOf value) - { - throw new NotImplementedException(); - } - - public event Func, 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.OnValueChanged - { - add => throw new NotImplementedException(); - remove => throw new NotImplementedException(); - } - - event Action ISettingBase.OnValueChanged - { - add => throw new NotImplementedException(); - remove => throw new NotImplementedException(); - } - - public OneOf 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)}: 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)}: The type {typeof(T).Name} is not supported."); + break; + } + } + catch (Exception e) + { + // Suppress unless we're testing. +#if DEBUG + throw; +#endif + } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingList.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingList.cs deleted file mode 100644 index 313c4f9b3..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/SettingList.cs +++ /dev/null @@ -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 : ISettingList where T : IEquatable -{ - 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 value) - { - throw new NotImplementedException(); - } - - public bool IsAssignable(OneOf value) - { - throw new NotImplementedException(); - } - - public event Func, 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.OnValueChanged - { - add => throw new NotImplementedException(); - remove => throw new NotImplementedException(); - } - - public IReadOnlyList Options { get; } - - event Action> ISettingEntry.OnValueChanged - { - add => throw new NotImplementedException(); - remove => throw new NotImplementedException(); - } - - event Action ISettingBase.OnValueChanged - { - add => throw new NotImplementedException(); - remove => throw new NotImplementedException(); - } - - public OneOf 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(); - } -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index c2b75b325..0e044d6f9 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -106,7 +106,7 @@ namespace Barotrauma internal set => _isCsEnabled?.TrySetValue(value); } - private ISettingEntry _isCsEnabled; + private SettingEntry _isCsEnabled; /// /// 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 _disableErrorGUIOverlay; + private SettingEntry _disableErrorGUIOverlay; /// /// 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 _hideUserNamesInLogs; + private SettingEntry _hideUserNamesInLogs; /// /// 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 _luaForBarotraumaSteamId; + private SettingEntry _luaForBarotraumaSteamId; /// /// TODO: @evilfactory@users.noreply.github.com @@ -146,7 +146,7 @@ namespace Barotrauma get => _restrictMessageSize?.Value ?? false; internal set => _restrictMessageSize?.TrySetValue(value); } - private ISettingEntry _restrictMessageSize; + private SettingEntry _restrictMessageSize; /// /// 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 _localDataSavePath; + private SettingEntry _localDataSavePath; void LoadLuaCsConfig() { - _isCsEnabled = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "IsCsEnabled", out var val1) ? val1 + _isCsEnabled = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "IsCsEnabled", out var val1) ? val1 : throw new NullReferenceException($"{nameof(IsCsEnabled)} cannot be loaded."); - _disableErrorGUIOverlay = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "DisableErrorGUIOverlay", out var val3) ? val3 + _disableErrorGUIOverlay = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "DisableErrorGUIOverlay", out var val3) ? val3 : throw new NullReferenceException($"{nameof(DisableErrorGUIOverlay)} cannot be loaded."); - _hideUserNamesInLogs = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "HideUserNamesInLogs", out var val4) ? val4 + _hideUserNamesInLogs = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "HideUserNamesInLogs", out var val4) ? val4 : throw new NullReferenceException($"{nameof(HideUserNamesInLogs)} cannot be loaded."); - _luaForBarotraumaSteamId = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LuaForBarotraumaSteamId", out var val5) ? val5 + _luaForBarotraumaSteamId = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LuaForBarotraumaSteamId", out var val5) ? val5 : throw new NullReferenceException($"{nameof(LuaForBarotraumaSteamId)} cannot be loaded."); - _restrictMessageSize = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "RestrictMessageSize", out var val7) ? val7 + _restrictMessageSize = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "RestrictMessageSize", out var val7) ? val7 : throw new NullReferenceException($"{nameof(RestrictMessageSize)} cannot be loaded."); - _localDataSavePath = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LocalDataSavePath", out var val8) ? val8 + _localDataSavePath = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LocalDataSavePath", out var val8) ? val8 : throw new NullReferenceException($"{nameof(LocalDataSavePath)} cannot be loaded."); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Networking/INetworkSyncEntity.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Networking/INetworkSyncEntity.cs index 3a93e787b..1847dc2ef 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Networking/INetworkSyncEntity.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Networking/INetworkSyncEntity.cs @@ -11,7 +11,14 @@ public interface INetworkSyncEntity /// /// Network-synchronized object ID. Used for networking send/receive message events. /// - Guid InstanceId { get; } + ulong InstanceId { get; } + + /// + /// Sets the that is currently managing this instance. The + /// is retrieved from here. + /// + /// The networking service managing this instance or null to deregister. + void SetNetworkOwner(IEntityNetworkingService networkingService); /// /// Synchronization type. See for more information. diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigInitializers.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigInitializers.cs deleted file mode 100644 index a4db95e52..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigInitializers.cs +++ /dev/null @@ -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> CreateConfigEntry(IConfigInfo configInfo, - Action, IReadMessage> readHandler, - Action, IWriteMessage> writeHandler) - where T : IEquatable - { - throw new NotImplementedException(); - } - - private Result> CreateConfigList(IConfigInfo configInfo, - Action, IReadMessage> readHandler, Action, IWriteMessage> writeHandler) - where T : IEquatable - { - throw new NotImplementedException(); - } - - public void RegisterTypeInitializers(IConfigService configService) - { - if (configService == null) - throw new ArgumentNullException($"{nameof(RegisterTypeInitializers)}: {nameof(IConfigService)} is null."); - - /*configService.RegisterTypeInitializer>(this.CreateConfigBool); - configService.RegisterTypeInitializer>(this.CreateConfigSbyte); - configService.RegisterTypeInitializer>(this.CreateConfigByte); - configService.RegisterTypeInitializer>(this.CreateConfigShort); - configService.RegisterTypeInitializer>(this.CreateConfigUShort); - configService.RegisterTypeInitializer>(this.CreateConfigInt32); - configService.RegisterTypeInitializer>(this.CreateConfigUInt32); - configService.RegisterTypeInitializer>(this.CreateConfigInt64); - configService.RegisterTypeInitializer>(this.CreateConfigUInt64); - configService.RegisterTypeInitializer>(this.CreateConfigFloat32); - configService.RegisterTypeInitializer>(this.CreateConfigFloat64); - configService.RegisterTypeInitializer>(this.CreateConfigFloat128); - configService.RegisterTypeInitializer>(this.CreateConfigChar); - configService.RegisterTypeInitializer>(this.CreateConfigString); - configService.RegisterTypeInitializer>(this.CreateConfigColor); - configService.RegisterTypeInitializer>(this.CreateConfigVector2); - configService.RegisterTypeInitializer>(this.CreateConfigVector3); - configService.RegisterTypeInitializer>(this.CreateConfigVector4);*/ - } - - - #region InitializerWrappers_NetworkInjected - - private void AssignValueConditional(T val, ISettingEntry inst) where T : IEquatable - { -#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> CreateConfigBool(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadBoolean(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteBoolean(inst.Value); - }); - } - - private Result> CreateConfigSbyte(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional((sbyte)readMsg.ReadInt16(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteInt16((short)inst.Value); - }); - } - - private Result> CreateConfigByte(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional((byte)readMsg.ReadUInt16(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteUInt16((byte)inst.Value); - }); - } - - private Result> CreateConfigShort(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadInt16(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteInt16(inst.Value); - }); - } - - private Result> CreateConfigUShort(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadUInt16(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteUInt16(inst.Value); - }); - } - - private Result> CreateConfigInt32(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadInt32(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteInt32(inst.Value); - }); - } - - private Result> CreateConfigUInt32(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadUInt32(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteUInt32(inst.Value); - }); - } - - private Result> CreateConfigInt64(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadInt64(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteInt64(inst.Value); - }); - } - - private Result> CreateConfigUInt64(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadUInt64(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteUInt64(inst.Value); - }); - } - - private Result> CreateConfigFloat32(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadSingle(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteSingle(inst.Value); - }); - } - - private Result> CreateConfigFloat64(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadDouble(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteDouble(inst.Value); - }); - } - - private Result> CreateConfigFloat128(IConfigInfo configInfo) - { - return CreateConfigEntry(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> CreateConfigChar(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional((char)readMsg.ReadUInt16(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteUInt16(inst.Value); - }); - } - - private Result> CreateConfigString(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadString(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteString(inst.Value); - }); - } - - private Result> CreateConfigColor(IConfigInfo configInfo) - { - return CreateConfigEntry(configInfo, (inst, readMsg) => - { - AssignValueConditional(readMsg.ReadColorR8G8B8A8(), inst); - - }, (inst, writeMsg) => - { - writeMsg.WriteColorR8G8B8A8(inst.Value); - }); - } - - private Result> CreateConfigVector2(IConfigInfo configInfo) - { - return CreateConfigEntry(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> CreateConfigVector3(IConfigInfo configInfo) - { - return CreateConfigEntry(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> CreateConfigVector4(IConfigInfo configInfo) - { - return CreateConfigEntry(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 -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/NetworkingService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/NetworkingService.cs index 463821d96..1c8b6b32d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/NetworkingService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/NetworkingService.cs @@ -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(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/INetworkingService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/INetworkingService.cs index 5d39e5e20..c3676da59 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/INetworkingService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/INetworkingService.cs @@ -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); }