Skip to content

Commit

Permalink
Add event debug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 21, 2024
1 parent 7947602 commit 60f6db4
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//#include "modules/Draw.h"
#include "modules/Log.h"
#include "modules/ScriptLog.h"
#include "modules/Level.h"

#include "cdc/render/PCDeviceManager.h"

Expand Down Expand Up @@ -116,6 +117,7 @@ void Hook::RegisterModules()
RegisterModule<Skew>();

#ifndef TR8
RegisterModule<LevelModule>();
//RegisterModule<Render>();
//RegisterModule<Draw>();
#else
Expand Down
5 changes: 5 additions & 0 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ GameTracker* Game::GetGameTracker()
return (GameTracker*)GET_ADDRESS(0x10E5370, 0x838330, 0x00E7F088);
}

STracker* Game::GetStreamTracker()
{
return (STracker*)GET_ADDRESS(0x11582F8, 0x8AE378, 0xDBAB40);
}

void GAMELOOP_RequestLevelChangeByName(char* name, GameTracker* gameTracker, int doneType)
{
auto addr = GET_ADDRESS(0x451970, 0xC61CFA, 0x5DF8C0);
Expand Down
2 changes: 2 additions & 0 deletions src/game/Game.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "instance/Instance.h"
#include "level/Stream.h"

struct menu_t;
struct MemCardInfo;
Expand Down Expand Up @@ -111,6 +112,7 @@ class Game
public:
static Instance* GetPlayerInstance();
static GameTracker* GetGameTracker();
static STracker* GetStreamTracker();
};

void GAMELOOP_RequestLevelChangeByName(char* name, GameTracker* gameTracker, int doneType);
9 changes: 9 additions & 0 deletions src/level/Event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Event.h"
#include "util/Hooking.h"

void* RELOC_GetProcAddress(void* peData, char* lpszName)
{
auto addr = GET_ADDRESS(0x464550, 0x4680C0, 0x000000);

return Hooking::CallReturn<void*>(addr, peData, lpszName);
}
22 changes: 22 additions & 0 deletions src/level/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

struct EventVar
{
char* name;
int variable;
};

struct UnsavedVar
{
char* name;
int* variable;
};

struct GlobalData
{
char pad1[232];

int* eventVars;
};

void* RELOC_GetProcAddress(void* peData, char* lpszName);
111 changes: 111 additions & 0 deletions src/level/Level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#include "cdc/math/Vector.h"

struct Intro;
struct StreamUnitPortal;
struct Signal;
struct Instance;
struct KDNode;

struct IndexedFace
{
unsigned __int16 i0;
unsigned __int16 i1;
unsigned __int16 i2;

unsigned __int8 adjacencyFlags;
unsigned __int8 collisionFlags;
unsigned __int8 clientFlags;
unsigned __int8 materialType;
};

struct MeshVertex
{
__int16 x;
__int16 y;
__int16 z;
};

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

struct BBox
{
cdc::Vector3 bMin;
cdc::Vector3 bMax;
};

struct Mesh
{
BBox m_box;
cdc::Vector3 m_position;

void* m_vertices;
IndexedFace* m_faces;
KDNode* m_root;

void* m_clientData;

unsigned __int16 m_vertexType;
unsigned __int16 m_numNodes;
unsigned __int16 m_numFaces;
unsigned __int16 m_numVertices;
unsigned __int16 m_height;
unsigned __int16 m_numDegenerateFaces;
unsigned __int16 m_numNonManifoldEdges;
};

struct Level;

struct TerrainGroup
{
cdc::Vector3 globalOffset;
cdc::Vector3 localOffset;

int flags;
int ID;
int uniqueID;
int splineID;
Instance* instanceSpline;
Level* level;
Mesh* mesh;

char pad1[116];
};

struct Terrain
{
__int16 UnitChangeFlags;
__int16 spad;

int numIntros;
Intro* introList;

int numStreamUnitPortals;
StreamUnitPortal* streamUnitPortals;

int numTerrainGroups;
TerrainGroup* terrainGroups;

TerrainGroup* signalTerrainGroup;
Signal* signals;
};

struct Level
{
Terrain* terrain;

char pad1[140];

Signal* SignalListStart;
__int16* SignalIDList;
void* splineCameraData;

void* relocModule; // Pointer to script executable
};
24 changes: 24 additions & 0 deletions src/level/Stream.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
#pragma once

