Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Freeeaky committed Apr 21, 2015
0 parents commit ad8fb8f
Show file tree
Hide file tree
Showing 27 changed files with 3,175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
x64*
GTALua.vcxproj*
35 changes: 35 additions & 0 deletions src/DLLMain.cpp
@@ -0,0 +1,35 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "GTALua.h"

// =================================================================================
// Global
// =================================================================================
GTALua* g_pGTALua = NULL;

// =================================================================================
// Init
// =================================================================================
void Init()
{
// Init
g_pGTALua = new GTALua();
}

// =================================================================================
// DLLMain
// =================================================================================
BOOL __stdcall DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
// Thread
if (dwReason == DLL_PROCESS_ATTACH)
{
// Go
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Init, 0, 0, 0);
}

// Success
return TRUE;
}
17 changes: 17 additions & 0 deletions src/GTALua.h
@@ -0,0 +1,17 @@
// =================================================================================
// GTALua
// =================================================================================
class GTALua
{
public:
GTALua();

// Game Version
bool VersionCheck();
char* GetGameVersion();
};

// =================================================================================
// Global Instance
// =================================================================================
extern GTALua* g_pGTALua;
13 changes: 13 additions & 0 deletions src/GameHooks.cpp
@@ -0,0 +1,13 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "Memory/Memory.h"

// =================================================================================
// Hooks
// =================================================================================
void GameMemory::InstallHooks()
{

}
13 changes: 13 additions & 0 deletions src/GamePatches.cpp
@@ -0,0 +1,13 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "Memory/Memory.h"

// =================================================================================
// Patches
// =================================================================================
void GameMemory::InstallPatches()
{

}
19 changes: 19 additions & 0 deletions src/Includes.h
@@ -0,0 +1,19 @@
// =================================================================================
// Remove Warnings
// =================================================================================
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable: 4244)

// =================================================================================
// Windows Includes
// =================================================================================
#include <Windows.h>
#include <stdio.h>
#include <vector>

using namespace std;

// =================================================================================
// Libraries
// =================================================================================
#pragma comment(lib, "version.lib") // GameMemory Version Fetch
20 changes: 20 additions & 0 deletions src/Main.cpp
@@ -0,0 +1,20 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "GTALua.h"
#include "Memory/Memory.h"

// =================================================================================
// Init
// =================================================================================
GTALua::GTALua()
{
AllocConsole();
AttachConsole(GetCurrentProcessId());
freopen("CON", "w", stdout);
printf("Console allocated and attached!\n");

// Prepare Game Memory
GameMemory::Init();
}
132 changes: 132 additions & 0 deletions src/Memory/GameMemory.cpp
@@ -0,0 +1,132 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "Memory.h"

// =================================================================================
// Init
// =================================================================================
void GameMemory::Init()
{
// Module
GameModule = GetModuleHandle("GTA5.exe");
if (GameModule == NULL)
{
printf("GameMemory::Init GTA5.exe module not found!");
return;
}

// Base
Base = (DWORD64) GameModule;
printf("[GameMemory] Game Base: %p\n", Base);

// Size
Size = Memory::GetModuleSize(GameModule);
printf("[GameMemory] Game Size: %p\n", Size);

// Version
Version = NULL;
FetchVersion();

// Patches
InstallPatches();
}

// =================================================================================
// Wrapper
// =================================================================================
DWORD64 GameMemory::At(DWORD64 dwOffset)
{
return Base + dwOffset;
}
DWORD64 GameMemory::Find(BYTE* bMask, char* szMask)
{
return Memory::Find(Base, Size, bMask, szMask);
}

