From 67c195034d91c2459daa9938f75dbdb7e63ec617 Mon Sep 17 00:00:00 2001 From: Maplewheels Date: Sat, 7 Feb 2026 22:58:56 -0500 Subject: [PATCH] Network Sync work. Added network unique key. --- .../SharedSource/LuaCs/LuaCsSetup.cs | 1 + .../LuaCs/_Networking/INetworkIdProvider.cs | 37 +++++++++++++++++ .../LuaCs/_Networking/INetworkSyncEntity.cs | 2 +- .../LuaCs/_Networking/NetworkingIdProvider.cs | 41 +++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkIdProvider.cs create mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/NetworkingIdProvider.cs diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index b37b3d49b..78622d75a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -195,6 +195,7 @@ namespace Barotrauma servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType(ServiceLifetime.Transient); // All Lua Extras servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkIdProvider.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkIdProvider.cs new file mode 100644 index 000000000..441f6e8bc --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkIdProvider.cs @@ -0,0 +1,37 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Data; + +namespace Barotrauma.LuaCs; + +/// +/// Provides a deterministic ID for a given instance under multiple circumstances, for use with +/// network synchronization. +/// +internal interface INetworkIdProvider : IService +{ + /// + /// Deterministically generates a GUID for the given parameters. + /// + /// The instance. + /// The GUID for the entity. + Guid GetNetworkIdForInstance([NotNull] IDataInfo instance); + + /// + /// Deterministically generates a GUID for the given parameters. + /// + /// The instance. + /// The that this instance is attached to, if any. + /// The entity type, if any. + /// The GUID for the entity. + Guid GetNetworkIdForInstance([NotNull] IDataInfo instance, TEntity attachedEntity) where TEntity : Entity; + + /// + /// Deterministically generates a GUID for the given parameters. + /// + /// The instance. + /// The that this instance is attached to, if any. + /// The GUID for the entity. + Guid GetNetworkIdForInstance([NotNull] IDataInfo instance, [MaybeNull] ItemComponent attachedItemComponent); +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkSyncEntity.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkSyncEntity.cs index 75ec9d865..a13e0a190 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkSyncEntity.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/INetworkSyncEntity.cs @@ -5,7 +5,7 @@ using Barotrauma.Networking; namespace Barotrauma.LuaCs; -public interface INetworkSyncVar +public interface INetworkSyncVar : IDataInfo { /// /// Network-synchronized object ID. Used for networking send/receive message events. diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/NetworkingIdProvider.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/NetworkingIdProvider.cs new file mode 100644 index 000000000..a3b2157c2 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Networking/NetworkingIdProvider.cs @@ -0,0 +1,41 @@ +using System; +using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Data; +using System.Security.Cryptography; +using System.Text; + +namespace Barotrauma.LuaCs; + +internal class NetworkingIdProvider : INetworkIdProvider +{ + public void Dispose() + { + //stateless service + } + + public bool IsDisposed => false; + + private Guid GetNetworkIdFromStringMd5(string id) + { + return new Guid(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(id))); + } + + public Guid GetNetworkIdForInstance(IDataInfo instance) + { + var str = $"{instance.OwnerPackage.Name}.{instance.InternalName}"; + return GetNetworkIdFromStringMd5(str); + } + + public Guid GetNetworkIdForInstance(IDataInfo instance, TEntity attachedEntity) where TEntity : Entity + { + var str = $"{nameof(TEntity)}({attachedEntity.ID}).{instance.OwnerPackage.Name}.{instance.InternalName}"; + return GetNetworkIdFromStringMd5(str); + } + + public Guid GetNetworkIdForInstance(IDataInfo instance, ItemComponent attachedItemComponent) + { + var attachedEntity = attachedItemComponent.Item; + var str = $"{attachedEntity.GetType().Name}({attachedEntity.ID}).ComponentId({attachedEntity.Components.IndexOf(attachedItemComponent)}).{instance.OwnerPackage.Name}.{instance.InternalName}"; + return GetNetworkIdFromStringMd5(str); + } +}