Skip to content

Commit

Permalink
Optimising / More documentation on SpawningItem
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 committed Jul 31, 2024
1 parent ee011f7 commit 50d1b67
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions EXILED/Exiled.Events/Patches/Events/Map/SpawningItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Ldc_I4_0).MoveLabelsFrom(newInstructions[lastIndex]),
new(OpCodes.Stloc_S, initiallySpawn.LocalIndex),

// door = nametagExtension.TargetDoor
// door = doorNametagExtension.TargetDoor
new(OpCodes.Ldloc_2),
new(OpCodes.Ldfld, Field(typeof(DoorVariantExtension), nameof(DoorVariantExtension.TargetDoor))),
new(OpCodes.Stloc_S, door.LocalIndex),
Expand Down Expand Up @@ -99,30 +99,28 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// goto doorSpawn
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(SpawningItemEventArgs), nameof(SpawningItemEventArgs.ShouldInitiallySpawn))),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Ceq),
new(OpCodes.Brfalse_S, doorSpawn),

new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(SpawningItemEventArgs), nameof(SpawningItemEventArgs.TriggerDoor))),
new(OpCodes.Ldnull),
new(OpCodes.Cgt_Un),

new(OpCodes.And),
new(OpCodes.Brtrue_S, doorSpawn),
new(OpCodes.Brfalse_S, doorSpawn),
});

lastIndex = newInstructions.FindLastIndex(instruction => instruction.IsLdarg(0));

newInstructions[lastIndex].labels.Add(doorSpawn);

// Replace
// "base.RegisterUnspawnedObject(doorNametagExtension.TargetDoor, itemPickupBase.gameObject);"
// with "base.RegisterUnspawnedObject(ev.Door.Base, itemPickupBase.gameObject);"
offset = -1;
index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Ldfld) + offset;

newInstructions.RemoveRange(index, 2);

newInstructions.InsertRange(index, new[]
{
// door = ev.Door.Base
// ev.Door.Base
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(SpawningItemEventArgs), nameof(SpawningItemEventArgs.TriggerDoor))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Door), nameof(Door.Base))),
Expand Down

0 comments on commit 50d1b67

Please sign in to comment.