Skip to content

Commit

Permalink
feat: sync player dialogue through chat
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 11, 2022
1 parent 99b16c4 commit e811196
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 10 deletions.
11 changes: 11 additions & 0 deletions Code/client/Events/PlayerDialogueEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

struct PlayerDialogueEvent
{
PlayerDialogueEvent(String aText)
: Text(std::move(aText))
{
}

String Text;
};
47 changes: 47 additions & 0 deletions Code/client/Games/Misc/MenuTopicManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "MenuTopicManager.h"

#include <Events/PlayerDialogueEvent.h>

TP_THIS_FUNCTION(TPlayDialogueOption, bool, MenuTopicManager, int32_t aIndex);
static TPlayDialogueOption* RealPlayDialogueOption = nullptr;

MenuTopicManager* MenuTopicManager::Get() noexcept
{
POINTER_SKYRIMSE(MenuTopicManager*, s_singleton, 401099);

return *s_singleton.Get();
}

bool TP_MAKE_THISCALL(HookPlayDialogueOption, MenuTopicManager, int32_t aIndex)
{
if (apThis->pOptions)
{
int i = 0;
const char* pText = nullptr;
for (auto option : *apThis->pOptions)
{
if (i == aIndex)
{
pText = option->text;
break;
}
i++;
}

if (pText != nullptr)
World::Get().GetRunner().Trigger(PlayerDialogueEvent(pText));
}

return ThisCall(RealPlayDialogueOption, apThis, aIndex);
}

TiltedPhoques::Initializer s_menuTopicHooks([]()
{
POINTER_SKYRIMSE(TPlayDialogueOption, s_playDialogueOption, 35269);

RealPlayDialogueOption = s_playDialogueOption.Get();

TP_HOOK(&RealPlayDialogueOption, HookPlayDialogueOption);
});


14 changes: 14 additions & 0 deletions Code/client/Games/Misc/MenuTopicManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

struct MenuTopicManager
{
static MenuTopicManager* Get() noexcept;

struct DialogueOption
{
const char* text;
};

uint8_t pad0[0x20];
GameList<DialogueOption>* pOptions;
};
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ int32_t Actor::GetGoldAmount() noexcept

