170 lines
5.5 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;
using System.Linq;
2020-09-18 15:32:22 +01:00
namespace QSB.ConversationSync.Patches
2020-09-18 15:32:22 +01: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
public static readonly string[] PersistentConditionsToSync =
{
2022-01-08 11:41:55 +00:00
"MET_SOLANUM",
"MET_PRISONER",
"TALKED_TO_GABBRO",
"GABBRO_MERGE_TRIGGERED",
"KNOWS_MEDITATION"
};
[HarmonyPrefix]
[HarmonyPatch(typeof(DialogueConditionManager), nameof(DialogueConditionManager.SetConditionState))]
public static bool SetConditionState(string conditionName, bool conditionState)
{
new DialogueConditionMessage(conditionName, conditionState).Send();
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
2021-06-18 21:54:32 +01: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 22:38:32 +01:00
2021-04-29 18:30:45 +01: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 21:54:32 +01:00
public static bool CharacterDialogueTree_EndConversation(CharacterDialogueTree __instance)
2020-12-02 21:23:01 +00:00
{
if (!__instance.enabled)
{
return false;
}
2021-06-18 22:38:32 +01:00
2021-04-29 18:30:45 +01: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 22:38:32 +01:00
2021-04-29 18:30:45 +01: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 15:32:22 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.InputDialogueOption))]
public static bool CharacterDialogueTree_InputDialogueOption(CharacterDialogueTree __instance, int optionIndex)
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 20:42:45 +02:00
var selectedOption = __instance._currentDialogueBox.OptionFromUIIndex(optionIndex);
2020-12-02 21:23:01 +00:00
ConversationManager.Instance.SendPlayerOption(selectedOption.Text);
return true;
}
2020-09-22 21:11:29 +01:00
[HarmonyPostfix]
[HarmonyPatch(typeof(DialogueNode), nameof(DialogueNode.GetNextPage))]
public static void DialogueNode_GetNextPage(DialogueNode __instance)
2020-12-02 21:23:01 +00:00
{
var key = __instance._name + __instance._listPagesToDisplay[__instance._currentPage];
2020-12-02 21:23:01 +00:00
// Sending key so translation can be done on client side - should make different language-d clients compatible
2021-04-29 18:30:45 +01: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;
2022-01-25 22:58:07 -08:00
var dialogueIndex = -1;
2021-12-20 11:48:50 +00:00
for (var i = 0; i < __instance._listDialogues.Length; i++)
{
if (!__instance._activatedDialogues[i])
{
2022-01-25 22:58:07 -08:00
var allConditionsMet = true;
var anyConditionsMet = __instance._listDialogues[i].prereqConditions.Length == 0;
2021-12-20 11:48:50 +00:00
2022-01-25 22:58:07 -08:00
foreach (var prereqCondition in __instance._listDialogues[i].prereqConditions)
2021-12-20 11:48:50 +00:00
{
2022-01-25 22:58:07 -08:00
if (DialogueConditionManager.SharedInstance.GetConditionState(prereqCondition))
2021-12-20 11:48:50 +00:00
{
2022-01-25 22:58:07 -08:00
anyConditionsMet = true;
2021-12-20 11:48:50 +00:00
}
else
{
2022-01-25 22:58:07 -08:00
allConditionsMet = false;
2021-12-20 11:48:50 +00:00
}
}
2022-01-25 22:58:07 -08:00
var conditionsMet = false;
var prereqConditionType = __instance._listDialogues[i].prereqConditionType;
2021-12-20 11:48:50 +00:00
if (prereqConditionType != RemoteDialogueTrigger.MultiConditionType.OR)
{
2022-01-25 22:58:07 -08:00
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-01-25 22:58:07 -08:00
else if (anyConditionsMet)
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-01-25 22:58:07 -08:00
if (conditionsMet && __instance._listDialogues[i].priority < int.MaxValue)
2021-12-20 11:48:50 +00:00
{
dialogue = __instance._listDialogues[i];
2022-01-25 22:58:07 -08:00
dialogueIndex = i;
2021-12-20 11:48:50 +00:00
}
}
}
2022-01-25 22:58:07 -08:00
if (dialogueIndex == -1)
2021-12-20 11:48:50 +00:00
{
__result = false;
return false;
}
2022-01-25 22:58:07 -08:00
__instance._activatedDialogues[dialogueIndex] = true;
__result = true;
2021-12-20 11:48:50 +00:00
__instance.GetWorldObject<QSBRemoteDialogueTrigger>()
2022-01-25 22:58:07 -08:00
.SendMessage(new EnterRemoteDialogueMessage(dialogueIndex));
2021-12-20 11:48:50 +00:00
return false;
}
2022-01-08 11:41:55 +00:00
[HarmonyPostfix]
[HarmonyPatch(typeof(GameSave), nameof(GameSave.SetPersistentCondition))]
public static void SetPersistentCondition(string condition, bool state)
{
if (PersistentConditionsToSync.Contains(condition))
{
2022-01-08 15:04:52 +00:00
new PersistentConditionMessage(condition, state).Send();
2022-01-08 11:41:55 +00:00
}
}
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
}