#include "instance/Object.h"
#include "level/Level.h"

#if TRAE || TR7
#define MAX_UNIT_NAME_LENGTH 20
#else
#define MAX_UNIT_NAME_LENGTH 128
#endif

#define MAX_STREAM_UNITS 8

struct ResolveObject;

struct StreamUnit
{
int StreamUnitID;

char used;
char unitHidden;
__int16 unitFlags;

Level* level;
char baseAreaName[20];

ResolveObject* resolveObj;

char pad1[56];
};

struct STracker
{
StreamUnit StreamList[MAX_STREAM_UNITS];
};

struct ObjectTracker
{
ResolveObject* resolveObj;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/InstanceViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void InstanceViewer::OnDraw()
if (m_show)
{
ImGui::Begin("Instances", &m_show);
ImGui::Columns(2, "instances");
ImGui::Columns(2);

// Filter
ImGui::InputText("Name", m_filter, sizeof(m_filter));
Expand Down
147 changes: 147 additions & 0 deletions src/modules/Level.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <imgui.h>
#include <MinHook.h>

#include "Level.h"
#include "level/Stream.h"
#include "level/Event.h"
#include "game/Game.h"
#include "util/Hooking.h"

static bool s_disableScript = false;
static void (*s_STREAM_FinishLoad)(StreamUnit*);

static void STREAM_FinishLoad(StreamUnit* streamUnit)
{
if (s_disableScript)
{
streamUnit->level->relocModule = nullptr;
}

s_STREAM_FinishLoad(streamUnit);
}

LevelModule::LevelModule()
{
// Insert hook
auto addr = GET_ADDRESS(0x5D5640, 0x5DB680, 0x000000);

MH_CreateHook((void*)addr, STREAM_FinishLoad, (void**)&s_STREAM_FinishLoad);
MH_EnableHook(MH_ALL_HOOKS);
}

void LevelModule::OnMenu()
{
if (ImGui::BeginMenu("Level"))
{
ImGui::MenuItem("Disable script", nullptr, &s_disableScript);
ImGui::MenuItem("Event debug", nullptr, &m_eventDebug);

ImGui::EndMenu();
}
}

void LevelModule::OnDraw()
{
if (m_eventDebug)
{
DrawEventDebug();
}
}

void LevelModule::DrawEventDebug()
{
ImGui::Begin("Event debug", &m_eventDebug);
ImGui::Columns(2);

auto stream = Game::GetStreamTracker();

// Level list
ImGui::BeginChild("LevelsTree");

for (int i = 0; i < MAX_STREAM_UNITS; i++)
{
auto unit = &stream->StreamList[i];

if (unit->used != 2)
{
continue;
}

if (ImGui::TreeNodeEx((void*)unit, ImGuiTreeNodeFlags_Leaf, "%s", unit->baseAreaName))
{
if (ImGui::IsItemClicked())
{
m_selected = unit;
}

ImGui::TreePop();
}
}

ImGui::EndChild();

// Event debug
ImGui::NextColumn();

if (m_selected && m_selected->used != 2)
{
m_selected = nullptr;
}

if (m_selected)
{
ImGui::BeginChild("EventDebug");

DrawEventDebug(m_selected);

ImGui::EndChild();
}

ImGui::End();
}

void LevelModule::DrawEventDebug(StreamUnit* unit)
{
auto level = unit->level;

if (!level->relocModule)
{
return;
}

// Get the event debug export
auto eventVars = (EventVar*)RELOC_GetProcAddress(level->relocModule, "EventDebug");

if (eventVars)
{
auto globalData = (GlobalData*)GET_ADDRESS(0x1076980, 0x7C8A50, 0x000000);
auto i = 0;

// Display all event variables
while (true)
{
auto var = &eventVars[i++];

if (var->name == nullptr)
break;

ImGui::Text("%d %s = %d", var->variable, var->name, globalData->eventVars[var->variable]);
}

// Display all unsaved variables
auto unsavedVars = (UnsavedVar*)eventVars;
while (true)
{
auto var = &unsavedVars[i++];

if (var->name == nullptr || var->variable == nullptr)
break;

ImGui::Text("%s %d", var->name, *var->variable);
}
}
else
{
ImGui::Text("No event debug for this level");
}
}
Loading

0 comments on commit 60f6db4

Please sign in to comment.