Decompiled source of MeteorShowerChance v1.1.1

MeteorShowerChance.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using MeteorShowerChance.Patches;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MeteorShowerChance")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MeteorShowerChance")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("89435176-9a4e-45ff-b607-6af9a7b08a46")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MeteorShowerChance
{
	[BepInPlugin("ironbean.MeteorShowerChance", "Meteor Shower Chance", "1.1.1")]
	public class MeteorChance : BaseUnityPlugin
	{
		private const string modGUID = "ironbean.MeteorShowerChance";

		private const string modName = "Meteor Shower Chance";

		private const string modVersion = "1.1.1";

		private readonly Harmony harmony = new Harmony("ironbean.MeteorShowerChance");

		internal static MeteorChance Instance;

		internal static ManualLogSource mls;

		internal static int daysWithoutMeteor;

		internal static bool isChallengeFile;

		internal static bool debug;

		public ConfigEntry<bool> resetOnRestart;

		public ConfigEntry<float> rateCap;

		public ConfigEntry<string> chanceType;

		public ConfigEntry<float> fixedRate;

		public ConfigEntry<float> linearRate;

		public ConfigEntry<float> exponentialRate;

		private void Awake()
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Expected O, but got Unknown
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Expected O, but got Unknown
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Expected O, but got Unknown
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Expected O, but got Unknown
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("ironbean.MeteorShowerChance");
			mls.LogInfo((object)"Meteor Happens");
			resetOnRestart = ((BaseUnityPlugin)Instance).Config.Bind<bool>("Chances", "ResetOnRestart", true, new ConfigDescription("Whether or not Linear/Exponential rates retain the day count when reloading/a new save.", (AcceptableValueBase)null, Array.Empty<object>()));
			rateCap = ((BaseUnityPlugin)Instance).Config.Bind<float>("Chances", "RateCap", 15f, new ConfigDescription("The maximum chance for a meteor shower to occur. Set to 0% to disable meteor showers. If FixedRate is used, this value is ignored.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			string[] array = new string[3] { "FixedRate", "LinearRate", "ExponentialRate" };
			chanceType = ((BaseUnityPlugin)Instance).Config.Bind<string>("Chances", "ChanceType", "FixedRate", new ConfigDescription("Determines which type of chance the meteor shower will use. ( FixedRate / LinearRate / ExponentialRate )", (AcceptableValueBase)(object)new AcceptableValueList<string>(array), Array.Empty<object>()));
			fixedRate = ((BaseUnityPlugin)Instance).Config.Bind<float>("Chances", "FixedRate", 1f, new ConfigDescription("When selected, the chance of a meteor shower occurring on a day will be this, in percentage.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			linearRate = ((BaseUnityPlugin)Instance).Config.Bind<float>("Chances", "LinearRate", 1f, new ConfigDescription("When selected, the chance of a meteor shower occurring on a day will increase by this amount daily, in percentage.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			exponentialRate = ((BaseUnityPlugin)Instance).Config.Bind<float>("Chances", "ExponentialRate", 0.96f, new ConfigDescription("When selected, the chance of a meteor shower occurring on a day will increase at a ramping curve. This value is used as the base for the exponent, multiplied by 100. Smaller value = more frequent. [ curve is: -1+Rate^(-DAYS_SINCE_METEORS) ]", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 1f), Array.Empty<object>()));
			string text = "Using a " + chanceType.Value + " of ";
			text = chanceType.Value switch
			{
				"FixedRate" => text + fixedRate.Value + "%", 
				"LinearRate" => text + linearRate.Value + "% added daily.", 
				"ExponentialRate" => text + linearRate.Value + "^(-x)", 
				_ => "Unknown type", 
			};
			if (chanceType.Value != "Fixed Rate")
			{
				text = text + ", with a cap of " + rateCap.Value + "%";
				if (rateCap.Value == 0f)
				{
					text = "Meteors are disabled.";
				}
			}
			mls.LogInfo((object)text);
			harmony.PatchAll(typeof(MeteorChance));
			harmony.PatchAll(typeof(MeteorPatch));
			harmony.PatchAll(typeof(StartOfRoundPatch));
		}
	}
}
namespace MeteorShowerChance.Patches
{
	[HarmonyPatch(typeof(TimeOfDay))]
	internal class MeteorPatch
	{
		[HarmonyPatch("DecideRandomDayEvents")]
		[HarmonyPostfix]
		public static void OverwriteMeteorChance(TimeOfDay __instance)
		{
			if (!((NetworkBehaviour)__instance).IsServer || MeteorChance.isChallengeFile)
			{
				return;
			}
			bool flag = MeteorChance.Instance.chanceType.Value == "FixedRate";
			if (flag || MeteorChance.Instance.rateCap.Value != 0f)
			{
				Random random = new Random(StartOfRound.Instance.randomMapSeed + 28);
				float num = 0.7f;
				int daysWithoutMeteor = MeteorChance.daysWithoutMeteor;
				switch (MeteorChance.Instance.chanceType.Value)
				{
				case "FixedRate":
					num = MeteorChance.Instance.fixedRate.Value;
					break;
				case "LinearRate":
					num = MeteorChance.Instance.linearRate.Value * (float)(daysWithoutMeteor + 1);
					break;
				case "ExponentialRate":
					num = (float)Math.Pow(MeteorChance.Instance.exponentialRate.Value, -daysWithoutMeteor) - 1f;
					break;
				}
				if (!flag)
				{
					num = Math.Min(num, MeteorChance.Instance.rateCap.Value);
				}
				int num2 = (int)Math.Ceiling(num * 10f);
				if (MeteorChance.debug)
				{
					MeteorChance.mls.LogInfo((object)("Current chance today is " + (float)num2 / 10f + "%"));
					MeteorChance.mls.LogInfo((object)("Day count: " + daysWithoutMeteor));
					MeteorChance.mls.LogInfo((object)("Percentage full: " + num));
				}
				if (random.Next(0, 1000) < num2)
				{
					MeteorChance.daysWithoutMeteor = 0;
					__instance.meteorShowerAtTime = (float)random.Next(5, 80) / 100f;
				}
				else
				{
					MeteorChance.daysWithoutMeteor++;
					__instance.meteorShowerAtTime = -1f;
				}
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("StartGame")]
		[HarmonyPostfix]
		public static void LeverPulled(StartOfRound __instance)
		{
			MeteorChance.isChallengeFile = __instance.isChallengeFile;
		}

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void StartSession()
		{
			if (MeteorChance.Instance.resetOnRestart.Value)
			{
				MeteorChance.daysWithoutMeteor = 0;
			}
		}

		[HarmonyPatch("ResetShip")]
		[HarmonyPostfix]
		public static void ResetDayCount()
		{
			if (MeteorChance.Instance.resetOnRestart.Value)
			{
				MeteorChance.daysWithoutMeteor = 0;
			}
		}
	}
}