Decompiled source of DangerSense v0.0.3

DangerSense.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
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 BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UpgradeLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DangerSense")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1")]
[assembly: AssemblyProduct("DangerSense")]
[assembly: AssemblyTitle("DangerSense")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.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 DangerSense
{
	[BepInPlugin("eu.xaru.dangersense", "DangerSense", "0.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class DangerSensePlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "eu.xaru.dangersense";

		public const string PluginName = "DangerSense";

		public const string PluginVersion = "0.0.3";

		private static ManualLogSource s_log;

		private static readonly UpgradeDefinition DangerSenseUpgrade = new UpgradeDefinition(90001, "DANGER_SENSE", "Danger Sense", "Alerts you with a beep if an enemy is near. The beep is spatial, so it gets louder the closer they are.", 75, false, (Action<bool>)delegate(bool fromSave)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			if ((Object)(object)DangerSenseBehaviour.Instance == (Object)null)
			{
				GameObject val = new GameObject("DangerSenseBehaviourObj");
				Object.DontDestroyOnLoad((Object)(object)val);
				val.AddComponent<DangerSenseBehaviour>();
			}
			Debug.Log((object)$"[DangerSense] DangerSense => OnPurchased triggered (fromSave={fromSave}).");
		}, (Action)delegate
		{
			if ((Object)(object)DangerSenseBehaviour.Instance != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)DangerSenseBehaviour.Instance).gameObject);
			}
			Debug.Log((object)"[DangerSense] DangerSense => OnLockedAllUpgrades => beep logic removed.");
		});

		private void Awake()
		{
			s_log = ((BaseUnityPlugin)this).Logger;
			s_log.LogInfo((object)"DangerSense v0.0.3 loading...");
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			UpgradeLibManager.RegisterUpgrade(DangerSenseUpgrade);
			s_log.LogInfo((object)"DangerSense awake complete. DangerSense upgrade is now registered.");
		}
	}
	public class DangerSenseBehaviour : MonoBehaviour
	{
		private float _checkInterval = 2f;

		private float _timer = 0f;

		private float _dangerRadius = 35f;

		private AudioSource _source;

		private AudioClip _beepClip;

		private FieldInfo _entitiesField;

		public static DangerSenseBehaviour Instance { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			Instance = this;
			_source = ((Component)this).gameObject.AddComponent<AudioSource>();
			_source.playOnAwake = false;
			_source.loop = false;
			_source.spatialBlend = 1f;
			_source.rolloffMode = (AudioRolloffMode)0;
			_source.minDistance = 0.5f;
			_source.maxDistance = _dangerRadius;
			_source.volume = 1f;
			string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";
			string text = Path.Combine(path, "beep.mp3");
			_beepClip = LoadAudioClipFromFile(text);
			if ((Object)(object)_beepClip != (Object)null)
			{
				_source.clip = _beepClip;
				Debug.Log((object)("[DangerSense] Loaded beep from '" + text + "'."));
			}
			else
			{
				Debug.LogWarning((object)("[DangerSense] Could not load beep from '" + text + "'. No beep audio!"));
			}
			try
			{
				Type typeFromHandle = typeof(EntitySpawner);
				_entitiesField = typeFromHandle.GetField("entities", BindingFlags.Instance | BindingFlags.NonPublic);
				if (_entitiesField == null)
				{
					Debug.LogError((object)"[DangerSense] Could not find private field 'entities' in EntitySpawner!");
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[DangerSense] Reflection setup error: " + ex));
			}
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			Debug.Log((object)"[DangerSense] DangerSenseBehaviour active with spatial beep.");
		}

		private void Update()
		{
			if (!((Object)(object)_beepClip == (Object)null) && !((Object)(object)_source == (Object)null))
			{
				_timer += Time.deltaTime;
				if (_timer >= _checkInterval)
				{
					_timer = 0f;
					ScanEnemiesAndBeep();
				}
			}
		}

		private void ScanEnemiesAndBeep()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: 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_014a: Unknown result type (might be due to invalid IL or missing references)
			EntitySpawner instance = EntitySpawner.instance;
			if (!Object.op_Implicit((Object)(object)instance))
			{
				Debug.LogWarning((object)"[DangerSense] No EntitySpawner.instance, can't scan enemies!");
				return;
			}
			List<Entity> list = null;
			if (_entitiesField != null)
			{
				try
				{
					list = _entitiesField.GetValue(instance) as List<Entity>;
				}
				catch
				{
				}
			}
			if (list == null || list.Count == 0 || !Object.op_Implicit((Object)(object)PlayerLocomotionController.instance))
			{
				return;
			}
			Vector3 position = ((Component)PlayerLocomotionController.instance).transform.position;
			float num = float.MaxValue;
			Entity val = null;
			foreach (Entity item in list)
			{
				if (Object.op_Implicit((Object)(object)item))
				{
					float num2 = Vector3.Distance(((Component)item).transform.position, position);
					if (num2 < num)
					{
						num = num2;
						val = item;
					}
				}
			}
			if (num <= _dangerRadius && (Object)(object)val != (Object)null)
			{
				Vector3 position2 = Vector3.Lerp(position, ((Component)val).transform.position, 0.5f);
				((Component)this).transform.position = position2;
				float num3 = Mathf.Clamp01(1f - num / _dangerRadius);
				_source.volume = num3;
				_source.pitch = Mathf.Lerp(0.75f, 1.25f, num3);
				_source.PlayOneShot(_beepClip, _source.volume);
			}
		}

		private AudioClip LoadAudioClipFromFile(string filePath)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			if (!File.Exists(filePath))
			{
				return null;
			}
			string text = "file://" + filePath;
			WWW val = new WWW(text);
			try
			{
				while (!val.isDone)
				{
				}
				if (string.IsNullOrEmpty(val.error))
				{
					return val.GetAudioClip(false, false);
				}
				Debug.LogWarning((object)("[DangerSense] Audio load error: " + val.error));
				return null;
			}
			finally
			{
				((IDisposable)val)?.Dispose();
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DangerSense";

		public const string PLUGIN_NAME = "DangerSense";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}