Rage.dll

Decompiled 2 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using Il2Cpp;
using Il2CppHurricaneVR.Framework.Core.Player;
using Il2CppHurricaneVR.Framework.Core.ScriptableObjects;
using Il2CppHurricaneVR.Framework.Shared;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MelonLoader;
using Microsoft.CodeAnalysis;
using RageMod;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Core), "Rage", "1.5.0", "coope", null)]
[assembly: MelonGame("Kubunautilus", "BrutalistickVR")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("Rage")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Rage")]
[assembly: AssemblyTitle("Rage")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 RageMod
{
	public static class Mci
	{
		public const string Alias = "blackrage_snd";

		public static bool Opened;

		public static string LastError;

		[DllImport("winmm.dll", CharSet = CharSet.Ansi, EntryPoint = "mciSendStringA")]
		private static extern int mciSendString(string cmd, StringBuilder buf, int bufSize, IntPtr cb);

		[DllImport("winmm.dll", SetLastError = true)]
		private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

		private static int Send(string cmd)
		{
			StringBuilder stringBuilder = new StringBuilder(256);
			int num = mciSendString(cmd, stringBuilder, stringBuilder.Capacity, IntPtr.Zero);
			if (num != 0)
			{
				LastError = cmd.Trim() + " -> " + num;
			}
			return num;
		}

		public static void Open(string path, float volume)
		{
			if (!Opened)
			{
				Send("close blackrage_snd");
				SetGlobalVolume(volume);
				if (Send("open \"" + path + "\" type waveaudio alias blackrage_snd") != 0)
				{
					Opened = false;
					return;
				}
				Send("set blackrage_snd time format milliseconds");
				Opened = true;
			}
		}

		public static void SetGlobalVolume(float volume)
		{
			float num = volume;
			if (num < 0f)
			{
				num = 0f;
			}
			if (num > 1f)
			{
				num = 1f;
			}
			uint num2 = (uint)(num * 65535f);
			uint dwVolume = (num2 << 16) | num2;
			waveOutSetVolume(IntPtr.Zero, dwVolume);
		}

		public static void Play()
		{
			if (Opened)
			{
				Send("seek blackrage_snd to start");
				Send("play blackrage_snd");
			}
		}
	}
	public class Core : MelonMod
	{
		private const string RigName = "Hexa4Rig26.10.23";

		private const float RAGE_MULT = 15f;

		private const float TINT_R = 0.7f;

		private const float TINT_G = 0.02f;

		private const float TINT_B = 0.04f;

		private const float PULSE_MIN = 0.2f;

		private const float PULSE_MAX = 0.45f;

		private const float PULSE_SPEED = 3.141593f;

		private const float INTRO_TIME = 0.3f;

		private const float OVERLAY_DIST = 0.35f;

		private const float OVERLAY_SIZE = 1.4f;

		public static HVRHexaBodyInputs playerInputs;

		private HVRHexaBodyHands _leftHands;

		private HVRHexaBodyHands _rightHands;

		private bool _rageOn;

		private bool _wasB;

		private bool _saved;

		private bool _errLogged;

		private float _rageTime;

		private float _savedSpring;

		private float _savedTorqueSpring;

		private float _savedMaxTorque;

		private float _savedMaxForce;

		private GameObject _overlay;

		private MeshRenderer _overlayMr;

		private Shader _tintShader;

		private const string SoundFolder = "RageSounds";

		private const string SoundFile = "Rage.wav";

		private const float SoundVolume = 1f;

		public override void OnInitializeMelon()
		{
			((MelonBase)this).LoggerInstance.Msg("========================================");
			((MelonBase)this).LoggerInstance.Msg("RAGE v1.5.0");
			((MelonBase)this).LoggerInstance.Msg("Oculus/Index: knopka B | Vive Wand: klik tachpada ili Menu");
			((MelonBase)this).LoggerInstance.Msg("Nazhmi eshcho raz chtoby vyklyuchit");
			((MelonBase)this).LoggerInstance.Msg("========================================");
			InitSound();
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			playerInputs = null;
			_leftHands = null;
			_rightHands = null;
			_rageOn = false;
			_wasB = false;
			_saved = false;
			_rageTime = 0f;
			DestroyOverlay();
			_tintShader = Shader.Find("Sprites/Default");
			if (sceneName == "MainMenuScene" || sceneName == "IntroScene")
			{
				return;
			}
			try
			{
				GameObject val = GameObject.Find("Hexa4Rig26.10.23");
				if ((Object)(object)val == (Object)null)
				{
					val = FindRigByPrefix();
				}
				if ((Object)(object)val != (Object)null)
				{
					playerInputs = val.GetComponentInChildren<HVRHexaBodyInputs>();
					GameObject val2 = GameObject.Find("Physics LeftHand");
					GameObject val3 = GameObject.Find("Physics RightHand");
					if ((Object)(object)val2 != (Object)null)
					{
						_leftHands = val2.GetComponent<HVRHexaBodyHands>();
					}
					if ((Object)(object)val3 != (Object)null)
					{
						_rightHands = val3.GetComponent<HVRHexaBodyHands>();
					}
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Rage init error: " + ex.Message);
			}
			((MelonBase)this).LoggerInstance.Msg("Scene " + sceneName + " L=" + ((Object)(object)_leftHands != (Object)null) + " R=" + ((Object)(object)_rightHands != (Object)null) + " inputs=" + ((Object)(object)playerInputs != (Object)null) + " shader=" + ((Object)(object)_tintShader != (Object)null));
			LogControllerType();
		}

		private void LogControllerType()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: 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)
			try
			{
				if (!((Object)(object)playerInputs == (Object)null))
				{
					HVRController leftController = playerInputs.LeftController;
					HVRController rightController = playerInputs.RightController;
					((MelonBase)this).LoggerInstance.Msg("Controllers: L=" + (((Object)(object)leftController != (Object)null) ? ((object)leftController.ControllerType/*cast due to .constrained prefix*/).ToString() : "null") + " R=" + (((Object)(object)rightController != (Object)null) ? ((object)rightController.ControllerType/*cast due to .constrained prefix*/).ToString() : "null"));
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Controller type error: " + ex.Message);
			}
		}

		public override void OnUpdate()
		{
			if ((Object)(object)_tintShader == (Object)null)
			{
				_tintShader = Shader.Find("Sprites/Default");
			}
			try
			{
				ReadButtonAndToggle();
			}
			catch (Exception ex)
			{
				if (!_errLogged)
				{
					_errLogged = true;
					((MelonBase)this).LoggerInstance.Warning("Rage loop error: " + ex.Message);
				}
			}
			if (_rageOn)
			{
				_rageTime += Time.deltaTime;
				UpdateOverlayColor();
			}
		}

		private void ReadButtonAndToggle()
		{
			bool flag = ReadRageButton();
			if (flag && !_wasB)
			{
				ToggleRage();
			}
			_wasB = flag;
		}

		private bool ReadRageButton()
		{
			if ((Object)(object)playerInputs == (Object)null)
			{
				return false;
			}
			return IsRagePressed(playerInputs.LeftController) || IsRagePressed(playerInputs.RightController);
		}

		private bool IsRagePressed(HVRController c)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Invalid comparison between Unknown and I4
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: 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)
			if ((Object)(object)c == (Object)null)
			{
				return false;
			}
			try
			{
				if ((int)c.ControllerType == 3)
				{
					return c.TrackpadButtonState.Active || c.MenuButtonState.Active;
				}
				return c.SecondaryButtonState.Active;
			}
			catch (Exception)
			{
				return false;
			}
		}

		private void ToggleRage()
		{
			if (_rageOn)
			{
				DeactivateRage();
				return;
			}
			if ((Object)(object)_leftHands == (Object)null && (Object)(object)_rightHands == (Object)null)
			{
				((MelonBase)this).LoggerInstance.Warning("Rage: hands not found");
				return;
			}
			SetStrength(_leftHands, 15f);
			SetStrength(_rightHands, 15f);
			if (!_saved)
			{
				((MelonBase)this).LoggerInstance.Warning("Rage: strength not captured");
				return;
			}
			_rageOn = true;
			_rageTime = 0f;
			CreateOverlay();
			Mci.Play();
			if ((Object)(object)_overlay != (Object)null)
			{
				((MelonBase)this).LoggerInstance.Msg("Rage ON x" + 15f + " | spring " + _savedSpring + " -> " + _savedSpring * 15f + " | torqueSpring " + _savedTorqueSpring + " -> " + _savedTorqueSpring * 15f + " | maxTorque " + _savedMaxTorque + " -> " + _savedMaxTorque * 15f + " | maxForce " + _savedMaxForce + " -> " + _savedMaxForce * 15f);
			}
			else
			{
				((MelonBase)this).LoggerInstance.Warning("Rage ON but overlay failed (camera/shader null)");
			}
		}

		private void DeactivateRage()
		{
			_rageOn = false;
			SetStrength(_leftHands, 0f);
			SetStrength(_rightHands, 0f);
			FadeOutOverlay();
			((MelonBase)this).LoggerInstance.Msg("Rage OFF");
		}

		private void InitSound()
		{
			try
			{
				string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
				if (string.IsNullOrEmpty(directoryName))
				{
					((MelonBase)this).LoggerInstance.Warning("Sound: cannot resolve mod folder");
					return;
				}
				string path = Path.Combine(directoryName, "RageSounds");
				string text = Path.Combine(path, "Rage.wav");
				if (!File.Exists(text))
				{
					((MelonBase)this).LoggerInstance.Warning("Sound not found: " + text);
					return;
				}
				Mci.Open(text, 1f);
				if (Mci.Opened)
				{
					((MelonBase)this).LoggerInstance.Msg("MCI sound ready: " + text);
				}
				else
				{
					((MelonBase)this).LoggerInstance.Warning("MCI open failed: " + Mci.LastError);
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Sound init error: " + ex.Message);
			}
		}

		private void SetStrength(HVRHexaBodyHands hands, float mult)
		{
			if ((Object)(object)hands == (Object)null)
			{
				return;
			}
			try
			{
				HVRHandStrengthHandler strengthHandler = hands.StrengthHandler;
				if ((Object)(object)strengthHandler == (Object)null)
				{
					return;
				}
				PDStrength currentStrength = strengthHandler.CurrentStrength;
				if (!((Object)(object)currentStrength == (Object)null))
				{
					if (!_saved)
					{
						_savedSpring = currentStrength.Spring;
						_savedTorqueSpring = currentStrength.TorqueSpring;
						_savedMaxTorque = currentStrength.MaxTorque;
						_savedMaxForce = currentStrength.MaxForce;
						_saved = true;
					}
					if (mult > 0f)
					{
						currentStrength.Spring = _savedSpring * mult;
						currentStrength.TorqueSpring = _savedTorqueSpring * mult;
						currentStrength.MaxTorque = _savedMaxTorque * mult;
						currentStrength.MaxForce = _savedMaxForce * mult;
					}
					else
					{
						currentStrength.Spring = _savedSpring;
						currentStrength.TorqueSpring = _savedTorqueSpring;
						currentStrength.MaxTorque = _savedMaxTorque;
						currentStrength.MaxForce = _savedMaxForce;
					}
					strengthHandler.UpdateStrength(currentStrength);
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Rage strength error: " + ex.Message);
			}
		}

		private void CreateOverlay()
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: 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)
			if (!((Object)(object)_overlay != (Object)null))
			{
				Camera main = Camera.main;
				if (!((Object)(object)main == (Object)null) && !((Object)(object)_tintShader == (Object)null))
				{
					_overlay = GameObject.CreatePrimitive((PrimitiveType)5);
					Object.Destroy((Object)(object)_overlay.GetComponent<Collider>());
					((Object)_overlay).name = "RageTint";
					_overlayMr = _overlay.GetComponent<MeshRenderer>();
					Material val = new Material(_tintShader);
					val.color = new Color(0.7f, 0.02f, 0.04f, 0f);
					((Renderer)_overlayMr).material = val;
					((Renderer)_overlayMr).shadowCastingMode = (ShadowCastingMode)0;
					((Renderer)_overlayMr).receiveShadows = false;
					_overlay.transform.SetParent(((Component)main).transform, false);
					_overlay.transform.localPosition = new Vector3(0f, 0f, 0.35f);
					_overlay.transform.localRotation = Quaternion.identity;
					_overlay.transform.localScale = new Vector3(1.4f, 1.4f, 1f);
				}
			}
		}

		private void UpdateOverlayColor()
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_overlayMr == (Object)null) && !((Object)(object)((Renderer)_overlayMr).material == (Object)null))
			{
				float num = Mathf.Pow(Mathf.Max(0f, Mathf.Sin(_rageTime * 3.141593f)), 2f);
				float num2 = 0.2f + 0.24999999f * num;
				float num3 = Mathf.Clamp01(_rageTime / 0.3f);
				float a = num2 + (1f - num3) * 0.2f;
				Color color = ((Renderer)_overlayMr).material.color;
				color.r = 0.7f;
				color.g = 0.02f;
				color.b = 0.04f;
				color.a = a;
				((Renderer)_overlayMr).material.color = color;
			}
		}

		private void FadeOutOverlay()
		{
			if (!((Object)(object)_overlay == (Object)null))
			{
				MelonCoroutines.Start(FadeRoutine());
			}
		}

		private IEnumerator FadeRoutine()
		{
			GameObject go = _overlay;
			MeshRenderer mr = _overlayMr;
			_overlay = null;
			_overlayMr = null;
			float t = 0f;
			while (t < 0.25f)
			{
				t += Time.deltaTime;
				if ((Object)(object)mr != (Object)null && (Object)(object)((Renderer)mr).material != (Object)null)
				{
					Color c = ((Renderer)mr).material.color;
					c.a = Mathf.Max(0f, c.a - Time.deltaTime * 1.6f);
					((Renderer)mr).material.color = c;
				}
				yield return null;
			}
			if ((Object)(object)go != (Object)null)
			{
				Object.Destroy((Object)(object)go);
			}
		}

		private void DestroyOverlay()
		{
			if ((Object)(object)_overlay != (Object)null)
			{
				Object.Destroy((Object)(object)_overlay);
			}
			_overlay = null;
			_overlayMr = null;
		}

		private GameObject FindRigByPrefix()
		{
			Il2CppArrayBase<Transform> val = Object.FindObjectsOfType<Transform>(false);
			for (int i = 0; i < val.Length; i++)
			{
				Transform val2 = val[i];
				if (!((Object)(object)val2 == (Object)null) && ((Object)val2).name.StartsWith("Hexa4Rig", StringComparison.OrdinalIgnoreCase))
				{
					return ((Component)val2).gameObject;
				}
			}
			return null;
		}
	}
}