Skip to content

Commit

Permalink
Added a lua flag which indicates the DLL is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestionableM committed Jul 14, 2023
1 parent 4a5c7bd commit 771a0ba
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
57 changes: 44 additions & 13 deletions Code/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ void load_sound_config(const std::string& key_repl)

SoundStorage::PreloadSound(v_sound_path, v_sound_name, is_sound_3d);
}

/*
"soundList": {
"CustomSoundName": {
"path": "$CONTENT_DATA/CustomAudio/1_2_Oatmeal.mp3",
"is3D": true
},
"CustomSoundName2": {
"path": "$CONTENT_DATA/CustomAudio/1_2_Oatmeal.mp3",
"is3D": false
}
}
*/
}

bool separate_key(std::string& path)
Expand Down Expand Up @@ -127,11 +114,47 @@ void Hooks::h_InitShapeManager(const char* file_data, unsigned int file_line)
return Hooks::o_InitShapeManager(file_data, file_line);
}


struct lua_State {};

struct LuaVM
{
lua_State* state;
};

using luaL_loadstring_func = int(*)(lua_State* state, const char* s);
using lua_pcall_func = int(*)(lua_State* state, int nargs, int nresults, int errfunc);
static luaL_loadstring_func luaL_loadstring_ptr = nullptr;
static lua_pcall_func lua_pcall_ptr = nullptr;

int __fastcall Hooks::h_LuaInitFunc(LuaVM* lua_vm, void** some_ptr, int some_number)
{
const int v_result = Hooks::o_LuaInitFunc(lua_vm, some_ptr, some_number);
if (!v_result)
{
const int v_inject_result = luaL_loadstring_ptr(lua_vm->state, "unsafe_env.sm.dlm_injected = true\nsm.log.warning(unsafe_env)");
if (!v_inject_result)
return lua_pcall_ptr(lua_vm->state, 0, -1, 0);

return v_inject_result;
}

return v_result;
}

void Hooks::RunHooks()
{
const std::uintptr_t v_module_handle = std::uintptr_t(GetModuleHandle(NULL));
const std::uintptr_t v_load_shapesets_addr = v_module_handle + 0x5C4250;
const std::uintptr_t v_init_shape_manager_addr = v_module_handle + 0x378170;
const std::uintptr_t v_lua_init_addr = v_module_handle + 0x5784D0;

HMODULE v_lua_dll = GetModuleHandleA("lua51.dll");
if (v_lua_dll)
{
luaL_loadstring_ptr = (luaL_loadstring_func)GetProcAddress(v_lua_dll, "luaL_loadstring");
lua_pcall_ptr = (lua_pcall_func)GetProcAddress(v_lua_dll, "lua_pcall");
}

if (MH_CreateHook(
(LPVOID)v_load_shapesets_addr,
Expand All @@ -148,4 +171,12 @@ void Hooks::RunHooks()
{
DebugErrorL("Couldn't hoook the lua manager init function!");
}

if (MH_CreateHook(
(LPVOID)v_lua_init_addr,
(LPVOID)Hooks::h_LuaInitFunc,
(LPVOID*)&Hooks::o_LuaInitFunc) != MH_OK)
{
DebugErrorL("Couldn't hook the lua init function!");
}
}
6 changes: 6 additions & 0 deletions Code/hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

#include <string>

struct LuaVM;

class Hooks
{
public:
using LoadShapesetsSig = void(__fastcall*)(void* shape_manager, const std::string& shape_set, int some_flag);
using InitShapeManagerSig = void(__fastcall*)(const char* file_data, unsigned int file_line);
using LuaInitializeFunc = int(__fastcall*)(LuaVM* lua_vm, void** some_ptr, int some_number);

inline static LoadShapesetsSig o_LoadShapesetsFunction = nullptr;
static void __fastcall h_LoadShapesetsFunction(void* shape_manager, const std::string& shape_set, int some_flag);

inline static InitShapeManagerSig o_InitShapeManager = nullptr;
static void __fastcall h_InitShapeManager(const char* file_data, unsigned int file_line);

inline static LuaInitializeFunc o_LuaInitFunc = nullptr;
static int __fastcall h_LuaInitFunc(LuaVM* lua_vm, void** some_ptr, int some_number);

static void RunHooks();
};
14 changes: 8 additions & 6 deletions Code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ void dll_initialize()
if (MH_EnableHook(MH_ALL_HOOKS) != MH_OK)
return;

while (true)
{
Sleep(100);
}
while (true) { Sleep(100); }

MH_DisableHook(MH_ALL_HOOKS);
MH_Uninitialize();
Expand All @@ -30,7 +27,7 @@ void dll_initialize()

void dll_entry_func(HMODULE module)
{
Sleep(1500);
Sleep(100);
dll_initialize();

FreeLibraryAndExitThread(module, 0);
Expand All @@ -39,9 +36,14 @@ void dll_entry_func(HMODULE module)
void dll_attach()
{
AttachDebugConsole();
DebugOutL("DLM: Attached");
if (MH_Initialize() != MH_OK)
{
DebugErrorL("[DLM] Couldn't initialize MinHook!");
return;
}

FMODHooks::Hook();
Hooks::RunHooks();

MH_EnableHook(MH_ALL_HOOKS);
}
Expand Down
8 changes: 6 additions & 2 deletions DLM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)Dependencies\MinHook\include;$(SolutionDir)Dependencies\FMOD\include;$(SolutionDir)Dependencies\simdjson;$(SolutionDir)Code;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\Build\libs-$(PlatformShortName)-$(Configuration);$(SolutionDir)Dependencies\FMOD\lib\$(PlatformShortName);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libMinHook.lib;fmod_vc.lib;fmodL_vc.lib;fmodstudio_vc.lib;fmodstudioL_vc.lib;simdjson.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>libMinHook.lib;fmod_vc.lib;fmodL_vc.lib;fmodstudio_vc.lib;fmodstudioL_vc.lib;simdjson.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -79,14 +80,17 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)Dependencies\MinHook\include;$(SolutionDir)Dependencies\FMOD\include;$(SolutionDir)Dependencies\simdjson;$(SolutionDir)Code;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<DebugInformationFormat>None</DebugInformationFormat>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\Build\libs-$(PlatformShortName)-$(Configuration);$(SolutionDir)Dependencies\FMOD\lib\$(PlatformShortName);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libMinHook.lib;fmod_vc.lib;fmodL_vc.lib;fmodstudio_vc.lib;fmodstudioL_vc.lib;simdjson.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>libMinHook.lib;fmod_vc.lib;fmodL_vc.lib;fmodstudio_vc.lib;fmodstudioL_vc.lib;simdjson.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions libMinHook/libMinHook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<DebugInformationFormat>None</DebugInformationFormat>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down

0 comments on commit 771a0ba

Please sign in to comment.