forked from Exiled-Official/EXILED
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Useless Event (939 Placed Amnestic Cloud) (#40)
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
EXILED/Exiled.Events/EventArgs/Scp939/PlacedAmnesticCloudEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="PlacedAmnesticCloudEventArgs.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.EventArgs.Scp939 | ||
{ | ||
using API.Features; | ||
using API.Features.Hazards; | ||
using Interfaces; | ||
using PlayerRoles.PlayableScps.Scp939; | ||
|
||
using Scp939Role = API.Features.Roles.Scp939Role; | ||
|
||
/// <summary> | ||
/// Contains all information after SCP-939 used its amnestic cloud ability. | ||
/// </summary> | ||
public class PlacedAmnesticCloudEventArgs : IScp939Event | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlacedAmnesticCloudEventArgs" /> class. | ||
/// </summary> | ||
/// <param name="hub"> | ||
/// <inheritdoc cref="ReferenceHub" /> | ||
/// </param> | ||
/// <param name="cloud"> | ||
/// <inheritdoc cref="PlayerRoles.PlayableScps.Scp939.Scp939AmnesticCloudInstance" /> | ||
/// </param> | ||
public PlacedAmnesticCloudEventArgs(ReferenceHub hub, Scp939AmnesticCloudInstance cloud) | ||
{ | ||
Player = Player.Get(hub); | ||
AmnesticCloud = new AmnesticCloudHazard(cloud); | ||
Scp939 = Player.Role.As<Scp939Role>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the player who's controlling SCP-939. | ||
/// </summary> | ||
public Player Player { get; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="AmnesticCloudHazard"/> instance. | ||
/// </summary> | ||
public AmnesticCloudHazard AmnesticCloud { get; } | ||
|
||
/// <inheritdoc/> | ||
public Scp939Role Scp939 { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
EXILED/Exiled.Events/Patches/Events/Scp939/PlacedAmnesticCloud.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="PlacedAmnesticCloud.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.Patches.Events.Scp939 | ||
{ | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
|
||
using Exiled.API.Features.Pools; | ||
using Exiled.Events.Attributes; | ||
using Exiled.Events.EventArgs.Scp939; | ||
using Exiled.Events.Handlers; | ||
using HarmonyLib; | ||
using PlayerRoles.PlayableScps.Scp939; | ||
|
||
using static HarmonyLib.AccessTools; | ||
|
||
/// <summary> | ||
/// Patches <see cref="Scp939AmnesticCloudAbility.OnStateEnabled" /> | ||
/// to add the <see cref="Scp939.PlacedAmnesticCloud" /> event. | ||
/// </summary> | ||
[EventPatch(typeof(Scp939), nameof(Scp939.PlacedAmnesticCloud))] | ||
[HarmonyPatch(typeof(Scp939AmnesticCloudAbility), nameof(Scp939AmnesticCloudAbility.OnStateEnabled))] | ||
internal static class PlacedAmnesticCloud | ||
{ | ||
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
||
LocalBuilder cloud = generator.DeclareLocal(typeof(Scp939AmnesticCloudInstance)); | ||
|
||
const int offset = -2; | ||
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Callvirt && x.OperandIs(Method(typeof(Scp939AmnesticCloudInstance), nameof(Scp939AmnesticCloudInstance.ServerSetup)))) + offset; | ||
|
||
newInstructions.InsertRange( | ||
index, | ||
new[] | ||
{ | ||
// Scp939AmnesticCloudInstance cloud = Object.Instantiate<Scp939AmnesticCloudInstance>(this._instancePrefab) | ||
new CodeInstruction(OpCodes.Dup), | ||
new CodeInstruction(OpCodes.Stloc_S, cloud), | ||
}); | ||
|
||
index = newInstructions.Count - 1; | ||
|
||
// Scp939.OnPlacedAmnesticCloud(new PlacedAmnesticCloudEventArgs(this.Owner, cloud)); | ||
newInstructions.InsertRange( | ||
index, | ||
new[] | ||
{ | ||
// this.Owner | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp939AmnesticCloudAbility), nameof(Scp939AmnesticCloudAbility.Owner))), | ||
|
||
// cloud | ||
new CodeInstruction(OpCodes.Ldloc_S, cloud), | ||
|
||
// Scp939.OnPlacedAmnesticCloud(new PlacedAmnesticCloudEventArgs(this.Owner, cloud)); | ||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(PlacedAmnesticCloudEventArgs))[0]), | ||
new(OpCodes.Call, Method(typeof(Scp939), nameof(Scp939.OnPlacedAmnesticCloud))), | ||
}); | ||
|
||
for (int z = 0; z < newInstructions.Count; z++) | ||
yield return newInstructions[z]; | ||
|
||
ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
} | ||
} | ||
} |