Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of solochatvoicekeep v0.1.1
SoloChatVoiceKeep.dll
Decompiled 2 weeks agousing System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using BepInEx; using BepInEx.Logging; using HarmonyLib; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] namespace SoloChatVoiceKeep; [BepInProcess("REPO.exe")] [BepInPlugin("com.solochatvoicekeep.repo", "SoloChatVoiceKeep", "0.1.0")] public class SoloChatVoiceKeepPlugin : BaseUnityPlugin { internal static ManualLogSource ModLog; internal static bool Enabled = true; internal static bool ForceMultiplayer = false; private void Awake() { //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Expected O, but got Unknown ModLog = ((BaseUnityPlugin)this).Logger; Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "是否启用本模组(单人模式下保持打字聊天与语音聊天系统打开)").Value; Harmony val = new Harmony("com.solochatvoicekeep.repo"); val.PatchAll(); if (!ChatManagerUpdatePatch.InstalledSuccessfully) { val.UnpatchSelf(); ModLog.LogError((object)("[Fatal] ChatManager.Update Transpiler 未按预期挂载,已整体卸载本模组:" + ChatManagerUpdatePatch.FailureReason)); return; } ((Component)this).gameObject.AddComponent<SoloTtsController>(); int num = 0; foreach (MethodBase patchedMethod in val.GetPatchedMethods()) { num++; ModLog.LogInfo((object)("[Diag] Patched: " + patchedMethod.DeclaringType.Name + "." + patchedMethod.Name)); } ModLog.LogInfo((object)("SoloChatVoiceKeep 0.1.0 loaded (Enabled=" + Enabled + ") patches=" + num)); } } [HarmonyPatch(typeof(ChatManager), "Update")] [HarmonyPriority(0)] public class ChatManagerUpdatePatch { private const int ExpectedGuardCount = 2; internal static bool InstalledSuccessfully = false; internal static string FailureReason = "ChatManager.Update Transpiler 未执行"; private static readonly MethodInfo VanillaMultiplayerCheck = AccessTools.Method(typeof(SemiFunc), "IsMultiplayer", Type.EmptyTypes, (Type[])null); private static readonly MethodInfo SoloChatEnabledCheck = AccessTools.Method(typeof(ChatManagerUpdatePatch), "ChatEnabledForThisUpdate", Type.EmptyTypes, (Type[])null); private static float lastLog = -1f; private static void Prefix(ChatManager __instance) { if (!SoloChatVoiceKeepPlugin.Enabled) { return; } float time = Time.time; if (!(time - lastLog > 2f)) { return; } lastLog = time; try { int num = (int)AccessTools.Field(typeof(ChatManager), "chatState").GetValue(__instance); SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[Diag:Update] state=" + num)); } catch (Exception ex) { SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Diag:Update] " + ex.Message)); } } private static bool ChatEnabledForThisUpdate() { return SoloChatVoiceKeepPlugin.Enabled; } private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) { //IL_0108: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Expected O, but got Unknown List<CodeInstruction> list = new List<CodeInstruction>(instructions); List<int> list2 = new List<int>(); if (VanillaMultiplayerCheck == null || SoloChatEnabledCheck == null) { FailureReason = "无法解析 SemiFunc.IsMultiplayer 或替换方法"; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Transpiler] " + FailureReason)); return list; } for (int i = 0; i < list.Count; i++) { if (CodeInstructionExtensions.Calls(list[i], VanillaMultiplayerCheck)) { list2.Add(i); } } if (list2.Count != 2) { FailureReason = "期望在 ChatManager.Update 中找到 " + 2 + " 处 SemiFunc.IsMultiplayer 调用,实际找到 " + list2.Count + " 处(游戏版本可能不兼容)"; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Transpiler] " + FailureReason)); return list; } foreach (int item in list2) { CodeInstruction val = list[item]; CodeInstruction val2 = new CodeInstruction(OpCodes.Call, (object)SoloChatEnabledCheck); val2.labels = val.labels; val2.blocks = val.blocks; list[item] = val2; } InstalledSuccessfully = true; FailureReason = null; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[Transpiler] ChatManager.Update 已替换 " + list2.Count + " 处 IsMultiplayer 调用")); return list; } } [HarmonyPatch(typeof(PlayerVoiceChat), "Update")] public class PlayerVoiceChatUpdatePatch { private static void Prefix() { if (SoloChatVoiceKeepPlugin.Enabled) { SoloChatVoiceKeepPlugin.ForceMultiplayer = true; } } private static void Postfix() { SoloChatVoiceKeepPlugin.ForceMultiplayer = false; } } [HarmonyPatch(typeof(PlayerVoiceChat), "TtsFollowVoiceSettings")] public class TtsFollowVoiceSettingsPatch { private static void Prefix() { if (SoloChatVoiceKeepPlugin.Enabled) { SoloChatVoiceKeepPlugin.ForceMultiplayer = true; } } private static void Postfix() { SoloChatVoiceKeepPlugin.ForceMultiplayer = false; } } [HarmonyPatch(typeof(PlayerVoiceChat), "TtsRoutingSet")] public class TtsRoutingSetPatch { private static void Prefix() { if (SoloChatVoiceKeepPlugin.Enabled) { SoloChatVoiceKeepPlugin.ForceMultiplayer = true; } } private static void Postfix() { SoloChatVoiceKeepPlugin.ForceMultiplayer = false; } } [HarmonyPatch(typeof(SemiFunc), "IsMultiplayer")] public class IsMultiplayerPatch { private static bool Prefix(ref bool __result) { if (SoloChatVoiceKeepPlugin.Enabled && SoloChatVoiceKeepPlugin.ForceMultiplayer) { __result = true; return false; } return true; } } [HarmonyPatch(typeof(ChatManager), "ImportantFetches")] public class ChatManagerImportantFetchesPatch { private static readonly FieldInfo PlayerAvatarField = AccessTools.Field(typeof(ChatManager), "playerAvatar"); private static readonly FieldInfo LocalAvatarFetchedField = AccessTools.Field(typeof(ChatManager), "localPlayerAvatarFetched"); private static readonly FieldInfo TextMeshFetchedField = AccessTools.Field(typeof(ChatManager), "textMeshFetched"); private static int frame = 0; private static void Prefix(ChatManager __instance) { if (!SoloChatVoiceKeepPlugin.Enabled || SemiFunc.IsMultiplayer() || SoloChatVoiceKeepPlugin.ForceMultiplayer) { return; } PlayerAvatar instance = PlayerAvatar.instance; if ((Object)(object)instance != (Object)null) { PlayerAvatarField.SetValue(__instance, instance); LocalAvatarFetchedField.SetValue(__instance, true); TextMeshFetchedField.SetValue(__instance, true); if (frame++ % 120 == 0) { SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[Diag:Important] injected pa+tmf=true"); } } else if (frame++ % 120 == 0) { SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[Diag:Important] PlayerAvatar.instance IS NULL"); } } } [HarmonyPatch(typeof(ChatManager), "StateInactive")] public class ChatManagerStateInactivePatch { private static FieldInfo hasAnyFocusField = null; private static float lastLog = -1f; private static void Prefix() { if (!SoloChatVoiceKeepPlugin.Enabled || SemiFunc.IsMultiplayer() || SoloChatVoiceKeepPlugin.ForceMultiplayer) { return; } try { bool flag = false; if (hasAnyFocusField == null) { Type type = AccessTools.TypeByName("MenuLib.MonoBehaviors.REPOInputStringSystem, MenuLib"); if (type != null) { hasAnyFocusField = AccessTools.Field(type, "hasAnyFocus"); } } if (hasAnyFocusField != null) { flag = (bool)hasAnyFocusField.GetValue(null); if (flag) { hasAnyFocusField.SetValue(null, false); SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[Diag:StateInactive] hasAnyFocus was true, bypassed"); } } float time = Time.time; if (!(time - lastLog > 1f)) { return; } lastLog = time; bool flag2 = SemiFunc.NoTextInputsActive(); bool flag3 = SemiFunc.InputDown((InputKey)7); string text = "null"; try { if ((Object)(object)MenuManager.instance != (Object)null) { FieldInfo fieldInfo = AccessTools.Field(typeof(MenuManager), "currentMenuPage"); object value = fieldInfo.GetValue(MenuManager.instance); if (value != null) { FieldInfo fieldInfo2 = AccessTools.Field(value.GetType(), "menuPageIndex"); text = fieldInfo2.GetValue(value).ToString(); } } } catch (Exception) { } SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[Diag:StateInactive] hasAnyFocus=" + flag + " noText=" + flag2 + " inputDown7=" + flag3 + " menuPage=" + text)); } catch (Exception ex2) { SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Diag:StateInactive] " + ex2.Message)); } } } [HarmonyPatch(typeof(PlayerVoiceChat), "MicrophoneOwnerIsLocal")] public class MicrophoneOwnerIsLocalPatch { private static bool Prefix(ref bool __result) { if (SoloChatVoiceKeepPlugin.Enabled && !SemiFunc.IsMultiplayer()) { __result = true; return false; } return true; } } [HarmonyPatch(typeof(PlayerAvatarTalkAnimation), "Update")] public class PlayerAvatarTalkAnimationUpdatePatch { internal static bool InstalledSuccessfully = false; internal static string FailureReason = "PlayerAvatarTalkAnimation.Update Transpiler 未执行"; private static readonly MethodInfo VanillaMultiplayerCheck = AccessTools.Method(typeof(GameManager), "Multiplayer", Type.EmptyTypes, (Type[])null); private static readonly MethodInfo TalkEnabledCheck = AccessTools.Method(typeof(PlayerAvatarTalkAnimationUpdatePatch), "TalkEnabledForThisUpdate", Type.EmptyTypes, (Type[])null); private static bool TalkEnabledForThisUpdate() { return SoloChatVoiceKeepPlugin.Enabled; } private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) { //IL_00df: Unknown result type (might be due to invalid IL or missing references) //IL_00e6: Expected O, but got Unknown List<CodeInstruction> list = new List<CodeInstruction>(instructions); List<int> list2 = new List<int>(); if (VanillaMultiplayerCheck == null || TalkEnabledCheck == null) { FailureReason = "无法解析 GameManager.Multiplayer 或替换方法"; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Transpiler] " + FailureReason)); return list; } for (int i = 0; i < list.Count; i++) { if (CodeInstructionExtensions.Calls(list[i], VanillaMultiplayerCheck)) { list2.Add(i); } } if (list2.Count != 1) { FailureReason = "期望在 PlayerAvatarTalkAnimation.Update 中找到 1 处 GameManager.Multiplayer 调用,实际找到 " + list2.Count + " 处(游戏版本可能不兼容)"; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[Transpiler] " + FailureReason)); return list; } foreach (int item in list2) { CodeInstruction val = list[item]; CodeInstruction val2 = new CodeInstruction(OpCodes.Call, (object)TalkEnabledCheck); val2.labels = val.labels; val2.blocks = val.blocks; list[item] = val2; } InstalledSuccessfully = true; FailureReason = null; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[Transpiler] PlayerAvatarTalkAnimation.Update 已放行多人分支"); return list; } } [HarmonyPatch(typeof(PlayerAvatar), "Update")] public class PlayerAvatarVoiceChatBindPatch { private static readonly FieldInfo VoiceChatField = AccessTools.Field(typeof(PlayerAvatar), "voiceChat"); private static readonly FieldInfo VoiceChatFetchedField = AccessTools.Field(typeof(PlayerAvatar), "voiceChatFetched"); private static PlayerVoiceChat cachedVoiceChat = null; private static int logCounter = 0; private static void Prefix(PlayerAvatar __instance) { if (!SoloChatVoiceKeepPlugin.Enabled || SemiFunc.IsMultiplayer() || SoloChatVoiceKeepPlugin.ForceMultiplayer || VoiceChatField == null || VoiceChatField.GetValue(__instance) != null) { return; } if ((Object)(object)cachedVoiceChat == (Object)null) { PlayerVoiceChat val = PlayerVoiceChat.instance; if ((Object)(object)val == (Object)null) { PlayerVoiceChat[] array = Resources.FindObjectsOfTypeAll<PlayerVoiceChat>(); for (int i = 0; i < array.Length; i++) { if ((Object)(object)array[i] != (Object)null) { val = array[i]; break; } } } cachedVoiceChat = val; } if (!((Object)(object)cachedVoiceChat == (Object)null)) { VoiceChatField.SetValue(__instance, cachedVoiceChat); if (VoiceChatFetchedField != null) { VoiceChatFetchedField.SetValue(__instance, true); } if (logCounter++ % 120 == 0) { SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[MIC] bound PlayerAvatar.voiceChat -> PlayerVoiceChat.instance"); } } } } [HarmonyPatch(typeof(PlayerAvatarTalkAnimation), "Update")] public class PlayerAvatarTalkAnimationPreparePatch { private static readonly FieldInfo PlayerAvatarField = AccessTools.Field(typeof(PlayerAvatarTalkAnimation), "playerAvatar"); private static readonly FieldInfo AudioSourceField = AccessTools.Field(typeof(PlayerAvatarTalkAnimation), "audioSource"); private static readonly FieldInfo AudioSourceFetchedField = AccessTools.Field(typeof(PlayerAvatarTalkAnimation), "audioSourceFetched"); private static readonly FieldInfo VoiceChatField = AccessTools.Field(typeof(PlayerAvatar), "voiceChat"); private static readonly FieldInfo VoiceChatFetchedField = AccessTools.Field(typeof(PlayerAvatar), "voiceChatFetched"); private static readonly FieldInfo VcAudioSourceField = AccessTools.Field(typeof(PlayerVoiceChat), "audioSource"); private static readonly FieldInfo VcClipLoudnessField = AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudness"); private static readonly FieldInfo VcClipLoudnessTTSField = AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudnessTTS"); private static readonly FieldInfo ObjectToRotateField = AccessTools.Field(typeof(PlayerAvatarTalkAnimation), "objectToRotate"); private static int logCounter = 0; private static void Prefix(PlayerAvatarTalkAnimation __instance) { //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Expected O, but got Unknown //IL_0061: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Expected O, but got Unknown if (!SoloChatVoiceKeepPlugin.Enabled || SemiFunc.IsMultiplayer() || SoloChatVoiceKeepPlugin.ForceMultiplayer) { return; } PlayerAvatar val = null; try { val = (PlayerAvatar)PlayerAvatarField.GetValue(__instance); } catch (Exception) { return; } if ((Object)(object)val == (Object)null) { val = PlayerAvatar.instance; if ((Object)(object)val == (Object)null) { return; } PlayerAvatarField.SetValue(__instance, val); } PlayerVoiceChat val2 = null; try { val2 = (PlayerVoiceChat)VoiceChatField.GetValue(val); } catch (Exception) { return; } if ((Object)(object)val2 == (Object)null) { val2 = PlayerVoiceChat.instance; if ((Object)(object)val2 == (Object)null) { PlayerVoiceChat[] array = Resources.FindObjectsOfTypeAll<PlayerVoiceChat>(); for (int i = 0; i < array.Length; i++) { if ((Object)(object)array[i] != (Object)null) { val2 = array[i]; break; } } } if ((Object)(object)val2 == (Object)null) { return; } VoiceChatField.SetValue(val, val2); if (VoiceChatFetchedField != null) { VoiceChatFetchedField.SetValue(val, true); } } if (AudioSourceFetchedField != null) { try { if (!(bool)AudioSourceFetchedField.GetValue(__instance)) { if (VcAudioSourceField.GetValue(val2) == null) { AudioSource val3 = ((Component)val2).gameObject.GetComponent<AudioSource>(); if ((Object)(object)val3 == (Object)null) { val3 = ((Component)val2).gameObject.AddComponent<AudioSource>(); } VcAudioSourceField.SetValue(val2, val3); SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[TALK] created AudioSource for solo voiceChat"); } AudioSourceField.SetValue(__instance, VcAudioSourceField.GetValue(val2)); AudioSourceFetchedField.SetValue(__instance, true); } } catch (Exception) { } } try { float num = SoloTtsController.SampleTtsLoudness(); if (num > 0f) { VcClipLoudnessField.SetValue(val2, num); VcClipLoudnessTTSField.SetValue(val2, num); } } catch (Exception) { } try { PlayerVoiceChatMicrophoneFeedbackPatch.SoloUpdate(val2); } catch (Exception) { } if (logCounter++ % 60 != 0) { return; } try { int num2 = -1; if ((Object)(object)GameManager.instance != (Object)null) { num2 = GameManager.instance.gameMode; } bool flag = false; try { flag = (bool)VoiceChatFetchedField.GetValue(val); } catch (Exception) { } bool flag2 = true; try { flag2 = VcAudioSourceField.GetValue(val2) == null; } catch (Exception) { } bool flag3 = true; try { flag3 = AudioSourceField.GetValue(__instance) == null; } catch (Exception) { } bool flag4 = true; try { flag4 = ObjectToRotateField.GetValue(__instance) == null; } catch (Exception) { } SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[TALK] diag gm=" + num2 + " vcFetched=" + flag + " vcAudioNull=" + flag2 + " animAudioNull=" + flag3 + " objNull=" + flag4 + " clipLoudness=" + ((float)VcClipLoudnessField.GetValue(val2)).ToString("F4") + " ttsLoud=" + ((float)VcClipLoudnessTTSField.GetValue(val2)).ToString("F4"))); } catch (Exception) { } } } [HarmonyPatch(typeof(PlayerVoiceChat), "Update")] public class PlayerVoiceChatMicrophoneFeedbackPatch { private const float MicGain = 4f; private static AudioClip micClip = null; private static float[] sampleBuf = null; private static bool micActive = false; private static float lastLog = -1f; private static float lastDiagLog = -1f; private static PropertyInfo transmitEnabledProp = null; private static MethodInfo getDataMethod = null; private static void Postfix(AudioSource ___audioSource, object ___recorder, ref float ___clipLoudness, ref float ___clipLoudnessNoTTS, ref float ___clipLoudnessTTS) { if (!SoloChatVoiceKeepPlugin.Enabled) { return; } float time = Time.time; if (time - lastDiagLog > 2f) { lastDiagLog = time; try { int num = -1; if ((Object)(object)GameManager.instance != (Object)null) { num = GameManager.instance.gameMode; } SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[MIC] diag gm=" + num + " audioNull=" + ((Object)(object)___audioSource == (Object)null) + " recorderNull=" + (___recorder == null) + " isSpeaking=" + SoloTtsController.IsSpeaking)); } catch (Exception ex) { SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[MIC] diag error: " + ex.Message)); } } if ((Object)(object)GameManager.instance != (Object)null && GameManager.instance.gameMode == 1) { StopMic(___audioSource); } else { if (SoloTtsController.IsSpeaking) { return; } bool flag = false; if (___recorder != null) { if (transmitEnabledProp == null) { transmitEnabledProp = AccessTools.Property(___recorder.GetType(), "TransmitEnabled"); } if (transmitEnabledProp != null) { flag = (bool)transmitEnabledProp.GetValue(___recorder, null); } } bool flag2 = false; try { flag2 = Input.GetKey((KeyCode)118); } catch (Exception ex2) { flag2 = false; if (Time.time - lastLog > 5f) { lastLog = Time.time; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[MIC] Input.GetKey failed: " + ex2.Message)); } } if ((flag || flag2) && (Object)(object)___audioSource != (Object)null) { EnsureMic(___audioSource); if ((Object)(object)micClip != (Object)null && sampleBuf != null) { if (getDataMethod == null) { getDataMethod = typeof(AudioClip).GetMethod("GetData", new Type[2] { typeof(float[]), typeof(int) }); if (getDataMethod == null) { SoloChatVoiceKeepPlugin.ModLog.LogError((object)"[MIC] AudioClip.GetData(float[],int) not found"); } } if (getDataMethod != null) { getDataMethod.Invoke(micClip, new object[2] { sampleBuf, 0 }); float num2 = 0f; for (int i = 0; i < sampleBuf.Length; i++) { num2 += Mathf.Abs(sampleBuf[i]); } float num3 = (___clipLoudnessNoTTS = (___clipLoudness = Mathf.Clamp01(num2 / (float)sampleBuf.Length * 4f))); float time2 = Time.time; if (time2 - lastLog > 0.5f) { lastLog = time2; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[MIC] speaking loud=" + num3.ToString("F4") + " pos=" + Microphone.GetPosition((string)null) + " samples=" + sampleBuf.Length)); } return; } } } StopMic(___audioSource); ___clipLoudness = 0f; ___clipLoudnessNoTTS = 0f; ___clipLoudnessTTS = 0f; } } private static void EnsureMic(AudioSource audioSource) { if (micActive && (Object)(object)micClip != (Object)null) { return; } AudioClip val = Microphone.Start((string)null, true, 1, 44100); if ((Object)(object)val == (Object)null) { if (Time.time - lastLog > 2f) { lastLog = Time.time; SoloChatVoiceKeepPlugin.ModLog.LogError((object)"[MIC] Microphone.Start failed"); } return; } micClip = val; sampleBuf = new float[44100]; audioSource.clip = val; audioSource.loop = true; audioSource.spatialBlend = 0f; audioSource.volume = 1f; audioSource.Play(); micActive = true; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[MIC] microphone loop started"); } private static void StopMic(AudioSource audioSource) { if (micActive) { if ((Object)(object)audioSource != (Object)null) { audioSource.Stop(); audioSource.clip = null; } Microphone.End((string)null); micClip = null; sampleBuf = null; micActive = false; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[MIC] microphone loop stopped"); } } internal static void SoloUpdate(PlayerVoiceChat vc) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Expected O, but got Unknown if (!SoloChatVoiceKeepPlugin.Enabled || (Object)(object)vc == (Object)null) { return; } AudioSource val = null; try { val = (AudioSource)AccessTools.Field(typeof(PlayerVoiceChat), "audioSource").GetValue(vc); } catch (Exception) { } if ((Object)(object)val == (Object)null) { return; } if ((Object)(object)GameManager.instance != (Object)null && GameManager.instance.gameMode == 1) { StopMic(val); } else { if (SoloTtsController.IsSpeaking) { return; } object obj = null; try { obj = AccessTools.Field(typeof(PlayerVoiceChat), "recorder").GetValue(vc); } catch (Exception) { } bool flag = false; if (obj != null) { if (transmitEnabledProp == null) { transmitEnabledProp = AccessTools.Property(obj.GetType(), "TransmitEnabled"); } if (transmitEnabledProp != null) { flag = (bool)transmitEnabledProp.GetValue(obj, null); } } bool flag2 = false; try { flag2 = Input.GetKey((KeyCode)118); } catch (Exception) { } if ((flag || flag2) && (Object)(object)val != (Object)null) { EnsureMic(val); if ((Object)(object)micClip != (Object)null && sampleBuf != null) { if (getDataMethod == null) { getDataMethod = typeof(AudioClip).GetMethod("GetData", new Type[2] { typeof(float[]), typeof(int) }); } if (getDataMethod != null) { getDataMethod.Invoke(micClip, new object[2] { sampleBuf, 0 }); float num = 0f; for (int i = 0; i < sampleBuf.Length; i++) { num += Mathf.Abs(sampleBuf[i]); } float num2 = Mathf.Clamp01(num / (float)sampleBuf.Length * 4f); try { AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudness").SetValue(vc, num2); } catch (Exception) { } try { AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudnessNoTTS").SetValue(vc, num2); } catch (Exception) { } float time = Time.time; if (time - lastLog > 0.5f) { lastLog = time; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[MIC] solo speaking loud=" + num2.ToString("F4"))); } return; } } } StopMic(val); try { AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudness").SetValue(vc, 0f); } catch (Exception) { } try { AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudnessNoTTS").SetValue(vc, 0f); } catch (Exception) { } try { AccessTools.Field(typeof(PlayerVoiceChat), "clipLoudnessTTS").SetValue(vc, 0f); } catch (Exception) { } } } } [HarmonyPatch(typeof(PlayerAvatar), "ChatMessageSpeak")] public class PlayerAvatarChatMessageSpeakPatch { private static float lastLog = -1f; private static bool Prefix(PlayerAvatar __instance, string _message, bool crouching) { if (SemiFunc.IsMultiplayer() || SoloTtsController.HasExistingVanillaVoice(__instance)) { return true; } bool flag = SoloTtsController.TrySpeak(__instance, _message, crouching); float time = Time.time; if (time - lastLog > 1f) { lastLog = time; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[TTS] ChatMessageSpeak solo len=" + ((_message == null) ? "null" : _message.Length.ToString()) + " crouch=" + crouching + " trySpeak=" + flag + " ready=" + SoloTtsController.VoiceReady)); } return !flag; } } [HarmonyPatch(typeof(TTSVoice), "Update")] public class SoloTtsVoiceUpdatePatch { private static bool Prefix(TTSVoice __instance, ref bool ___isSpeaking, AudioSource ___audioSource) { if (!SoloTtsController.Owns(__instance)) { return true; } ___isSpeaking = Object.op_Implicit((Object)(object)___audioSource) && ___audioSource.isPlaying; return false; } } [HarmonyPatch(typeof(LobbyChatUI), "Update")] public class LobbyChatUiUpdatePatch { private static void Prefix(LobbyChatUI __instance, ref TTSVoice ___ttsVoice) { if (!SemiFunc.IsMultiplayer() && __instance.isGameplay && Object.op_Implicit((Object)(object)SoloTtsController.Voice)) { ___ttsVoice = SoloTtsController.Voice; } } } [HarmonyPatch(typeof(ChatManager), "PossessChatCustomLogic")] public class ChatManagerPossessVoicePatch { private static void Postfix(ref float ___isSpeakingTimer) { if (!SemiFunc.IsMultiplayer() && SoloTtsController.IsSpeaking && ___isSpeakingTimer < 0.2f) { ___isSpeakingTimer = 0.2f; } } } public sealed class SoloTtsController : MonoBehaviour { private static readonly FieldInfo TtsPlayerAvatarField = AccessTools.Field(typeof(TTSVoice), "playerAvatar"); private static readonly FieldInfo PlayerAvatarVoiceChatField = AccessTools.Field(typeof(PlayerAvatar), "voiceChat"); private static MethodInfo outputDataMethod = null; private static float[] loudnessBuf = new float[256]; private TTSVoice _voice; private AudioSource _audioSource; private PlayerAvatar _boundAvatar; private bool _creating; private bool _ready; private bool _resourceFailureLogged; private string _pendingMessage; private bool _pendingCrouching; internal static SoloTtsController Instance { get; private set; } internal static TTSVoice Voice { get { if (!((Object)(object)Instance != (Object)null)) { return null; } return Instance._voice; } } internal static bool IsSpeaking { get { if ((Object)(object)Instance != (Object)null && Instance._ready && (Object)(object)Instance._audioSource != (Object)null) { return Instance._audioSource.isPlaying; } return false; } } internal static bool VoiceReady { get { if ((Object)(object)Instance != (Object)null && Instance._ready) { return (Object)(object)Instance._voice != (Object)null; } return false; } } internal static float SampleTtsLoudness() { if ((Object)(object)Instance == (Object)null || (Object)(object)Instance._audioSource == (Object)null || !Instance._audioSource.isPlaying) { return 0f; } if (outputDataMethod == null) { outputDataMethod = typeof(AudioSource).GetMethod("GetOutputData", new Type[2] { typeof(float[]), typeof(int) }); if (outputDataMethod == null) { return 0f; } } try { outputDataMethod.Invoke(Instance._audioSource, new object[2] { loudnessBuf, 0 }); float num = 0f; for (int i = 0; i < loudnessBuf.Length; i++) { num += Mathf.Abs(loudnessBuf[i]); } return Mathf.Clamp01(num / (float)loudnessBuf.Length * 4f); } catch (Exception) { return 0f; } } private void Awake() { Instance = this; } private void Update() { if (SemiFunc.IsMultiplayer()) { DestroySoloVoice(); } else if (!((Object)(object)LevelGenerator.Instance == (Object)null) && LevelGenerator.Instance.Generated && Object.op_Implicit((Object)(object)PlayerAvatar.instance)) { if (Object.op_Implicit((Object)(object)_voice) && (Object)(object)_boundAvatar != (Object)(object)PlayerAvatar.instance) { BindAvatar(PlayerAvatar.instance); } if (!Object.op_Implicit((Object)(object)_voice) && !_creating && !_resourceFailureLogged) { ((MonoBehaviour)this).StartCoroutine(CreateNativeVoice(PlayerAvatar.instance)); } } } internal static bool HasExistingVanillaVoice(PlayerAvatar avatar) { if (!Object.op_Implicit((Object)(object)avatar) || PlayerAvatarVoiceChatField == null) { return false; } object? value = PlayerAvatarVoiceChatField.GetValue(avatar); PlayerVoiceChat val = (PlayerVoiceChat)((value is PlayerVoiceChat) ? value : null); if (Object.op_Implicit((Object)(object)val)) { return Object.op_Implicit((Object)(object)val.ttsVoice); } return false; } internal static bool TrySpeak(PlayerAvatar avatar, string message, bool crouching) { if ((Object)(object)Instance == (Object)null || !Object.op_Implicit((Object)(object)avatar) || string.IsNullOrEmpty(message)) { return false; } return Instance.QueueOrSpeak(avatar, message, crouching); } internal static bool Owns(TTSVoice voice) { if ((Object)(object)Instance != (Object)null) { return (Object)(object)Instance._voice == (Object)(object)voice; } return false; } internal void Shutdown() { ((MonoBehaviour)this).StopAllCoroutines(); DestroySoloVoice(); if ((Object)(object)Instance == (Object)(object)this) { Instance = null; } } private void SpeakNow(string message, bool crouching) { string text = ""; if ((Object)(object)LocalizationManager.instance != (Object)null) { text = LocalizationManager.instance.TTSLocaleGetCode(); } try { _voice.TTSSpeakNow(message, crouching, text); SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)("[TTS] SpeakNow ok len=" + message.Length + " locale=" + text + " voice=" + ((Object)(object)_voice != (Object)null) + " audio=" + ((Object)(object)_audioSource != (Object)null) + " playing=" + ((Object)(object)_audioSource != (Object)null && _audioSource.isPlaying))); } catch (Exception ex) { SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[TTS] SpeakNow threw: " + ex)); } } private bool QueueOrSpeak(PlayerAvatar avatar, string message, bool crouching) { if (SemiFunc.IsMultiplayer()) { return false; } if (_resourceFailureLogged) { return false; } if (Object.op_Implicit((Object)(object)_voice) && (Object)(object)_boundAvatar != (Object)(object)avatar) { BindAvatar(avatar); } if (_ready && Object.op_Implicit((Object)(object)_voice)) { SpeakNow(message, crouching); return true; } _pendingMessage = message; _pendingCrouching = crouching; if (!_creating) { ((MonoBehaviour)this).StartCoroutine(CreateNativeVoice(avatar)); } return true; } private IEnumerator CreateNativeVoice(PlayerAvatar avatar) { _creating = true; if (!TryCreateNativeVoice(avatar)) { _creating = false; yield break; } yield return null; _creating = false; if (Object.op_Implicit((Object)(object)_voice)) { _ready = true; SoloChatVoiceKeepPlugin.ModLog.LogInfo((object)"[TTS] Native singleplayer TTS voice is ready."); if (!string.IsNullOrEmpty(_pendingMessage)) { string pendingMessage = _pendingMessage; bool pendingCrouching = _pendingCrouching; _pendingMessage = null; SpeakNow(pendingMessage, pendingCrouching); } } } private bool TryCreateNativeVoice(PlayerAvatar avatar) { //IL_00ce: Unknown result type (might be due to invalid IL or missing references) //IL_00df: Unknown result type (might be due to invalid IL or missing references) if (TtsPlayerAvatarField == null) { LogResourceFailure("TTSVoice.playerAvatar could not be resolved."); return false; } PlayerVoiceChat val = null; GameObject val2 = Resources.Load<GameObject>("Voice"); if (Object.op_Implicit((Object)(object)val2)) { val = val2.GetComponent<PlayerVoiceChat>(); } if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)val.TTSprefab)) { PlayerVoiceChat[] array = Resources.FindObjectsOfTypeAll<PlayerVoiceChat>(); PlayerVoiceChat[] array2 = array; foreach (PlayerVoiceChat val3 in array2) { if (Object.op_Implicit((Object)(object)val3) && Object.op_Implicit((Object)(object)val3.TTSprefab)) { val = val3; break; } } } if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)val.TTSprefab)) { LogResourceFailure("The vanilla Voice prefab or its TTS prefab could not be found."); return false; } GameObject val4 = Object.Instantiate<GameObject>(val.TTSprefab, ((Component)this).transform); ((Object)val4).name = "Solo Chat Vanilla TTS"; val4.transform.localPosition = Vector3.zero; val4.transform.localRotation = Quaternion.identity; if (!val4.activeSelf) { val4.SetActive(true); } _voice = val4.GetComponent<TTSVoice>(); _audioSource = val4.GetComponent<AudioSource>(); if (!Object.op_Implicit((Object)(object)_voice) || !Object.op_Implicit((Object)(object)_audioSource)) { Object.Destroy((Object)(object)val4); _voice = null; _audioSource = null; LogResourceFailure("The vanilla TTS prefab is missing TTSVoice or AudioSource."); return false; } _audioSource.outputAudioMixerGroup = val.mixerTTSSound; _audioSource.spatialBlend = 0f; _audioSource.volume = 1f; BindAvatar(avatar); return true; } private void BindAvatar(PlayerAvatar avatar) { _boundAvatar = avatar; if (Object.op_Implicit((Object)(object)_voice) && TtsPlayerAvatarField != null) { TtsPlayerAvatarField.SetValue(_voice, avatar); } } private void DestroySoloVoice() { _pendingMessage = null; _creating = false; _ready = false; _boundAvatar = null; _audioSource = null; if (Object.op_Implicit((Object)(object)_voice)) { Object.Destroy((Object)(object)((Component)_voice).gameObject); _voice = null; } } private void LogResourceFailure(string reason) { if (!_resourceFailureLogged) { _resourceFailureLogged = true; SoloChatVoiceKeepPlugin.ModLog.LogError((object)("[TTS] Native singleplayer TTS could not initialize. " + reason)); } } }