Skip to content

Commit

Permalink
Add modules and instance viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 14, 2024
1 parent 0d085e4 commit 8a136c4
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "Hook.h"
#include "input/MessageHook.h"

// Modules
#include "modules/InstanceViewer.h"

#include "cdc/render/PCDeviceManager.h"

using namespace std::placeholders;
Expand All @@ -19,13 +22,15 @@ static bool D3D_Init()
return ret;
}

Hook::Hook() : m_menu(nullptr)
Hook::Hook() : m_menu(nullptr), m_modules()
{
Initialize();
}

void Hook::Initialize()
{
RegisterModules();

MH_Initialize();
MH_CreateHook((void*)0x4153E0, D3D_Init, (void**)&s_D3D_Init);
MH_EnableHook(MH_ALL_HOOKS);
Expand All @@ -43,6 +48,11 @@ void Hook::PostInitialize()
void Hook::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_menu->OnMessage(hWnd, msg, wParam, lParam);

for (auto& mod : m_modules)
{
mod->OnInput(hWnd, msg, wParam, lParam);
}
}

void Hook::OnDevice()
Expand All @@ -54,6 +64,19 @@ void Hook::OnDevice()
PostInitialize();
}

template<typename T>
void Hook::RegisterModule()
{
auto module = std::make_shared<T>();

m_modules.push_back(module);
}

void Hook::RegisterModules()
{
RegisterModule<InstanceViewer>();
}

Hook& Hook::GetInstance()
{
static Hook instance;
Expand Down
9 changes: 9 additions & 0 deletions src/Hook.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
#pragma once

#include <memory>
#include <list>

#include "menu/Menu.h"
#include "modules/Module.h"

class Hook
{
private:
std::unique_ptr<Menu> m_menu;
std::list<std::shared_ptr<Module>> m_modules;

void Initialize();
void PostInitialize();

template<typename T>
void RegisterModule();
void RegisterModules();

void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

public:
Hook();

void OnDevice();

const auto& GetModules() { return m_modules; }

static Hook& GetInstance();
};
15 changes: 15 additions & 0 deletions src/cdc/math/Matrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "Vector.h"

namespace cdc
{
class Matrix
{
public:
cdc::Vector col0;
cdc::Vector col1;
cdc::Vector col2;
cdc::Vector col3;
};
}
39 changes: 39 additions & 0 deletions src/cdc/math/Vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <xmmintrin.h>

namespace cdc
{
class Vector
{
public:
union
{
__m128 vec128;

struct
{
float x;
float y;
float z;
float w;
};
};
};

class Vector2 : public Vector
{
};

class Vector3 : public Vector
{
};

class Vector4 : public Vector
{
};

class Euler : public Vector
{
};
}
64 changes: 64 additions & 0 deletions src/instance/Instance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "Object.h"

#include "cdc/math/Vector.h"
#include "cdc/math/Matrix.h"

struct NodeType
{
NodeType* prev;
NodeType* next;
};

struct Intro
{
};

struct HModel;
struct SoundInstanceData;
struct Body;
struct AnimatedGeoms;
struct AnimComponent;
struct CharacterProxy;

struct Instance;

struct BaseInstance
{
NodeType node;

Instance* next;
Instance* prev;

cdc::Vector3 position;
cdc::Vector3 oldPos;

cdc::Euler rotation;
cdc::Euler oldRotation;

cdc::Vector3 scale;
cdc::Vector3 shadowPosition;
cdc::Vector3 centerOfMass;

cdc::Matrix* matrix;
cdc::Matrix* oldMatrix;

char pad1[12];

Object* object;
Intro* intro;

char pad2[100];
};

struct Instance : BaseInstance
{
char pad1[192];

void* data;

char pad2[12];

int introUniqueID;
};
11 changes: 11 additions & 0 deletions src/instance/Instances.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Instances.h"

void Instances::Iterate(std::function<void(Instance*)> callback)
{
auto first = *(Instance**)0x817D64;

for (auto instance = first; instance != nullptr; instance = instance->next)
{
callback(instance);
}
}
11 changes: 11 additions & 0 deletions src/instance/Instances.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <functional>

#include "Instance.h"

class Instances
{
public:
static void Iterate(std::function<void(Instance*)> callback);
};
42 changes: 42 additions & 0 deletions src/instance/Object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

struct Model
{
};

struct AnimListEntry;
struct AnimFxHeader;
struct AnimScriptObject;
struct ObjectBase;

struct Object
{
int oflags;
int oflags2;

int uniqueID;
unsigned int guiID;
int functionTableID;

void* obsoleteSoundBank;

__int16 numModels;
__int16 numAnims;
__int16 numAnimPatterns;

Model** modelList;

AnimListEntry* animList;
AnimFxHeader** animFXList;
AnimScriptObject** animPatternList;

int introDist;
int vvIntroDist;
int removeDist;
int vvRemoveDist;

ObjectBase* baseData;

void* data;
char* name;
};
15 changes: 15 additions & 0 deletions src/menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "render/RenderContext.h"
#include "input/MouseHook.h"
#include "input/Input.h"
#include "Hook.h"

#include "cdc/render/PCDeviceManager.h"

Expand Down Expand Up @@ -88,10 +89,24 @@ void Menu::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

void Menu::Draw()
{
auto& modules = Hook::GetInstance().GetModules();

if (ImGui::BeginMainMenuBar())
{
// Draw all modules menus
for (auto& mod : modules)
{
mod->OnMenu();
}

ImGui::EndMainMenuBar();
}

// Draw all menus
for (auto& mod : modules)
{
mod->OnDraw();
}
}

void Menu::SetFocus(bool focus)
Expand Down
72 changes: 72 additions & 0 deletions src/modules/InstanceViewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <imgui.h>

#include "InstanceViewer.h"
#include "instance/Instances.h"

void InstanceViewer::OnMenu()
{
if (ImGui::BeginMenu("Instance"))
{
ImGui::MenuItem("Instances", nullptr, &m_show);

ImGui::EndMenu();
}
}

void InstanceViewer::OnDraw()
{
if (m_show)
{
ImGui::Begin("Instances", &m_show);
ImGui::Columns(2, "instances");

// Instance list
ImGui::BeginChild("InstancesTree");

Instances::Iterate([this](Instance* instance)
{
if (ImGui::TreeNodeEx((void*)instance, ImGuiTreeNodeFlags_Leaf, "%d %s", instance->introUniqueID, instance->object->name))
{
if (ImGui::IsItemClicked())
{
m_selected = instance;
}

ImGui::TreePop();
}
});

ImGui::EndChild();

// Instance properties
ImGui::NextColumn();

// Check if the instance still exists
if (m_selected && m_selected->node.prev == nullptr)
{
m_selected = nullptr;
}

if (m_selected)
{
DrawInstance();
}

ImGui::End();
}
}

void InstanceViewer::DrawInstance()
{
auto instance = m_selected;

ImGui::Text("%s", instance->object->name);

auto position = instance->position;
auto rotation = instance->rotation;

ImGui::Text("Position: %f %f %f", position.x, position.y, position.z);
ImGui::Text("Rotation: %f %f %f", rotation.x, rotation.y, rotation.z);
ImGui::Text("Intro: %d", instance->introUniqueID);
ImGui::Text("Address: %p", instance);
}
Loading

0 comments on commit 8a136c4

Please sign in to comment.