Decompiled source of InteriorFogConfig v2.0.0

Tomatobird.InteriorFogConfig.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using InteriorFogConfig.Extensions;
using InteriorFogConfig.MonoBehaviours;
using InteriorFogConfig.Patches;
using InteriorFogConfig.Utils;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

[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("Tomatobird.InteriorFogConfig")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0+4a1a81141dc91ee41d5b7d5c26b01de6b6d25447")]
[assembly: AssemblyProduct("InteriorFogConfig")]
[assembly: AssemblyTitle("Tomatobird.InteriorFogConfig")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 InteriorFogConfig
{
	public class Configuration
	{
		public string name = "";

		public bool configEnabled;

		public ConfigEntry<bool> fogEnabled;

		public ConfigEntry<bool> forceFog;

		public ConfigEntry<int> fogThinness;

		public ConfigEntry<bool> randomFogThinness;

		public ConfigEntry<int> minThinness;

		public ConfigEntry<int> maxThinness;

		public ConfigEntry<bool> changeFogColor;

		public ConfigEntry<string> fogColor;

		public ConfigEntry<float> fogAlpha;

		public ConfigEntry<bool> fogOnFoggy;

		public ConfigEntry<bool> animateFogThinness;

		public ConfigEntry<string> fogThinnessAnimation;

		public Configuration(string name, bool configEnabled, ConfigEntry<bool> fogEnabled, ConfigEntry<bool> forceFog, ConfigEntry<int> fogThinness, ConfigEntry<bool> randomFogThinness, ConfigEntry<int> minThinness, ConfigEntry<int> maxThinness, ConfigEntry<bool> changeFogColor, ConfigEntry<string> fogColor, ConfigEntry<float> fogAlpha, ConfigEntry<bool> fogOnFoggy, ConfigEntry<bool> animateFogThinness, ConfigEntry<string> fogThinnessAnimation)
		{
			this.name = name;
			this.configEnabled = configEnabled;
			this.forceFog = forceFog;
			this.fogEnabled = fogEnabled;
			this.fogThinness = fogThinness;
			this.randomFogThinness = randomFogThinness;
			this.minThinness = minThinness;
			this.maxThinness = maxThinness;
			this.changeFogColor = changeFogColor;
			this.fogColor = fogColor;
			this.fogAlpha = fogAlpha;
			this.fogOnFoggy = fogOnFoggy;
			this.animateFogThinness = animateFogThinness;
			this.fogThinnessAnimation = fogThinnessAnimation;
		}
	}
	[BepInPlugin("Tomatobird.InteriorFogConfig", "InteriorFogConfig", "2.0.0")]
	public class InteriorFogConfig : BaseUnityPlugin
	{
		public static List<Configuration> configurations = new List<Configuration>();

		public static InteriorFogConfig Instance { get; private set; } = null;

		internal static ManualLogSource Logger { get; private set; } = null;

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			configurations.Add(GenerateConfiguration("default", forceEnabled: true));
			Patch();
			Logger.LogInfo((object)"Tomatobird.InteriorFogConfig v2.0.0 has loaded!");
		}

		internal static Configuration GenerateConfiguration(string category, bool forceEnabled)
		{
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Expected O, but got Unknown
			return new Configuration(category, forceEnabled || ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "ConfigEnabled", false, "Should this configuration be used for the moon? Uses defaults if false.").Value, ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "FogEnabled", true, "Should interior fog be enabled? Interior fog will spawn randomly as determined by the game."), ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "ForceFog", false, "Should interior fog be forced to spawn?"), ((BaseUnityPlugin)Instance).Config.Bind<int>(category, "FogThinness", 5, "Control the density of the fog. Higher values make fog thinner. Ignored if RandomFogThinness is used."), ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "RandomFogThinness", false, "Make the density of the fog random per each round. Note that this isn't network synchronized and in multiplayer players will see different fog density."), ((BaseUnityPlugin)Instance).Config.Bind<int>(category, "MinThinness", 3, "Minimum thinness of the fog. The densest the fog will get. Inclusive."), ((BaseUnityPlugin)Instance).Config.Bind<int>(category, "MaxThinness", 8, "Maximum thinness of the fog. The least dense the fog will get. Exclusive. Set this higher than MinThinness"), ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "ChangeFogColor", false, "Should the fog color be changed?"), ((BaseUnityPlugin)Instance).Config.Bind<string>(category, "FogColor", "#261710", "Color of the interior fog."), ((BaseUnityPlugin)Instance).Config.Bind<float>(category, "FogAlpha", 1f, new ConfigDescription("Alpha value of the fog. 0 is fully transparent, 1 is fully opaque. Vanilla uses value of 1.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>())), ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "FogOnFoggy", false, "Should interior fog be always enabled if the weather is foggy?"), ((BaseUnityPlugin)Instance).Config.Bind<bool>(category, "AnimateFogThinness", false, "Should interior fog density change over time?"), ((BaseUnityPlugin)Instance).Config.Bind<string>(category, "FogThinnessAnimation", "", "Define an animation curve that multiplies the set fog thinness with a value from the curve depending on normalize time of day. First value is the normalized time, second value is the fog multiplier. Example: 0.0,2.0 ; 0.2,1.5 ; 0.4,0.8 ; 0.6,0.5 ; 0.8,1.0 ; 1.0,3.0 (You can also define in/out tangets and weights for each key like so: 0.4,0.2,1.1,2,0.4,0.6 (Check Keyframe declaration in unity documentation!))"));
		}

		internal static void Patch()
		{
			//IL_000c: 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_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Tomatobird.InteriorFogConfig");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Tomatobird.InteriorFogConfig";

		public const string PLUGIN_NAME = "InteriorFogConfig";

		public const string PLUGIN_VERSION = "2.0.0";
	}
}
namespace InteriorFogConfig.Utils
{
	internal class CurveUtil
	{
		internal static AnimationCurve CreateCurve(string s)
		{
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			string[] array = s.Split(';');
			if (array.Length == 1 && float.TryParse(array[0], out var result))
			{
				return AnimationCurve.Constant(0f, 1f, result);
			}
			List<Keyframe> list = new List<Keyframe>();
			string[] array2 = array;
			foreach (string text in array2)
			{
				string[] array3 = text.Split(",");
				if (array3.Length == 2)
				{
					string s2 = array3[0].Trim();
					string s3 = array3[1].Trim();
					if (float.TryParse(s2, out var result2) && float.TryParse(s3, out var result3))
					{
						list.Add(new Keyframe(result2, result3));
					}
				}
				else if (array3.Length == 6)
				{
					string s4 = array3[0].Trim();
					string s5 = array3[1].Trim();
					string s6 = array3[2].Trim();
					string s7 = array3[3].Trim();
					string s8 = array3[4].Trim();
					string s9 = array3[5].Trim();
					if (float.TryParse(s4, out var result4) && float.TryParse(s5, out var result5) && float.TryParse(s6, out var result6) && float.TryParse(s7, out var result7) && float.TryParse(s8, out var result8) && float.TryParse(s9, out var result9))
					{
						list.Add(new Keyframe(result4, result5, result6, result7, result8, result9));
					}
				}
			}
			return new AnimationCurve(list.ToArray());
		}
	}
}
namespace InteriorFogConfig.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class ConfigurationGenerator
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		internal static void GenerateConfigurations(StartOfRound __instance)
		{
			InteriorFogConfig.Logger.LogDebug((object)"Generating configurations for each moon.");
			SelectableLevel[] levels = __instance.levels;
			for (int i = 0; i < levels.Length; i++)
			{
				if (levels[i].spawnEnemiesAndScrap)
				{
					InteriorFogConfig.configurations.Add(InteriorFogConfig.GenerateConfiguration("Moon: " + levels[i].PlanetName, forceEnabled: false));
				}
			}
		}
	}
	[HarmonyPatch]
	public class InteriorFogPatch
	{
		public static int currentIndex;

		[HarmonyPatch(typeof(RoundManager), "RefreshEnemiesList")]
		[HarmonyPostfix]
		private static void ApplyNewFogValues(RoundManager __instance)
		{
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Invalid comparison between Unknown and I4
			//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance.indoorFog == (Object)null)
			{
				return;
			}
			bool flag = false;
			for (int i = 0; i < InteriorFogConfig.configurations.Count; i++)
			{
				if (!(InteriorFogConfig.configurations[i].name != "Moon: " + RoundManager.Instance.currentLevel.PlanetName))
				{
					if (InteriorFogConfig.configurations[i].configEnabled)
					{
						currentIndex = i;
					}
					else
					{
						currentIndex = 0;
					}
					flag = true;
					InteriorFogConfig.Logger.LogInfo((object)("Configuration found for " + RoundManager.Instance.currentLevel.PlanetName + "! " + (InteriorFogConfig.configurations[i].configEnabled ? "Using moon specific configuration." : "Using default configuration.")));
					break;
				}
			}
			if (!flag)
			{
				currentIndex = 0;
				InteriorFogConfig.Logger.LogWarning((object)("Configuration for " + RoundManager.Instance.currentLevel.PlanetName + " was not found! Using defaults..."));
			}
			if (!InteriorFogConfig.configurations[currentIndex].fogEnabled.Value)
			{
				((Component)__instance.indoorFog).gameObject.SetActive(false);
				InteriorFogConfig.Logger.LogDebug((object)"Set interior fog to not active.");
				return;
			}
			if (InteriorFogConfig.configurations[currentIndex].forceFog.Value || (InteriorFogConfig.configurations[currentIndex].fogOnFoggy.Value && (int)RoundManager.Instance.currentLevel.currentWeather == 3))
			{
				((Component)__instance.indoorFog).gameObject.SetActive(true);
				InteriorFogConfig.Logger.LogDebug((object)"Set interior fog to active.");
			}
			if (InteriorFogConfig.configurations[currentIndex].randomFogThinness.Value)
			{
				Random random = new Random(StartOfRound.Instance.randomMapSeed + 69);
				float meanFreePath = random.NextFloat(InteriorFogConfig.configurations[currentIndex].minThinness.Value, InteriorFogConfig.configurations[currentIndex].maxThinness.Value);
				InteriorFogConfig.Logger.LogDebug((object)("Random fog thinness value: " + meanFreePath));
				__instance.indoorFog.parameters.meanFreePath = meanFreePath;
			}
			else
			{
				InteriorFogConfig.Logger.LogDebug((object)("Set fog thinness to:" + InteriorFogConfig.configurations[currentIndex].fogThinness.Value));
				__instance.indoorFog.parameters.meanFreePath = InteriorFogConfig.configurations[currentIndex].fogThinness.Value;
			}
			if (InteriorFogConfig.configurations[currentIndex].changeFogColor.Value)
			{
				InteriorFogConfig.Logger.LogDebug((object)"Changing interior fog color.");
				Color? val = ParseColor(InteriorFogConfig.configurations[currentIndex].fogColor.Value);
				if (val.HasValue)
				{
					__instance.indoorFog.parameters.albedo = val.Value;
					__instance.indoorFog.parameters.albedo.a = InteriorFogConfig.configurations[currentIndex].fogAlpha.Value;
				}
			}
			FogDensityAnimator fogDensityAnimator3 = default(FogDensityAnimator);
			if (InteriorFogConfig.configurations[currentIndex].animateFogThinness.Value)
			{
				if (InteriorFogConfig.configurations[currentIndex].fogThinnessAnimation.Value == "")
				{
					InteriorFogConfig.Logger.LogWarning((object)"Animation was enabled but no animation was defined!");
					FogDensityAnimator fogDensityAnimator = default(FogDensityAnimator);
					if (((Component)__instance.indoorFog).gameObject.TryGetComponent<FogDensityAnimator>(ref fogDensityAnimator))
					{
						InteriorFogConfig.Logger.LogDebug((object)"Destroying fog animator.");
						fogDensityAnimator.DestroySelf();
					}
				}
				else
				{
					FogDensityAnimator fogDensityAnimator2 = default(FogDensityAnimator);
					if (!((Component)__instance.indoorFog).gameObject.TryGetComponent<FogDensityAnimator>(ref fogDensityAnimator2))
					{
						fogDensityAnimator2 = ((Component)__instance.indoorFog).gameObject.AddComponent<FogDensityAnimator>();
					}
					InteriorFogConfig.Logger.LogDebug((object)"Adding fog animator.");
					fogDensityAnimator2.fogAnimation = CurveUtil.CreateCurve(InteriorFogConfig.configurations[currentIndex].fogThinnessAnimation.Value);
					fogDensityAnimator2.fogDensity = __instance.indoorFog.parameters.meanFreePath;
				}
			}
			else if (((Component)__instance.indoorFog).gameObject.TryGetComponent<FogDensityAnimator>(ref fogDensityAnimator3))
			{
				InteriorFogConfig.Logger.LogDebug((object)"Destroying fog animator.");
				fogDensityAnimator3.DestroySelf();
			}
			InteriorFogConfig.Logger.LogInfo((object)"Indoor fog changes done.");
		}

		[HarmonyPatch(typeof(StartOfRound), "EndOfGame")]
		[HarmonyPostfix]
		private static void KillAnimator()
		{
			FogDensityAnimator fogDensityAnimator = default(FogDensityAnimator);
			if (((Component)RoundManager.Instance.indoorFog).gameObject.TryGetComponent<FogDensityAnimator>(ref fogDensityAnimator))
			{
				InteriorFogConfig.Logger.LogDebug((object)"Destroying fog animator.");
				fogDensityAnimator.DestroySelf();
			}
		}

		private static Color? ParseColor(string colorString)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			Color value = default(Color);
			if (ColorUtility.TryParseHtmlString(colorString, ref value))
			{
				return value;
			}
			InteriorFogConfig.Logger.LogWarning((object)"Couldn't parse color string! Make sure the formatting is correct.");
			return null;
		}
	}
	[HarmonyPatch(typeof(NetworkManager))]
	internal static class RegisterNetworkPrefabPatch
	{
		private static readonly string MOD_GUID = "Tomatobird.InteriorFogConfig";

		[HarmonyPatch("SetSingleton")]
		[HarmonyPostfix]
		private static void RegisterPrefab()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(MOD_GUID + " Prefab");
			((Object)val).hideFlags = (HideFlags)(((Object)val).hideFlags | 0x3D);
			Object.DontDestroyOnLoad((Object)(object)val);
			NetworkObject obj = val.AddComponent<NetworkObject>();
			FieldInfo field = typeof(NetworkObject).GetField("GlobalObjectIdHash", BindingFlags.Instance | BindingFlags.NonPublic);
			field.SetValue(obj, GetHash(MOD_GUID));
			NetworkManager.Singleton.PrefabHandler.AddNetworkPrefab(val);
			static uint GetHash(string value)
			{
				return value?.Aggregate(17u, (uint current, char c) => (current * 31) ^ c) ?? 0;
			}
		}
	}
}
namespace InteriorFogConfig.MonoBehaviours
{
	internal class FogDensityAnimator : MonoBehaviour
	{
		public AnimationCurve? fogAnimation;

