Decompiled source of MagPerception v1.2.2

Mods/MagPerception.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Data;
using Il2CppTMPro;
using MelonLoader;
using NEP.MagPerception;
using NEP.MagPerception.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTrademark(null)]
[assembly: ComVisible(false)]
[assembly: MelonInfo(typeof(Main), "MagPerception", "1.2.2", null, null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace NEP.MagPerception
{
	public static class Hooks
	{
		[HarmonyPatch(typeof(Magazine), "OnGrab")]
		public static class OnMagAttached
		{
			public static void Postfix(Hand hand, Magazine __instance)
			{
				MagPerceptionManager.instance.OnMagazineAttached(__instance);
			}
		}

		[HarmonyPatch(typeof(Gun), "OnTriggerGripAttached")]
		public static class OnGunAttached
		{
			public static void Postfix(Gun __instance)
			{
				MagPerceptionManager.instance.OnGunAttached(__instance);
			}
		}

		[HarmonyPatch(typeof(Gun), "OnTriggerGripDetached")]
		public static class OnGunDetached
		{
			public static void Postfix(Gun __instance)
			{
				if (!((Object)(object)__instance == (Object)null))
				{
					MagPerceptionManager.instance.OnGunDetached(__instance);
				}
			}
		}

		[HarmonyPatch(typeof(Gun), "EjectCartridge")]
		public static class OnGunEjectRound
		{
			public static void Postfix()
			{
				MagPerceptionManager.instance.OnGunEjectRound();
			}
		}

		[HarmonyPatch(typeof(Gun), "OnMagazineInserted")]
		public static class OnMagazineInserted
		{
			public static void Postfix(Gun __instance)
			{
				MagPerceptionManager.instance.OnMagazineInserted(__instance.MagazineState, __instance);
			}
		}

		[HarmonyPatch(typeof(Gun), "OnMagazineRemoved")]
		public static class OnMagazineRemoved
		{
			public static void Postfix(Gun __instance)
			{
				MagPerceptionManager.instance.OnMagazineInserted(__instance.MagazineState, __instance);
			}
		}
	}
	[RegisterTypeInIl2Cpp]
	public class MagPerceptionManager : MonoBehaviour
	{
		public static MagPerceptionManager instance;

		public Transform lastGrabbedTransform;

		public Gun lastGun;

		public Magazine lastMag;

		public Hand lastHand;

		public MagazineUI magazineUI { get; private set; }

		public MagazineData grabbedMagazineData { get; private set; }

		public Hand playerLeftHand => Player.LeftHand;

		public Hand playerRightHand => Player.RightHand;

		public MagPerceptionManager(IntPtr ptr)
			: base(ptr)
		{
		}

		private void Awake()
		{
			instance = this;
		}

		private void Start()
		{
			GameObject val = Object.Instantiate<GameObject>(((Il2CppObjectBase)Main.resources.LoadAsset("MagazineLayer")).Cast<GameObject>(), ((Component)this).transform);
			val.transform.SetParent(((Component)this).transform);
			magazineUI = val.AddComponent<MagazineUI>();
			magazineUI.ammoCounterText = ((Component)val.transform.Find("AmmoCounter")).GetComponent<TextMeshProUGUI>();
			magazineUI.ammoInventoryText = ((Component)val.transform.Find("AmmoInventory")).GetComponent<TextMeshProUGUI>();
			magazineUI.ammoTypeText = ((Component)val.transform.Find("AmmoType")).GetComponent<TextMeshProUGUI>();
			magazineUI.animator = val.GetComponent<Animator>();
			val.SetActive(false);
		}

		public void OnMagazineAttached(Magazine magazine)
		{
			lastMag = magazine;
			magazineUI.Show();
			magazineUI.OnMagEvent();
			magazineUI.UpdateParent(lastMag.insertPointTransform);
			magazineUI.DisplayMagInfo(magazine.magazineState);
		}

		public void OnMagazineInserted(MagazineState magazineState, Gun gun)
		{
			lastGun = gun;
			magazineUI.Show();
			magazineUI.OnMagEvent();
			magazineUI.UpdateParent(lastGun.firePointTransform);
			magazineUI.DisplayGunInfo(lastGun);
		}

		public void OnMagazineEjected(MagazineState magazineState, Gun gun)
		{
			magazineUI.Show();
			magazineUI.OnMagEvent();
			magazineUI.UpdateParent(lastGun.firePointTransform);
			magazineUI.DisplayGunInfo(lastGun);
		}

		public void OnGunAttached(Gun gun)
		{
			if (Settings.ShowWithGun)
			{
				lastGun = gun;
				magazineUI.OnMagEvent();
				magazineUI.UpdateParent(gun.firePointTransform);
				magazineUI.Show();
			}
		}

		public void OnGunDetached(Gun gun)
		{
			if (Settings.ShowWithGun)
			{
				lastGun = null;
				magazineUI.OnMagEvent();
				if ((Object)(object)lastMag != (Object)null)
				{
					magazineUI.UpdateParent(gun.firePointTransform);
					magazineUI.DisplayMagInfo(lastMag.magazineState);
				}
			}
		}

		public void OnGunEjectRound()
		{
			if (Settings.ShowWithGun)
			{
				magazineUI.OnMagEvent();
				magazineUI.DisplayGunInfo(lastGun);
			}
		}
	}
	public static class BuildInfo
	{
		public const string Name = "MagPerception";

		public const string Author = null;

		public const string Company = null;

		public const string Version = "1.2.2";

		public const string DownloadLink = null;
	}
	public class Main : MelonMod
	{
		public static AssetBundle resources { get; private set; }

		public override void OnInitializeMelon()
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string text = "NEP.MagPerception.Resources.";
			string text2 = (HelperMethods.IsAndroid() ? "mp_resources_quest.pack" : "mp_resources_pcvr.pack");
			resources = HelperMethods.LoadEmbeddedAssetBundle(executingAssembly, text + text2);
			if ((Object)(object)resources == (Object)null)
			{
				throw new Exception("Resources file is missing/invalid!");
			}
			SetupBonemenu();
			Hooking.OnLevelLoaded += OnSceneWasLoaded;
		}

		private void SetupBonemenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			Page obj = Page.Root.CreatePage("Not Enough Photons", Color.white, 0, true).CreatePage("MagPerception", Color.white, 0, true);
			Page val = obj.CreatePage("Offset", Color.white, 0, true);
			obj.CreateFloat("Scale", Color.white, 0.75f, 0.25f, 0.25f, 1.5f, (Action<float>)delegate(float value)
			{
				Settings.InfoScale = value;
			});
			obj.CreateEnum("Show Type", Color.white, (Enum)UIShowType.FadeShow, (Action<Enum>)delegate(Enum showType)
			{
				Settings.ShowType = (UIShowType)(object)showType;
			});
			obj.CreateFloat("Time Until Hidden", Color.white, 3f, 0.5f, 0f, 10f, (Action<float>)delegate(float value)
			{
				Settings.TimeUntilHidden = value;
			});
			obj.CreateBool("Show With Gun", Color.white, false, (Action<bool>)delegate(bool value)
			{
				Settings.ShowWithGun = value;
			});
			val.CreateFloat("X", Color.red, 0.075f, 0.025f, -1f, 1f, (Action<float>)delegate(float value)
			{
				Settings.Offset.x = value;
			});
			val.CreateFloat("Y", Color.green, 0f, 0.025f, -1f, 1f, (Action<float>)delegate(float value)
			{
				Settings.Offset.y = value;
			});
			val.CreateFloat("Z", Color.blue, 0f, 0.025f, -1f, 1f, (Action<float>)delegate(float value)
			{
				Settings.Offset.z = value;
			});
		}

		public void OnSceneWasLoaded(LevelInfo info)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new GameObject("Mag Perception Manager").AddComponent<MagPerceptionManager>();
		}

		public static Object GetObjectFromResources(string name)
		{
			return ((IEnumerable<Object>)Il2CppArrayBase<Object>.op_Implicit((Il2CppArrayBase<Object>)(object)resources.LoadAllAssets())).FirstOrDefault((Func<Object, bool>)((Object asset) => asset.name == name));
		}
	}
	public static class Settings
	{
		public static float InfoScale = 0.75f;

		public static Vector3 Offset = new Vector3(0.075f, 0f, 0f);

		public static float TimeUntilHidden = 1.5f;

		public static UIShowType ShowType = UIShowType.FadeShow;

		public static bool ShowWithGun = false;
	}
}
namespace NEP.MagPerception.UI
{
	[RegisterTypeInIl2Cpp]
	public class MagazineUI : MonoBehaviour
	{
		public TextMeshProUGUI ammoCounterText;

