Decompiled source of BetterLightning v1.0.3

BetterLightning.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;

[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 = "")]
[assembly: AssemblyCompany("BetterLightning")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterLightning")]
[assembly: AssemblyTitle("BetterLightning")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace BetterLightning
{
	[BepInPlugin("Archmage.BetterLightning", "BetterLightning", "1.0.3")]
	public class BetterLightningMod : BaseUnityPlugin
	{
		private const string modGUID = "Archmage.BetterLightning";

		private const string modName = "BetterLightning";

		private const string modVersion = "1.0.3";

		public static BetterLightningMod Instance;

		private readonly Harmony harmony = new Harmony("Archmage.BetterLightning");

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Archmage.BetterLightning is loaded!");
			harmony.PatchAll();
		}
	}
}
namespace BetterLightning.Patches
{
	internal static class Util
	{
		public static bool StartParticleEffect(ref StormyWeather _instance, ref GameObject warningObject, ref Vector3 currentRandomStrikePos, ref float randomThunderTime, ref ParticleSystem particles)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)warningObject == (Object)null)
			{
				warningObject = GameObject.CreatePrimitive((PrimitiveType)2);
				((Object)warningObject).name = "LightningSpot";
			}
			warningObject.transform.localPosition = currentRandomStrikePos;
			GameObject obj = warningObject;
			if (obj != null)
			{
				obj.SetActive(true);
			}
			float num = MathF.Abs(TimeOfDay.Instance.globalTime - randomThunderTime);
			ShapeModule shape = particles.shape;
			GameObject obj2 = warningObject;
			((ShapeModule)(ref shape)).meshRenderer = ((obj2 != null) ? obj2.GetComponent<MeshRenderer>() : null);
			((Component)particles).transform.localPosition = currentRandomStrikePos;
			Transform transform = ((Component)particles).transform;
			transform.localPosition += new Vector3(0f, 0.01f, 0f);
			float length = _instance.staticElectricityAudio.length;
			if (num <= length)
			{
				StartAudioAndParticles(length - num, ref particles, ref _instance);
				return true;
			}
			return false;
		}

		public static void StartAudioAndParticles(float offset, ref ParticleSystem particles, ref StormyWeather _instance)
		{
			particles.time = offset + 2f;
			particles.Play();
			particles.time = offset + 2f;
			((Component)particles).gameObject.GetComponent<AudioSource>().clip = _instance.staticElectricityAudio;
			((Component)particles).gameObject.GetComponent<AudioSource>().loop = false;
			((Component)particles).gameObject.GetComponent<AudioSource>().time = offset + 2f;
			((Component)particles).gameObject.GetComponent<AudioSource>().Play();
		}
	}
	[HarmonyPatch(typeof(StormyWeather))]
	internal class StormyWeatherPatch
	{
		public static Vector3 lastRandomStrikePos = Vector3.zero;

		public static Vector3 currentRandomStrikePos = Vector3.zero;

		public static float randomThunderTime = 0f;

		public static bool generatedWarning = false;

		public static GameObject warningObject = null;

		public static float AudioTimer = -1f;

		public static ParticleSystem RandomLightningParticle = null;

		public static GameObject ParticleObject = null;

		private static bool StartedWarning = false;

		[HarmonyPatch("OnEnable")]
		[HarmonyPostfix]
		private static void OnEnable(ref StormyWeather __instance)
		{
			generatedWarning = false;
			if ((Object)(object)warningObject != (Object)null)
			{
				Object.Destroy((Object)(object)warningObject);
			}
			if ((Object)(object)ParticleObject == (Object)null)
			{
				ParticleObject = Object.Instantiate<GameObject>(((Component)__instance.staticElectricityParticle).gameObject);
				((Object)ParticleObject).name = "RandomLightningParticles";
				RandomLightningParticle = ParticleObject.GetComponent<ParticleSystem>();
			}
			randomThunderTime = 0f;
		}

		[HarmonyPatch("OnDisable")]
		[HarmonyPostfix]
		private static void OnDisable()
		{
			if ((Object)(object)warningObject != (Object)null)
			{
				Object.Destroy((Object)(object)warningObject);
			}
			if ((Object)(object)ParticleObject != (Object)null)
			{
				Object.Destroy((Object)(object)ParticleObject);
				RandomLightningParticle = null;
			}
		}

		[HarmonyPatch("LightningStrikeRandom")]
		[HarmonyPrefix]
		private static bool patch_Pre_LightningStrikeRandom(ref StormyWeather __instance, ref float ___timeAtLastStrike, ref GameObject[] ___outsideNodes, ref NavMeshHit ___navHit)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: 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_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			FieldInfo fieldInfo = AccessTools.Field(typeof(StormyWeather), "seed");
			Random random = (Random)fieldInfo.GetValue(__instance);
			Vector3 randomNavMeshPositionInBoxPredictable;
			if (random.Next(0, 100) < 60 && (randomThunderTime - ___timeAtLastStrike) * TimeOfDay.Instance.currentWeatherVariable < 3f)
			{
				randomNavMeshPositionInBoxPredictable = currentRandomStrikePos;
			}
			else
			{
				int num = random.Next(0, ___outsideNodes.Length);
				if (___outsideNodes == null || (Object)(object)___outsideNodes[num] == (Object)null)
				{
					___outsideNodes = (from x in GameObject.FindGameObjectsWithTag("OutsideAINode")
						orderby x.transform.position.x + x.transform.position.z
						select x).ToArray();
				}
				randomNavMeshPositionInBoxPredictable = ___outsideNodes[num].transform.position;
				randomNavMeshPositionInBoxPredictable = RoundManager.Instance.GetRandomNavMeshPositionInBoxPredictable(randomNavMeshPositionInBoxPredictable, 15f, ___navHit, random, -1);
			}
			currentRandomStrikePos = randomNavMeshPositionInBoxPredictable;
			generatedWarning = true;
			return false;
		}

		[HarmonyPatch("DetermineNextStrikeInterval")]
		[HarmonyPrefix]
		private static void patch_Pre_DetermineNextStrikeInterval(ref float ___randomThunderTime)
		{
			___randomThunderTime = randomThunderTime;
		}

		[HarmonyPatch("DetermineNextStrikeInterval")]
		[HarmonyPostfix]
		private static void patch_Post_DetermineNextStrikeInterval(ref float ___randomThunderTime)
		{
			randomThunderTime = ___randomThunderTime;
			___randomThunderTime = TimeOfDay.Instance.globalTime + 1f;
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void patch_Pre_Update(ref StormyWeather __instance, ref float ___randomThunderTime)
		{
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			if (!((Component)__instance).gameObject.activeInHierarchy)
			{
				return;
			}
			___randomThunderTime = TimeOfDay.Instance.globalTime + 1f;
			if (!generatedWarning)
			{
				AccessTools.Method(typeof(StormyWeather), "DetermineNextStrikeInterval", (Type[])null, (Type[])null).Invoke(__instance, null);
				AccessTools.Method(typeof(StormyWeather), "LightningStrikeRandom", (Type[])null, (Type[])null).Invoke(__instance, null);
				StartedWarning = Util.StartParticleEffect(ref __instance, ref warningObject, ref currentRandomStrikePos, ref randomThunderTime, ref RandomLightningParticle);
				AudioTimer = __instance.staticElectricityAudio.length;
			}
			if (!StartedWarning)
			{
				float num = MathF.Abs(TimeOfDay.Instance.globalTime - randomThunderTime);
				if (num <= AudioTimer)
				{
					Util.StartAudioAndParticles(AudioTimer - num, ref RandomLightningParticle, ref __instance);
					StartedWarning = true;
				}
			}
			if (TimeOfDay.Instance.globalTime > randomThunderTime)
			{
				__instance.LightningStrike(currentRandomStrikePos, false);
				generatedWarning = false;
				RandomLightningParticle.Stop();
				((Component)RandomLightningParticle).gameObject.GetComponent<AudioSource>().Stop();
				GameObject obj = warningObject;
				if (obj != null)
				{
					obj.SetActive(false);
				}
			}
		}
	}
}