Skip to content

Commit

Permalink
Add back Underworld script log
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 21, 2024
1 parent 0b8c8fd commit f4cfbea
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "modules/Render.h"
#include "modules/Draw.h"
#include "modules/Log.h"
#include "modules/ScriptLog.h"

#include "cdc/render/PCDeviceManager.h"

Expand Down Expand Up @@ -41,6 +42,10 @@ Hook::Hook() : m_menu(nullptr), m_modules()

void Hook::Initialize()
{
// Initialize MinHook
MH_Initialize();

// Register all modules
RegisterModules();

#ifndef TR8
Expand All @@ -52,7 +57,6 @@ void Hook::Initialize()
#endif

// Create the initial hook
MH_Initialize();
MH_CreateHook(match.get_first(), D3D_Init, (void**)&s_D3D_Init);
MH_EnableHook(MH_ALL_HOOKS);
}
Expand Down Expand Up @@ -110,11 +114,16 @@ void Hook::RegisterModules()
RegisterModule<MainMenu>();
RegisterModule<InstanceViewer>();
RegisterModule<Skew>();

#ifndef TR8
RegisterModule<Render>();
RegisterModule<Draw>();
#else
RegisterModule<ScriptLog>();
#endif

RegisterModule<Log>();

}

Hook& Hook::GetInstance()
Expand Down
14 changes: 12 additions & 2 deletions src/modules/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ void Log::OnDraw()
ImGui::BeginChild("Log");

ImGui::TextUnformatted(m_buffer.begin());
ImGui::SetScrollHereY(1.f);

ImGui::EndChild();
ImGui::End();
}
}

void Log::LogMessage(const char* fmt, ...)
void Log::Write(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);

m_buffer.appendfv(fmt, args);

va_end(args);
}

void Log::WriteLine(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);

// Append-format the log to the buffer
m_buffer.appendfv(fmt, args);
m_buffer.append("\n");

Expand Down
6 changes: 4 additions & 2 deletions src/modules/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Log : public Module
void OnMenu();
void OnDraw();

// Logs a message
void LogMessage(const char* fmt, ...);
// Writes to the log
void Write(const char* fmt, ...);
// Writes a line to the log
void WriteLine(const char* fmt, ...);

// Clears the log
void Clear();
Expand Down
63 changes: 63 additions & 0 deletions src/modules/ScriptLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <any>
#include <MinHook.h>

#include "ScriptLog.h"

#include "Log.h"
#include "Hook.h"

static void LogValue(const char* fmt, std::any value, bool newLine)
{
auto log = Hook::GetInstance().GetModule<Log>();
log->Write(fmt, value);

if (newLine)
{
log->Write("\n");
}
}

// Script function hooks

void __stdcall ScriptPrintInt(int value, int time, bool appendNewLine)
{
LogValue("%d", value, appendNewLine);
}

void __stdcall ScriptPrintFloat(float value, int time, bool appendNewLine)
{
LogValue("%f", value, appendNewLine);
}

void __stdcall ScriptLogInt(int value, bool appendNewLine)
{
LogValue("%d", value, appendNewLine);
}

void __stdcall ScriptLogFloat(float value, bool appendNewLine)
{
LogValue("%f", value, appendNewLine);
}

void __cdecl NsCoreBase_PrintString(int instance, int numArgs, void** args)
{
LogValue("%s", args[0], (__int8)args[1] != 0);
}

void __cdecl NsCoreBase_LogString(int instance, int numArgs, void** args)
{
LogValue("%s", args[0], (__int8)args[1] != 0);
}

ScriptLog::ScriptLog()
{
// Insert all hooks
MH_CreateHook((void*)0x4A3280, ScriptPrintInt, nullptr);
MH_CreateHook((void*)0x4A32C0, ScriptPrintFloat, nullptr);
MH_CreateHook((void*)0x4A3200, ScriptLogInt, nullptr);
MH_CreateHook((void*)0x4A3240, ScriptLogFloat, nullptr);
MH_CreateHook((void*)0x795E30, NsCoreBase_PrintString, nullptr);
MH_CreateHook((void*)0x795DB0, NsCoreBase_PrintString, nullptr);

MH_EnableHook(MH_ALL_HOOKS);
}
9 changes: 9 additions & 0 deletions src/modules/ScriptLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "Module.h"

class ScriptLog : public Module
{
public:
ScriptLog();
};

0 comments on commit f4cfbea

Please sign in to comment.