		public TextMeshProUGUI ammoInventoryText;

		public TextMeshProUGUI ammoTypeText;

		public Animator animator;

		private float timeSinceLastEvent;

		private bool fadeOut;

		private float fadeOutTime;

		private float fadeOutDuration = 0.25f;

		private Vector3 targetPosition;

		private Quaternion lastRotation;

		public bool isBeingInteracted { get; set; }

		public MagazineUI(IntPtr ptr)
			: base(ptr)
		{
		}

		private void Awake()
		{
			animator = ((Component)this).GetComponent<Animator>();
		}

		private void FixedUpdate()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			((Component)this).transform.localScale = new Vector3(-1f, 1f, 1f) * Settings.InfoScale;
			((Component)this).transform.LookAt(Player.Head);
			((Component)this).transform.localPosition = Vector3.Lerp(((Component)this).transform.localPosition, targetPosition + Settings.Offset, 8f * Time.fixedDeltaTime);
			((Component)this).transform.rotation = Quaternion.Slerp(lastRotation, ((Component)this).transform.rotation, 8f * Time.fixedDeltaTime);
			switch (Settings.ShowType)
			{
			case UIShowType.Always:
				Show();
				break;
			case UIShowType.FadeShow:
				timeSinceLastEvent += Time.deltaTime;
				if (timeSinceLastEvent > Settings.TimeUntilHidden)
				{
					timeSinceLastEvent = 0f;
					FadeOut();
				}
				break;
			case UIShowType.Hide:
				Hide();
				break;
			}
		}

