Skip to content
Permalink
Browse files

Merge PR #2354: plugins: add arch-specific mumble_plugin_win32.h vari…

…ants
  • Loading branch information...
mkrautz committed Jun 23, 2016
2 parents dce83ca + 6c2cf49 commit 51af7852474d92a7d0514a0f57903551c3595f97
@@ -6,128 +6,12 @@
#ifndef MUMBLE_MUMBLE_PLUGIN_WIN32_H_
#define MUMBLE_MUMBLE_PLUGIN_WIN32_H_

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#define NOMINMAX
#include <windows.h>
#include <tlhelp32.h>
#include <math.h>
#include <sstream>
#include <iostream>

#include "mumble_plugin.h"

DWORD dwPid;
static HANDLE hProcess;
static BYTE *pModule;

static inline DWORD getProcess(const wchar_t *exename) {
PROCESSENTRY32 pe;
DWORD pid = 0;

pe.dwSize = sizeof(pe);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE) {
BOOL ok = Process32First(hSnap, &pe);

while (ok) {
if (wcscmp(pe.szExeFile, exename)==0) {
pid = pe.th32ProcessID;
break;
}
ok = Process32Next(hSnap, &pe);
}
CloseHandle(hSnap);
}
return pid;
}

static inline BYTE *getModuleAddr(DWORD pid, const wchar_t *modname) {
MODULEENTRY32 me;
BYTE *addr = NULL;
me.dwSize = sizeof(me);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
if (hSnap != INVALID_HANDLE_VALUE) {
BOOL ok = Module32First(hSnap, &me);

while (ok) {
if (wcscmp(me.szModule, modname)==0) {
addr = me.modBaseAddr;
break;
}
ok = Module32Next(hSnap, &me);
}
CloseHandle(hSnap);
}
return addr;
}

static inline BYTE *getModuleAddr(const wchar_t *modname) {
return getModuleAddr(dwPid, modname);
}

static inline bool peekProc(VOID *base, VOID *dest, SIZE_T len) {
SIZE_T r;
BOOL ok=ReadProcessMemory(hProcess, base, dest, len, &r);
return (ok && (r == len));
}

template<class T>
bool peekProc(VOID *base, T &dest) {
SIZE_T r;
BOOL ok=ReadProcessMemory(hProcess, base, reinterpret_cast<VOID *>(& dest), sizeof(T), &r);
return (ok && (r == sizeof(T)));
}

template<class T>
T peekProc(VOID *base) {
T v = 0;
peekProc(base, reinterpret_cast<T *>(&v), sizeof(T));
return v;
}

static bool inline initialize(const std::multimap<std::wstring, unsigned long long int> &pids, const wchar_t *procname, const wchar_t *modname = NULL) {
hProcess = NULL;
pModule = NULL;

if (! pids.empty()) {
std::multimap<std::wstring, unsigned long long int>::const_iterator iter = pids.find(std::wstring(procname));

if (iter != pids.end())
dwPid = static_cast<DWORD>(iter->second);
else
dwPid = 0;
} else {
dwPid=getProcess(procname);
}

if (!dwPid)
return false;

pModule=getModuleAddr(modname ? modname : procname);
if (!pModule) {
dwPid = 0;
return false;
}

hProcess=OpenProcess(PROCESS_VM_READ, false, dwPid);
if (!hProcess) {
dwPid = 0;
pModule = NULL;
return false;
}

return true;
}

static void generic_unlock() {
if (hProcess) {
CloseHandle(hProcess);
hProcess = NULL;
pModule = NULL;
dwPid = 0;
}
}
#ifdef USE_PLUGIN_LEGACY_PTR
# define PTR_TYPE VOID *
# define PTR_TYPE_CONCRETE BYTE *
# include "mumble_plugin_win32_ptr_type.h"
#else
# error The mumble_plugin_win32.h header is no longer supported
#endif

#endif
@@ -0,0 +1,137 @@
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_PLUGIN_WIN32_PTR_TYPE_H_
#define MUMBLE_MUMBLE_PLUGIN_WIN32_PTR_TYPE_H_

