Decompiled source of BetterFog v1.0.3

BetterFog.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BetterFog.Config;
using BetterFog.Features;
using BetterFog.Infrastructure;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BetterFog")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+d54e1bedb5d240709cab10532152cafc1dfd7cdc")]
[assembly: AssemblyProduct("BetterFog")]
[assembly: AssemblyTitle("BetterFog")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
[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 BetterFog
{
	[BepInPlugin("JevMods.BetterFog", "BetterFog", "1.0.3")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "JevMods.BetterFog";

		public const string PluginName = "BetterFog";

		private IFeature[] _features;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			GameEvents events = (GameEvents.Instance = new GameEvents());
			ModSettings settings = new ModSettings(((BaseUnityPlugin)this).Config);
			_features = new IFeature[3]
			{
				new DistanceFogFeature(events, settings),
				new MistFeature(events, settings),
				new SmokeFeature(events, settings)
			};
			IFeature[] features = _features;
			for (int i = 0; i < features.Length; i++)
			{
				features[i].Enable();
			}
			_harmony = new Harmony("JevMods.BetterFog");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			if (_features != null)
			{
				IFeature[] features = _features;
				for (int i = 0; i < features.Length; i++)
				{
					features[i].Disable();
				}
			}
			GameEvents.Instance = null;
		}
	}
	internal static class BuildInfo
	{
		public const string Version = "1.0.3";
	}
}
namespace BetterFog.Patches
{
	internal static class FogPatches
	{
		[HarmonyPatch(typeof(EnvMan), "SetEnv")]
		private static class SetEnv
		{
			private static void Postfix()
			{
				if ((Object)(object)Camera.main != (Object)null)
				{
					GameEvents.Instance?.RaiseEnvironmentApplied();
				}
			}
		}

		[HarmonyPatch(typeof(ParticleMist), "Awake")]
		private static class MistAwake
		{
			private static void Postfix(ParticleMist __instance)
			{
				GameEvents.Instance?.RaiseMistCreated(__instance);
			}
		}

		[HarmonyPatch(typeof(DistantFogEmitter), "Update")]
		private static class FogEmitterUpdate
		{
			private static void Prefix(DistantFogEmitter __instance)
			{
				GameEvents.Instance?.RaiseFogEmitterUpdating(__instance);
			}
		}
	}
	internal static class SmokePatches
	{
		[HarmonyPatch(typeof(Smoke), "GetAlpha")]
		private static class GetAlpha
		{
			private static void Postfix(ref float __result)
			{
				GameEvents.Instance?.RaiseSmokeAlphaCalculated(ref __result);
			}
		}
	}
}
namespace BetterFog.Infrastructure
{
	public sealed class GameEvents : IGameEvents
	{
		internal static GameEvents Instance { get; set; }

		public event Action EnvironmentApplied;

		public event Action<ParticleMist> MistCreated;

		public event Action<DistantFogEmitter> FogEmitterUpdating;

		public event SmokeAlphaHandler SmokeAlphaCalculated;

		internal void RaiseEnvironmentApplied()
		{
			this.EnvironmentApplied?.Invoke();
		}

		internal void RaiseMistCreated(ParticleMist mist)
		{
			this.MistCreated?.Invoke(mist);
		}

		internal void RaiseFogEmitterUpdating(DistantFogEmitter emitter)
		{
			this.FogEmitterUpdating?.Invoke(emitter);
		}

		internal void RaiseSmokeAlphaCalculated(ref float alpha)
		{
			this.SmokeAlphaCalculated?.Invoke(ref alpha);
		}
	}
	public interface IGameEvents
	{
		event Action EnvironmentApplied;

		event Action<ParticleMist> MistCreated;

		event Action<DistantFogEmitter> FogEmitterUpdating;

		event SmokeAlphaHandler SmokeAlphaCalculated;
	}
	internal static class ParticleFade
	{
		private const string ColorProperty = "_Color";

		public static void Scale(ParticleSystem system, float factor)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			Material material = ((Renderer)((Component)system).GetComponent<ParticleSystemRenderer>()).material;
			if (material.HasProperty("_Color"))
			{
				material.SetColor("_Color", Faded(material.GetColor("_Color"), factor));
			}
			else
			{
				ScaleStartColor(system, factor);
			}
		}

		private static void ScaleStartColor(ParticleSystem system, float factor)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			MainModule main = system.main;
			MinMaxGradient startColor = ((MainModule)(ref main)).startColor;
			ParticleSystemGradientMode mode = ((MinMaxGradient)(ref startColor)).mode;
			if ((int)mode != 0)
			{
				if ((int)mode != 2)
				{
					return;
				}
				((MinMaxGradient)(ref startColor)).colorMin = Faded(((MinMaxGradient)(ref startColor)).colorMin, factor);
				((MinMaxGradient)(ref startColor)).colorMax = Faded(((MinMaxGradient)(ref startColor)).colorMax, factor);
			}
			else
			{
				((MinMaxGradient)(ref startColor)).color = Faded(((MinMaxGradient)(ref startColor)).color, factor);
			}
			((MainModule)(ref main)).startColor = startColor;
		}

		private static Color Faded(Color color, float factor)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			color.a *= factor;
			return color;
		}
	}
	public delegate void SmokeAlphaHandler(ref float alpha);
}
namespace BetterFog.Features
{
	public sealed class DistanceFogFeature : IFeature
	{
		private readonly IGameEvents _events;

