Skip to content

Commit

Permalink
Code cleanup (clang-tidy code to lower_case style)
Browse files Browse the repository at this point in the history
clang-tidy pass 1 (D3D11Hook.cpp)

clang-tidy pass 2

clang-tidy pass 3

clang-tidy pass 4 (ObjectExplorer.cpp)

Cleanup

Add build_vs2019.bat

Add Math.hpp
  • Loading branch information
praydog committed Feb 9, 2020
1 parent 57a9ab6 commit 6d179aa
Show file tree
Hide file tree
Showing 52 changed files with 1,398 additions and 1,324 deletions.
61 changes: 61 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Checks: -*,readability-identifier-naming,readability-else-after-return
HeaderFilterRegex: '^.*src.*(RE|Mod|FreeCam|FirstPerson|Hook|Developer|Object|Flashlight|utility).*\.hpp$'
# HeaderFilterRegex: '^(.*src.*REContext).*\.hpp$'
CheckOptions:
# Classes, structs, ...
- key: readability-identifier-naming.NamespaceCase
value: lower_case
- key: readability-identifier-naming.ClassCase
value: CamelCase
- key: readability-identifier-naming.StructCase
value: CamelCase
- key: readability-identifier-naming.EnumCase
value: CamelCase
- key: readability-identifier-naming.UnionCase
value: CamelCase
- key: readability-identifier-naming.TypedefCase
value: CamelCase

# Variables, member variables, ...
- key: readability-identifier-naming.ParameterCase
value: lower_case
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.MemberCase
value: lower_case
- key: readability-identifier-naming.PublicMemberCase
value: lower_case
- key: readability-identifier-naming.ProtectedMemberCase
value: lower_case
- key: readability-identifier-naming.PrivateMemberCase
value: lower_case
- key: readability-identifier-naming.PrivateMemberSuffix
value: ''

# Functions, methods, ...
- key: readability-identifier-naming.FunctionCase
value: lower_case
- key: readability-identifier-naming.MethodCase
value: lower_case

# Constants
- key: readability-identifier-naming.ConstantPrefix
value: ''
- key: readability-identifier-naming.ConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.ConstantMemberPrefix
value: ''
- key: readability-identifier-naming.ConstantMemberCase
value: lower_case
- key: readability-identifier-naming.ConstantParameterPrefix
value: ''
- key: readability-identifier-naming.ConstantParameterCase
value: lower_case
- key: readability-identifier-naming.LocalConstantParameterPrefix
value: ''
- key: readability-identifier-naming.LocalConstantCase
value: lower_case
- key: readability-identifier-naming.ConstexprVariablePrefix
value: ''
- key: readability-identifier-naming.ConstexprVariableCase
value: lower_casse
6 changes: 6 additions & 0 deletions build_vs2019.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
git pull --recurse-submodules
git submodule update --init --recursive
mkdir build64
cd build64
cmake .. -G "Visual Studio 16 2019" -A x64 -DDEVELOPER_MODE=OFF
cmake --build . --config Release
74 changes: 37 additions & 37 deletions src/D3D11Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using namespace std;

static D3D11Hook* g_d3d11Hook = nullptr;
static D3D11Hook* g_d3d11_hook = nullptr;

