Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Re-implemented Kiero but stripped down for just what we need (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
std-nullptr authored Dec 9, 2020
1 parent 92f9a91 commit 437ebbc
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 67 deletions.
2 changes: 2 additions & 0 deletions AmongUsMenu.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ClCompile Include="hooks\Dleks.cpp" />
<ClCompile Include="hooks\GameOptionsData.cpp" />
<ClCompile Include="hooks\HudManager.cpp" />
<ClCompile Include="includes\directx11.cpp" />
<ClCompile Include="includes\detours\detours.cpp" />
<ClCompile Include="includes\detours\disasm.cpp" />
<ClCompile Include="includes\detours\modules.cpp" />
Expand Down Expand Up @@ -111,6 +112,7 @@
<ClInclude Include="gui\tabs\tasks_tab.h" />
<ClInclude Include="hooks\DirectX.h" />
<ClInclude Include="hooks\_hooks.hpp" />
<ClInclude Include="includes\directx11.h" />
<ClInclude Include="includes\detours\detours.h" />
<ClInclude Include="includes\imhotkeys.h" />
<ClInclude Include="resources\resource_data.h" />
Expand Down
5 changes: 2 additions & 3 deletions AmongUsMenu.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
<ClCompile Include="hooks\DirectX.cpp">
<Filter>hooks</Filter>
</ClCompile>
<ClCompile Include="includes\directx11.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="user\main.h">
Expand Down Expand Up @@ -323,6 +324,7 @@
<ClInclude Include="hooks\DirectX.h">
<Filter>hooks</Filter>
</ClInclude>
<ClInclude Include="includes\directx11.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="user">
Expand All @@ -346,9 +348,6 @@
<Filter Include="includes\detours">
<UniqueIdentifier>{166264ce-d359-420e-ba62-adba2279ff8f}</UniqueIdentifier>
</Filter>
<Filter Include="includes\kiero">
<UniqueIdentifier>{20efd3a7-e4c8-4470-a08e-648e1abcd71e}</UniqueIdentifier>
</Filter>
<Filter Include="includes\imgui">
<UniqueIdentifier>{0ea105ce-0c37-46f9-8a5e-b845cb40a743}</UniqueIdentifier>
</Filter>
Expand Down
88 changes: 32 additions & 56 deletions hooks/DirectX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,42 @@ LRESULT __stdcall dWndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
}

HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT flags) {
bool ImGuiInitialization(IDXGISwapChain* pSwapChain) {
if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice))) {
pDevice->GetImmediateContext(&pContext);
DXGI_SWAP_CHAIN_DESC sd;
pSwapChain->GetDesc(&sd);
window = sd.OutputWindow;
ID3D11Texture2D* pBackBuffer = NULL;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
pBackBuffer->Release();
oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)dWndProc);

ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange;
ImGui_ImplWin32_Init(window);
ImGui_ImplDX11_Init(pDevice, pContext);

maps.push_back({ D3D11Image(Resource(IDB_PNG1), pDevice), 277.F, 77.F, 11.5F });
maps.push_back({ D3D11Image(Resource(IDB_PNG2), pDevice), 115.F, 240.F, 9.25F });
maps.push_back({ D3D11Image(Resource(IDB_PNG3), pDevice), 8.F, 21.F, 10.F });

return true;
}

return false;
}

HRESULT __stdcall dPresent(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags) {
WaitForSingleObject(hPresentMutex, 0); //We're claiming ownership, but we'll be damned if we're going to wait
if (!State.ImGuiInitialized) {
if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice))) {
pDevice->GetImmediateContext(&pContext);
DXGI_SWAP_CHAIN_DESC sd;
pSwapChain->GetDesc(&sd);
window = sd.OutputWindow;
ID3D11Texture2D* pBackBuffer;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
pBackBuffer->Release();
oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)dWndProc);

ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange;
ImGui_ImplWin32_Init(window);
ImGui_ImplDX11_Init(pDevice, pContext);

maps.push_back({ D3D11Image(Resource(IDB_PNG1), pDevice), 277.F, 77.F, 11.5F });
maps.push_back({ D3D11Image(Resource(IDB_PNG2), pDevice), 115.F, 240.F, 9.25F });
maps.push_back({ D3D11Image(Resource(IDB_PNG3), pDevice), 8.F, 21.F, 10.F });

if (ImGuiInitialization(__this)) {
State.ImGuiInitialized = true;
} else {
ReleaseMutex(hPresentMutex);
return oPresent(pSwapChain, syncInterval, flags);
return oPresent(__this, SyncInterval, Flags);
}
}

Expand Down Expand Up @@ -147,39 +155,7 @@ HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT f

ReleaseMutex(hPresentMutex);

return oPresent(pSwapChain, syncInterval, flags);
}

