Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions SpeechMod/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using Kingmaker.PubSubSystem;
using SpeechMod.Configuration;
using SpeechMod.Keybinds;
using SpeechMod.Unity;
Expand Down Expand Up @@ -37,6 +38,7 @@ public static class Main
}).ToDictionary(p => p.Key, p => p.Value);

public static ISpeech Speech;
private static DialogCueListener _cueListener;
private static bool m_Loaded = false;

private static bool Load(UnityModManager.ModEntry modEntry)
Expand Down Expand Up @@ -69,6 +71,9 @@ private static bool Load(UnityModManager.ModEntry modEntry)

PhoneticDictionary.LoadDictionary();

_cueListener = new DialogCueListener();
EventBus.Subscribe(_cueListener);

ButtonFactory.Initialize();

Debug.Log("Pathfinder Kingmaker Speech Mod Initialized!");
Expand Down
38 changes: 0 additions & 38 deletions SpeechMod/Patches/UiDialogController_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using HarmonyLib;
using Kingmaker;
using Kingmaker.Controllers.Dialog;
using Kingmaker.Localization;
using SpeechMod.Unity;
using SpeechMod.Unity.Extensions;
using UnityEngine;
Expand Down Expand Up @@ -68,40 +66,4 @@ private static void AddDialogButtonByPath(string path)
buttonGameObject.SetActive(true);
}

[HarmonyPatch(typeof(DialogController), nameof(DialogController.HandleOnCueShow), typeof(CueShowData))]
[HarmonyPostfix]
public static void HandleOnCueShow_Postfix()
{
if (!Main.Enabled)
return;

#if DEBUG
Debug.Log($"{nameof(DialogController)}_{nameof(HandleOnCueShow_Postfix)}");
#endif

if (!Main.Settings!.AutoPlay)
{
#if DEBUG
Debug.Log($"{nameof(DialogController)}: AutoPlay is disabled!");
#endif
return;
}

string key = Game.Instance?.DialogController?.CurrentCue?.Text?.Key;
if (string.IsNullOrWhiteSpace(key))
key = Game.Instance?.DialogController?.CurrentCue?.Text?.Shared?.String?.Key;

if (string.IsNullOrWhiteSpace(key))
return;

// Stop playing and don't play if the dialog is voice acted.
if (!Main.Settings.AutoPlayIgnoreVoice &&
!string.IsNullOrWhiteSpace(LocalizationManager.SoundPack?.GetText(key, false)))
{
Main.Speech?.Stop();
return;
}

Main.Speech?.SpeakDialog(Game.Instance?.DialogController?.CurrentCue?.DisplayText, 0.5f);
}
}
34 changes: 34 additions & 0 deletions SpeechMod/Voice/DialogCueListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Kingmaker;
using Kingmaker.Controllers.Dialog;
using Kingmaker.Localization;
using Kingmaker.PubSubSystem;

namespace SpeechMod.Voice;

// Subscribes to the game's event bus so autoplay fires for all input modes
// (keyboard/mouse AND controller). The Harmony patch on UI.Dialog.DialogController
// only fires for keyboard, which is why controller autoplay was broken.
public class DialogCueListener : IDialogCueHandler
{
public void HandleOnCueShow(CueShowData data)
{
if (!Main.Enabled || !Main.Settings!.AutoPlay)
return;

string key = Game.Instance?.DialogController?.CurrentCue?.Text?.Key;
if (string.IsNullOrWhiteSpace(key))
key = Game.Instance?.DialogController?.CurrentCue?.Text?.Shared?.String?.Key;

if (string.IsNullOrWhiteSpace(key))
return;

if (!Main.Settings.AutoPlayIgnoreVoice &&
!string.IsNullOrWhiteSpace(LocalizationManager.SoundPack?.GetText(key, false)))
{
Main.Speech?.Stop();
return;
}

Main.Speech?.SpeakDialog(Game.Instance?.DialogController?.CurrentCue?.DisplayText, 0.5f);
}
}