void Actor::SetActorInventory(Inventory& aInventory) noexcept
{
spdlog::info("Setting inventory for actor {:X}", formID);
spdlog::debug("Setting inventory for actor {:X}", formID);

UnEquipAll();

Expand Down
4 changes: 2 additions & 2 deletions Code/client/Games/Skyrim/EquipManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void* TP_MAKE_THISCALL(EquipHook, EquipManager, Actor* apActor, TESForm* apItem,
const auto pExtension = apActor->GetExtension();
if (pExtension->IsRemote())
{
spdlog::info("Actor[{:X}]::Equip(), item form id: {:X}", apActor->formID, apItem->formID);
spdlog::debug("Actor[{:X}]::Equip(), item form id: {:X}", apActor->formID, apItem->formID);
if (!ScopedEquipOverride::IsOverriden())
return nullptr;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ void* TP_MAKE_THISCALL(UnEquipHook, EquipManager, Actor* apActor, TESForm* apIte
const auto pExtension = apActor->GetExtension();
if (pExtension->IsRemote())
{
spdlog::info("Actor[{:X}]::Unequip(), item form id: {:X}, IsOverridden, equip: {}, inventory: {}", apActor->formID, apItem->formID, ScopedEquipOverride::IsOverriden(), ScopedInventoryOverride::IsOverriden());
spdlog::debug("Actor[{:X}]::Unequip(), item form id: {:X}, IsOverridden, equip: {}, inventory: {}", apActor->formID, apItem->formID, ScopedEquipOverride::IsOverriden(), ScopedInventoryOverride::IsOverriden());
// The ScopedInventoryOverride check is here to allow the item to be unequipped if it is removed
// Without this check, the game will not accept null as a return, and it'll keep trying to unequip infinitely
if (!ScopedEquipOverride::IsOverriden() && !ScopedInventoryOverride::IsOverriden())
Expand Down
6 changes: 5 additions & 1 deletion Code/client/Games/Skyrim/Events/EventDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ struct TESPlayerBowShotEvent

struct TESTopicInfoEvent
{

TESObjectREFR* hSpeakerRef;
void* pCallback;
uint32_t uiTopicInfoFormID;
int32_t eType;
uint16_t usStage;
};

struct TESTrackedStatsEvent
Expand Down
3 changes: 2 additions & 1 deletion Code/client/Games/Skyrim/TESObjectREFR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ Inventory TESObjectREFR::GetEquippedItems() const noexcept

void TESObjectREFR::SetInventory(const Inventory& aInventory) noexcept
{
spdlog::info("Setting inventory for {:X}", formID);
spdlog::debug("Setting inventory for {:X}", formID);

ScopedInventoryOverride _;

Expand Down Expand Up @@ -520,6 +520,7 @@ void TESObjectREFR::AddOrRemoveItem(const Inventory::Entry& arEntry) noexcept

// TODO(cosideci): this needs to be tested. This just creates a copy of the "quest object".
// Might cause issues when delivering quests.
// Maybe make it so that the quest leader gets the ref, and the other party members get the copy?
if (arEntry.IsQuestItem)
{
Actor* pActor = Cast<Actor>(this);
Expand Down
7 changes: 7 additions & 0 deletions Code/client/Services/Generic/OverlayService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <Messages/NotifyChatMessageBroadcast.h>
#include <Messages/NotifyPlayerList.h>
#include <Messages/NotifyPlayerDialogue.h>

#include <Events/ConnectedEvent.h>
#include <Events/DisconnectedEvent.h>
Expand Down Expand Up @@ -60,6 +61,7 @@ OverlayService::OverlayService(World& aWorld, TransportService& transport, entt:
//m_playerListConnection = aDispatcher.sink<NotifyPlayerList>().connect<&OverlayService::OnPlayerList>(this);
//m_cellChangeEventConnection = aDispatcher.sink<CellChangeEvent>().connect<&OverlayService::OnCellChangeEvent>(this);
m_chatMessageConnection = aDispatcher.sink<NotifyChatMessageBroadcast>().connect<&OverlayService::OnChatMessageReceived>(this);
m_playerDialogueConnection = aDispatcher.sink<NotifyPlayerDialogue>().connect<&OverlayService::OnPlayerDialogue>(this);
}

OverlayService::~OverlayService() noexcept
Expand Down Expand Up @@ -193,6 +195,11 @@ void OverlayService::OnDisconnectedEvent(const DisconnectedEvent&) noexcept
SendSystemMessage("Disconnected from server");
}

void OverlayService::OnPlayerDialogue(const NotifyPlayerDialogue& acMessage) noexcept
{
SendSystemMessage(acMessage.Text.c_str());
}

void OverlayService::OnPlayerList(const NotifyPlayerList& acPlayerList) noexcept
{
for (auto& player : acPlayerList.Players)
Expand Down
15 changes: 15 additions & 0 deletions Code/client/Services/Generic/PlayerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <Events/DisconnectedEvent.h>
#include <Events/GridCellChangeEvent.h>
#include <Events/CellChangeEvent.h>
#include <Events/PlayerDialogueEvent.h>

#include <Messages/PlayerRespawnRequest.h>
#include <Messages/NotifyPlayerRespawn.h>
#include <Messages/ShiftGridCellRequest.h>
#include <Messages/EnterExteriorCellRequest.h>
#include <Messages/EnterInteriorCellRequest.h>
#include <Messages/PlayerDialogueRequest.h>

#include <Structs/ServerSettings.h>

Expand All @@ -29,6 +31,7 @@ PlayerService::PlayerService(World& aWorld, entt::dispatcher& aDispatcher, Trans
m_notifyRespawnConnection = m_dispatcher.sink<NotifyPlayerRespawn>().connect<&PlayerService::OnNotifyPlayerRespawn>(this);
m_gridCellChangeConnection = m_dispatcher.sink<GridCellChangeEvent>().connect<&PlayerService::OnGridCellChangeEvent>(this);
m_cellChangeConnection = m_dispatcher.sink<CellChangeEvent>().connect<&PlayerService::OnCellChangeEvent>(this);
m_playerDialogueConnection = m_dispatcher.sink<PlayerDialogueEvent>().connect<&PlayerService::OnPlayerDialogueEvent>(this);
}

void PlayerService::OnUpdate(const UpdateEvent& acEvent) noexcept
Expand Down Expand Up @@ -106,6 +109,18 @@ void PlayerService::OnCellChangeEvent(const CellChangeEvent& acEvent) const noex
}
}

void PlayerService::OnPlayerDialogueEvent(const PlayerDialogueEvent& acEvent) const noexcept
{
const auto& partyService = m_world.GetPartyService();
if (!partyService.IsInParty() || !partyService.IsLeader())
return;

PlayerDialogueRequest request{};
request.Text = acEvent.Text;

m_transport.Send(request);
}

void PlayerService::RunRespawnUpdates(const double acDeltaTime) noexcept
{
static bool s_startTimer = false;
Expand Down
3 changes: 3 additions & 0 deletions Code/client/Services/OverlayService.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct CellChangeEvent;
struct TransportService;
struct NotifyChatMessageBroadcast;
struct NotifyPlayerList;
struct NotifyPlayerDialogue;

using TiltedPhoques::OverlayApp;

Expand Down Expand Up @@ -58,6 +59,7 @@ struct OverlayService
// void OnPlayerLeave(const PlayerLeaveEvent&) noexcept;
void OnCellChangeEvent(const CellChangeEvent&) noexcept;
void OnChatMessageReceived(const NotifyChatMessageBroadcast&) noexcept;
void OnPlayerDialogue(const NotifyPlayerDialogue&) noexcept;
void OnPlayerList(const NotifyPlayerList&) noexcept;

private:
Expand All @@ -75,4 +77,5 @@ struct OverlayService
entt::scoped_connection m_cellChangeEventConnection;
entt::scoped_connection m_chatMessageConnection;
entt::scoped_connection m_playerListConnection;
entt::scoped_connection m_playerDialogueConnection;
};
3 changes: 3 additions & 0 deletions Code/client/Services/PlayerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct DisconnectedEvent;
struct ServerSettings;
struct GridCellChangeEvent;
struct CellChangeEvent;
struct PlayerDialogueEvent;

struct NotifyPlayerRespawn;

Expand All @@ -29,6 +30,7 @@ struct PlayerService
void OnNotifyPlayerRespawn(const NotifyPlayerRespawn& acMessage) const noexcept;
void OnGridCellChangeEvent(const GridCellChangeEvent& acEvent) const noexcept;
void OnCellChangeEvent(const CellChangeEvent& acEvent) const noexcept;
void OnPlayerDialogueEvent(const PlayerDialogueEvent& acEvent) const noexcept;

private:

Expand All @@ -55,4 +57,5 @@ struct PlayerService
entt::scoped_connection m_notifyRespawnConnection;
entt::scoped_connection m_gridCellChangeConnection;
entt::scoped_connection m_cellChangeConnection;
entt::scoped_connection m_playerDialogueConnection;
};
3 changes: 2 additions & 1 deletion Code/encoding/Messages/ClientMessageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <Messages/PlayerRespawnRequest.h>
#include <Messages/DialogueRequest.h>
#include <Messages/SubtitleRequest.h>
#include <Messages/PlayerDialogueRequest.h>

using TiltedPhoques::UniquePtr;

Expand All @@ -67,7 +68,7 @@ struct ClientMessageFactory
RequestOwnershipClaim, RequestObjectInventoryChanges, SpellCastRequest, ProjectileLaunchRequest, InterruptCastRequest,
AddTargetRequest, ScriptAnimationRequest, DrawWeaponRequest, MountRequest, NewPackageRequest,
RequestRespawn, SyncExperienceRequest, RequestEquipmentChanges, SendChatMessageRequest,
TeleportCommandRequest, PlayerRespawnRequest, DialogueRequest, SubtitleRequest>;
TeleportCommandRequest, PlayerRespawnRequest, DialogueRequest, SubtitleRequest, PlayerDialogueRequest>;

return s_visitor(std::forward<T>(func));
}
Expand Down
13 changes: 13 additions & 0 deletions Code/encoding/Messages/NotifyPlayerDialogue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Messages/NotifyPlayerDialogue.h>

void NotifyPlayerDialogue::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteString(aWriter, Text);
}

void NotifyPlayerDialogue::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ServerMessage::DeserializeRaw(aReader);

Text = Serialization::ReadString(aReader);
}
23 changes: 23 additions & 0 deletions Code/encoding/Messages/NotifyPlayerDialogue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Message.h"

struct NotifyPlayerDialogue final : ServerMessage
{
static constexpr ServerOpcode Opcode = kNotifyPlayerDialogue;

NotifyPlayerDialogue() : ServerMessage(Opcode)
{
}

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const NotifyPlayerDialogue& achRhs) const noexcept
{
return GetOpcode() == achRhs.GetOpcode() &&
Text == achRhs.Text;
}

TiltedPhoques::String Text{};
};
13 changes: 13 additions & 0 deletions Code/encoding/Messages/PlayerDialogueRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Messages/PlayerDialogueRequest.h>