D3D11Hook::~D3D11Hook() {
unhook();
Expand All @@ -14,43 +14,43 @@ D3D11Hook::~D3D11Hook() {
bool D3D11Hook::hook() {
spdlog::info("Hooking D3D11");

g_d3d11Hook = this;
g_d3d11_hook = this;

HWND hWnd = GetDesktopWindow();
IDXGISwapChain* swapChain = nullptr;
HWND h_wnd = GetDesktopWindow();
IDXGISwapChain* swap_chain = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;

D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0;
DXGI_SWAP_CHAIN_DESC swap_chain_desc;

ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
ZeroMemory(&swap_chain_desc, sizeof(swap_chain_desc));

swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = TRUE;
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swap_chain_desc.BufferCount = 1;
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swap_chain_desc.OutputWindow = h_wnd;
swap_chain_desc.SampleDesc.Count = 1;
swap_chain_desc.Windowed = TRUE;
swap_chain_desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swap_chain_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

if (FAILED(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, nullptr, &context))) {
if (FAILED(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, &feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, nullptr, &context))) {
spdlog::error("Failed to create D3D11 device");
return false;
}

auto presentFn = (*(uintptr_t**)swapChain)[8];
auto resizeBuffersFn = (*(uintptr_t**)swapChain)[13];
m_presentHook = std::make_unique<FunctionHook>(presentFn, (uintptr_t)&D3D11Hook::present);
m_resizeBuffersHook = std::make_unique<FunctionHook>(resizeBuffersFn, (uintptr_t)&D3D11Hook::resizeBuffers);
auto present_fn = (*(uintptr_t**)swap_chain)[8];
auto resize_buffers_fn = (*(uintptr_t**)swap_chain)[13];
m_present_hook = std::make_unique<FunctionHook>(present_fn, (uintptr_t)&D3D11Hook::present);
m_resize_buffers_hook = std::make_unique<FunctionHook>(resize_buffers_fn, (uintptr_t)&D3D11Hook::resize_buffers);

device->Release();
context->Release();
swapChain->Release();
swap_chain->Release();

m_hooked = m_presentHook->create() && m_resizeBuffersHook->create();
m_hooked = m_present_hook->create() && m_resize_buffers_hook->create();

return m_hooked;
}
Expand All @@ -59,29 +59,29 @@ bool D3D11Hook::unhook() {
return true;
}

HRESULT WINAPI D3D11Hook::present(IDXGISwapChain* swapChain, UINT syncInterval, UINT flags) {
auto d3d11 = g_d3d11Hook;
HRESULT WINAPI D3D11Hook::present(IDXGISwapChain* swap_chain, UINT sync_interval, UINT flags) {
auto d3d11 = g_d3d11_hook;

d3d11->m_swapChain = swapChain;
swapChain->GetDevice(__uuidof(d3d11->m_device), (void**)&d3d11->m_device);
d3d11->m_swap_chain = swap_chain;
swap_chain->GetDevice(__uuidof(d3d11->m_device), (void**)&d3d11->m_device);

if (d3d11->m_onPresent) {
d3d11->m_onPresent(*d3d11);
if (d3d11->m_on_present) {
d3d11->m_on_present(*d3d11);
}

auto presentFn = d3d11->m_presentHook->getOriginal<decltype(D3D11Hook::present)>();
auto present_fn = d3d11->m_present_hook->get_original<decltype(D3D11Hook::present)>();

return presentFn(swapChain, syncInterval, flags);
return present_fn(swap_chain, sync_interval, flags);
}

HRESULT WINAPI D3D11Hook::resizeBuffers(IDXGISwapChain* swapChain, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags) {
auto d3d11 = g_d3d11Hook;
HRESULT WINAPI D3D11Hook::resize_buffers(IDXGISwapChain* swap_chain, UINT buffer_count, UINT width, UINT height, DXGI_FORMAT new_format, UINT swap_chain_flags) {
auto d3d11 = g_d3d11_hook;

if (d3d11->m_onResizeBuffers) {
d3d11->m_onResizeBuffers(*d3d11);
if (d3d11->m_on_resize_buffers) {
d3d11->m_on_resize_buffers(*d3d11);
}

auto resizeBuffersFn = d3d11->m_resizeBuffersHook->getOriginal<decltype(D3D11Hook::resizeBuffers)>();
auto resize_buffers_fn = d3d11->m_resize_buffers_hook->get_original<decltype(D3D11Hook::resize_buffers)>();

return resizeBuffersFn(swapChain, BufferCount, Width, Height, NewFormat, SwapChainFlags);
return resize_buffers_fn(swap_chain, buffer_count, width, height, new_format, swap_chain_flags);
}
22 changes: 11 additions & 11 deletions src/D3D11Hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ class D3D11Hook {
bool hook();
bool unhook();

void onPresent(OnPresentFn fn) { m_onPresent = fn; }
void onResizeBuffers(OnResizeBuffersFn fn) { m_onResizeBuffers = fn; }
void on_present(OnPresentFn fn) { m_on_present = fn; }
void on_resize_buffers(OnResizeBuffersFn fn) { m_on_resize_buffers = fn; }

ID3D11Device* getDevice() { return m_device; }
IDXGISwapChain* getSwapChain() { return m_swapChain; }
ID3D11Device* get_device() { return m_device; }
IDXGISwapChain* get_swap_chain() { return m_swap_chain; }

protected:
ID3D11Device* m_device{ nullptr };
IDXGISwapChain* m_swapChain{ nullptr };
IDXGISwapChain* m_swap_chain{ nullptr };
bool m_hooked{ false };

std::unique_ptr<FunctionHook> m_presentHook{};
std::unique_ptr<FunctionHook> m_resizeBuffersHook{};
OnPresentFn m_onPresent{ nullptr };
OnResizeBuffersFn m_onResizeBuffers{ nullptr };
std::unique_ptr<FunctionHook> m_present_hook{};
std::unique_ptr<FunctionHook> m_resize_buffers_hook{};
OnPresentFn m_on_present{ nullptr };
OnResizeBuffersFn m_on_resize_buffers{ nullptr };

static HRESULT WINAPI present(IDXGISwapChain* swapChain, UINT syncInterval, UINT flags);
static HRESULT WINAPI resizeBuffers(IDXGISwapChain* swapChain, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags);
static HRESULT WINAPI present(IDXGISwapChain* swap_chain, UINT sync_interval, UINT flags);
static HRESULT WINAPI resize_buffers(IDXGISwapChain* swap_chain, UINT buffer_count, UINT width, UINT height, DXGI_FORMAT new_format, UINT swap_chain_flags);
};
48 changes: 24 additions & 24 deletions src/DInputHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

using namespace std;

static DInputHook* g_dinputHook{ nullptr };
static DInputHook* g_dinput_hook{ nullptr };

DInputHook::DInputHook(HWND wnd)
: m_wnd{ wnd },
m_isIgnoringInput{ false },
m_doOnce{ true }
m_is_ignoring_input{ false },
m_do_once{ true }
{
if (g_dinputHook == nullptr) {
if (g_dinput_hook == nullptr) {
if (hook()) {
spdlog::info("DInputHook hooked successfully.");
g_dinputHook = this;
g_dinput_hook = this;
}
else {
spdlog::info("DInputHook failed to hook.");
Expand All @@ -27,9 +27,9 @@ DInputHook::DInputHook(HWND wnd)

DInputHook::~DInputHook() {
// Explicitly unhook the methods we hooked so we can reset g_dinputHook.
m_getDeviceStateHook.reset();
m_get_device_state_hook.reset();

g_dinputHook = nullptr;
g_dinput_hook = nullptr;
}

bool DInputHook::hook() {
Expand All @@ -40,19 +40,19 @@ bool DInputHook::hook() {
using DirectInput8CreateFn = HRESULT(WINAPI*)(HINSTANCE, DWORD, REFIID, LPVOID*, LPUNKNOWN);

auto dinput8 = LoadLibrary("dinput8.dll");
auto dinput8Create = (DirectInput8CreateFn)GetProcAddress(dinput8, "DirectInput8Create");
auto dinput8_create = (DirectInput8CreateFn)GetProcAddress(dinput8, "DirectInput8Create");

if (dinput8Create == nullptr) {
if (dinput8_create == nullptr) {
spdlog::info("Failed to find DirectInput8Create.");
return false;
}

spdlog::info("Got DirectInput8Create {:p}", (void*)dinput8Create);
spdlog::info("Got DirectInput8Create {:p}", (void*)dinput8_create);

auto instance = (HINSTANCE)GetModuleHandle(nullptr);
IDirectInput* dinput{ nullptr };

if (FAILED(dinput8Create(instance, DIRECTINPUT_VERSION, IID_IDirectInput8W, (LPVOID*)&dinput, nullptr))) {
if (FAILED(dinput8_create(instance, DIRECTINPUT_VERSION, IID_IDirectInput8W, (LPVOID*)&dinput, nullptr))) {
spdlog::info("Failed to create IDirectInput.");
return false;
}
Expand All @@ -70,44 +70,44 @@ bool DInputHook::hook() {
spdlog::info("Got IDirectInputDevice {:p}", (void*)device);

// Get the addresses of the methods we want to hook.
auto getDeviceState = (*(uintptr_t**)device)[9];
auto get_device_state = (*(uintptr_t**)device)[9];

spdlog::info("Got IDirectInputDevice::GetDeviceState {:p}", getDeviceState);
spdlog::info("Got IDirectInputDevice::GetDeviceState {:p}", get_device_state);

device->Release();
dinput->Release();

// Hook them.
m_getDeviceStateHook = make_unique<FunctionHook>(getDeviceState, (uintptr_t)&DInputHook::getDeviceState);
m_get_device_state_hook = make_unique<FunctionHook>(get_device_state, (uintptr_t)&DInputHook::get_device_state);

return m_getDeviceStateHook->create();
return m_get_device_state_hook->create();
}

HRESULT DInputHook::getDeviceState_Internal(IDirectInputDevice* device, DWORD size, LPVOID data) {
auto originalGetDeviceState = (decltype(DInputHook::getDeviceState)*)m_getDeviceStateHook->getOriginal();
HRESULT DInputHook::get_device_state_internal(IDirectInputDevice* device, DWORD size, LPVOID data) {
auto original_get_device_state = (decltype(DInputHook::get_device_state)*)m_get_device_state_hook->get_original();

spdlog::debug("getDeviceState");

// If we are ignoring input then we call the original to remove buffered
// input events from the devices queue without modifying the out parameters.
if ((m_isIgnoringInput || m_doOnce) && size == 256) {
if ((m_is_ignoring_input || m_do_once) && size == 256) {
device->Unacquire();
device->SetCooperativeLevel(m_wnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
device->Acquire();

m_doOnce = false;
m_do_once = false;
}

auto res = originalGetDeviceState(device, size, data);
auto res = original_get_device_state(device, size, data);

// Feed keys back to the framework
if (res == DI_OK && !m_isIgnoringInput && data != nullptr && size == 256) {
g_framework->onDirectInputKeys(*(std::array<uint8_t, 256>*)data);
if (res == DI_OK && !m_is_ignoring_input && data != nullptr && size == 256) {
g_framework->on_direct_input_keys(*(std::array<uint8_t, 256>*)data);
}

return res;
}

HRESULT WINAPI DInputHook::getDeviceState(IDirectInputDevice* device, DWORD size, LPVOID data) {
return g_dinputHook->getDeviceState_Internal(device, size, data);
HRESULT WINAPI DInputHook::get_device_state(IDirectInputDevice* device, DWORD size, LPVOID data) {
return g_dinput_hook->get_device_state_internal(device, size, data);
}
28 changes: 14 additions & 14 deletions src/DInputHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ class DInputHook {
DInputHook(HWND wnd);
virtual ~DInputHook();

void ignoreInput() {
m_isIgnoringInput = true;
void ignore_input() {
m_is_ignoring_input = true;
}

void acknowledgeInput() {
m_isIgnoringInput = false;
void acknowledge_input() {
m_is_ignoring_input = false;
}

auto isIgnoringInput() const {
return m_isIgnoringInput;
auto is_ignoring_input() const {
return m_is_ignoring_input;
}

auto isValid() const {
return m_getDeviceStateHook->isValid();
auto is_valid() const {
return m_get_device_state_hook->is_valid();
}

void setWindow(HWND wnd) {
void set_window(HWND wnd) {
m_wnd = wnd;
}

Expand All @@ -42,13 +42,13 @@ class DInputHook {
private:
HWND m_wnd;

std::unique_ptr<FunctionHook> m_getDeviceStateHook;
std::unique_ptr<FunctionHook> m_get_device_state_hook;

bool m_isIgnoringInput;
bool m_doOnce;
bool m_is_ignoring_input;
bool m_do_once;

bool hook();

HRESULT getDeviceState_Internal(IDirectInputDevice* device, DWORD size, LPVOID data);
static HRESULT WINAPI getDeviceState(IDirectInputDevice* device, DWORD size, LPVOID data);
HRESULT get_device_state_internal(IDirectInputDevice* device, DWORD size, LPVOID data);
static HRESULT WINAPI get_device_state(IDirectInputDevice* device, DWORD size, LPVOID data);
};
Loading

0 comments on commit 6d179aa

Please sign in to comment.