This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.h
168 lines (162 loc) · 8.24 KB
/
Device.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
//
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND window, UINT message, WPARAM wideParam, LPARAM longParam);
namespace Device
{
using PresentFn = HRESULT(__stdcall*) (IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags);
using ResizeBuffersFn = HRESULT(__stdcall*) (IDXGISwapChain* pSwapChain, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags);
inline PresentFn oPresent = nullptr;
inline ResizeBuffersFn oResizeBuffers = nullptr;
inline LRESULT WINAPI WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (GlobalSetting::Unload)
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
if (uMsg == WM_KEYUP && wParam == VK_F1)
{
GlobalSetting::ShowMenu = !GlobalSetting::ShowMenu;
}
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
if (!GlobalSetting::ShowMenu)
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
return true;
}
inline HRESULT Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
if (GlobalSetting::Unload)
return oPresent(pSwapChain, SyncInterval, Flags);
static auto bMakeOnce = [&]()->bool
{
if (FAILED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice)))
{
return false;
}
pDevice->GetImmediateContext(&pContext);
DXGI_SWAP_CHAIN_DESC sd;
pSwapChain->GetDesc(&sd);
hWnd = sd.OutputWindow;
ID3D11Texture2D* pBackBuffer = NULL;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
D3D11_RENDER_TARGET_VIEW_DESC desc = {};
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
pDevice->CreateRenderTargetView(pBackBuffer, &desc, &mainRenderTargetView);
pBackBuffer->Release();
oWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WndProc);
ImGui::CreateContext();
ImGui::GetIO().IniFilename = nullptr;
ImGui::GetIO().LogFilename = nullptr;
ImGui::GetStyle().FrameRounding = 3.0f;
ImGui::GetStyle().WindowRounding = 2.0f;
ImGui::GetStyle().GrabRounding = 2.0f;
ImGui::GetStyle().WindowRounding = 3.0f;
ImGui::GetStyle().WindowTitleAlign = { 0.5f ,0.5f };
{
ImGuiStyle* style = &ImGui::GetStyle();
ImVec4* colors = style->Colors;
colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f);
colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);
colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.54f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.28f, 0.28f, 0.28f, 0.40f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.17f, 0.17f, 0.17f, 0.67f);
colors[ImGuiCol_TitleBg] = ImVec4(0.21f, 0.21f, 0.21f, 1.00f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.21f, 0.21f, 0.21f, 1.00f);
colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.22f, 0.22f, 0.22f, 0.51f);
colors[ImGuiCol_MenuBarBg] = ImVec4(0.26f, 0.26f, 0.26f, 1.00f);
colors[ImGuiCol_ScrollbarBg] = ImVec4(0.14f, 0.14f, 0.14f, 0.53f);
colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);
colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);
colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);
colors[ImGuiCol_CheckMark] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.30f, 0.30f, 0.30f, 1.00f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
colors[ImGuiCol_Button] = ImVec4(0.44f, 0.44f, 0.44f, 0.40f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.27f, 0.27f, 0.27f, 1.00f);
colors[ImGuiCol_Header] = ImVec4(0.46f, 0.46f, 0.46f, 0.31f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.32f, 0.32f, 0.32f, 0.80f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);
colors[ImGuiCol_Separator] = colors[ImGuiCol_Border];
colors[ImGuiCol_SeparatorHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.78f);
colors[ImGuiCol_SeparatorActive] = ImVec4(0.71f, 0.71f, 0.71f, 1.00f);
colors[ImGuiCol_ResizeGrip] = ImVec4(0.71f, 0.71f, 0.71f, 0.20f);
colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.56f, 0.56f, 0.56f, 0.67f);
colors[ImGuiCol_ResizeGripActive] = ImVec4(0.43f, 0.43f, 0.43f, 0.95f);
colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.40f);
colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered];
colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.80f);
colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f);
colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f);
colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.92f, 0.92f, 0.92f, 1.00f);
colors[ImGuiCol_PlotHistogram] = ImVec4(0.43f, 0.43f, 0.43f, 1.00f);
colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f);
colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f);
colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f);
colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.36f, 0.36f, 0.36f, 0.35f);
colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.26f, 0.26f, 1.00f);
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);
}
ImGui_ImplWin32_Init(hWnd);
ImGui_ImplDX11_Init(pDevice, pContext);
ImGui::GetIO().Fonts->AddFontFromFileTTF("c:\\windows\\fonts\\msyh.ttc", 16, NULL, ImGui::GetIO().Fonts->GetGlyphRangesChineseFull());
Cheat::Setup();
return true;
} ();
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::Render();
pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
return oPresent(pSwapChain, SyncInterval, Flags);
}
inline HRESULT ResizeBuffers(IDXGISwapChain* pSwapChain, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
{
if (GlobalSetting::Unload)
return oResizeBuffers(pSwapChain, BufferCount, Width, Height, NewFormat, SwapChainFlags);
if (mainRenderTargetView)
{
pContext->OMSetRenderTargets(0, 0, 0);
mainRenderTargetView->Release();
mainRenderTargetView = NULL;
}
HRESULT hr = oResizeBuffers(pSwapChain, BufferCount, Width, Height, NewFormat, SwapChainFlags);
ID3D11Texture2D* pBuffer = NULL;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBuffer);
pDevice->CreateRenderTargetView(pBuffer, nullptr, &mainRenderTargetView);
pBuffer->Release();
pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)Width;
vp.Height = (FLOAT)Height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
pContext->RSSetViewports(1, &vp);
return hr;
}
inline bool Setup()
{
static auto CreateHook = [](LPVOID pTarget, LPVOID pDetour, LPVOID* ppOriginal)->bool
{
return MH_CreateHook(pTarget, pDetour, ppOriginal) == MH_OK && MH_EnableHook(pTarget) == MH_OK;
};
if (!CreateHook(Direct3D.Present, Device::Present, reinterpret_cast<void**>(&Device::oPresent)))
return false;
if (!CreateHook(Direct3D.ResizeBuffers, Device::ResizeBuffers, reinterpret_cast<void**>(&Device::oResizeBuffers)))
return false;
return true;
}
}