Skip to content

Commit

Permalink
Merge pull request #10 from Meigyoku-Thmn/feature/c-preprocessor-lua-…
Browse files Browse the repository at this point in the history
…script

Feature/c preprocessor lua script
  • Loading branch information
Meigyoku-Thmn authored Sep 25, 2024
2 parents d2bd359 + 93e7c5b commit 90e7e52
Show file tree
Hide file tree
Showing 34 changed files with 360 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vcpkg_installed/
ThMouseXServer/AutoGenerated/*
ThMouseXServer/*.manifest
*.i
Common/*.lua

# User-specific files
*.rsuser
Expand Down
2 changes: 0 additions & 2 deletions Common/CallbackStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "DataTypes.h"

namespace common::callbackstore {
using UninitializeCallbackType = void (*)(bool isProcessTerminating);
using CallbackType = void (*)(void);
void RegisterUninitializeCallback(UninitializeCallbackType callback, bool isFromManagedCode = false);
void RegisterPostRenderCallback(CallbackType callback);
void RegisterClearMeasurementFlagsCallback(CallbackType callback);
Expand Down
7 changes: 0 additions & 7 deletions Common/Common.def

This file was deleted.

20 changes: 19 additions & 1 deletion Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
<Command>
</Command>
</PreBuildEvent>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="$(SolutionDir)PostBuildTool\bin\x86\$(Configuration)\net48\PostBuildTool"
cl PreparationScript.h /P /EP /u /nologo
if %errorlevel% neq 0 exit /b %errorlevel%
copy PreparationScript.i PreparationScript.lua /Y
if %errorlevel% neq 0 exit /b %errorlevel%
%PostBuildTool% FormatLuaScript PreparationScript.lua</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand Down Expand Up @@ -108,6 +117,15 @@
<Command>
</Command>
</PreBuildEvent>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="$(SolutionDir)PostBuildTool\bin\x86\$(Configuration)\net48\PostBuildTool"
cl PreparationScript.h /P /EP /u /nologo
if %errorlevel% neq 0 exit /b %errorlevel%
copy PreparationScript.i PreparationScript.lua /Y
if %errorlevel% neq 0 exit /b %errorlevel%
%PostBuildTool% FormatLuaScript PreparationScript.lua</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="ComServer.h" />
Expand All @@ -127,6 +145,7 @@
<ClInclude Include="MinHook.h" />
<ClInclude Include="NeoLua.h" />
<ClInclude Include="CallbackStore.h" />
<ClInclude Include="PreparationScript.h" />
<ClInclude Include="Variables.h" />
</ItemGroup>
<ItemGroup>
Expand All @@ -148,7 +167,6 @@
<ClCompile Include="Variables.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="Common.def" />
<None Include="PreparationScript.lua" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
8 changes: 4 additions & 4 deletions Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<ClInclude Include="ComServer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PreparationScript.h">
<Filter>Header Files\LuaScript</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Log.cpp">
Expand Down Expand Up @@ -135,11 +138,8 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Common.def">
<Filter>Source Files</Filter>
</None>
<None Include="PreparationScript.lua">
<Filter>Source Files\LuaScript</Filter>
<Filter>Header Files\LuaScript</Filter>
</None>
</ItemGroup>
</Project>
17 changes: 12 additions & 5 deletions Common/DataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ struct GameConfigLocal : GameConfig {
CComSafeArray<DWORD> AddressChain;
CComHeapPtr<WCHAR> ProcessName;
void Initialize() {
this->AddressChain.Attach(this->Address);
this->ProcessName.Attach(this->processName);
if (this->Address)
this->AddressChain.Attach(this->Address);
if (this->processName)
this->ProcessName.Attach(this->processName);
}
};

Expand Down Expand Up @@ -51,8 +53,10 @@ struct CommonConfigLocal : CommonConfig {
CComHeapPtr<WCHAR> _TextureFilePath;
CComHeapPtr<WCHAR> _ImGuiFontPath;
void Initialize() {
this->_TextureFilePath.Attach(this->TextureFilePath);
this->_ImGuiFontPath.Attach(this->ImGuiFontPath);
if (this->TextureFilePath)
this->_TextureFilePath.Attach(this->TextureFilePath);
if (this->ImGuiFontPath)
this->_ImGuiFontPath.Attach(this->ImGuiFontPath);
}
};

Expand Down Expand Up @@ -279,4 +283,7 @@ struct ShellcodeInput {
ImportWinAPI(ntdll, LdrUnloadDll);
};

using ThreadFunc = LPTHREAD_START_ROUTINE;
using ThreadFunc = LPTHREAD_START_ROUTINE;

typedef void(__cdecl *UninitializeCallbackType)(bool isProcessTerminating);
typedef void(__cdecl *CallbackType)();
14 changes: 8 additions & 6 deletions Common/Helper.Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ using namespace std;
namespace note = common::log;

namespace common::helper::memory {
thread_local vector<DWORD> lastOffsets;
thread_local bool lastIsValid = true;
DWORD ResolveAddress(span<const DWORD> offsets) {
static vector<DWORD> lastOffsets;
static bool lastIsValid = true;
void ResetValidationState() {
lastOffsets.resize(0);
lastIsValid = true;
}
DWORD ResolveAddress(span<const DWORD> offsets, bool doNotValidateLastAddress) {
if (offsets.size() <= 0)
return NULL;
auto memInvalidated = offsets.size() != lastOffsets.size()
Expand All @@ -31,17 +35,15 @@ namespace common::helper::memory {
for (size_t i = 1; i < offsets.size(); i++) {
if (memInvalidated && IsBadReadMem((PVOID)address, sizeof(address))) {
lastIsValid = false;
note::ToFile("Access bad memory region, please check the game's version!");
return NULL;
}
address = *PDWORD(address);
if (!address)
break;
address += offsets[i];
}
if (memInvalidated && IsBadReadMem((PVOID)address, sizeof(address))) {
if (!doNotValidateLastAddress && memInvalidated && IsBadReadMem((PVOID)address, sizeof(address))) {
lastIsValid = false;
note::ToFile("Access bad memory region, please check the game's version!");
return NULL;
}
return address;
Expand Down
3 changes: 2 additions & 1 deletion Common/Helper.Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <functional>

namespace common::helper::memory {
DWORD ResolveAddress(std::span<const DWORD> offsets);
void ResetValidationState();
DWORD ResolveAddress(std::span<const DWORD> offsets, bool doNotValidateLastAddress);
std::string GetAddressConfigAsString();
bool IsBadReadMem(const void* ptr, size_t size);
}
13 changes: 1 addition & 12 deletions Common/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ namespace common::helper {
LocalFree(errorMessage);
}

string& Replace(string& input, const char* keyword, const char* replacement) {
size_t keywordPos = 0;
auto keywordLen = strlen(keyword);
auto replacementLen = strlen(replacement);
while ((keywordPos = input.find(keyword, keywordPos)) != string::npos) {
input.replace(keywordPos, keywordLen, replacement);
keywordPos += replacementLen;
}
return input;
}

tuple<float, const char*> ConvertToFloat(const string& input) {
char* endPtr;
const char* message = nil;
Expand Down Expand Up @@ -185,7 +174,7 @@ namespace common::helper {
return lua::GetPositionAddress();
else {
auto& addressChain = g_gameConfig.AddressChain;
return memory::ResolveAddress(span{ &addressChain[addressChain.GetLowerBound()], addressChain.GetCount() });
return memory::ResolveAddress(span{ &addressChain[addressChain.GetLowerBound()], addressChain.GetCount() }, false);
}
}

Expand Down
1 change: 0 additions & 1 deletion Common/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace common::helper {
UINT, WParamPredicate, LParamPredicate
>;
void ReportLastError(const char* title);
std::string& Replace(std::string& input, const char* keyword, const char* replacement);
std::tuple<float, const char*> ConvertToFloat(const std::string& input);
std::tuple<long, const char*> ConvertToLong(const std::string& input, int base);
std::tuple<unsigned long, const char*> ConvertToULong(const std::string& input, int base);
Expand Down
12 changes: 8 additions & 4 deletions Common/Lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ using namespace std;
static bool scriptingDisabled = false;
static bool usePullMechanism = false;

#define GET_POSITION_ADDRESS "getPositionAddress"

static lua_State* L;

template <CompileTimeString funcName, typename FuncType>
Expand Down Expand Up @@ -63,6 +61,8 @@ namespace common::lua {
decltype(&lua_pushvalue) _lua_pushvalue;
decltype(&lua_type) _lua_type;
decltype(&lua_getfield) _lua_getfield;
decltype(&lua_setfield) _lua_setfield;
decltype(&lua_pushinteger) _lua_pushinteger;

static bool Validate(lua_State* L, int r) {
if (r != 0) {
Expand Down Expand Up @@ -149,6 +149,8 @@ namespace common::lua {
if (!TryImportFunc<SYM_NAME(lua_pushvalue)>(_lua_pushvalue, lua, luaDllName)) return;
if (!TryImportFunc<SYM_NAME(lua_type)>(_lua_type, lua, luaDllName)) return;
if (!TryImportFunc<SYM_NAME(lua_getfield)>(_lua_getfield, lua, luaDllName)) return;
if (!TryImportFunc<SYM_NAME(lua_setfield)>(_lua_setfield, lua, luaDllName)) return;
if (!TryImportFunc<SYM_NAME(lua_pushinteger)>(_lua_pushinteger, lua, luaDllName)) return;

minhook::CreateHook(vector<minhook::HookConfig>{
{ _luaL_callmeta, & luaL_callmeta_hook, &ori_luaL_callmeta },
Expand Down Expand Up @@ -193,8 +195,10 @@ namespace common::lua {

auto oldStackSize = _lua_gettop(L);

_lua_pushinteger(L, uintptr_t(g_coreModule));
_lua_setfield(L, LUA_GLOBALSINDEX, THMOUSEX_MODULE_HANDLE);
auto rs = 0;
if ((rs = _luaL_loadstring(L, luaapi::MakePreparationScript().c_str())) == 0)
if ((rs = _luaL_loadstring(L, luaapi::LuaJitPrepScript.c_str())) == 0)
rs = ori_lua_pcall(L, 0, LUA_MULTRET, 0);
if (!Validate(L, rs)) {
_lua_settop(L, oldStackSize);
Expand Down Expand Up @@ -233,7 +237,7 @@ namespace common::lua {
return NULL;

if (!usePullMechanism)
return Lua_GetPositionAddress();
return luaapi::GetPositionAddress();

auto stackSize = _lua_gettop(L);

Expand Down
Loading

0 comments on commit 90e7e52

Please sign in to comment.