Decompiled source of MoonOfTheDay v1.0.4

MoonOfTheDay.dll

Decompiled 6 months 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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;

[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("MoonOfTheDay")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Adds daily and weekly seeded moons to the game!")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4")]
[assembly: AssemblyProduct("MoonOfTheDay")]
[assembly: AssemblyTitle("MoonOfTheDay")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.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 MoonOfTheDay
{
	[BepInPlugin("MoonOfTheDay", "MoonOfTheDay", "1.0.4")]
	public class Plugin : BaseUnityPlugin
	{
		public const string DailyMoonName = "Daily Moon";

		public const string WeeklyMoonName = "Weekly Moon";

		internal static ManualLogSource Logger;

		private Harmony _harmony;

		private bool _isPatched;

		private static int HighestLevelID { get; set; } = -1;


		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			PatchAll();
			Logger.LogInfo((object)"Plugin MoonOfTheDay is loaded!");
		}

		public void PatchAll()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			if (_isPatched)
			{
				Logger.LogWarning((object)"Already patched!");
				return;
			}
			Logger.LogDebug((object)"Patching...");
			_harmony = new Harmony("MoonOfTheDay");
			_harmony.PatchAll();
			_isPatched = true;
			Logger.LogDebug((object)"Patched!");
		}

		public void UnpatchAll()
		{
			if (!_isPatched)
			{
				Logger.LogWarning((object)"Not patched!");
				return;
			}
			Logger.LogDebug((object)"Unpatching...");
			_harmony.UnpatchSelf();
			_isPatched = false;
			Logger.LogDebug((object)"Unpatched!");
		}

		public static int GetDailySeed()
		{
			return (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalDays;
		}

		public static int GetWeeklySeed()
		{
			return (int)Math.Floor((float)(int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalDays / 7f);
		}

		private static void SetHighestLevelID(IEnumerable<SelectableLevel> moons)
		{
			if (HighestLevelID == -1)
			{
				HighestLevelID = moons.Max((SelectableLevel moon) => moon.levelID);
				Logger.LogDebug((object)$"Highest vanilla level ID is {HighestLevelID}");
			}
		}

		private static SelectableLevel GetCustomMoon(SelectableLevel[] moons, bool isDailyElseWeekly = true)
		{
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			SetHighestLevelID(moons);
			Random random = new Random(isDailyElseWeekly ? GetDailySeed() : GetWeeklySeed());
			SelectableLevel[] array = moons.Where((SelectableLevel moon) => moon.levelID != 3).ToArray();
			SelectableLevel obj = Object.Instantiate<SelectableLevel>(array.OrderBy((SelectableLevel moon) => moon.levelID).ToArray()[random.Next(0, array.Length)]);
			((Object)obj).name = (isDailyElseWeekly ? "Daily Moon" : "Weekly Moon");
			obj.PlanetName = (isDailyElseWeekly ? "Daily Moon" : "Weekly Moon");
			obj.LevelDescription = "This moon looks familiar...";
			obj.riskLevel = "???";
			obj.levelID = HighestLevelID + (isDailyElseWeekly ? 1 : 2);
			obj.randomWeathers = Array.Empty<RandomWeatherWithVariables>();
			obj.overrideWeather = true;
			obj.currentWeather = (obj.overrideWeatherType = Enum.GetValues(typeof(LevelWeatherType)).Cast<LevelWeatherType>().ToArray()[random.Next(0, Enum.GetValues(typeof(LevelWeatherType)).Length)]);
			return obj;
		}

		public static SelectableLevel GetDailyMoon(SelectableLevel[] moons)
		{
			return GetCustomMoon(moons);
		}

		public static SelectableLevel GetWeeklyMoon(SelectableLevel[] moons)
		{
			return GetCustomMoon(moons, isDailyElseWeekly: false);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MoonOfTheDay";

		public const string PLUGIN_NAME = "MoonOfTheDay";

		public const string PLUGIN_VERSION = "1.0.4";
	}
}
namespace MoonOfTheDay.Patches
{
	public static class PatchRoundManagerHelpers
	{
		public static Vector3 SeededInsideUnitSphere(ref Random random)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3((float)(random.NextDouble() * 2.0 - 1.0), (float)(random.NextDouble() * 2.0 - 1.0), (float)(random.NextDouble() * 2.0 - 1.0));
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	public class PatchRoundManager
	{
		[HarmonyPatch("GetRandomNavMeshPositionInRadiusSpherical")]
		[HarmonyPrefix]
		public static bool GetRandomNavMeshPositionInRadiusSpherical(ref RoundManager __instance, ref Vector3 __result, Vector3 pos, float radius = 10f, NavMeshHit navHit = default(NavMeshHit))
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			pos = PatchRoundManagerHelpers.SeededInsideUnitSphere(ref __instance.LevelRandom) * radius + pos;
			__result = (NavMesh.SamplePosition(pos, ref navHit, radius, -1) ? ((NavMeshHit)(ref navHit)).position : pos);
			return false;
		}

		[HarmonyPatch("GetRandomNavMeshPositionInRadius")]
		[HarmonyPrefix]
		public static bool GetRandomNavMeshPositionInRadius(ref RoundManager __instance, ref Vector3 __result, Vector3 pos, float radius = 10f, NavMeshHit navHit = default(NavMeshHit))
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			float y = pos.y;
			pos = PatchRoundManagerHelpers.SeededInsideUnitSphere(ref __instance.LevelRandom) * radius + pos;
			pos.y = y;
			if (NavMesh.SamplePosition(pos, ref navHit, radius, -1))
			{
				__result = ((NavMeshHit)(ref navHit)).position;
				return false;
			}
			Debug.Log((object)"Unable to get random nav mesh position in radius! Returning old pos");
			__result = pos;
			return false;
		}
	}
	internal static class PatchStartOfRoundHelpers
	{
		public static bool IsVanillaMoon(SelectableLevel moon)
		{
			return !new string[2] { "Daily Moon", "Weekly Moon" }.Contains(moon.PlanetName);
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	public class PatchStartOfRound
	{
		[HarmonyPatch("StartGame")]
		[HarmonyPrefix]
		public static void SetSeed(StartOfRound __instance)
		{
			if ((Object)(object)__instance.currentLevel == (Object)null)
			{
				return;
			}
			if (PatchStartOfRoundHelpers.IsVanillaMoon(__instance.currentLevel))
			{
				Plugin.Logger.LogDebug((object)("Not setting seed for " + __instance.currentLevel.PlanetName + "..."));
				__instance.overrideRandomSeed = false;
				return;
			}
			Plugin.Logger.LogDebug((object)("Setting seed for " + __instance.currentLevel.PlanetName + "..."));
			__instance.overrideRandomSeed = true;
			string planetName = __instance.currentLevel.PlanetName;
			if (!(planetName == "Daily Moon"))
			{
				if (planetName == "Weekly Moon")
				{
					__instance.overrideSeedNumber = Plugin.GetWeeklySeed();
				}
			}
			else
			{
				__instance.overrideSeedNumber = Plugin.GetDailySeed();
			}
		}

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void InsertMoons(StartOfRound __instance)
		{
			Plugin.Logger.LogDebug((object)"Inserting moons in StartOfRound...");
			StartOfRound instance = StartOfRound.Instance;
			List<SelectableLevel> list = instance.levels.ToList();
			SelectableLevel dailyMoon = Plugin.GetDailyMoon(instance.levels);
			SelectableLevel weeklyMoon = Plugin.GetWeeklyMoon(instance.levels);
			list.RemoveAll((SelectableLevel level) => level.PlanetName == dailyMoon.PlanetName);
			list.RemoveAll((SelectableLevel level) => level.PlanetName == weeklyMoon.PlanetName);
			list.Add(dailyMoon);
			list.Add(weeklyMoon);
			instance.levels = list.ToArray();
		}
	}
	internal static class PatchTerminalHelpers
	{
		public static void AddTerminalCommand(ref Terminal terminal, SelectableLevel moonToAdd)
		{
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Expected O, but got Unknown
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Expected O, but got Unknown
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Unknown result type (might be due to invalid IL or missing references)
			//IL_0210: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Expected O, but got Unknown
			Plugin.Logger.LogDebug((object)("Adding terminal command for " + moonToAdd.PlanetName + "..."));
			TerminalKeyword noun = terminal.terminalNodes.allKeywords.First((TerminalKeyword keyword) => ((Object)keyword).name == "Confirm");
			TerminalKeyword noun2 = terminal.terminalNodes.allKeywords.First((TerminalKeyword keyword) => ((Object)keyword).name == "Deny");
			TerminalKeyword val = terminal.terminalNodes.allKeywords.First((TerminalKeyword keyword) => ((Object)keyword).name == "Route");
			TerminalNode result = val.compatibleNouns[0].result.terminalOptions[0].result;
			TerminalKeyword val2 = ScriptableObject.CreateInstance<TerminalKeyword>();
			((Object)val2).name = GetKWNameForMoon(moonToAdd);
			val2.word = GetKWWordforMoon(moonToAdd);
			val2.defaultVerb = val;
			val2.compatibleNouns = Array.Empty<CompatibleNoun>();
			TerminalNode val3 = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val3).name = GetTravelNodeNameForMoon(moonToAdd);
			val3.displayText = "Routing autopilot to " + moonToAdd.PlanetName + ".\n\nPlease enjoy your flight.";
			val3.clearPreviousText = true;
			val3.buyRerouteToMoon = moonToAdd.levelID;
			TerminalNode val4 = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val4).name = "Daily Moon";
			val4.displayText = "The company has detected a rogue planet. It might not be available for long. Do you want to go there?\n\nIt is currently [currentPlanetTime] on this moon.\n\nPlease CONFIRM or DENY.\n\n";
			val4.clearPreviousText = true;
			val4.displayPlanetInfo = moonToAdd.levelID;
			val4.buyRerouteToMoon = -2;
			val4.overrideOptions = true;
			val4.terminalOptions = (CompatibleNoun[])(object)new CompatibleNoun[2]
			{
				new CompatibleNoun
				{
					noun = noun2,
					result = result
				},
				new CompatibleNoun
				{
					noun = noun,
					result = val3
				}
			};
			List<TerminalKeyword> list = terminal.terminalNodes.allKeywords.ToList();
			list.Add(val2);
			terminal.terminalNodes.allKeywords = list.ToArray();
			List<CompatibleNoun> list2 = val.compatibleNouns.ToList();
			list2.Add(new CompatibleNoun
			{
				noun = val2,
				result = val4
			});
			val.compatibleNouns = list2.ToArray();
			TerminalNode specialKeywordResult = terminal.terminalNodes.allKeywords.First((TerminalKeyword keyword) => ((Object)keyword).name == "Moons").specialKeywordResult;
			specialKeywordResult.displayText = specialKeywordResult.displayText + "* " + moonToAdd.PlanetName + " [planetTime]\n";
			Plugin.Logger.LogDebug((object)("Added terminal command for " + moonToAdd.PlanetName + "."));
		}

		public static string GetKWNameForMoon(SelectableLevel moon)
		{
			return "KW" + moon.PlanetName.ToLower().Replace(" ", "-");
		}

		public static string GetKWWordforMoon(SelectableLevel moon)
		{
			return moon.PlanetName.ToLower().Replace(" ", "-");
		}

		public static string GetTravelNodeNameForMoon(SelectableLevel moon)
		{
			return "Travel" + moon.PlanetName.ToLower().Replace(" ", "-");
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	public class PatchTerminal
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void InsertMoons(Terminal __instance)
		{
			Plugin.Logger.LogDebug((object)"Inserting moons in Terminal...");
			SelectableLevel dailyMoon = Plugin.GetDailyMoon(__instance.moonsCatalogueList);
			SelectableLevel weeklyMoon = Plugin.GetWeeklyMoon(__instance.moonsCatalogueList);
			List<SelectableLevel> list = __instance.moonsCatalogueList.ToList();
			list.RemoveAll((SelectableLevel moon) => moon.PlanetName == dailyMoon.PlanetName);
			list.RemoveAll((SelectableLevel moon) => moon.PlanetName == weeklyMoon.PlanetName);
			list.Add(dailyMoon);
			list.Add(weeklyMoon);
			__instance.moonsCatalogueList = list.ToArray();
		}

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void AddTerminalCommands(Terminal __instance)
		{
			SelectableLevel dailyMoon = Plugin.GetDailyMoon(__instance.moonsCatalogueList);
			SelectableLevel weeklyMoon = Plugin.GetWeeklyMoon(__instance.moonsCatalogueList);
			if (__instance.terminalNodes.allKeywords.Any((TerminalKeyword keyword) => ((Object)keyword).name == PatchTerminalHelpers.GetKWNameForMoon(dailyMoon)))
			{
				Plugin.Logger.LogDebug((object)"Terminal nodes have already been modified.");
				return;
			}
			Plugin.Logger.LogDebug((object)"Modifying terminal nodes...");
			PatchTerminalHelpers.AddTerminalCommand(ref __instance, dailyMoon);
			PatchTerminalHelpers.AddTerminalCommand(ref __instance, weeklyMoon);
			TerminalNode specialKeywordResult = __instance.terminalNodes.allKeywords.First((TerminalKeyword keyword) => ((Object)keyword).name == "Moons").specialKeywordResult;
			specialKeywordResult.displayText += "\n";
		}
	}
}