Skip to content

Commit

Permalink
Fix InitMenu, little refactor
Browse files Browse the repository at this point in the history
1. Fix InitMenu bugs
2. Add BitStream Reset
3. Little refactor

+ fix + use only version X.Y.Z.0 (auto build number maybe be added in future)
  • Loading branch information
povargek committed Sep 15, 2024
1 parent 1cb8db8 commit cd52bec
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 67 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 3.15)

project ("MiTurboFix"
VERSION 0.0.3
VERSION 0.0.4
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
Expand Down
94 changes: 57 additions & 37 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
#include <RakNet/StringCompressor.h>
#include "RPCEnumerations.h"

#include <iomanip>
#include <sstream>

void alloc_console() {
#ifdef _DEBUG
AllocConsole();
SetConsoleOutputCP(1251);

FILE* fDummy;
freopen_s(&fDummy, "CONIN$", "r", stdin);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
freopen_s(&fDummy, "CONOUT$", "w", stdout);
#endif
}


Plugin::Plugin(HMODULE hndl) : hModule(hndl) {
alloc_console();
InitConsole();

using namespace std::placeholders;
hookCTimerUpdate.set_cb(std::bind(&Plugin::mainloop, this, _1));
hookCTimerUpdate.install();
Expand All @@ -41,6 +33,8 @@ void Plugin::mainloop(const decltype(hookCTimerUpdate)& hook) {
rakhook::on_receive_rpc += std::bind(&PluginRPC::ApplyAnimation, &RPC, _1, _2);
rakhook::on_receive_rpc += std::bind(&PluginRPC::ApplyActorAnimation, &RPC, _1, _2);
rakhook::on_receive_rpc += std::bind(&PluginRPC::ShowPlayerDialog, &RPC, _1, _2);
rakhook::on_receive_rpc += std::bind(&PluginRPC::InitMenu, &RPC, _1, _2);


inited = true;
}
Expand All @@ -51,17 +45,60 @@ void Plugin::CMessages_AddBigMessageHooked(const decltype(hookCMessages_AddBigMe
char* text, uint32_t duration, uint16_t style)
{
if (style > 6) {
#ifdef _DEBUG
Plugin::AddChatMessage(0xFFFFFFFF, __FUNCTION__ ": bad style = %d, strlen = %d", style, strlen(text));
#endif
Plugin::AddChatMessageDebug(0xFFFFFFFF, __FUNCTION__ ": GameText bad style = %d, strlen = %d", style, strlen(text));

return;
}

return hook.get_trampoline()(text, duration, style);
}

void Plugin::AddChatMessage(std::uint32_t dwColor, std::string sFmrMessage, ...)

/**
* SetSpawnInfo buffer overflow fix (not installing until SAMP is unavailable, for maybe not break single-player mode)
*
* int __cdecl CRestart::AddHospitalRestartPoint(RwV3D *point, float angle, int town)
* mov CRestarts__m_dwHospitalRestartCount, cx
*
* =>
*
* nop (7 bytes)
*/
void Plugin::InstallPatchAddHospitalRestartPoint()
{
MemSet(reinterpret_cast<void*>(0x460773), 0x90, 0x7);
}

void Plugin::MemSet(LPVOID lpAddr, int iVal, size_t dwSize)
{
DWORD oldProtection = PAGE_EXECUTE_READWRITE;
VirtualProtect(lpAddr, dwSize, oldProtection, &oldProtection);
memset(lpAddr, iVal, dwSize);
VirtualProtect(lpAddr, dwSize, oldProtection, &oldProtection);
}

/// <summary>
/// DEBUG Helpers
/// </summary>

void Plugin::PrintBin(std::string str)
{
#ifdef _DEBUG
for (char c : str) { std::cout << std::hex << std::setfill('0') << std::setw(2) << int(c); }

std::cout << std::endl;
#endif
}

/// <summary>
/// Add a debug chat message + WinDbg Message. Displayed ONLY IF you compile a DEBUG build
/// </summary>
/// <param name="dwColor">Message Color</param>
/// <param name="sFmtMessage">Message to format</param>
/// <param name="">Args...</param>
void Plugin::AddChatMessageDebug(std::uint32_t dwColor, std::string sFmtMessage, ...)
{
#ifdef _DEBUG
if (!rakhook::initialized)
return;

Expand All @@ -70,8 +107,8 @@ void Plugin::AddChatMessage(std::uint32_t dwColor, std::string sFmrMessage, ...)
memset(szBuffer, 0x0, sizeof(szBuffer));

va_list args;
va_start(args, sFmrMessage);
vsnprintf(szBuffer, 144, sFmrMessage.c_str(), args);
va_start(args, sFmtMessage);
vsnprintf(szBuffer, 144, sFmtMessage.c_str(), args);
va_end(args);

std::string sMessage = std::string(szBuffer);
Expand All @@ -81,24 +118,7 @@ void Plugin::AddChatMessage(std::uint32_t dwColor, std::string sFmrMessage, ...)
bs.Write<std::uint32_t>(sMessage.size());
bs.Write(sMessage.data(), sMessage.size());
rakhook::emul_rpc(ScriptRPC::ClientMessage, bs);
}

/**
* SetSpawnInfo buffer overflow fix (not installing until SAMP is unavailable, for maybe not break single-player mode)
*
* int __cdecl CRestart::AddHospitalRestartPoint(RwV3D *point, float angle, int town)
* mov CRestarts__m_dwHospitalRestartCount, cx
*
* =>
*
* nop (7 bytes)
*/
void Plugin::InstallPatchAddHospitalRestartPoint()
{
auto address = reinterpret_cast<void*>(0x460773);
auto size = 0x7;
DWORD oldProtection = PAGE_EXECUTE_READWRITE;
VirtualProtect(address, size, oldProtection, &oldProtection);
memset(address, 0x90, size);
VirtualProtect(address, size, oldProtection, &oldProtection);
OutputDebugStringA(std::string(std::string("[MiTurboFix] ") + sMessage).data());
#endif
}
25 changes: 24 additions & 1 deletion src/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ class Plugin {
Plugin(HMODULE hModule);
HMODULE hModule;

static void AddChatMessage(std::uint32_t dwColor, std::string sFmrMessage, ...);
void InstallPatchAddHospitalRestartPoint();

void MemSet(LPVOID lpAddr, int iVal, size_t dwSize);

static void AddChatMessageDebug(std::uint32_t dwColor, std::string sFmrMessage, ...); // DEBUG
private:
PluginRPC RPC;
kthook::kthook_simple<CTimerProto> hookCTimerUpdate{ reinterpret_cast<void*>(0x561B10) };
Expand All @@ -23,4 +26,24 @@ class Plugin {
void mainloop(const decltype(hookCTimerUpdate)& hook);
void CMessages_AddBigMessageHooked(const decltype(hookCMessages_AddBigMessageHooked)& hook, char* text, uint32_t duration, uint16_t style);


/// <summary>
/// Debug helpers
/// </summary>

void PrintBin(std::string str);

inline void InitConsole()
{
#ifdef _DEBUG
AllocConsole();
SetConsoleOutputCP(1251);

FILE* fDummy;
freopen_s(&fDummy, "CONIN$", "r", stdin);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
freopen_s(&fDummy, "CONOUT$", "w", stdout);
#endif
}

};
Loading

0 comments on commit cd52bec

Please sign in to comment.