Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch Rework DoorList #2270

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading