Decompiled source of LaunchRandom v1.3.3

LaunchRandom.dll

Decompiled 9 months ago
using System;
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 LaunchRandom.Configs;
using LaunchRandom.Managers;
using LaunchRandom.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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LaunchRandom")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LaunchRandom")]
[assembly: AssemblyTitle("LaunchRandom")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LaunchRandom
{
	[BepInPlugin("ovofnl.LaunchRandom", "Launch Random", "1.3.3")]
	public class Plugin : BaseUnityPlugin
	{
		private const string modGUID = "ovofnl.LaunchRandom";

		private const string modName = "Launch Random";

		private const string modVersion = "1.3.3";

		private readonly Harmony harmony = new Harmony("ovofnl.LaunchRandom");

		public static Plugin Instance;

		internal static ManualLogSource mls;

		public static PluginConfig cfg;

		private void Awake()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("ovofnl.LaunchRandom");
			cfg = new PluginConfig(((BaseUnityPlugin)this).Config);
			cfg.InitBindings();
			GameObject val = new GameObject("RDManager");
			val.AddComponent<RDManager>();
			mls.LogInfo((object)"Message on mod load");
			harmony.PatchAll(typeof(Plugin));
			harmony.PatchAll(typeof(StartOfRoundPatch));
			harmony.PatchAll(typeof(StartMatchLeverPatch));
		}
	}
}
namespace LaunchRandom.Patches
{
	internal delegate bool judge();
	[HarmonyPatch(typeof(StartMatchLever))]
	public class StartMatchLeverPatch
	{
		public static StartMatchLeverPatch Instance;

		public PluginConfig cfg;

		public ManualLogSource mls;