void DirectX::Initialize()
{
ID3D11Device* pDevice = NULL;
IDXGISwapChain* pSwapchain = NULL;

DXGI_SWAP_CHAIN_DESC swapChainDesc{ 0 };

hPresentMutex = CreateMutex(NULL, FALSE, NULL);

swapChainDesc.BufferCount = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.OutputWindow = GetDesktopWindow();
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.SampleDesc.Count = 1;

HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_REFERENCE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapchain, &pDevice, NULL, NULL);

if (GetLastError() == 0x594) SetLastError(0); // Ignore any errors related to the output window

if (FAILED(result)) return;

void** pVMT = *(void***)pSwapchain;
oPresent = (D3D_PRESENT_FUNCTION)pVMT[8];

if (pDevice) {
pDevice->Release();
pDevice = NULL;
}
return oPresent(__this, SyncInterval, Flags);
}

void DirectX::Shutdown() {
Expand Down
8 changes: 3 additions & 5 deletions hooks/DirectX.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "resources.h"
#include "directx11.h"
#include <vector>

struct MapTexture {
Expand All @@ -9,14 +10,11 @@ struct MapTexture {
float scale;
};

typedef HRESULT(__stdcall* D3D_PRESENT_FUNCTION)(IDXGISwapChain* pThis, UINT SyncInterval, UINT Flags);

extern D3D_PRESENT_FUNCTION oPresent;
extern std::vector<MapTexture> maps;

HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT flags);
extern D3D_PRESENT_FUNCTION oPresent;
HRESULT __stdcall dPresent(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags);

namespace DirectX {
void Initialize();
void Shutdown();
}
12 changes: 9 additions & 3 deletions hooks/_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool HookFunction(PVOID* ppPointer, PVOID pDetour, const char* functionName) {
std::cout << "Failed to hook " << functionName << std::endl;
return false;
}
std::cout << "Hooked " << functionName << std::endl;
//std::cout << "Hooked " << functionName << std::endl;
return true;
}

Expand All @@ -22,6 +22,14 @@ void DetourInitilization() {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());

directx11 d3d11 = directx11();
if (!d3d11.presentFunction) {
std::cout << "Unable to retrieve IDXGISwapChain::Present method" << std::endl;
return;
} else {
oPresent = d3d11.presentFunction;
}

HOOKFUNC(SceneManager_Internal_ActiveSceneChanged);
HOOKFUNC(HatManager_GetUnlockedHats);
HOOKFUNC(HatManager_GetUnlockedPets);
Expand Down Expand Up @@ -54,8 +62,6 @@ void DetourInitilization() {
HOOKFUNC(MeetingHud_Awake);
HOOKFUNC(MeetingHud_Close);
HOOKFUNC(Constants_ShouldFlipSkeld);

DirectX::Initialize();
if (!HookFunction(&(PVOID&)oPresent, dPresent, "D3D_PRESENT_FUNCTION")) return;

DetourTransactionCommit();
Expand Down
80 changes: 80 additions & 0 deletions includes/directx11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "directx11.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

directx11::directx11()
{
WNDCLASSEX windowClass = {
sizeof(WNDCLASSEX),
CS_HREDRAW | CS_VREDRAW,
DefWindowProc,
0,
0,
GetModuleHandle(NULL),
NULL,
NULL,
NULL,
NULL,
L"DirectX 11",
NULL,
};

RegisterClassEx(&windowClass);
HWND window = CreateWindow(windowClass.lpszClassName, L"DirectX 11 Window", WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, NULL, NULL, windowClass.hInstance, NULL);

D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_11_0 };

DXGI_MODE_DESC bufferDesc = {
100,
100,
{60, 1},
DXGI_FORMAT_R8G8B8A8_UNORM,
DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED,
DXGI_MODE_SCALING_UNSPECIFIED,
};

DXGI_SWAP_CHAIN_DESC swapChainDesc = {
bufferDesc,
{1, 0},
DXGI_USAGE_RENDER_TARGET_OUTPUT,
1,
window,
1,
DXGI_SWAP_EFFECT_DISCARD,
DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH,
};

IDXGISwapChain* pSwapChain;
ID3D11Device* pDevice;
ID3D11DeviceContext* pContext;

HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featureLevels, 2, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, &featureLevel, &pContext);

if (FAILED(result)) {
DestroyWindow(window);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
return;
}

void** pVMT = *(void***)pSwapChain;
presentFunction = (D3D_PRESENT_FUNCTION)pVMT[8];

if (pSwapChain) {
pSwapChain->Release();
pSwapChain = NULL;
}

if (pDevice) {
pDevice->Release();
pDevice = NULL;
}

if (pContext) {
pContext->Release();
pContext = NULL;
}

DestroyWindow(window);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
}
12 changes: 12 additions & 0 deletions includes/directx11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
#include <dxgi.h>
#include <d3d11.h>

typedef HRESULT(__stdcall* D3D_PRESENT_FUNCTION)(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags);

class directx11 {
public:
D3D_PRESENT_FUNCTION presentFunction;
directx11();
};

0 comments on commit 437ebbc

Please sign in to comment.