-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
147 lines (122 loc) · 2.96 KB
/
Main.cpp
File metadata and controls
147 lines (122 loc) · 2.96 KB
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
#include "Main.h"
#pragma comment(lib, "d3d11.lib")
bool ScanDXGIFunction(void**& vTable)
{
WNDCLASSEX windowClass =
{
sizeof(WNDCLASSEX),
CS_HREDRAW | CS_VREDRAW,
DefWindowProc,
0,
0,
GetModuleHandle(NULL),
NULL,
NULL,
NULL,
NULL,
XOR(L"DirectX 11"),
NULL,
};
RegisterClassEx(&windowClass);
HWND window = CreateWindow(windowClass.lpszClassName, XOR(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* swapChain = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featureLevels, 2, D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, &featureLevel, &context);
if (FAILED(result))
{
DestroyWindow(window);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
return false;
}
vTable = *(void***)swapChain;
if (swapChain)
{
swapChain->Release();
swapChain = NULL;
}
if (device)
{
device->Release();
device = NULL;
}
if (context)
{
context->Release();
context = NULL;
}
DestroyWindow(window);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
return true;
}
bool DllInitialize()
{
CallbackManager::Get()->Invoke(XOR("Dll Initialize"));
if (MH_Initialize() != MH_OK)
{
return false;
}
if (!WinUser::hiSetCursor.Hook(SetCursor)
|| !WinUser::hiSetCursorPos.Hook(SetCursorPos))
{
return false;
}
void** dxgiFunctionVTable = nullptr;
if (!ScanDXGIFunction(dxgiFunctionVTable))
{
return false;
}
if (!dxgi::hiPresent.Hook((void*)dxgiFunctionVTable[8])
|| !dxgi::hiResizeBuffers.Hook((void*)dxgiFunctionVTable[13]))
{
return false;
}
return true;
}
void Main()
{
Application::Get()->internal.unload = !DllInitialize();
while (!Application::Get()->internal.unload)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
singletonManager.DestroyAll();
MH_Uninitialize();
SetWindowLongPtr(Application::Get()->internal.window, GWLP_WNDPROC, (LONG_PTR)dxgi::oWndProc);
FreeLibraryAndExitThread(Application::Get()->internal.module, 0);
Application::Destroy();
}
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(module);
Application::Create(module);
CreateThread(LPSECURITY_ATTRIBUTES {}, SIZE_T(NULL), (LPTHREAD_START_ROUTINE)Main, LPVOID(NULL), DWORD(NULL), LPDWORD(NULL));
break;
}
return TRUE;
}