Decompiled source of LCLightsStartOffMod v2.0.5

LCLightsStartOffMod.dll

Decompiled 10 months ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LCLightsStartOffMod.Patches;
using TerminalApi;
using TerminalApi.Events;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LCLightsStartOffMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LCLightsStartOffMod")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("95b746fa-cff7-49a2-89ac-867d303d2a38")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LCLightsStartOffMod
{
	[BepInPlugin("Joe.LCLightsStartOffMod", "LC Lights Start Off", "2.0.5")]
	[BepInDependency("atomic.terminalapi", "1.3.0")]
	public class LightsStartOffMod : BaseUnityPlugin
	{
		private const string modGUID = "Joe.LCLightsStartOffMod";

		private const string modName = "LC Lights Start Off";

		private const string modVersion = "2.0.5";

		private readonly Harmony harmony = new Harmony("Joe.LCLightsStartOffMod");

		public static LightsStartOffMod Instance;

		public static ManualLogSource mls;

		public static ConfigEntry<int> configPercentChance;

		public static ConfigEntry<bool> disableTerminalCommands;

		public static ConfigEntry<bool> disableClientPermissions;

		public const string localKey = "lso.localPercentChance";

		public const string defaultKey = "lso.defaultPercentChance";

		public const string permissionsKey = "lso.disableClientPermissions";

		private static bool developmentBuild;

		public static void Log(string text)
		{
			if (developmentBuild)
			{
				text = "\n\n\n" + text + "\n\n\n";
			}
			mls.LogInfo((object)text);
		}

		private void Awake()
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("Joe.LCLightsStartOffMod");
			configPercentChance = ((BaseUnityPlugin)this).Config.Bind<int>("Joe.LCLightsStartOffMod", "lightStartOffChance", 100, "Set the percent chance the lights turn off at the beginning of a round.");
			disableTerminalCommands = ((BaseUnityPlugin)this).Config.Bind<bool>("Joe.LCLightsStartOffMod", "disableTerminalCommands", false, "If true, disables all terminal commands.");
			disableClientPermissions = ((BaseUnityPlugin)this).Config.Bind<bool>("Joe.LCLightsStartOffMod", "disableClientPermissions", false, "If true, disables terminal commands for clients.");
			Events.TerminalParsedSentence += new TerminalParseSentenceEventHandler(TextSubmitted);
			Events.TerminalAwake += new TerminalEventHandler(TerminalIsAwake);
			harmony.PatchAll(typeof(LCLightsStartOffModPatch));
		}

		private static void initializeLocalVariable(string key, object value, bool skipExistenceCheck = false)
		{
			if (skipExistenceCheck || !ES3.KeyExists(key, GameNetworkManager.Instance.currentSaveFileName))
			{
				ES3.Save(key, value, GameNetworkManager.Instance.currentSaveFileName);
			}
		}

		private static void TextSubmitted(object sender, TerminalParseSentenceEventArgs e)
		{
			TerminalNode returnedNode = e.ReturnedNode;
			string text = e.SubmittedText.ToLower();
			Log("Entered command: " + text);
			if (text.StartsWith("lso"))
			{
				string outputText = executeCommand(text.Substring(3));
				returnedNode.displayText = getDisplayText(outputText);
			}
		}

		private static void TerminalIsAwake(object sender, TerminalEventArgs e)
		{
			if (GameNetworkManager.Instance.isHostingGame)
			{
				initializeLocalVariable("lso.localPercentChance", -1);
				initializeLocalVariable("lso.defaultPercentChance", configPercentChance.Value, skipExistenceCheck: true);
				initializeLocalVariable("lso.disableClientPermissions", disableClientPermissions.Value);
				if (!disableTerminalCommands.Value)
				{
					TerminalApi.AddCommand("lso", "", "", true);
				}
			}
		}

		private static string executeCommand(string command)
		{
			string text = "";
			string[] array = command.Split(new char[1] { ' ' }).Skip(1).ToArray();
			bool flag = (bool)ES3.Load("lso.disableClientPermissions", GameNetworkManager.Instance.currentSaveFileName);
			string action = getAction(array);
			if (action == "set" && array.Length == 3)
			{
				string text2 = array[1];
				string text3 = array[2];
				switch (text2)
				{
				case "chance":
					if (GameNetworkManager.Instance.isHostingGame || !flag)
					{
						if (text3 == "default")
						{
							SaveInt("lso.localPercentChance", -1);
							break;
						}
						int num2 = convertToValidInt(text3);
						if (num2 == -1)
						{
							text = "Invalid numnber entered. Must be between 0 and 100.";
						}
						else
						{
							SaveInt("lso.localPercentChance", num2);
						}
					}
					else
					{
						text = "Must be host to use this command.";
					}
					break;
				case "config":
				{
					int num = convertToValidInt(text3);
					if (num == -1)
					{
						text = "Invalid numnber entered. Must be between 0 and 100.";
						break;
					}
					configPercentChance.Value = num;
					if (GameNetworkManager.Instance.isHostingGame)
					{
						SaveInt("lso.defaultPercentChance", num);
					}
					break;
				}
				case "permission":
					if (GameNetworkManager.Instance.isHostingGame)
					{
						if (text3 == "client")
						{
							SaveBool("lso.disableClientPermissions", value: false);
						}
						else if (text3 == "host")
						{
							SaveBool("lso.disableClientPermissions", value: true);
						}
						else
						{
							text = "Invalid target entered. Must be host or client.";
						}
					}
					else
					{
						text = "Must be host to use this command.";
					}
					break;
				default:
					text = "Invalid target entered. Must be chance, default, or permissions.";
					break;
				}
			}
			else if (!(command == "") && !(command == " "))
			{
				text = "Invalid command: " + command;
			}
			if (text == "")
			{
				return "";
			}
			return text + "\n\n";
		}

		private static string getDisplayText(string outputText)
		{
			string text = "";
			string text2 = "";
			if (GameNetworkManager.Instance.isHostingGame)
			{
				text = "lso set chance [0-100 | default]\n";
				text2 = "lso set permission [host | client]\n";
			}
			if (!LoadBool("lso.disableClientPermissions"))
			{
				text = "lso set chance [0-100 | default]\n";
			}
			string text3 = "";
			if (GameNetworkManager.Instance.gameHasStarted && developmentBuild)
			{
				text3 = $" Lights Are Powered = {Object.FindObjectOfType<BreakerBox>().isPowerOn}\n";
			}
			int num = LoadInt("lso.defaultPercentChance");
			string currentValueString = getCurrentValueString();
			string permission = getPermission();
			return "\n" + $"  Config LSO Chance = {configPercentChance.Value}%\n" + $" Default LSO Chance = {num}%\n" + " Current LSO Chance = " + currentValueString + "\nRequired Permission = " + permission + "\n" + text3 + "\n\nInputs: \n\nlso set config [0-100]\n" + text + text2 + "\n\n" + outputText;
		}

		private static string getCurrentValueString()
		{
			int num = LoadInt("lso.localPercentChance");
			if (num == -1)
			{
				return "default";
			}
			return $"{num}%";
		}

		private static string getPermission()
		{
			if (LoadBool("lso.disableClientPermissions"))
			{
				return "host";
			}
			return "client";
		}

		private static string getAction(string[] array)
		{
			if (array.Length != 0)
			{
				return array[0];
			}
			return "";
		}

		private static int convertToValidInt(string stringValue)
		{
			if (int.TryParse(stringValue, NumberStyles.Number, CultureInfo.CurrentCulture.NumberFormat, out var result) && (result >= 0 || result <= 100))
			{
				return result;
			}
			return -1;
		}

		public static int LoadInt(string key)
		{
			return (int)ES3.Load(key, GameNetworkManager.Instance.currentSaveFileName);
		}

		public static bool LoadBool(string key)
		{
			return (bool)ES3.Load(key, GameNetworkManager.Instance.currentSaveFileName);
		}

		public static void SaveInt(string key, int value)
		{
			ES3.Save<int>(key, value, GameNetworkManager.Instance.currentSaveFileName);
		}

		public static void SaveBool(string key, bool value)
		{
			ES3.Save<bool>(key, value, GameNetworkManager.Instance.currentSaveFileName);
		}
	}
}
namespace LCLightsStartOffMod.Patches
{
	public class LCLightsStartOffModPatch
	{
		[HarmonyPatch(typeof(RoundManager), "SetPowerOffAtStart")]
		[HarmonyPostfix]
		private static void LightsStartOffPatch()
		{
			int currentChance = getCurrentChance();
			int num = new Random(StartOfRound.Instance.randomMapSeed + 3).Next(100);
			LightsStartOffMod.Log($"Lights Start Off: Random Value -> {num} / {currentChance + 1} <- target for lights on");
			if (num < currentChance)
			{
				RoundManager.Instance.PowerSwitchOffClientRpc();
				RoundManager.Instance.TurnBreakerSwitchesOff();
				LightsStartOffMod.Log("The lights have been turned off");
			}
			else
			{
				RoundManager.Instance.PowerSwitchOnClientRpc();
				LightsStartOffMod.Log("The lights have been kept on");
			}
		}

		private static int getCurrentChance()
		{
			int num = LightsStartOffMod.LoadInt("lso.localPercentChance");
			if (num != -1)
			{
				LightsStartOffMod.Log($"Loaded key lso.localPercentChance = {num}");
				return num;
			}
			int num2 = LightsStartOffMod.LoadInt("lso.defaultPercentChance");
			LightsStartOffMod.Log($"Loaded key lso.defaultPercentChance = {num2}");
			return num2;
		}
	}
}