Skip to content

Commit

Permalink
[Events] Fix null reference (#15)
Browse files Browse the repository at this point in the history
* fix situation when `ply == null`

* lol why

---------

Co-authored-by: IRacle <zornik2006@yandex.ru>
  • Loading branch information
louis1706 and IRacle1 authored Aug 6, 2024
1 parent f1cc6f4 commit eb046ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit eb046ac

Please sign in to comment.