Decompiled source of Random Moons v1.0.1

plugins/4902-Random_Moons.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace r_m;

[BepInPlugin("4902.Random_Moons", "Random_Moons", "1.0.0")]
public class c : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("4902.Random_Moons");

	public static ManualLogSource mls;

	public static ConfigEntry<bool> cfg_ltn;

	public static ConfigEntry<bool> cfg_ltw;

	public static ConfigEntry<bool> cfg_wet;

	public static ConfigEntry<bool> cfg_rld;

	public static ConfigEntry<bool> cfg_psm;

	public static ConfigEntry<bool> cfg_mdn;

	public static ConfigEntry<bool> cfg_slm;

	public static ConfigEntry<bool> cfg_mcc;

	public static ConfigEntry<bool> cfg_mcm;

	public static ConfigEntry<bool> cfg_cmm;

	public static ConfigEntry<int> cfg_dsc;

	public static ConfigEntry<bool> cfg_pri;

	public static bool chosen = false;

	public static bool chosen_moon = false;

	public static int real = -1;

	public static string moon_n = "name";

	public static string moon_c = "ffffff";

	public static string moon_d = "description";

	private void Awake()
	{
		cfg_ltn = ((BaseUnityPlugin)this).Config.Bind<bool>("", "-print", false, "[Print Moon Name]\nprints the moon name if there are <= 1 players");
		cfg_ltw = ((BaseUnityPlugin)this).Config.Bind<bool>("", "--print", false, "[Print Moon Weather]\nprints the moon weather if there are <= 1 players");
		cfg_wet = ((BaseUnityPlugin)this).Config.Bind<bool>("", "---print", true, "[Print Moon Weather]\nprints the weather at all");
		cfg_rld = ((BaseUnityPlugin)this).Config.Bind<bool>("", "random", true, "[Random Description]\nshould the description on the monitor display a random levels description\ntrue, display any levels description, unrelated to the actual orbited level\nfalse, display unknown");
		cfg_psm = ((BaseUnityPlugin)this).Config.Bind<bool>("", "prevent", true, "[Prevent Same Moon]\nprevents routing to the same level as the currently orbited level\nif current challenge moon is Moon-11 and its level is Adamance, rerouting would or would not be able to pick Adamance again");
		cfg_mdn = ((BaseUnityPlugin)this).Config.Bind<bool>("", "default", true, "[Default Moon Name]\ndisplay the levels default name instead of its challenge moon name when landing\ndisplay 'CELESTIAL BODY: 20 Adamance' or 'CELESTIAL BODY: Moon-11'");
		cfg_slm = ((BaseUnityPlugin)this).Config.Bind<bool>("", "save/load", true, "[Save / Load]\nsave and load the current challenge moon being orbited\ntrue, when rejoining the orbited moon will be the same challenge moon as it was previously\nfalse, if challenge moon was Moon-11 and level was Adamance then when rejoining the orbited moon will be non-challenge Adamance");
		cfg_mcc = ((BaseUnityPlugin)this).Config.Bind<bool>("", "-color", true, "[Printed Moon Color]\nshould the moon name printed to chat have color");
		cfg_mcm = ((BaseUnityPlugin)this).Config.Bind<bool>("", "--color", true, "[Monitor Moon Color]\nshould the moon name on the monitor have color");
		cfg_cmm = ((BaseUnityPlugin)this).Config.Bind<bool>("", "modifiers", true, "[Challenge Moon Modifiers]\nshould challenge moon modifiers be enabled for challenge moons");
		cfg_dsc = ((BaseUnityPlugin)this).Config.Bind<int>("", "option", 1, "[Seed / Route Options]\n1 = different seed each day (like vanilla gameplay)\n2 = same seed for the same challenge moon (like weekly challenge moons)\n3 = route to company when going to orbit from a challenge moon (to play a different level each day)");
		cfg_pri = ((BaseUnityPlugin)this).Config.Bind<bool>("", "print", false, "[Print to Console]\nprint statements in console for debugging");
		mls = Logger.CreateLogSource("Random Moons");
		harmony.PatchAll();
	}
}
[HarmonyPatch(typeof(Terminal))]
internal class t
{
	private static TerminalNode _node;

	private static TerminalKeyword _word;

	private static string text1 = "Route the autopilot to a random challenge moon.\n\nPlease CONFIRM or DENY.\n\n";

