Decompiled source of BoomBoxOverhaul v1.0.1

BoomBoxOverhaul.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BoomBoxOverhaul")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BoomBoxOverhaul")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("fcfd438e-dcd9-4eef-9cfc-78057bd6157b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BoomboxOverhaul
{
	[BepInPlugin("Henrehs.BoomBoxOverhaul", "BoomBoxOverhaul", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static class PluginInfo
		{
			public const string PLUGIN_GUID = "Henrehs.BoomBoxOverhaul";

			public const string PLUGIN_NAME = "BooomBoxOverhaul";

			public const string PLUGIN_VERSION = "1.0.0";
		}

		public static Plugin instance;

		private Harmony _harmony;

		public static Key volumeUpKey = (Key)14;

		public static Key volumeDownKey = (Key)13;

		public static string volumeHoverTip = "Volume up:   [+]\nVolume down: [-]";

		public static float volumeIncrement = 0.1f;

		public static float maxDisplayedVolume = 1.5f;

		public static float defaultVolume = 1f;

		private void Awake()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			_harmony = new Harmony("BoomBoxOverhaul");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BoomBox Upgraded!");
			instance = this;
		}

		public static void Log(string message)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)message);
		}
	}
}
namespace Plugin.Patches
{
	[HarmonyPatch(typeof(BoomboxItem))]
	public class Plugin
	{
		[HarmonyPatch]
		internal class ChangeVolumePatcher
		{
			private static AudioPlayerItemType boomboxItems = new AudioPlayerItemType("Boomboxes", "Grab boombox: [E]");

			private static AudioPlayerPlaceableType recordPlayers = new AudioPlayerPlaceableType("Record players", "Record player: [E]");

			private static AudioPlayerPlaceableType televisions = new AudioPlayerPlaceableType("Televisions", "Television: [E]");

			[HarmonyPatch(typeof(PlayerControllerB), "Update")]
			[HarmonyPrefix]
			public static void GetObjectLookingAt(PlayerControllerB __instance)
			{
				//IL_0094: Unknown result type (might be due to invalid IL or missing references)
				//IL_009a: Expected O, but got Unknown
				//IL_009b: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a6: Expected O, but got Unknown
				//IL_011f: Unknown result type (might be due to invalid IL or missing references)
				//IL_012a: Expected O, but got Unknown
				//IL_0164: Unknown result type (might be due to invalid IL or missing references)
				//IL_016f: Expected O, but got Unknown
				boomboxItems.lookingAtDeviceType = false;
				recordPlayers.lookingAtDeviceType = false;
				televisions.lookingAtDeviceType = false;
				if (!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || __instance.inTerminalMenu || __instance.isTypingChat || __instance.isPlayerDead)
				{
					return;
				}
				InteractTrigger hoveringOverTrigger = __instance.hoveringOverTrigger;
				object obj;
				if ((Object)(object)hoveringOverTrigger == (Object)null)
				{
					obj = null;
				}
				else
				{
					Transform parent = ((Component)hoveringOverTrigger).transform.parent;
					obj = (((Object)(object)parent != (Object)null) ? ((Component)parent).gameObject : null);
				}
				GameObject val = (GameObject)obj;
				if ((Object)val != (Object)null)
				{
					if (((Object)val).name.Contains("RecordPlayer"))
					{
						recordPlayers.lookingAtDeviceType = true;
					}
					else if (((Object)val).name.Contains("Television"))
					{
						televisions.lookingAtDeviceType = true;
					}
				}
				if (!recordPlayers.lookingAtDeviceType && !televisions.lookingAtDeviceType)
				{
					if ((Object)__instance.cursorTip != (Object)null && ((TMP_Text)__instance.cursorTip).text.Contains(boomboxItems.originalHoverTip))
					{
						boomboxItems.lookingAtDeviceType = true;
					}
					else if ((Object)__instance.currentlyHeldObjectServer != (Object)null && __instance.currentlyHeldObjectServer is BoomboxItem)
					{
						boomboxItems.lookingAtDeviceType = true;
					}
				}
			}

