Decompiled source of HearthRange v1.0.0

plugins/HearthRange.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Extensions;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[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("HearthRangeDebug")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HearthRangeDebug")]
[assembly: AssemblyTitle("HearthRangeDebug")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("eros.hearthrange", "Hearth Range", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class HearthRange : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static class ZNetScene_Awake_Patch
	{
		private static void Postfix(ZNetScene __instance)
		{
			ApplyHearthWarmth();
		}
	}

	[HarmonyPatch(typeof(SE_Rested), "GetNearbyComfortPieces")]
	private static class SE_Rested_GetNearbyComfortPieces_Patch
	{
		private static readonly MethodInfo OriginalMethod = AccessTools.Method(typeof(Piece), "GetAllComfortPiecesInRadius", (Type[])null, (Type[])null);

		private static readonly MethodInfo ReplacementMethod = AccessTools.Method(typeof(SE_Rested_GetNearbyComfortPieces_Patch), "GetHearthAwareComfortPieces", (Type[])null, (Type[])null);

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			foreach (CodeInstruction instruction in instructions)
			{
				if (CodeInstructionExtensions.Calls(instruction, OriginalMethod))
				{
					yield return new CodeInstruction(OpCodes.Call, (object)ReplacementMethod);
				}
				else
				{
					yield return instruction;
				}
			}
		}

		private static void GetHearthAwareComfortPieces(Vector3 point, float radius, List<Piece> pieces)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			Piece.GetAllComfortPiecesInRadius(point, 10f, pieces);
			List<Piece> list = new List<Piece>();
			Piece.GetAllComfortPiecesInRadius(point, ComfortRadius.Value, list);
			foreach (Piece item in list)
			{
				if (!((Object)(object)item == (Object)null) && !(item.m_name != "$piece_hearth") && !pieces.Contains(item))
				{
					pieces.Add(item);
				}
			}
		}
	}

	public static ConfigEntry<float> WarmthRadius;

	public static ConfigEntry<float> ComfortRadius;

	private void Awake()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Hearth Range loading...");
		CreateConfig();
		new Harmony("eros.hearthrange").PatchAll();
		SynchronizationManager.OnConfigurationSynchronized += delegate(object obj, ConfigurationSynchronizationEventArgs attr)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[Hearth Range] Config synchronized. Initial: " + attr.InitialSynchronization + " | Warmth: " + WarmthRadius.Value + " | Comfort: " + ComfortRadius.Value));
			ApplyHearthWarmth();
		};
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Hearth Range loaded. Warmth: " + WarmthRadius.Value + "m, Comfort: " + ComfortRadius.Value + "m"));
	}

	private void CreateConfig()
	{
		WarmthRadius = ConfigFileExtensions.BindConfig<float>(((BaseUnityPlugin)this).Config, "Hearth", "WarmthRadius", 20f, "Heat range of Hearths in meters.", true, (int?)null, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null);
		ComfortRadius = ConfigFileExtensions.BindConfig<float>(((BaseUnityPlugin)this).Config, "Hearth", "ComfortRadius", 20f, "Comfort range of Hearths in meters.", true, (int?)null, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null);
	}

	public static void ApplyHearthWarmth()
	{
		if ((Object)(object)ZNetScene.instance == (Object)null)
		{
			Debug.Log((object)"[Hearth Range] Cannot apply warmth yet: ZNetScene is null.");
			return;
		}
		if (ZNetScene.instance.m_prefabs == null)
		{
			Debug.Log((object)"[Hearth Range] Cannot apply warmth yet: prefab list is null.");
			return;
		}
		foreach (GameObject prefab in ZNetScene.instance.m_prefabs)
		{
			if ((Object)(object)prefab == (Object)null)
			{
				continue;
			}
			Piece component = prefab.GetComponent<Piece>();
			if ((Object)(object)component == (Object)null || component.m_name != "$piece_hearth")
			{
				continue;
			}
			SphereCollider[] componentsInChildren = prefab.GetComponentsInChildren<SphereCollider>(true);
			foreach (SphereCollider val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !(((Object)val).name != "FireWarmth"))
				{
					float radius = val.radius;
					val.radius = WarmthRadius.Value;
					Debug.Log((object)("[Hearth Range] FireWarmth radius: " + radius + " -> " + WarmthRadius.Value));
				}
			}
		}
	}
}