void PlayerDialogueRequest::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteString(aWriter, Text);
}

void PlayerDialogueRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ClientMessage::DeserializeRaw(aReader);

Text = Serialization::ReadString(aReader);
}
26 changes: 26 additions & 0 deletions Code/encoding/Messages/PlayerDialogueRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "Message.h"

using TiltedPhoques::String;

struct PlayerDialogueRequest final : ClientMessage
{
static constexpr ClientOpcode Opcode = kPlayerDialogueRequest;

PlayerDialogueRequest() : ClientMessage(Opcode)
{
}

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const PlayerDialogueRequest& acRhs) const noexcept
{
return GetOpcode() == acRhs.GetOpcode() &&
Text == acRhs.Text;
}

TiltedPhoques::String Text{};
};

3 changes: 2 additions & 1 deletion Code/encoding/Messages/ServerMessageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <Messages/NotifyPlayerRespawn.h>
#include <Messages/NotifyDialogue.h>
#include <Messages/NotifySubtitle.h>
#include <Messages/NotifyPlayerDialogue.h>

using TiltedPhoques::UniquePtr;

Expand All @@ -64,7 +65,7 @@ struct ServerMessageFactory
NotifyObjectInventoryChanges, NotifySpellCast, NotifyProjectileLaunch, NotifyInterruptCast,
NotifyAddTarget, NotifyScriptAnimation, NotifyDrawWeapon, NotifyMount, NotifyNewPackage,
NotifyRespawn, NotifySyncExperience, NotifyEquipmentChanges, NotifyChatMessageBroadcast,
TeleportCommandResponse, NotifyPlayerRespawn, NotifyDialogue, NotifySubtitle>;
TeleportCommandResponse, NotifyPlayerRespawn, NotifyDialogue, NotifySubtitle, NotifyPlayerDialogue>;

return s_visitor(std::forward<T>(func));
}
Expand Down
2 changes: 2 additions & 0 deletions Code/encoding/Opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum ClientOpcode : unsigned char
kPlayerRespawnRequest,
kDialogueRequest,
kSubtitleRequest,
kPlayerDialogueRequest,
kClientOpcodeMax
};

Expand Down Expand Up @@ -91,5 +92,6 @@ enum ServerOpcode : unsigned char
kNotifyPlayerRespawn,
kNotifyDialogue,
kNotifySubtitle,
kNotifyPlayerDialogue,
kServerOpcodeMax
};
Loading

0 comments on commit e811196

Please sign in to comment.