Decompiled source of DizzinessMod v1.0.1

plugins/DizzinessMod.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DizzinessMod
{
	[BepInPlugin("com.tambistudios.dizziness", "Dizziness From Speed", "1.0.1")]
	public class DizzinessPlugin : BaseUnityPlugin
	{
		public const string MOD_GUID = "com.tambistudios.dizziness";

		public const string MOD_NAME = "Dizziness From Speed";

		public const string MOD_VERSION = "1.0.1";

		internal const STATUSTYPE OUR_STATUS = 4;

		internal static DizzinessPlugin Instance;

		internal static ManualLogSource Log;

		internal static Sprite DizzinessIconSprite;

		internal static readonly Color DizzinessColor = new Color(0f, 0.808f, 0.82f, 1f);

		private const float MIN_SPEED_THRESHOLD = 15f;

		private const float DIZZINESS_RATE = 0.0036f;

		private const float RECOVERY_PER_SEC = 0.05f;

		private const float RECOVERY_DELAY = 1f;

		internal static bool isOurAddStatusCall = false;

		private float lastAddedTime = -999f;

		private Character trackedCharacter;

		private bool barAfflictionStyled;

		private ConfigEntry<bool> debugMode;

		private Rigidbody cachedRigidbody;

		private Character cachedCharacter;

		private void Awake()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"[Dizziness From Speed] Loaded! Version 1.0.1");
			debugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableDebugKeys", false, "Enable F5 (teleport up) and on-screen debug HUD");
			LoadIconSprite();
			new Harmony("com.tambistudios.dizziness").PatchAll();
			Log.LogInfo((object)"Harmony patches applied");
		}

		private void LoadIconSprite()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				string text = "DizzinessMod.Assets.dizziness_icon.png";
				using Stream stream = executingAssembly.GetManifestResourceStream(text);
				if (stream == null)
				{
					Log.LogWarning((object)("Embedded icon not found: " + text));
					return;
				}
				byte[] array = new byte[stream.Length];
				stream.Read(array, 0, array.Length);
				Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
				if (!ImageConversion.LoadImage(val, array))
				{
					Log.LogWarning((object)"Failed to parse icon image");
					return;
				}
				((Texture)val).filterMode = (FilterMode)1;
				((Texture)val).wrapMode = (TextureWrapMode)1;
				DizzinessIconSprite = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
				((Object)DizzinessIconSprite).name = "DizzinessIcon";
				Log.LogInfo((object)("Icon loaded: " + ((Texture)val).width + "x" + ((Texture)val).height));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Icon load error: " + ex.Message));
			}
		}

		private Rigidbody GetPlayerRigidbody()
		{
			Character localCharacter = Character.localCharacter;
			if ((Object)(object)localCharacter == (Object)null)
			{
				return null;
			}
			if ((Object)(object)cachedCharacter != (Object)(object)localCharacter)
			{
				cachedCharacter = localCharacter;
				cachedRigidbody = null;
			}
			if ((Object)(object)cachedRigidbody == (Object)null)
			{
				cachedRigidbody = ((Component)localCharacter).gameObject.GetComponentInChildren<Rigidbody>();
			}
			return cachedRigidbody;
		}

		private void TryStyleCrabBar()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Invalid comparison between Unknown and I4
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			if (barAfflictionStyled)
			{
				return;
			}
			StaminaBar val = Object.FindObjectOfType<StaminaBar>();
			if ((Object)(object)val == (Object)null || val.afflictions == null)
			{
				return;
			}
			BarAffliction[] afflictions = val.afflictions;
			foreach (BarAffliction val2 in afflictions)
			{
				if ((Object)(object)val2 == (Object)null || (int)val2.afflictionType != 4)
				{
					continue;
				}
				if ((Object)(object)val2.icon != (Object)null && (Object)(object)DizzinessIconSprite != (Object)null)
				{
					val2.icon.sprite = DizzinessIconSprite;
				}
				Image[] componentsInChildren = ((Component)val2).gameObject.GetComponentsInChildren<Image>(true);
				foreach (Image val3 in componentsInChildren)
				{
					if (!((Object)(object)val3 == (Object)null))
					{
						Color color = ((Graphic)val3).color;
						((Graphic)val3).color = new Color(DizzinessColor.r, DizzinessColor.g, DizzinessColor.b, color.a);
					}
				}
				Log.LogInfo((object)"BarAffliction[Crab] restyled");
				barAfflictionStyled = true;
				break;
			}
		}

		private void Update()
		{
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			if (debugMode.Value)
			{
				HandleDebugKeys();
			}
			Character localCharacter = Character.localCharacter;
			if ((Object)(object)localCharacter == (Object)null)
			{
				return;
			}
			if ((Object)(object)trackedCharacter != (Object)(object)localCharacter)
			{
				trackedCharacter = localCharacter;
				lastAddedTime = -999f;
				barAfflictionStyled = false;
			}
			TryStyleCrabBar();
			CharacterAfflictions val = localCharacter.refs?.afflictions;
			if ((Object)(object)localCharacter.data != (Object)null && (localCharacter.data.passedOut || localCharacter.data.fullyPassedOut))
			{
				if ((Object)(object)val != (Object)null && val.GetCurrentStatus((STATUSTYPE)4) > 0f)
				{
					SetOurStatus(val, 0f);
				}
			}
			else
			{
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				float etcDamageMultiplier = Ascents.etcDamageMultiplier;
				Rigidbody playerRigidbody = GetPlayerRigidbody();
				if ((Object)(object)playerRigidbody == (Object)null)
				{
					return;
				}
				Vector3 linearVelocity = playerRigidbody.linearVelocity;
				float magnitude = ((Vector3)(ref linearVelocity)).magnitude;
				float currentStatus = val.GetCurrentStatus((STATUSTYPE)4);
				if (etcDamageMultiplier > 0f && magnitude > 15f)
				{
					float num = (magnitude - 15f) * 0.0036f * etcDamageMultiplier * Time.deltaTime;
					isOurAddStatusCall = true;
					try
					{
						val.AddStatus((STATUSTYPE)4, num, false, false, false);
					}
					finally
					{
						isOurAddStatusCall = false;
					}
					lastAddedTime = Time.time;
				}
				else if (currentStatus > 0f && Time.time - lastAddedTime > 1f)
				{
					float num2 = 0.05f * Time.deltaTime;
					val.SubtractStatus((STATUSTYPE)4, num2, false, true);
				}
			}
		}

		private void SetOurStatus(CharacterAfflictions a, float value)
		{
			isOurAddStatusCall = true;
			try
			{
				a.SetStatus((STATUSTYPE)4, value, true);
			}
			finally
			{
				isOurAddStatusCall = false;
			}
		}

		private void HandleDebugKeys()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			Character localCharacter = Character.localCharacter;
			if (!((Object)(object)localCharacter == (Object)null) && Input.GetKeyDown((KeyCode)286))
			{
				Vector3 position = ((Component)localCharacter).gameObject.transform.position;
				((Component)localCharacter).gameObject.transform.position = position + Vector3.up * 100f;
				Log.LogInfo((object)"[F5] Teleported 100m up");
			}
		}

		private void OnGUI()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: 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_00c0: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			if (!debugMode.Value)
			{
				return;
			}
			Character localCharacter = Character.localCharacter;
			if (!((Object)(object)localCharacter == (Object)null))
			{
				Rigidbody playerRigidbody = GetPlayerRigidbody();
				CharacterAfflictions val = localCharacter.refs?.afflictions;
				if (!((Object)(object)playerRigidbody == (Object)null) && !((Object)(object)val == (Object)null))
				{
					Vector3 linearVelocity = playerRigidbody.linearVelocity;
					float magnitude = ((Vector3)(ref linearVelocity)).magnitude;
					float currentStatus = val.GetCurrentStatus((STATUSTYPE)4);
					float currentStatus2 = val.GetCurrentStatus((STATUSTYPE)5);
					float statusSum = val.statusSum;
					float etcDamageMultiplier = Ascents.etcDamageMultiplier;
					string text = ((etcDamageMultiplier <= 0f) ? "OFF" : ((etcDamageMultiplier < 0.75f) ? "Low" : ((!(etcDamageMultiplier < 1.25f)) ? "High" : "Normal")));
					GUIStyle val2 = new GUIStyle();
					val2.fontSize = 18;
					val2.normal.textColor = Color.white;
					GUI.Label(new Rect(20f, 20f, 600f, 30f), "Speed: " + magnitude.ToString("F1") + " m/s", val2);
					GUI.Label(new Rect(20f, 45f, 600f, 30f), "Dizziness (Crab): " + currentStatus.ToString("F2"), val2);
					GUI.Label(new Rect(20f, 70f, 600f, 30f), "Real Curse: " + currentStatus2.ToString("F2"), val2);
					GUI.Label(new Rect(20f, 95f, 600f, 30f), "StatusSum = " + statusSum.ToString("F2") + " / 0.99 = pass out", val2);
					GUI.Label(new Rect(20f, 120f, 600f, 30f), "Other damage: " + text + " (x" + etcDamageMultiplier.ToString("F2") + ")", val2);
					GUI.Label(new Rect(20f, 150f, 600f, 30f), "F5 — teleport up", val2);
				}
			}
		}
	}
	[HarmonyPatch(typeof(CharacterAfflictions), "AddStatus")]
	public class CharacterAfflictions_AddStatus_Patch
	{
		private static bool Prefix(STATUSTYPE statusType)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)statusType != 4)
			{
				return true;
			}
			if (DizzinessPlugin.isOurAddStatusCall)
			{
				return true;
			}
			return false;
		}
	}
}