Decompiled source of PackUseMultiplier v1.0.0

PackUseMultiplier.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using AK;
using Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using GTFO.API.Utilities;
using GameEvent;
using Gear;
using HarmonyLib;
using Il2CppSystem.Collections.Generic;
using LevelGeneration;
using Microsoft.CodeAnalysis;
using Player;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("PackUseMultiplier")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PackUseMultiplier")]
[assembly: AssemblyTitle("PackUseMultiplier")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace PackUseMultiplier
{
	public static class Configuration
	{
		private static readonly string DefaultMulti;

		private static readonly ConfigEntry<bool> _showPackPercent;

		private static readonly ConfigEntry<string> _ammoMultiStr;

		private static readonly ConfigEntry<string> _toolMultiStr;

		private static readonly ConfigEntry<string> _mediMultiStr;

		private static readonly ConfigEntry<string> _disinfectMultiStr;

		private static float[] _ammoMulti;

		private static float[] _toolMulti;

		private static float[] _mediMulti;

		private static float[] _disinfectMulti;

		private static readonly ConfigFile _configFile;

		public static bool ShowPackPercent => _showPackPercent.Value;

		public static float GetMulti(eResourceContainerSpawnType packType)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected I4, but got Unknown
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Invalid comparison between Unknown and I4
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			float[] arr;
			switch ((int)packType)
			{
			default:
				if ((int)packType == 9)
				{
					arr = _disinfectMulti;
					break;
				}
				throw new ArgumentException($"Got {packType}, but only multipliers for resource packs are valid!", "packType");
			case 1:
				arr = _ammoMulti;
				break;
			case 2:
				arr = _toolMulti;
				break;
			case 0:
				arr = _mediMulti;
				break;
			}
			return GetLastMulti(arr);
		}

		private static float GetLastMulti(float[] arr)
		{
			if (arr.Length == 0)
			{
				return 1f;
			}
			return arr[Math.Min(PlayerManager.PlayerAgentsInLevel.Count, arr.Length) - 1];
		}

		static Configuration()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			DefaultMulti = string.Join(", ", new float[3] { 2f, 1.5f, 1f });
			_configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "PackUseMultiplier.cfg"), true);
			string text = "Display Settings";
			string text2 = "Shows resource pack remaining as the percentage instead of number of uses.";
			_showPackPercent = _configFile.Bind<bool>(text, "Show Pack Percent", false, text2);
			text = "Compress Settings";
			text2 = "A list of multipliers on ammo pack usage for each player count (solo, duo, trio, quad, etc.). If the player count is higher than the list size, the last value will be used.";
			_ammoMultiStr = _configFile.Bind<string>(text, "Ammo Multi", DefaultMulti, text2);
			text2 = "A list of multipliers on tool pack usage for each player count (solo, duo, trio, quad, etc.). If the player count is higher than the list size, the last value will be used.";
			_toolMultiStr = _configFile.Bind<string>(text, "Tool Multi", DefaultMulti, text2);
			text2 = "A list of multipliers on medipack usage for each player count (solo, duo, trio, quad, etc.). If the player count is higher than the list size, the last value will be used.";
			_mediMultiStr = _configFile.Bind<string>(text, "Medi Multi", DefaultMulti, text2);
			text2 = "A list of multipliers on disinfect pack usage for each player count (solo, duo, trio, quad, etc.). Affects disinfect station speed. If the player count is higher than the list size, the last value will be used.";
			_disinfectMultiStr = _configFile.Bind<string>(text, "Disinfect Multi", DefaultMulti, text2);
			CacheValues();
		}

		internal static void Init()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			LiveEdit.CreateListener(Paths.ConfigPath, "PackUseMultiplier.cfg", false).FileChanged += new LiveEditEventHandler(OnFileReloaded);
		}

		private static void OnFileReloaded(LiveEditEventArgs _)
		{
			_configFile.Reload();
			CacheValues();
		}

		[MemberNotNull(new string[] { "_ammoMulti", "_toolMulti", "_mediMulti", "_disinfectMulti" })]
		private static void CacheValues()
		{
			_ammoMulti = ParseArrayStr(_ammoMultiStr.Value);
			_toolMulti = ParseArrayStr(_toolMultiStr.Value);
			_mediMulti = ParseArrayStr(_mediMultiStr.Value);
			_disinfectMulti = ParseArrayStr(_disinfectMultiStr.Value);
			if (PlayerBackpackManager.m_hasLocalPlayerBackpack)
			{
				PlayerAmmoStorage ammoStorage = PlayerBackpackManager.LocalBackpack.AmmoStorage;
				InventorySlotAmmo resourcePackAmmo = ammoStorage.ResourcePackAmmo;
				if ((ShowPackPercent ? 1f : 20f) != resourcePackAmmo.CostOfBullet)
				{
					resourcePackAmmo.Setup(20f, 0, -1f);
					ammoStorage.UpdateSlotAmmoUI(resourcePackAmmo, 0);
				}
			}
		}

		private static float[] ParseArrayStr(string str)
		{
			string[] array = str.Split(',');
			float[] array2 = new float[array.Length];
			int num = 0;
			string[] array3 = array;
			foreach (string text in array3)
			{
				if (float.TryParse(text.Trim(), out var result))
				{
					array2[num++] = result;
					continue;
				}
				DinoLogger.Error($"Unable to read {text.Trim()} in list {str} as number!");
			}
			if (num != array2.Length)
			{
				float[] array4 = new float[num];
				Array.Copy(array2, array4, num);
				return array4;
			}
			return array2;
		}
	}
	internal static class DinoLogger
	{
		private static ManualLogSource logger = Logger.CreateLogSource("PackUseMultiplier");

		public static void Log(string format, params object[] args)
		{
			Log(string.Format(format, args));
		}

		public static void Log(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)8, (object)str);
			}
		}

		public static void Warning(string format, params object[] args)
		{
			Warning(string.Format(format, args));
		}

		public static void Warning(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)4, (object)str);
			}
		}

		public static void Error(string format, params object[] args)
		{
			Error(string.Format(format, args));
		}

		public static void Error(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)2, (object)str);
			}
		}

		public static void Debug(string format, params object[] args)
		{
			Debug(string.Format(format, args));
		}

		public static void Debug(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)32, (object)str);
			}
		}
	}
	[HarmonyPatch]
	internal static class DisinfectPatches
	{
		private static LG_DisinfectionStation? _station;

		private static float _baseDisinfectSpeed;

		[HarmonyPatch(typeof(LG_DisinfectionStation), "AttemptInteract")]
		[HarmonyPostfix]
		private static void Post_StartDisinfect(LG_DisinfectionStation __instance, pDisinfectionStationInteraction interaction)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Invalid comparison between Unknown and I4
			eDisinfectionStationInteractionType type = interaction.type;
			if ((int)type != 2)
			{
				if ((int)type == 3 && (Object)(object)_station == (Object)(object)__instance)
				{
					__instance.m_disinfectSpeed = _baseDisinfectSpeed;
					_baseDisinfectSpeed = 0f;
					_station = null;
				}
				return;
			}
			if ((Object)(object)_station != (Object)null)
			{
				_station.m_disinfectSpeed = _baseDisinfectSpeed;
			}
			_station = __instance;
			_baseDisinfectSpeed = __instance.m_disinfectSpeed;
			__instance.m_disinfectSpeed *= Configuration.GetMulti((eResourceContainerSpawnType)9);
		}
	}
	[BepInPlugin("Dinorush.PackUseMultiplier", "PackUseMultiplier", "1.0.0")]
	internal sealed class EntryPoint : BasePlugin
	{
		public const string MODNAME = "PackUseMultiplier";

		public override void Load()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			Configuration.Init();
			new Harmony("PackUseMultiplier").PatchAll();
			((BasePlugin)this).Log.LogMessage((object)"Loaded PackUseMultiplier");
		}
	}
	[HarmonyPatch]
	internal static class StoragePatches
	{
		private const float MinPackAmmoRel = 0.01f;

		[HarmonyPatch(typeof(InventorySlotAmmo), "Setup")]
		[HarmonyPrefix]
		private static void Pre_Setup(InventorySlotAmmo __instance, ref float costOfBullet)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)__instance.AmmoType == 3 && Configuration.ShowPackPercent)
			{
				costOfBullet = 1f;
			}
		}

		[HarmonyPatch(typeof(ResourcePackFirstPerson), "ApplyPack")]
		[HarmonyPrefix]
		private static bool Pre_ApplyPack(ResourcePackFirstPerson __instance)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Invalid comparison between Unknown and I4
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Invalid comparison between Unknown and I4
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Invalid comparison between Unknown and I4
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			PlayerAgent owner = ((Item)__instance).Owner;
			owner.FPItemHolder.ItemHiddenTrigger = true;
			__instance.m_itemUpTimer = Clock.Time + 0.6f;
			PlayerAmmoStorage ammoStorage = PlayerBackpackManager.LocalBackpack.AmmoStorage;
			InventorySlotAmmo resourcePackAmmo = ammoStorage.ResourcePackAmmo;
			eResourceContainerSpawnType packType = __instance.m_packType;
			float num = Math.Min(20f * Configuration.GetMulti(packType), resourcePackAmmo.AmmoInPack);
			float num2 = num / resourcePackAmmo.AmmoMaxCap;
			float num3 = UpdateAmmo(ammoStorage, resourcePackAmmo, 0f - num) / resourcePackAmmo.AmmoMaxCap;
			if (num3 < 0.01f)
			{
				num2 += num3;
			}
			__instance.UpdateInteractionActionName("", true);
			iResourcePackReceiver actionReceiver = __instance.m_actionReceiver;
			if ((int)packType != 0)
			{
				if (packType - 1 > 1)
				{
					if ((int)packType == 9)
					{
						if (actionReceiver != null)
						{
							actionReceiver.GiveDisinfection(owner, num2);
							((ItemEquippable)__instance).Sound.Post(EVENTS.MEDPACK_APPLY, true);
						}
						GameEventManager.PostEvent((eGameEvent)9, owner, 0f, "", (Dictionary<string, string>)null);
					}
				}
				else
				{
					if (actionReceiver != null)
					{
						float num4 = AgentModifierManager.ApplyModifier((Agent)(object)owner, (AgentModifier)108, 1f);
						float num5;
						float num6;
						float num7;
						if ((int)packType == 2)
						{
							num5 = 0f;
							num6 = 0f;
							num7 = num2;
						}
						else
						{
							num5 = num2;
							num6 = num2;
							num7 = 0f;
						}
						actionReceiver.GiveAmmoRel(owner, num5 * num4, num6 * num4, num7 * num4);
						((ItemEquippable)__instance).Sound.Post(EVENTS.AMMOPACK_APPLY, true);
					}
					GameEventManager.PostEvent((eGameEvent)8, owner, 0f, "", (Dictionary<string, string>)null);
				}
			}
			else
			{
				if (actionReceiver != null)
				{
					actionReceiver.GiveHealth(owner, AgentModifierManager.ApplyModifier((Agent)(object)owner, (AgentModifier)3, num2));
					((ItemEquippable)__instance).Sound.Post(EVENTS.MEDPACK_APPLY, true);
				}
				GameEventManager.PostEvent((eGameEvent)6, owner, 0f, "", (Dictionary<string, string>)null);
			}
			PlayerBackpackManager.SendLocalAmmoData();
			__instance.m_lastActionReceiver = null;
			if (num3 < 0.01f)
			{
				PlayerBackpackManager.ClearLocalSlot(((Item)__instance).ItemDataBlock.inventorySlot, true, true);
				((ItemEquippable)__instance).Sound.Post(EVENTS.PACK_DISCARD, true);
			}
			return false;
		}

		private static float UpdateAmmo(PlayerAmmoStorage storage, InventorySlotAmmo slotAmmo, float amount)
		{
			float result = (slotAmmo.AmmoInPack = Math.Clamp(slotAmmo.AmmoInPack + amount, 0f, slotAmmo.AmmoMaxCap));
			slotAmmo.OnBulletsUpdateCallback?.Invoke(slotAmmo.BulletsInPack);
			storage.NeedsSync = true;
			storage.UpdateSlotAmmoUI(slotAmmo, 0);
			return result;
		}
	}
}