Skip to content

Commit

Permalink
Add Lua functions for process affinity / priority (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
RutreD authored Aug 16, 2023
1 parent f76bbfe commit e33f282
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ This are just the patch files for this game. I decided to separate them from pat
- section/StopReclaimWhenPaused.cpp

## Additions
- Adds GetProcessAffinityMask, SetProcessAffinityMask, SetProcessPriority to "init.lua"
- hooks/OnCreateInitLuaState.cpp
- section/OnCreateInitLuaState.cpp
- Adds the commandType, position, targetId and blueprintId to sim unit:GetCommandQueue
- hooks/SimGetCommandQueue.cpp
- section/SimGetCommandQueue.cpp
Expand Down
5 changes: 5 additions & 0 deletions hooks/OnCreateInitLuaState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "../define.h"
asm(
".section h0; .set h0, 0x459EE3;"
"call "QU(OnCreateInitLuaState)";"
);
35 changes: 35 additions & 0 deletions section/OnCreateInitLuaState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "include/moho.h"

void __thiscall OnCreateInitLuaState(LuaState* state, int enumStdLibs) {
reinterpret_cast<void(__thiscall*)(LuaState*, int)>(0x90AC10)(state, enumStdLibs); //original

lua_register(state->m_state, "GetProcessAffinityMask", [](lua_State *L) -> int {
//bool, uint32_t, uint32_t = GetProcessAffinityMask()
auto GetProcessAffinityMask = *reinterpret_cast<int(__stdcall**)(void*, uint32_t*, uint32_t*)>(0x00C0F584);
uint32_t dwProcessAffinityMask;
uint32_t dwSystemAffinityMask;
int res = GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
lua_pushboolean(L, res != 0);
lua_pushnumber(L, dwProcessAffinityMask);
lua_pushnumber(L, dwSystemAffinityMask);
return 3;
});

lua_register(state->m_state, "SetProcessAffinityMask", [](lua_State *L) -> int {
//bool SetProcessAffinityMask(uint32_t dwProcessAffinityMask)
uint32_t dwProcessAffinityMask = luaL_checknumber(L, 1);
auto SetProcessAffinityMask = *reinterpret_cast<int(__stdcall**)(void*, uint32_t)>(0x00C0F444);
int res = SetProcessAffinityMask(GetCurrentProcess(), dwProcessAffinityMask);
lua_pushboolean(L, res != 0);
return 1;
});

lua_register(state->m_state, "SetProcessPriority", [](lua_State *L) -> int {
//bool SetProcessPriority(uint32_t dwPriorityClass)
uint32_t dwPriorityClass = luaL_checknumber(L, 1);
auto SetPriorityClass = reinterpret_cast<uint32_t(__stdcall*)(void*, uint32_t)>(GetProcAddress(GetModuleHandle("kernel32.dll"), "SetPriorityClass"));
int res = SetPriorityClass(GetCurrentProcess(), dwPriorityClass);
lua_pushboolean(L, res != 0);
return 1;
});
}
5 changes: 3 additions & 2 deletions section/include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ FDecl(0xA82F32, FAsprintf_s, int (*)(char *Buffer, size_t BufferCount, const cha
FDecl(0x405550, InitString, __thiscall void (*)(void *this_, const char *str))
FDecl(0x4059E0, AssignString, __thiscall void (*)(void *this_, const char *str, size_t size))

#define GetModuleHandle WDecl(0xC0F378, __stdcall void* (*)(const char *lpLibFileName))
#define GetProcAddress WDecl(0xC0F48C, __stdcall void* (*)(void* hModule, const char *lpProcName))
#define GetModuleHandle WDecl(0xC0F378, __stdcall void* (*)(const char *lpLibFileName))
#define GetProcAddress WDecl(0xC0F48C, __stdcall void* (*)(void* hModule, const char *lpProcName))
#define GetCurrentProcess WDecl(0xC0F58C, __stdcall void* (*)())

#define QueryPerformanceCounter WDecl(0xC0F470, __stdcall bool (*)(int64_t*))
#define QueryPerformanceFrequency WDecl(0xC0F46C, __stdcall bool (*)(int64_t*))
Expand Down

0 comments on commit e33f282

Please sign in to comment.