			[HarmonyPatch(typeof(PlayerControllerB), "Update")]
			[HarmonyPostfix]
			public static void GetVolumeInput(PlayerControllerB __instance)
			{
				if (!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || __instance.inTerminalMenu || __instance.isTypingChat || __instance.isPlayerDead || (!boomboxItems.lookingAtDeviceType && !recordPlayers.lookingAtDeviceType && !televisions.lookingAtDeviceType))
				{
					return;
				}
				float num = 0f;
				if (((ButtonControl)Keyboard.current.minusKey).wasPressedThisFrame)
				{
					num = 0f - volumeIncrement;
				}
				if (((ButtonControl)Keyboard.current.equalsKey).wasPressedThisFrame)
				{
					num = volumeIncrement;
				}
				if (num != 0f)
				{
					AudioPlayerTypeBase audioPlayerTypeBase = null;
					if (boomboxItems.lookingAtDeviceType)
					{
						audioPlayerTypeBase = boomboxItems;
					}
					else if (recordPlayers.lookingAtDeviceType)
					{
						audioPlayerTypeBase = recordPlayers;
					}
					else if (televisions.lookingAtDeviceType)
					{
						audioPlayerTypeBase = televisions;
					}
					if (audioPlayerTypeBase != null)
					{
						audioPlayerTypeBase.currentVolume = Mathf.Clamp(audioPlayerTypeBase.currentVolume + num, 0f, maxDisplayedVolume);
						audioPlayerTypeBase.UpdateVolumes();
						audioPlayerTypeBase.UpdateTooltips();
					}
				}
			}

			[HarmonyPatch(typeof(BoomboxItem), "Start")]
			[HarmonyPostfix]
			public static void SetBoomboxHoverTip(BoomboxItem __instance)
			{
				//IL_0022: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Expected O, but got Unknown
				boomboxItems.audioSources.Add(__instance.boomboxAudio);
				boomboxItems.items.Add((GrabbableObject)__instance);
				((GrabbableObject)__instance).itemProperties.canBeGrabbedBeforeGameStart = true;
				if (boomboxItems.defaultVolume == 0f)
				{
					boomboxItems.defaultVolume = __instance.boomboxAudio.volume;
				}
				if (boomboxItems.controlTooltips == null)
				{
					boomboxItems.controlTooltips = new List<string>(((GrabbableObject)__instance).itemProperties.toolTips);
					boomboxItems.controlTooltips.Add("");
				}
				boomboxItems.UpdateTooltips();
				boomboxItems.UpdateVolumes();
			}

			[HarmonyPatch(typeof(AutoParentToShip), "Awake")]
			[HarmonyPostfix]
			public static void SetRecordPlayerHoverTip(AutoParentToShip __instance)
			{
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Expected O, but got Unknown
				//IL_0080: Unknown result type (might be due to invalid IL or missing references)
				//IL_008b: Expected O, but got Unknown
				if (((Object)__instance).name.Contains("RecordPlayerContainer"))
				{
					AudioSource val = ((Component)__instance).GetComponentInChildren<AnimatedObjectTrigger>()?.thisAudioSource;
					if ((Object)val != (Object)null)
					{
						recordPlayers.audioSources.Add(val);
					}
					InteractTrigger componentInChildren = ((Component)__instance).GetComponentInChildren<InteractTrigger>();
					if (recordPlayers.defaultVolume == 0f)
					{
						recordPlayers.defaultVolume = val.volume;
					}
					if (!((Object)componentInChildren == (Object)null))
					{
						recordPlayers.triggers.Add(componentInChildren);
						recordPlayers.UpdateTooltips();
						recordPlayers.UpdateVolumes();
					}
				}
			}

			[HarmonyPatch(typeof(TVScript), "__initializeVariables")]
			[HarmonyPostfix]
			public static void SetTelevisionHoverTip(TVScript __instance)
			{
				//IL_0063: Unknown result type (might be due to invalid IL or missing references)
				//IL_006e: Expected O, but got Unknown
				televisions.audioSources.Add(__instance.tvSFX);
				Transform parent = ((Component)__instance).transform.parent;
				InteractTrigger val = (((Object)(object)parent != (Object)null) ? ((Component)parent).GetComponentInChildren<InteractTrigger>() : null);
				if (televisions.defaultVolume == 0f)
				{
					televisions.defaultVolume = __instance.tvSFX.volume;
				}
				if (!((Object)val == (Object)null))
				{
					televisions.triggers.Add(val);
					televisions.UpdateTooltips();
					televisions.UpdateVolumes();
				}
			}
		}

		internal abstract class AudioPlayerTypeBase
		{
			public string name;

			public List<AudioSource> audioSources;

			public string originalHoverTip;

			public float defaultVolume;

			public float currentVolume;

