Decompiled source of JetpackWarning v2.1.1

JetpackWarning.dll

Decompiled 11 months ago
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("klepticat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("A Lethal Company Mod that adds a visual and audio indicator for when your jetpack is about to explode.")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
[assembly: AssemblyProduct("JetpackWarning")]
[assembly: AssemblyTitle("JetpackWarning")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.0.0")]
[module: UnverifiableCode]
namespace JetpackWarning;

public static class Assets
{
	public static string mainAssetBundleName = "jetpackAssets";

	public static AssetBundle MainAssetBundle = null;

	private static string GetAssemblyName()
	{
		return Assembly.GetExecutingAssembly().FullName.Split(',')[0];
	}

	public static void PopulateAssets()
	{
		if ((Object)(object)MainAssetBundle == (Object)null)
		{
			using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetAssemblyName() + "." + mainAssetBundleName))
			{
				MainAssetBundle = AssetBundle.LoadFromStream(stream);
			}
		}
	}
}
[BepInPlugin("JetpackWarning", "JetpackWarning", "2.0.0")]
public class JetpackWarningPlugin : BaseUnityPlugin
{
	public static Harmony _harmony;

	public static AudioClip jetpackCriticalBeep;

	public static GameObject meterContainer;

	public static GameObject meter;

	public static GameObject frame;

	public static GameObject warning;

	private void Awake()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin JetpackWarning is loaded!");
		Assets.PopulateAssets();
		_harmony = new Harmony("JetpackWarning");
		_harmony.PatchAll(typeof(Patches));
		SceneManager.sceneLoaded += OnSceneRelayLoaded;
		jetpackCriticalBeep = Assets.MainAssetBundle.LoadAsset<AudioClip>("JetpackCriticalBeep");
	}

	private void OnSceneRelayLoaded(Scene scene, LoadSceneMode loadMode)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: 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)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		if (((Scene)(ref scene)).name == "SampleSceneRelay")
		{
			GameObject val = GameObject.Find("IngamePlayerHUD");
			meterContainer = new GameObject("jetpackMeterContainer");
			meterContainer.AddComponent<CanvasGroup>();
			RectTransform obj = meterContainer.AddComponent<RectTransform>();
			((Transform)obj).parent = val.transform;
			((Transform)obj).localScale = Vector3.one;
			obj.anchoredPosition = Vector2.zero;
			((Transform)obj).localPosition = Vector2.op_Implicit(new Vector2(50f, 0f));
			obj.sizeDelta = Vector2.one;
			meter = AddImageToHUD("jetpackMeter", scene);
			frame = AddImageToHUD("jetpackMeterFrame", scene);
			warning = AddImageToHUD("jetpackMeterWarning", scene);
			GameObject[] array = (GameObject[])(object)new GameObject[3] { meter, frame, warning };
			foreach (GameObject obj2 in array)
			{
				obj2.transform.parent = meterContainer.transform;
				obj2.transform.localPosition = Vector2.op_Implicit(Vector2.zero);
			}
			meter.GetComponent<Image>().type = (Type)3;
			meter.GetComponent<Image>().fillMethod = (FillMethod)1;
			Transform transform = warning.transform;
			transform.localPosition += new Vector3(30f, 0f);
		}
	}

	private GameObject AddImageToHUD(string imageName, Scene scene)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Expected O, but got Unknown
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: 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_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: 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_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Expected O, but got Unknown
		Sprite val = Assets.MainAssetBundle.LoadAsset<Sprite>(imageName);
		GameObject val2 = new GameObject(imageName);
		SceneManager.MoveGameObjectToScene(val2, scene);
		GameObject val3 = GameObject.Find("IngamePlayerHUD");
		RectTransform obj = val2.AddComponent<RectTransform>();
		((Transform)obj).parent = val3.transform;
		((Transform)obj).localScale = Vector2.op_Implicit(Vector2.one);
		obj.anchoredPosition = Vector2.zero;
		((Transform)obj).localPosition = Vector2.op_Implicit(Vector2.zero);
		Rect rect = val.rect;
		float num = ((Rect)(ref rect)).width / 2f;
		rect = val.rect;
		obj.sizeDelta = new Vector2(num, ((Rect)(ref rect)).height / 2f);
		val2.AddComponent<Image>().sprite = val;
		val2.AddComponent<CanvasRenderer>();
		return val2;
	}
}
internal class Patches
{
	private static bool playJetpackCritical = false;

	private static bool playingJetpackCritical = false;

	private static float currentFill = 0f;

	private static float fillVelocity = 0f;

	private static float criticalFill = 0.75f;

	private static float fillTime = 0.1f;

