Skip to content
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
23 changes: 20 additions & 3 deletions EXILED/Exiled.API/Features/Pickups/FirearmPickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Exiled.API.Features.Pickups
{
using System;

using Exiled.API.Features.Items.FirearmModules;
using Exiled.API.Features.Items.FirearmModules.Primary;
using Exiled.API.Interfaces;
using InventorySystem.Items;
using InventorySystem.Items.Firearms;
Expand Down Expand Up @@ -168,13 +170,28 @@ internal override void ReadItemInfo(Items.Item item)
protected override void InitializeProperties(ItemBase itemBase)
{
base.InitializeProperties(itemBase);
if (!(itemBase as Firearm).TryGetModule(out IPrimaryAmmoContainerModule magazine))
if (itemBase is not Firearm firearm)
{
Log.Error($"firearm prefab {itemBase.ItemTypeId} doesnt have an primary magazine module(unexpected)");
Log.Error("FirearmPickup::InitializeProperties called with a non-firearm item!");
return;
}

MaxAmmo = magazine.AmmoMax;
foreach (ModuleBase module in firearm.Modules)
{
switch (module)
{
case IPrimaryAmmoContainerModule primaryAmmoModule:
MaxAmmo = primaryAmmoModule.AmmoMax;
break;

case HitscanHitregModuleBase hitregModule:
Damage = hitregModule.BaseDamage;
Inaccuracy = hitregModule.BaseBulletInaccuracy;
Penetration = hitregModule.BasePenetration;
DamageFalloffDistance = hitregModule.DamageFalloffDistance;
break;
}
}
}
}
}
49 changes: 49 additions & 0 deletions EXILED/Exiled.Events/Patches/Fixes/Scp127TryGetCurPairFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -----------------------------------------------------------------------
// <copyright file="Scp127TryGetCurPairFix.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Fixes
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features.Pools;
using HarmonyLib;
using InventorySystem.Items.Firearms.Modules.Scp127;
using UnityEngine;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp127Hitscan.TryGetCurPair"/> to work and return false when the module has no owner.
/// </summary>
[HarmonyPatch(typeof(Scp127Hitscan), nameof(Scp127Hitscan.TryGetCurPair))]
public class Scp127TryGetCurPairFix
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label retLabel = generator.DefineLabel();

newInstructions.FindLast(x => x.opcode == OpCodes.Ldarg_1).labels.Add(retLabel);

newInstructions.InsertRange(0, new CodeInstruction[]
{
// check if owner is null, if so, jump to last 2 lines of method, else, run the method.
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp127Hitscan), nameof(Scp127Hitscan.Owner))),
new(OpCodes.Call, Method(typeof(Object), "op_Implicit")),
new(OpCodes.Brfalse_S, retLabel),
});

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

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