Decompiled source of Warming Lava v2.1.0

tony4twentys-Warming Lava.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Warming Lava")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Warming Lava")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f5351afc-7109-4520-8347-767a2b39bc28")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("tony4twentys.Warming_Lava", "Warming Lava", "2.1.0")]
public class LavaColdReducer : BaseUnityPlugin
{
	[HarmonyPatch(typeof(MapHandler), "GoToSegment")]
	public class SegmentTransitionPatch
	{
		private static void Postfix(Segment s)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			Instance.currentSegment = s;
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)$"[Warming Lava] Segment changed to: {s}");
			Instance.EnsureWarmthStateForContext();
		}
	}

	[HarmonyPatch(typeof(RunManager), "StartRun")]
	public class RunStartPatch
	{
		private static void Postfix()
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if (Instance.warmthCoroutine != null)
			{
				Instance.InstanceStopWarmth();
			}
			if (Instance.dayNightWatcher != null)
			{
				((MonoBehaviour)Instance).StopCoroutine(Instance.dayNightWatcher);
				Instance.dayNightWatcher = null;
			}
			Instance.player = null;
			Instance.isNightCold = Ascents.isNightCold;
			Instance.currentSegment = (Segment)1;
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)$"[Warming Lava] Run started. isNightCold={Instance.isNightCold}");
			if (Instance.isNightCold)
			{
				Instance.dayNightWatcher = ((MonoBehaviour)Instance).StartCoroutine(Instance.DayNightWatcher());
			}
			Instance.RecacheSceneRefs();
			Instance.EnsureWarmthStateForContext();
		}
	}

	[CompilerGenerated]
	private sealed class <CheckWarmthLoop>d__13 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public LavaColdReducer <>4__this;

		private float <recacheTimer>5__1;

		private float <amount>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <CheckWarmthLoop>d__13(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)"[Warming Lava] Warmth loop STARTED.");
				<recacheTimer>5__1 = 0f;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if ((Object)(object)<>4__this.player == (Object)null)
			{
				<>4__this.player = Character.localCharacter;
			}
			<recacheTimer>5__1 -= <>4__this.checkInterval;
			if (<recacheTimer>5__1 <= 0f)
			{
				<>4__this.RecacheSceneRefs();
				<recacheTimer>5__1 = 5f;
			}
			if ((Object)(object)<>4__this.player != (Object)null && <>4__this.IsNearAnyLava(<>4__this.player.Center))
			{
				<amount>5__2 = <>4__this.ColdReductionPerSecond.Value * <>4__this.checkInterval;
				<>4__this.player.refs.afflictions.AdjustStatus((STATUSTYPE)2, 0f - <amount>5__2, false);
				((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)$"[Warming Lava] Warming player near lava. Cold reduced by {<amount>5__2}.");
			}
			<>2__current = (object)new WaitForSecondsRealtime(<>4__this.checkInterval);
			<>1__state = 1;
			return true;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <DayNightWatcher>d__16 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public LavaColdReducer <>4__this;

		private bool <lastNight>5__1;

		private bool <nowNight>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <DayNightWatcher>d__16(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<lastNight>5__1 = IsNightNow();
				((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)$"[Warming Lava] Day/Night watcher started. Initial night={<lastNight>5__1}");
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			<nowNight>5__2 = IsNightNow();
			if (<nowNight>5__2 != <lastNight>5__1)
			{
				((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)$"[Warming Lava] Day/Night changed ⇒ night={<nowNight>5__2}");
				<>4__this.EnsureWarmthStateForContext();
				<lastNight>5__1 = <nowNight>5__2;
			}
			<>2__current = (object)new WaitForSecondsRealtime(1f);
			<>1__state = 1;
			return true;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private ConfigEntry<float> ColdReductionPerSecond;

	private ConfigEntry<float> WarmthRadius;

	private Character player;

	private readonly float checkInterval = 1f;

	private Coroutine warmthCoroutine;

	private Coroutine dayNightWatcher;

	private bool isNightCold;

	private Segment currentSegment;

	private MeshRenderer[] allRenderers = Array.Empty<MeshRenderer>();

	private Lava[] lavaPools = Array.Empty<Lava>();

	private LavaRiver[] lavaRivers = Array.Empty<LavaRiver>();

	private MovingLava[] movingLavas = Array.Empty<MovingLava>();

	public static LavaColdReducer Instance => Chainloader.ManagerObject.GetComponent<LavaColdReducer>();

	private void Awake()
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		ColdReductionPerSecond = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "ColdReductionPerSecond", 0.1f, "Cold reduced per second while near lava.");
		WarmthRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "WarmthRadius", 8f, "Radius around lava/lava rivers that applies warmth.");
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Loaded. ColdReduction/s: {ColdReductionPerSecond.Value}, Radius: {WarmthRadius.Value}");
		new Harmony("tony4twentys.Warming_Lava").PatchAll();
	}

	[IteratorStateMachine(typeof(<CheckWarmthLoop>d__13))]
	private IEnumerator CheckWarmthLoop()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <CheckWarmthLoop>d__13(0)
		{
			<>4__this = this
		};
	}

	private void StartWarmth()
	{
		if (warmthCoroutine == null)
		{
			warmthCoroutine = ((MonoBehaviour)this).StartCoroutine(CheckWarmthLoop());
		}
	}

	private void StopWarmth()
	{
		if (warmthCoroutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(warmthCoroutine);
			warmthCoroutine = null;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] Warmth loop STOPPED.");
		}
	}

	[IteratorStateMachine(typeof(<DayNightWatcher>d__16))]
	private IEnumerator DayNightWatcher()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <DayNightWatcher>d__16(0)
		{
			<>4__this = this
		};
	}

	private static bool IsNightNow()
	{
		DayNightManager instance = DayNightManager.instance;
		if ((Object)(object)instance == (Object)null)
		{
			return false;
		}
		float timeOfDay = instance.timeOfDay;
		return timeOfDay < 6f || timeOfDay > 20f;
	}

	private void RecacheSceneRefs()
	{
		try
		{
			lavaPools = Object.FindObjectsByType<Lava>((FindObjectsSortMode)0);
			lavaRivers = Object.FindObjectsByType<LavaRiver>((FindObjectsSortMode)0);
			movingLavas = Object.FindObjectsByType<MovingLava>((FindObjectsSortMode)0);
			allRenderers = Object.FindObjectsByType<MeshRenderer>((FindObjectsSortMode)0);
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Recached: pools={lavaPools.Length}, rivers={lavaRivers.Length}, moving={movingLavas.Length}, renderers={allRenderers.Length}");
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"[Warming Lava] Recache failed: {arg}");
		}
	}

	private bool IsNearAnyLava(Vector3 pos)
	{
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_022d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0235: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02db: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
		float value = WarmthRadius.Value;
		bool result = false;
		Lava[] array = lavaPools;
		Bounds bounds;
		foreach (Lava val in array)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			MeshRenderer componentInChildren = ((Component)val).GetComponentInChildren<MeshRenderer>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				bounds = ((Renderer)componentInChildren).bounds;
				Vector3 val2 = ((Bounds)(ref bounds)).ClosestPoint(pos);
				float num = Vector3.Distance(pos, val2);
				if (num <= value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Near Lava (pool bounds). d={num:F2}");
					result = true;
				}
			}
			else
			{
				float num2 = Vector3.Distance(pos, ((Component)val).transform.position);
				if (num2 <= value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Near Lava (pool transform). d={num2:F2}");
					result = true;
				}
			}
		}
		LavaRiver[] array2 = lavaRivers;
		foreach (LavaRiver val3 in array2)
		{
			if ((Object)(object)val3 == (Object)null || val3.frames == null)
			{
				continue;
			}
			for (int k = 0; k < val3.frames.Count; k++)
			{
				if (Vector3.Distance(pos, val3.frames[k].position) <= value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] Near LavaRiver (spline).");
					result = true;
					break;
				}
			}
		}
		MovingLava[] array3 = movingLavas;
		foreach (MovingLava val4 in array3)
		{
			if ((Object)(object)val4 == (Object)null)
			{
				continue;
			}
			MeshRenderer componentInChildren2 = ((Component)val4).GetComponentInChildren<MeshRenderer>();
			if ((Object)(object)componentInChildren2 != (Object)null)
			{
				bounds = ((Renderer)componentInChildren2).bounds;
				Vector3 val5 = ((Bounds)(ref bounds)).ClosestPoint(pos);
				float num3 = Vector3.Distance(pos, val5);
				if (num3 <= value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Near MovingLava (bounds). d={num3:F2}");
					result = true;
				}
			}
			else
			{
				float num4 = Vector3.Distance(pos, ((Component)val4).transform.position);
				if (num4 <= value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Near MovingLava (transform). d={num4:F2}");
					result = true;
				}
			}
		}
		for (int m = 0; m < allRenderers.Length; m++)
		{
			MeshRenderer val6 = allRenderers[m];
			if (!((Object)(object)val6 == (Object)null) && ((Renderer)val6).enabled && LooksLikeLavaRenderer(val6))
			{
				bounds = ((Renderer)val6).bounds;
				Vector3 val7 = ((Bounds)(ref bounds)).ClosestPoint(pos);
				float num5 = Vector3.Distance(pos, val7);
				if (num5 <= value)
				{
					string hierarchyPath = GetHierarchyPath(((Component)val6).transform);
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Warming Lava] Near Lava-like MeshRenderer (material/shader contains 'lava'). d={num5:F2} path={hierarchyPath}");
					result = true;
				}
			}
		}
		return result;
	}

	private static bool LooksLikeLavaRenderer(MeshRenderer r)
	{
		if (((Object)r).name.IndexOf("lava", StringComparison.OrdinalIgnoreCase) >= 0)
		{
			return true;
		}
		try
		{
			Material[] sharedMaterials = ((Renderer)r).sharedMaterials;
			foreach (Material val in sharedMaterials)
			{
				if (!((Object)(object)val == (Object)null))
				{
					if (((Object)val).name.IndexOf("lava", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						return true;
					}
					if ((Object)(object)val.shader != (Object)null && ((Object)val.shader).name.IndexOf("lava", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						return true;
					}
				}
			}
		}
		catch
		{
		}
		return false;
	}

	private static string GetHierarchyPath(Transform t)
	{
		List<string> list = new List<string>();
		while ((Object)(object)t != (Object)null)
		{
			list.Add(((Object)t).name);
			t = t.parent;
		}
		list.Reverse();
		return string.Join("/", list);
	}

	private void EnsureWarmthStateForContext()
	{
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Invalid comparison between Unknown and I4
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Invalid comparison between Unknown and I4
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Invalid comparison between Unknown and I4
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Invalid comparison between Unknown and I4
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Invalid comparison between Unknown and I4
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Invalid comparison between Unknown and I4
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Invalid comparison between Unknown and I4
		bool flag = false;
		if (!isNightCold)
		{
			if ((int)currentSegment == 2)
			{
				flag = true;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] (A1-4) Alpine ⇒ warmth ON.");
			}
			else if ((int)currentSegment == 3 || (int)currentSegment == 4)
			{
				flag = false;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] (A1-4) Caldera/Kiln ⇒ warmth OFF.");
			}
			else
			{
				flag = false;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] (A1-4) Non-Alpine ⇒ warmth OFF.");
			}
		}
		else if ((int)currentSegment == 2)
		{
			flag = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] (A5+) Alpine ⇒ warmth ON (day & night).");
		}
		else if ((int)currentSegment == 1)
		{
			bool flag2 = IsNightNow();
			flag = flag2;
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("[Warming Lava] (A5+) Tropics ⇒ night={0} ⇒ warmth {1}.", flag2, flag2 ? "ON" : "OFF"));
		}
		else if ((int)currentSegment == 3 || (int)currentSegment == 4)
		{
			bool flag3 = IsNightNow();
			flag = flag3;
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("[Warming Lava] (A5+) Caldera/Kiln ⇒ night={0} ⇒ warmth {1}.", flag3, flag3 ? "ON" : "OFF"));
		}
		else
		{
			flag = false;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[Warming Lava] (A5+) Non-target segment ⇒ warmth OFF.");
		}
		if (flag)
		{
			StartWarmth();
		}
		else
		{
			StopWarmth();
		}
	}

	private void InstanceStopWarmth()
	{
		StopWarmth();
	}
}