Decompiled source of MOONApi v1.0.9

BepInEx/plugins/MOON_API.dll

Decompiled a year 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 HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("MOON_API")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Utilities for plugin devs")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MOON_API")]
[assembly: AssemblyTitle("MOON_API")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace MOON_API
{
	[BepInPlugin("MOON_API", "MOON_API", "1.0.0")]
	public class Core : BaseUnityPlugin
	{
		private Harmony _harmony;

		public static IDictionary<string, SelectableLevel> ModdedMoons = new Dictionary<string, SelectableLevel>();

		public static IDictionary<string, GameObject> MoonPrefabs = new Dictionary<string, GameObject>();

		public static IDictionary<string, int> ModdedIds = new Dictionary<string, int>();

		public IDictionary<string, SelectableLevel> mlevels;

		public static IDictionary<string, SelectableLevel> GetMoons()
		{
			return ModdedMoons;
		}

		public static void AddMoon(SelectableLevel level, GameObject Prefab)
		{
			AddPrefab(((Object)level).name, Prefab);
			ModdedMoons[((Object)level).name] = level;
		}

		private static void AddPrefab(string MoonName, GameObject Prefab)
		{
			MoonPrefabs[MoonName] = Prefab;
		}

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("MOON_API");
			_harmony.PatchAll();
			SceneManager.sceneLoaded += OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			if (!(((Scene)(ref scene)).name == "InitSceneLaunchOptions") || !((Object)(object)StartOfRound.Instance != (Object)null) || !((Object)(object)StartOfRound.Instance.currentLevel != (Object)null) || ((Object)StartOfRound.Instance.currentLevel).name == null || !MoonPrefabs.ContainsKey(((Object)StartOfRound.Instance.currentLevel).name))
			{
				return;
			}
			Scene sceneByName = SceneManager.GetSceneByName("InitSceneLaunchOptions");
			GameObject[] rootGameObjects = ((Scene)(ref sceneByName)).GetRootGameObjects();
			foreach (GameObject val in rootGameObjects)
			{
				Debug.Log((object)((Object)val).name);
				Debug.Log((object)"A");
				if (((Object)val).name == "InitSceneScript" || ((Object)val).name == "EventSystem" || ((Object)val).name == "Volume" || ((Object)val).name == "UICamera" || ((Object)val).name == "Canvas")
				{
					val.SetActive(false);
				}
			}
			Object.Instantiate<GameObject>(MoonPrefabs[((Object)StartOfRound.Instance.currentLevel).name]);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "MOON_API";

		public const string PLUGIN_NAME = "MOON_API";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace MOON_API.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class InternalPatch
	{
		private static MethodInfo TargetMethod()
		{
			return typeof(StartOfRound).GetMethod("SetTimeAndPlanetToSavedSettings", BindingFlags.Instance | BindingFlags.NonPublic);
		}

		[HarmonyPrefix]
		private static bool Prefix(StartOfRound __instance)
		{
			string currentSaveFileName = GameNetworkManager.Instance.currentSaveFileName;
			__instance.randomMapSeed = ES3.Load<int>("RandomSeed", currentSaveFileName, 0);
			string text = ES3.Load<string>("ModdedPlanetId", currentSaveFileName, "");
			int num = ((string.IsNullOrEmpty(text) || !Core.ModdedIds.ContainsKey(text)) ? ES3.Load<int>("CurrentPlanetID", currentSaveFileName, __instance.defaultPlanet) : Core.ModdedIds[text]);
			if (!__instance.testRoom.activeInHierarchy)
			{
				__instance.ChangeLevel(num);
				__instance.ChangePlanet();
			}
			TimeOfDay.Instance.timesFulfilledQuota = ES3.Load<int>("QuotasPassed", currentSaveFileName, 0);
			TimeOfDay.Instance.profitQuota = ES3.Load<int>("ProfitQuota", currentSaveFileName, TimeOfDay.Instance.quotaVariables.startingQuota);
			TimeOfDay.Instance.totalTime = TimeOfDay.Instance.lengthOfHours * (float)TimeOfDay.Instance.numberOfHours;
			TimeOfDay.Instance.timeUntilDeadline = ES3.Load<int>("DeadlineTime", currentSaveFileName, (int)(TimeOfDay.Instance.totalTime * (float)TimeOfDay.Instance.quotaVariables.deadlineDaysAmount));
			TimeOfDay.Instance.quotaFulfilled = ES3.Load<int>("QuotaFulfilled", currentSaveFileName, 0);
			TimeOfDay.Instance.SetBuyingRateForDay();
			__instance.gameStats.daysSpent = ES3.Load<int>("Stats_DaysSpent", currentSaveFileName, 0);
			__instance.gameStats.deaths = ES3.Load<int>("Stats_Deaths", currentSaveFileName, 0);
			__instance.gameStats.scrapValueCollected = ES3.Load<int>("Stats_ValueCollected", currentSaveFileName, 0);
			__instance.gameStats.allStepsTaken = ES3.Load<int>("Stats_StepsTaken", currentSaveFileName, 0);
			TimeOfDay.Instance.UpdateProfitQuotaCurrentTime();
			__instance.SetPlanetsWeather(0);
			Object.FindObjectOfType<Terminal>().SetItemSales();
			if (__instance.gameStats.daysSpent == 0)
			{
				MethodInfo method = typeof(StartOfRound).GetMethod("PlayFirstDayShipAnimation", BindingFlags.Static | BindingFlags.NonPublic);
				object[] parameters = new object[1] { true };
				method.Invoke(__instance, parameters);
			}
			if (TimeOfDay.Instance.timeUntilDeadline > 0f && TimeOfDay.Instance.daysUntilDeadline <= 0 && TimeOfDay.Instance.timesFulfilledQuota <= 0)
			{
				typeof(StartOfRound).GetMethod("playDaysLeftAlertSFXDelayed", BindingFlags.Static | BindingFlags.NonPublic).Invoke(__instance, null);
			}
			return false;
		}
	}
	[HarmonyPatch]
	internal class MoonPatches
	{
		public static T[] ResizeArray<T>(T[] oldArray, int newSize)
		{
			T[] array = new T[newSize];
			oldArray.CopyTo(array, 0);
			return array;
		}

		private static int AddToMoons(StartOfRound SOR, SelectableLevel Moon)
		{
			int num = -1;
			for (int i = 0; i < SOR.levels.Length; i++)
			{
				if (SOR.levels[i] == null)
				{
					num = i;
					break;
				}
			}
			if (num == -1)
			{
				throw new NullReferenceException("No null value found in StartOfRound.levels");
			}
			SOR.levels[num] = Moon;
			return num;
		}

		[HarmonyPatch(typeof(GameNetworkManager), "SaveGameValues")]
		[HarmonyPostfix]
		public static void SaveGameValues(GameNetworkManager __instance)
		{
			if (!__instance.isHostingGame || !StartOfRound.Instance.inShipPhase)
			{
				return;
			}
			try
			{
				if ((Object)(object)StartOfRound.Instance.currentLevel != (Object)null && Core.ModdedMoons.ContainsKey(((Object)StartOfRound.Instance.currentLevel).name))
				{
					ES3.Save<string>("ModdedPlanetID", ((Object)StartOfRound.Instance.currentLevel).name, __instance.currentSaveFileName);
				}
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"Error while trying to save game values when disconnecting as host: {arg}");
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "Awake")]
		[HarmonyPrefix]
		[HarmonyPriority(0)]
		private static void Awake(StartOfRound __instance)
		{
			__instance.levels = ResizeArray(__instance.levels, __instance.levels.Length + Core.ModdedMoons.Count());
			foreach (KeyValuePair<string, SelectableLevel> moddedMoon in Core.ModdedMoons)
			{
				int num = AddToMoons(__instance, moddedMoon.Value);
				moddedMoon.Value.levelID = num;
				Core.ModdedIds[moddedMoon.Key] = num;
			}
		}
	}
	[HarmonyPatch]
	internal class TerminalPatches
	{
		private static bool DontRun;

		[HarmonyPatch(typeof(Terminal), "Awake")]
		[HarmonyPrefix]
		public static bool Awake(Terminal __instance)
		{
			if (DontRun)
			{
				return true;
			}
			IDictionary<string, SelectableLevel> moons = Core.GetMoons();
			TerminalNode specialKeywordResult = __instance.terminalNodes.allKeywords[21].specialKeywordResult;
			specialKeywordResult.displayText.Substring(specialKeywordResult.displayText.Length - 3);
			foreach (KeyValuePair<string, SelectableLevel> item in moons)
			{
				specialKeywordResult.displayText = specialKeywordResult.displayText + "\n* " + item.Value.PlanetName + " [planetTime]";
			}
			specialKeywordResult.displayText += "\n\n";
			DontRun = true;
			return true;
		}
	}
}