Skip to content

Commit

Permalink
Replace call_function to CFileMgr::SetDir from scripts with CLEO_SetS…
Browse files Browse the repository at this point in the history
…criptWorkDir
  • Loading branch information
MiranDMC committed Dec 4, 2024
1 parent dde625c commit 71cc603
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
33 changes: 33 additions & 0 deletions cleo_plugins/MemoryOperations/AntiHacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "CLEO.h"
#include <string>

namespace AntiHacks
{
// checks if specific function call is allowed. Performs replacement action if needed
static bool CheckCall(CLEO::CRunningScript* thread, void* function, void* object, CLEO::SCRIPT_VAR* args, size_t argCount, DWORD& result)
{
switch ((size_t)function)
{
// Setting work directory. Some older mods used these instead of 0A99 opcode
case 0x005387D0: // CFileMgr::SetDir(const char* relPath)
case 0x00836F1E: // _chdir(LPCSTR lpPathName)
case 0x0085824C: // SetCurrentDirectoryA(LPCSTR lpPathName)
{
auto resolved = std::string(args[0].pcParam);
resolved.resize(MAX_PATH);
CLEO_ResolvePath(thread, resolved.data(), resolved.size());

CLEO_SetScriptWorkDir(thread, resolved.c_str());
return false; // block call
}
break;

default:
break;
}

return true; // allow call
}
};

52 changes: 28 additions & 24 deletions cleo_plugins/MemoryOperations/MemoryOperations.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "CLEO.h"
#include "CLEO_Utils.h"
#include "AntiHacks.h"
#include "plugin.h"
#include "CTheScripts.h"
#include <filesystem>
Expand Down Expand Up @@ -160,30 +161,33 @@ class MemoryOperations

SCRIPT_VAR* arguments_end = arguments + numArg;
numPop *= 4; // bytes peer argument
DWORD result;
int oriSp, postSp; // stack validation
_asm
{
mov oriSp, esp

// transfer args to stack
lea ecx, arguments
call_func_loop :
cmp ecx, arguments_end
jae call_func_loop_end
push[ecx]
add ecx, 0x4
jmp call_func_loop
call_func_loop_end :

// call function
mov ecx, obj
xor eax, eax
call func
mov result, eax // get result
add esp, numPop // cleanup stack

mov postSp, esp
DWORD result = 0;
int oriSp = 0, postSp = 0; // stack validation
if (AntiHacks::CheckCall(thread, func, obj, scriptParams, numArg, result))
{
_asm
{
mov oriSp, esp

// transfer args to stack
lea ecx, arguments
call_func_loop :
cmp ecx, arguments_end
jae call_func_loop_end
push[ecx]
add ecx, 0x4
jmp call_func_loop
call_func_loop_end :

// call function
mov ecx, obj
xor eax, eax
call func
mov result, eax // get result
add esp, numPop // cleanup stack

mov postSp, esp
}
}

// validate stack pointer
Expand Down
1 change: 1 addition & 0 deletions cleo_plugins/MemoryOperations/MemoryOperations.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ if defined GTA_SA_DIR (
<ItemGroup>
<ClInclude Include="..\..\cleo_sdk\CLEO.h" />
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h" />
<ClInclude Include="AntiHacks.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\Resource.rc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h">
<Filter>cleo_sdk</Filter>
</ClInclude>
<ClInclude Include="AntiHacks.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\Resource.rc" />
Expand Down

0 comments on commit 71cc603

Please sign in to comment.