Created
January 28, 2019 14:20
-
-
Save plenarius/eab565e4a5ecbf41b8691a9cb4843ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Events/MapEvents.hpp" | |
#include "API/CNWSPlayer.hpp" | |
#include "API/CNWSMessage.hpp" | |
#include "API/Functions.hpp" | |
#include "API/Constants.hpp" | |
#include "Events.hpp" | |
#include "Utils.hpp" | |
#include <cstring> | |
namespace Events { | |
using namespace NWNXLib; | |
using namespace NWNXLib::API; | |
using namespace NWNXLib::Platform; | |
static NWNXLib::Hooking::FunctionHook* m_HandlePlayerToServerMapPinSetMapPinAtHook = nullptr; | |
static NWNXLib::Hooking::FunctionHook* m_HandlePlayerToServerMapPinChangePinHook = nullptr; | |
static NWNXLib::Hooking::FunctionHook* m_HandlePlayerToServerMapPinDestroyMapPinHook = nullptr; | |
MapEvents::MapEvents(ViewPtr<Services::HooksProxy> hooker) | |
{ | |
hooker->RequestExclusiveHook<Functions::CNWSMessage__HandlePlayerToServerMapPinSetMapPinAt, int32_t, | |
CNWSMessage*, CNWSPlayer*>(&HandleMapPinSetMapPinAtMessageHook); | |
m_HandlePlayerToServerMapPinSetMapPinAtHook = hooker->FindHookByAddress(API::Functions::CNWSMessage__HandlePlayerToServerMapPinSetMapPinAt); | |
hooker->RequestExclusiveHook<Functions::CNWSMessage__HandlePlayerToServerMapPinChangePin, int32_t, | |
CNWSMessage*, CNWSPlayer*>(&HandleMapPinChangePinMessageHook); | |
m_HandlePlayerToServerMapPinChangePinHook = hooker->FindHookByAddress(API::Functions::CNWSMessage__HandlePlayerToServerMapPinChangePin);\ | |
hooker->RequestExclusiveHook<Functions::CNWSMessage__HandlePlayerToServerMapPinDestroyMapPin, int32_t, | |
CNWSMessage*, CNWSPlayer*>(&HandleMapPinDestroyMapPinMessageHook); | |
m_HandlePlayerToServerMapPinDestroyMapPinHook = hooker->FindHookByAddress(API::Functions::CNWSMessage__HandlePlayerToServerMapPinDestroyMapPin); | |
} | |
template <typename T> | |
static T PeekMessage(CNWSMessage *pMessage, int32_t offset) | |
{ | |
static_assert(std::is_pod<T>::value); | |
T value; | |
uint8_t *ptr = pMessage->m_pnReadBuffer + pMessage->m_nReadBufferPtr + offset; | |
std::memcpy(&value, ptr, sizeof(T)); | |
return value; | |
} | |
int32_t MapEvents::HandleMapPinSetMapPinAtMessageHook(CNWSMessage *thisPtr, CNWSPlayer *pPlayer) | |
{ | |
int32_t retVal; | |
Types::ObjectID oidPlayer = pPlayer ? pPlayer->m_oidNWSObject : Constants::OBJECT_INVALID; | |
Events::SignalEvent("NWNX_ON_MAP_PIN_ADD_PIN", oidPlayer); | |
retVal = m_HandlePlayerToServerMapPinSetMapPinAtHook->CallOriginal<int32_t>(thisPtr, pPlayer); | |
return retVal; | |
} | |
int32_t MapEvents::HandleMapPinChangePinMessageHook(CNWSMessage *thisPtr, CNWSPlayer *pPlayer) | |
{ | |
int32_t retVal; | |
Types::ObjectID oidPlayer = pPlayer ? pPlayer->m_oidNWSObject : Constants::OBJECT_INVALID; | |
Events::SignalEvent("NWNX_ON_MAP_PIN_CHANGE_PIN", oidPlayer); | |
retVal = m_HandlePlayerToServerMapPinChangePinHook->CallOriginal<int32_t>(thisPtr, pPlayer); | |
return retVal; | |
} | |
int32_t MapEvents::HandleMapPinDestroyMapPinMessageHook(CNWSMessage *thisPtr, CNWSPlayer *pPlayer) | |
{ | |
int32_t retVal; | |
Types::ObjectID oidPlayer = pPlayer ? pPlayer->m_oidNWSObject : Constants::OBJECT_INVALID; | |
static std::string pin_id; | |
pin_id = std::to_string(PeekMessage<int32_t>(thisPtr, 0)); | |
Events::PushEventData("NWNX_ON_MAP_PIN_DESTROY_PIN_ID", pin_id); | |
Events::SignalEvent("NWNX_ON_MAP_PIN_DESTROY_PIN", oidPlayer); | |
retVal = m_HandlePlayerToServerMapPinDestroyMapPinHook->CallOriginal<int32_t>(thisPtr, pPlayer); | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment