Skip to content

Commit

Permalink
sound argument
Browse files Browse the repository at this point in the history
  • Loading branch information
IRacle1 committed Aug 12, 2024
1 parent dea5569 commit 2df1e62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public InteractingScp330EventArgs(Player player, int usage)
Candy = Scp330Candies.GetRandom();
UsageCount = usage;
ShouldSever = usage >= 2;
ShouldPlaySound = true;
IsAllowed = Player.IsHuman;
}

Expand All @@ -53,10 +54,16 @@ public InteractingScp330EventArgs(Player player, int usage)
/// </summary>
public bool ShouldSever { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the sound should be played.
/// </summary>
/// <remarks>It won't work if <see cref="IsAllowed"/> = <see langword="false"/>.</remarks>
public bool ShouldPlaySound { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the player is allowed to interact with SCP-330.
/// </summary>
public bool IsAllowed { get; set; } = true;
public bool IsAllowed { get; set; }

/// <summary>
/// Gets the <see cref="API.Features.Player" /> triggering the event.
Expand Down
44 changes: 34 additions & 10 deletions EXILED/Exiled.Events/Patches/Events/Scp330/InteractingScp330.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse, returnLabel),
});

/* next code will used to override sound rpc check by EXILED
* old:
* if (args.PlaySound)
* new:
* if (args.PlaySound || ev.PlaySound)
*/

offset = 1;
index = newInstructions.FindLastIndex(
instruction => instruction.Calls(PropertyGetter(typeof(PlayerInteractScp330Event), nameof(PlayerInteractScp330Event.PlaySound)))) + offset;

newInstructions.InsertRange(
index,
new[]
{
// load ev.ShouldPlaySound and or operation with nw property.
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingScp330EventArgs), nameof(InteractingScp330EventArgs.ShouldPlaySound))),
new(OpCodes.Or),
});

/* next code will used to override Sever check by EXILED
* old:
* if (args.AllowPunishment && uses >= 2)
Expand All @@ -82,34 +103,37 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
*/

// set `notSeverLabel`
int addTakenCandiesOffset = -1;
int addTakenCandiesIndex = newInstructions.FindLastIndex(
instruction => instruction.LoadsField(Field(typeof(Scp330Interobject), nameof(Scp330Interobject._takenCandies)))) + addTakenCandiesOffset;
offset = -1;
index = newInstructions.FindLastIndex(
instruction => instruction.LoadsField(Field(typeof(Scp330Interobject), nameof(Scp330Interobject._takenCandies)))) + offset;

Label notSeverLabel = newInstructions[addTakenCandiesIndex].labels[0];
Label notSeverLabel = newInstructions[index].labels[0];

int allowPunishmentOffset = 2;
int allowPunishmentIndex = newInstructions.FindLastIndex(
instruction => instruction.Calls(PropertyGetter(typeof(PlayerInteractScp330Event), nameof(PlayerInteractScp330Event.AllowPunishment)))) + allowPunishmentOffset;
offset = 2;
index = newInstructions.FindLastIndex(
instruction => instruction.Calls(PropertyGetter(typeof(PlayerInteractScp330Event), nameof(PlayerInteractScp330Event.AllowPunishment)))) + offset;

// remove `uses >= 2` check, to override that by ev.ShouldSever
newInstructions.RemoveRange(allowPunishmentIndex, 3);
newInstructions.RemoveRange(index, 3);

newInstructions.InsertRange(
allowPunishmentIndex,
index,
new[]
{
// if (!ev.ShouldSever)
// goto shouldNotSever;
new CodeInstruction(OpCodes.Ldloc, ev.LocalIndex),
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingScp330EventArgs), nameof(InteractingScp330EventArgs.ShouldSever))),
new(OpCodes.Brfalse_S, notSeverLabel),
});

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);

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

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

0 comments on commit 2df1e62

Please sign in to comment.