Skip to content

Commit

Permalink
new better SendFakeSyncVar method
Browse files Browse the repository at this point in the history
  • Loading branch information
IRacle1 committed Aug 11, 2024
1 parent 4caf07f commit 6b6c0ad
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions EXILED/Exiled.API/Extensions/MirrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public static void MessageTranslated(this Player player, string words, string tr
/// <param name="targetType"><see cref="NetworkBehaviour"/>'s type.</param>
/// <param name="propertyName">Property name starting with Network.</param>
/// <param name="value">Value of send to target.</param>
[Obsolete("Use overload with type-template instead.")]
public static void SendFakeSyncVar(this Player target, NetworkIdentity behaviorOwner, Type targetType, string propertyName, object value)
{
if (!target.IsConnected)
Expand All @@ -386,6 +387,38 @@ void CustomSyncVarGenerator(NetworkWriter targetWriter)
}
}

/// <summary>
/// Send fake values to client's <see cref="SyncVarAttribute"/>.
/// </summary>
/// <typeparam name="T">Target SyncVar property type.</typeparam>
/// <param name="target">Target to send.</param>
/// <param name="behaviorOwner"><see cref="NetworkIdentity"/> of object that owns <see cref="NetworkBehaviour"/>.</param>
/// <param name="targetType"><see cref="NetworkBehaviour"/>'s type.</param>
/// <param name="propertyName">Property name starting with Network.</param>
/// <param name="value">Value of send to target.</param>
public static void SendFakeSyncVar<T>(this Player target, NetworkIdentity behaviorOwner, Type targetType, string propertyName, T value)
{
if (!target.IsConnected)
return;

NetworkWriterPooled writer = NetworkWriterPool.Get();
NetworkWriterPooled writer2 = NetworkWriterPool.Get();
MakeCustomSyncWriter(behaviorOwner, targetType, null, CustomSyncVarGenerator, writer, writer2);
target.Connection.Send(new EntityStateMessage
{
netId = behaviorOwner.netId,
payload = writer.ToArraySegment(),
});

NetworkWriterPool.Return(writer);
NetworkWriterPool.Return(writer2);
void CustomSyncVarGenerator(NetworkWriter targetWriter)
{
targetWriter.WriteULong(SyncVarDirtyBits[$"{targetType.Name}.{propertyName}"]);
WriterExtensions[typeof(T)]?.Invoke(null, new object[2] { targetWriter, value });
}
}

/// <summary>
/// Force resync to client's <see cref="SyncVarAttribute"/>.
/// </summary>
Expand Down

0 comments on commit 6b6c0ad

Please sign in to comment.