Skip to content

Commit

Permalink
Patch Rework DoorList (#2270)
Browse files Browse the repository at this point in the history
* Patch Rework

* Update Exiled.Events/Patches/Generic/DoorList.cs

* uhmmmm
  • Loading branch information
louis1706 authored Dec 5, 2023
1 parent d6e0921 commit 304d8da
Showing 1 changed file with 6 additions and 49 deletions.
55 changes: 6 additions & 49 deletions Exiled.Events/Patches/Generic/DoorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Exiled.Events.Patches.Generic
{
#pragma warning disable SA1313
#pragma warning disable SA1402
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
Expand All @@ -22,44 +21,20 @@ namespace Exiled.Events.Patches.Generic
using HarmonyLib;

using Interactables.Interobjects.DoorUtils;
using MapGeneration;

using UnityEngine;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="DoorVariant.RegisterRooms"/>.
/// </summary>
// TODO: transpiler
[HarmonyPatch(typeof(DoorVariant), nameof(DoorVariant.RegisterRooms))]
internal class DoorList
{
private static bool Prefix(DoorVariant __instance)
private static void Postfix(DoorVariant __instance)
{
/*EXILED*/
if (__instance.Rooms != null)
return false;
/*EXILED*/

Vector3 position = __instance.transform.position;
int length = 0;

for (int index = 0; index < 4; ++index)
{
Vector3Int coords = RoomIdUtils.PositionToCoords(position + DoorVariant.WorldDirections[index]);

if (RoomIdentifier.RoomsByCoordinates.TryGetValue(coords, out RoomIdentifier key) && DoorVariant.DoorsByRoom.GetOrAdd(key, () => new HashSet<DoorVariant>()).Add(__instance))
{
DoorVariant.RoomsNonAlloc[length] = key;
++length;
}
}

__instance.Rooms = new RoomIdentifier[length];
Array.Copy(DoorVariant.RoomsNonAlloc, __instance.Rooms, length);
if (Door.DoorVariantToDoor.ContainsKey(__instance))
return;

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

Door door = Door.Create(__instance, rooms);
Expand All @@ -78,9 +53,7 @@ private static bool Prefix(DoorVariant __instance)
}
}

/*EXILED*/

return false;
return;
}
}

Expand All @@ -90,25 +63,9 @@ private static bool Prefix(DoorVariant __instance)
[HarmonyPatch(typeof(DoorVariant), nameof(DoorVariant.OnDestroy))]
internal class DoorListRemove
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> codeInstructions)
private static void Prefix(DoorVariant __instance)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(codeInstructions);

// Door.DoorVariantToDoor.Remove(this)
newInstructions.InsertRange(
0,
new CodeInstruction[]
{
new(OpCodes.Ldsfld, Field(typeof(Door), nameof(Door.DoorVariantToDoor))),
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, Method(typeof(Dictionary<DoorVariant, Door>), nameof(Dictionary<DoorVariant, Door>.Remove), new[] { typeof(DoorVariant) })),
new(OpCodes.Pop),
});

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
Door.DoorVariantToDoor.Remove(__instance);
}
}
}

0 comments on commit 304d8da

Please sign in to comment.