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

Scp173BeingLooked Transpiler #2276

Merged
merged 4 commits into from
Dec 18, 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: 44 additions & 11 deletions Exiled.Events/Patches/Generic/Scp173BeingLooked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

namespace Exiled.Events.Patches.Generic
{
#pragma warning disable SA1313
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using Exiled.API.Features.Pools;

using HarmonyLib;

using PlayerRoles;
using PlayerRoles.PlayableScps.Scp173;

using static HarmonyLib.AccessTools;

using ExiledEvents = Exiled.Events.Events;
using Scp173Role = API.Features.Roles.Scp173Role;

Expand All @@ -24,19 +29,47 @@ namespace Exiled.Events.Patches.Generic
[HarmonyPatch(typeof(Scp173ObserversTracker), nameof(Scp173ObserversTracker.UpdateObserver))]
internal static class Scp173BeingLooked
{
private static bool Prefix(Scp173ObserversTracker __instance, ReferenceHub targetHub, ref int __result)
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
if (Player.Get(targetHub) is Player player &&
((player.Role.Type == RoleTypeId.Tutorial &&
!ExiledEvents.Instance.Config.CanTutorialBlockScp173) ||
Scp173Role.TurnedPlayers.Contains(player)) &&
__instance.IsObservedBy(targetHub, 0.2f))
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label continueLabel = generator.DefineLabel();
Label skipLabel = generator.DefineLabel();

newInstructions.InsertRange(0, new CodeInstruction[]
{
__result = __instance.Observers.Remove(targetHub) ? -1 : 0;
return false;
}
// if(Scp173BeingLooked.HelpMethod(Scp173BeingLooked, ReferenceHub))
// return this.Observers.Remove(ReferenceHub) ? -1 : 0;
new(OpCodes.Ldarg_0),
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Scp173BeingLooked), nameof(HelpMethod))),

new(OpCodes.Brfalse_S, continueLabel),

new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(Scp173ObserversTracker), nameof(Scp173ObserversTracker.Observers))),
new(OpCodes.Ldarg_1),
new(OpCodes.Callvirt, Method(typeof(HashSet<ReferenceHub>), nameof(HashSet<ReferenceHub>.Remove))),
new(OpCodes.Brtrue_S, skipLabel),

new(OpCodes.Ldc_I4_0),
new(OpCodes.Ret),

return true;
new CodeInstruction(OpCodes.Ldc_I4_M1).WithLabels(skipLabel),
new(OpCodes.Ret),

new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel),
});

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

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

private static bool HelpMethod(Scp173ObserversTracker instance, ReferenceHub targetHub)
{
return Player.Get(targetHub) is Player player && ((player.Role.Type == RoleTypeId.Tutorial && !ExiledEvents.Instance.Config.CanTutorialBlockScp173) || Scp173Role.TurnedPlayers.Contains(player)) && instance.IsObservedBy(targetHub, Scp173ObserversTracker.WidthMultiplier);
}
}
}
Loading