		[HarmonyPatch("BeginHoldingInteractOnLever")]
		[HarmonyPrefix]
		private static void ChangeHoldingLeverTime(ref InteractTrigger ___triggerScript)
		{
			if (RDManager.Instance.shipHasLanded)
			{
				return;
			}
			if (RDManager.Instance.cfg.LEVEL_NAME_ANOTHER.Length != 0 && RDManager.Instance.startOfRoundInstance.levels.Length - RDManager.Instance.moonsListExcept.Length <= 2)
			{
				RDManager.Instance.repeatLaunch = true;
				RDManager.mls.LogInfo((object)"The repeat Launch is TRUE");
			}
			if (!RDManager.Instance.leverHasBeenPulled)
			{
				return;
			}
			RDManager.Instance.leverHasBeenPulled = false;
			if (TimeOfDay.Instance.daysUntilDeadline <= 0)
			{
				RDManager.Instance.startOfRoundInstance.ChangeLevelServerRpc(RDManager.Instance.companyBuildingLevelID, Object.FindObjectOfType<Terminal>().groupCredits);
			}
			else
			{
				if (!RDManager.Instance.randomEnableAnother)
				{
					return;
				}
				RDManager.Instance.BalanceOfMoonsExcept();
				int num = RDManager.Instance.StartToRandomLevelAnother();
				if (!RDManager.Instance.repeatLaunch)
				{
					while (Instance.ThreeDaysJudge(RDManager.Instance.preLevel, num))
					{
						num = RDManager.Instance.StartToRandomLevelAnother();
					}
				}
				RDManager.Instance.startOfRoundInstance.ChangeLevelServerRpc(num, Object.FindObjectOfType<Terminal>().groupCredits);
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void Launch_RandomCore(ref bool ___leverHasBeenPulled)
		{
			if (___leverHasBeenPulled && !RDManager.Instance.leverHasBeenPulled)
			{
				RDManager.Instance.leverHasBeenPulled = true;
			}
			if (RDManager.Instance.shipHasLanded && RDManager.Instance.startOfRoundInstance.currentLevel.levelID != RDManager.Instance.companyBuildingLevelID)
			{
				RDManager.Instance.preLevel = RDManager.Instance.startOfRoundInstance.currentLevel.levelID;
			}
		}

		public bool ThreeDaysJudge(int preLevel, int currentLevel)
		{
			bool flag = false;
			if (RDManager.Instance.startOfRoundInstance.gameStats.daysSpent < 4 && RDManager.Instance.moonsBalance[currentLevel] == 0)
			{
				flag = true;
			}
			return preLevel == currentLevel || flag;
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		private static void GetInstance(StartOfRound __instance)
		{
			RDManager.Instance.startOfRoundInstance = __instance;
			StartMatchLeverPatch.Instance = new StartMatchLeverPatch();
		}

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void GetMoonsInfoPost()
		{
			if (RDManager.Instance.cfg.LEVEL_NAME_ANOTHER.Length == 0)
			{
				RDManager.Instance.GetMoonsInfoExceptNull();
			}
			else
			{
				RDManager.Instance.GetMoonsInfoExcept();
			}
			RDManager.Instance.GetMoonsWeightExcept();
			RDManager.Instance.leverHasBeenPulled = true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void GetShipInfo(bool ___shipHasLanded)
		{
			RDManager.Instance.shipHasLanded = ___shipHasLanded;
		}
	}
}
namespace LaunchRandom.Managers
{
	internal class RDManager : NetworkBehaviour
	{
		public static RDManager Instance;

		public PluginConfig cfg;

		internal static ManualLogSource mls;

		internal StartOfRound startOfRoundInstance;

		public string[] moonsListExcept;

		public float[] moonsWeightExcept;

		public int moonsAllowAmount = 0;

		public int moonsExceptAmount = 0;

		public string[] moonsLevelBalanceExcept;

		public int[] moonsBalance;

		public int[] moonsLevelIDExcept;

		public int preLevel = 0;

		public int tempLevel = 0;

		public int companyBuildingLevelID = -1;

		public int levelDay;

		public float levelBalancesC;

		public bool randomEnableAnother;

		public bool repeatLaunch;

		public bool threeDaysBefore;

		public bool leverHasBeenPulled = true;

		public bool moonsListExceptNull = false;

		public bool shipHasLanded;

		public void InitSettings()
		{
			if (cfg.LEVEL_DAYS_AFTER.HasValue)
			{
				levelDay = cfg.LEVEL_DAYS_AFTER.Value;
			}
			else
			{
				levelDay = 4;
			}
			levelBalancesC = cfg.LEVEL_COEFFICIENT;
			if (cfg.LEVEL_NAME_ANOTHER.Length != 0)
			{
				moonsListExcept = cfg.LEVEL_NAME_ANOTHER.Split(",");
			}
			if (cfg.LEVEL_LV_EXCEPT.Length != 0)
			{
				moonsLevelBalanceExcept = cfg.LEVEL_LV_EXCEPT.Split(",");
			}
			else
			{
				moonsLevelBalanceExcept = new string[1] { "D,C,B" };
			}
			randomEnableAnother = cfg.RANDOM_ENABLE_ANOTHER;
			repeatLaunch = cfg.REPEAT_LAUNCH;
			threeDaysBefore = cfg.THREE_DAYS_SETTING;
		}

		public void GetMoonsInfoExcept()
		{
			moonsBalance = new int[startOfRoundInstance.levels.Length];
			moonsLevelIDExcept = new int[startOfRoundInstance.levels.Length];
			for (int i = 0; i < moonsListExcept.Length; i++)
			{
				for (int j = 0; j < startOfRoundInstance.levels.Length; j++)
				{
					for (int k = 0; k < moonsLevelBalanceExcept.Length; k++)
					{
						if (moonsBalance[j] != 1 && startOfRoundInstance.levels[j].riskLevel.IndexOf(moonsLevelBalanceExcept[k]) != -1)
						{
							moonsBalance[j] = 1;
						}
					}
					if (moonsLevelIDExcept[j] != -1)
					{
						moonsLevelIDExcept[j] = j;
					}
					if (((object)startOfRoundInstance.levels[j]).ToString().Contains("CompanyBuilding"))
					{
						companyBuildingLevelID = j;
						moonsLevelIDExcept[j] = -1;
						moonsBalance[j] = 0;
					}
					if (((object)startOfRoundInstance.levels[j]).ToString().IndexOf(moonsListExcept[i]) != -1)
					{
						moonsLevelIDExcept[j] = -1;
					}
				}
			}
		}

		public void GetMoonsInfoExceptNull()
		{
			moonsBalance = new int[startOfRoundInstance.levels.Length];
			moonsLevelIDExcept = new int[startOfRoundInstance.levels.Length];
			for (int i = 0; i < startOfRoundInstance.levels.Length; i++)
			{
				for (int j = 0; j < moonsLevelBalanceExcept.Length; j++)
				{
					if (moonsBalance[i] != 1 && startOfRoundInstance.levels[i].riskLevel.IndexOf(moonsLevelBalanceExcept[j]) != -1)
					{
						moonsBalance[i] = 1;
					}
				}
				if (moonsLevelIDExcept[i] != -1)
				{
					moonsLevelIDExcept[i] = i;
				}
				if (((object)startOfRoundInstance.levels[i]).ToString().Contains("CompanyBuilding"))
				{
					companyBuildingLevelID = i;
					moonsLevelIDExcept[i] = -1;
					moonsBalance[i] = 0;
				}
			}
		}

		public void GetMoonsWeightExcept()
		{
			float lEVEL_WEIGHT_ANOTHER_EASY = cfg.LEVEL_WEIGHT_ANOTHER_EASY;
			float lEVEL_WEIGHT_ANOTHER_DANGER = cfg.LEVEL_WEIGHT_ANOTHER_DANGER;
			moonsWeightExcept = new float[startOfRoundInstance.levels.Length];
			for (int i = 0; i < moonsBalance.Length; i++)
			{
				if (moonsLevelIDExcept[i] != -1)
				{
					if (moonsBalance[i] != 0)
					{
						moonsWeightExcept[i] = lEVEL_WEIGHT_ANOTHER_EASY;
					}
					else
					{
						moonsWeightExcept[i] = lEVEL_WEIGHT_ANOTHER_DANGER;
					}
				}
			}
		}

		public void InputMoonsInfo()
		{
			for (int i = 0; i < startOfRoundInstance.levels.Length; i++)
			{
				mls.LogInfo((object)(i + " - " + ((object)startOfRoundInstance.levels[i]).ToString()));
			}
		}

		public void InputTestInfo()
		{
			for (int i = 0; i < moonsLevelIDExcept.Length; i++)
			{
				mls.LogInfo((object)(moonsLevelIDExcept[i] + " - " + ((object)startOfRoundInstance.levels[i]).ToString()));
				mls.LogInfo((object)("riskLevel:" + startOfRoundInstance.levels[i].riskLevel + " - moonsBalance:" + moonsBalance[i] + " - moonsWeight:" + moonsWeightExcept[i]));
			}
		}

		public float GetMoonsWeightExceptSum()
		{
			float num = 0f;
			for (int i = 0; i < moonsLevelIDExcept.Length; i++)
			{
				if (moonsLevelIDExcept[i] != -1)
				{
					num += moonsWeightExcept[i];
				}
			}
			return num;
		}

		public static double NextDouble(Random rd, double Min, double Max)
		{
			return rd.NextDouble() * (Max - Min) + Min;
		}

		public int StartToRandomLevelAnother()
		{
			float num = 0f;
			float num2 = 0f;
			int result = 0;
			int hashCode = Guid.NewGuid().GetHashCode();
			Random rd = new Random(hashCode);
			double num3 = NextDouble(rd, 0.0, GetMoonsWeightExceptSum());
			for (int i = 0; i < moonsWeightExcept.Length; i++)
			{
				if (moonsLevelIDExcept[i] != -1)
				{
					num += moonsWeightExcept[i];
					if (num3 <= (double)num && num3 >= (double)num2)
					{
						result = i;
						break;
					}
					num2 += moonsWeightExcept[i];
				}
			}
			return result;
		}

		public void BalanceOfMoonsExcept()
		{
			for (int i = 0; i < moonsBalance.Length; i++)
			{
				if (moonsBalance[i] == 0 && moonsLevelIDExcept[i] != -1)
				{
					moonsWeightExcept[i] = cfg.LEVEL_WEIGHT_ANOTHER_DANGER + levelBalancesC * (float)(startOfRoundInstance.gameStats.daysSpent / levelDay);
				}
			}
		}

		public void InputBalanceOfMoonsWeight()
		{
			for (int i = 0; i < moonsBalance.Length; i++)
			{
				mls.LogInfo((object)(i + " - moonsLevelIDExcept: " + moonsLevelIDExcept[i] + " - moonsBalance: " + moonsBalance[i] + " - moonsWeightExcept: " + moonsWeightExcept[i]));
			}
		}

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			cfg = Plugin.cfg;
			mls = Logger.CreateLogSource("LaunchRandom");
			InitSettings();
		}
	}
}
namespace LaunchRandom.Configs
{
	public class PluginConfig
	{
		private readonly ConfigFile configFile;

		public bool RANDOM_ENABLE_ANOTHER { get; private set; }

		public bool LEVEL_BALANCE_ENABLE { get; private set; }

		public bool REPEAT_LAUNCH { get; private set; }

		public bool THREE_DAYS_SETTING { get; private set; }

		public string LEVEL_NAME_ANOTHER { get; private set; }

		public float LEVEL_COEFFICIENT { get; private set; }

		public int? LEVEL_DAYS_AFTER { get; private set; }

		public string LEVEL_LV_EXCEPT { get; private set; }

		public float LEVEL_WEIGHT_ANOTHER_EASY { get; private set; }

		public float LEVEL_WEIGHT_ANOTHER_DANGER { get; private set; }

		public PluginConfig(ConfigFile config)
		{
			configFile = config;
		}

		private T ConfigEntry<T>(string section, string key, T defaultValue, string description)
		{
			return configFile.Bind<T>(section, key, defaultValue, description).Value;
		}

		public void InitBindings()
		{
			string section = "Core";
			RANDOM_ENABLE_ANOTHER = ConfigEntry(section, "Allow Random Launch(Except)", defaultValue: true, "If true, when the lever has been pulled, it will launch to moon randomly,for the last day it will go to company building.Prefer to use this.");
			LEVEL_BALANCE_ENABLE = ConfigEntry(section, "Allow Level Balance", defaultValue: true, "If true, enable the balance setting.");
			REPEAT_LAUNCH = ConfigEntry(section, "Allow Repeat Launch", defaultValue: false, "If true, it will allow to launch to last moon");
			THREE_DAYS_SETTING = ConfigEntry(section, "Allow to paid moon before 3 days", defaultValue: false, "If true, it will allow to launch to paid moon before 3 days.");
			section = "Moons Settings Another";
			LEVEL_NAME_ANOTHER = ConfigEntry(section, "Moons List(Except)", "Experimentation,Assurance", "These moons will be removed in the random list.");
			LEVEL_LV_EXCEPT = ConfigEntry(section, "Moons Level(Except)", "D,C,B", "Except the RiskLevel in this list.It's for adept to some mod moons.");
			LEVEL_WEIGHT_ANOTHER_EASY = ConfigEntry(section, "Moons Weight Another Easy", 5f, "The Weight data is for those riskLevel less than the Moons Level data.");
			LEVEL_WEIGHT_ANOTHER_DANGER = ConfigEntry(section, "Moons Weight Another Danger", 1f, "The Weight data is for those riskLevel more than the Moons Level data.");
			section = "Balance Settings";
			LEVEL_COEFFICIENT = ConfigEntry(section, "Balance Coefficient", 1f, "The higher the value, the higher the probability of those paid moons andthe lower the probability of those free moons");
			LEVEL_DAYS_AFTER = ConfigEntry(section, "Balance Days", 4, "After 4 days, the probability will increase once by the balance coefficient");
		}
	}
}