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

[Events] Fix null reference #15

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Item Item
get => newItem;
set
{
if (!Player.Inventory.UserInventory.Items.TryGetValue(value.Serial, out _))
if (value != null && !Player.Inventory.UserInventory.Items.TryGetValue(value.Serial, out _))
throw new InvalidOperationException("ev.NewItem cannot be set to an item they do not have.");

newItem = value;
Expand Down
17 changes: 16 additions & 1 deletion EXILED/Exiled.Events/Patches/Events/Player/Healing.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="Healing.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
Expand Down Expand Up @@ -34,6 +34,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label continueLabel = generator.DefineLabel();
Label skipHealing = generator.DefineLabel();
Label skipHealed = generator.DefineLabel();

LocalBuilder ev = generator.DeclareLocal(typeof(HealingEventArgs));
LocalBuilder player = generator.DeclareLocal(typeof(Player));
Expand All @@ -48,10 +50,14 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Stloc_S, lastHealth.LocalIndex),

// player = Player.Get(this.Hub);
// if (player == null)
// skip;
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(HealthStat), nameof(HealthStat.Hub))),
new(OpCodes.Call, AccessTools.Method(typeof(Player), nameof(Player.Get), new Type[] { typeof(ReferenceHub) })),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, player.LocalIndex),
new(OpCodes.Brfalse_S, skipHealing),

// HealingEventArgs ev = new(Player, amount)
new(OpCodes.Ldloc_S, player.LocalIndex),
Expand All @@ -76,10 +82,19 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex).WithLabels(continueLabel),
new(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(HealingEventArgs), nameof(HealingEventArgs.Amount))),
new(OpCodes.Starg_S, 1),

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

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

newInstructions.InsertRange(newInstructions.Count - 1, new[]
{
// if (player == null)
// skip;
new(OpCodes.Ldloc_S, player.LocalIndex),
new(OpCodes.Brfalse_S, skipHealed),

// HealedEventArgs ev = new(Player, lastAmount)
new(OpCodes.Ldloc_S, player.LocalIndex),
new(OpCodes.Ldloc_S, lastHealth.LocalIndex),
Expand Down
Loading