Skip to content

Commit

Permalink
πŸ˜±πŸ˜ŽπŸ™‚πŸ’€πŸ˜ˆ (#51)
Browse files Browse the repository at this point in the history
* Π Π°Π·Ρ€Π°Π±Ρ‹ Π΄Π°ΡƒΠ½Ρ‹πŸ˜±πŸ˜ŽπŸ™‚πŸ’€πŸ˜ˆ

* new better `SendFakeSyncVar` method

* ililililililil

* c
  • Loading branch information
IRacle1 authored Aug 27, 2024
1 parent 5fc1b14 commit 327801e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 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 @@ -431,6 +431,7 @@ public static void ScaleNetworkIdentityObject(this NetworkIdentity identity, Vec
/// <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 @@ -454,6 +455,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
42 changes: 38 additions & 4 deletions EXILED/Exiled.Events/Patches/Generic/DoorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,48 @@ namespace Exiled.Events.Patches.Generic
[HarmonyPatch(typeof(DoorVariant), nameof(DoorVariant.RegisterRooms))]
internal class DoorList
{
private static void Postfix(DoorVariant __instance)
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
if (Door.DoorVariantToDoor.ContainsKey(__instance))
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label ret = generator.DefineLabel();

// if (Rooms == null)
// return;
newInstructions.InsertRange(
0,
new CodeInstruction[]
{
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(DoorVariant), nameof(DoorVariant.Rooms))),
new(OpCodes.Brfalse_S, ret),
});

// DoorList.InitDoor(this);
newInstructions.InsertRange(
newInstructions.Count - 1,
new CodeInstruction[]
{
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(DoorList), nameof(DoorList.InitDoor))),
});

newInstructions[newInstructions.Count - 1].labels.Add(ret);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

private static void InitDoor(DoorVariant doorVariant)
{
if (Door.DoorVariantToDoor.ContainsKey(doorVariant))
return;

List<Room> rooms = __instance.Rooms.Select(identifier => Room.RoomIdentifierToRoom[identifier]).ToList();
List<Room> rooms = doorVariant.Rooms.Select(identifier => Room.RoomIdentifierToRoom[identifier]).ToList();

Door door = Door.Create(__instance, rooms);
Door door = Door.Create(doorVariant, rooms);

foreach (Room room in rooms)
{
Expand Down

0 comments on commit 327801e

Please sign in to comment.