		public float fogDensity = 5f;

		private LocalVolumetricFog? fog;

		private void Awake()
		{
			if ((Object)(object)TimeOfDay.Instance == (Object)null || !((Component)this).gameObject.activeInHierarchy)
			{
				Object.Destroy((Object)(object)this);
			}
		}

		private void Start()
		{
			fog = ((Component)this).GetComponent<LocalVolumetricFog>();
		}

		public void DestroySelf()
		{
			Object.Destroy((Object)(object)this);
		}

		private void Update()
		{
			if (InteriorFogConfig.configurations[InteriorFogPatch.currentIndex].animateFogThinness.Value && fogAnimation != null)
			{
				fog.parameters.meanFreePath = fogDensity * fogAnimation.Evaluate(TimeOfDay.Instance.normalizedTimeOfDay);
			}
		}
	}
}
namespace InteriorFogConfig.Extensions
{
	public static class RandomExtensions
	{
		public static T NextEnum<T>(this Random random) where T : struct, Enum
		{
			Array values = Enum.GetValues(typeof(T));
			return (T)values.GetValue(random.Next(values.Length));
		}

		public static T NextItem<T>(this Random random, List<T> collection)
		{
			int index = random.Next(collection.Count);
			return collection[index];
		}

		public static T NextItem<T>(this Random random, T[] collection)
		{
			int num = random.Next(collection.Length);
			return collection[num];
		}

		public static double NextDouble(this Random random, double min, double max)
		{
			return random.NextDouble() * (max - min) + min;
		}

		public static float NextFloat(this Random random, float min, float max)
		{
			return (float)random.NextDouble(min, max);
		}

		public static bool NextBool(this Random random)
		{
			return random.Next(2) == 0;
		}

		public static int NextSign(this Random random)
		{
			if (!random.NextBool())
			{
				return -1;
			}
			return 1;
		}

		public static Quaternion NextQuaternion(this Random random)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			return Quaternion.Euler(random.NextFloat(0f, 360f), random.NextFloat(0f, 360f), random.NextFloat(0f, 360f));
		}
	}
}