Decompiled source of Comfortable v1.0.5

Comfortable.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Comfortable")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("Comfortable")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a9d5f180-5ffb-4854-9a5e-81940e3b5666")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.5.0")]
namespace Comfortable;

[BepInPlugin("dev.crystal.comfortable", "Comfortable", "1.0.5.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class ComfortablePlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(FejdStartup))]
	private static class FejdStartup_Patches
	{
		[HarmonyPatch("SetupObjectDB")]
		[HarmonyPostfix]
		private static void SetupObjectDB_Postfix()
		{
			OnObjectDBSetup();
		}
	}

	[HarmonyPatch(typeof(Player))]
	private static class Player_Patches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(Player __instance)
		{
			sPlayers.Add(__instance);
		}

		[HarmonyPatch("OnDestroy")]
		[HarmonyPrefix]
		private static void OnDestroy_Prefix(Player __instance)
		{
			sPlayers.Remove(__instance);
		}
	}

	[HarmonyPatch(typeof(SE_Rested))]
	private static class SE_Rested_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Finishing
		}

		[CompilerGenerated]
		private sealed class <GetNearbyComfortPieces_Transpiler>d__1 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private TranspilerState <state>5__2;

			private IEnumerator<CodeInstruction> <>7__wrap2;

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

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

			[DebuggerHidden]
			public <GetNearbyComfortPieces_Transpiler>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 2u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b5: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<state>5__2 = TranspilerState.Searching;
						<>7__wrap2 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						<state>5__2 = TranspilerState.Finishing;
						break;
					case 2:
						<>1__state = -3;
						break;
					case 3:
						<>1__state = -3;
						break;
					}
					while (<>7__wrap2.MoveNext())
					{
						CodeInstruction current = <>7__wrap2.Current;
						switch (<state>5__2)
						{
						case TranspilerState.Searching:
							if (current.opcode == OpCodes.Ldc_R4 && (float)current.operand == sComfortRadiusConstant)
							{
								<>2__current = new CodeInstruction(OpCodes.Ldc_R4, (object)ComfortItemRadius.Value);
								<>1__state = 1;
								return true;
							}
							<>2__current = current;
							<>1__state = 2;
							return true;
						case TranspilerState.Finishing:
							<>2__current = current;
							<>1__state = 3;
							return true;
						}
					}
					<>m__Finally1();
					<>7__wrap2 = null;
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

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

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap2 != null)
				{
					<>7__wrap2.Dispose();
				}
			}

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

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<GetNearbyComfortPieces_Transpiler>d__1 <GetNearbyComfortPieces_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<GetNearbyComfortPieces_Transpiler>d__ = this;
				}
				else
				{
					<GetNearbyComfortPieces_Transpiler>d__ = new <GetNearbyComfortPieces_Transpiler>d__1(0);
				}
				<GetNearbyComfortPieces_Transpiler>d__.instructions = <>3__instructions;
				return <GetNearbyComfortPieces_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<GetNearbyComfortPieces_Transpiler>d__1))]
		[HarmonyPatch("GetNearbyComfortPieces")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GetNearbyComfortPieces_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <GetNearbyComfortPieces_Transpiler>d__1(-2)
			{
				<>3__instructions = instructions
			};
		}
	}

	[HarmonyPatch(typeof(EffectArea))]
	private static class EffectArea_Patches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(EffectArea __instance)
		{
			UpdateFireEffectRadius(__instance, 1f, FireItemRadiusMultiplier.Value);
		}
	}

	[CompilerGenerated]
	private sealed class <GetAllRestedEffects>d__21 : IEnumerable<SE_Rested>, IEnumerable, IEnumerator<SE_Rested>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private SE_Rested <>2__current;

		private int <>l__initialThreadId;

		private List<Player>.Enumerator <>7__wrap1;

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

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

		[DebuggerHidden]
		public <GetAllRestedEffects>d__21(int <>1__state)
		{
			this.<>1__state = <>1__state;
			<>l__initialThreadId = Environment.CurrentManagedThreadId;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			int num = <>1__state;
			if (num == -3 || num == 2)
			{
				try
				{
				}
				finally
				{
					<>m__Finally1();
				}
			}
			<>7__wrap1 = default(List<Player>.Enumerator);
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			try
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
				{
					<>1__state = -1;
					SE_Rested restedEffect = GetRestedEffect();
					if ((Object)(object)restedEffect != (Object)null)
					{
						<>2__current = restedEffect;
						<>1__state = 1;
						return true;
					}
					goto IL_004f;
				}
				case 1:
					<>1__state = -1;
					goto IL_004f;
				case 2:
					{
						<>1__state = -3;
						break;
					}
					IL_004f:
					<>7__wrap1 = sPlayers.GetEnumerator();
					<>1__state = -3;
					break;
				}
				while (<>7__wrap1.MoveNext())
				{
					SE_Rested restedEffect2 = GetRestedEffect(<>7__wrap1.Current);
					if ((Object)(object)restedEffect2 != (Object)null)
					{
						<>2__current = restedEffect2;
						<>1__state = 2;
						return true;
					}
				}
				<>m__Finally1();
				<>7__wrap1 = default(List<Player>.Enumerator);
				return false;
			}
			catch
			{
				//try-fault
				((IDisposable)this).Dispose();
				throw;
			}
		}

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

		private void <>m__Finally1()
		{
			<>1__state = -1;
			((IDisposable)<>7__wrap1).Dispose();
		}

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

		[DebuggerHidden]
		IEnumerator<SE_Rested> IEnumerable<SE_Rested>.GetEnumerator()
		{
			if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
			{
				<>1__state = 0;
				return this;
			}
			return new <GetAllRestedEffects>d__21(0);
		}

		[DebuggerHidden]
		IEnumerator IEnumerable.GetEnumerator()
		{
			return ((IEnumerable<SE_Rested>)this).GetEnumerator();
		}
	}

	public const string ModId = "dev.crystal.comfortable";

	public static ConfigEntry<float> BaseRestTime;

	public static ConfigEntry<float> RestTimePerComfort;

	public static ConfigEntry<float> ComfortItemRadius;

	public static ConfigEntry<float> FireItemRadiusMultiplier;

	private static Harmony sFejdStartupHarmony;

	private static Harmony sPlayerHarmony;

	private static Harmony sSERestedHarmony;

	private static Harmony sEffectAreaHarmony;

	private static readonly List<Player> sPlayers;

	private static readonly float sComfortRadiusConstant;

	private float mFireRadiusMultiplier;

	static ComfortablePlugin()
	{
		sPlayers = new List<Player>();
		sComfortRadiusConstant = (float)typeof(SE_Rested).GetField("c_ComfortRadius", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
	}

	private void Awake()
	{
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Expected O, but got Unknown
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Expected O, but got Unknown
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Expected O, but got Unknown
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Expected O, but got Unknown
		BaseRestTime = ((BaseUnityPlugin)this).Config.Bind<float>("Comfort", "BaseRestTime", 480f, "The base time of the rested buff, in seconds. Game default 480.");
		BaseRestTime.SettingChanged += BaseRestTime_SettingChanged;
		RestTimePerComfort = ((BaseUnityPlugin)this).Config.Bind<float>("Comfort", "RestTimePerComfort", 60f, "The time to add to the rested buff, in seconds, for each comfort level beyond 1. Game default 60.");
		RestTimePerComfort.SettingChanged += RestTimePerComfort_SettingChanged;
		ComfortItemRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Comfort", "ComfortItemRadius", 10f, "The range at which comforting items will affect the comfort level of players. Game default 10.");
		ComfortItemRadius.SettingChanged += ComfortItemRadius_SettingChanged;
		FireItemRadiusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Comfort", "FireItemRadiusMultiplier", 1f, "A multiplier to apply to the range at which items provide the \"Fire\" buff. Game default 1. Very high values may cause performance issues.");
		FireItemRadiusMultiplier.SettingChanged += FireItemRadiusMultiplier_SettingChanged;
		ClampConfig();
		mFireRadiusMultiplier = FireItemRadiusMultiplier.Value;
		sFejdStartupHarmony = new Harmony("dev.crystal.comfortable_FejdStartup");
		sPlayerHarmony = new Harmony("dev.crystal.comfortable_Player");
		sSERestedHarmony = new Harmony("dev.crystal.comfortable_SE_Rested");
		sEffectAreaHarmony = new Harmony("dev.crystal.comfortable_EffectArea");
		sFejdStartupHarmony.PatchAll(typeof(FejdStartup_Patches));
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
		sSERestedHarmony.PatchAll(typeof(SE_Rested_Patches));
		sEffectAreaHarmony.PatchAll(typeof(EffectArea_Patches));
	}

	private void OnDestroy()
	{
		sFejdStartupHarmony.UnpatchSelf();
		sPlayerHarmony.UnpatchSelf();
		sSERestedHarmony.UnpatchSelf();
		sEffectAreaHarmony.UnpatchSelf();
	}

	private void BaseRestTime_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (SE_Rested allRestedEffect in GetAllRestedEffects())
		{
			allRestedEffect.m_baseTTL = BaseRestTime.Value;
		}
	}

	private void RestTimePerComfort_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (SE_Rested allRestedEffect in GetAllRestedEffects())
		{
			allRestedEffect.m_TTLPerComfortLevel = RestTimePerComfort.Value;
		}
	}

	private void ComfortItemRadius_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		sSERestedHarmony.UnpatchSelf();
		sSERestedHarmony.PatchAll(typeof(SE_Rested_Patches));
	}

	private void FireItemRadiusMultiplier_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (EffectArea allArea in EffectArea.GetAllAreas())
		{
			UpdateFireEffectRadius(allArea, mFireRadiusMultiplier, FireItemRadiusMultiplier.Value);
		}
		mFireRadiusMultiplier = FireItemRadiusMultiplier.Value;
	}

	private static void ClampConfig()
	{
		if (BaseRestTime.Value < 0f)
		{
			BaseRestTime.Value = 0f;
		}
		if (BaseRestTime.Value > 36000f)
		{
			BaseRestTime.Value = 36000f;
		}
		if (RestTimePerComfort.Value < 0f)
		{
			RestTimePerComfort.Value = 0f;
		}
		if (RestTimePerComfort.Value > 3600f)
		{
			RestTimePerComfort.Value = 3600f;
		}
		if (ComfortItemRadius.Value < 0f)
		{
			ComfortItemRadius.Value = 0f;
		}
		if (ComfortItemRadius.Value > 1000f)
		{
			ComfortItemRadius.Value = 1000f;
		}
		if (FireItemRadiusMultiplier.Value < 0.01f)
		{
			FireItemRadiusMultiplier.Value = 0.01f;
		}
		if (FireItemRadiusMultiplier.Value > 10f)
		{
			FireItemRadiusMultiplier.Value = 10f;
		}
	}

	private static void OnObjectDBSetup()
	{
		SE_Rested restedEffect = GetRestedEffect();
		if ((Object)(object)restedEffect != (Object)null)
		{
			restedEffect.m_baseTTL = BaseRestTime.Value;
			restedEffect.m_TTLPerComfortLevel = RestTimePerComfort.Value;
		}
		else
		{
			Debug.LogError((object)"Comfortable: Could not locate SE_Rested effect object. This mod will not function properly.");
		}
	}

	[IteratorStateMachine(typeof(<GetAllRestedEffects>d__21))]
	private static IEnumerable<SE_Rested> GetAllRestedEffects()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <GetAllRestedEffects>d__21(-2);
	}

	private static SE_Rested GetRestedEffect()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		ObjectDB instance = ObjectDB.instance;
		return (SE_Rested)((instance != null) ? instance.GetStatusEffect(SEMan.s_statusEffectRested) : null);
	}

	private static SE_Rested GetRestedEffect(Player player)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		return (SE_Rested)((Character)player).GetSEMan().GetStatusEffect(SEMan.s_statusEffectRested);
	}

	private static void UpdateFireEffectRadius(EffectArea area, float oldMultiplier, float newMultiplier)
	{
		//IL_0001: 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)
		if ((area.m_type & 1) != 0)
		{
			Collider component = ((Component)area).GetComponent<Collider>();
			SphereCollider val = (SphereCollider)(object)((component is SphereCollider) ? component : null);
			if ((Object)(object)val != (Object)null)
			{
				val.radius = val.radius / oldMultiplier * newMultiplier;
			}
		}
	}
}