Decompiled source of DeadTTS v0.1.0

REPO-DeadTTS.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 REPO_DeadTTS.Config;
using TMPro;
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("REPO-DeadTTS")]
[assembly: AssemblyDescription("Mod created by flipf17")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("REPO-DeadTTS")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8e05cd18-c8aa-419a-b430-33faaf371490")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace REPO_DeadTTS
{
	[BepInPlugin("flipf17.DeadTTS", "DeadTTS", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		public static Plugin instance;

		private static ManualLogSource logger;

		private void Awake()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			instance = this;
			CreateCustomLogger();
			ConfigSettings.BindConfigSettings();
			_harmony = new Harmony("DeadTTS");
			PatchAll();
			Log("DeadTTS loaded");
		}

		private void PatchAll()
		{
			IEnumerable<Type> enumerable;
			try
			{
				enumerable = Assembly.GetExecutingAssembly().GetTypes();
			}
			catch (ReflectionTypeLoadException ex)
			{
				enumerable = ex.Types.Where((Type t) => t != null);
			}
			foreach (Type item in enumerable)
			{
				_harmony.PatchAll(item);
			}
		}

		private void CreateCustomLogger()
		{
			try
			{
				logger = Logger.CreateLogSource(string.Format("{0}-{1}", "DeadTTS", "0.1.0"));
			}
			catch
			{
				logger = ((BaseUnityPlugin)this).Logger;
			}
		}

		public static void Log(string message)
		{
			logger.LogInfo((object)message);
		}

		public static void LogError(string message)
		{
			logger.LogError((object)message);
		}

		public static void LogWarning(string message)
		{
			logger.LogWarning((object)message);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "flipf17.DeadTTS";

		public const string PLUGIN_NAME = "DeadTTS";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}
namespace REPO_DeadTTS.Patches
{
	[HarmonyPatch]
	public static class UIPatcher
	{
		private static HashSet<WorldSpaceUITTS> deadTTSElements = new HashSet<WorldSpaceUITTS>();

		private static FieldInfo textField = typeof(WorldSpaceUITTS).GetField("text", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo playerAvatarField = typeof(WorldSpaceUITTS).GetField("playerAvatar", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo followTransformField = typeof(WorldSpaceUITTS).GetField("followTransform", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo worldPositionField = typeof(WorldSpaceUITTS).GetField("worldPosition", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo followPositionField = typeof(WorldSpaceUITTS).GetField("followPosition", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo wordTimeField = typeof(WorldSpaceUITTS).GetField("wordTime", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo voiceChatField = typeof(PlayerAvatar).GetField("voiceChat", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo ttsVoiceField = typeof(WorldSpaceUITTS).GetField("ttsVoice", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo isSpeakingField = typeof(TTSVoice).GetField("isSpeaking", BindingFlags.Instance | BindingFlags.NonPublic);

		[HarmonyPatch(typeof(WorldSpaceUIParent), "TTS")]
		[HarmonyPrefix]
		private static void OnTTSUI(PlayerAvatar _player, string _text, float _time, WorldSpaceUIParent __instance)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Expected O, but got Unknown
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Expected O, but got Unknown
			if (!ConfigSettings.deadTTSSpatialAudio.Value || (int)GameDirector.instance.currentState != 2 || !Object.op_Implicit((Object)(object)_player) || ((Behaviour)_player).isActiveAndEnabled || !Object.op_Implicit((Object)(object)_player.playerDeathHead) || string.IsNullOrEmpty(_text))
			{
				return;
			}
			try
			{
				WorldSpaceUITTS component = Object.Instantiate<GameObject>(__instance.TTSPrefab, ((Component)__instance).transform.position, ((Component)__instance).transform.rotation, ((Component)__instance).transform).GetComponent<WorldSpaceUITTS>();
				if (Object.op_Implicit((Object)(object)component))
				{
					TMP_Text val = (TMP_Text)textField.GetValue(component);
					val.text = _text;
					playerAvatarField.SetValue(component, _player);
					Transform transform = ((Component)_player.playerDeathHead).transform;
					followTransformField.SetValue(component, transform);
					worldPositionField.SetValue(component, transform.position);
					followPositionField.SetValue(component, transform.position);
					wordTimeField.SetValue(component, _time);
					PlayerVoiceChat val2 = (PlayerVoiceChat)voiceChatField.GetValue(_player);
					ttsVoiceField.SetValue(component, val2.ttsVoice);
					deadTTSElements.Add(component);
				}
			}
			catch (Exception ex)
			{
				Plugin.LogError("Error initializing dead TTS UI:\n" + ex);
			}
		}

		[HarmonyPatch(typeof(WorldSpaceUITTS), "Update")]
		[HarmonyPrefix]
		private static void OnUpdatePrefix(WorldSpaceUITTS __instance)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			if (!ConfigSettings.deadTTSSpatialAudio.Value || !deadTTSElements.Contains(__instance))
			{
				return;
			}
			try
			{
				TTSVoice obj = (TTSVoice)ttsVoiceField.GetValue(__instance);
				if ((bool)isSpeakingField.GetValue(obj))
				{
					FieldInfo field = typeof(WorldSpaceUITTS).GetField("textAlpha", BindingFlags.Instance | BindingFlags.NonPublic);
					field.SetValue(__instance, 10f);
				}
			}
			catch (Exception ex)
			{
				Plugin.LogError("Error updating dead TTS UI location:\n" + ex);
				deadTTSElements.Remove(__instance);
			}
		}
	}
	[HarmonyPatch]
	public static class PlayerPatcher
	{
		internal static PlayerAvatar localPlayer;

		[HarmonyPatch(typeof(PlayerAvatar), "Awake")]
		[HarmonyPostfix]
		private static void InitLocalPlayer(ref bool ___isLocal, PlayerAvatar __instance)
		{
			if (___isLocal)
			{
				localPlayer = __instance;
			}
		}

		[HarmonyPatch(typeof(PlayerVoiceChat), "TtsFollowVoiceSettings")]
		[HarmonyPostfix]
		private static void OnTtsFollowVoiceSettings(ref PlayerAvatar ___playerAvatar, ref AudioLowPassLogic ___lowPassLogicTTS, ref bool ___inLobbyMixerTTS, PlayerVoiceChat __instance)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Invalid comparison between Unknown and I4
			if (Object.op_Implicit((Object)(object)___playerAvatar) && Object.op_Implicit((Object)(object)___playerAvatar.playerDeathHead) && Object.op_Implicit((Object)(object)__instance.ttsAudioSource) && Object.op_Implicit((Object)(object)__instance.ttsVoice) && Object.op_Implicit((Object)(object)__instance.mixerTTSSound) && GameManager.Multiplayer() && (int)GameDirector.instance.currentState >= 2 && ((!((Behaviour)___playerAvatar).isActiveAndEnabled && ((Behaviour)___playerAvatar.playerDeathHead).isActiveAndEnabled) & ___inLobbyMixerTTS))
			{
				if ((Object)(object)__instance.ttsAudioSource.outputAudioMixerGroup != (Object)(object)__instance.mixerTTSSound)
				{
					Plugin.Log("The game has toggled ON lobby chat for player: " + ((Object)___playerAvatar).name + ". Disabling TTS lobby mixer.");
					__instance.ttsVoice.setVoice(1);
					__instance.ttsAudioSource.outputAudioMixerGroup = __instance.mixerTTSSound;
					__instance.ttsVoice.StopAndClearVoice();
				}
				__instance.ttsAudioSource.pitch = ConfigSettings.deadTTSPitch.Value;
				__instance.ttsAudioSource.volume = ConfigSettings.deadTTSVolume.Value;
				if (ConfigSettings.deadTTSSpatialAudio.Value)
				{
					__instance.ttsAudioSource.spatialBlend = 1f;
				}
			}
		}

		[HarmonyPatch(typeof(PlayerVoiceChat), "ToggleLobby")]
		[HarmonyPostfix]
		private static void OnToggleOffLobbyChat(bool _toggle, ref PlayerAvatar ___playerAvatar, PlayerVoiceChat __instance)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Invalid comparison between Unknown and I4
			if (Object.op_Implicit((Object)(object)__instance.ttsAudioSource) && Object.op_Implicit((Object)(object)__instance.mixerTTSSound) && GameManager.Multiplayer() && (int)GameDirector.instance.currentState >= 2 && !_toggle)
			{
				Plugin.Log("The game has toggled OFF lobby chat for player: " + ((Object)___playerAvatar).name);
				__instance.ttsAudioSource.volume = 1f;
			}
		}

		[HarmonyPatch(typeof(PlayerVoiceChat), "LateUpdate")]
		[HarmonyPrefix]
		private static void MoveTTSAudioTransform(ref PlayerAvatar ___playerAvatar, ref bool ___inLobbyMixerTTS, PlayerVoiceChat __instance)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			if (LevelGenerator.Instance.Generated && GameManager.Multiplayer() && (int)GameDirector.instance.currentState >= 2 && Object.op_Implicit((Object)(object)___playerAvatar) && ___inLobbyMixerTTS && !((Behaviour)___playerAvatar).isActiveAndEnabled && Object.op_Implicit((Object)(object)___playerAvatar.playerDeathHead) && Object.op_Implicit((Object)(object)__instance.ttsVoice))
			{
				((Component)__instance).transform.position = Vector3.Lerp(((Component)__instance).transform.position, ((Component)___playerAvatar.playerDeathHead).transform.position, 30f * Time.deltaTime);
			}
		}
	}
}
namespace REPO_DeadTTS.Config
{
	[Serializable]
	public static class ConfigSettings
	{
		public static ConfigEntry<float> deadTTSPitch;

		public static ConfigEntry<float> deadTTSVolume;

		public static ConfigEntry<bool> displayDeadTTSText;

		public static ConfigEntry<bool> deadTTSSpatialAudio;

		public static Dictionary<string, ConfigEntryBase> currentConfigEntries = new Dictionary<string, ConfigEntryBase>();

		public static void BindConfigSettings()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			Plugin.Log("Binding Configs");
			deadTTSPitch = AddConfigEntry<float>(((BaseUnityPlugin)Plugin.instance).Config.Bind<float>("General", "Dead TTS Pitch", 1.1f, new ConfigDescription("Affects the TTS pitch of all dead players.\nValues will be clamped between 0.1 and 2.0", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 2f), Array.Empty<object>())));
			deadTTSVolume = AddConfigEntry<float>(((BaseUnityPlugin)Plugin.instance).Config.Bind<float>("General", "Dead TTS Volume", 0.8f, new ConfigDescription("Affects the TTS volume of all dead players.\nValues will be clamped between 0.1 and 1.0", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1f), Array.Empty<object>())));
			displayDeadTTSText = AddConfigEntry<bool>(((BaseUnityPlugin)Plugin.instance).Config.Bind<bool>("General", "Display Dead TTS Text", true, "If true, TTS Text will appear from dead players' heads."));
			deadTTSSpatialAudio = AddConfigEntry<bool>(((BaseUnityPlugin)Plugin.instance).Config.Bind<bool>("General", "Use Spatial Audio", true, "If true, TTS audio from dead players should be 3D directional.\nIf false, the audio should appear as if it's in your head all the time."));
			deadTTSPitch.Value = Mathf.Clamp(deadTTSPitch.Value, 0.1f, 2f);
			deadTTSVolume.Value = Mathf.Clamp(deadTTSVolume.Value, 0.1f, 2f);
		}

		public static ConfigEntry<T> AddConfigEntry<T>(ConfigEntry<T> configEntry)
		{
			currentConfigEntries.Add(((ConfigEntryBase)configEntry).Definition.Key, (ConfigEntryBase)(object)configEntry);
			return configEntry;
		}
	}
}