	[HarmonyPostfix]
	[HarmonyPatch("Awake")]
	private static void pst1(Terminal __instance)
	{
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Expected O, but got Unknown
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Expected O, but got Unknown
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Expected O, but got Unknown
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Expected O, but got Unknown
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Expected O, but got Unknown
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Expected O, but got Unknown
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Expected O, but got Unknown
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Expected O, but got Unknown
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Expected O, but got Unknown
		print("1.0");
		if (!((Object)(object)_node != (Object)null))
		{
			print("1.1");
			TerminalNode specialKeywordResult = __instance.terminalNodes.allKeywords.First((TerminalKeyword _) => ((Object)_).name == "Moons").specialKeywordResult;
			specialKeywordResult.displayText += "* Randomizer   //   Random challenge moons\n\n";
			TerminalKeyword val = __instance.terminalNodes.allKeywords.First((TerminalKeyword _) => ((Object)_).name == "Route");
			TerminalNode val2 = new TerminalNode();
			((Object)val2).name = "randomizer_options";
			val2.displayText = text1;
			val2.clearPreviousText = true;
			val2.overrideOptions = true;
			CompatibleNoun[] array = (CompatibleNoun[])(object)new CompatibleNoun[2];
			CompatibleNoun val3 = new CompatibleNoun();
			TerminalNode val4 = new TerminalNode();
			val4.displayText = "You have cancelled the order.\n\n";
			val4.clearPreviousText = true;
			val3.result = val4;
			TerminalKeyword val5 = new TerminalKeyword();
			val5.word = "deny";
			val3.noun = val5;
			array[0] = val3;
			CompatibleNoun val6 = new CompatibleNoun();
			TerminalNode val7 = new TerminalNode();
			((Object)val7).name = "randomizer_confirm";
			val7.displayText = "Routing autopilot to a random challenge moon.\n\nGood luck.\n\n";
			val7.clearPreviousText = true;
			val6.result = val7;
			TerminalKeyword val8 = new TerminalKeyword();
			val8.word = "confirm";
			val6.noun = val8;
			array[1] = val6;
			val2.terminalOptions = array;
			_node = val2;
			TerminalKeyword val9 = new TerminalKeyword();
			val9.word = "randomizer";
			val9.specialKeywordResult = _node;
			val9.defaultVerb = val;
			_word = val9;
			__instance.terminalNodes.allKeywords = CollectionExtensions.AddToArray<TerminalKeyword>(__instance.terminalNodes.allKeywords, _word);
			CompatibleNoun[] compatibleNouns = val.compatibleNouns;
			CompatibleNoun val10 = new CompatibleNoun();
			val10.result = _node;
			val10.noun = _word;
			val.compatibleNouns = CollectionExtensions.AddToArray<CompatibleNoun>(compatibleNouns, val10);
		}
	}

	[HarmonyPatch("LoadNewNode")]
	private static void Prefix(ref TerminalNode node)
	{
		print("2.0");
		if (((Object)node).name == "randomizer_options")
		{
			print("2.1");
			if (StartOfRound.Instance.connectedPlayersAmount + 1 > 1)
			{
				print("2.2");
				node.displayText = "! Ship will automatically start !\n\n" + text1;
			}
			else
			{
				print("2.3");
				node.displayText = text1;
			}
		}
	}

	[HarmonyPatch("LoadNewNode")]
	[HarmonyPostfix]
	private static void pst2(Terminal __instance, ref TerminalNode node, ref int ___groupCredits)
	{
		print("3.0");
		if (((Object)node).name == "randomizer_confirm")
		{
			print("3.1");
			if (GameNetworkManager.Instance.isHostingGame)
			{
				print("3.2");
				StartOfRound val = Object.FindObjectOfType<StartOfRound>();
				if (val.inShipPhase && !val.travellingToNewLevel)
				{
					print("3.3");
					if (!val.isChallengeFile)
					{
						print("3.4");
						c.chosen = true;
						val.ChangeLevelServerRpc(moons.list(c.cfg_psm.Value), ___groupCredits);
					}
					else
					{
						print("3.5");
						__instance.LoadNewNode(__instance.terminalNodes.specialNodes[24]);
					}
				}
				else
				{
					print("3.6");
					__instance.LoadNewNode(__instance.terminalNodes.specialNodes[3]);
				}
			}
			else
			{
				print("3.7");
				__instance.currentText = "\n\n\nUnable to route to a challenge moon while not host\n\n";
				__instance.screenText.text = "\n\n\nUnable to route to a challenge moon while not host\n\n";
			}
		}
		if (((Object)node).name == "MoonsCatalogue")
		{
			add(__instance);
		}
	}