		public void Show()
		{
			((Component)this).gameObject.SetActive(true);
		}

		public void Hide()
		{
			((Component)this).gameObject.SetActive(false);
		}

		public void OnMagEvent()
		{
			switch (Settings.ShowType)
			{
			case UIShowType.FadeShow:
				FadeIn();
				break;
			case UIShowType.Hide:
				Hide();
				break;
			}
		}

		public void DisplayGunInfo(Gun gun)
		{
			if (!((Object)(object)gun == (Object)null))
			{
				string text = "";
				MagazineState magazineState = gun.MagazineState;
				if (magazineState == null)
				{
					text = (((Object)(object)gun.chamberedCartridge != (Object)null) ? "+1/0" : "0/0");
					((TMP_Text)ammoCounterText).text = text;
					((TMP_Text)ammoInventoryText).text = "RESERVE: None";
					((TMP_Text)ammoTypeText).text = "Unknown";
					return;
				}
				bool num = (Object)(object)gun.chamberedCartridge != (Object)null && magazineState.AmmoCount == magazineState.magazineData.rounds;
				int ammoCount = magazineState.AmmoCount;
				int rounds = magazineState.magazineData.rounds;
				string platform = magazineState.magazineData.platform;
				int cartridgeCount = AmmoInventory.Instance.GetCartridgeCount(magazineState.cartridgeData);
				text = ((!num) ? $"{ammoCount}/{rounds}" : $"{ammoCount}+1/{rounds}");
				((TMP_Text)ammoCounterText).text = text;
				((TMP_Text)ammoInventoryText).text = "RESERVE: " + cartridgeCount;
				((TMP_Text)ammoTypeText).text = platform;
			}
		}

		public void DisplayMagInfo(MagazineState magazineState)
		{
			if (magazineState != null)
			{
				int ammoCount = magazineState.AmmoCount;
				int rounds = magazineState.magazineData.rounds;
				string platform = magazineState.magazineData.platform;
				int cartridgeCount = AmmoInventory.Instance.GetCartridgeCount(magazineState.cartridgeData);
				((TMP_Text)ammoCounterText).text = $"{ammoCount}/{rounds}";
				((TMP_Text)ammoInventoryText).text = "RESERVE: " + cartridgeCount;
				((TMP_Text)ammoTypeText).text = platform;
			}
		}

		public void UpdateParent(Transform attachment)
		{
			((Component)this).transform.parent = attachment;
		}

		private void FadeIn()
		{
			timeSinceLastEvent = 0f;
			if (fadeOut)
			{
				((Component)this).gameObject.SetActive(true);
				Animator obj = animator;
				if (obj != null)
				{
					obj.Play("mag_enter_01");
				}
				fadeOut = false;
			}
		}

		private void FadeOut()
		{
			if (!fadeOut)
			{
				Animator obj = animator;
				if (obj != null)
				{
					obj.SetTrigger("exit");
				}
				fadeOut = true;
			}
			else
			{
				fadeOutTime += Time.deltaTime;
				if (fadeOutTime > fadeOutDuration)
				{
					fadeOutTime = 0f;
					((Component)this).gameObject.SetActive(false);
				}
			}
		}
	}
	[Obsolete("Hand Type is now obsolete since it wouldn't make sense for it to stay on one hand.")]
	public enum UIHandType
	{
		Left,
		Right
	}
	public enum UIShowType
	{
		Always,
		Hide,
		FadeShow
	}
}