Decompiled source of ReceiveAmmoGiveFix v1.1.0

Hikaria.ReceiveAmmoGiveFix.dll

Decompiled 5 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Unity.IL2CPP;
using GameData;
using Gear;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Player;
using SNetwork;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("Hikaria.ReceiveAmmoGiveFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Hikaria.ReceiveAmmoGiveFix")]
[assembly: AssemblyTitle("Hikaria.ReceiveAmmoGiveFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Hikaria.ReceiveAmmoGiveFix
{
	[BepInPlugin("Hikaria.ReceiveAmmoGiveFix", "ReceiveAmmoGiveFix", "1.1.0")]
	public class EntryPoint : BasePlugin
	{
		private static Harmony harmony;

		public static EntryPoint Instance { get; private set; }

		public override void Load()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			Instance = this;
			harmony = new Harmony("Hikaria.ReceiveAmmoGiveFix");
			harmony.PatchAll();
			Logs.LogMessage("OK");
		}
	}
	internal static class Logs
	{
		public static void LogDebug(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogDebug(data);
		}

		public static void LogError(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogError(data);
		}

		public static void LogFatal(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogFatal(data);
		}

		public static void LogInfo(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogInfo(data);
		}

		public static void LogMessage(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogMessage(data);
		}

		public static void LogWarning(object data)
		{
			((BasePlugin)EntryPoint.Instance).Log.LogWarning(data);
		}
	}
	public static class PluginInfo
	{
		public const string GUID = "Hikaria.ReceiveAmmoGiveFix";

		public const string NAME = "ReceiveAmmoGiveFix";

		public const string VERSION = "1.1.0";

		public const string AUTHOR = "Tuesday";
	}
}
namespace Hikaria.ReceiveAmmoGiveFix.Patches
{
	[HarmonyPatch(typeof(PlayerBackpackManager), "ReceiveAmmoGive")]
	public class PlayerBackpackManager__ReceiveAmmoGive__Patch
	{
		private static Dictionary<InventorySlot, float> AmmoMaxCapLookup = new Dictionary<InventorySlot, float>
		{
			{
				(InventorySlot)1,
				0f
			},
			{
				(InventorySlot)2,
				0f
			}
		};

		private static InventorySlotAmmo StandardAmmo;

		private static InventorySlotAmmo SpeacialAmmo;

		private static bool NeedRestoredStandardAmmoMaxCap = false;

		private static bool NeedRestoredSpecialAmmoMaxCap = false;

		private static float AmmoStandardResourcePackMaxCap => GameDataBlockBase<PlayerDataBlock>.GetBlock(1u).AmmoStandardResourcePackMaxCap;

		private static float AmmoSpecialResourcePackMaxCap => GameDataBlockBase<PlayerDataBlock>.GetBlock(1u).AmmoSpecialResourcePackMaxCap;

		private static void Prefix(ref pAmmoGive data)
		{
			bool flag = false;
			bool flag2 = false;
			float num = 0f;
			float num2 = 0f;
			SNet_Player val = default(SNet_Player);
			if (((pPlayer)(ref data.targetPlayer)).TryGetPlayer(ref val) && val.IsLocal)
			{
				BackpackItem val2 = default(BackpackItem);
				if (PlayerBackpackManager.LocalBackpack.TryGetBackpackItem((InventorySlot)1, ref val2))
				{
					BulletWeapon val3 = ((Il2CppObjectBase)val2.Instance).TryCast<BulletWeapon>();
					float num3 = (float)(((ItemEquippable)val3).ClipSize - val3.m_clip) * ((ItemEquippable)val3).ArchetypeData.CostOfBullet;
					StandardAmmo = PlayerBackpackManager.LocalBackpack.AmmoStorage.StandardAmmo;
					AmmoMaxCapLookup[(InventorySlot)1] = StandardAmmo.AmmoMaxCap;
					InventorySlotAmmo standardAmmo = StandardAmmo;
					standardAmmo.AmmoMaxCap += num3;
					NeedRestoredStandardAmmoMaxCap = true;
					num = data.ammoStandardRel * AmmoStandardResourcePackMaxCap + StandardAmmo.AmmoInPack - StandardAmmo.AmmoMaxCap;
					flag = num > 0f;
				}
				if (PlayerBackpackManager.LocalBackpack.TryGetBackpackItem((InventorySlot)2, ref val2))
				{
					BulletWeapon val4 = ((Il2CppObjectBase)val2.Instance).TryCast<BulletWeapon>();
					float num4 = (float)(((ItemEquippable)val4).ClipSize - val4.m_clip) * ((ItemEquippable)val4).ArchetypeData.CostOfBullet;
					SpeacialAmmo = PlayerBackpackManager.LocalBackpack.AmmoStorage.SpecialAmmo;
					AmmoMaxCapLookup[(InventorySlot)2] = SpeacialAmmo.AmmoMaxCap;
					InventorySlotAmmo speacialAmmo = SpeacialAmmo;
					speacialAmmo.AmmoMaxCap += num4;
					NeedRestoredSpecialAmmoMaxCap = true;
					num2 = data.ammoSpecialRel * AmmoSpecialResourcePackMaxCap + SpeacialAmmo.AmmoInPack - SpeacialAmmo.AmmoMaxCap;
					flag2 = num2 > 0f;
				}
				if (flag && !flag2)
				{
					data.ammoSpecialRel += num / AmmoStandardResourcePackMaxCap;
				}
				else if (!flag && flag2)
				{
					data.ammoStandardRel += num2 / AmmoSpecialResourcePackMaxCap;
				}
			}
		}

		private static void Postfix(pAmmoGive data)
		{
			SNet_Player val = default(SNet_Player);
			if (((pPlayer)(ref data.targetPlayer)).TryGetPlayer(ref val) && val.IsLocal)
			{
				BackpackItem val2 = default(BackpackItem);
				if (PlayerBackpackManager.LocalBackpack.TryGetBackpackItem((InventorySlot)1, ref val2))
				{
					StandardAmmo.AmmoMaxCap = AmmoMaxCapLookup[(InventorySlot)1];
					PlayerBackpackManager.LocalBackpack.AmmoStorage.UpdateSlotAmmoUI((InventorySlot)1);
					NeedRestoredStandardAmmoMaxCap = false;
				}
				if (PlayerBackpackManager.LocalBackpack.TryGetBackpackItem((InventorySlot)2, ref val2))
				{
					SpeacialAmmo.AmmoMaxCap = AmmoMaxCapLookup[(InventorySlot)2];
					PlayerBackpackManager.LocalBackpack.AmmoStorage.UpdateSlotAmmoUI((InventorySlot)2);
					NeedRestoredSpecialAmmoMaxCap = false;
				}
			}
		}
	}
}