	private static async void add(Terminal __instance)
	{
		print("4.0");
		await Task.Delay(1);
		string text2 = __instance.currentText;
		if (text2.Contains("PREVIEW:") || text2.Contains("╔"))
		{
			print("4.1");
			string text3 = ((!text2.Contains("╔")) ? "* u___" : (text2.Contains("PREVIEW:") ? "\n\n u---" : " u---"));
			text2 = (__instance.currentText = text2.Insert((from Match _ in Regex.Matches(text2, "\n" + text3.Split(new char[1] { 'u' })[1])
				select _.Index).ToList().Last(), text3.Split(new char[1] { 'u' })[0] + "Randomizer   //   Random challenge moons\n"));
			__instance.screenText.text = text2;
		}
	}

	private static void print(string _)
	{
		if (c.cfg_pri.Value)
		{
			c.mls.LogInfo((object)("Terminal:" + _));
		}
	}
}
[HarmonyPatch(typeof(StartOfRound))]
internal class sor
{
	private static string current_name = "name";

	private static string current_color = "ffffff";

	private static bool temp1 = false;

	private static bool temp2 = false;

	[HarmonyPostfix]
	[HarmonyPatch("Start")]
	private static void pst1(StartOfRound __instance)
	{
		print("1.0");
		c.chosen = false;
		c.real = (StartOfRound.Instance.isChallengeFile ? 1 : 0);
		if (!c.cfg_slm.Value || c.real == 1 || !GameNetworkManager.Instance.isHostingGame)
		{
			return;
		}
		print("1.1");
		try
		{
			string currentSaveFileName = GameNetworkManager.Instance.currentSaveFileName;
			c.chosen_moon = ES3.Load<bool>("4902.Random_Moons-1", currentSaveFileName, false);
			if (c.chosen_moon)
			{
				print("1.2");
				c.moon_n = ES3.Load<string>("4902.Random_Moons-2", currentSaveFileName, "name");
				c.moon_c = ES3.Load<string>("4902.Random_Moons-3", currentSaveFileName, "ffffff");
				c.moon_d = ES3.Load<string>("4902.Random_Moons-4", currentSaveFileName, "description");
				current_name = c.moon_n;
				current_color = c.moon_c;
				((TMP_Text)__instance.screenLevelDescription).text = moons.create_description(c.moon_n, c.moon_c, c.moon_d);
				((Behaviour)__instance.screenLevelVideoReel).enabled = false;
				((Component)__instance.screenLevelVideoReel).gameObject.SetActive(false);
			}
		}
		catch (Exception ex)
		{
			c.mls.LogError((object)("Error while trying to load game values when connecting as host: " + ex));
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch("CancelChangeLevelClientRpc")]
	private static void pre1()
	{
		print("2.0");
		c.chosen = false;
	}

	[HarmonyPatch("ChangeLevel")]
	[HarmonyPostfix]
	private static void pst2()
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		print("3.0");
		if (c.chosen)
		{
			print("3.1");
			c.moon_n = GameNetworkManager.Instance.GetNameForWeekNumber(-1);
			c.moon_c = ColorUtility.ToHtmlStringRGB(Random.ColorHSV(0f, 1f, 0.8f, 0.8f, 1f, 1f));
			if (c.cfg_ltn.Value || StartOfRound.Instance.connectedPlayersAmount + 1 > 1)
			{
				print("3.2");
				string text = (c.cfg_mcc.Value ? c.moon_c : "FFFF00");
				HUDManager.Instance.AddTextToChatOnServer("</color><color=#FF0000>Routing to random challenge moon:</color>\n<size=14><color=#" + text + ">" + c.moon_n + "</color></size>", -1);
				c.mls.LogMessage((object)("Routing to random challenge moon: " + c.moon_n + "(" + c.moon_c + ")"));
			}
		}
		c.chosen_moon = c.chosen;
	}

	[HarmonyPatch("ArriveAtLevel")]
	[HarmonyPrefix]
	private static void pre2()
	{
		print("4.0");
		if (c.chosen)
		{
			print("4.1");
			temp1 = true;
		}
		c.chosen = false;
	}

	[HarmonyPostfix]
	[HarmonyPatch("ArriveAtLevel")]
	private static void pst3(StartOfRound __instance)
	{
		arrive(__instance);
	}

