Decompiled source of Weather Gains v2.0.0

WeatherGains.dll

Decompiled 6 hours 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.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using WeatherGains.Types;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("WeatherGains")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0c97963c20d8636044683a071482cf56f891cf70")]
[assembly: AssemblyProduct("WeatherGains")]
[assembly: AssemblyTitle("WeatherGains")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 WeatherGains
{
	public static class MultiplierManager
	{
		public static int ApplyValueMultiplier(int initialValue)
		{
			if (!WeatherGains.BoundConfig.ValueMultiEnabled.Value)
			{
				return initialValue;
			}
			float num = GetMultiplierGroupForCurrentWeather().ValueMultiplier.Value;
			if (WeatherGains.BoundConfig.ValueMultiVariation.Value > 0f)
			{
				float value = WeatherGains.BoundConfig.ValueMultiVariation.Value;
				float num2 = 1f + Random.Range(0f - value, value) / 100f;
				num *= num2;
			}
			return (int)Mathf.Max((float)initialValue * num, 1f);
		}

		public static float ApplyAmountMultiplier(float initialAmount)
		{
			if (!WeatherGains.BoundConfig.AmountMultiEnabled.Value)
			{
				return initialAmount;
			}
			float value = GetMultiplierGroupForCurrentWeather().AmountMultiplier.Value;
			return Mathf.Max(initialAmount * value, 1f);
		}

		private static MultiValueGroup GetMultiplierGroupForCurrentWeather()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			return WeatherGains.BoundConfig.Multipliers[TimeOfDay.Instance.currentLevelWeather];
		}
	}
	public class PluginConfig
	{
		private const string SectionMultipliers = "Multipliers";

		private const string SectionGeneral = "General";

		private const int ConfigVersion = 2;

		public ConfigEntry<bool> LungValueMultiEnabled;

		public ConfigEntry<bool> AmountMultiEnabled;

		public ConfigEntry<bool> ValueMultiEnabled;

		public ConfigEntry<bool> CompatibilityMode;

		public ConfigEntry<bool> PluginEnabled;

		public ConfigEntry<float> ValueMultiVariation;

		public readonly Dictionary<LevelWeatherType, MultiValueGroup> Multipliers = new Dictionary<LevelWeatherType, MultiValueGroup>();

		public PluginConfig(ConfigFile config)
		{
			config.SaveOnConfigSet = true;
			ApplyConfig(config);
			ClearOrphanedEntries(config);
			config.Save();
		}

		private void ApplyConfig(ConfigFile config)
		{
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			config.Bind<int>("Meta", "ConfigVersion", 2, "!!! DO NOT CHANGE THIS VALUE !!!");
			LungValueMultiEnabled = config.Bind<bool>("General", "ApparatusMultiplierEnabled", true, "Toggles the value multiplier on the apparatus (lung).");
			ValueMultiVariation = config.Bind<float>("General", "ValueMultiVariation", 5f, "Sets the maximum random variation (in percent) for the value multiplier (makes scrap values more natural). Set to 0 to disable.");
			AmountMultiEnabled = config.Bind<bool>("General", "AmountMultiplierEnabled", true, "Toggles the amount multiplier on scrap.");
			ValueMultiEnabled = config.Bind<bool>("General", "ValueMultiplierEnabled", true, "Toggles the value multiplier on scrap.");
			CompatibilityMode = config.Bind<bool>("General", "CompatibilityMode", false, "Disables all transpilers for compatibility with other mods. This will prevent multipliers from being applied to bee hives.");
			PluginEnabled = config.Bind<bool>("General", "PluginEnabled", true, "Toggles the functionality of the plugin; an easy way to turn everything off!");
			foreach (LevelWeatherType value in Enum.GetValues(typeof(LevelWeatherType)))
			{
				Multipliers[value] = new MultiValueGroup
				{
					AmountMultiplier = BindMultiplier(config, value, "Amount", DefaultAmount(value)),
					ValueMultiplier = BindMultiplier(config, value, "Value", DefaultValue(value))
				};
			}
		}

		private static ConfigEntry<float> BindMultiplier(ConfigFile config, LevelWeatherType weather, string type, float defaultValue)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			return config.Bind<float>("Multipliers", $"{weather}{type}Multiplier", defaultValue, type + " multiplier applied during " + ((object)(LevelWeatherType)(ref weather)).ToString().ToLower() + " weather. Set to 1 to disable.");
		}

		private static float DefaultAmount(LevelWeatherType weather)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected I4, but got Unknown
			return (weather - 1) switch
			{
				2 => 1.1f, 
				0 => 1.1f, 
				1 => 1.2f, 
				3 => 1.2f, 
				4 => 1.3f, 
				_ => 1f, 
			};
		}

		private static float DefaultValue(LevelWeatherType weather)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected I4, but got Unknown
			return (weather - 1) switch
			{
				2 => 1.2f, 
				0 => 1.2f, 
				1 => 1.35f, 
				3 => 1.4f, 
				4 => 1.75f, 
				_ => 1f, 
			};
		}

		private static void ClearOrphanedEntries(ConfigFile config)
		{
			if (AccessTools.Property(typeof(ConfigFile), "OrphanedEntries")?.GetValue(config) is IDictionary dictionary)
			{
				dictionary.Clear();
			}
		}
	}
	internal static class PluginInfo
	{
		public const string GUID = "uk.co.sulphurdev.weathergains";

		public const string NAME = "Weather Gains";

		public const string VERSION = "2.0.0";
	}
	[BepInPlugin("uk.co.sulphurdev.weathergains", "Weather Gains", "2.0.0")]
	public class WeatherGains : BaseUnityPlugin
	{
		internal static PluginConfig BoundConfig { get; private set; }

		private static ManualLogSource Logger { get; set; }

		private static Harmony Harmony { get; set; }

		private void Awake()
		{
			BoundConfig = new PluginConfig(((BaseUnityPlugin)this).Config);
			Logger = ((BaseUnityPlugin)this).Logger;
			if (!BoundConfig.PluginEnabled.Value)
			{
				Logger.LogWarning((object)"Weather Gains v2.0.0 is disabled! If this is not intentional, please enable it in the config file.");
				return;
			}
			Logger.LogInfo((object)"Weather Gains v2.0.0 is loading...");
			ApplyPatches();
			Logger.LogInfo((object)"Weather Gains v2.0.0 has loaded!");
		}

		private static void ApplyPatches()
		{
			//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("uk.co.sulphurdev.weathergains");
			}
			Logger.LogDebug((object)"Applying patches...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Successfully applied all patches!");
		}

		internal static void RemovePatches()
		{
			if (Harmony != null)
			{
				Logger.LogDebug((object)"Removing patches...");
				Harmony.UnpatchAll(Harmony.Id);
				Logger.LogDebug((object)"Successfully removed all patches!");
				Harmony = null;
			}
		}
	}
}
namespace WeatherGains.Types
{
	public class MultiValueGroup
	{
		public ConfigEntry<float> ValueMultiplier { get; set; }