// =================================================================================
// Version
// =================================================================================
void GameMemory::FetchVersion()
{
// One-Time-Only
if (Version != NULL) return;

// Game EXE
char* sVersionFile = Memory::GetModulePath((HMODULE) Base);
if (sVersionFile == NULL)
{
printf("GameMemory::FetchVersion failed [Memory::GetModulePath returned 0]\n");
return;
}

// Version Info Size
DWORD dwVersionInfoSize = GetFileVersionInfoSize(sVersionFile, NULL);
if (dwVersionInfoSize == NULL)
{
printf("GameMemory::FetchVersion failed! [GetFileVersionInfoSize returned 0]\n");

// Cleanup
free(sVersionFile);

return;
}

// Version Info
VS_FIXEDFILEINFO* pFileInfo = (VS_FIXEDFILEINFO*) new BYTE[dwVersionInfoSize];
DWORD dwVersionHandle = NULL;
if (!GetFileVersionInfo(sVersionFile, dwVersionHandle, dwVersionInfoSize, pFileInfo))
{
printf("GameMemory::FetchVersion failed! [GetFileVersionInfo failed]\n");

// Cleanup
free(sVersionFile);
delete[] pFileInfo;

return;
}

// Query
UINT uiFileInfoLength = 0;
VS_FIXEDFILEINFO* pVersionInfo = NULL;
if (!VerQueryValue(pFileInfo, "\\", (LPVOID*) &pVersionInfo, &uiFileInfoLength) || uiFileInfoLength == 0)
{
printf("GameMemory::FetchVersion failed! [VerQueryValue failed]\n");

// Cleanup
free(sVersionFile);
delete[] pFileInfo;
if (pVersionInfo != NULL)
delete pVersionInfo;

return;
}

// Signature
if (pVersionInfo->dwSignature != 0xFEEF04BD)
{
printf("GameMemory::FetchVersion failed! [Signature mismatch, got %X]\n", pVersionInfo->dwSignature);

// Cleanup
free(sVersionFile);
delete[] pFileInfo;
if (pVersionInfo != NULL)
delete pVersionInfo;

return;
}

// Build Version String
Version = new char[128];
sprintf(Version, "%d.%d.%d.%d",
(pVersionInfo->dwFileVersionMS >> 16) & 0xffff,
(pVersionInfo->dwFileVersionMS >> 0) & 0xffff,
(pVersionInfo->dwFileVersionLS >> 16) & 0xffff,
(pVersionInfo->dwFileVersionLS >> 0) & 0xffff);
printf("[GameMemory] Detected Version %s\n", Version);

// Cleanup
free(sVersionFile);
delete[] pFileInfo;
}
25 changes: 25 additions & 0 deletions src/Memory/GameMemory.h
@@ -0,0 +1,25 @@
// =================================================================================
// Game Memory
// =================================================================================
namespace GameMemory
{
// Init
void Init();
void FetchVersion();
void InstallPatches();
void InstallHooks();

// Module
static HMODULE GameModule;
static DWORD64 Base;
static DWORD64 Size;

// Version
static char* Version;

// Function Wrappers
DWORD64 Find(BYTE* bMask, char* szMask);

// Calculate
DWORD64 At(DWORD64 dwOffset);
}
65 changes: 65 additions & 0 deletions src/Memory/Hooking.cpp
@@ -0,0 +1,65 @@
// =================================================================================
// Includes
// =================================================================================
#include "Includes.h"
#include "Memory.h"
#include "minhook/include/MinHook.h"

// =================================================================================
// Init
// =================================================================================
void Memory::Init()
{
// Init MinHook
if (MH_Initialize() != MH_OK)
{
printf("[Memory::Init] MH_Initialize failed!\n");
return;
}
}

// =================================================================================
// Hook Function
// =================================================================================
bool Memory::HookFunction(DWORD64 pAddress, void* pDetour, void** pOriginal)
{
// Create Hook
int iResult = MH_CreateHook((void*)pAddress, pDetour, pOriginal);
if (iResult != MH_OK)
{
printf("[Memory::HookFunction] MH_CreateHook failed: %p [Error %i]\n", pAddress, iResult);
return false;
}

// Enable Hook
iResult = MH_EnableHook((void*)pAddress);
if (iResult != MH_OK)
{
printf("[Memory::HookFunction] MH_EnableHook failed: %p [Error %i]\n", pAddress, iResult);
return false;
}

// Success
return true;
}
bool Memory::HookLibraryFunction(char* sLibrary, char* sName, void* pDetour, void** pOriginal)
{
// Module
HMODULE hModule = GetModuleHandle(sLibrary);
if (hModule == NULL)
{
printf("[Memory::HookLibraryFunction] Module %s not (yet) loaded!\n", sLibrary);
return false;
}

// Proc
void* pProc = GetProcAddress(hModule, sName);
if (pProc == NULL)
{
printf("[Memory::HookLibraryFunction] Module %s has no exported function %s!\n", sLibrary, sName);
return false;
}

// Hook
return HookFunction((DWORD64)pProc, pDetour, pOriginal);
}

0 comments on commit ad8fb8f

Please sign in to comment.