Skip to content

Commit

Permalink
pos: persist across logins to turd
Browse files Browse the repository at this point in the history
  • Loading branch information
mtijanic committed Jan 19, 2019
1 parent 0b90b10 commit f812779
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Core/NWNXCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ void NWNXCore::InitialSetupHooks()

m_services->m_hooks->RequestSharedHook<API::Functions::CNWSObject__CNWSObjectDtor__0, void>(&Services::PerObjectStorage::CNWSObject__CNWSObjectDtor__0_hook);
m_services->m_hooks->RequestSharedHook<API::Functions::CNWSArea__CNWSAreaDtor__0, void>(&Services::PerObjectStorage::CNWSArea__CNWSAreaDtor__0_hook);
m_services->m_hooks->RequestSharedHook<API::Functions::CNWSPlayer__EatTURD, void>(&Services::PerObjectStorage::CNWSPlayer__EatTURD_hook);
m_services->m_hooks->RequestSharedHook<API::Functions::CNWSPlayer__DropTURD, void>(&Services::PerObjectStorage::CNWSPlayer__DropTURD_hook);

g_setStringHook = m_services->m_hooks->FindHookByAddress(API::Functions::CNWSScriptVarTable__SetString);
g_getStringHook = m_services->m_hooks->FindHookByAddress(API::Functions::CNWSScriptVarTable__GetString);
Expand Down
65 changes: 63 additions & 2 deletions NWNXLib/Services/PerObjectStorage/PerObjectStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
#include "API/CGameObject.hpp"
#include "API/CNWSArea.hpp"
#include "API/CNWSObject.hpp"
#include "API/CNWSPlayer.hpp"
#include "API/CNWSPlayerTURD.hpp"
#include "API/CNWSModule.hpp"
#include "API/CExoLinkedListInternal.hpp"
#include "API/CExoLinkedListNode.hpp"
#include "API/Constants.hpp"

#include <sstream>

namespace NWNXLib {

namespace Services {
Expand Down Expand Up @@ -124,7 +131,47 @@ PerObjectStorage::ObjectStorage::~ObjectStorage()
}
}

void PerObjectStorage::ObjectStorage::CloneFrom(PerObjectStorage::ObjectStorage *other)
{
if (!other)
return;

if (other->m_IntMap)
m_IntMap = std::make_unique<IntMap>(*other->m_IntMap);
if (other->m_FloatMap)
m_FloatMap = std::make_unique<FloatMap>(*other->m_FloatMap);
if (other->m_StringMap)
m_StringMap = std::make_unique<StringMap>(*other->m_StringMap);
if (other->m_PointerMap)
m_PointerMap = std::make_unique<PointerMap>(*other->m_PointerMap);
}

std::string PerObjectStorage::ObjectStorage::DumpToString()
{
std::stringstream ss;
ss << "Object ID: " << std::hex << m_oidOwner << std::endl;
if (m_IntMap)
{
for (auto it: *m_IntMap)
ss << it.first << " = " << std::dec << it.second << std::endl;
}
if (m_FloatMap)
{
for (auto it: *m_FloatMap)
ss << it.first << " = " << it.second << std::endl;
}
if (m_StringMap)
{
for (auto it: *m_StringMap)
ss << it.first << " = " << it.second << std::endl;
}
if (m_PointerMap)
{
for (auto it: *m_PointerMap)
ss << it.first << " = " << it.second.first << std::endl;
}
return ss.str();
}

PerObjectStorageProxy::PerObjectStorageProxy(PerObjectStorage& perObjectStorage, std::string pluginName)
: ServiceProxy<PerObjectStorage>(perObjectStorage)
Expand Down Expand Up @@ -209,7 +256,6 @@ void PerObjectStorage::DestroyObjectStorage(API::CGameObject *pGameObject)
{
if (pGameObject->m_pNwnxData)
{
LOG_DEBUG("Destroying object storage for objectId:0x%08x", pGameObject->m_idSelf);
delete static_cast<PerObjectStorage::ObjectStorage*>(pGameObject->m_pNwnxData);
pGameObject->m_pNwnxData = nullptr;
}
Expand All @@ -225,7 +271,22 @@ void PerObjectStorage::CNWSArea__CNWSAreaDtor__0_hook(Services::Hooks::CallType
if (type == Services::Hooks::CallType::AFTER_ORIGINAL)
DestroyObjectStorage(static_cast<API::CGameObject*>(pThis));
}

void PerObjectStorage::CNWSPlayer__EatTURD_hook(Services::Hooks::CallType type, API::CNWSPlayer* thisPtr, API::CNWSPlayerTURD* pTURD)
{
if (type == Services::Hooks::CallType::BEFORE_ORIGINAL)
{
GetObjectStorage(thisPtr->m_oidNWSObject)->CloneFrom(GetObjectStorage(pTURD));
}
}
void PerObjectStorage::CNWSPlayer__DropTURD_hook(Services::Hooks::CallType type, API::CNWSPlayer* thisPtr)
{
if (type == Services::Hooks::CallType::AFTER_ORIGINAL)
{
auto turdlist = Utils::GetModule()->m_lstTURDList.m_pcExoLinkedListInternal;
auto *pTURD = static_cast<API::CNWSPlayerTURD*>(turdlist->pHead->pObject);
GetObjectStorage(pTURD)->CloneFrom(GetObjectStorage(thisPtr->m_oidNWSObject));
}
}

}
}
5 changes: 5 additions & 0 deletions NWNXLib/Services/PerObjectStorage/PerObjectStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class PerObjectStorage : public ServiceBase

static void CNWSObject__CNWSObjectDtor__0_hook(Services::Hooks::CallType type, API::CNWSObject* thisPtr);
static void CNWSArea__CNWSAreaDtor__0_hook(Services::Hooks::CallType type, API::CNWSArea* thisPtr);
static void CNWSPlayer__EatTURD_hook(Services::Hooks::CallType type, API::CNWSPlayer* thisPtr, API::CNWSPlayerTURD* pTURD);
static void CNWSPlayer__DropTURD_hook(Services::Hooks::CallType type, API::CNWSPlayer* thisPtr);
private:
class ObjectStorage
{
Expand All @@ -54,6 +56,9 @@ class PerObjectStorage : public ServiceBase
ObjectStorage(API::Types::ObjectID owner);
~ObjectStorage();

void CloneFrom(ObjectStorage *other);
std::string DumpToString();

API::Types::ObjectID m_oidOwner;
std::unique_ptr<IntMap> m_IntMap;
std::unique_ptr<FloatMap> m_FloatMap;
Expand Down

0 comments on commit f812779

Please sign in to comment.