Skip to content

Commit

Permalink
Rewritten Mirror stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
marchellc committed Sep 8, 2024
1 parent cb1a579 commit 2821ce4
Show file tree
Hide file tree
Showing 20 changed files with 832 additions and 483 deletions.
85 changes: 31 additions & 54 deletions LabExtended/API/AdminToy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

using LabExtended.API.Toys;
using LabExtended.API.Wrappers;

using LabExtended.Core;
using LabExtended.Core.Networking;

using LabExtended.Patches.Functions;
using LabExtended.Utilities;

using Mirror;

Expand All @@ -17,7 +19,10 @@ namespace LabExtended.API
public class AdminToy : NetworkWrapper<AdminToyBase>
{
static AdminToy()
=> NetworkSpawnPatch.OnSpawned += OnIdentitySpawned;
=> MirrorEvents.OnSpawn += OnIdentitySpawned;

private NetworkWriterPooled _varWriter = NetworkWriterPool.Get();
private NetworkWriterPooled _segWriter = NetworkWriterPool.Get();

public AdminToy(AdminToyBase baseValue) : base(baseValue) { }

Expand Down Expand Up @@ -93,70 +98,42 @@ private void Modify(bool pos, bool rot, bool scale, bool smoothing, Action actio

private void ResyncStatic(bool writePos, bool writeRot, bool writeScale, bool writeSmoothing)
{
var msg = NetworkUtils.WriteMessage<EntityStateMessage>(writer =>
{
using (var writer2 = NetworkWriterPool.Get())
{
var segment = NetworkUtils.WriteSegment(data =>
{
var num = 0UL | 1UL << (Identity.NetworkBehaviours.IndexOf(Base) & 31);
var mask = 0UL;

Compression.CompressVarUInt(data, num);

if (writePos)
mask |= 1UL;

if (writeRot)
mask |= 2UL;

if (writeScale)
mask |= 4UL;
var mask = 0UL;

if (writeSmoothing)
mask |= 8UL;
if (writePos)
mask |= 1UL;

var pos1 = data.Position;
if (writeRot)
mask |= 2UL;

data.WriteByte(0);
if (writeScale)
mask |= 4UL;

var pos2 = data.Position;
if (writeSmoothing)
mask |= 8UL;

data.WriteULong(mask);
_varWriter.Reset();
_segWriter.Reset();

if (writePos)
data.WriteVector3(Base.Position);

if (writeRot)
data.WriteLowPrecisionQuaternion(Base.Rotation);

if (writeScale)
data.WriteVector3(Base.Scale);

if (writeSmoothing)
data.WriteByte(Base.MovementSmoothing);

var pos3 = data.Position;

data.Position = pos1;

var b = (byte)((pos3 - pos2) & 255);
_varWriter.WriteCustomSyncVars(Base, mask, x =>
{
if (writePos)
x.WriteVector3(Base.Position);

data.WriteByte(b);
data.Position = pos3;
if (writeRot)
x.WriteLowPrecisionQuaternion(Base.Rotation);

Base.ClearAllDirtyBits();
});
if (writeScale)
x.WriteVector3(Base.Scale);

writer2.WriteBytes(segment.Array, segment.Offset, segment.Count);
if (writeSmoothing)
x.WriteByte(Base.MovementSmoothing);
}, _segWriter);

writer.WriteUInt(NetId);
writer.WriteArraySegmentAndSize(writer2.ToArraySegment());
}
});
var data = _varWriter.ToArraySegment();

foreach (var player in ExPlayer.Players)
player.Connection.Send(msg);
player.Connection.Send(data);
}

public static AdminToy Create(AdminToyBase adminToy)
Expand Down
3 changes: 2 additions & 1 deletion LabExtended/API/Containers/RotationContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LabExtended.API.Interfaces;
using LabExtended.Core;
using LabExtended.Core.Networking;
using LabExtended.Utilities;

using Mirror;
Expand Down Expand Up @@ -261,7 +262,7 @@ public void Set(CompressedRotation rotation)
/// <param name="sendPosition">Whether or not to send a position.</param>
/// <param name="positionToSend">The position to send.</param>
public void Set(ushort compressedHorizontal, ushort compressedVertical, bool sendPosition = false, RelativePosition? positionToSend = null)
=> Player.Connection.WriteAndSend<FpcPositionMessage>(writer => InternalWrite(writer, compressedHorizontal, compressedVertical, sendPosition, positionToSend));
=> Player.Connection.WriteMessageToConnection<FpcPositionMessage>(writer => InternalWrite(writer, compressedHorizontal, compressedVertical, sendPosition, positionToSend));

private void InternalWrite(NetworkWriter writer, ushort horizontal, ushort vertical, bool writePosition, RelativePosition? positionToSend)
{
Expand Down
5 changes: 3 additions & 2 deletions LabExtended/API/ExMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
using LabExtended.API.Toys;

using LabExtended.Core;
using LabExtended.Events;
using LabExtended.Core.Networking;

using LabExtended.Extensions;

using LightContainmentZoneDecontamination;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static class ExMap
{
static ExMap()
{
NetworkEvents.OnIdentityDestroyed += OnIdentityDestroyed;
MirrorEvents.OnDestroy += OnIdentityDestroyed;
RagdollManager.ServerOnRagdollCreated += OnRagdollSpawned;
}

Expand Down
90 changes: 0 additions & 90 deletions LabExtended/API/ExPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
using LabExtended.API.Voice;

using LabExtended.Core;
using LabExtended.Core.Ticking;

using LabExtended.Extensions;

using LabExtended.Utilities;
using LabExtended.Utilities.Values;

using LiteNetLib;
Expand All @@ -36,7 +34,6 @@
using PlayerRoles.FirstPersonControl;
using PlayerRoles.FirstPersonControl.NetworkMessages;
using PlayerRoles.Spectating;
using PlayerRoles.Visibility;

using PlayerStatsSystem;

Expand Down Expand Up @@ -1245,93 +1242,6 @@ public void MakeInvisibleFor(ExPlayer player)
public void MakeVisibleFor(ExPlayer player)
=> _invisibility.Remove(player);

/// <summary>
/// Sends a fake SyncVar message.
/// </summary>
/// <param name="behaviorOwner">Identity of the behaviour's owner.</param>
/// <param name="targetType">Type of the behaviour.</param>
/// <param name="propertyName">Name of the network property.</param>
/// <param name="value">The fake value.</param>
public void SendFakeSyncVar(NetworkIdentity behaviorOwner, Type targetType, string propertyName, object value)
{
if (!IsOnline)
return;

var writer = NetworkWriterPool.Get();
var writer2 = NetworkWriterPool.Get();

behaviorOwner.MakeCustomSyncWriter(targetType, null, CustomSyncVarGenerator, writer, writer2);

Connection.Send(new EntityStateMessage
{
netId = behaviorOwner.netId,
payload = writer.ToArraySegment(),
});

NetworkWriterPool.Return(writer);
NetworkWriterPool.Return(writer2);

void CustomSyncVarGenerator(NetworkWriter targetWriter)
{
targetWriter.WriteULong(NetworkUtils._syncVars[$"{targetType.Name}.{propertyName}"]);
NetworkUtils._writerExtensions[value.GetType()]?.Invoke(null, new object[] { targetWriter, value });
}
}

/// <summary>
/// Sends a fake Rpc message.
/// </summary>
/// <param name="behaviorOwner">Identity of the behaviour's owner.</param>
/// <param name="targetType">Type of the behaviour.</param>
/// <param name="rpcName">Name of the network property.</param>
/// <param name="values">The method arguments.</param>
public void SendFakeTargetRpc(NetworkIdentity behaviorOwner, Type targetType, string rpcName, params object[] values)
{
if (!IsOnline)
return;

var writer = NetworkWriterPool.Get();

foreach (var value in values)
NetworkUtils._writerExtensions[value.GetType()].Invoke(null, new object[] { writer, value });

var msg = new RpcMessage()
{
netId = behaviorOwner.netId,

componentIndex = (byte)behaviorOwner.GetComponentIndex(targetType),
functionHash = (ushort)NetworkUtils._rpcNames[$"{targetType.Name}.{rpcName}"].GetStableHashCode(),

payload = writer.ToArraySegment(),
};

Connection.Send(msg);

NetworkWriterPool.Return(writer);
}

/// <summary>
/// Sends a fake SyncObject.
/// </summary>
/// <param name="behaviorOwner">Identity of the behaviour's owner.</param>
/// <param name="targetType">Type of the behaviour.</param>
/// <param name="customAction">The method used to write custom data.</param>
public void SendFakeSyncObject(NetworkIdentity behaviorOwner, Type targetType, Action<NetworkWriter> customAction)
{
if (!IsOnline)
return;

var writer = NetworkWriterPool.Get();
var writer2 = NetworkWriterPool.Get();

behaviorOwner.MakeCustomSyncWriter(targetType, customAction, null, writer, writer2);

Connection.Send(new EntityStateMessage() { netId = behaviorOwner.netId, payload = writer.ToArraySegment() });

NetworkWriterPool.Return(writer);
NetworkWriterPool.Return(writer2);
}

#region Helper Methods
private void SetScale(Vector3 scale)
{
Expand Down
1 change: 1 addition & 0 deletions LabExtended/API/Modules/Module.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LabExtended.Core.Ticking;
using LabExtended.Core.Ticking.Interfaces;

using LabExtended.Extensions;

namespace LabExtended.API.Modules
Expand Down
3 changes: 2 additions & 1 deletion LabExtended/API/Prefabs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using LabExtended.API.Enums;

using LabExtended.Core;
using LabExtended.Core.Networking;
using LabExtended.Events;
using LabExtended.Extensions;
using LabExtended.Patches.Fixes;
Expand All @@ -31,7 +32,7 @@ namespace LabExtended.API
public static class Prefabs
{
static Prefabs()
=> NetworkEvents.OnIdentityDestroyed += HandleNetIdDestroy;
=> MirrorEvents.OnDestroy += HandleNetIdDestroy;

internal static readonly LockedHashSet<DoorVariant> _spawnedDoors = new LockedHashSet<DoorVariant>();

Expand Down
3 changes: 1 addition & 2 deletions LabExtended/Core/Hooking/HookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
using LabExtended.Core.Hooking.Interfaces;
using LabExtended.Core.Hooking.Objects;

using LabExtended.Core.Synchronization.Position;

using MEC;

using PluginAPI.Events;
using PluginAPI.Core.Attributes;

using System.Reflection;
using LabExtended.Core.Networking.Synchronization.Position;

namespace LabExtended.Core.Hooking
{
Expand Down
18 changes: 18 additions & 0 deletions LabExtended/Core/Networking/MirrorEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using LabExtended.Extensions;

using Mirror;

namespace LabExtended.Core.Networking
{
public static class MirrorEvents
{
public static event Action<NetworkIdentity> OnDestroy;
public static event Action<NetworkIdentity> OnSpawn;

internal static void InternalInvokeDestroy(NetworkIdentity identity)
=> OnDestroy.InvokeSafe(identity);

internal static void InternalInvokeSpawn(NetworkIdentity identity)
=> OnSpawn.InvokeSafe(identity);
}
}
Loading

0 comments on commit 2821ce4

Please sign in to comment.