#ifndef PTR_TYPE_CONCRETE
# define PTR_TYPE_CONCRETE PTR_TYPE
#endif

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#define NOMINMAX
#include <windows.h>
#include <tlhelp32.h>
#include <math.h>
#include <sstream>
#include <iostream>

#include "mumble_plugin.h"

DWORD dwPid;
static HANDLE hProcess;
static PTR_TYPE_CONCRETE pModule;

static inline DWORD getProcess(const wchar_t *exename) {
PROCESSENTRY32 pe;
DWORD pid = 0;

pe.dwSize = sizeof(pe);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE) {
BOOL ok = Process32First(hSnap, &pe);

while (ok) {
if (wcscmp(pe.szExeFile, exename)==0) {
pid = pe.th32ProcessID;
break;
}
ok = Process32Next(hSnap, &pe);
}
CloseHandle(hSnap);
}
return pid;
}

static inline PTR_TYPE_CONCRETE getModuleAddr(DWORD pid, const wchar_t *modname) {
MODULEENTRY32 me;
PTR_TYPE_CONCRETE addr = NULL;
me.dwSize = sizeof(me);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
if (hSnap != INVALID_HANDLE_VALUE) {
BOOL ok = Module32First(hSnap, &me);

while (ok) {
if (wcscmp(me.szModule, modname)==0) {
addr = reinterpret_cast<PTR_TYPE_CONCRETE>(me.modBaseAddr);
break;
}
ok = Module32Next(hSnap, &me);
}
CloseHandle(hSnap);
}
return addr;
}

static inline PTR_TYPE_CONCRETE getModuleAddr(const wchar_t *modname) {
return getModuleAddr(dwPid, modname);
}

static inline bool peekProc(PTR_TYPE base, VOID *dest, SIZE_T len) {
SIZE_T r;
BOOL ok=ReadProcessMemory(hProcess, reinterpret_cast<VOID *>(base), dest, len, &r);
return (ok && (r == len));
}

template<class T>
bool peekProc(PTR_TYPE base, T &dest) {
SIZE_T r;
BOOL ok=ReadProcessMemory(hProcess, reinterpret_cast<VOID *>(base), reinterpret_cast<VOID *>(& dest), sizeof(T), &r);
return (ok && (r == sizeof(T)));
}

template<class T>
T peekProc(PTR_TYPE base) {
T v = 0;
peekProc(base, reinterpret_cast<T *>(&v), sizeof(T));
return v;
}

static bool inline initialize(const std::multimap<std::wstring, unsigned long long int> &pids, const wchar_t *procname, const wchar_t *modname = NULL) {
hProcess = NULL;
pModule = NULL;

if (! pids.empty()) {
std::multimap<std::wstring, unsigned long long int>::const_iterator iter = pids.find(std::wstring(procname));

if (iter != pids.end())
dwPid = static_cast<DWORD>(iter->second);
else
dwPid = 0;
} else {
dwPid=getProcess(procname);
}

if (!dwPid)
return false;

pModule=getModuleAddr(modname ? modname : procname);
if (!pModule) {
dwPid = 0;
return false;
}

hProcess=OpenProcess(PROCESS_VM_READ, false, dwPid);
if (!hProcess) {
dwPid = 0;
pModule = NULL;
return false;
}

return true;
}

static void generic_unlock() {
if (hProcess) {
CloseHandle(hProcess);
hProcess = NULL;
pModule = NULL;
dwPid = 0;
}
}

#endif
@@ -0,0 +1,14 @@
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_PLUGIN_WIN32_X64_H_
#define MUMBLE_MUMBLE_PLUGIN_WIN32_X64_H_

typedef unsigned long long procptr64_t;

#define PTR_TYPE procptr64_t
#include "mumble_plugin_win32_ptr_type.h"

#endif
@@ -0,0 +1,14 @@
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_PLUGIN_WIN32_X86_H_
#define MUMBLE_MUMBLE_PLUGIN_WIN32_X86_H_