		public ConfigEntry<float> AmountMultiplier { get; set; }
	}
}
namespace WeatherGains.Transpilers
{
	[HarmonyPatch(typeof(RedLocustBees))]
	public class RedLocustBeesTranspiler
	{
		[HarmonyPatch("SpawnHiveNearEnemy")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> SpawnHiveNearEnemyTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Expected O, but got Unknown
			if (WeatherGains.BoundConfig.CompatibilityMode.Value)
			{
				return instructions;
			}
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			MethodInfo methodInfo = AccessTools.Method(typeof(RedLocustBees), "SpawnHiveClientRpc", (Type[])null, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(MultiplierManager), "ApplyValueMultiplier", (Type[])null, (Type[])null);
			for (int i = 1; i < list.Count; i++)
			{
				if (!CodeInstructionExtensions.Calls(list[i], methodInfo))
				{
					continue;
				}
				for (int num = i - 1; num >= 0; num--)
				{
					if (list[num].opcode == OpCodes.Ldloc_3)
					{
						list.Insert(num + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo2));
						return list;
					}
				}
				return list;
			}
			return list;
		}
	}
}
namespace WeatherGains.Patches
{
	[HarmonyPatch(typeof(GrabbableObject))]
	public class GrabbableObjectPatch
	{
		[HarmonyPatch("SetScrapValue")]
		[HarmonyPrefix]
		private static void SetScrapValuePrefix(ref int setValueTo)
		{
			setValueTo = MultiplierManager.ApplyValueMultiplier(setValueTo);
		}
	}
	[HarmonyPatch(typeof(LungProp))]
	public class LungPropPatch
	{
		[HarmonyPatch("DisconnectFromMachinery")]
		[HarmonyPrefix]
		private static void DisconnectFromMachineryPatch(LungProp __instance)
		{
			if (WeatherGains.BoundConfig.LungValueMultiEnabled.Value)
			{
				((GrabbableObject)__instance).SetScrapValue(MultiplierManager.ApplyValueMultiplier(((GrabbableObject)__instance).scrapValue));
			}
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	public class RoundManagerPatch
	{
		private static float _appliedMultiplier = 1f;

		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPrefix]
		private static void SpawnScrapInLevelPrefix()
		{
			float num = MultiplierManager.ApplyAmountMultiplier(RoundManager.Instance.scrapAmountMultiplier);
			RoundManager.Instance.scrapAmountMultiplier = num;
			_appliedMultiplier = num;
		}

		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyPostfix]
		private static void SpawnScrapInLevelPostfix()
		{
			RoundManager instance = RoundManager.Instance;
			instance.scrapAmountMultiplier /= _appliedMultiplier;
			_appliedMultiplier = 1f;
		}
	}
}