diff --git a/3rd/Detours/Include/detours.h b/3rd/Detours/Include/detours.h deleted file mode 100644 index fe7acf1..0000000 --- a/3rd/Detours/Include/detours.h +++ /dev/null @@ -1,1059 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// Core Detours Functionality (detours.h of detours.lib) -// -// Microsoft Research Detours Package, Version 4.0.1 -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// - -#pragma once -#ifndef _DETOURS_H_ -#define _DETOURS_H_ - -#define DETOURS_VERSION 0x4c0c1 // 0xMAJORcMINORcPATCH - -////////////////////////////////////////////////////////////////////////////// -// - -#undef DETOURS_X64 -#undef DETOURS_X86 -#undef DETOURS_IA64 -#undef DETOURS_ARM -#undef DETOURS_ARM64 -#undef DETOURS_BITS -#undef DETOURS_32BIT -#undef DETOURS_64BIT - -#if defined(_X86_) -#define DETOURS_X86 -#define DETOURS_OPTION_BITS 64 - -#elif defined(_AMD64_) -#define DETOURS_X64 -#define DETOURS_OPTION_BITS 32 - -#elif defined(_IA64_) -#define DETOURS_IA64 -#define DETOURS_OPTION_BITS 32 - -#elif defined(_ARM_) -#define DETOURS_ARM - -#elif defined(_ARM64_) -#define DETOURS_ARM64 - -#else -#error Unknown architecture (x86, amd64, ia64, arm, arm64) -#endif - -#ifdef _WIN64 -#undef DETOURS_32BIT -#define DETOURS_64BIT 1 -#define DETOURS_BITS 64 -// If all 64bit kernels can run one and only one 32bit architecture. -//#define DETOURS_OPTION_BITS 32 -#else -#define DETOURS_32BIT 1 -#undef DETOURS_64BIT -#define DETOURS_BITS 32 -// If all 64bit kernels can run one and only one 32bit architecture. -//#define DETOURS_OPTION_BITS 32 -#endif - -#define VER_DETOURS_BITS DETOUR_STRINGIFY(DETOURS_BITS) - -////////////////////////////////////////////////////////////////////////////// -// - -#if (_MSC_VER < 1299) -typedef LONG LONG_PTR; -typedef ULONG ULONG_PTR; -#endif - -///////////////////////////////////////////////// SAL 2.0 Annotations w/o SAL. -// -// These definitions are include so that Detours will build even if the -// compiler doesn't have full SAL 2.0 support. -// -#ifndef DETOURS_DONT_REMOVE_SAL_20 - -#ifdef DETOURS_TEST_REMOVE_SAL_20 -#undef _Analysis_assume_ -#undef _Benign_race_begin_ -#undef _Benign_race_end_ -#undef _Field_range_ -#undef _Field_size_ -#undef _In_ -#undef _In_bytecount_ -#undef _In_count_ -#undef _In_opt_ -#undef _In_opt_bytecount_ -#undef _In_opt_count_ -#undef _In_opt_z_ -#undef _In_range_ -#undef _In_reads_ -#undef _In_reads_bytes_ -#undef _In_reads_opt_ -#undef _In_reads_opt_bytes_ -#undef _In_reads_or_z_ -#undef _In_z_ -#undef _Inout_ -#undef _Inout_opt_ -#undef _Inout_z_count_ -#undef _Out_ -#undef _Out_opt_ -#undef _Out_writes_ -#undef _Outptr_result_maybenull_ -#undef _Readable_bytes_ -#undef _Success_ -#undef _Writable_bytes_ -#undef _Pre_notnull_ -#endif - -#if defined(_Deref_out_opt_z_) && !defined(_Outptr_result_maybenull_) -#define _Outptr_result_maybenull_ _Deref_out_opt_z_ -#endif - -#if defined(_In_count_) && !defined(_In_reads_) -#define _In_reads_(x) _In_count_(x) -#endif - -#if defined(_In_opt_count_) && !defined(_In_reads_opt_) -#define _In_reads_opt_(x) _In_opt_count_(x) -#endif - -#if defined(_In_opt_bytecount_) && !defined(_In_reads_opt_bytes_) -#define _In_reads_opt_bytes_(x) _In_opt_bytecount_(x) -#endif - -#if defined(_In_bytecount_) && !defined(_In_reads_bytes_) -#define _In_reads_bytes_(x) _In_bytecount_(x) -#endif - -#ifndef _In_ -#define _In_ -#endif - -#ifndef _In_bytecount_ -#define _In_bytecount_(x) -#endif - -#ifndef _In_count_ -#define _In_count_(x) -#endif - -#ifndef _In_opt_ -#define _In_opt_ -#endif - -#ifndef _In_opt_bytecount_ -#define _In_opt_bytecount_(x) -#endif - -#ifndef _In_opt_count_ -#define _In_opt_count_(x) -#endif - -#ifndef _In_opt_z_ -#define _In_opt_z_ -#endif - -#ifndef _In_range_ -#define _In_range_(x,y) -#endif - -#ifndef _In_reads_ -#define _In_reads_(x) -#endif - -#ifndef _In_reads_bytes_ -#define _In_reads_bytes_(x) -#endif - -#ifndef _In_reads_opt_ -#define _In_reads_opt_(x) -#endif - -#ifndef _In_reads_opt_bytes_ -#define _In_reads_opt_bytes_(x) -#endif - -#ifndef _In_reads_or_z_ -#define _In_reads_or_z_ -#endif - -#ifndef _In_z_ -#define _In_z_ -#endif - -#ifndef _Inout_ -#define _Inout_ -#endif - -#ifndef _Inout_opt_ -#define _Inout_opt_ -#endif - -#ifndef _Inout_z_count_ -#define _Inout_z_count_(x) -#endif - -#ifndef _Out_ -#define _Out_ -#endif - -#ifndef _Out_opt_ -#define _Out_opt_ -#endif - -#ifndef _Out_writes_ -#define _Out_writes_(x) -#endif - -#ifndef _Outptr_result_maybenull_ -#define _Outptr_result_maybenull_ -#endif - -#ifndef _Writable_bytes_ -#define _Writable_bytes_(x) -#endif - -#ifndef _Readable_bytes_ -#define _Readable_bytes_(x) -#endif - -#ifndef _Success_ -#define _Success_(x) -#endif - -#ifndef _Pre_notnull_ -#define _Pre_notnull_ -#endif - -#ifdef DETOURS_INTERNAL - -#pragma warning(disable:4615) // unknown warning type (suppress with older compilers) - -#ifndef _Benign_race_begin_ -#define _Benign_race_begin_ -#endif - -#ifndef _Benign_race_end_ -#define _Benign_race_end_ -#endif - -#ifndef _Field_size_ -#define _Field_size_(x) -#endif - -#ifndef _Field_range_ -#define _Field_range_(x,y) -#endif - -#ifndef _Analysis_assume_ -#define _Analysis_assume_(x) -#endif - -#endif // DETOURS_INTERNAL -#endif // DETOURS_DONT_REMOVE_SAL_20 - -////////////////////////////////////////////////////////////////////////////// -// -#ifndef GUID_DEFINED -#define GUID_DEFINED -typedef struct _GUID -{ - DWORD Data1; - WORD Data2; - WORD Data3; - BYTE Data4[ 8 ]; -} GUID; - -#ifdef INITGUID -#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ - const GUID name \ - = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } -#else -#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ - const GUID name -#endif // INITGUID -#endif // !GUID_DEFINED - -#if defined(__cplusplus) -#ifndef _REFGUID_DEFINED -#define _REFGUID_DEFINED -#define REFGUID const GUID & -#endif // !_REFGUID_DEFINED -#else // !__cplusplus -#ifndef _REFGUID_DEFINED -#define _REFGUID_DEFINED -#define REFGUID const GUID * const -#endif // !_REFGUID_DEFINED -#endif // !__cplusplus - -#ifndef ARRAYSIZE -#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])) -#endif - -// -////////////////////////////////////////////////////////////////////////////// - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -/////////////////////////////////////////////////// Instruction Target Macros. -// -#define DETOUR_INSTRUCTION_TARGET_NONE ((PVOID)0) -#define DETOUR_INSTRUCTION_TARGET_DYNAMIC ((PVOID)(LONG_PTR)-1) -#define DETOUR_SECTION_HEADER_SIGNATURE 0x00727444 // "Dtr\0" - -extern const GUID DETOUR_EXE_RESTORE_GUID; -extern const GUID DETOUR_EXE_HELPER_GUID; - -#define DETOUR_TRAMPOLINE_SIGNATURE 0x21727444 // Dtr! -typedef struct _DETOUR_TRAMPOLINE DETOUR_TRAMPOLINE, *PDETOUR_TRAMPOLINE; - -/////////////////////////////////////////////////////////// Binary Structures. -// -#pragma pack(push, 8) -typedef struct _DETOUR_SECTION_HEADER -{ - DWORD cbHeaderSize; - DWORD nSignature; - DWORD nDataOffset; - DWORD cbDataSize; - - DWORD nOriginalImportVirtualAddress; - DWORD nOriginalImportSize; - DWORD nOriginalBoundImportVirtualAddress; - DWORD nOriginalBoundImportSize; - - DWORD nOriginalIatVirtualAddress; - DWORD nOriginalIatSize; - DWORD nOriginalSizeOfImage; - DWORD cbPrePE; - - DWORD nOriginalClrFlags; - DWORD reserved1; - DWORD reserved2; - DWORD reserved3; - - // Followed by cbPrePE bytes of data. -} DETOUR_SECTION_HEADER, *PDETOUR_SECTION_HEADER; - -typedef struct _DETOUR_SECTION_RECORD -{ - DWORD cbBytes; - DWORD nReserved; - GUID guid; -} DETOUR_SECTION_RECORD, *PDETOUR_SECTION_RECORD; - -typedef struct _DETOUR_CLR_HEADER -{ - // Header versioning - ULONG cb; - USHORT MajorRuntimeVersion; - USHORT MinorRuntimeVersion; - - // Symbol table and startup information - IMAGE_DATA_DIRECTORY MetaData; - ULONG Flags; - - // Followed by the rest of the IMAGE_COR20_HEADER -} DETOUR_CLR_HEADER, *PDETOUR_CLR_HEADER; - -typedef struct _DETOUR_EXE_RESTORE -{ - DWORD cb; - DWORD cbidh; - DWORD cbinh; - DWORD cbclr; - - PBYTE pidh; - PBYTE pinh; - PBYTE pclr; - - IMAGE_DOS_HEADER idh; - union { - IMAGE_NT_HEADERS inh; - IMAGE_NT_HEADERS32 inh32; - IMAGE_NT_HEADERS64 inh64; - BYTE raw[sizeof(IMAGE_NT_HEADERS64) + - sizeof(IMAGE_SECTION_HEADER) * 32]; - }; - DETOUR_CLR_HEADER clr; - -} DETOUR_EXE_RESTORE, *PDETOUR_EXE_RESTORE; - -typedef struct _DETOUR_EXE_HELPER -{ - DWORD cb; - DWORD pid; - DWORD nDlls; - CHAR rDlls[4]; -} DETOUR_EXE_HELPER, *PDETOUR_EXE_HELPER; - -#pragma pack(pop) - -#define DETOUR_SECTION_HEADER_DECLARE(cbSectionSize) \ -{ \ - sizeof(DETOUR_SECTION_HEADER),\ - DETOUR_SECTION_HEADER_SIGNATURE,\ - sizeof(DETOUR_SECTION_HEADER),\ - (cbSectionSize),\ - \ - 0,\ - 0,\ - 0,\ - 0,\ - \ - 0,\ - 0,\ - 0,\ - 0,\ -} - -/////////////////////////////////////////////////////////////// Helper Macros. -// -#define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(x) -#define DETOURS_STRINGIFY_(x) #x - -///////////////////////////////////////////////////////////// Binary Typedefs. -// -typedef BOOL (CALLBACK *PF_DETOUR_BINARY_BYWAY_CALLBACK)( - _In_opt_ PVOID pContext, - _In_opt_ LPCSTR pszFile, - _Outptr_result_maybenull_ LPCSTR *ppszOutFile); - -typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FILE_CALLBACK)( - _In_opt_ PVOID pContext, - _In_ LPCSTR pszOrigFile, - _In_ LPCSTR pszFile, - _Outptr_result_maybenull_ LPCSTR *ppszOutFile); - -typedef BOOL (CALLBACK *PF_DETOUR_BINARY_SYMBOL_CALLBACK)( - _In_opt_ PVOID pContext, - _In_ ULONG nOrigOrdinal, - _In_ ULONG nOrdinal, - _Out_ ULONG *pnOutOrdinal, - _In_opt_ LPCSTR pszOrigSymbol, - _In_opt_ LPCSTR pszSymbol, - _Outptr_result_maybenull_ LPCSTR *ppszOutSymbol); - -typedef BOOL (CALLBACK *PF_DETOUR_BINARY_COMMIT_CALLBACK)( - _In_opt_ PVOID pContext); - -typedef BOOL (CALLBACK *PF_DETOUR_ENUMERATE_EXPORT_CALLBACK)(_In_opt_ PVOID pContext, - _In_ ULONG nOrdinal, - _In_opt_ LPCSTR pszName, - _In_opt_ PVOID pCode); - -typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FILE_CALLBACK)(_In_opt_ PVOID pContext, - _In_opt_ HMODULE hModule, - _In_opt_ LPCSTR pszFile); - -typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FUNC_CALLBACK)(_In_opt_ PVOID pContext, - _In_ DWORD nOrdinal, - _In_opt_ LPCSTR pszFunc, - _In_opt_ PVOID pvFunc); - -// Same as PF_DETOUR_IMPORT_FUNC_CALLBACK but extra indirection on last parameter. -typedef BOOL (CALLBACK *PF_DETOUR_IMPORT_FUNC_CALLBACK_EX)(_In_opt_ PVOID pContext, - _In_ DWORD nOrdinal, - _In_opt_ LPCSTR pszFunc, - _In_opt_ PVOID* ppvFunc); - -typedef VOID * PDETOUR_BINARY; -typedef VOID * PDETOUR_LOADED_BINARY; - -//////////////////////////////////////////////////////////// Transaction APIs. -// -LONG WINAPI DetourTransactionBegin(VOID); -LONG WINAPI DetourTransactionAbort(VOID); -LONG WINAPI DetourTransactionCommit(VOID); -LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer); - -LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread); - -LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer, - _In_ PVOID pDetour); - -LONG WINAPI DetourAttachEx(_Inout_ PVOID *ppPointer, - _In_ PVOID pDetour, - _Out_opt_ PDETOUR_TRAMPOLINE *ppRealTrampoline, - _Out_opt_ PVOID *ppRealTarget, - _Out_opt_ PVOID *ppRealDetour); - -LONG WINAPI DetourDetach(_Inout_ PVOID *ppPointer, - _In_ PVOID pDetour); - -BOOL WINAPI DetourSetIgnoreTooSmall(_In_ BOOL fIgnore); -BOOL WINAPI DetourSetRetainRegions(_In_ BOOL fRetain); -PVOID WINAPI DetourSetSystemRegionLowerBound(_In_ PVOID pSystemRegionLowerBound); -PVOID WINAPI DetourSetSystemRegionUpperBound(_In_ PVOID pSystemRegionUpperBound); - -////////////////////////////////////////////////////////////// Code Functions. -// -PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule, - _In_ LPCSTR pszFunction); -PVOID WINAPI DetourCodeFromPointer(_In_ PVOID pPointer, - _Out_opt_ PVOID *ppGlobals); -PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst, - _Inout_opt_ PVOID *ppDstPool, - _In_ PVOID pSrc, - _Out_opt_ PVOID *ppTarget, - _Out_opt_ LONG *plExtra); -BOOL WINAPI DetourSetCodeModule(_In_ HMODULE hModule, - _In_ BOOL fLimitReferencesToModule); - -///////////////////////////////////////////////////// Loaded Binary Functions. -// -HMODULE WINAPI DetourGetContainingModule(_In_ PVOID pvAddr); -HMODULE WINAPI DetourEnumerateModules(_In_opt_ HMODULE hModuleLast); -PVOID WINAPI DetourGetEntryPoint(_In_opt_ HMODULE hModule); -ULONG WINAPI DetourGetModuleSize(_In_opt_ HMODULE hModule); -BOOL WINAPI DetourEnumerateExports(_In_ HMODULE hModule, - _In_opt_ PVOID pContext, - _In_ PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport); -BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule, - _In_opt_ PVOID pContext, - _In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile, - _In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc); - -BOOL WINAPI DetourEnumerateImportsEx(_In_opt_ HMODULE hModule, - _In_opt_ PVOID pContext, - _In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile, - _In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK_EX pfImportFuncEx); - -_Writable_bytes_(*pcbData) -_Readable_bytes_(*pcbData) -_Success_(return != NULL) -PVOID WINAPI DetourFindPayload(_In_opt_ HMODULE hModule, - _In_ REFGUID rguid, - _Out_ DWORD *pcbData); - -_Writable_bytes_(*pcbData) -_Readable_bytes_(*pcbData) -_Success_(return != NULL) -PVOID WINAPI DetourFindPayloadEx(_In_ REFGUID rguid, - _Out_ DWORD * pcbData); - -DWORD WINAPI DetourGetSizeOfPayloads(_In_opt_ HMODULE hModule); - -///////////////////////////////////////////////// Persistent Binary Functions. -// - -PDETOUR_BINARY WINAPI DetourBinaryOpen(_In_ HANDLE hFile); - -_Writable_bytes_(*pcbData) -_Readable_bytes_(*pcbData) -_Success_(return != NULL) -PVOID WINAPI DetourBinaryEnumeratePayloads(_In_ PDETOUR_BINARY pBinary, - _Out_opt_ GUID *pGuid, - _Out_ DWORD *pcbData, - _Inout_ DWORD *pnIterator); - -_Writable_bytes_(*pcbData) -_Readable_bytes_(*pcbData) -_Success_(return != NULL) -PVOID WINAPI DetourBinaryFindPayload(_In_ PDETOUR_BINARY pBinary, - _In_ REFGUID rguid, - _Out_ DWORD *pcbData); - -PVOID WINAPI DetourBinarySetPayload(_In_ PDETOUR_BINARY pBinary, - _In_ REFGUID rguid, - _In_reads_opt_(cbData) PVOID pData, - _In_ DWORD cbData); -BOOL WINAPI DetourBinaryDeletePayload(_In_ PDETOUR_BINARY pBinary, _In_ REFGUID rguid); -BOOL WINAPI DetourBinaryPurgePayloads(_In_ PDETOUR_BINARY pBinary); -BOOL WINAPI DetourBinaryResetImports(_In_ PDETOUR_BINARY pBinary); -BOOL WINAPI DetourBinaryEditImports(_In_ PDETOUR_BINARY pBinary, - _In_opt_ PVOID pContext, - _In_opt_ PF_DETOUR_BINARY_BYWAY_CALLBACK pfByway, - _In_opt_ PF_DETOUR_BINARY_FILE_CALLBACK pfFile, - _In_opt_ PF_DETOUR_BINARY_SYMBOL_CALLBACK pfSymbol, - _In_opt_ PF_DETOUR_BINARY_COMMIT_CALLBACK pfCommit); -BOOL WINAPI DetourBinaryWrite(_In_ PDETOUR_BINARY pBinary, _In_ HANDLE hFile); -BOOL WINAPI DetourBinaryClose(_In_ PDETOUR_BINARY pBinary); - -/////////////////////////////////////////////////// Create Process & Load Dll. -// -typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEA)( - _In_opt_ LPCSTR lpApplicationName, - _Inout_opt_ LPSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOA lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation); - -typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEW)( - _In_opt_ LPCWSTR lpApplicationName, - _Inout_opt_ LPWSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCWSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOW lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation); - -BOOL WINAPI DetourCreateProcessWithDllA(_In_opt_ LPCSTR lpApplicationName, - _Inout_opt_ LPSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOA lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ LPCSTR lpDllName, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); - -BOOL WINAPI DetourCreateProcessWithDllW(_In_opt_ LPCWSTR lpApplicationName, - _Inout_opt_ LPWSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCWSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOW lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ LPCSTR lpDllName, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); - -#ifdef UNICODE -#define DetourCreateProcessWithDll DetourCreateProcessWithDllW -#define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEW -#else -#define DetourCreateProcessWithDll DetourCreateProcessWithDllA -#define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEA -#endif // !UNICODE - -BOOL WINAPI DetourCreateProcessWithDllExA(_In_opt_ LPCSTR lpApplicationName, - _Inout_opt_ LPSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOA lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ LPCSTR lpDllName, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); - -BOOL WINAPI DetourCreateProcessWithDllExW(_In_opt_ LPCWSTR lpApplicationName, - _Inout_opt_ LPWSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCWSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOW lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ LPCSTR lpDllName, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); - -#ifdef UNICODE -#define DetourCreateProcessWithDllEx DetourCreateProcessWithDllExW -#else -#define DetourCreateProcessWithDllEx DetourCreateProcessWithDllExA -#endif // !UNICODE - -BOOL WINAPI DetourCreateProcessWithDllsA(_In_opt_ LPCSTR lpApplicationName, - _Inout_opt_ LPSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOA lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ DWORD nDlls, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); - -BOOL WINAPI DetourCreateProcessWithDllsW(_In_opt_ LPCWSTR lpApplicationName, - _Inout_opt_ LPWSTR lpCommandLine, - _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, - _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, - _In_ BOOL bInheritHandles, - _In_ DWORD dwCreationFlags, - _In_opt_ LPVOID lpEnvironment, - _In_opt_ LPCWSTR lpCurrentDirectory, - _In_ LPSTARTUPINFOW lpStartupInfo, - _Out_ LPPROCESS_INFORMATION lpProcessInformation, - _In_ DWORD nDlls, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_opt_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); - -#ifdef UNICODE -#define DetourCreateProcessWithDlls DetourCreateProcessWithDllsW -#else -#define DetourCreateProcessWithDlls DetourCreateProcessWithDllsA -#endif // !UNICODE - -BOOL WINAPI DetourProcessViaHelperA(_In_ DWORD dwTargetPid, - _In_ LPCSTR lpDllName, - _In_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); - -BOOL WINAPI DetourProcessViaHelperW(_In_ DWORD dwTargetPid, - _In_ LPCSTR lpDllName, - _In_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); - -#ifdef UNICODE -#define DetourProcessViaHelper DetourProcessViaHelperW -#else -#define DetourProcessViaHelper DetourProcessViaHelperA -#endif // !UNICODE - -BOOL WINAPI DetourProcessViaHelperDllsA(_In_ DWORD dwTargetPid, - _In_ DWORD nDlls, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_ PDETOUR_CREATE_PROCESS_ROUTINEA pfCreateProcessA); - -BOOL WINAPI DetourProcessViaHelperDllsW(_In_ DWORD dwTargetPid, - _In_ DWORD nDlls, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_ PDETOUR_CREATE_PROCESS_ROUTINEW pfCreateProcessW); - -#ifdef UNICODE -#define DetourProcessViaHelperDlls DetourProcessViaHelperDllsW -#else -#define DetourProcessViaHelperDlls DetourProcessViaHelperDllsA -#endif // !UNICODE - -BOOL WINAPI DetourUpdateProcessWithDll(_In_ HANDLE hProcess, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_ DWORD nDlls); - -BOOL WINAPI DetourUpdateProcessWithDllEx(_In_ HANDLE hProcess, - _In_ HMODULE hImage, - _In_ BOOL bIs32Bit, - _In_reads_(nDlls) LPCSTR *rlpDlls, - _In_ DWORD nDlls); - -BOOL WINAPI DetourCopyPayloadToProcess(_In_ HANDLE hProcess, - _In_ REFGUID rguid, - _In_reads_bytes_(cbData) PVOID pvData, - _In_ DWORD cbData); -BOOL WINAPI DetourRestoreAfterWith(VOID); -BOOL WINAPI DetourRestoreAfterWithEx(_In_reads_bytes_(cbData) PVOID pvData, - _In_ DWORD cbData); -BOOL WINAPI DetourIsHelperProcess(VOID); -VOID CALLBACK DetourFinishHelperProcess(_In_ HWND, - _In_ HINSTANCE, - _In_ LPSTR, - _In_ INT); - -// -////////////////////////////////////////////////////////////////////////////// -#ifdef __cplusplus -} -#endif // __cplusplus - -//////////////////////////////////////////////// Detours Internal Definitions. -// -#ifdef __cplusplus -#ifdef DETOURS_INTERNAL - -#define NOTHROW -// #define NOTHROW (nothrow) - -////////////////////////////////////////////////////////////////////////////// -// -#if (_MSC_VER < 1299) -#include -typedef IMAGEHLP_MODULE IMAGEHLP_MODULE64; -typedef PIMAGEHLP_MODULE PIMAGEHLP_MODULE64; -typedef IMAGEHLP_SYMBOL SYMBOL_INFO; -typedef PIMAGEHLP_SYMBOL PSYMBOL_INFO; - -static inline -LONG InterlockedCompareExchange(_Inout_ LONG *ptr, _In_ LONG nval, _In_ LONG oval) -{ - return (LONG)::InterlockedCompareExchange((PVOID*)ptr, (PVOID)nval, (PVOID)oval); -} -#else -#pragma warning(push) -#pragma warning(disable:4091) // empty typedef -#include -#pragma warning(pop) -#endif - -#ifdef IMAGEAPI // defined by DBGHELP.H -typedef LPAPI_VERSION (NTAPI *PF_ImagehlpApiVersionEx)(_In_ LPAPI_VERSION AppVersion); - -typedef BOOL (NTAPI *PF_SymInitialize)(_In_ HANDLE hProcess, - _In_opt_ LPCSTR UserSearchPath, - _In_ BOOL fInvadeProcess); -typedef DWORD (NTAPI *PF_SymSetOptions)(_In_ DWORD SymOptions); -typedef DWORD (NTAPI *PF_SymGetOptions)(VOID); -typedef DWORD64 (NTAPI *PF_SymLoadModule64)(_In_ HANDLE hProcess, - _In_opt_ HANDLE hFile, - _In_ LPSTR ImageName, - _In_opt_ LPSTR ModuleName, - _In_ DWORD64 BaseOfDll, - _In_opt_ DWORD SizeOfDll); -typedef BOOL (NTAPI *PF_SymGetModuleInfo64)(_In_ HANDLE hProcess, - _In_ DWORD64 qwAddr, - _Out_ PIMAGEHLP_MODULE64 ModuleInfo); -typedef BOOL (NTAPI *PF_SymFromName)(_In_ HANDLE hProcess, - _In_ LPSTR Name, - _Out_ PSYMBOL_INFO Symbol); - -typedef struct _DETOUR_SYM_INFO -{ - HANDLE hProcess; - HMODULE hDbgHelp; - PF_ImagehlpApiVersionEx pfImagehlpApiVersionEx; - PF_SymInitialize pfSymInitialize; - PF_SymSetOptions pfSymSetOptions; - PF_SymGetOptions pfSymGetOptions; - PF_SymLoadModule64 pfSymLoadModule64; - PF_SymGetModuleInfo64 pfSymGetModuleInfo64; - PF_SymFromName pfSymFromName; -} DETOUR_SYM_INFO, *PDETOUR_SYM_INFO; - -PDETOUR_SYM_INFO DetourLoadImageHlp(VOID); - -#endif // IMAGEAPI - -#if defined(_INC_STDIO) && !defined(_CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS) -#error detours.h must be included before stdio.h (or at least define _CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS earlier) -#endif -#define _CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS 1 - -#ifndef DETOUR_TRACE -#if DETOUR_DEBUG -#define DETOUR_TRACE(x) printf x -#define DETOUR_BREAK() __debugbreak() -#include -#include -#else -#define DETOUR_TRACE(x) -#define DETOUR_BREAK() -#endif -#endif - -#if 1 || defined(DETOURS_IA64) - -// -// IA64 instructions are 41 bits, 3 per bundle, plus 5 bit bundle template => 128 bits per bundle. -// - -#define DETOUR_IA64_INSTRUCTIONS_PER_BUNDLE (3) - -#define DETOUR_IA64_TEMPLATE_OFFSET (0) -#define DETOUR_IA64_TEMPLATE_SIZE (5) - -#define DETOUR_IA64_INSTRUCTION_SIZE (41) -#define DETOUR_IA64_INSTRUCTION0_OFFSET (DETOUR_IA64_TEMPLATE_SIZE) -#define DETOUR_IA64_INSTRUCTION1_OFFSET (DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTION_SIZE) -#define DETOUR_IA64_INSTRUCTION2_OFFSET (DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTION_SIZE + DETOUR_IA64_INSTRUCTION_SIZE) - -C_ASSERT(DETOUR_IA64_TEMPLATE_SIZE + DETOUR_IA64_INSTRUCTIONS_PER_BUNDLE * DETOUR_IA64_INSTRUCTION_SIZE == 128); - -__declspec(align(16)) struct DETOUR_IA64_BUNDLE -{ - public: - union - { - BYTE data[16]; - UINT64 wide[2]; - }; - - enum { - A_UNIT = 1u, - I_UNIT = 2u, - M_UNIT = 3u, - B_UNIT = 4u, - F_UNIT = 5u, - L_UNIT = 6u, - X_UNIT = 7u, - }; - struct DETOUR_IA64_METADATA - { - ULONG nTemplate : 8; // Instruction template. - ULONG nUnit0 : 4; // Unit for slot 0 - ULONG nUnit1 : 4; // Unit for slot 1 - ULONG nUnit2 : 4; // Unit for slot 2 - }; - - protected: - static const DETOUR_IA64_METADATA s_rceCopyTable[33]; - - UINT RelocateBundle(_Inout_ DETOUR_IA64_BUNDLE* pDst, _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const; - - bool RelocateInstruction(_Inout_ DETOUR_IA64_BUNDLE* pDst, - _In_ BYTE slot, - _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const; - - // 120 112 104 96 88 80 72 64 56 48 40 32 24 16 8 0 - // f. e. d. c. b. a. 9. 8. 7. 6. 5. 4. 3. 2. 1. 0. - - // 00 - // f.e. d.c. b.a. 9.8. 7.6. 5.4. 3.2. 1.0. - // 0000 0000 0000 0000 0000 0000 0000 001f : Template [4..0] - // 0000 0000 0000 0000 0000 03ff ffff ffe0 : Zero [ 41.. 5] - // 0000 0000 0000 0000 0000 3c00 0000 0000 : Zero [ 45.. 42] - // 0000 0000 0007 ffff ffff c000 0000 0000 : One [ 82.. 46] - // 0000 0000 0078 0000 0000 0000 0000 0000 : One [ 86.. 83] - // 0fff ffff ff80 0000 0000 0000 0000 0000 : Two [123.. 87] - // f000 0000 0000 0000 0000 0000 0000 0000 : Two [127..124] - BYTE GetTemplate() const; - // Get 4 bit opcodes. - BYTE GetInst0() const; - BYTE GetInst1() const; - BYTE GetInst2() const; - BYTE GetUnit(BYTE slot) const; - BYTE GetUnit0() const; - BYTE GetUnit1() const; - BYTE GetUnit2() const; - // Get 37 bit data. - UINT64 GetData0() const; - UINT64 GetData1() const; - UINT64 GetData2() const; - - // Get/set the full 41 bit instructions. - UINT64 GetInstruction(BYTE slot) const; - UINT64 GetInstruction0() const; - UINT64 GetInstruction1() const; - UINT64 GetInstruction2() const; - void SetInstruction(BYTE slot, UINT64 instruction); - void SetInstruction0(UINT64 instruction); - void SetInstruction1(UINT64 instruction); - void SetInstruction2(UINT64 instruction); - - // Get/set bitfields. - static UINT64 GetBits(UINT64 Value, UINT64 Offset, UINT64 Count); - static UINT64 SetBits(UINT64 Value, UINT64 Offset, UINT64 Count, UINT64 Field); - - // Get specific read-only fields. - static UINT64 GetOpcode(UINT64 instruction); // 4bit opcode - static UINT64 GetX(UINT64 instruction); // 1bit opcode extension - static UINT64 GetX3(UINT64 instruction); // 3bit opcode extension - static UINT64 GetX6(UINT64 instruction); // 6bit opcode extension - - // Get/set specific fields. - static UINT64 GetImm7a(UINT64 instruction); - static UINT64 SetImm7a(UINT64 instruction, UINT64 imm7a); - static UINT64 GetImm13c(UINT64 instruction); - static UINT64 SetImm13c(UINT64 instruction, UINT64 imm13c); - static UINT64 GetSignBit(UINT64 instruction); - static UINT64 SetSignBit(UINT64 instruction, UINT64 signBit); - static UINT64 GetImm20a(UINT64 instruction); - static UINT64 SetImm20a(UINT64 instruction, UINT64 imm20a); - static UINT64 GetImm20b(UINT64 instruction); - static UINT64 SetImm20b(UINT64 instruction, UINT64 imm20b); - - static UINT64 SignExtend(UINT64 Value, UINT64 Offset); - - BOOL IsMovlGp() const; - - VOID SetInst(BYTE Slot, BYTE nInst); - VOID SetInst0(BYTE nInst); - VOID SetInst1(BYTE nInst); - VOID SetInst2(BYTE nInst); - VOID SetData(BYTE Slot, UINT64 nData); - VOID SetData0(UINT64 nData); - VOID SetData1(UINT64 nData); - VOID SetData2(UINT64 nData); - BOOL SetNop(BYTE Slot); - BOOL SetNop0(); - BOOL SetNop1(); - BOOL SetNop2(); - - public: - BOOL IsBrl() const; - VOID SetBrl(); - VOID SetBrl(UINT64 target); - UINT64 GetBrlTarget() const; - VOID SetBrlTarget(UINT64 target); - VOID SetBrlImm(UINT64 imm); - UINT64 GetBrlImm() const; - - UINT64 GetMovlGp() const; - VOID SetMovlGp(UINT64 gp); - - VOID SetStop(); - - UINT Copy(_Out_ DETOUR_IA64_BUNDLE *pDst, _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra = NULL) const; -}; -#endif // DETOURS_IA64 - -#ifdef DETOURS_ARM - -#define DETOURS_PFUNC_TO_PBYTE(p) ((PBYTE)(((ULONG_PTR)(p)) & ~(ULONG_PTR)1)) -#define DETOURS_PBYTE_TO_PFUNC(p) ((PBYTE)(((ULONG_PTR)(p)) | (ULONG_PTR)1)) - -#endif // DETOURS_ARM - -////////////////////////////////////////////////////////////////////////////// - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -#define DETOUR_OFFLINE_LIBRARY(x) \ -PVOID WINAPI DetourCopyInstruction##x(_In_opt_ PVOID pDst, \ - _Inout_opt_ PVOID *ppDstPool, \ - _In_ PVOID pSrc, \ - _Out_opt_ PVOID *ppTarget, \ - _Out_opt_ LONG *plExtra); \ - \ -BOOL WINAPI DetourSetCodeModule##x(_In_ HMODULE hModule, \ - _In_ BOOL fLimitReferencesToModule); \ - -DETOUR_OFFLINE_LIBRARY(X86) -DETOUR_OFFLINE_LIBRARY(X64) -DETOUR_OFFLINE_LIBRARY(ARM) -DETOUR_OFFLINE_LIBRARY(ARM64) -DETOUR_OFFLINE_LIBRARY(IA64) - -#undef DETOUR_OFFLINE_LIBRARY - -////////////////////////////////////////////////////////////////////////////// -// -// Helpers for manipulating page protection. -// - -_Success_(return != FALSE) -BOOL WINAPI DetourVirtualProtectSameExecuteEx(_In_ HANDLE hProcess, - _In_ PVOID pAddress, - _In_ SIZE_T nSize, - _In_ DWORD dwNewProtect, - _Out_ PDWORD pdwOldProtect); - -_Success_(return != FALSE) -BOOL WINAPI DetourVirtualProtectSameExecute(_In_ PVOID pAddress, - _In_ SIZE_T nSize, - _In_ DWORD dwNewProtect, - _Out_ PDWORD pdwOldProtect); -#ifdef __cplusplus -} -#endif // __cplusplus - -////////////////////////////////////////////////////////////////////////////// - -#define MM_ALLOCATION_GRANULARITY 0x10000 - -////////////////////////////////////////////////////////////////////////////// - -#endif // DETOURS_INTERNAL -#endif // __cplusplus - -#endif // _DETOURS_H_ -// -//////////////////////////////////////////////////////////////// End of File. diff --git a/3rd/Detours/Include/detver.h b/3rd/Detours/Include/detver.h deleted file mode 100644 index f0aae9b..0000000 --- a/3rd/Detours/Include/detver.h +++ /dev/null @@ -1,27 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -// Common version parameters. -// -// Microsoft Research Detours Package, Version 4.0.1 -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// - -#define _USING_V110_SDK71_ 1 -#include "winver.h" -#if 0 -#include -#include -#else -#ifndef DETOURS_STRINGIFY -#define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(x) -#define DETOURS_STRINGIFY_(x) #x -#endif - -#define VER_FILEFLAGSMASK 0x3fL -#define VER_FILEFLAGS 0x0L -#define VER_FILEOS 0x00040004L -#define VER_FILETYPE 0x00000002L -#define VER_FILESUBTYPE 0x00000000L -#endif -#define VER_DETOURS_BITS DETOUR_STRINGIFY(DETOURS_BITS) diff --git a/3rd/Detours/Include/syelog.h b/3rd/Detours/Include/syelog.h deleted file mode 100644 index 7cfa9f3..0000000 --- a/3rd/Detours/Include/syelog.h +++ /dev/null @@ -1,89 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -// Detours Test Program (syelog.h of syelog.lib) -// -// Microsoft Research Detours Package -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -#pragma once -#ifndef _SYELOGD_H_ -#define _SYELOGD_H_ -#include - -#pragma pack(push, 1) -#pragma warning(push) -#pragma warning(disable: 4200) - -////////////////////////////////////////////////////////////////////////////// -// -// -#define SYELOG_PIPE_NAMEA "\\\\.\\pipe\\syelog" -#define SYELOG_PIPE_NAMEW L"\\\\.\\pipe\\syelog" -#ifdef UNICODE -#define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEW -#else -#define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEA -#endif - -////////////////////////////////////////////////////////////////////////////// -// -#define SYELOG_MAXIMUM_MESSAGE 4086 // 4096 - sizeof(header stuff) - -typedef struct _SYELOG_MESSAGE -{ - USHORT nBytes; - BYTE nFacility; - BYTE nSeverity; - DWORD nProcessId; - FILETIME ftOccurance; - BOOL fTerminate; - CHAR szMessage[SYELOG_MAXIMUM_MESSAGE]; -} SYELOG_MESSAGE, *PSYELOG_MESSAGE; - - -// Facility Codes. -// -#define SYELOG_FACILITY_KERNEL 0x10 // OS Kernel -#define SYELOG_FACILITY_SECURITY 0x20 // OS Security -#define SYELOG_FACILITY_LOGGING 0x30 // OS Logging-internal -#define SYELOG_FACILITY_SERVICE 0x40 // User-mode system daemon -#define SYELOG_FACILITY_APPLICATION 0x50 // User-mode application -#define SYELOG_FACILITY_USER 0x60 // User self-generated. -#define SYELOG_FACILITY_LOCAL0 0x70 // Locally defined. -#define SYELOG_FACILITY_LOCAL1 0x71 // Locally defined. -#define SYELOG_FACILITY_LOCAL2 0x72 // Locally defined. -#define SYELOG_FACILITY_LOCAL3 0x73 // Locally defined. -#define SYELOG_FACILITY_LOCAL4 0x74 // Locally defined. -#define SYELOG_FACILITY_LOCAL5 0x75 // Locally defined. -#define SYELOG_FACILITY_LOCAL6 0x76 // Locally defined. -#define SYELOG_FACILITY_LOCAL7 0x77 // Locally defined. -#define SYELOG_FACILITY_LOCAL8 0x78 // Locally defined. -#define SYELOG_FACILITY_LOCAL9 0x79 // Locally defined. - -// Severity Codes. -// -#define SYELOG_SEVERITY_FATAL 0x00 // System is dead. -#define SYELOG_SEVERITY_ALERT 0x10 // Take action immediately. -#define SYELOG_SEVERITY_CRITICAL 0x20 // Critical condition. -#define SYELOG_SEVERITY_ERROR 0x30 // Error -#define SYELOG_SEVERITY_WARNING 0x40 // Warning -#define SYELOG_SEVERITY_NOTICE 0x50 // Significant condition. -#define SYELOG_SEVERITY_INFORMATION 0x60 // Informational -#define SYELOG_SEVERITY_AUDIT_FAIL 0x66 // Audit Failed -#define SYELOG_SEVERITY_AUDIT_PASS 0x67 // Audit Succeeeded -#define SYELOG_SEVERITY_DEBUG 0x70 // Debugging - -// Logging Functions. -// -VOID SyelogOpen(PCSTR pszIdentifier, BYTE nFacility); -VOID Syelog(BYTE nSeverity, PCSTR pszMsgf, ...); -VOID SyelogV(BYTE nSeverity, PCSTR pszMsgf, va_list args); -VOID SyelogClose(BOOL fTerminate); - -#pragma warning(pop) -#pragma pack(pop) - -#endif // _SYELOGD_H_ -// -///////////////////////////////////////////////////////////////// End of File. diff --git a/3rd/Detours/Lib/detours.lib b/3rd/Detours/Lib/detours.lib deleted file mode 100644 index ce7eaca..0000000 Binary files a/3rd/Detours/Lib/detours.lib and /dev/null differ diff --git a/README.md b/README.md index 91a541f..8bacfd8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# War3 Lua - 1.1.9 (Outland) +# War3 Lua - 1.2.9 (Outland) [![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml) ![lua](https://img.shields.io/badge/lua-v5.4.4-blue) diff --git a/Src/Detours.cpp b/Src/Detours.cpp new file mode 100644 index 0000000..63205cf --- /dev/null +++ b/Src/Detours.cpp @@ -0,0 +1,42 @@ +#include "pch.h" +#include "Detours.h" + +namespace Detour { + BOOL Install(UINT_PTR* pointer_ptr, UINT_PTR detour) { + BOOL error = NO_ERROR; + + if ((error = DetourTransactionBegin()) != NO_ERROR) { printf("DetourTransactionBegin failed with error: %d.\n", error); } + if (!error && (error = DetourUpdateThread(GetCurrentThread())) != NO_ERROR) { printf("DetourUpdateThread failed with error: %d.\n", error); } + if (!error && (error = DetourAttach((PVOID*)pointer_ptr, (PVOID)detour)) != NO_ERROR) { printf("DetourAttach failed with error: %d.\n", error); } + if (!error && (error = DetourTransactionCommit()) != NO_ERROR) { printf("DetourTransactionCommit failed with error: %d.\n", error); } + + if (error != NO_ERROR) { DetourTransactionAbort(); } + + return error; + } + + BOOL Uninstall(UINT_PTR* pointer_ptr, UINT_PTR detour) { + BOOL error = NO_ERROR; + + if ((error = DetourTransactionBegin()) != NO_ERROR) { wprintf_s(L"DetourTransactionBegin failed with error: %d.\n", error); } + if (!error && (error = DetourUpdateThread(::GetCurrentThread())) != NO_ERROR) { wprintf_s(L"DetourUpdateThread failed with error: %d.\n", error); } + if (!error && (error = DetourDetach((PVOID*)pointer_ptr, (PVOID)detour)) != NO_ERROR) { wprintf_s(L"DetourAttach failed with error: %d.\n", error); } + if (!error && (error = DetourTransactionCommit()) != NO_ERROR) { wprintf_s(L"DetourTransactionCommit failed with error: %d.\n", error); } + + if (error != NO_ERROR) { DetourTransactionAbort(); } + + return error; + } + + BOOL InstallEx(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour) { + if (pointer_ptr && *pointer_ptr != NULL) { + return flag ? Install(pointer_ptr, detour) : Uninstall(pointer_ptr, detour); + } + + return -1; + } + + BOOL SetState(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour) { + return flag ? Install(pointer_ptr, detour) : Uninstall(pointer_ptr, detour); + } +} \ No newline at end of file diff --git a/Src/Detours.h b/Src/Detours.h new file mode 100644 index 0000000..edf0f04 --- /dev/null +++ b/Src/Detours.h @@ -0,0 +1,8 @@ +#pragma once + +namespace Detour { + BOOL Install(UINT_PTR* pointer_ptr, UINT_PTR detour); + BOOL Uninstall(UINT_PTR* pointer_ptr, UINT_PTR detour); + BOOL InstallEx(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour); + BOOL SetState(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour); +} \ No newline at end of file diff --git a/Src/DllMain.cpp b/Src/DllMain.cpp index b7ef24b..ad5d4d7 100644 --- a/Src/DllMain.cpp +++ b/Src/DllMain.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "JassMachine.h" #include "JassNatives.h" +#include "Offsets.h" #include "Hooks.h" #include "Logger.h" #include "EasterEgg.h" @@ -30,12 +31,11 @@ BOOL APIENTRY DllMain(HMODULE module, UINT reason, LPVOID reserved) { JassMachine::JassOpcodeInitialize(); - Hooks::AttachHooks(); + Hooks::SetHooksState(true); break; case DLL_PROCESS_DETACH: - - Hooks::DetachHooks(); + Hooks::SetHooksState(false); Logger::CloseConsole(); @@ -48,23 +48,17 @@ BOOL APIENTRY DllMain(HMODULE module, UINT reason, LPVOID reserved) { //--------------------------------------------------------------------- bool StartUp() { - DWORD handle; - DWORD size = GetFileVersionInfoSize("game.dll", &handle); - - LPSTR buffer = new char[size]; - GetFileVersionInfo("game.dll", handle, size, buffer); + if (!InitOffsets()) { + MessageBox(NULL, "Unsupported version of game.dll.\nLua will be unloaded.", "Error", MB_ICONHAND | MB_TOPMOST); - VS_FIXEDFILEINFO* verInfo; - size = sizeof(VS_FIXEDFILEINFO); - VerQueryValue(buffer, "\\", (LPVOID*)&verInfo, (UINT*)&size); - delete[] buffer; + return false; + } - if (((verInfo->dwFileVersionMS >> 16) & 0xffff) != 1 || ((verInfo->dwFileVersionMS >> 0) & 0xffff) != 26 || ((verInfo->dwFileVersionLS >> 16) & 0xffff) != 0 || ((verInfo->dwFileVersionLS >> 0) & 0xffff) != 6401) - { + /*if (Warcraft::GetWarcraftVersion() != Warcraft::VERSION::V126a) { MessageBox(NULL, "Unsupported version of game.dll.\nLua will be unloaded.", "Error", MB_ICONHAND | MB_TOPMOST); return false; - } + }*/ PSTR cmdline = GetCommandLine(); size_t i; diff --git a/Src/Hooks.cpp b/Src/Hooks.cpp index c755208..58194a8 100644 --- a/Src/Hooks.cpp +++ b/Src/Hooks.cpp @@ -1,42 +1,68 @@ #include "pch.h" #include "Hooks.h" +#include "Offsets.h" #include "LuaMachine.h" -#define AttachDetour(pointer, detour) (DetourUpdateThread(GetCurrentThread()), DetourAttach(&(PVOID&)pointer, detour)) -#define DetachDetour(pointer, detour) (DetourUpdateThread(GetCurrentThread()), DetourDetach(&(PVOID&)pointer, detour)) - namespace Hooks { - auto SetJassState = (void(__fastcall*)(BOOL jassState))((std::ptrdiff_t)gameBase + 0x2ab0e0); - auto GetWarcraftID = (DWORD(__stdcall*)())((std::ptrdiff_t)gameBase + 0x537ed0); + enum class EventTypes : UINT { + EVENT_CNET_GAME_START = 0x4009007e, + EVENT_CNET_GAME_LEAVE = 0x40090081, + }; - DWORD GetWarcraftIDCustom() - { - return *(DWORD*)GAME_ID; - } + enum class ObserverRegistryTypes : UINT { + GAME_START_LOADING = 0x00402000, + GAME_FINISH_LOADING = 0x00140800 + }; - void __fastcall SetJassStateCustom(BOOL jassState) { - if (jassState == TRUE) { - LuaMachine::StartLua(); - } + typedef struct { + PVOID vtable; // 0x0 + size_t refCount; // 0x4 + UINT* registry; // 0x8 ObserverRegistry + } CObserver; + + typedef struct { + PVOID vtable; // 0x0 + UINT unk_04; // 0x4 + UINT id; // 0x8 + PVOID object; // 0xC + DWORD data; // 0x10 | serves as keyCode for KeyEvent + } CEvent; - return SetJassState(jassState); + BOOL CGameProcessEvent(CObserver* observer, CEvent* cevent) { + return pOffsets[(UINT)Offset::CGameEventHandler] ? this_call(pOffsets[(UINT)Offset::CGameEventHandler], observer, cevent) : NULL; } - void AttachHooks() { - DetourTransactionBegin(); + DWORD GetWarcraftVersionKey() { + return pOffsets[(UINT)Offset::GetWarcraftVersionKey] ? std_call(pOffsets[(UINT)Offset::GetWarcraftVersionKey]) : NULL; + } - AttachDetour(SetJassState, SetJassStateCustom); - AttachDetour(GetWarcraftID, GetWarcraftIDCustom); + //-------------------------------------------- - DetourTransactionCommit(); + DWORD GetLuaVersionKey() { + return *(DWORD*)GAME_ID; } - void DetachHooks() { - DetourTransactionBegin(); + BOOL __fastcall CGameEventHandler(CObserver* observer, PVOID, CEvent* cevent) { + if (observer && cevent) { + switch (cevent->id) { + case (UINT)EventTypes::EVENT_CNET_GAME_START: + if (*observer->registry == (UINT)ObserverRegistryTypes::GAME_FINISH_LOADING) { + LuaMachine::StartLua(); + } + + break; + case (UINT)EventTypes::EVENT_CNET_GAME_LEAVE: + LuaMachine::DestroyLua(); - DetachDetour(SetJassState, SetJassStateCustom); - DetachDetour(GetWarcraftID, GetWarcraftIDCustom); + break; + } + } + + return CGameProcessEvent(observer, cevent); + } - DetourTransactionCommit(); + void SetHooksState(bool flag) { + Detour::SetState(flag,&pOffsets[(UINT)Offset::CGameEventHandler], (UINT_PTR)CGameEventHandler); + Detour::SetState(flag,&pOffsets[(UINT)Offset::GetWarcraftVersionKey], (UINT_PTR)GetLuaVersionKey); } } \ No newline at end of file diff --git a/Src/Hooks.h b/Src/Hooks.h index 6b83c14..e61df79 100644 --- a/Src/Hooks.h +++ b/Src/Hooks.h @@ -1,7 +1,5 @@ #pragma once namespace Hooks { - void AttachHooks(); - - void DetachHooks(); + void SetHooksState(bool flag); } \ No newline at end of file diff --git a/Src/JassMachine.cpp b/Src/JassMachine.cpp index 975c360..69efbe2 100644 --- a/Src/JassMachine.cpp +++ b/Src/JassMachine.cpp @@ -1,25 +1,25 @@ #include "pch.h" #include "JassMachine.h" #include "LuaMachine.h" +#include "Offsets.h" #include "fp_call.h" namespace JassMachine { - PVOID** ppOpcodeList = (PVOID**)((std::ptrdiff_t)gameBase + 0x45ea5a); - BYTE* pOpcodeListSize = (BYTE*)((std::ptrdiff_t)gameBase + 0x45ea4d); - PVOID opcodeDefaultOutput = (PVOID)((std::ptrdiff_t)gameBase + 0x45f79a); - PVOID OPCODE_FUNCTIONS[44]; DWORD OpcodeStartLuaThread() { LuaMachine::StartLuaThread(); - return c_call(JassMachine::opcodeDefaultOutput); + return c_call(pOffsets[(UINT)Offset::OpcodeDefaultOutput]); // jump } void JassOpcodeInitialize() { + PVOID** ppOpcodeList = (PVOID**)(pOffsets[(UINT)Offset::OpcodeList]); + BYTE* pOpcodeListSize = (BYTE*)(pOffsets[(UINT)Offset::OpcodeSize]); + CopyMemory(OPCODE_FUNCTIONS, *ppOpcodeList, sizeof(OPCODE_FUNCTIONS)); - OPCODE_FUNCTIONS[OPTYPE_STARTLUATHREAD - 2] = OpcodeStartLuaThread; // My own opcode function + OPCODE_FUNCTIONS[(UINT)OPCODE_TYPE::STARTLUATHREAD - 2] = OpcodeStartLuaThread; // My own opcode function DWORD dwOldProtect; VirtualProtect(pOpcodeListSize, sizeof(BYTE), PAGE_EXECUTE_READWRITE, &dwOldProtect); @@ -34,7 +34,7 @@ namespace JassMachine { //----------------------------------------------------------- PJASS_THREAD_LOCAL GetJassThreadLocal() { - return (PJASS_THREAD_LOCAL)GetInstance(5); + return (PJASS_THREAD_LOCAL)Warcraft::GetTLSValueByIndex(5); } PJASS_INSTANCE GetJassMachine(UINT index) { diff --git a/Src/JassMachine.h b/Src/JassMachine.h index 6e8fd23..37b32de 100644 --- a/Src/JassMachine.h +++ b/Src/JassMachine.h @@ -3,71 +3,71 @@ #include "Warcraft.h" #include "JassNatives.h" -namespace JassMachine { - enum OPCODES : BYTE { - OPTYPE_ENDPROGRAM = 1, - OPTYPE_OLDJUMP, - OPTYPE_FUNCTION, - OPTYPE_ENDFUNCTION, - OPTYPE_LOCAL, - OPTYPE_GLOBAL, - OPTYPE_CONSTANT, - OPTYPE_FUNCARG, - OPTYPE_EXTENDS, - OPTYPE_TYPE, - OPTYPE_POPN, - OPTYPE_MOVRLITERAL, - OPTYPE_MOVRR, - OPTYPE_MOVRV, - OPTYPE_MOVRCODE, - OPTYPE_MOVRA, - OPTYPE_MOVVR, - OPTYPE_MOVAR, - OPTYPE_PUSH, - OPTYPE_POP, - OPTYPE_CALLNATIVE, - OPTYPE_CALLJASS, - OPTYPE_I2R, - OPTYPE_AND, - OPTYPE_OR, - OPTYPE_EQUAL, - OPTYPE_NOTEQUAL, - OPTYPE_LESSEREQUAL, - OPTYPE_GREATEREQUAL, - OPTYPE_LESSER, - OPTYPE_GREATER, - OPTYPE_ADD, - OPTYPE_SUB, - OPTYPE_MUL, - OPTYPE_DIV, - OPTYPE_MOD, - OPTYPE_NEGATE, - OPTYPE_NOT, - OPTYPE_RETURN, - OPTYPE_LABE, - OPTYPE_JUMPIFTRUE, - OPTYPE_JUMPIFFALSE, - OPTYPE_JUMP, - OPTYPE_STARTLUATHREAD - }; - - enum OPCODE_VARIABLE_TYPE : BYTE { - OPCODE_VARIABLE_NOTHING = 0, - OPCODE_VARIABLE_UNKNOWN, - OPCODE_VARIABLE_NULL, - OPCODE_VARIABLE_CODE, - OPCODE_VARIABLE_INTEGER, - OPCODE_VARIABLE_REAL, - OPCODE_VARIABLE_STRING, - OPCODE_VARIABLE_HANDLE, - OPCODE_VARIABLE_BOOLEAN, - OPCODE_VARIABLE_INTEGER_ARRAY, - OPCODE_VARIABLE_REAL_ARRAY, - OPCODE_VARIABLE_STRING_ARRAY, - OPCODE_VARIABLE_HANDLE_ARRAY, - OPCODE_VARIABLE_BOOLEAN_ARRAY - }; +enum class OPCODE_TYPE : BYTE { + ENDPROGRAM = 1, + OLDJUMP, + FUNCTION, + ENDFUNCTION, + LOCAL, + GLOBAL, + CONSTANT, + FUNCARG, + EXTENDS, + TYPE, + POPN, + MOVRLITERAL, + MOVRR, + MOVRV, + MOVRCODE, + MOVRA, + MOVVR, + MOVAR, + PUSH, + POP, + CALLNATIVE, + CALLJASS, + I2R, + AND, + OR, + EQUAL, + NOTEQUAL, + LESSEREQUAL, + GREATEREQUAL, + LESSER, + GREATER, + ADD, + SUB, + MUL, + DIV, + MOD, + NEGATE, + NOT, + RETURN, + LABE, + JUMPIFTRUE, + JUMPIFFALSE, + JUMP, + STARTLUATHREAD +}; + +enum class OPCODE_VARIABLE : BYTE { + TYPE_NOTHING = 0, + TYPE_UNKNOWN, + TYPE_NULL, + TYPE_CODE, + TYPE_INTEGER, + TYPE_REAL, + TYPE_STRING, + TYPE_HANDLE, + TYPE_BOOLEAN, + TYPE_INTEGER_ARRAY, + TYPE_REAL_ARRAY, + TYPE_STRING_ARRAY, + TYPE_HANDLE_ARRAY, + TYPE_BOOLEAN_ARRAY +}; +namespace JassMachine { typedef struct { DWORD unk; DWORD zero1; @@ -75,15 +75,15 @@ namespace JassMachine { DWORD zero3; DWORD zero4; DWORD zero5; - DWORD type1; - DWORD type2; + DWORD type1; // OPCODE_VARIABLE + DWORD type2; // OPCODE_VARIABLE DWORD value; DWORD zero6; - void Set(DWORD value, OPCODE_VARIABLE_TYPE type) { + void Set(DWORD value, OPCODE_VARIABLE type) { this->value = value; - type1 = type; - type2 = type; + type1 = (DWORD)type; + type2 = (DWORD)type; } } JASS_DATA_SLOT, * PJASS_DATA_SLOT; @@ -182,10 +182,10 @@ namespace JassMachine { } CODE_TABLE, * PCODE_TABLE; typedef struct { - BYTE returntype; - BYTE type; + OPCODE_VARIABLE returntype; + OPCODE_VARIABLE type; BYTE reg; - BYTE opcode; + OPCODE_TYPE opcode; DWORD value; } JASS_OPCODE, * PJASS_OPCODE; @@ -224,7 +224,7 @@ namespace JassMachine { size_t minArgs; // 0x2c uintptr_t unk5; // 0x30 uintptr_t unk6; // 0x34 - OPCODE_VARIABLE_TYPE returnType; // 0x38 + OPCODE_VARIABLE returnType; // 0x38 }; #pragma pack(pop) @@ -251,7 +251,7 @@ namespace JassMachine { private: std::vector oplist; public: - void AddOperation(OPCODES opcode, BYTE reg = 0, DWORD value = NULL, OPCODE_VARIABLE_TYPE type = OPCODE_VARIABLE_NOTHING, OPCODE_VARIABLE_TYPE returntype = OPCODE_VARIABLE_NOTHING) { + void AddOperation(OPCODE_TYPE opcode, BYTE reg = 0, DWORD value = NULL, OPCODE_VARIABLE type = OPCODE_VARIABLE::TYPE_NOTHING, OPCODE_VARIABLE returntype = OPCODE_VARIABLE::TYPE_NOTHING) { JASS_OPCODE* _opcode = new JASS_OPCODE; _opcode->returntype = returntype; _opcode->type = type; diff --git a/Src/JassNatives.cpp b/Src/JassNatives.cpp index 306218e..e81476d 100644 --- a/Src/JassNatives.cpp +++ b/Src/JassNatives.cpp @@ -21,11 +21,11 @@ namespace Jass { JassMachine::JASS_OPLIST& oplist = jassopcodes[key]; BYTE reg = 0xD8; - oplist.AddOperation(JassMachine::OPTYPE_MOVRLITERAL, reg, LuaMachine::PushFunctionRef(l, index), JassMachine::OPCODE_VARIABLE_INTEGER); - oplist.AddOperation(JassMachine::OPTYPE_PUSH, reg); - oplist.AddOperation(JassMachine::OPTYPE_STARTLUATHREAD); - oplist.AddOperation(JassMachine::OPTYPE_MOVRR); - oplist.AddOperation(JassMachine::OPTYPE_RETURN); + oplist.AddOperation(OPCODE_TYPE::MOVRLITERAL, reg, LuaMachine::PushFunctionRef(l, index), OPCODE_VARIABLE::TYPE_INTEGER); + oplist.AddOperation(OPCODE_TYPE::PUSH, reg); + oplist.AddOperation(OPCODE_TYPE::STARTLUATHREAD); + oplist.AddOperation(OPCODE_TYPE::MOVRR); + oplist.AddOperation(OPCODE_TYPE::RETURN); return oplist.GetCode(); } diff --git a/Src/JassNatives.h b/Src/JassNatives.h index ecdae18..1da1a45 100644 --- a/Src/JassNatives.h +++ b/Src/JassNatives.h @@ -1,5 +1,7 @@ #pragma once +#include "Offsets.h" + //enum JASS_TYPE : BYTE { // TYPE_NONE = 0, // TYPE_BOOLEAN = 'B', @@ -21,7 +23,7 @@ namespace Jass { } inline UINT ToString(LPCSTR string) { - return this_call((std::ptrdiff_t)gameBase + 0x3baa20, string); + return this_call(pOffsets[(UINT)Offset::jStringToString], string); //return std_call((std::ptrdiff_t)gameBase + 0x454930, string, 0); /* UINT* pString = new UINT[8]; diff --git a/Src/Logger.cpp b/Src/Logger.cpp index 5a8ea41..0d91ef8 100644 --- a/Src/Logger.cpp +++ b/Src/Logger.cpp @@ -24,9 +24,6 @@ namespace Logger { } void ClearConsole() { - /*fflush(stdout); - fflush(stderr);*/ - HANDLE hStdOut; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD count; @@ -56,7 +53,7 @@ namespace Logger { SetConsoleCursorPosition(hStdOut, homeCoords); } - void Log(LOG_LEVEL level, std::string format, ...) { + void Log(LEVEL level, std::string format, ...) { char buffer[8192] = { 0 }; va_list args; @@ -70,22 +67,22 @@ namespace Logger { switch (level) { - case LOG_INFO: + case LEVEL::LOG_INFO: color |= FOREGROUND_GREEN; text = "Info"; break; - case LOG_ERROR: + case LEVEL::LOG_ERROR: color |= FOREGROUND_RED; text = "Error"; break; - case LOG_DEBUG: + case LEVEL::LOG_DEBUG: color |= FOREGROUND_BLUE; text = "Debug"; break; - case LOG_WARNING: + case LEVEL::LOG_WARNING: color |= FOREGROUND_RED | FOREGROUND_GREEN; text = "Warning"; diff --git a/Src/Logger.h b/Src/Logger.h index d79b5ba..b8a6a36 100644 --- a/Src/Logger.h +++ b/Src/Logger.h @@ -7,7 +7,7 @@ #define DEBUG_INFO "[" __FILE__ ":" LINE_STRING "]" namespace Logger { - enum LOG_LEVEL { + enum class LEVEL : BYTE { LOG_NONE, LOG_INFO, LOG_ERROR, @@ -21,7 +21,7 @@ namespace Logger { void ClearConsole(); - void Log(LOG_LEVEL level, std::string format, ...); + void Log(LEVEL level, std::string format, ...); std::string format(std::string format, ...); } \ No newline at end of file diff --git a/Src/LuaFunctions.cpp b/Src/LuaFunctions.cpp index a5674e9..9e80eb0 100644 --- a/Src/LuaFunctions.cpp +++ b/Src/LuaFunctions.cpp @@ -314,7 +314,7 @@ namespace LuaFunctions { std::string string = Logger::format("%s: %08X", lua_tostring(l, 3), handle); if (developerMode && handle > 0x100000) { - string += Logger::format(" | %08X", ConvertHandle(handle)); + string += Logger::format(" | %08X", Warcraft::ConvertHandle(handle)); } lua_pop(l, 2); @@ -370,8 +370,6 @@ namespace LuaFunctions { } void lua_openJassNatives(lua_State* l) { - Jass::JassNativesReset(); - LuaMachine::HandleMetatablesReset(); Jass::JassNativesParse(); for (const auto& type : LuaMachine::handlemetatypes) { @@ -403,7 +401,7 @@ namespace LuaFunctions { int lua_GetMapFileName(lua_State* l) { if (lua_isboolean(l, 1)) { Storm::Archive map; - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); std::string name = map.GetArchiveName(); if (!lua_toboolean(l, 1)) { diff --git a/Src/LuaHooks.cpp b/Src/LuaHooks.cpp index 57269d0..c640834 100644 --- a/Src/LuaHooks.cpp +++ b/Src/LuaHooks.cpp @@ -2,6 +2,7 @@ #include "LuaHooks.h" #include "EasyStormLib/EasyStormLib.h" #include "Warcraft.h" +#include "Offsets.h" namespace LuaHooks { //--------------------------------------------------------------------------------- @@ -27,7 +28,7 @@ namespace LuaHooks { } if (scriptName == "war3map") { - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); } scriptName += std::string(".lua"); @@ -35,7 +36,7 @@ namespace LuaHooks { std::string mapPath = map.GetArchiveName(scriptName); if (mapPath.empty()) { - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); mapPath = map.GetArchiveName(); map.Close(); } @@ -71,7 +72,7 @@ namespace LuaHooks { Storm::Archive map; std::string mapPath = map.GetArchiveName(scriptName); if (mapPath.empty()) { - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); mapPath = map.GetArchiveName(); map.Close(); } @@ -123,7 +124,7 @@ namespace LuaHooks { Storm::Archive map; std::string mapPath = map.GetArchiveName(scriptName); if (mapPath.empty()) { - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); mapPath = map.GetArchiveName(); map.Close(); } @@ -355,10 +356,10 @@ namespace LuaHooks { LPCSTR s = luaL_tolstring(L, i, &l); if (i > 1) { - PrintChat("\t", 60.f); + Warcraft::PrintChat("\t", 60.f); } - PrintChat(luaL_tolstring(L, -1, NULL), 60.f); + Warcraft::PrintChat(luaL_tolstring(L, -1, NULL), 60.f); lua_pop(L, 1); } diff --git a/Src/LuaMachine.cpp b/Src/LuaMachine.cpp index 2d0aa76..9500191 100644 --- a/Src/LuaMachine.cpp +++ b/Src/LuaMachine.cpp @@ -40,6 +40,8 @@ namespace LuaMachine { lua_close(mainState); mainState = NULL; Jass::JassOpcodesReset(); + Jass::JassNativesReset(); + LuaMachine::HandleMetatablesReset(); } Logger::ClearConsole(); @@ -47,11 +49,9 @@ namespace LuaMachine { void StartLua() { - DestroyLua(); - lua_State* l = GetMainState(); Storm::Archive map; - map.Connect(*pMapMpq); + map.Connect(*(HANDLE*)pOffsets[(UINT)Offset::LastPlayedMap]); if (!map["war3map.lua"].empty()) { lua_pushcfunction(l, stacktrace); lua_getglobal(l, "require"); @@ -83,12 +83,12 @@ namespace LuaMachine { int res; switch (lua_resume(thread, l, 0, &res)) { case LUA_OK: - JassVM->condition_return_value.Set(lua_toboolean(thread, 1), JassMachine::OPCODE_VARIABLE_BOOLEAN); + JassVM->condition_return_value.Set(lua_toboolean(thread, 1), OPCODE_VARIABLE::TYPE_BOOLEAN); break; case LUA_ERRRUN: Error: - PVOID handle = ConvertHandle(Jass::GetNative("GetTriggeringTrigger").Invoke(NULL, NULL) | Jass::GetNative("GetExpiredTimer").Invoke(NULL, NULL)); + PVOID handle = Warcraft::ConvertHandle(Jass::GetNative("GetTriggeringTrigger").Invoke(NULL, NULL) | Jass::GetNative("GetExpiredTimer").Invoke(NULL, NULL)); if (handle) { fast_call((*(UINT*)(*(UINT*)handle + 0x5c)), handle); @@ -156,8 +156,8 @@ namespace LuaMachine { void lua_throwerr(lua_State* l) { std::string error = lua_tostring(l, -1); - Logger::Log(Logger::LOG_LEVEL::LOG_ERROR, error.c_str()); - PrintfChat(100, "\n[|cFFFF0000Error|r] %s\n\n", error.c_str()); + Logger::Log(Logger::LEVEL::LOG_ERROR, error.c_str()); + Warcraft::PrintfChat(100, "\n[|cFFFF0000Error|r] %s\n\n", error.c_str()); //printf("\n%s--------------------Lua Error--------------------%s\n%s\n%s-------------------------------------------------%s\n\n", ANSI_COLOR_RED, ANSI_COLOR_RESET, error, ANSI_COLOR_RED, ANSI_COLOR_RESET); //printfChat(100, "\n|cFFFF0000--------------------Lua Error--------------------|r\n%s\n|cFFFF0000------------------------------------------------------------|r\n\n", error); } diff --git a/Src/LuaMachine.h b/Src/LuaMachine.h index 42330ba..a4fe64e 100644 --- a/Src/LuaMachine.h +++ b/Src/LuaMachine.h @@ -3,6 +3,7 @@ namespace LuaMachine { extern std::map handlemetatypes; + void DestroyLua(); void StartLua(); BOOL __stdcall StartLuaThread(); diff --git a/Src/Offsets.cpp b/Src/Offsets.cpp new file mode 100644 index 0000000..5498654 --- /dev/null +++ b/Src/Offsets.cpp @@ -0,0 +1,88 @@ +#include "pch.h" +#include "Warcraft.h" +#include "Offsets.h" + +UINT_PTR pOffsets[(size_t)Offset::SIZE]; + +bool InitOffsets() { + switch (Warcraft::GetWarcraftVersion()) { + case Warcraft::Version::V124e: + pOffsets[(UINT)Offset::CGameEventHandler] = 0x62a230; + pOffsets[(UINT)Offset::GetWarcraftVersionKey] = 0x5389d0; + pOffsets[(UINT)Offset::OpcodeList] = 0x45f56a; + pOffsets[(UINT)Offset::OpcodeSize] = 0x45f56d; + pOffsets[(UINT)Offset::OpcodeDefaultOutput] = 0x4602aa; + pOffsets[(UINT)Offset::jStringToString] = 0x3bb560; + pOffsets[(UINT)Offset::LastPlayedMap] = 0xac55e0; + pOffsets[(UINT)Offset::GameTLSIndex] = 0xacea4c; + pOffsets[(UINT)Offset::GameObject] = 0xacd44c; + pOffsets[(UINT)Offset::GetGameUI] = 0x301250; + pOffsets[(UINT)Offset::DisplayTimedTextToPlayer] = 0x2f9980; + + break; + case Warcraft::Version::V126a: + pOffsets[(UINT)Offset::CGameEventHandler] = 0x629a90; + pOffsets[(UINT)Offset::GetWarcraftVersionKey] = 0x537ed0; + pOffsets[(UINT)Offset::OpcodeList] = 0x45ea5a; + pOffsets[(UINT)Offset::OpcodeSize] = 0x45ea4d; + pOffsets[(UINT)Offset::OpcodeDefaultOutput] = 0x45f79a; + pOffsets[(UINT)Offset::jStringToString] = 0x3baa20; + pOffsets[(UINT)Offset::LastPlayedMap] = 0xaae788; + pOffsets[(UINT)Offset::GameTLSIndex] = 0xab7bf4; + pOffsets[(UINT)Offset::GameObject] = 0xab65f4; + pOffsets[(UINT)Offset::GetGameUI] = 0x300710; + pOffsets[(UINT)Offset::DisplayTimedTextToPlayer] = 0x2f8e40; + + break; + case Warcraft::Version::V127a: + pOffsets[(UINT)Offset::CGameEventHandler] = 0x0562e0; + pOffsets[(UINT)Offset::GetWarcraftVersionKey] = 0x30d970; + pOffsets[(UINT)Offset::OpcodeList] = 0x7f1aaa; + pOffsets[(UINT)Offset::OpcodeSize] = 0x7f1aad; + pOffsets[(UINT)Offset::OpcodeDefaultOutput] = 0x7f1c3e; + pOffsets[(UINT)Offset::jStringToString] = 0x1da520; + pOffsets[(UINT)Offset::LastPlayedMap] = 0xbee150; + pOffsets[(UINT)Offset::GameTLSIndex] = 0xbb8628; + pOffsets[(UINT)Offset::GameObject] = 0xbe4238; + pOffsets[(UINT)Offset::GetGameUI] = 0x34f3a0; + pOffsets[(UINT)Offset::DisplayTimedTextToPlayer] = 0x357640; + + break; + case Warcraft::Version::V127b: + pOffsets[(UINT)Offset::CGameEventHandler] = 0x071da0; + pOffsets[(UINT)Offset::GetWarcraftVersionKey] = 0x32b0e0; + pOffsets[(UINT)Offset::OpcodeList] = 0x91b4ea; + pOffsets[(UINT)Offset::OpcodeSize] = 0x91b4ed; + pOffsets[(UINT)Offset::OpcodeDefaultOutput] = 0x91c1d5; + pOffsets[(UINT)Offset::jStringToString] = 0x1f7f60; + pOffsets[(UINT)Offset::LastPlayedMap] = 0xd726b8; + pOffsets[(UINT)Offset::GameTLSIndex] = 0xd3cb98; + pOffsets[(UINT)Offset::GameObject] = 0xd687a8; + pOffsets[(UINT)Offset::GetGameUI] = 0x36cb20; + pOffsets[(UINT)Offset::DisplayTimedTextToPlayer] = 0x374de0; + + break; + case Warcraft::Version::V128f: + pOffsets[(UINT)Offset::CGameEventHandler] = 0x09b9d0; + pOffsets[(UINT)Offset::GetWarcraftVersionKey] = 0x35ed70; + pOffsets[(UINT)Offset::OpcodeList] = 0x8d043a; + pOffsets[(UINT)Offset::OpcodeSize] = 0x8d043d; + pOffsets[(UINT)Offset::OpcodeDefaultOutput] = 0x8d1125; + pOffsets[(UINT)Offset::jStringToString] = 0x22a770; + pOffsets[(UINT)Offset::LastPlayedMap] = 0xd3A4f0; + pOffsets[(UINT)Offset::GameTLSIndex] = 0xd047a8; + pOffsets[(UINT)Offset::GameObject] = 0xd305e0; + pOffsets[(UINT)Offset::GetGameUI] = 0x3a0b70; + pOffsets[(UINT)Offset::DisplayTimedTextToPlayer] = 0x3a8eb0; + + break; + default: + return false; + } + + for (auto& offset : pOffsets) { + offset += offset ? (UINT_PTR)gameBase : NULL; + } + + return true; +} \ No newline at end of file diff --git a/Src/Offsets.h b/Src/Offsets.h new file mode 100644 index 0000000..74d620f --- /dev/null +++ b/Src/Offsets.h @@ -0,0 +1,20 @@ +#pragma once + +enum class Offset : UINT { + CGameEventHandler, + GetWarcraftVersionKey, + OpcodeList, + OpcodeSize, + OpcodeDefaultOutput, + jStringToString, + LastPlayedMap, + GameTLSIndex, + GameObject, + GetGameUI, + DisplayTimedTextToPlayer, + SIZE +}; + +extern UINT_PTR pOffsets[(size_t)Offset::SIZE]; + +bool InitOffsets(); \ No newline at end of file diff --git a/Src/Warcraft.cpp b/Src/Warcraft.cpp index 39603f7..0a2a783 100644 --- a/Src/Warcraft.cpp +++ b/Src/Warcraft.cpp @@ -1,31 +1,67 @@ #include "pch.h" #include "Warcraft.h" +#include "Offsets.h" #include "fp_call.h" -auto GetInstanceTrue = (HANDLE(__fastcall*)(UINT))((std::ptrdiff_t)gameBase + 0x4c34d0); -UINT pGameState = (std::ptrdiff_t)gameBase + 0xab65f4; -UINT pGameClass2 = (std::ptrdiff_t)gameBase + 0xab4f80; +namespace Warcraft { + VS_FIXEDFILEINFO* GetFileVersion(LPCSTR filename) { + VS_FIXEDFILEINFO* version = NULL; + DWORD handle; + DWORD size = GetFileVersionInfoSize(filename, &handle); -void PrintChat(LPCSTR text, float duration) { - fast_call((std::ptrdiff_t)gameBase + 0x2f8e40, *(UINT*)pGameClass2, NULL, 0, 0, text, *(UINT*)&duration, 0xFFFFFFFF); - //((void(__fastcall*)(UINT, UINT, UINT, UINT, LPCSTR, UINT, UINT))((UINT)gameBase + 0x2f8e40))(*(UINT*)pGameClass2, NULL, 0, 0, text, *(UINT*)&duration, 0xFFFFFFFF); -} + if (size) { + LPSTR buffer = new char[size]; -void PrintfChat(float duration, LPCSTR format, ...) { - char text[8192] = { NULL }; + if (GetFileVersionInfo(filename, handle, size, buffer) && buffer) { + size = sizeof(VS_FIXEDFILEINFO); + VerQueryValue(buffer, "\\", (LPVOID*)&version, (UINT*)&size); + + } - va_list args; - va_start(args, format); - vsprintf_s(text, format, args); - va_end(args); + delete[] buffer; + } - PrintChat(text, duration); -} + return version; + } -HANDLE GetInstance(UINT index) { - return GetInstanceTrue(index); -} + Version GetWarcraftVersion() { + VS_FIXEDFILEINFO* version = GetFileVersion("game.dll"); -LPVOID ConvertHandle(UINT handle) { - return handle ? *(LPVOID*)(*(UINT*)(*(UINT*)(*(UINT*)pGameState + 0x1c) + 0x19c) + handle * 0xc - 0x2fffff * 4) : NULL; + return version ? Version(version->dwFileVersionLS & 0xFFFF) : Version::NONE; + } + + UINT GetGameUI(BOOL bInit, BOOL bRelease) { + return pOffsets[(UINT)Offset::GetGameUI] ? fast_call(pOffsets[(UINT)Offset::GetGameUI], bInit, bRelease) : NULL; + } + + void PrintChat(LPCSTR text, float duration, DWORD color) { + fast_call(pOffsets[(UINT)Offset::DisplayTimedTextToPlayer], GetGameUI(0, 0), NULL, 0, 0, text, *(UINT*)&duration, color); + } + + void PrintfChat(float duration, LPCSTR format, ...) { + char text[8192] = { NULL }; + + va_list args; + va_start(args, format); + vsprintf_s(text, format, args); + va_end(args); + + PrintChat(text, duration); + } + + HANDLE GetTLSValue() { + return pOffsets[(UINT)Offset::GameTLSIndex] ? TlsGetValue(*(DWORD*)pOffsets[(UINT)Offset::GameTLSIndex]) : NULL; + } + + DWORD GetTLSData(UINT_PTR pOffset) { + return GetTLSValue() ? *(UINT_PTR*)((UINT_PTR)GetTLSValue() + pOffset) : NULL; + } + + DWORD GetTLSValueByIndex(UINT index) { + return GetTLSValue() ? ((DWORD*)GetTLSValue())[index] : NULL; + } + + LPVOID ConvertHandle(UINT handle) { + return handle ? *(LPVOID*)(*(UINT*)(*(UINT*)(*(UINT*)pOffsets[(UINT)Offset::GameObject] + 0x1c) + 0x19c) + handle * 0xc - 0x2fffff * 4) : NULL; + } } \ No newline at end of file diff --git a/Src/Warcraft.h b/Src/Warcraft.h index 6ae3866..4953ab3 100644 --- a/Src/Warcraft.h +++ b/Src/Warcraft.h @@ -1,6 +1,65 @@ #pragma once -void PrintChat(LPCSTR text, float duration); -void PrintfChat(float duration, LPCSTR format, ...); -HANDLE GetInstance(UINT index); -LPVOID ConvertHandle(UINT handle); \ No newline at end of file +namespace Warcraft { + enum class Version : DWORD { + NONE = 0, + V100a = 4448, + V101a = 4482, + V101b = 4483, + V102a = 4531, + V102b = 4563, + V103a = 4653, + V104a = 4709, + V104b = 4709, + V104c = 4905, + V105a = 4944, + V106a = 5551, + V107a = 5535, + V110a = 5610, + V111a = 5616, + V112a = 5636, + V113a = 5816, + V113b = 5818, + V114a = 5840, + V114b = 5846, + V115a = 5917, + V116a = 5926, + V117a = 5988, + V118a = 6030, + V119a = 6041, + V119b = 6046, + V120a = 6048, + V120b = 6056, + V120c = 6065, + V120d = 6070, + V120e = 6074, + V121a = 6263, + V121b = 6300, + V122a = 6328, + V123a = 6352, + V124a = 6372, + V124b = 6374, + V124c = 6378, + V124d = 6384, + V124e = 6387, + V125a = 6396, + V125b = 6397, + V126a = 6401, + V127a = 52240, + V127b = 7085, + V128a = 7205, + V128b = 7365, + V128c = 7395, + V128d = 7554, + V128e = 7608, + V128f = 7680, + }; + + VS_FIXEDFILEINFO* GetFileVersion(LPCSTR filename); + Version GetWarcraftVersion(); + + void PrintChat(LPCSTR text, float duration, DWORD color = 0xFFFFFFFF); + void PrintfChat(float duration, LPCSTR format, ...); + DWORD GetTLSValueByIndex(UINT index); + LPVOID ConvertHandle(UINT handle); +} \ No newline at end of file diff --git a/Src/pch.h b/Src/pch.h index 2f1edaa..da20b58 100644 --- a/Src/pch.h +++ b/Src/pch.h @@ -11,6 +11,7 @@ #include #include #include +#include "Detours.h" #include "fp_call.h" #define ANSI_COLOR_RED "\x1B[31m" @@ -22,7 +23,7 @@ #define ANSI_COLOR_RESET "\x1B[0m" #define WAR3_LUA_MAJOR "1" -#define WAR3_LUA_MINOR "1" +#define WAR3_LUA_MINOR "2" #define WAR3_LUA_RELEASE "9" #define WAR3_LUA_VERSION_NAME ANSI_COLOR_YELLOW "Outland" ANSI_COLOR_RESET @@ -32,7 +33,6 @@ #define GAME_ID "W3L" static HMODULE gameBase = GetModuleHandle("game.dll"); -static HANDLE* pMapMpq = (HANDLE*)((std::ptrdiff_t)gameBase + 0xaae788); extern bool developerMode; diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..31cfc2d --- /dev/null +++ b/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/war3_lua.vcxproj b/war3_lua.vcxproj index 59b7ddf..1401fab 100644 --- a/war3_lua.vcxproj +++ b/war3_lua.vcxproj @@ -384,6 +384,7 @@ NotUsing NotUsing + @@ -399,6 +400,7 @@ + Create Create @@ -436,6 +438,7 @@ + @@ -447,10 +450,21 @@ + + + + + + + + Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}. + + + \ No newline at end of file diff --git a/war3_lua.vcxproj.filters b/war3_lua.vcxproj.filters index 4e41a62..16644b8 100644 --- a/war3_lua.vcxproj.filters +++ b/war3_lua.vcxproj.filters @@ -153,6 +153,12 @@ Исходные файлы + + Исходные файлы + + + Исходные файлы + @@ -278,5 +284,14 @@ Файлы заголовков + + Файлы заголовков + + + Файлы заголовков + + + + \ No newline at end of file