Decompiled source of End of day countdown v1.1.0

LC CountDownMod.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.IO;
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 LC_CountDownMod;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LC CountDownMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP")]
[assembly: AssemblyProduct("LC CountDownMod")]
[assembly: AssemblyCopyright("Copyright © HP 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("11a234bc-517b-4953-b6a2-618fe9f49eab")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[CreateAssetMenu(fileName = "CountDowner", menuName = "EODCDmod/CountDowner", order = 1)]
public class Countdowner : ScriptableObject
{
	public GameObject CountdownPrefab;

	private GameObject PrefabInstance;

	private Animator Anim;

	private AudioSource Count1;

	private AudioSource Count2;

	private Canvas CountdownCanvas;

	public void SpawnCountdown()
	{
		PrefabInstance = Object.Instantiate<GameObject>(CountdownPrefab);
		CountdownCanvas = PrefabInstance.GetComponent<Canvas>();
		Anim = ((Component)PrefabInstance.transform.Find("Counter")).GetComponent<Animator>();
		Count1 = ((Component)PrefabInstance.transform.Find("CountA")).GetComponentInChildren<AudioSource>();
		Count2 = ((Component)PrefabInstance.transform.Find("CountB")).GetComponentInChildren<AudioSource>();
	}

	public void DespawnCountdown()
	{
		Object.Destroy((Object)(object)PrefabInstance);
		Anim = null;
		Count1 = null;
		Count2 = null;
		CountdownCanvas = null;
	}

	public void Countdown(int Count)
	{
		if (Count < 0 || Count > 10)
		{
			throw new ArgumentOutOfRangeException("Count", "Limit must be between 0 and 10");
		}
		switch (Count)
		{
		case 10:
			Anim.Play("Ten");
			Count1.Play();
			Count2.Play();
			break;
		case 9:
			Anim.Play("Nine");
			Count1.Play();
			Count2.Play();
			break;
		case 8:
			Anim.Play("Eight");
			Count1.Play();
			Count2.Play();
			break;
		case 7:
			Anim.Play("Seven");
			Count1.Play();
			Count2.Play();
			break;
		case 6:
			Anim.Play("Six");
			Count1.Play();
			Count2.Play();
			break;
		case 5:
			Anim.Play("Five");
			Count1.Play();
			Count2.Play();
			break;
		case 4:
			Anim.Play("Four");
			Count1.Play();
			Count2.Play();
			break;
		case 3:
			Anim.Play("Three");
			Count1.Play();
			Count2.Play();
			break;
		case 2:
			Anim.Play("Two");
			Count1.Play();
			Count2.Play();
			break;
		case 1:
			Anim.Play("One");
			Count1.Play();
			Count2.Play();
			break;
		case 0:
			Anim.Play("Zero");
			Count1.Play();
			Count2.Play();
			break;
		}
	}

	public void SetCanvasLayer(int layer)
	{
		if ((Object)(object)CountdownCanvas == (Object)null)
		{
			Debug.LogError((object)"CountdownCanvas is null!");
			return;
		}
		CountdownCanvas.sortingOrder = layer;
		if (!((Object)(object)GameNetworkManager.Instance.localPlayerController.gameplayCamera == (Object)null))
		{
			if (CountDownModBase.CDScreenSpace.Value)
			{
				CountdownCanvas.renderMode = (RenderMode)1;
				CountdownCanvas.worldCamera = ((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).GetComponent<Camera>();
				CountdownCanvas.planeDistance = ((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).GetComponent<Camera>().nearClipPlane + 0.1f;
			}
			else
			{
				CountdownCanvas.renderMode = (RenderMode)0;
			}
		}
	}
}
namespace LC_CountDownMod
{
	[BepInPlugin("NoteBoxz.CountDownMod", "LC End of day CountDown", "1.1.0")]
	public class CountDownModBase : BaseUnityPlugin
	{
		private const string modGUID = "NoteBoxz.CountDownMod";

		private const string modName = "LC End of day CountDown";

		private const string modVersion = "1.1.0";

		private readonly Harmony harmony = new Harmony("NoteBoxz.CountDownMod");

		internal ManualLogSource logSource;

		public static CountDownModBase instance;

		public static AssetBundle CustomAssets;

		public static Countdowner countdowner;

		public static ConfigEntry<int> CountdownUISortOrder;

		public static ConfigEntry<bool> DebugMode;

		public static ConfigEntry<bool> CDScreenSpace;

		public static ConfigEntry<bool> StartCountdownWhenShipLeaveEarly;

		public static ConfigEntry<bool> StopCountdownAfterDeath;

		public static ConfigEntry<bool> StopCountdownAfterShipLeaves;

		public static ConfigEntry<bool> StopCountdownAfterTwelve;

		public static Camera PlayerCamera;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			CountdownUISortOrder = ((BaseUnityPlugin)this).Config.Bind<int>("UI", "CountdownUISortOrder", 0, "Changes the sorting order of the Countdown UI canvas");
			CDScreenSpace = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "ShouldCountdownBeCameraSpace", true, "Changes the count down's UI to be camera space, meaning that it will disappear at the same time the other UI disappear and it won't overlay the pause meun. Note that this could also cause so visual errors");
			StartCountdownWhenShipLeaveEarly = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "StartCountdownWhenShipVotedLeaveEarly", true, "Starts the countdown when ship is voted to leave early.");
			DebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugMode", false, "Enables info like TimeOfDay in the modding console and allows you to play the countdown animation by pressing the 5 key");
			StopCountdownAfterDeath = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "StopCountdownAfterDeath", true, "Stops the countdown when the player dies");
			StopCountdownAfterShipLeaves = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "StopCountdownAfterShipLeaves", true, "Stop the countdown if the ship leaves during the countdown.");
			StopCountdownAfterTwelve = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "StopCountdownAfter12Am", true, "Stop the countdown when it's 12 AM during the countdown.");
			((BaseUnityPlugin)this).Config.Save();
			logSource = Logger.CreateLogSource("NoteBoxz.CountDownMod");
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			CustomAssets = AssetBundle.LoadFromFile(Path.Combine(directoryName, "endofdaycountdownmod"));
			countdowner = ScriptableObject.CreateInstance<Countdowner>();
			countdowner.CountdownPrefab = CustomAssets.LoadAsset<GameObject>("Assets/ModAssets/Prefabs/Countdown Method 2.prefab");
			if ((Object)(object)CustomAssets == (Object)null)
			{
				logSource.LogError((object)"Failed to load custom assets.");
				return;
			}
			if ((Object)(object)countdowner == (Object)null)
			{
				logSource.LogError((object)"CountdownTimer failed to initalize.");
				return;
			}
			if ((Object)(object)countdowner.CountdownPrefab == (Object)null)
			{
				logSource.LogError((object)"CountdownTimer prefab failed to load.");
				return;
			}
			logSource.LogInfo((object)"Final CountDown has started");
			harmony.PatchAll();
		}
	}
}
namespace LC_CountDownMod.Patches
{
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDManagerPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void HudCamPatch()
		{
		}
	}
	[HarmonyPatch(typeof(TimeOfDay))]
	internal class TimeOfDayUpdatePatch
	{
		private static bool CountingDown;

		private static bool CalcuatedWhenToCountdown;

		private static float DownTimer;

		private static int Number;

		private static SelectableLevel PatchcurrentLevel;

		private static InputAction keyAction = new InputAction((string)null, (InputActionType)0, "<Keyboard>/5", (string)null, (string)null, (string)null);

		private static float DebugTimer;

		private static float ShipLeavePerdictionNormalized;

		private static float ShipLeavePerdiction;

		private static float PatchGlobalTime;

		private static float PatchTotalTime;

		private static float EarlyCountDownTime;

		private static float EarlyCountDownDuration = 46.5f;

		private static int PlayersInMatch;

		private static bool DisableDisabler = false;

		private static bool LeavingEarly;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void CountDownPatch(ref float ___currentDayTime, ref SelectableLevel ___currentLevel, ref float ___normalizedTimeOfDay, ref float ___totalTime, ref int ___numberOfHours, ref float ___globalTime, ref int ___votesForShipToLeaveEarly)
		{
			PatchGlobalTime = ___globalTime;
			PatchcurrentLevel = ___currentLevel;
			PatchTotalTime = ___totalTime;
			PlayersInMatch = StartOfRound.Instance.connectedPlayersAmount + 1;
			int num = ((PlayersInMatch <= 1) ? 1 : (StartOfRound.Instance.connectedPlayersAmount + 1 - StartOfRound.Instance.livingPlayers));
			LeavingEarly = ___votesForShipToLeaveEarly >= num;
			if (!LeavingEarly)
			{
				ShipLeavePerdiction = (___normalizedTimeOfDay + 0.1f) * ___totalTime;
				ShipLeavePerdictionNormalized = ___normalizedTimeOfDay + 0.1f;
			}
			if (CountDownModBase.DebugMode.Value)
			{
				if (DebugTimer > 0f)
				{
					DebugTimer -= Time.deltaTime;
				}
				else
				{
					Debug.Log((object)"-GAMETIME-");
					Debug.Log((object)("ClockTime " + GetClock(___normalizedTimeOfDay, ___numberOfHours, createNewLine: false)));
					Debug.Log((object)("ShipLeaveClockTimePerdiction " + GetClock(ShipLeavePerdictionNormalized, ___numberOfHours, createNewLine: false)));
					Debug.Log((object)("NormalizedDayTime: " + ___normalizedTimeOfDay));
					Debug.Log((object)("ShipLeavePerditionNormal: " + ShipLeavePerdictionNormalized));
					Debug.Log((object)("CurrentDayTime: " + ___currentDayTime));
					Debug.Log((object)("ShipLeavePerdition: " + ShipLeavePerdiction));
					Debug.Log((object)("NumberOfHours: " + ___numberOfHours));
					Debug.Log((object)("TotalTime " + ___totalTime));
					Debug.Log((object)("connectedPlayers: " + PlayersInMatch));
					Debug.Log((object)"-COUNTDOWN-");
					Debug.Log((object)("EarlyCountDownTime: " + EarlyCountDownTime));
					Debug.Log((object)("LeavingEarly: " + LeavingEarly));
					Debug.Log((object)("CountingDown: " + CountingDown));
					Debug.Log((object)("DownTimer: " + DownTimer));
					Debug.Log((object)("Number: " + Number));
					DebugTimer = 0.1f;
				}
				keyAction.Enable();
				keyAction.performed += delegate
				{
					DisableDisabler = true;
					DownTimer = 3f;
					Number = 9;
					CountingDown = true;
					CountDownModBase.countdowner.SpawnCountdown();
					CountDownModBase.countdowner.SetCanvasLayer(CountDownModBase.CountdownUISortOrder.Value);
					CountDownModBase.countdowner.Countdown(10);
				};
			}
			if (___currentDayTime >= 1032f && ___currentDayTime <= 1035f && !CountingDown)
			{
				DisableDisabler = false;
				DownTimer = 3f;
				Number = 9;
				CountingDown = true;
				CountDownModBase.countdowner.SpawnCountdown();
				CountDownModBase.countdowner.SetCanvasLayer(CountDownModBase.CountdownUISortOrder.Value);
				CountDownModBase.countdowner.Countdown(10);
			}
			if (LeavingEarly && !CalcuatedWhenToCountdown)
			{
				CalcuatedWhenToCountdown = true;
				EarlyCountDownTime = ShipLeavePerdiction - EarlyCountDownDuration;
			}
			if (___currentDayTime >= EarlyCountDownTime && ___currentDayTime <= EarlyCountDownTime + 3f && CountDownModBase.StartCountdownWhenShipLeaveEarly.Value && LeavingEarly && CalcuatedWhenToCountdown && ___currentDayTime >= 120f && !CountingDown)
			{
				DisableDisabler = false;
				DownTimer = 3f;
				Number = 9;
				CountingDown = true;
				CountDownModBase.countdowner.SpawnCountdown();
				CountDownModBase.countdowner.SetCanvasLayer(CountDownModBase.CountdownUISortOrder.Value);
				CountDownModBase.countdowner.Countdown(10);
			}
			if (CountingDown)
			{
				if (DownTimer > 0f)
				{
					DownTimer -= Time.deltaTime * ___currentLevel.DaySpeedMultiplier;
				}
				else if (Number > -1)
				{
					CountDownModBase.countdowner.SetCanvasLayer(CountDownModBase.CountdownUISortOrder.Value);
					CountDownModBase.countdowner.Countdown(Number);
					Number--;
					DownTimer = 3f;
				}
				else
				{
					StopCountdown();
				}
				if (___currentDayTime >= 1079f && CountDownModBase.StopCountdownAfterTwelve.Value && !DisableDisabler)
				{
					StopCountdown();
				}
				if (StartOfRound.Instance.shipIsLeaving && CountDownModBase.StopCountdownAfterShipLeaves.Value && !DisableDisabler)
				{
					StopCountdown();
				}
				if (GameNetworkManager.Instance.localPlayerController.isPlayerDead && CountDownModBase.StopCountdownAfterDeath.Value)
				{
					StopCountdown();
				}
			}
		}

		public static void StopCountdown()
		{
			CountDownModBase.countdowner.DespawnCountdown();
			Number = 0;
			DownTimer = 0f;
			CountingDown = false;
			DisableDisabler = false;
			CalcuatedWhenToCountdown = false;
		}

		public static string GetClock(float timeNormalized, float numberOfHours, bool createNewLine = true)
		{
			int num = (int)(timeNormalized * (60f * numberOfHours)) + 360;
			int num2 = (int)Mathf.Floor((float)(num / 60));
			string text = "";
			string text2 = "";
			text = (createNewLine ? "\n" : " ");
			text2 = text + "AM";
			if (num2 >= 24)
			{
				return "12:00\nAM";
			}
			text2 = ((num2 >= 12) ? (text + "PM") : (text + "AM"));
			if (num2 > 12)
			{
				num2 %= 12;
			}
			int num3 = num % 60;
			return $"{num2:00}:{num3:00}".TrimStart(new char[1] { '0' }) + text2;
		}

		public static float PlanetTime()
		{
			return (PatchGlobalTime + PatchcurrentLevel.OffsetFromGlobalTime) * PatchcurrentLevel.DaySpeedMultiplier % (PatchTotalTime + 1f);
		}
	}
}