Decompiled source of LCSeedPicker v1.2.2

LCSeedPicker.dll

Decompiled 9 months ago
using System;
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 LCSeedPicker.Components;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LCSeedPicker")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("A seed picker mod for Lethal Company")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyInformationalVersion("1.2.1")]
[assembly: AssemblyProduct("LCSeedPicker")]
[assembly: AssemblyTitle("LCSeedPicker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.1.0")]
[module: UnverifiableCode]
namespace LCSeedPicker
{
	[BepInPlugin("LCSeedPicker", "LCSeedPicker", "1.2.1")]
	public class Plugin : BaseUnityPlugin
	{
		internal static GameObject SeedInput;

		internal static ManualLogSource Logger;

		private Harmony _harmony;

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

		public void PatchAll()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			Logger.LogDebug((object)"Applying harmony patches");
			_harmony = new Harmony("LCSeedPicker");
			_harmony.PatchAll();
			Logger.LogDebug((object)"Patching done.");
		}

		public void UnpatchAll()
		{
			Logger.LogDebug((object)"Removing harmony patches.");
			_harmony.UnpatchSelf();
			Logger.LogDebug((object)"Unpatching done.");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LCSeedPicker";

		public const string PLUGIN_NAME = "LCSeedPicker";

		public const string PLUGIN_VERSION = "1.2.1";
	}
}
namespace LCSeedPicker.Patches
{
	[HarmonyPatch(typeof(GameNetworkManager))]
	internal class PatchGameNetworkManager
	{
		[HarmonyPatch("StartHost")]
		[HarmonyPrefix]
		public static void PatchOnLobbyCreated()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			if ((Object)(object)Plugin.SeedInput == (Object)null)
			{
				Plugin.SeedInput = new GameObject("SeedInputField");
				((Object)Plugin.SeedInput).hideFlags = (HideFlags)61;
				Plugin.SeedInput.AddComponent<SeedInput>();
			}
		}
	}
	[HarmonyPatch(typeof(QuickMenuManager))]
	internal class PatchQuickMenuManager
	{
		[HarmonyPatch("OpenQuickMenu")]
		[HarmonyPrefix]
		public static void openMenu()
		{
			open();
		}

		[HarmonyPatch("CloseQuickMenu")]
		[HarmonyPrefix]
		public static void closeMenu()
		{
			close();
		}

		[HarmonyPatch("LeaveGameConfirm")]
		[HarmonyPrefix]
		public static void destroyMenuOnLeave()
		{
			SeedInput seedInput = GetSeedInput();
			if ((Object)(object)seedInput != (Object)null)
			{
				seedInput.DestroyInput();
			}
			Plugin.SeedInput = null;
		}

		[HarmonyPatch("LeaveGame")]
		[HarmonyPrefix]
		public static void openLeaveGameWindow()
		{
			close();
		}

		private static void open()
		{
			SeedInput seedInput = GetSeedInput();
			if ((Object)(object)seedInput != (Object)null)
			{
				seedInput.isVisible = true;
			}
		}

		private static void close()
		{
			SeedInput seedInput = GetSeedInput();
			if ((Object)(object)seedInput != (Object)null)
			{
				seedInput.isVisible = false;
			}
		}

		private static SeedInput GetSeedInput()
		{
			if ((Object)(object)Plugin.SeedInput == (Object)null)
			{
				return null;
			}
			return Plugin.SeedInput.GetComponent<SeedInput>();
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	public class PatchStartOfRound
	{
		[HarmonyPatch("StartGame")]
		[HarmonyPrefix]
		[HarmonyAfter(new string[] { "MoonOfTheDay" })]
		[HarmonyPriority(0)]
		public static void SetSeed(StartOfRound __instance)
		{
			if ((Object)(object)Plugin.SeedInput == (Object)null)
			{
				return;
			}
			SeedInput component = Plugin.SeedInput.GetComponent<SeedInput>();
			if (!((Object)(object)__instance.currentLevel == (Object)null) && !((Object)(object)component == (Object)null))
			{
				int seed = component.GetSeed();
				if (seed == -1)
				{
					Plugin.Logger.LogDebug((object)("Using random seed for " + __instance.currentLevel.PlanetName + "..."));
					__instance.overrideRandomSeed = false;
				}
				else
				{
					Plugin.Logger.LogDebug((object)$"Setting seed to {seed} for {__instance.currentLevel.PlanetName}...");
					__instance.overrideRandomSeed = true;
					__instance.overrideSeedNumber = seed;
				}
			}
		}

		[HarmonyPatch("StartGame")]
		[HarmonyPostfix]
		public static void GetSeed(StartOfRound __instance)
		{
			SeedInput component = Plugin.SeedInput.GetComponent<SeedInput>();
			if (!((Object)(object)__instance.currentLevel == (Object)null) && !((Object)(object)component == (Object)null) && !__instance.overrideRandomSeed)
			{
				component.SetLastSeed(__instance.randomMapSeed);
			}
		}
	}
}
namespace LCSeedPicker.Components
{
	public class SeedInput : MonoBehaviour
	{
		public bool isVisible;

		private string _seedInputString = "";

		private string _seedOutputString = "";

		private int _maxSeedLength = 100;

		private float _inputWidth;

		private float _inputHeight;

		private GUIStyle _inputStyle;

		public void Awake()
		{
			Object.DontDestroyOnLoad((Object)(object)this);
			isVisible = false;
			Plugin.Logger.LogInfo((object)"SeedInput created and Awake.");
			_inputWidth = Math.Max((float)Screen.width / 12f, 100f);
			_inputHeight = Math.Max((float)Screen.height / 12f, 10f);
		}

		private void OnGUI()
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			if (isVisible)
			{
				if (_inputStyle == null)
				{
					_inputStyle = new GUIStyle(GUI.skin.textField);
				}
				float num = _inputWidth * 2f;
				float num2 = (float)(Screen.height / 2) - _inputHeight / 2f;
				GUILayout.BeginArea(new Rect(num, num2, _inputWidth, _inputHeight));
				GUILayout.Label("Seed (empty for random):", Array.Empty<GUILayoutOption>());
				_seedInputString = FilterSeedInput(GUILayout.TextField(_seedInputString, _maxSeedLength, _inputStyle, Array.Empty<GUILayoutOption>()));
				GUILayout.EndArea();
				GUILayout.BeginArea(new Rect(num + _inputWidth, num2, _inputWidth, _inputHeight));
				GUILayout.Label("Last random seed:", Array.Empty<GUILayoutOption>());
				GUILayout.TextArea(_seedOutputString, _inputStyle, Array.Empty<GUILayoutOption>());
				GUILayout.EndArea();
			}
		}

		private string FilterSeedInput(string input)
		{
			if (string.IsNullOrWhiteSpace(input))
			{
				return "";
			}
			return new string(input.Where((char c) => char.IsDigit(c) || c == '.').ToArray());
		}

		public int GetSeed()
		{
			int num;
			try
			{
				num = int.Parse(_seedInputString);
			}
			catch (Exception)
			{
				num = -1;
			}
			if (num < 0 || (double)num > Math.Pow(10.0, _maxSeedLength))
			{
				num = -1;
			}
			return num;
		}

		public void SetLastSeed(int lastSeed)
		{
			_seedOutputString = $"{lastSeed}";
		}

		public void DestroyInput()
		{
			Object.Destroy((Object)(object)this);
		}
	}
}