forked from praydog/REFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREFramework.hpp
291 lines (226 loc) · 8.21 KB
/
REFramework.hpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#pragma once
#include <array>
#include <unordered_set>
#include <filesystem>
#include <spdlog/spdlog.h>
#include <imgui.h>
class Mods;
class REGlobals;
class RETypes;
#include "D3D11Hook.hpp"
#include "D3D12Hook.hpp"
#include "DInputHook.hpp"
#include "WindowsMessageHook.hpp"
// Global facilitator
class REFramework {
private:
void hook_monitor();
public:
REFramework(HMODULE reframework_module);
virtual ~REFramework();
auto get_reframework_module() const { return m_reframework_module; }
bool is_valid() const { return m_valid; }
bool is_dx11() const { return m_is_d3d11; }
bool is_dx12() const { return m_is_d3d12; }
const auto& get_mods() const { return m_mods; }
const auto& get_mouse_delta() const { return m_mouse_delta; }
const auto& get_keyboard_state() const { return m_last_keys; }
Address get_module() const { return m_game_module; }
bool is_ready() const { return m_initialized && m_game_data_initialized; }
void run_imgui_frame();
void on_frame_d3d11();
void on_post_present_d3d11();
void on_frame_d3d12();
void on_post_present_d3d12();
void on_reset();
bool on_message(HWND wnd, UINT message, WPARAM w_param, LPARAM l_param);
void on_direct_input_keys(const std::array<uint8_t, 256>& keys);
void save_config();
enum class RendererType : uint8_t {
D3D11,
D3D12
};
auto get_renderer_type() const { return m_renderer_type; }
auto& get_d3d11_hook() const { return m_d3d11_hook; }
auto& get_d3d12_hook() const { return m_d3d12_hook; }
auto get_window() const { return m_wnd; }
auto get_last_window_pos() const { return m_last_window_pos; } // REFramework imgui window
auto get_last_window_size() const { return m_last_window_size; } // REFramework imgui window
static const char* get_game_name() {
#if defined(RE2)
return "re2";
#elif defined(RE3)
return "re3";
#elif defined(RE7)
return "re7";
#elif defined(RE8)
return "re8";
#elif defined(DMC5)
return "dmc5";
#elif defined(MHRISE)
return "mhrise";
#else
return "unknown";
#endif
}
bool is_drawing_ui() const {
return m_draw_ui;
}
void set_draw_ui(bool state, bool should_save = true);
auto& get_hook_monitor_mutex() {
return m_hook_monitor_mutex;
}
void set_font_size(int size) {
if (m_font_size != size) {
m_font_size = size;
m_fonts_need_updating = true;
}
}
auto get_font_size() const { return m_font_size; }
int add_font(const std::filesystem::path& filepath, int size, const std::vector<ImWchar>& ranges = {});
ImFont* get_font(int index) const {
if (index >= 0 && index < m_additional_fonts.size()) {
return m_additional_fonts[index].font;
} else {
return nullptr;
}
}
private:
void consume_input();
void update_fonts();
void invalidate_device_objects();
void draw_ui();
void draw_about();
bool hook_d3d11();
bool hook_d3d12();
bool initialize();
bool initialize_windows_message_hook();
void call_on_frame();
HMODULE m_reframework_module{};
bool m_first_frame{true};
bool m_is_d3d12{false};
bool m_is_d3d11{false};
bool m_valid{false};
bool m_initialized{false};
bool m_created_default_cfg{false};
std::atomic<bool> m_game_data_initialized{false};
// UI
bool m_has_frame{false};
bool m_wants_device_object_cleanup{false};
bool m_draw_ui{true};
bool m_last_draw_ui{m_draw_ui};
bool m_is_ui_focused{false};
bool m_cursor_state{false};
bool m_cursor_state_changed{true};
bool m_ui_option_transparent{true};
bool m_ui_passthrough{false};
ImVec2 m_last_window_pos{};
ImVec2 m_last_window_size{};
struct AdditionalFont {
std::filesystem::path filepath{};
int size{16};
std::vector<ImWchar> ranges{};
ImFont* font{};
};
bool m_fonts_need_updating{true};
int m_font_size{16};
std::vector<AdditionalFont> m_additional_fonts{};
std::mutex m_input_mutex{};
std::recursive_mutex m_config_mtx{};
std::recursive_mutex m_imgui_mtx{};
HWND m_wnd{0};
HMODULE m_game_module{0};
float m_accumulated_mouse_delta[2]{};
float m_mouse_delta[2]{};
std::array<uint8_t, 256> m_last_keys{0};
std::unique_ptr<D3D11Hook> m_d3d11_hook{};
std::unique_ptr<D3D12Hook> m_d3d12_hook{};
std::unique_ptr<WindowsMessageHook> m_windows_message_hook;
std::unique_ptr<DInputHook> m_dinput_hook;
std::shared_ptr<spdlog::logger> m_logger;
std::string m_error{""};
// Game-specific stuff
std::unique_ptr<Mods> m_mods;
std::recursive_mutex m_hook_monitor_mutex{};
std::unique_ptr<std::thread> m_d3d_monitor_thread{};
std::chrono::steady_clock::time_point m_last_present_time{};
std::chrono::steady_clock::time_point m_last_message_time{};
std::chrono::steady_clock::time_point m_last_sendmessage_time{};
std::chrono::steady_clock::time_point m_last_chance_time{};
uint32_t m_frames_since_init{0};
bool m_has_last_chance{true};
bool m_first_initialize{true};
bool m_sent_message{false};
bool m_message_hook_requested{false};
RendererType m_renderer_type{RendererType::D3D11};
template <typename T> using ComPtr = Microsoft::WRL::ComPtr<T>;
private: // D3D misc
void set_imgui_style() noexcept;
private: // D3D11 Init
bool init_d3d11();
void deinit_d3d11();
private: // D3D12 Init
bool init_d3d12();
void deinit_d3d12();
private: // D3D11 members
struct D3D11 {
ComPtr<ID3D11Texture2D> blank_rt{};
ComPtr<ID3D11Texture2D> rt{};
ComPtr<ID3D11RenderTargetView> blank_rt_rtv{};
ComPtr<ID3D11RenderTargetView> rt_rtv{};
ComPtr<ID3D11ShaderResourceView> rt_srv{};
uint32_t rt_width{};
uint32_t rt_height{};
ComPtr<ID3D11RenderTargetView> bb_rtv{};
} m_d3d11{};
public:
auto& get_blank_rendertarget_d3d11() { return m_d3d11.blank_rt; }
auto& get_rendertarget_d3d11() { return m_d3d11.rt; }
auto get_rendertarget_width_d3d11() const { return m_d3d11.rt_width; }
auto get_rendertarget_height_d3d11() const { return m_d3d11.rt_height; }
private: // D3D12 members
struct D3D12 {
ComPtr<ID3D12CommandAllocator> cmd_allocator{};
ComPtr<ID3D12GraphicsCommandList> cmd_list{};
enum class RTV : int{
BACKBUFFER_0,
BACKBUFFER_1,
BACKBUFFER_2,
BACKBUFFER_3,
IMGUI,
BLANK,
COUNT,
};
enum class SRV : int {
IMGUI_FONT,
IMGUI,
BLANK,
COUNT
};
ComPtr<ID3D12DescriptorHeap> rtv_desc_heap{};
ComPtr<ID3D12DescriptorHeap> srv_desc_heap{};
ComPtr<ID3D12Resource> rts[(int)RTV::COUNT]{};
auto& get_rt(RTV rtv) { return rts[(int)rtv]; }
D3D12_CPU_DESCRIPTOR_HANDLE get_cpu_rtv(ID3D12Device* device, RTV rtv) {
return {rtv_desc_heap->GetCPUDescriptorHandleForHeapStart().ptr +
(SIZE_T)rtv * (SIZE_T)device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV)};
}
D3D12_CPU_DESCRIPTOR_HANDLE get_cpu_srv(ID3D12Device* device, SRV srv) {
return {srv_desc_heap->GetCPUDescriptorHandleForHeapStart().ptr +
(SIZE_T)srv * (SIZE_T)device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)};
}
D3D12_GPU_DESCRIPTOR_HANDLE get_gpu_srv(ID3D12Device* device, SRV srv) {
return {srv_desc_heap->GetGPUDescriptorHandleForHeapStart().ptr +
(SIZE_T)srv * (SIZE_T)device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)};
}
uint32_t rt_width{};
uint32_t rt_height{};
} m_d3d12{};
public:
auto& get_blank_rendertarget_d3d12() { return m_d3d12.get_rt(D3D12::RTV::BLANK); }
auto& get_rendertarget_d3d12() { return m_d3d12.get_rt(D3D12::RTV::IMGUI); }
auto get_rendertarget_width_d3d12() { return m_d3d12.rt_width; }
auto get_rendertarget_height_d3d12() { return m_d3d12.rt_height; }
private:
};
extern std::unique_ptr<REFramework> g_framework;