Decompiled source of WispDistance v1.0.4

BepInEx/plugins/WispDistance/WispDistance.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("WispDistance")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4")]
[assembly: AssemblyProduct("WispDistance")]
[assembly: AssemblyTitle("WispDistance")]
[assembly: AssemblyVersion("1.0.4.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 WispDistance
{
	[BepInPlugin("Rollstuhltreter.WispDistance", "WispDistance", "1.0.4")]
	public sealed class WispDistancePlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Demister), "OnEnable")]
		private static class DemisterPatch
		{
			[HarmonyPostfix]
			[HarmonyPriority(0)]
			private static void Postfix(Demister __instance)
			{
				_instance?.SetWispRange(__instance);
			}
		}

		public const string PluginGuid = "Rollstuhltreter.WispDistance";

		public const string PluginName = "WispDistance";

		public const string PluginVersion = "1.0.4";

		private static WispDistancePlugin _instance;

		private readonly Dictionary<Light, float> _lightRanges = new Dictionary<Light, float>();

		private readonly Dictionary<ParticleSystemForceField, float> _mistRanges = new Dictionary<ParticleSystemForceField, float>();

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _distance;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			_instance = this;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Aktiviert oder deaktiviert WispDistance.");
			_distance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Distance", 50f, "Reichweite des ausgerüsteten Wisps in Metern. Kein Maximalwert; sehr hohe Werte kosten Leistung.");
			_harmony = new Harmony("Rollstuhltreter.WispDistance");
			_harmony.PatchAll(typeof(WispDistancePlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"WispDistance geladen.");
		}

		private void Update()
		{
			if (!_enabled.Value && (_lightRanges.Count != 0 || _mistRanges.Count != 0))
			{
				RestoreOriginalRanges();
			}
		}

		private void OnDestroy()
		{
			RestoreOriginalRanges();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			if ((Object)(object)_instance == (Object)(object)this)
			{
				_instance = null;
			}
		}

		private void SetWispRange(Demister demister)
		{
			if (!_enabled.Value)
			{
				RestoreOriginalRanges();
			}
			else
			{
				if ((Object)(object)demister == (Object)null || (Object)(object)demister.m_forceField == (Object)null || !HasDemisterTrinket())
				{
					return;
				}
				float value = _distance.Value;
				if (IsUsableDistance(value))
				{
					SetRange(demister.m_forceField, value);
					Light[] componentsInChildren = ((Component)demister).GetComponentsInChildren<Light>(true);
					foreach (Light light in componentsInChildren)
					{
						SetRange(light, value);
					}
				}
			}
		}

		private static bool HasDemisterTrinket()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return false;
			}
			foreach (StatusEffect statusEffect in ((Character)localPlayer).GetSEMan().GetStatusEffects())
			{
				if ((Object)(object)statusEffect != (Object)null && ((Object)statusEffect).name == "Demister")
				{
					return true;
				}
			}
			return false;
		}

		private static bool IsUsableDistance(float distance)
		{
			if (distance > 0f && !float.IsNaN(distance))
			{
				return !float.IsInfinity(distance);
			}
			return false;
		}

		private void SetRange(ParticleSystemForceField forceField, float distance)
		{
			if (!_mistRanges.ContainsKey(forceField))
			{
				_mistRanges.Add(forceField, forceField.endRange);
			}
			forceField.endRange = distance;
		}

		private void SetRange(Light light, float distance)
		{
			if (!((Object)(object)light == (Object)null))
			{
				if (!_lightRanges.ContainsKey(light))
				{
					_lightRanges.Add(light, light.range);
				}
				light.range = distance;
			}
		}

		private void RestoreOriginalRanges()
		{
			foreach (KeyValuePair<ParticleSystemForceField, float> mistRange in _mistRanges)
			{
				if ((Object)(object)mistRange.Key != (Object)null)
				{
					mistRange.Key.endRange = mistRange.Value;
				}
			}
			foreach (KeyValuePair<Light, float> lightRange in _lightRanges)
			{
				if ((Object)(object)lightRange.Key != (Object)null)
				{
					lightRange.Key.range = lightRange.Value;
				}
			}
			_mistRanges.Clear();
			_lightRanges.Clear();
		}
	}
}