Lately i've been reverse engineering Crimson Desert a lot.
You can find my findings here: PureLiquid-Crimson-Desert-External on GitHub
I found that there are a lot of so called Managers which are all following the same structure.
So i wrote a dumper for it to create a class for each manager and collect the offset to each Manager at runtime.
How does it work?
Every manager has basically the following structure:
#pragma once
#include <GlobalData/Include.h>
#include <Math/Vector.h>
#include <string_view>
#include <CrimsonDesert/Structures/ManagerLoopDumper.h>
#include <cstdint>
#include <cstddef>
using namespace Globals;
namespace CrimsonDesert {
class MetaData {
public:
inline std::string GetName() {
return pProc->ReadString(pProc->ReadDirect<uintptr_t>(pProc->ReadDirect<uintptr_t>(reinterpret_cast<uintptr_t>(this))));
}
inline std::string GetLocation() {
return pProc->ReadString(pProc->ReadDirect<uintptr_t>(pProc->ReadDirect<uintptr_t>(reinterpret_cast<uintptr_t>(this))) + 0x19);
}
};
class ManagerListEntry {
private:
char pad[0x18];
public:
PROPERTY(m_iHash, int, 0x0);
PROPERTY(m_iUnknown, int, 0x4);
PROPERTY(m_pMetaData, MetaData*, 0x8);
PROPERTY(m_iUnknown2, int, 0x10);
PROPERTY(m_iUnknown3, int, 0x14);
};
class BaseManager {
public:
PROPERTY(m_iCount, int, 0x28);
inline ManagerListEntry* GetEntry(int i) {
return reinterpret_cast<ManagerListEntry*>(pProc->ReadDirect<uintptr_t>(reinterpret_cast<uintptr_t>(this) + 0x20) + (sizeof(ManagerListEntry) * i));
}
protected:
template <typename T>
inline static T *GetInstanceFor() {
std::string name = typeid(T).name();
auto pos = name.rfind("::");
if (pos != std::string::npos)
name = name.substr(pos + 2);
uintptr_t pOffset = ManagerLoopDumper::GetOffsetFor(name);
if (!pOffset) {
printf("Error Collecting offset for \"%s\"!", name.c_str());
return nullptr;
}
return pProc->ReadDirect<T*>(pExeMod->GetAddr() + pOffset);
}
};
}
Beside this, every manager has the same GetEntry function in memory.
That means i can catch every Manager using the same pattern, collect it's offset for static usage or runtime extraction and get it's name using Reflection/ RTTI in c++!
So basically when i search for the Manager's GetEntry function Pattern in IDA (
48 83 EC ? 44 0F B7 01 48 8B 0D ? ? ? ? 44 3B 41 ? 72 ? 48 8B 05 ? ? ? ? 48 83 C4 ? C3),
i can find all of them:
In total there are at least 120 so called "Managers" in this Game.
You can see an example of a Manager's GetEntry function in ASM here:
As i said, the cool thing is that every Manager follows the same structure!
So you already saw the BaseManager class above.
To dump every Managers Entries we just need to generate a class for Each Manager extending the BaseManager class.
How do we do that?
Let's create our header:
#pragma once
#include <GlobalData/Include.h>
#include <Math/Vector.h>
#include <cstdint>
#include <cstddef>
#include <functional>
#include <string>
#include <map>
namespace CrimsonDesert {
class ManagerLoopDumper {
private:
inline static bool bOffsetsCollected = false;
inline static std::map<std::string, uintptr_t> managerOffsetMap{};
static void CollectOffsets();
static void ForEachManager(
const std::function<void(
const std::string& className,
uintptr_t pOffset,
uintptr_t functionLocation
)>& callback
);
public:
static void Generate(bool bStatic = false);
static uintptr_t GetOffsetFor(std::string);
};
}
For the implementation we need the following helpers:
struct MSVC_RTTICompleteObjectLocator
{
uint32_t signature; // 0x00
uint32_t offset; // 0x04
uint32_t cdOffset; // 0x08
uint32_t pTypeDescriptor; // 0x0C
uint32_t pClassDescriptor; // 0x10
uint32_t pSelf; // 0x14
};
// custom demangle function since "DbgHelp" didnt work for me.
std::string CustomDemangle(std::string_view name)
{
constexpr std::string_view prefix = ".?AV";
constexpr std::string_view suffix = "@pa@@";
if (!name.starts_with(prefix) || !name.ends_with(suffix))
return std::string(name);
auto className = name.substr(
prefix.size(),
name.size() - prefix.size() - suffix.size()
);
return std::string(className);
}
After that we can go on to pattern scanning and abstracting the loop:
void ManagerLoopDumper::ForEachManager(
const std::function<void(
const std::string&,
uintptr_t,
uintptr_t
)>& callback
)
{
auto mgrFns = pExeMod->ScanMemoryMulti(
"48 83 EC ? 44 0F B7 01 48 8B 0D ? ? ? ? 44 3B 41 ? 72 ? 48 8B 05 ? ? ? ? 48 83 C4 ? C3"
);
uintptr_t pBaseAddr = pExeMod->GetAddr();
for (const auto& mgrFn : mgrFns)
{
auto pOffset = pExeMod->ResolveRIP(
mgrFn + 0x8,
3,
7
) - pBaseAddr;
auto pInstAddr = pProc->ReadDirect<uintptr_t>(
pOffset + pBaseAddr
);
if (!pInstAddr)
continue;
uintptr_t vtable = pProc->ReadDirect<uintptr_t>(pInstAddr);
if (!vtable)
continue;
uintptr_t colAddress = pProc->ReadDirect<uintptr_t>(vtable - 0x8);
if (!colAddress)
continue;
auto col = pProc->ReadDirect<MSVC_RTTICompleteObjectLocator>(
colAddress
);
if (col.signature != 1 || !col.pTypeDescriptor || !col.pSelf)
continue;
uintptr_t imageBase = colAddress - col.pSelf;
uintptr_t typeDescriptor = imageBase + col.pTypeDescriptor;
char name[256]{};
for (size_t i = 0; i < sizeof(name) - 1; ++i)
{
name[i] = pProc->ReadDirect<char>(
typeDescriptor + 0x10 + i
);
if (!name[i])
break;
}
if (!name[0])
continue;
callback(
CustomDemangle(name),
pOffset,
reinterpret_cast<uintptr_t>(mgrFn) - pBaseAddr
);
}
}
Now we can use it generate our header files extending the BaseManager:
void ManagerLoopDumper::Generate(bool bStatic)
{
std::string outputDir = GENERATED_MANAGERS_DIR;
// Generate shared offsets header
std::ofstream offsetsFile(outputDir + "Offsets.h");
// Generate shared manager includes header
std::ofstream includesFile(outputDir + "Include.h");
if (!offsetsFile.is_open() || !includesFile.is_open())
{
printf("Failed to create generated headers!\n");
return;
}
offsetsFile << "#pragma once\n\n";
includesFile << "#pragma once\n\n";
std::unordered_set<std::string> generatedManagers;
ForEachManager([&offsetsFile,&outputDir,&generatedManagers, &includesFile, &bStatic](
const std::string& className,
uintptr_t pOffset,
uintptr_t functionLocation)
{
if (!generatedManagers.insert(className).second)
return;
// Generate manager header
std::string headerPath = outputDir + className + ".h";
std::ofstream headerFile(headerPath);
if (!headerFile.is_open())
{
printf("Failed to create %s!\n", headerPath.c_str());
generatedManagers.erase(className);
return;
}
headerFile
<< "#pragma once\n"
<< "#include <GlobalData/Include.h>\n"
<< "#include <CrimsonDesert/Structures/BaseManager.h>\n";
if (bStatic)
headerFile << "#include <CrimsonDesert/Managers/Offsets.h>\n";
headerFile
<< "#include <Math/Vector.h>\n"
<< "\n"
<< "#include <cstdint>\n"
<< "#include <cstddef>\n"
<< "\n"
<< "using namespace Globals;\n"
<< "\n"
<< "namespace CrimsonDesert {\n"
<< "\tclass " << className << " : public BaseManager {\n"
<< "\tpublic:\n"
<< "\t\tinline static " << className << "* GetInstance() {\n";
if (bStatic) {
headerFile
<< "\t\t\treturn pProc->ReadDirect<" << className
<< "*>(pExeMod->GetAddr() + " << className << "_offset);\n";
}
else {
headerFile
<< "\t\t\treturn BaseManager::GetInstanceFor<"<<className << ">();\n";
}
headerFile
<< "\t\t}\n"
<< "\t};\n"
<< "}\n";
headerFile.close();
// Generate offset definition
offsetsFile
<< "#define " << className << "_offset 0x"
<< std::hex << std::uppercase << pOffset
<< std::dec << "\n";
// Generate include entry
includesFile
<< "#include <CrimsonDesert/Managers/"
<< className << ".h>\n";
printf(
"Generated: %s (offset: 0x%llX)\n",
className.c_str(),
static_cast<unsigned long long>(pOffset)
);
});
offsetsFile.close();
includesFile.close();
printf(
"Generated %zu manager headers.\n",
generatedManagers.size()
);
}
Additional CollectOffsetFor function for runtime resolving:
void ManagerLoopDumper::CollectOffsets()
{
if (bOffsetsCollected)
return;
ForEachManager([](
const std::string& className,
uintptr_t pOffset,
uintptr_t functionLocation)
{
ManagerLoopDumper::managerOffsetMap.insert({ className, pOffset });
});
bOffsetsCollected = true;
}
uintptr_t ManagerLoopDumper::GetOffsetFor(std::string managerName)
{
if (!bOffsetsCollected)
CollectOffsets();
return managerOffsetMap.contains(managerName) ? managerOffsetMap.at(managerName) : uintptr_t();
}
Our generated class looks like this now for example:
#pragma once
#include <GlobalData/Include.h>
#include <CrimsonDesert/Structures/BaseManager.h>
#include <Math/Vector.h>
#include <cstdint>
#include <cstddef>
using namespace Globals;
namespace CrimsonDesert {
class NpcInfoManager : public BaseManager {
public:
inline static NpcInfoManager* GetInstance() {
return BaseManager::GetInstanceFor<NpcInfoManager>();
}
};
}
If i now startup and loop through the npc info manager like for example:
auto pNpcManager = CrimsonDesert::NpcInfoManager::GetInstance();
printf("%i Entities\n", pNpcManager->m_iCount);
for (int i = 0; i < pNpcManager->m_iCount; i++) {
auto npc = pNpcManager->GetEntry(i);
auto hash = npc->m_iHash;
auto pFirstNpc = npc->m_pMetaData;
auto idx = pProc->ReadDirect<int>(reinterpret_cast<uintptr_t>(pFirstNpc) + 0x8);
printf("Npc[%i => 0x%p ( %i )] => %s | %s\n", i, pFirstNpc, idx, pFirstNpc->GetName().c_str(), pFirstNpc->GetLocation().c_str());
}
We can Dump all the existing Entities in the Game Right now:
Hope somebody found that useful!
Happy Reversing :)



Top comments (0)