Decompiled source of ComfortRadius Ymiron v1.0.1

ComfortRadius.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("ComfortRadius")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("ComfortRadius")]
[assembly: AssemblyTitle("ComfortRadius")]
[assembly: AssemblyVersion("1.0.1.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 ComfortRadius
{
	internal static class ModConfig
	{
		internal const float VanillaRadius = 10f;

		internal static ConfigEntry<float> Radius;

		internal static void Bind(ConfigFile cfg)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			Radius = cfg.Bind<float>("1 - Comfort", "Radius", 20f, new ConfigDescription("Distance (in meters) within which comfort pieces (beds, chairs, banners, fires...) count towards the comfort level. The vanilla game uses 10. Can be changed while the game is running: comfort is recalculated every few seconds. Values above ~60 m make little difference, because distant pieces are not loaded.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 100f), Array.Empty<object>()));
		}
	}
	[HarmonyPatch(typeof(SE_Rested), "GetNearbyComfortPieces")]
	internal static class ComfortRadiusPatch
	{
		private static readonly MethodInfo RadiusGetter = AccessTools.Method(typeof(ComfortRadiusPatch), "GetRadius", (Type[])null, (Type[])null);

		private static float GetRadius()
		{
			return ModConfig.Radius.Value;
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int num = 0;
			foreach (CodeInstruction item in list)
			{
				if (item.opcode == OpCodes.Ldc_R4 && item.operand is float num2 && num2 == 10f)
				{
					item.opcode = OpCodes.Call;
					item.operand = RadiusGetter;
					num++;
				}
			}
			if (num == 1)
			{
				ComfortRadiusPlugin.Log.LogInfo((object)"Comfort radius hooked to the config.");
			}
			else
			{
				ComfortRadiusPlugin.Log.LogWarning((object)($"Expected a single 10f in SE_Rested.GetNearbyComfortPieces, found {num}: " + "a game update may have changed the code, the mod needs checking."));
			}
			return list;
		}
	}
	[BepInPlugin("ymiron.valheim.comfortradius", "ComfortRadius-Ymiron", "1.0.1")]
	[BepInProcess("valheim.exe")]
	public class ComfortRadiusPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "ymiron.valheim.comfortradius";

		public const string PluginName = "ComfortRadius-Ymiron";

		public const string PluginVersion = "1.0.1";

		private Harmony _harmony;

		private FileSystemWatcher _watcher;

		private volatile bool _configChanged;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ModConfig.Bind(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("ymiron.valheim.comfortradius");
			_harmony.PatchAll(typeof(ComfortRadiusPlugin).Assembly);
			WatchConfigFile();
			Log.LogInfo((object)string.Format("{0} {1} loaded, comfort radius {2} m.", "ComfortRadius-Ymiron", "1.0.1", ModConfig.Radius.Value));
		}

		private void WatchConfigFile()
		{
			_watcher = new FileSystemWatcher(Path.GetDirectoryName(((BaseUnityPlugin)this).Config.ConfigFilePath), Path.GetFileName(((BaseUnityPlugin)this).Config.ConfigFilePath))
			{
				NotifyFilter = (NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite),
				IncludeSubdirectories = false
			};
			_watcher.Changed += delegate
			{
				_configChanged = true;
			};
			_watcher.Created += delegate
			{
				_configChanged = true;
			};
			_watcher.Renamed += delegate
			{
				_configChanged = true;
			};
			_watcher.EnableRaisingEvents = true;
		}

		private void Update()
		{
			if (!_configChanged)
			{
				return;
			}
			_configChanged = false;
			try
			{
				((BaseUnityPlugin)this).Config.Reload();
				Log.LogInfo((object)$"Config reloaded: comfort radius {ModConfig.Radius.Value} m.");
			}
			catch (IOException)
			{
				_configChanged = true;
			}
		}

		private void OnDestroy()
		{
			_watcher?.Dispose();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}