Skip to content

Commit ed260ed

Browse files
committed
feat: VoiceLinesEventArgs VoiceLinesName ClipId Scp3114VoiceLines & fix issue that it's was called too early (#826)
1 parent 4c8162d commit ed260ed

2 files changed

Lines changed: 66 additions & 19 deletions

File tree

EXILED/Exiled.Events/EventArgs/Scp3114/VoiceLinesEventArgs.cs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ public class VoiceLinesEventArgs : IScp3114Event, IDeniableEvent
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="VoiceLinesEventArgs" /> class.
2323
/// </summary>
24-
/// <param name="player">
25-
/// <inheritdoc cref="Player" />
26-
/// </param>
27-
/// <param name="voiceLine">
28-
/// <inheritdoc cref="VoiceLine" />
29-
/// </param>
30-
/// <param name="isAllowed">
31-
/// <inheritdoc cref="IsAllowed" />
32-
/// </param>
33-
public VoiceLinesEventArgs(ReferenceHub player, VoiceLinesDefinition voiceLine, bool isAllowed = true)
24+
/// <param name="scp3114VoiceLines"><inheritdoc cref="Scp3114VoiceLines" /> </param>
25+
/// <param name="voiceLineDefinition"><inheritdoc cref="VoiceLine" /> </param>
26+
/// <param name="clipId"><inheritdoc cref="ClipId" /> </param>
27+
/// <param name="isAllowed"><inheritdoc cref="IsAllowed" /></param>
28+
public VoiceLinesEventArgs(PlayerRoles.PlayableScps.Scp3114.Scp3114VoiceLines scp3114VoiceLines, VoiceLinesDefinition voiceLineDefinition, byte clipId, bool isAllowed = true)
3429
{
35-
Player = Player.Get(player);
30+
Scp3114VoiceLines = scp3114VoiceLines;
31+
Player = Player.Get(scp3114VoiceLines.Owner);
3632
Scp3114 = Player.Role.As<Scp3114Role>();
37-
VoiceLine = voiceLine;
33+
VoiceLine = voiceLineDefinition;
34+
ClipId = clipId;
3835
IsAllowed = isAllowed;
3936
}
4037

@@ -44,11 +41,53 @@ public VoiceLinesEventArgs(ReferenceHub player, VoiceLinesDefinition voiceLine,
4441
/// <inheritdoc/>
4542
public Scp3114Role Scp3114 { get; }
4643

44+
/// <summary>
45+
/// Gets the <see cref="PlayerRoles.PlayableScps.Scp3114.Scp3114VoiceLines" />.
46+
/// </summary>
47+
public PlayerRoles.PlayableScps.Scp3114.Scp3114VoiceLines Scp3114VoiceLines { get; }
48+
4749
/// <summary>
4850
/// Gets or sets the <see cref="VoiceLinesDefinition" />.
4951
/// </summary>
5052
public VoiceLinesDefinition VoiceLine { get; set; }
5153

54+
/// <summary>
55+
/// Gets or sets the <see cref="PlayerRoles.PlayableScps.Scp3114.Scp3114VoiceLines.VoiceLinesName" />.
56+
/// </summary>
57+
public VoiceLinesName VoiceLinesName
58+
{
59+
get => VoiceLine.Label;
60+
set
61+
{
62+
if (value == VoiceLine.Label)
63+
return;
64+
65+
foreach (VoiceLinesDefinition voiceLinesDefinition in Scp3114VoiceLines._voiceLines)
66+
{
67+
if (voiceLinesDefinition.Label != value)
68+
continue;
69+
70+
VoiceLine = voiceLinesDefinition;
71+
return;
72+
}
73+
74+
Log.Error($"[{typeof(VoiceLinesEventArgs)}.{nameof(VoiceLinesName)}] didn't found VoiceLinesName: {value}");
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Gets or sets the <see cref="VoiceLinesDefinition" />.
80+
/// </summary>
81+
public byte ClipId
82+
{
83+
get => (byte)VoiceLine._lastIndex;
84+
set
85+
{
86+
value = (byte)(value % VoiceLine._order.Count);
87+
VoiceLine._lastIndex = value;
88+
}
89+
}
90+
5291
/// <inheritdoc/>
5392
public bool IsAllowed { get; set; }
5493
}

EXILED/Exiled.Events/Patches/Events/Scp3114/VoiceLines.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Exiled.Events.Patches.Events.Scp3114
2020
using PlayerRoles.PlayableScps.Scp3114;
2121

2222
using static HarmonyLib.AccessTools;
23+
using static PlayerRoles.PlayableScps.Scp3114.Scp3114VoiceLines;
2324

2425
/// <summary>
2526
/// Patches <see cref="Scp3114VoiceLines.ServerPlayConditionally" />.
@@ -37,29 +38,31 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
3738

3839
LocalBuilder ev = generator.DeclareLocal(typeof(VoiceLinesEventArgs));
3940

40-
int offset = 1;
41-
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Blt_S) + offset;
41+
int offset = 3;
42+
int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(VoiceLinesDefinition), nameof(VoiceLinesDefinition.TryDrawNext)))) + offset;
4243

4344
newInstructions.InsertRange(index, new CodeInstruction[]
4445
{
45-
// this.Owner
46-
new(OpCodes.Ldarg_0),
47-
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp3114VoiceLines), nameof(Scp3114VoiceLines.Owner))),
46+
// this
47+
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
4848

4949
// voiceLinesDefinition
5050
new(OpCodes.Ldloc_0),
5151

52+
// clipId
53+
new(OpCodes.Ldloc_2),
54+
5255
// true
5356
new(OpCodes.Ldc_I4_1),
5457

55-
// VoiceLinesEventArgs ev = new VoiceLinesEventArgs(ReferenceHub, VoiceLinesDefinition, bool);
58+
// VoiceLinesEventArgs ev = new VoiceLinesEventArgs(Scp3114VoiceLines, VoiceLinesDefinition, byte, bool);
5659
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(VoiceLinesEventArgs))[0]),
5760
new(OpCodes.Dup),
5861
new(OpCodes.Dup),
5962
new(OpCodes.Stloc_S, ev.LocalIndex),
6063

6164
// Handlers.Scp3114.OnVoiceLines(ev);
62-
new(OpCodes.Call, Method(typeof(Handlers.Scp3114), nameof(Handlers.Scp3114.OnVoiceLines))),
65+
new(OpCodes.Call, Method(typeof(Scp3114), nameof(Scp3114.OnVoiceLines))),
6366

6467
// if(!ev.IsAllowed)
6568
// return;
@@ -70,6 +73,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
7073
new(OpCodes.Ldloc_S, ev.LocalIndex),
7174
new(OpCodes.Callvirt, PropertyGetter(typeof(VoiceLinesEventArgs), nameof(VoiceLinesEventArgs.VoiceLine))),
7275
new(OpCodes.Stloc_S, 0),
76+
77+
// clipId = ev.ClipId;
78+
new(OpCodes.Ldloc_S, ev.LocalIndex),
79+
new(OpCodes.Callvirt, PropertyGetter(typeof(VoiceLinesEventArgs), nameof(VoiceLinesEventArgs.ClipId))),
80+
new(OpCodes.Stloc_S, 2),
7381
});
7482

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

0 commit comments

Comments
 (0)