quantum-space-buddies/QSB/ConversationSync/Patches/ConversationPatches.cs

149 lines
4.8 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Common;
2021-12-24 06:59:53 +00:00
using QSB.ConversationSync.Messages;
2021-12-20 11:48:50 +00:00
using QSB.ConversationSync.WorldObjects;
2021-12-24 06:59:53 +00: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 14:32:22 +00:00
using System.Collections.Generic;
namespace QSB.ConversationSync.Patches
2020-09-18 14:32:22 +00:00
{
[HarmonyPatch]
2020-12-02 21:23:01 +00:00
public class ConversationPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2020-11-03 21:11:10 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
2021-06-18 20:54:32 +00:00
public static void CharacterDialogueTree_StartConversation(CharacterDialogueTree __instance)
2020-12-02 21:23:01 +00:00
{
2020-12-11 13:14:58 +00:00
var index = QSBWorldSync.OldDialogueTrees.FindIndex(x => x == __instance);
2020-12-02 21:23:01 +00:00
if (index == -1)
{
DebugLog.ToConsole($"Warning - Index for tree {__instance.name} was -1.", MessageType.Warning);
}
2021-06-18 21:38:32 +00:00
2021-04-29 17:30:45 +00:00
QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId = index;
2020-12-02 21:23:01 +00:00
ConversationManager.Instance.SendConvState(index, true);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.EndConversation))]
2021-06-18 20:54:32 +00:00
public static bool CharacterDialogueTree_EndConversation(CharacterDialogueTree __instance)
2020-12-02 21:23:01 +00:00
{
if (!__instance.enabled)
{
return false;
}
2021-06-18 21:38:32 +00:00
2021-04-29 17:30:45 +00:00
if (QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId == -1)
2020-12-02 21:23:01 +00:00
{
DebugLog.ToConsole($"Warning - Ending conversation with CurrentDialogueId of -1! Called from {__instance.name}", MessageType.Warning);
2020-12-19 18:52:36 +00:00
return true;
2020-12-02 21:23:01 +00:00
}
2021-06-18 21:38:32 +00:00
2021-04-29 17:30:45 +00:00
ConversationManager.Instance.SendConvState(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId, false);
ConversationManager.Instance.CloseBoxCharacter(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId);
QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId = -1;
2020-12-02 21:23:01 +00:00
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2020-09-18 14:32:22 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.InputDialogueOption))]
2021-06-18 20:54:32 +00:00
public static bool CharacterDialogueTree_InputDialogueOption(int optionIndex, DialogueBoxVer2 ____currentDialogueBox)
2020-12-02 21:23:01 +00:00
{
if (optionIndex < 0)
{
// in a page where there is no selectable options
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2020-09-29 18:42:45 +00:00
2020-12-02 21:23:01 +00:00
var selectedOption = ____currentDialogueBox.OptionFromUIIndex(optionIndex);
ConversationManager.Instance.SendPlayerOption(selectedOption.Text);
return true;
}
2020-09-22 20:11:29 +00:00
[HarmonyPostfix]
[HarmonyPatch(typeof(DialogueNode), nameof(DialogueNode.GetNextPage))]
2021-06-18 20:54:32 +00:00
public static void DialogueNode_GetNextPage(string ____name, List<string> ____listPagesToDisplay, int ____currentPage)
2020-12-02 21:23:01 +00:00
{
var key = ____name + ____listPagesToDisplay[____currentPage];
// Sending key so translation can be done on client side - should make different language-d clients compatible
2021-04-29 17:30:45 +00:00
QSBCore.UnityEvents.RunWhen(() => QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId != -1,
() => ConversationManager.Instance.SendCharacterDialogue(QSBPlayerManager.LocalPlayer.CurrentCharacterDialogueTreeId, key));
2020-12-02 21:23:01 +00:00
}
2021-12-20 11:48:50 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(RemoteDialogueTrigger), nameof(RemoteDialogueTrigger.ConversationTriggered))]
public static bool ConversationTriggeredReplacement(RemoteDialogueTrigger __instance, ref bool __result, out RemoteDialogueTrigger.RemoteDialogueCondition dialogue)
{
dialogue = default;
var maxValue = int.MaxValue;
var num = -1;
var sharedInstance = DialogueConditionManager.SharedInstance;
for (var i = 0; i < __instance._listDialogues.Length; i++)
{
if (!__instance._activatedDialogues[i])
{
var flag = true;
var flag2 = false;
if (__instance._listDialogues[i].prereqConditions.Length == 0)
{
flag2 = true;
}
for (int j = 0; j < __instance._listDialogues[i].prereqConditions.Length; j++)
{
if (sharedInstance.GetConditionState(__instance._listDialogues[i].prereqConditions[j]))
{
flag2 = true;
}
else
{
flag = false;
}
}
bool flag3 = false;
RemoteDialogueTrigger.MultiConditionType prereqConditionType = __instance._listDialogues[i].prereqConditionType;
if (prereqConditionType != RemoteDialogueTrigger.MultiConditionType.OR)
{
if (prereqConditionType == RemoteDialogueTrigger.MultiConditionType.AND && flag)
{
flag3 = true;
}
}
else if (flag2)
{
flag3 = true;
}
if (flag3 && __instance._listDialogues[i].priority < maxValue)
{
dialogue = __instance._listDialogues[i];
num = i;
}
}
}
if (num == -1)
{
__result = false;
return false;
}
__instance._activatedDialogues[num] = true;
__instance.GetWorldObject<QSBRemoteDialogueTrigger>()
2021-12-24 06:59:53 +00:00
.SendMessage(new EnterRemoteDialogueMessage(num, __instance._listDialogues.IndexOf(dialogue)));
2021-12-20 11:48:50 +00:00
__result = true;
return false;
}
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
}