Decompiled source of IndoorWeather v1.0.0

IndoorWeather.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("IndoorWeather")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("IndoorWeather")]
[assembly: AssemblyTitle("IndoorWeather")]
[assembly: AssemblyVersion("1.0.0.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 IndoorWeather
{
	[BepInPlugin("snowfarm.valheim.indoorweather", "IndoorWeather", "1.0.0")]
	public class IndoorWeatherPlugin : BaseUnityPlugin
	{
		public const string Guid = "snowfarm.valheim.indoorweather";

		public const string Name = "IndoorWeather";

		public const string Version = "1.0.0";

		private static ManualLogSource _log;

		private static ConfigEntry<bool> _enabled;

		private static ConfigEntry<string> _biomeList;

		private static ConfigEntry<float> _fogMultiplier;

		private static ConfigEntry<bool> _hideParticles;

		private static ConfigEntry<float> _coverThreshold;

		private static readonly HashSet<Biome> Biomes = new HashSet<Biome>();

		private static readonly FieldRef<Player, bool> UnderRoof = AccessTools.FieldRefAccess<Player, bool>("m_underRoof");

		private static readonly FieldRef<Player, float> Cover = AccessTools.FieldRefAccess<Player, float>("m_coverPercentage");

		private static readonly FieldRef<EnvMan, GameObject[]> CurrentPSystems = AccessTools.FieldRefAccess<EnvMan, GameObject[]>("m_currentPSystems");

		private static MethodInfo _setParticleArray;

		private static float _savedFog = -1f;

		private static bool _particlesHidden;

		private void Awake()
		{
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			_log = ((BaseUnityPlugin)this).Logger;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Turn the plugin on or off.");
			_biomeList = ((BaseUnityPlugin)this).Config.Bind<string>("1 - General", "Biomes", "Mountain,DeepNorth", "Only apply in these biomes (comma separated Heightmap.Biome names).");
			_fogMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Fog", "FogMultiplier", 0.05f, new ConfigDescription("Multiply the weather fog density by this while indoors. 0 = no fog at all.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_hideParticles = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Particles", "HideParticles", true, "Also stop the environment particle systems (drifting snow) while indoors.");
			_coverThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("4 - Shelter", "CoverThreshold", 0.5f, new ConfigDescription("Cover percentage that counts as 'indoors' (Player.m_underRoof also counts).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			Biomes.Clear();
			string[] array = (_biomeList.Value ?? "").Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length != 0)
				{
					if (Enum.TryParse<Biome>(text, ignoreCase: true, out Biome result))
					{
						Biomes.Add(result);
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)("Unknown biome '" + text + "'"));
					}
				}
			}
			_setParticleArray = AccessTools.Method(typeof(EnvMan), "SetParticleArrayEnabled", (Type[])null, (Type[])null);
			new Harmony("snowfarm.valheim.indoorweather").PatchAll(typeof(IndoorWeatherPlugin));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("IndoorWeather 1.0.0 loaded. biomes: " + string.Join(", ", Biomes)));
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(EnvMan), "Update")]
		private static void EnvManUpdatePostfix()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			if (!_enabled.Value)
			{
				Restore();
				return;
			}
			EnvMan instance = EnvMan.instance;
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)instance == (Object)null || (Object)(object)localPlayer == (Object)null)
			{
				Restore();
				return;
			}
			bool flag2;
			try
			{
				bool num = Biomes.Contains(instance.GetCurrentBiome());
				bool flag = UnderRoof.Invoke(localPlayer) || Cover.Invoke(localPlayer) >= _coverThreshold.Value;
				flag2 = num && flag;
			}
			catch (Exception ex)
			{
				_log.LogDebug((object)("Weather check failed: " + ex.Message));
				return;
			}
			ApplyFog(flag2);
			if (_hideParticles.Value)
			{
				ApplyParticles(instance, flag2);
			}
			else if (_particlesHidden)
			{
				ApplyParticles(instance, hide: false);
			}
		}

		private static void ApplyFog(bool active)
		{
			if (active)
			{
				if (_savedFog < 0f)
				{
					_savedFog = RenderSettings.fogDensity;
				}
				float num = _savedFog * _fogMultiplier.Value;
				if (Mathf.Abs(RenderSettings.fogDensity - num) > 1E-05f)
				{
					_savedFog = RenderSettings.fogDensity;
					num = _savedFog * _fogMultiplier.Value;
				}
				RenderSettings.fogDensity = num;
			}
			else
			{
				RestoreFog();
			}
		}

		private static void RestoreFog()
		{
			if (!(_savedFog < 0f))
			{
				RenderSettings.fogDensity = _savedFog;
				_savedFog = -1f;
			}
		}

		private static void ApplyParticles(EnvMan env, bool hide)
		{
			if (_setParticleArray == null)
			{
				return;
			}
			GameObject[] array = CurrentPSystems.Invoke(env);
			if (array == null)
			{
				return;
			}
			if (hide)
			{
				bool flag = false;
				GameObject[] array2 = array;
				foreach (GameObject val in array2)
				{
					if ((Object)(object)val != (Object)null && val.activeSelf)
					{
						flag = true;
						break;
					}
				}
				if (!_particlesHidden || flag)
				{
					_setParticleArray.Invoke(env, new object[2] { array, false });
					_particlesHidden = true;
				}
			}
			else if (_particlesHidden)
			{
				_setParticleArray.Invoke(env, new object[2] { array, true });
				_particlesHidden = false;
			}
		}

		private static void Restore()
		{
			RestoreFog();
			if (_particlesHidden && (Object)(object)EnvMan.instance != (Object)null)
			{
				ApplyParticles(EnvMan.instance, hide: false);
			}
		}
	}
}