typedef unsigned long procptr32_t;

#define PTR_TYPE procptr32_t
#include "mumble_plugin_win32_ptr_type.h"

#endif
@@ -16,6 +16,10 @@ CONFIG(static) {
CONFIG += qt_dynamic_lookup
}

!CONFIG(no-plugin-legacy-ptr) {
DEFINES += USE_PLUGIN_LEGACY_PTR
}

CONFIG(debug, debug|release) {
CONFIG += console
DESTDIR = ../../debug/plugins
@@ -3,7 +3,7 @@
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "../mumble_plugin_win32.h"
#include "../mumble_plugin_win32_x86.h"

static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, float *camera_pos, float *camera_front, float *camera_top, std::string &context, std::wstring &identity) {
for (int i=0;i<3;i++)
@@ -16,23 +16,23 @@ static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, floa
float avatar_pos_corrector[3], camera_pos_corrector[3], avatar_front_corrector[3], camera_front_corrector[3], camera_top_corrector[3];

// Avatar pointers
BYTE *avatar_base = peekProc<BYTE *>(pModule + 0x016FEC04);
procptr32_t avatar_base = peekProc<procptr32_t>(pModule + 0x016FEC04);
if (!avatar_base) return false;
BYTE *avatar_offset_0 = peekProc<BYTE *>(avatar_base + 0x448);
procptr32_t avatar_offset_0 = peekProc<procptr32_t>(avatar_base + 0x448);
if (!avatar_offset_0) return false;
BYTE *avatar_offset_1 = peekProc<BYTE *>(avatar_offset_0 + 0x440);
procptr32_t avatar_offset_1 = peekProc<procptr32_t>(avatar_offset_0 + 0x440);
if (!avatar_offset_1) return false;
BYTE *avatar_offset_2 = peekProc<BYTE *>(avatar_offset_1 + 0x0);
procptr32_t avatar_offset_2 = peekProc<procptr32_t>(avatar_offset_1 + 0x0);
if (!avatar_offset_2) return false;
BYTE *avatar_offset = peekProc<BYTE *>(avatar_offset_2 + 0x1C);
procptr32_t avatar_offset = peekProc<procptr32_t>(avatar_offset_2 + 0x1C);
if (!avatar_offset) return false;

// Peekproc and assign game addresses to our containers, so we can retrieve positional data
ok = peekProc((BYTE *) avatar_offset + 0x0, &avatar_pos_corrector, 12) && // Avatar Position values (Z, Y and X).
peekProc((BYTE *) pModule + 0x016FEE40, &camera_pos_corrector, 12) && // Camera Position values (Z, Y and X).
peekProc((BYTE *) avatar_offset + 0xC, &avatar_front_corrector, 12) && // Avatar Front values (Z, Y and X).
peekProc((BYTE *) pModule + 0x016FEE28, &camera_front_corrector, 12) && // Camera Front Vector values (Z, Y and X).
peekProc((BYTE *) pModule + 0x016FEE34, &camera_top_corrector, 12); // Camera Top Vector values (Z, Y and X).
ok = peekProc(avatar_offset + 0x0, &avatar_pos_corrector, 12) && // Avatar Position values (Z, Y and X).
peekProc(pModule + 0x016FEE40, &camera_pos_corrector, 12) && // Camera Position values (Z, Y and X).
peekProc(avatar_offset + 0xC, &avatar_front_corrector, 12) && // Avatar Front values (Z, Y and X).
peekProc(pModule + 0x016FEE28, &camera_front_corrector, 12) && // Camera Front Vector values (Z, Y and X).
peekProc(pModule + 0x016FEE34, &camera_top_corrector, 12); // Camera Top Vector values (Z, Y and X).

// This prevents the plugin from linking to the game in case something goes wrong during values retrieval from memory addresses.
if (! ok)

0 comments on commit 51af785

Please sign in to comment.
You can’t perform that action at this time.