OBT/1.2.0(Spring Update)

Sync with Upstream
This commit is contained in:
NotAlwaysTrue
2026-04-25 13:25:41 +08:00
committed by GitHub
parent 5207b381b7
commit 59bc21973a
421 changed files with 24090 additions and 11391 deletions
@@ -0,0 +1,15 @@
using System;
namespace Barotrauma.LuaCs;
public partial interface INetCallback
{
public ushort CallbackId { get; }
}
#if SERVER
public partial interface INetCallback
{
}
#endif
@@ -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);
}
@@ -0,0 +1,72 @@
using System;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs;
public interface INetworkSyncVar : IDataInfo
{
/// <summary>
/// Network-synchronized object ID. Used for networking send/receive message events.
/// </summary>
Guid 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.
/// </summary>
NetSync SyncType { get; }
/// <summary>
/// Permissions needed by clients to send net-events and/or receive net messages.
/// </summary>
ClientPermissions WritePermissions { get; }
/// <summary>
/// Called when an incoming net message has data for this network object, typically from the same entity on another
/// machine.
/// </summary>
/// <param name="message">Wrapper for the internal type: <see cref="IReadMessage"/></param>
void ReadNetMessage(IReadMessage message);
/// <summary>
/// Called when a network send-event involving this entity is triggered. Any data expected to be read by the recipient
/// network object on the other instance(s) should be written to the packet.
/// </summary>
/// <param name="message">Wrapper for the internal type: <see cref="IWriteMessage"/></param>
void WriteNetMessage(IWriteMessage message);
}
/// <summary>
/// Specifies the networking send/receive relationship for network object. Objects implementing this interface are
/// expected to adhere to the contract or de-sync may occur.
/// </summary>
public enum NetSync
{
/// <summary>
/// No network synchronization.
/// </summary>
None,
/// <summary>
/// Both the client and the server have 'send' and 'receive' permissions (limited by <see cref="ClientPermissions"/>). Can also be used to allow two-way communication
/// with the server.
/// </summary>
TwoWay,
/// <summary>
/// Only the host/server has the authority to change this value.
/// </summary>
ServerAuthority,
/// <summary>
/// Only clients (with the required by <see cref="ClientPermissions"/>) may change the value and all value changes are communicated to the server/host.
/// <br/><br/><b>[Important] The host/server will not send the value to other connected clients.</b><br/>
/// Intended to allow clients to send one-way messages to the server.
/// </summary>
ClientOneWay
}
@@ -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);
}
}