Network Sync work. Added network unique key.

This commit is contained in:
Maplewheels
2026-02-07 22:58:56 -05:00
parent 02b1f524b6
commit 67c195034d
4 changed files with 80 additions and 1 deletions

View File

@@ -195,6 +195,7 @@ namespace Barotrauma
servicesProvider.RegisterServiceType<IParserServiceAsync<ResourceParserInfo, IConfigResourceInfo>, ModConfigFileParserService>(ServiceLifetime.Transient);
servicesProvider.RegisterServiceType<IParserServiceOneToManyAsync<IConfigResourceInfo, IConfigInfo>, SettingsFileParserService>(ServiceLifetime.Transient);
servicesProvider.RegisterServiceType<IParserServiceOneToManyAsync<IConfigResourceInfo, IConfigProfileInfo>, SettingsFileParserService>(ServiceLifetime.Transient);
servicesProvider.RegisterServiceType<INetworkIdProvider, NetworkingIdProvider>(ServiceLifetime.Transient);
// All Lua Extras
servicesProvider.RegisterServiceType<IDefaultLuaRegistrar, DefaultLuaRegistrar>(ServiceLifetime.Singleton);

View File

@@ -0,0 +1,37 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Barotrauma.Items.Components;
using Barotrauma.LuaCs.Data;
namespace Barotrauma.LuaCs;
/// <summary>
/// Provides a deterministic ID for a given <see cref="IDataInfo"/> instance under multiple circumstances, for use with
/// network synchronization.
/// </summary>
internal interface INetworkIdProvider : IService
{
/// <summary>
/// Deterministically generates a GUID for the given parameters.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>The GUID for the entity.</returns>
Guid GetNetworkIdForInstance([NotNull] IDataInfo instance);
/// <summary>
/// Deterministically generates a GUID for the given parameters.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="attachedEntity">The <see cref="Entity"/> that this instance is attached to, if any.</param>
/// <typeparam name="TEntity">The entity type, if any.</typeparam>
/// <returns>The GUID for the entity.</returns>
Guid GetNetworkIdForInstance<TEntity>([NotNull] IDataInfo instance, TEntity attachedEntity) where TEntity : Entity;
/// <summary>
/// Deterministically generates a GUID for the given parameters.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="attachedItemComponent">The <see cref="ItemComponent"/> that this instance is attached to, if any.</param>
/// <returns>The GUID for the entity.</returns>
Guid GetNetworkIdForInstance([NotNull] IDataInfo instance, [MaybeNull] ItemComponent attachedItemComponent);
}

View File

@@ -5,7 +5,7 @@ using Barotrauma.Networking;
namespace Barotrauma.LuaCs;
public interface INetworkSyncVar
public interface INetworkSyncVar : IDataInfo
{
/// <summary>
/// Network-synchronized object ID. Used for networking send/receive message events.

View File

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