	private static async void arrive(StartOfRound __instance)
	{
		print("5.0");
		if (temp1)
		{
			print("5.1");
			temp1 = false;
			c.moon_d = __instance.levels[moons.list()].LevelDescription;
			((TMP_Text)__instance.screenLevelDescription).text = moons.create_description(c.moon_n, c.moon_c, c.moon_d);
			((Behaviour)__instance.screenLevelVideoReel).enabled = false;
			((Component)__instance.screenLevelVideoReel).gameObject.SetActive(false);
			if (StartOfRound.Instance.connectedPlayersAmount + 1 > 1)
			{
				print("5.2");
				StartMatchLever lever = Object.FindObjectOfType<StartMatchLever>();
				await Task.Delay(250);
				lever.PlayLeverPullEffectsServerRpc(true);
				await Task.Delay(1500);
				lever.StartGame();
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch("StartGame")]
	private static void pre3(StartOfRound __instance)
	{
		print("6.0");
		if (c.cfg_dsc.Value == 2)
		{
			print("6.1");
			if (c.chosen_moon && c.moon_n == current_name && c.moon_c == current_color)
			{
				print("6.2");
				__instance.overrideRandomSeed = true;
				__instance.overrideSeedNumber = __instance.randomMapSeed;
			}
			current_name = c.moon_n;
			current_color = c.moon_c;
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch("StartGame")]
	private static void pst4(StartOfRound __instance)
	{
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Invalid comparison between Unknown and I4
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		print("7.0");
		if (!c.chosen_moon)
		{
			return;
		}
		print("7.1");
		if (c.cfg_dsc.Value == 2)
		{
			print("7.2");
			__instance.overrideRandomSeed = false;
		}
		if (c.cfg_cmm.Value)
		{
			print("7.3");
			__instance.isChallengeFile = true;
		}
		if (c.cfg_wet.Value && (c.cfg_ltw.Value || StartOfRound.Instance.connectedPlayersAmount + 1 > 1))
		{
			print("7.4");
			string text = (((int)__instance.currentLevel.currentWeather == -1) ? "none" : ("Weather: " + __instance.currentLevel.currentWeather));
			if (text != "none")
			{
				print("7.5");
				HUDManager.Instance.AddTextToChatOnServer("</color><color=#C0C0C0>" + text + "</color>", -1);
				c.mls.LogMessage((object)text);
			}
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch("ShipLeave")]
	private static void pre4()
	{
		print("8.0");
		if (c.cfg_cmm.Value && GameNetworkManager.Instance.isHostingGame && c.real != 1)
		{
			print("8.1");
			StartOfRound.Instance.isChallengeFile = false;
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch("PassTimeToNextDay")]
	private static void pst5(StartOfRound __instance)
	{
		print("9.0");
		if (c.chosen_moon)
		{
			print("9.1");
			((TMP_Text)__instance.screenLevelDescription).text = moons.create_description(c.moon_n, c.moon_c, c.moon_d);
			((Behaviour)__instance.screenLevelVideoReel).enabled = false;
			((Component)__instance.screenLevelVideoReel).gameObject.SetActive(false);
		}
	}

	[HarmonyPatch("SetShipReadyToLand")]
	[HarmonyPrefix]
	private static void pre5(StartOfRound __instance)
	{
		print("10.0");
		if (c.cfg_dsc.Value == 3 && c.chosen_moon && !__instance.travellingToNewLevel && c.real != 1 && GameNetworkManager.Instance.isHostingGame)
		{
			print("10.1");
			temp2 = true;
			c.chosen_moon = false;
			__instance.currentLevel = __instance.levels[3];
			__instance.currentLevelID = 3;
			TimeOfDay.Instance.currentLevel = __instance.currentLevel;
			RoundManager.Instance.currentLevel = __instance.currentLevel;
		}
	}

	[HarmonyPatch("SetShipReadyToLand")]
	[HarmonyPostfix]
	private static void pst6(StartOfRound __instance)
	{
		print("11.0");
		if (c.cfg_dsc.Value == 3 && temp2 && __instance.inShipPhase && !__instance.travellingToNewLevel && c.real != 1 && GameNetworkManager.Instance.isHostingGame)
		{
			print("11.1");
			temp2 = false;
			__instance.ChangeLevelServerRpc(3, Object.FindObjectOfType<Terminal>().groupCredits);
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch("OnLocalDisconnect")]
	private static void pst7()
	{
		print("12.0");
		c.chosen = false;
		c.chosen_moon = false;
		c.real = -1;
	}

	private static void print(string _)
	{
		if (c.cfg_pri.Value)
		{
			c.mls.LogInfo((object)("StartOfRound:" + _));
		}
	}
}
[HarmonyPatch(typeof(RoundManager))]
internal class rm
{
	[HarmonyPatch("SetChallengeFileRandomModifiers")]
	private static void Postfix(RoundManager __instance)
	{
		print("1.0");
		if (c.cfg_cmm.Value && c.chosen_moon && c.real != 1 && StartOfRound.Instance.connectedPlayersAmount + 1 > 1 && GameNetworkManager.Instance.isHostingGame)
		{
			print("1.1");
			__instance.increasedMapPropSpawnRateIndex = -1;
		}
	}

	private static void print(string _)
	{
		if (c.cfg_pri.Value)
		{
			c.mls.LogInfo((object)("RoundManager:" + _));
		}
	}
}
[HarmonyPatch(typeof(OutOfBoundsTrigger))]
internal class ote
{
	[HarmonyPatch("OnTriggerEnter")]
	private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		List<CodeInstruction> list = instructions.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].opcode == OpCodes.Ldfld && list[i].operand != null && list[i].operand.ToString() == "System.Boolean isChallengeFile")
			{
				list[i - 1].opcode = OpCodes.Nop;
				list[i].opcode = OpCodes.Nop;
				list[i + 1].opcode = OpCodes.Nop;
				break;
			}
		}
		return list;
	}
}
[HarmonyPatch(typeof(Animator))]
internal class uea
{
	[HarmonyPatch("SetTrigger", new Type[] { typeof(string) })]
	private static void Postfix(ref string name)
	{
		wait(name);
	}

	private static async void wait(string name)
	{
		if (name == "introAnimation" && c.chosen_moon)
		{
			print("1.1");
			await Task.Delay(10);
			string moon = (c.cfg_mdn.Value ? StartOfRound.Instance.currentLevel.PlanetName : c.moon_n);
			((TMP_Text)HUDManager.Instance.planetInfoHeaderText).text = "CELESTIAL BODY: " + moon;
		}
	}

	private static void print(string _)
	{
		if (c.cfg_pri.Value)
		{
			c.mls.LogInfo((object)("UnityEngine.Animator:" + _));
		}
	}
}
[HarmonyPatch(typeof(GameNetworkManager))]
internal class gnm
{
	[HarmonyPatch("GetWeekNumber")]
	private static bool Prefix(ref int __result)
	{
		print("1.0");
		if (c.chosen || c.chosen_moon)
		{
			print("1.1");
			__result = Random.Range(0, 100000000) - 51016;
			return false;
		}
		return true;
	}

	[HarmonyPatch("SaveGame")]
	private static void Postfix(GameNetworkManager __instance)
	{
		print("2.0");
		if (!c.cfg_slm.Value || c.real == 1 || !StartOfRound.Instance.inShipPhase || !__instance.isHostingGame)
		{
			return;
		}
		try
		{
			print("2.1");
			StartOfRound val = Object.FindObjectOfType<StartOfRound>();
			if ((Object)(object)val != (Object)null)
			{
				print("2.2");
				ES3.Save<bool>("4902.Random_Moons-1", c.chosen_moon, __instance.currentSaveFileName);
				if (c.chosen_moon)
				{
					print("2.3");
					ES3.Save<string>("4902.Random_Moons-2", c.moon_n, __instance.currentSaveFileName);
					ES3.Save<string>("4902.Random_Moons-3", c.moon_c, __instance.currentSaveFileName);
					ES3.Save<string>("4902.Random_Moons-4", c.moon_d, __instance.currentSaveFileName);
				}
			}
		}
		catch (Exception ex)
		{
			c.mls.LogError((object)("Error while trying to save game values when disconnecting as host: " + ex));
		}
	}

	private static void print(string _)
	{
		if (c.cfg_pri.Value)
		{
			c.mls.LogInfo((object)("GameNetworkManager:" + _));
		}
	}
}
internal class moons
{
	public static int list(bool compare_previous = false)
	{
		int num = 3;
		while (num == 3 || num == 11 || (compare_previous && num == StartOfRound.Instance.currentLevelID))
		{
			num = Random.Range(0, StartOfRound.Instance.levels.Length);
		}
		return num;
	}

	public static string create_description(string _n, string _c, string _d)
	{
		_n = (c.cfg_mcm.Value ? ("Orbiting: </color><color=#" + _c + ">" + _n + "</color>\n") : ("Orbiting: " + _n + "\n"));
		if (!c.cfg_rld.Value)
		{
			_d = "POPULATION: Unknown\nCONDITIONS: Unknown\nFAUNA: Unknown";
		}
		return _n + _d;
	}
}