153 lines
5.2 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Common;
2021-12-23 22:59:53 -08:00
using QSB.ConversationSync.Messages;
2021-12-20 11:48:50 +00:00
using QSB.ConversationSync.WorldObjects;
2021-12-23 22:59:53 -08:00
using QSB.Messaging;
2020-12-14 21:20:53 +00:00
using QSB.Patches;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-10-25 13:17:01 +00:00
using QSB.Utility;
using QSB.WorldSync;
2020-09-18 15:32:22 +01:00
2022-03-02 19:46:33 -08:00
namespace QSB.ConversationSync.Patches;
[HarmonyPatch]
public class ConversationPatches : QSBPatch
2020-09-18 15:32:22 +01:00
{
2022-03-02 19:46:33 -08:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(DialogueConditionManager), nameof(DialogueConditionManager.SetConditionState))]
2022-03-27 00:24:07 -07:00
public static void SetConditionState(string conditionName, bool conditionState) =>
2022-03-02 19:46:33 -08:00
new DialogueConditionMessage(conditionName, conditionState).Send();
2020-11-03 21:11:10 +00:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
public static void CharacterDialogueTree_StartConversation(CharacterDialogueTree __instance)
{
2022-05-13 22:38:06 +01:00
var worldObject = __instance.GetWorldObject<QSBCharacterDialogueTree>();
2022-05-13 22:38:06 +01:00
QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree = worldObject;
ConversationManager.Instance.SendConvState(worldObject, true);
2022-03-02 19:46:33 -08:00
}
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.EndConversation))]
public static bool CharacterDialogueTree_EndConversation(CharacterDialogueTree __instance)
{
if (!__instance.enabled)
{
return false;
2020-12-02 21:23:01 +00:00
}
2022-05-13 22:38:06 +01:00
if (QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree == null)
2020-12-02 21:23:01 +00:00
{
2022-05-13 22:38:06 +01:00
DebugLog.ToConsole($"Warning - Ending conversation with null CurrentCharacterDialogueTree! Called from {__instance.name}", MessageType.Warning);
2020-12-02 21:23:01 +00:00
return true;
}
2020-09-22 21:11:29 +01:00
ConversationManager.Instance.SendConvState(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree, false);
2022-05-13 22:38:06 +01:00
ConversationManager.Instance.CloseBoxCharacter(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree.ObjectId);
QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree = null;
2022-03-02 19:46:33 -08:00
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.InputDialogueOption))]
public static bool CharacterDialogueTree_InputDialogueOption(CharacterDialogueTree __instance, int optionIndex)
{
if (optionIndex < 0)
{
// in a page where there is no selectable options
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
var selectedOption = __instance._currentDialogueBox.OptionFromUIIndex(optionIndex);
2022-08-19 16:14:51 -07:00
// BUG: uses translated value instead of key
2022-03-02 19:46:33 -08:00
ConversationManager.Instance.SendPlayerOption(selectedOption.Text);
return true;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(DialogueNode), nameof(DialogueNode.GetNextPage))]
public static void DialogueNode_GetNextPage(DialogueNode __instance)
{
var key = __instance._name + __instance._listPagesToDisplay[__instance._currentPage];
// Sending key so translation can be done on client side - should make different language-d clients compatible
2022-05-13 22:38:06 +01:00
Delay.RunWhen(() => QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree != null,
() => ConversationManager.Instance.SendCharacterDialogue(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTree.ObjectId, key));
2022-03-02 19:46:33 -08:00
}
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(RemoteDialogueTrigger), nameof(RemoteDialogueTrigger.ConversationTriggered))]
public static bool ConversationTriggeredReplacement(RemoteDialogueTrigger __instance, out bool __result, out RemoteDialogueTrigger.RemoteDialogueCondition dialogue)
{
dialogue = default;
var dialogueIndex = -1;
for (var i = 0; i < __instance._listDialogues.Length; i++)
{
2022-03-02 19:46:33 -08:00
if (!__instance._activatedDialogues[i])
{
2022-03-02 19:46:33 -08:00
var allConditionsMet = true;
var anyConditionsMet = __instance._listDialogues[i].prereqConditions.Length == 0;
2022-03-02 19:46:33 -08:00
foreach (var prereqCondition in __instance._listDialogues[i].prereqConditions)
{
if (DialogueConditionManager.SharedInstance.GetConditionState(prereqCondition))
2021-12-20 11:48:50 +00:00
{
2022-03-02 19:46:33 -08:00
anyConditionsMet = true;
2021-12-20 11:48:50 +00:00
}
2022-03-02 19:46:33 -08:00
else
2021-12-20 11:48:50 +00:00
{
2022-03-02 19:46:33 -08:00
allConditionsMet = false;
2021-12-20 11:48:50 +00:00
}
2022-03-02 19:46:33 -08:00
}
var conditionsMet = false;
var prereqConditionType = __instance._listDialogues[i].prereqConditionType;
if (prereqConditionType != RemoteDialogueTrigger.MultiConditionType.OR)
{
if (prereqConditionType == RemoteDialogueTrigger.MultiConditionType.AND && allConditionsMet)
2021-12-20 11:48:50 +00:00
{
2022-01-25 22:58:07 -08:00
conditionsMet = true;
2021-12-20 11:48:50 +00:00
}
2022-03-02 19:46:33 -08:00
}
else if (anyConditionsMet)
{
conditionsMet = true;
}
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
if (conditionsMet && __instance._listDialogues[i].priority < int.MaxValue)
{
dialogue = __instance._listDialogues[i];
dialogueIndex = i;
2021-12-20 11:48:50 +00:00
}
}
2022-03-02 19:46:33 -08:00
}
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
if (dialogueIndex == -1)
{
__result = false;
return false;
}
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
__instance._activatedDialogues[dialogueIndex] = true;
__result = true;
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
__instance.GetWorldObject<QSBRemoteDialogueTrigger>()
.SendMessage(new EnterRemoteDialogueMessage(dialogueIndex));
2021-12-20 11:48:50 +00:00
2022-03-02 19:46:33 -08:00
return false;
}
2022-01-08 11:41:55 +00:00
2022-03-02 19:46:33 -08:00
[HarmonyPostfix]
[HarmonyPatch(typeof(GameSave), nameof(GameSave.SetPersistentCondition))]
2022-03-27 00:24:07 -07:00
public static void SetPersistentCondition(string condition, bool state) =>
new PersistentConditionMessage(condition, state).Send();
2022-01-29 20:41:12 +00:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(DialogueConditionManager), nameof(DialogueConditionManager.AddCondition))]
2022-03-27 00:24:07 -07:00
public static void AddCondition(string conditionName, bool conditionState) =>
2022-03-02 19:46:33 -08:00
new DialogueConditionMessage(conditionName, conditionState).Send();
2020-12-03 08:28:05 +00:00
}