Skip to content

Commit

Permalink
optimize and simplify rpcs (move some code for better debugability an…
Browse files Browse the repository at this point in the history
…d readability). also reduce memory usage
  • Loading branch information
RevenantX committed Dec 15, 2024
1 parent e008c9d commit d22174d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 150 deletions.
4 changes: 2 additions & 2 deletions LiteEntitySystem/ClientEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ private readonly struct SyncCallInfo
{
public readonly InternalEntity Entity;

private readonly OnSyncCallDelegate _onSync;
private readonly MethodCallDelegate _onSync;
private readonly int _prevDataPos;

public SyncCallInfo(OnSyncCallDelegate onSync, InternalEntity entity, int prevDataPos)
public SyncCallInfo(MethodCallDelegate onSync, InternalEntity entity, int prevDataPos)
{
_onSync = onSync;
Entity = entity;
Expand Down
2 changes: 1 addition & 1 deletion LiteEntitySystem/Internal/EntityFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal struct EntityFieldInfo
public readonly bool IsPredicted;
public OnSyncExecutionOrder OnSyncExecutionOrder;

public OnSyncCallDelegate OnSync;
public MethodCallDelegate OnSync;
public int FixedOffset;
public int PredictedOffset;

Expand Down
15 changes: 0 additions & 15 deletions LiteEntitySystem/Internal/InternalBaseClass.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
using System;
using System.Runtime.CompilerServices;

namespace LiteEntitySystem.Internal
{
public abstract class InternalBaseClass
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ExecuteRPC(in RemoteCall rpc) => rpc.Call(this);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ExecuteRPC<T>(in RemoteCall<T> rpc, T value) where T : unmanaged => rpc.Call(this, value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ExecuteRPC<T>(in RemoteCallSpan<T> rpc, ReadOnlySpan<T> value) where T : unmanaged => rpc.Call(this, value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ExecuteRPC<T>(in RemoteCallSerializable<T> rpc, T value) where T : struct, ISpanSerializable => rpc.Call(this, value);

protected internal virtual void OnSyncRequested()
{

Expand Down
57 changes: 55 additions & 2 deletions LiteEntitySystem/Internal/InternalEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected internal virtual void OnConstructed()

internal void RegisterRpcInternal()
{
ref var classData = ref EntityManager.ClassDataDict[ClassId];
ref var classData = ref EntityManager.ClassDataDict[ClassId];

//setup field ids for BindOnChange and pass on server this for OnChangedEvent to StateSerializer
var onChangeTarget = EntityManager.IsServer && !IsLocal ? this : null;
Expand All @@ -225,7 +225,7 @@ internal void RegisterRpcInternal()
if(classData.RemoteCallsClient == null)
{
rpcCahce = new List<RpcFieldInfo>();
var rpcRegistrator = new RPCRegistrator(rpcCahce);
var rpcRegistrator = new RPCRegistrator(rpcCahce, classData.Fields);
RegisterRPC(ref rpcRegistrator);
//Logger.Log($"RegisterRPCs for class: {classData.ClassId}");
}
Expand Down Expand Up @@ -266,6 +266,59 @@ protected virtual void RegisterRPC(ref RPCRegistrator r)
{
r.BindOnChange(this, ref _isDestroyed, OnDestroyChange);
}

protected void ExecuteRPC(in RemoteCall rpc)
{
if (IsServer)
{
if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnServer))
rpc.CachedAction(this);
ServerManager.AddRemoteCall(this, rpc.Id, rpc.Flags);
}
else if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnPrediction) && IsLocalControlled)
rpc.CachedAction(this);
}

protected void ExecuteRPC<T>(in RemoteCall<T> rpc, T value) where T : unmanaged
{
if (IsServer)
{
if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnServer))
rpc.CachedAction(this, value);
unsafe
{
ServerManager.AddRemoteCall(this, new ReadOnlySpan<T>(&value, 1), rpc.Id, rpc.Flags);
}
}
else if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnPrediction) && IsLocalControlled)
rpc.CachedAction(this, value);
}

protected void ExecuteRPC<T>(in RemoteCallSpan<T> rpc, ReadOnlySpan<T> value) where T : unmanaged
{
if (IsServer)
{
if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnServer))
rpc.CachedAction(this, value);
ServerManager.AddRemoteCall(this, value, rpc.Id, rpc.Flags);
}
else if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnPrediction) && IsLocalControlled)
rpc.CachedAction(this, value);
}

protected void ExecuteRPC<T>(in RemoteCallSerializable<T> rpc, T value) where T : struct, ISpanSerializable
{
if (IsServer)
{
if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnServer))
rpc.CachedAction(this, value);
var writer = new SpanWriter(stackalloc byte[value.MaxSize]);
value.Serialize(ref writer);
ServerManager.AddRemoteCall<byte>(this, writer.RawData.Slice(0, writer.Position), rpc.Id, rpc.Flags);
}
else if (rpc.Flags.HasFlagFast(ExecuteFlags.ExecuteOnPrediction) && IsLocalControlled)
rpc.CachedAction(this, value);
}

protected InternalEntity(EntityParams entityParams)
{
Expand Down
Loading

0 comments on commit d22174d

Please sign in to comment.