Decompiled source of WereRich v1.0.3

WereRich.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("WereRich")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("When you point at loot, the scavenger will state the obvious.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WereRich")]
[assembly: AssemblyTitle("WereRich")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 WereRich
{
	public class LootDetector : MonoBehaviour
	{
		public AudioSource audioSource;

		public bool voicedThisEmote;

		public static bool CheckForLoot(Camera camera)
		{
			//IL_0015: 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)
			WereRich.LOGGER.LogDebug((object)"Checking for loot...");
			return Physics.RaycastAll(((Component)camera).transform.position, ((Component)camera).transform.forward, 100f).ToList().Select(delegate(RaycastHit hit)
			{
				WereRich.LOGGER.LogDebug((object)("Hit " + ((Object)((RaycastHit)(ref hit)).transform).name));
				return ((Component)((RaycastHit)(ref hit)).transform).GetComponent<GrabbableObject>();
			})
				.Any((GrabbableObject obj) => obj?.itemProperties.isScrap ?? false);
		}

		public void CheckForLootAndPlaySound(Camera camera)
		{
			if (!audioSource.isPlaying && CheckForLoot(camera))
			{
				AudioClip clip = WereRich.sounds[Random.Range(0, WereRich.sounds.Length)];
				audioSource.clip = clip;
				audioSource.Play();
			}
		}
	}
	[BepInPlugin("com.raspberry.wererich", "We're Rich", "1.0.3")]
	public class WereRich : BaseUnityPlugin
	{
		public static ManualLogSource LOGGER;

		public static AudioClip[] sounds = (AudioClip[])(object)new AudioClip[4];

		public static AudioClip LoadSound(string path, AudioType type)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			AudioClip val = null;
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(path, type);
			try
			{
				UnityWebRequestAsyncOperation val2 = audioClip.SendWebRequest();
				try
				{
					while (!((AsyncOperation)val2).isDone)
					{
					}
					if ((int)audioClip.result != 1)
					{
						LOGGER.LogError((object)("Failed to load AudioClip from path: " + path + " Full error: " + audioClip.error));
					}
					else
					{
						val = DownloadHandlerAudioClip.GetContent(audioClip);
						LOGGER.LogDebug((object)$"Loaded AudioClip from path: {path} Length: {val.length}");
					}
				}
				catch (Exception arg)
				{
					LOGGER.LogError((object)$"Exception while loading AudioClip from path: {path} Full error: {arg}");
				}
			}
			finally
			{
				((IDisposable)audioClip)?.Dispose();
			}
			return val;
		}

		private void Awake()
		{
			LOGGER = ((BaseUnityPlugin)this).Logger;
			LOGGER.LogInfo((object)"Plugin We're Rich is loaded!");
			string path = Path.Combine(Paths.PluginPath, "Raspberry1111-WereRich");
			sounds[0] = LoadSound(Path.Combine(path, "WereRichDriller.mp3"), (AudioType)13);
			sounds[1] = LoadSound(Path.Combine(path, "WereRichGunner.mp3"), (AudioType)13);
			sounds[2] = LoadSound(Path.Combine(path, "WereRichScout.mp3"), (AudioType)13);
			sounds[3] = LoadSound(Path.Combine(path, "WereRich.ogg"), (AudioType)14);
			LOGGER.LogInfo((object)"Sounds loaded!");
			Harmony.CreateAndPatchAll(typeof(Patch), (string)null);
		}
	}
	internal class Patch
	{
		[HarmonyPatch(typeof(PlayerControllerB), "Start")]
		[HarmonyPrefix]
		private static bool StartPatch(PlayerControllerB __instance)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			WereRich.LOGGER.LogDebug((object)"StartPatch called!");
			GameObject val = new GameObject("LootDetector");
			val.transform.position = ((Component)__instance.gameplayCamera).transform.position;
			val.transform.SetParent(((Component)__instance).transform);
			LootDetector lootDetector = val.AddComponent<LootDetector>();
			lootDetector.audioSource = val.AddComponent<AudioSource>();
			lootDetector.audioSource.spatialBlend = 1f;
			lootDetector.audioSource.maxDistance = __instance.movementAudio.maxDistance;
			val.SetActive(true);
			return true;
		}

		[HarmonyPatch(typeof(PlayerControllerB), "__rpc_handler_1955832627")]
		[HarmonyPrefix]
		private static bool RpcPatch(NetworkBehaviour target)
		{
			WereRich.LOGGER.LogDebug((object)"Emote RpcPatch called!");
			PlayerControllerB component = ((Component)target).GetComponent<PlayerControllerB>();
			WereRich.LOGGER.LogDebug((object)$"controller = ({component})");
			Transform obj = ((Component)component).transform.Find("LootDetector");
			GameObject val = ((obj != null) ? ((Component)obj).gameObject : null);
			WereRich.LOGGER.LogDebug((object)$"lootDetectorObject = ({val})");
			LootDetector component2 = val.GetComponent<LootDetector>();
			WereRich.LOGGER.LogDebug((object)$"lootDetector = ({component2})");
			component2?.CheckForLootAndPlaySound(component.gameplayCamera);
			return true;
		}
	}
}