			public bool lookingAtDeviceType;

			public Type objectType;

			protected AudioPlayerTypeBase(Type objectType)
			{
				this.objectType = objectType;
			}

			public AudioPlayerTypeBase(string name, string originalHoverTip = "", float defaultVolume = 0f)
			{
				this.name = name;
				audioSources = new List<AudioSource>();
				this.originalHoverTip = originalHoverTip;
				this.defaultVolume = defaultVolume;
				currentVolume = Plugin.defaultVolume;
				lookingAtDeviceType = false;
			}

			public void UpdateVolumes()
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Expected O, but got Unknown
				for (int i = 0; i < audioSources.Count; i++)
				{
					if ((Object)audioSources[i] != (Object)null)
					{
						audioSources[i].volume = currentVolume / maxDisplayedVolume;
					}
				}
			}

			public abstract void UpdateTooltips();
		}

		internal class AudioPlayerItemType : AudioPlayerTypeBase
		{
			public List<GrabbableObject> items;

			public List<string> controlTooltips;

			public AudioPlayerItemType(string name, string originalHoverTip = "", float defaultVolume = 0f)
				: base(name, originalHoverTip, defaultVolume)
			{
				items = new List<GrabbableObject>();
			}

			public override void UpdateTooltips()
			{
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				//IL_0020: Expected O, but got Unknown
				//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fe: Expected O, but got Unknown
				//IL_0106: Unknown result type (might be due to invalid IL or missing references)
				//IL_0111: Expected O, but got Unknown
				for (int i = 0; i < items.Count; i++)
				{
					if ((Object)items[i] != (Object)null)
					{
						items[i].customGrabTooltip = $"{originalHoverTip}\n{Mathf.RoundToInt(currentVolume * 10f) * 10}% volume\n{volumeHoverTip}";
						controlTooltips[controlTooltips.Count - 1] = items[i].customGrabTooltip.Replace(originalHoverTip + "\n", "");
						items[i].itemProperties.toolTips = controlTooltips.ToArray();
					}
				}
				PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
				if ((Object)localPlayerController != (Object)null && (Object)localPlayerController.currentlyHeldObjectServer != (Object)null && items.Contains(localPlayerController.currentlyHeldObjectServer))
				{
					localPlayerController.currentlyHeldObjectServer.EquipItem();
				}
			}
		}

		internal class AudioPlayerPlaceableType : AudioPlayerTypeBase
		{
			public List<InteractTrigger> triggers;

			public AudioPlayerPlaceableType(string name, string originalHoverTip = "", float defaultVolume = 0f)
				: base(name, originalHoverTip, defaultVolume)
			{
				triggers = new List<InteractTrigger>();
			}

			public override void UpdateTooltips()
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Expected O, but got Unknown
				for (int i = 0; i < triggers.Count; i++)
				{
					if ((Object)triggers[i] != (Object)null)
					{
						triggers[i].hoverTip = $"{originalHoverTip}\n{Mathf.RoundToInt(currentVolume * 10f) * 10}% volume\n{volumeHoverTip}";
					}
				}
			}
		}

		public static Key volumeUpKey = (Key)14;

		public static Key volumeDownKey = (Key)13;

		public static string volumeHoverTip = "Volume up:   [+]\nVolume down: [-]";

		public static float volumeIncrement = 0.1f;

		public static float maxDisplayedVolume = 1.5f;

		public static float defaultVolume = 1f;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void PatchBattery(ref Item ___itemProperties)
		{
			___itemProperties.requiresBattery = false;
		}

		[HarmonyPatch("PocketItem")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			ManualLogSource val = Logger.CreateLogSource("Henrehs.BoomBoxOverhaul");
			int num = -1;
			int num2 = -1;
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 0; i < list.Count; i++)
			{
				try
				{
					if (!(list[i].opcode == OpCodes.Call))
					{
						continue;
					}
					string text = ((object)list[i]).ToString();
					if (!text.Contains("BoomboxItem::StartMusic"))
					{
						continue;
					}
					for (int num3 = i; num3 > 0; num3--)
					{
						if (list[num3].opcode == OpCodes.Ldarg_0)
						{
							num = num3;
						}
					}
					num2 = i;
				}
				catch (Exception)
				{
				}
			}
			if (num2 > -1 && num > -1)
			{
				list.RemoveRange(num, num2 - num + 1);
			}
			return list.AsEnumerable();
		}
	}
}