	[HarmonyPatch(typeof(PlayerControllerB), "LateUpdate")]
	[HarmonyPostfix]
	private static void PlayerControllerB_LateUpdate_Postfix(ref PlayerControllerB __instance)
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		//IL_029e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02af: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
		if (!((NetworkBehaviour)__instance).IsOwner || (((NetworkBehaviour)__instance).IsServer && !__instance.isHostPlayerObject) || !__instance.isPlayerControlled || __instance.isPlayerDead)
		{
			return;
		}
		if (__instance.isHoldingObject && __instance.currentlyHeldObjectServer is JetpackItem)
		{
			JetpackItem val = (JetpackItem)__instance.currentlyHeldObjectServer;
			JetpackWarningPlugin.meterContainer.SetActive(true);
			Vector3 val2 = (Vector3)typeof(JetpackItem).GetField("forces", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(val);
			float num = (float)typeof(JetpackItem).GetField("jetpackPower", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(val);
			RaycastHit val3 = default(RaycastHit);
			Physics.Raycast(((Component)__instance).transform.position, val2, ref val3, 25f, StartOfRound.Instance.allPlayersCollideWithMask);
			float num2 = ((num > ((Vector3)(ref val2)).magnitude) ? num : ((Vector3)(ref val2)).magnitude);
			float num3 = ((!(num2 < 80f)) ? (0.5f - Mathf.Clamp(num2 / 20f - 4f, 0f, 1f) / 2f) : (Mathf.Clamp(num2 / 25f, 0f, 1f) / 2f));
			float num4 = ((((Vector3)(ref val2)).magnitude - ((RaycastHit)(ref val3)).distance >= 0f) ? ((((Vector3)(ref val2)).magnitude - ((RaycastHit)(ref val3)).distance) / 50f) : 0f);
			float num5 = Mathf.Lerp((((Vector3)(ref val2)).magnitude / 2f + num2 / 2.2f - ((RaycastHit)(ref val3)).distance >= 0f) ? ((((Vector3)(ref val2)).magnitude / 2f + num2 / 2.2f - ((RaycastHit)(ref val3)).distance) / 50f) : 0f, num4, num3);
			Debug.Log((object)$"Force Magnitude: {((Vector3)(ref val2)).magnitude}, Jetpack Power: {num}, Interpolation: {num3}, Fill: {num5}");
			JetpackWarningPlugin.meter.GetComponent<Image>().fillAmount = currentFill;
			JetpackWarningPlugin.warning.SetActive(currentFill > criticalFill);
			currentFill = Mathf.SmoothDamp(currentFill, num5, ref fillVelocity, fillTime);
			playJetpackCritical = currentFill > criticalFill;
			Color color = Color.Lerp(new Color(1f, 0.82f, 0.405f, 1f), new Color(0.769f, 0.243f, 0.243f, 1f), currentFill);
			((Graphic)JetpackWarningPlugin.meter.GetComponent<Image>()).color = color;
			((Graphic)JetpackWarningPlugin.frame.GetComponent<Image>()).color = color;
			((Graphic)JetpackWarningPlugin.warning.GetComponent<Image>()).color = color;
			if (playJetpackCritical)
			{
				if (!playingJetpackCritical)
				{
					playingJetpackCritical = true;
					val.jetpackBeepsAudio.clip = JetpackWarningPlugin.jetpackCriticalBeep;
					val.jetpackBeepsAudio.Play();
				}
			}
			else
			{
				playingJetpackCritical = false;
			}
		}
		else
		{
			JetpackWarningPlugin.meterContainer.SetActive(false);
		}
	}

	[HarmonyPatch(typeof(JetpackItem), "SetJetpackAudios")]
	[HarmonyPrefix]
	private static bool JetpackItem_SetJetpackAudios_Prefix(ref bool ___jetpackActivated, ref AudioSource ___jetpackBeepsAudio)
	{
		return !playingJetpackCritical;
	}

	[HarmonyPatch(typeof(JetpackItem), "JetpackEffect")]
	[HarmonyPostfix]
	private static void JetpackItem_JetpackEffect_Postfix(ref bool __0, JetpackItem __instance)
	{
		if (__0 && playJetpackCritical)
		{
			playingJetpackCritical = true;
			__instance.jetpackBeepsAudio.clip = JetpackWarningPlugin.jetpackCriticalBeep;
			__instance.jetpackBeepsAudio.Play();
		}
	}
}
public static class PluginInfo
{
	public const string PLUGIN_GUID = "JetpackWarning";

	public const string PLUGIN_NAME = "JetpackWarning";

	public const string PLUGIN_VERSION = "2.0.0";
}