Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CGameMenu.cpp #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 71 additions & 80 deletions source/CGameMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,119 +1,110 @@
#include "stdafx.h"
#include "CGameMenu.h"
#include "cleo.h"
#include "CleoBase.h"
#include "CDebug.h"
#include "CFont.h"
#include "plugin.h"
#include <sstream>

namespace CLEO
{
const char CLEO_VERSION_STR[] = VERSION_STRING;
CMenuManager* MenuManager;

void(__cdecl * TextDraw) (float x, float y, const char* text);
void(__cdecl * SetTextAlign) (BYTE nAlign);
void(__cdecl * SetTextFont) (BYTE nFont);
void(__cdecl* SetTextEdge) (char nEdge);
void(__cdecl * SetLetterSize) (float w, float h);
void(__cdecl * SetLetterColor) (RGBA color);

// thiscalls dont work with memory_pointer operator overload template
DWORD CMenuManager__ScaleX;
DWORD CMenuManager__ScaleY;
DWORD CTexture__DrawInRect;
//void (__thiscall * CTexture__DrawInRect) (void *texture, RwRect2D *rect, RwRGBA color);

CMenuManager * MenuManager;

float CGameMenu_ScaleX(CMenuManager *pMenu, float w)
void __declspec(naked) CTexture_DrawInRect_Hook()
{
float fReturn;
_asm
__asm
{
mov ecx, pMenu
push w
call CMenuManager__ScaleX
fstp fReturn
push ebp
mov ebp, esp
push ecx
push dword ptr [ebp + 0x08]
push dword ptr [ebp + 0x04]
mov ecx, dword ptr [ebp + 0x0C]
call CTexture__DrawInRect
pop ebp
ret 0x04
}
return fReturn;
}

float CGameMenu_ScaleY(CMenuManager *pMenu, float w)
void __fastcall OnDrawMenuBackground(void *texture, int dummy, RwRect2D *rect, RwRGBA *color)
{
float fReturn;
_asm
{
mov ecx, pMenu
push w
call CMenuManager__ScaleY
fstp fReturn
}
return fReturn;
}
_asm push ebp
_asm mov ebp, esp

void CTexture_DrawInRect(void *pTexture, RwRect2D *rect, RwRGBA *colour)
{
// Call original CTexture_DrawInRect function
_asm
{
push colour
push color
push rect
mov ecx, pTexture
mov ecx, texture
call CTexture__DrawInRect
}
}

void __fastcall OnDrawMenuBackground(void *texture, int dummy, RwRect2D *rect, RwRGBA *color)
{
CTexture_DrawInRect(texture, rect, color);
// Set font properties
CFont::SetBackground(false, false);
CFont::SetWrapx(640.0f);
CFont::SetFontStyle(FONT_MENU);
CFont::SetProportional(true);
CFont::SetOrientation(ALIGN_LEFT);

CFont::SetColor({ 0xAD, 0xCE, 0xC4, 0xFF });
CFont::SetEdge(1);
CFont::SetDropColor({ 0x00, 0x00, 0x00, 0xFF });

// Calculate font size and positioning
const float fontSize = 0.5f / 448.0f;
const float aspect = (float)RsGlobal.maximumWidth / RsGlobal.maximumHeight;
const float subtextHeight = 0.75f; // factor of first line height
float sizeX = fontSize * 0.5f / aspect * RsGlobal.maximumWidth;
float sizeY = fontSize * RsGlobal.maximumHeight;

float posX = 25.0f * sizeX; // left margin
float posY = RsGlobal.maximumHeight - 15.0f * sizeY; // bottom margin

// Check if any scripts or plugins are active
auto cs_count = GetInstance().ScriptEngine.WorkingScriptsCount();
auto plugin_count = GetInstance().PluginSystem.GetNumPlugins();
std::ostringstream cleo_text;
cleo_text << "CLEO v" << CLEO_VERSION_STR;
if (cs_count || plugin_count)
{
posY -= 15.0f * sizeY; // add space for bottom text
}

// Draw CLEO version text
std::ostringstream text;
text &lt;&lt; "CLEO v" &lt;&lt; CLEO_VERSION_STR;
#ifdef _DEBUG
cleo_text << " DEBUG";
text &lt;&lt; " ~r~~h~DEBUG";
#endif
SetTextAlign(1);
SetTextFont(2);
if (SetTextEdge) SetTextEdge(1);
SetLetterSize(CGameMenu_ScaleX(MenuManager, 0.23f), CGameMenu_ScaleY(MenuManager, 0.4f));

SetLetterColor(RGBA(/*0xE1, 0xE1, 0xE1, 0xFF*/0xAD, 0xCE, 0xC4, 0xFF));
TextDraw(CGameMenu_ScaleX(MenuManager, 6.0f), CGameMenu_ScaleY(MenuManager, 428.0f), cleo_text.str().c_str());
cleo_text.str("");
CFont::SetScale(sizeX, sizeY);
CFont::PrintString(posX, posY - 15.0f * sizeY, text.str().c_str());

if (cs_count || plugin_count)
{
if (plugin_count) cleo_text << plugin_count << (plugin_count > 1 ? " plugins" : " plugin");
if (cs_count && plugin_count) cleo_text << " / ";
if (cs_count) cleo_text << cs_count << (cs_count > 1 ? " scripts" : " script");
//plugin_text << plugin_count << (plugin_count > 1 ? " plugins" : " plugin");
SetTextAlign(1);
//SetTextFont(2);

SetLetterSize(CGameMenu_ScaleX(MenuManager, 0.18f), CGameMenu_ScaleY(MenuManager, 0.34f));

SetLetterColor(RGBA(/*0xE1, 0xE1, 0xE1, 0xFF*/0xAD, 0xCE, 0xC4, 0xFF));
TextDraw(CGameMenu_ScaleX(MenuManager, 6.0f), CGameMenu_ScaleY(MenuManager, 436.0f), cleo_text.str().c_str());
text.str("");
if (plugin_count) text &lt;&lt; plugin_count &lt;&lt; (plugin_count &gt; 1 ? " plugins" : " plugin");
if (cs_count &amp;&amp; plugin_count) text &lt;&lt; " / ";
if (cs_count) text &lt;&lt; cs_count &lt;&lt; (cs_count &gt; 1 ? " scripts" : " script");

posY += 15.0f * sizeY;
sizeX *= subtextHeight;
sizeY *= subtextHeight;
CFont::SetScale(sizeX, sizeY);
CFont::PrintString(posX, posY - 15.0f * sizeY, text.str().c_str());
}

_asm pop ebp
_asm ret 0x04
}

void CGameMenu::Inject(CCodeInjector& inj)
void CGameMenu::Inject(CCodeInjector&amp; inj)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these &amp;

{
TRACE("Injecting MenuStatusNotifier...");
CGameVersionManager& gvm = GetInstance().VersionManager;
CGameVersionManager&amp; gvm = GetInstance().VersionManager;
MenuManager = gvm.TranslateMemoryAddress(MA_MENU_MANAGER);
TextDraw = gvm.TranslateMemoryAddress(MA_DRAW_TEXT_FUNCTION);
SetTextAlign = gvm.TranslateMemoryAddress(MA_SET_TEXT_ALIGN_FUNCTION);
SetTextFont = gvm.TranslateMemoryAddress(MA_SET_TEXT_FONT_FUNCTION);
SetTextEdge = gvm.TranslateMemoryAddress(MA_SET_TEXT_EDGE_FUNCTION);

// Meh...
CMenuManager__ScaleX = gvm.TranslateMemoryAddress(MA_CMENU_SCALE_X_FUNCTION);
CMenuManager__ScaleY = gvm.TranslateMemoryAddress(MA_CMENU_SCALE_Y_FUNCTION);

SetLetterSize = gvm.TranslateMemoryAddress(MA_SET_LETTER_SIZE_FUNCTION);
SetLetterColor = gvm.TranslateMemoryAddress(MA_SET_LETTER_COLOR_FUNCTION);
inj.MemoryNop(gvm.TranslateMemoryAddress(MA_CALL_CTEXTURE_DRAW_BG_RECT).address + 1, 5);
inj.MemoryJump(gvm.TranslateMemoryAddress(MA_CALL_CTEXTURE_DRAW_BG_RECT).address + 1, (void*)CTexture_DrawInRect_Hook, true);

inj.MemoryReadOffset(gvm.TranslateMemoryAddress(MA_CALL_CTEXTURE_DRAW_BG_RECT).address + 1, CTexture__DrawInRect, true);
//gvm.TranslateMemoryAddress(MA_CTEXTURE_DRAW_IN_RECT_FUNCTION);
inj.ReplaceFunction(OnDrawMenuBackground, gvm.TranslateMemoryAddress(MA_CALL_CTEXTURE_DRAW_BG_RECT));
}
}