Decompiled source of DeathBlackout v0.0.2

BepInEx/plugins/Death Screen.dll

Decompiled 6 days ago
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Death Screen")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Death Screen")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e7ded5df-8932-42b8-bedd-476e16598039")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DeathBlackoutMod;

[BepInPlugin("cy.deathblackout", "Death Blackout", "0.0.2")]
public class DeathBlackout : BaseUnityPlugin
{
	[HarmonyPatch(typeof(HUDManager))]
	public class HUDManagerPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void PostAwake(HUDManager __instance)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
			TMP_FontAsset[] array2 = array;
			foreach (TMP_FontAsset val in array2)
			{
				if (((Object)val).name.Contains("3270-Regular SDF"))
				{
					gameFont = val;
					break;
				}
			}
			if ((Object)(object)BlackRectObject == (Object)null)
			{
				GameObject hUDContainer = __instance.HUDContainer;
				BlackRectObject = new GameObject("BlackRect");
				BlackRectObject.transform.SetParent(hUDContainer.transform, false);
				BlackRectObject.transform.SetAsFirstSibling();
				blackCanvasGroup = BlackRectObject.AddComponent<CanvasGroup>();
				blackCanvasGroup.alpha = 0f;
				blackCanvasGroup.ignoreParentGroups = true;
				Image val2 = BlackRectObject.AddComponent<Image>();
				((Graphic)val2).color = new Color(0f, 0f, 0f, 1f);
				RectTransform component = BlackRectObject.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(0f, 0f);
				component.anchorMax = new Vector2(1f, 1f);
				GameObject val3 = new GameObject("DeathText");
				val3.transform.SetParent(BlackRectObject.transform, false);
				deathTextComponent = val3.AddComponent<TextMeshProUGUI>();
				((TMP_Text)deathTextComponent).text = "";
				((TMP_Text)deathTextComponent).fontSize = fontSize;
				((TMP_Text)deathTextComponent).alignment = (TextAlignmentOptions)514;
				((Graphic)deathTextComponent).color = Color.red;
				if ((Object)(object)gameFont != (Object)null)
				{
					((TMP_Text)deathTextComponent).font = gameFont;
				}
				RectTransform component2 = val3.GetComponent<RectTransform>();
				component2.sizeDelta = new Vector2(600f, 100f);
				component2.anchoredPosition = new Vector2(0f, 0f);
			}
		}
	}

	[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
	public class PlayerControllerB_KillPlayer_Patch
	{
		[HarmonyPostfix]
		public static void Postfix(PlayerControllerB __instance)
		{
			if ((Object)(object)blackCanvasGroup != (Object)null)
			{
				blackCanvasGroup.alpha = 1f;
				if (muteAudioOnDeath)
				{
					originalVolume = AudioListener.volume;
					AudioListener.volume = 0f;
				}
				if ((Object)(object)BlackRectObject != (Object)null && (Object)(object)deathTextComponent != (Object)null)
				{
					((MonoBehaviour)__instance).StartCoroutine(StartDeathBlackoutSequence(__instance));
				}
			}
		}
	}

	private const string ModGUID = "cy.deathblackout";

	private const string ModName = "Death Blackout";

	private const string ModVersion = "0.0.2";

	private readonly Harmony harmony = new Harmony("cy.deathblackout");

	public static float textDelay;

	public static float textTypingDuration;

	public static float postTextPauseDuration;

	public static float fadeDuration;

	public static bool muteAudioOnDeath;

	public static bool showDeathText;

	public static string deathMessage;

	public static float fontSize;

	public static bool blackoutUntilRespawn;

	private static GameObject BlackRectObject;

	private static CanvasGroup blackCanvasGroup;

	private static TextMeshProUGUI deathTextComponent;

	private static float originalVolume;

	private static TMP_FontAsset gameFont;

	private void Awake()
	{
		blackoutUntilRespawn = ((BaseUnityPlugin)this).Config.Bind<bool>("Blackout", "BlackoutUntilRespawn", false, "Should the blackout persist until the player respawns?").Value;
		muteAudioOnDeath = ((BaseUnityPlugin)this).Config.Bind<bool>("Blackout", "MuteAudioOnDeath", true, "Should audio be muted when the player dies?").Value;
		showDeathText = ((BaseUnityPlugin)this).Config.Bind<bool>("Blackout", "ShowDeathText", true, "Should the death message be displayed?").Value;
		deathMessage = ((BaseUnityPlugin)this).Config.Bind<string>("Blackout", "DeathMessage", "You Died..", "The message displayed when the player dies").Value;
		fontSize = ((BaseUnityPlugin)this).Config.Bind<float>("Blackout", "FontSize", 64f, "Font size for the death message").Value;
		textDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Blackout", "TextDelay", 4f, "Time before the death message starts typing (in seconds)").Value;
		textTypingDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Blackout", "TextTypingDuration", 2f, "Duration it takes to type the death message (in seconds)").Value;
		postTextPauseDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Blackout", "PostTextPauseDuration", 3f, "Pause after the message is typed before fading out (in seconds)").Value;
		fadeDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Blackout", "FadeDuration", 5f, "Duration of the fade-out effect (in seconds)").Value;
		harmony.PatchAll();
	}

	private static IEnumerator StartDeathBlackoutSequence(PlayerControllerB playerController)
	{
		yield return (object)new WaitForSeconds(textDelay);
		if (showDeathText)
		{
			float delayPerCharacter = textTypingDuration / (float)deathMessage.Length;
			((TMP_Text)deathTextComponent).text = "";
			for (int i = 0; i < deathMessage.Length; i++)
			{
				TextMeshProUGUI obj = deathTextComponent;
				((TMP_Text)obj).text = ((TMP_Text)obj).text + deathMessage[i];
				yield return (object)new WaitForSeconds(delayPerCharacter);
			}
		}
		yield return (object)new WaitForSeconds(postTextPauseDuration);
		if (blackoutUntilRespawn)
		{
			while (playerController.isPlayerDead)
			{
				yield return null;
			}
			((MonoBehaviour)playerController).StartCoroutine(FadeOutUIAndRestoreVolume(1f));
		}
		else
		{
			((MonoBehaviour)playerController).StartCoroutine(FadeOutScreenAndRestoreVolume(fadeDuration));
		}
	}

	private static IEnumerator FadeOutScreenAndRestoreVolume(float duration)
	{
		float elapsedTime = 0f;
		while (elapsedTime < duration)
		{
			elapsedTime += Time.deltaTime;
			float t = Mathf.Clamp01(elapsedTime / duration);
			blackCanvasGroup.alpha = Mathf.Lerp(1f, 0f, t);
			AudioListener.volume = Mathf.Lerp(0f, originalVolume, t);
			yield return null;
		}
		ResetBlackout();
	}

	private static IEnumerator FadeOutUIAndRestoreVolume(float fadeTime)
	{
		float elapsedTime = 0f;
		while (elapsedTime < fadeTime)
		{
			elapsedTime += Time.deltaTime;
			float t = Mathf.Clamp01(elapsedTime / fadeTime);
			blackCanvasGroup.alpha = Mathf.Lerp(1f, 0f, t);
			AudioListener.volume = Mathf.Lerp(0f, originalVolume, t);
			yield return null;
		}
		blackCanvasGroup.alpha = 0f;
		AudioListener.volume = originalVolume;
		ResetBlackout();
	}

	private static void ResetBlackout()
	{
		blackCanvasGroup.alpha = 0f;
		((TMP_Text)deathTextComponent).text = "";
	}
}