		private readonly IModSettings _settings;

		public DistanceFogFeature(IGameEvents events, IModSettings settings)
		{
			_events = events;
			_settings = settings;
		}

		public void Enable()
		{
			_events.EnvironmentApplied += OnEnvironmentApplied;
		}

		public void Disable()
		{
			_events.EnvironmentApplied -= OnEnvironmentApplied;
		}

		private void OnEnvironmentApplied()
		{
			RenderSettings.fogDensity *= _settings.FogDensity;
		}
	}
	public interface IFeature
	{
		void Enable();

		void Disable();
	}
	public sealed class MistFeature : IFeature
	{
		private readonly IGameEvents _events;

		private readonly IModSettings _settings;

		private readonly HashSet<ParticleSystem> _faded = new HashSet<ParticleSystem>();

		private GameObject[] _weatherSystems;

		public MistFeature(IGameEvents events, IModSettings settings)
		{
			_events = events;
			_settings = settings;
		}

		public void Enable()
		{
			_events.EnvironmentApplied += OnEnvironmentApplied;
			_events.MistCreated += OnMistCreated;
			_events.FogEmitterUpdating += OnFogEmitterUpdating;
		}

		public void Disable()
		{
			_events.EnvironmentApplied -= OnEnvironmentApplied;
			_events.MistCreated -= OnMistCreated;
			_events.FogEmitterUpdating -= OnFogEmitterUpdating;
		}

		private static bool IsFogLike(ParticleSystem system)
		{
			string name = ((Object)system).name;
			if (name.IndexOf("fog", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return name.IndexOf("mist", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		private void OnEnvironmentApplied()
		{
			GameObject[] currentPSystems = EnvMan.instance.m_currentPSystems;
			if (currentPSystems == null || currentPSystems == _weatherSystems)
			{
				return;
			}
			_weatherSystems = currentPSystems;
			GameObject[] array = currentPSystems;
			for (int i = 0; i < array.Length; i++)
			{
				ParticleSystem[] componentsInChildren = array[i].GetComponentsInChildren<ParticleSystem>(true);
				foreach (ParticleSystem system in componentsInChildren)
				{
					if (IsFogLike(system))
					{
						Fade(system);
					}
				}
			}
		}

		private void OnMistCreated(ParticleMist mist)
		{
			Fade(mist.m_ps);
		}

		private void OnFogEmitterUpdating(DistantFogEmitter emitter)
		{
			ParticleSystem[] psystems = emitter.m_psystems;
			foreach (ParticleSystem system in psystems)
			{
				Fade(system);
			}
		}

		private void Fade(ParticleSystem system)
		{
			if (_faded.Add(system))
			{
				ParticleFade.Scale(system, _settings.MistOpacity);
			}
		}
	}
	public sealed class SmokeFeature : IFeature
	{
		private readonly IGameEvents _events;

		private readonly IModSettings _settings;

		public SmokeFeature(IGameEvents events, IModSettings settings)
		{
			_events = events;
			_settings = settings;
		}

		public void Enable()
		{
			_events.SmokeAlphaCalculated += OnSmokeAlphaCalculated;
		}

		public void Disable()
		{
			_events.SmokeAlphaCalculated -= OnSmokeAlphaCalculated;
		}

		private void OnSmokeAlphaCalculated(ref float alpha)
		{
			alpha *= _settings.SmokeOpacity;
		}
	}
}
namespace BetterFog.Config
{
	public interface IModSettings
	{
		float FogDensity { get; }

		float MistOpacity { get; }

		float SmokeOpacity { get; }
	}
	public sealed class ModSettings : IModSettings
	{
		private readonly ConfigEntry<float> _fogDensity;

		private readonly ConfigEntry<float> _mistOpacity;

		private readonly ConfigEntry<float> _smokeOpacity;

		public float FogDensity => _fogDensity.Value;

		public float MistOpacity => _mistOpacity.Value;

		public float SmokeOpacity => _smokeOpacity.Value;

		public ModSettings(ConfigFile file)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Expected O, but got Unknown
			_fogDensity = file.Bind<float>("Fog", "Density", 0.5f, new ConfigDescription("Reduces the distance fog density to this fraction of vanilla, from 0 (no fog) to 1 (unchanged).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_mistOpacity = file.Bind<float>("Fog", "MistOpacity", 0.2f, new ConfigDescription("Reduces the opacity of the mist and fog particles (Mistlands mist, fog banks, weather fog) to this fraction of vanilla, from 0 (invisible) to 1 (unchanged). Restart the game after changing it.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_smokeOpacity = file.Bind<float>("Smoke", "Opacity", 0.5f, new ConfigDescription("Reduces the opacity of fire smoke to this fraction of vanilla, from 0 (invisible) to 1 (unchanged).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}