- fix launcher pinning a thread

- add fall back lua state listener
This commit is contained in:
Anonymous-275
2023-01-21 19:02:47 +00:00
parent ca4fbd10dd
commit 19e28a3d4d
8 changed files with 54 additions and 25 deletions
+2 -1
View File
@@ -16,13 +16,14 @@ class BeamNG {
static void SendIPC(const std::string& Data); static void SendIPC(const std::string& Data);
private: private:
static inline std::unique_ptr<Hook<def::GEUpdate>> TickCountDetour; static inline std::unique_ptr<Hook<def::update_function>> UpdateDetour;
static inline std::unique_ptr<Hook<def::lua_open_jit>> OpenJITDetour; static inline std::unique_ptr<Hook<def::lua_open_jit>> OpenJITDetour;
static inline std::unique_ptr<IPC> IPCFromLauncher; static inline std::unique_ptr<IPC> IPCFromLauncher;
static inline std::unique_ptr<IPC> IPCToLauncher; static inline std::unique_ptr<IPC> IPCToLauncher;
static inline uint64_t GameBaseAddr; static inline uint64_t GameBaseAddr;
static inline uint64_t DllBaseAddr; static inline uint64_t DllBaseAddr;
static int lua_open_jit_D(lua_State* State); static int lua_open_jit_D(lua_State* State);
static uint64_t update_D(lua_State* State);
static void RegisterGEFunctions(); static void RegisterGEFunctions();
// static int GetTickCount_D(void* GEState, void* Param2, void* Param3, void* // static int GetTickCount_D(void* GEState, void* Param2, void* Param3, void*
// Param4); // Param4);
+1
View File
@@ -14,6 +14,7 @@ namespace def {
void* Param4); void* Param4);
typedef uint32_t (*GetTickCount)(); typedef uint32_t (*GetTickCount)();
typedef int (*lua_open_jit)(lua_State* L); typedef int (*lua_open_jit)(lua_State* L);
typedef uint64_t (*update_function)(lua_State* L);
typedef void (*lua_get_field)(lua_State* L, int idx, const char* k); typedef void (*lua_get_field)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_push_fstring)(lua_State* L, const char* fmt, ...); typedef const char* (*lua_push_fstring)(lua_State* L, const char* fmt, ...);
typedef int (*lua_p_call)(lua_State* L, int arg, int res, int err); typedef int (*lua_p_call)(lua_State* L, int arg, int res, int err);
+1
View File
@@ -13,6 +13,7 @@ class GELua {
static inline def::lua_settop lua_settop; static inline def::lua_settop lua_settop;
static inline def::GetTickCount GetTickCount; static inline def::GetTickCount GetTickCount;
static inline def::lua_open_jit lua_open_jit; static inline def::lua_open_jit lua_open_jit;
static inline def::update_function update_function;
static inline def::lua_push_fstring lua_push_fstring; static inline def::lua_push_fstring lua_push_fstring;
static inline def::lua_get_field lua_get_field; static inline def::lua_get_field lua_get_field;
static inline def::lua_p_call lua_p_call; static inline def::lua_p_call lua_p_call;
+1
View File
@@ -14,6 +14,7 @@ class Memory {
static uint32_t GetLauncherPID(const std::set<uint32_t>& BL); static uint32_t GetLauncherPID(const std::set<uint32_t>& BL);
static uint64_t GetModuleBase(const char* Name); static uint64_t GetModuleBase(const char* Name);
static void Print(const std::string& msg); static void Print(const std::string& msg);
static std::string GetHex(uint64_t num);
static void Inject(uint32_t PID); static void Inject(uint32_t PID);
static uint32_t GetTickCount(); static uint32_t GetTickCount();
static uint32_t EntryPoint(); static uint32_t EntryPoint();
+4
View File
@@ -64,4 +64,8 @@ namespace Patterns {
const char* lua_settop[2]{ const char* lua_settop[2]{
"\x4c\x8b\xc1\x85\xd2\x7e\x00\x48\x8b\x41\x00\x48\x8b\x49", "\x4c\x8b\xc1\x85\xd2\x7e\x00\x48\x8b\x41\x00\x48\x8b\x49",
"xxxxxx?xxx?xxx"}; "xxxxxx?xxx?xxx"};
const char* update_function[2] {
"\x48\x89\x4c\x24\x00\x48\x83\xec\x00\xba\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x48\x8b\x48",
"xxxx?xxx?x????x????xxx"
};
} }
+22 -6
View File
@@ -10,10 +10,19 @@
std::unique_ptr<atomic_queue<std::string, 1000>> RCVQueue, SendQueue; std::unique_ptr<atomic_queue<std::string, 1000>> RCVQueue, SendQueue;
int BeamNG::lua_open_jit_D(lua_State* State) { int BeamNG::lua_open_jit_D(lua_State* State) {
Memory::Print("Got lua State"); Memory::Print("Got lua State -> " + Memory::GetHex(reinterpret_cast<uint64_t>(State)));
GELua::State = State; GELua::State = State;
RegisterGEFunctions(); RegisterGEFunctions();
return OpenJITDetour->Original(State); return OpenJITDetour->Original(State);
}
uint64_t BeamNG::update_D(lua_State* State) {
if(GELua::State != State) {
Memory::Print("Got lua State -> " + Memory::GetHex(reinterpret_cast<uint64_t>(State)));
GELua::State = State;
RegisterGEFunctions();
}
return UpdateDetour->Original(State);
} }
void BeamNG::EntryPoint() { void BeamNG::EntryPoint() {
@@ -27,6 +36,11 @@ void BeamNG::EntryPoint() {
GELua::FindAddresses(); GELua::FindAddresses();
/*GameBaseAddr = Memory::GetModuleBase(GameModule); /*GameBaseAddr = Memory::GetModuleBase(GameModule);
DllBaseAddr = Memory::GetModuleBase(DllModule);*/ DllBaseAddr = Memory::GetModuleBase(DllModule);*/
UpdateDetour = std::make_unique<Hook<def::update_function>>(
GELua::update_function, update_D);
UpdateDetour->Enable();
OpenJITDetour = std::make_unique<Hook<def::lua_open_jit>>( OpenJITDetour = std::make_unique<Hook<def::lua_open_jit>>(
GELua::lua_open_jit, lua_open_jit_D); GELua::lua_open_jit, lua_open_jit_D);
OpenJITDetour->Enable(); OpenJITDetour->Enable();
@@ -94,7 +108,7 @@ void BeamNG::IPCListener() {
IPCFromLauncher->confirm_receive(); IPCFromLauncher->confirm_receive();
} else TimeOuts++; } else TimeOuts++;
} }
Memory::Print("IPC Listener System shutting down"); Memory::Print("IPC Listener System shutting down (timeout)");
} }
uint32_t BeamNG::IPCSender(void* LP) { uint32_t BeamNG::IPCSender(void* LP) {
@@ -105,8 +119,10 @@ uint32_t BeamNG::IPCSender(void* LP) {
IPCToLauncher->send(result); IPCToLauncher->send(result);
if (!IPCToLauncher->send_timed_out()) TimeOuts = 0; if (!IPCToLauncher->send_timed_out()) TimeOuts = 0;
else TimeOuts++; else TimeOuts++;
} else {
Sleep(1); //TODO look into possibly have it wake up on a new message instead
} }
} }
Memory::Print("IPC Sender System shutting down"); Memory::Print("IPC Sender System shutting down (timeout)");
return 0; return 0;
} }
+17 -18
View File
@@ -10,61 +10,60 @@
const char* GameModule = "BeamNG.drive.x64.exe"; const char* GameModule = "BeamNG.drive.x64.exe";
const char* DllModule = "libbeamng.x64.dll"; const char* DllModule = "libbeamng.x64.dll";
std::string GetHex(uint64_t num) {
char buffer[30];
sprintf(buffer, "%llx", num);
return std::string{buffer};
}
void GELua::FindAddresses() { void GELua::FindAddresses() {
GELua::State = nullptr; GELua::State = nullptr;
auto Base = Memory::GetModuleBase(GameModule); auto Base = Memory::GetModuleBase(GameModule);
GetTickCount = reinterpret_cast<def::GetTickCount>( GetTickCount = reinterpret_cast<def::GetTickCount>(
Memory::FindPattern(GameModule, Patterns::GetTickCount)); Memory::FindPattern(GameModule, Patterns::GetTickCount));
Memory::Print("GetTickCount -> " + Memory::Print("GetTickCount -> " +
GetHex(reinterpret_cast<uint64_t>(GetTickCount) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(GetTickCount) - Base));
lua_open_jit = reinterpret_cast<def::lua_open_jit>( lua_open_jit = reinterpret_cast<def::lua_open_jit>(
Memory::FindPattern(GameModule, Patterns::open_jit)); Memory::FindPattern(GameModule, Patterns::open_jit));
Memory::Print("lua_open_jit -> " + Memory::Print("lua_open_jit -> " +
GetHex(reinterpret_cast<uint64_t>(lua_open_jit) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_open_jit) - Base));
lua_push_fstring = reinterpret_cast<def::lua_push_fstring>( lua_push_fstring = reinterpret_cast<def::lua_push_fstring>(
Memory::FindPattern(GameModule, Patterns::push_fstring)); Memory::FindPattern(GameModule, Patterns::push_fstring));
Memory::Print("lua_push_fstring -> " + Memory::Print("lua_push_fstring -> " +
GetHex(reinterpret_cast<uint64_t>(lua_push_fstring) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_push_fstring) - Base));
lua_get_field = reinterpret_cast<def::lua_get_field>( lua_get_field = reinterpret_cast<def::lua_get_field>(
Memory::FindPattern(GameModule, Patterns::get_field)); Memory::FindPattern(GameModule, Patterns::get_field));
Memory::Print("lua_get_field -> " + Memory::Print("lua_get_field -> " +
GetHex(reinterpret_cast<uint64_t>(lua_get_field) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_get_field) - Base));
lua_p_call = reinterpret_cast<def::lua_p_call>( lua_p_call = reinterpret_cast<def::lua_p_call>(
Memory::FindPattern(GameModule, Patterns::p_call)); Memory::FindPattern(GameModule, Patterns::p_call));
Memory::Print("lua_p_call -> " + Memory::Print("lua_p_call -> " +
GetHex(reinterpret_cast<uint64_t>(lua_p_call) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_p_call) - Base));
lua_createtable = reinterpret_cast<def::lua_createtable>( lua_createtable = reinterpret_cast<def::lua_createtable>(
Memory::FindPattern(GameModule, Patterns::lua_createtable)); Memory::FindPattern(GameModule, Patterns::lua_createtable));
Memory::Print("lua_createtable -> " + Memory::Print("lua_createtable -> " +
GetHex(reinterpret_cast<uint64_t>(lua_createtable) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_createtable) - Base));
lua_pushcclosure = reinterpret_cast<def::lua_pushcclosure>( lua_pushcclosure = reinterpret_cast<def::lua_pushcclosure>(
Memory::FindPattern(GameModule, Patterns::lua_pushcclosure)); Memory::FindPattern(GameModule, Patterns::lua_pushcclosure));
Memory::Print("lua_pushcclosure -> " + Memory::Print("lua_pushcclosure -> " +
GetHex(reinterpret_cast<uint64_t>(lua_pushcclosure) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_pushcclosure) - Base));
lua_setfield = reinterpret_cast<def::lua_setfield>( lua_setfield = reinterpret_cast<def::lua_setfield>(
Memory::FindPattern(GameModule, Patterns::lua_setfield)); Memory::FindPattern(GameModule, Patterns::lua_setfield));
Memory::Print("lua_setfield -> " + Memory::Print("lua_setfield -> " +
GetHex(reinterpret_cast<uint64_t>(lua_setfield) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_setfield) - Base));
lua_settable = reinterpret_cast<def::lua_settable>( lua_settable = reinterpret_cast<def::lua_settable>(
Memory::FindPattern(GameModule, Patterns::lua_settable)); Memory::FindPattern(GameModule, Patterns::lua_settable));
Memory::Print("lua_settable -> " + Memory::Print("lua_settable -> " +
GetHex(reinterpret_cast<uint64_t>(lua_settable) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_settable) - Base));
lua_tolstring = reinterpret_cast<def::lua_tolstring>( lua_tolstring = reinterpret_cast<def::lua_tolstring>(
Memory::FindPattern(GameModule, Patterns::lua_tolstring)); Memory::FindPattern(GameModule, Patterns::lua_tolstring));
Memory::Print("lua_tolstring -> " + Memory::Print("lua_tolstring -> " +
GetHex(reinterpret_cast<uint64_t>(lua_tolstring) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_tolstring) - Base));
GEUpdate = reinterpret_cast<def::GEUpdate>( GEUpdate = reinterpret_cast<def::GEUpdate>(
Memory::FindPattern(GameModule, Patterns::GEUpdate)); Memory::FindPattern(GameModule, Patterns::GEUpdate));
Memory::Print("GEUpdate -> " + Memory::Print("GEUpdate -> " +
GetHex(reinterpret_cast<uint64_t>(GEUpdate) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(GEUpdate) - Base));
lua_settop = reinterpret_cast<def::lua_settop>( lua_settop = reinterpret_cast<def::lua_settop>(
Memory::FindPattern(GameModule, Patterns::lua_settop)); Memory::FindPattern(GameModule, Patterns::lua_settop));
Memory::Print("lua_settop -> " + Memory::Print("lua_settop -> " +
GetHex(reinterpret_cast<uint64_t>(lua_settop) - Base)); Memory::GetHex(reinterpret_cast<uint64_t>(lua_settop) - Base));
update_function = reinterpret_cast<def::update_function>(
Memory::FindPattern(GameModule, Patterns::update_function));
Memory::Print("testupdatefunction -> " +
Memory::GetHex(reinterpret_cast<uint64_t>(update_function) - Base));
} }
+6
View File
@@ -83,6 +83,12 @@ uint64_t Memory::FindPattern(const char* module, const char* Pattern[]) {
return 0; return 0;
} }
std::string Memory::GetHex(uint64_t num) {
char buffer[30];
sprintf(buffer, "%llx", num);
return std::string{buffer};
}
void* operator new(size_t size) { void* operator new(size_t size) {
return GlobalAlloc(GPTR, size); return GlobalAlloc(GPTR, size);
} }