102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
dttr_mods.h
Go to the documentation of this file.
1
6
7#ifndef DTTR_MODS_H
8#define DTTR_MODS_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13
14#include <windows.h>
15
16#include <dttr_runtime.h>
17
18typedef struct SDL_Window SDL_Window;
19typedef union SDL_Event SDL_Event;
20
21#define DTTR_MODS_EXCEPTION_REPORT_STACK_TRACE_CAPACITY 16384u
22
23typedef struct {
24 uint32_t struct_size;
25 EXCEPTION_RECORD exception_record;
26 CONTEXT context;
28 const char *tag;
30
39
43);
44
46 int level,
47 const char *file,
48 int line,
49 const char *fmt,
50 ...
51);
52typedef bool (*DTTR_Mods_LogIsEnabledFn)(int level);
62
64 const DTTR_Mods_API *api
65) {
66 if (!api || api->abi_version < DTTR_SDK_ABI_VERSION
67 || api->struct_size < offsetof(DTTR_Mods_API, write_exception_report)
68 + sizeof(api->write_exception_report)) {
69 return NULL;
70 }
71
72 return api->write_exception_report;
73}
74
75// Host context passed to DTTR_Mod_Init. The pointer is valid until DTTR_Mod_Cleanup
76// returns, so mods may retain it for logging and runtime cleanup. Contained
77// window/graphics resources have shorter host lifetimes and may also outlive a
78// hot-reloaded mod: use destroying callbacks for host teardown/device loss, and
79// cleanup for resources owned by one mod instance.
80typedef struct {
81 uint32_t abi_version;
85 const char *loader_dir;
86 const char *exe_hash;
87 const void *config;
89 uint32_t struct_size;
90 uint32_t flags;
91 const void *reserved[4];
93
94typedef struct {
95 const char *name;
96 const char *version;
97 const char *author;
99
100typedef bool (*DTTR_Mods_InitFn)(const DTTR_Mods_Context *ctx);
103typedef bool (*DTTR_Mods_EventFn)(const SDL_Event *event);
104typedef const DTTR_Mods_Info *(*DTTR_Mods_InfoFn)();
107
108typedef struct {
109 uint64_t frame_index;
110 uint32_t window_w;
111 uint32_t window_h;
112 uint32_t game_x;
113 uint32_t game_y;
114 uint32_t game_w;
115 uint32_t game_h;
116 float scale;
118
119typedef struct {
120 uint64_t frame_index;
121 uint32_t window_w;
122 uint32_t window_h;
123 uint32_t game_x;
124 uint32_t game_y;
125 uint32_t game_w;
126 uint32_t game_h;
127 float scale;
131
132typedef struct {
134 HWND hwnd;
135 uint32_t window_w;
136 uint32_t window_h;
138
144
153
158
159typedef struct {
160 uint32_t width;
161 uint32_t height;
162 float scale;
164
165typedef struct {
166 uint32_t window_w;
167 uint32_t window_h;
168 uint32_t game_x;
169 uint32_t game_y;
170 uint32_t game_w;
171 uint32_t game_h;
172 float scale;
174
177
195typedef void (*DTTR_Mods_AfterEventFn)(const SDL_Event *event, bool consumed);
197
201
202// Logging macros.
203
204#define DTTR_MODS_LOG_LVL_TRACE 0
205#define DTTR_MODS_LOG_LVL_DEBUG 1
206#define DTTR_MODS_LOG_LVL_INFO 2
207#define DTTR_MODS_LOG_LVL_WARN 3
208#define DTTR_MODS_LOG_LVL_ERROR 4
209#define DTTR_MODS_LOG_LVL_FATAL 5
210
211#define DTTR_MODS_LOG(ctx, level, ...) \
212 do { \
213 if ((ctx)->api->log_is_enabled(level)) { \
214 (ctx)->api->log_unchecked(level, __FILE__, __LINE__, __VA_ARGS__); \
215 } \
216 } while (0)
217#define DTTR_MODS_LOG_TRACE(ctx, ...) \
218 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_TRACE, __VA_ARGS__)
219#define DTTR_MODS_LOG_DEBUG(ctx, ...) \
220 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_DEBUG, __VA_ARGS__)
221#define DTTR_MODS_LOG_INFO(ctx, ...) \
222 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_INFO, __VA_ARGS__)
223#define DTTR_MODS_LOG_WARN(ctx, ...) \
224 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_WARN, __VA_ARGS__)
225#define DTTR_MODS_LOG_ERROR(ctx, ...) \
226 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_ERROR, __VA_ARGS__)
227#define DTTR_MODS_LOG_FATAL(ctx, ...) \
228 DTTR_MODS_LOG(ctx, DTTR_MODS_LOG_LVL_FATAL, __VA_ARGS__)
229
230// Mod export macros.
231
232#ifdef __cplusplus
233#define DTTR_EXPORT extern "C" __declspec(dllexport)
234#else
235#define DTTR_EXPORT __declspec(dllexport)
236#endif
237
238#define DTTR_MODS_INFO(mod_name, mod_version, mod_author) \
239 static const DTTR_Mods_Info dttr_mod_info = { \
240 mod_name, \
241 mod_version, \
242 mod_author, \
243 }; \
244 DTTR_EXPORT const DTTR_Mods_Info *DTTR_Mod_Info() { \
245 return &dttr_mod_info; \
246 }
247
249 return ctx && ctx->abi_version >= DTTR_SDK_ABI_VERSION
250 && ctx->struct_size >= sizeof(DTTR_Mods_Context);
251}
252
253// Check SDK ABI compatibility and delegate to the mod body.
254#define DTTR_MODS_INIT \
255 static bool dttr_mod_init(const DTTR_Mods_Context *); \
256 DTTR_EXPORT bool DTTR_Mod_Init(const DTTR_Mods_Context *ctx) { \
257 if (!DTTR_Mods_ContextIsCompatible(ctx)) { \
258 return false; \
259 } \
260 return dttr_mod_init(ctx); \
261 } \
262 static bool dttr_mod_init(const DTTR_Mods_Context *ctx)
263
264#define DTTR_MODS_CLEANUP DTTR_EXPORT void DTTR_Mod_Cleanup()
265#define DTTR_MODS_TICK DTTR_EXPORT void DTTR_Mod_Tick()
266#define DTTR_MODS_LATE_INIT DTTR_EXPORT void DTTR_Mod_LateInit()
267#define DTTR_MODS_BEFORE_UNLOAD DTTR_EXPORT void DTTR_Mod_BeforeUnload()
268
269// Called at the start of a host frame before game-frame advancement and presentation.
270// The context pointer is callback-local; copy values you need after return.
271#define DTTR_MODS_FRAME_BEGIN \
272 DTTR_EXPORT void DTTR_Mod_FrameBegin(const DTTR_Mods_FrameContext *ctx)
273
274// Called from the render backend immediately before game image submission for a host
275// frame. This currently runs even when DTTR_MODS_SHOULD_ADVANCE_GAME_FRAME blocked
276// simulation; use DTTR_MODS_GAME_FRAME_ADVANCED for strictly advanced-frame work.
277#define DTTR_MODS_BEFORE_GAME_FRAME \
278 DTTR_EXPORT void DTTR_Mod_BeforeGameFrame(const DTTR_Mods_FrameContext *ctx)
279
280// Called from the render backend after game image submission for a host frame. This
281// also runs on blocked presentation frames; use DTTR_MODS_GAME_FRAME_ADVANCED for
282// strictly advanced-frame work.
283#define DTTR_MODS_AFTER_GAME_FRAME \
284 DTTR_EXPORT void DTTR_Mod_AfterGameFrame(const DTTR_Mods_FrameContext *ctx)
285
286// Called after the backend has queued game/overlay draw or blit work and
287// immediately before command-buffer submit or buffer swap. This can run even when
288// DTTR_MODS_SHOULD_ADVANCE_GAME_FRAME blocked simulation.
289#define DTTR_MODS_BEFORE_PRESENT \
290 DTTR_EXPORT void DTTR_Mod_BeforePresent(const DTTR_Mods_PresentContext *ctx)
291
292// Called after presentation work for this host frame.
293#define DTTR_MODS_AFTER_PRESENT \
294 DTTR_EXPORT void DTTR_Mod_AfterPresent(const DTTR_Mods_PresentContext *ctx)
295
296// Called at the end of a host frame after game-frame and presentation callbacks.
297#define DTTR_MODS_FRAME_END \
298 DTTR_EXPORT void DTTR_Mod_FrameEnd(const DTTR_Mods_FrameContext *ctx)
299
300#define DTTR_MODS_IMGUI_BEGIN \
301 DTTR_EXPORT void DTTR_Mod_ImGuiBegin(const DTTR_Mods_RenderContext *ctx)
302
303#define DTTR_MODS_IMGUI_END \
304 DTTR_EXPORT void DTTR_Mod_ImGuiEnd(const DTTR_Mods_RenderContext *ctx)
305
306#define DTTR_MODS_OVERLAY_VISIBLE_CHANGED \
307 DTTR_EXPORT void DTTR_Mod_OverlayVisibleChanged(bool visible)
308
309#define DTTR_MODS_WINDOW_CREATED \
310 DTTR_EXPORT void DTTR_Mod_WindowCreated(const DTTR_Mods_WindowContext *ctx)
311
312#define DTTR_MODS_WINDOW_RESIZED \
313 DTTR_EXPORT void DTTR_Mod_WindowResized(const DTTR_Mods_WindowContext *ctx)
314
315#define DTTR_MODS_WINDOW_DESTROYING \
316 DTTR_EXPORT void DTTR_Mod_WindowDestroying(const DTTR_Mods_WindowContext *ctx)
317
318#define DTTR_MODS_GRAPHICS_DEVICE_CREATED \
319 DTTR_EXPORT void DTTR_Mod_GraphicsDeviceCreated(const DTTR_Mods_GraphicsContext *ctx)
320
321#define DTTR_MODS_GRAPHICS_DEVICE_LOST \
322 DTTR_EXPORT void DTTR_Mod_GraphicsDeviceLost(const DTTR_Mods_GraphicsContext *ctx)
323
324#define DTTR_MODS_GRAPHICS_DEVICE_RESTORED \
325 DTTR_EXPORT void DTTR_Mod_GraphicsDeviceRestored(const DTTR_Mods_GraphicsContext *ctx)
326
327#define DTTR_MODS_GRAPHICS_DEVICE_DESTROYING \
328 DTTR_EXPORT void DTTR_Mod_GraphicsDeviceDestroying( \
329 const DTTR_Mods_GraphicsContext *ctx \
330 )
331
332#define DTTR_MODS_BEFORE_EVENT \
333 DTTR_EXPORT bool DTTR_Mod_BeforeEvent(const SDL_Event *event)
334
335#define DTTR_MODS_AFTER_EVENT \
336 DTTR_EXPORT void DTTR_Mod_AfterEvent(const SDL_Event *event, bool consumed)
337
338#define DTTR_MODS_INPUT_MODE_CHANGED \
339 DTTR_EXPORT void DTTR_Mod_InputModeChanged(const DTTR_Mods_InputContext *ctx)
340
341// Return true to consume the event.
342#define DTTR_MODS_EVENT DTTR_EXPORT bool DTTR_Mod_Event(const SDL_Event *event)
343
344// Render at game resolution, letterboxed and scaled with the game image.
345#define DTTR_MODS_RENDER_GAME \
346 DTTR_EXPORT void DTTR_Mod_RenderGame(const DTTR_Mods_RenderGameContext *ctx)
347
348// Render at full window resolution, above letterbox bars.
349#define DTTR_MODS_RENDER \
350 DTTR_EXPORT void DTTR_Mod_Render(const DTTR_Mods_RenderContext *ctx)
351
352// Return false to skip this host-loop game frame while still presenting overlays.
353#define DTTR_MODS_SHOULD_ADVANCE_GAME_FRAME \
354 DTTR_EXPORT bool DTTR_Mod_ShouldAdvanceGameFrame()
355
356// Called after a game frame was advanced because all mods allowed it.
357#define DTTR_MODS_GAME_FRAME_ADVANCED DTTR_EXPORT void DTTR_Mod_GameFrameAdvanced()
358
359// Called after a host frame presented overlays without advancing the game.
360#define DTTR_MODS_GAME_FRAME_BLOCKED DTTR_EXPORT void DTTR_Mod_GameFrameBlocked()
361
362#endif // DTTR_MODS_H
void void * ctx
void void DWORD HANDLE event
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
void(* DTTR_Mods_WindowResizedFn)(const DTTR_Mods_WindowContext *ctx)
Definition dttr_mods.h:188
void(* DTTR_Mods_ImGuiBeginFn)(const DTTR_Mods_RenderContext *ctx)
Definition dttr_mods.h:184
void(* DTTR_Mods_AfterEventFn)(const SDL_Event *event, bool consumed)
Definition dttr_mods.h:195
void(* DTTR_Mods_GraphicsDeviceDestroyingFn)(const DTTR_Mods_GraphicsContext *ctx)
Definition dttr_mods.h:193
void(* DTTR_Mods_GraphicsDeviceLostFn)(const DTTR_Mods_GraphicsContext *ctx)
Definition dttr_mods.h:191
void(* DTTR_Mods_CleanupFn)()
Definition dttr_mods.h:101
union SDL_Event SDL_Event
Definition dttr_mods.h:19
void(* DTTR_Mods_BeforePresentFn)(const DTTR_Mods_PresentContext *ctx)
Definition dttr_mods.h:181
struct SDL_Window SDL_Window
Definition dttr_mods.h:18
void(* DTTR_Mods_FrameBeginFn)(const DTTR_Mods_FrameContext *ctx)
Definition dttr_mods.h:178
bool(* DTTR_Mods_WriteExceptionReportFn)(const DTTR_Mods_ExceptionReportRequest *request, DTTR_Mods_ExceptionReport *report)
Definition dttr_mods.h:40
void(* DTTR_Mods_GraphicsDeviceCreatedFn)(const DTTR_Mods_GraphicsContext *ctx)
Definition dttr_mods.h:190
#define DTTR_MODS_EXCEPTION_REPORT_STACK_TRACE_CAPACITY
Definition dttr_mods.h:21
void(* DTTR_Mods_BeforeGameFrameFn)(const DTTR_Mods_FrameContext *ctx)
Definition dttr_mods.h:179
bool(* DTTR_Mods_EventFn)(const SDL_Event *event)
Definition dttr_mods.h:103
bool(* DTTR_Mods_InitFn)(const DTTR_Mods_Context *ctx)
Definition dttr_mods.h:100
void(* DTTR_Mods_AfterGameFrameFn)(const DTTR_Mods_FrameContext *ctx)
Definition dttr_mods.h:180
void(* DTTR_Mods_TickFn)()
Definition dttr_mods.h:102
void(* DTTR_Mods_ImGuiEndFn)(const DTTR_Mods_RenderContext *ctx)
Definition dttr_mods.h:185
void(* DTTR_Mods_BeforeUnloadFn)()
Definition dttr_mods.h:106
void(* DTTR_Mods_InputModeChangedFn)(const DTTR_Mods_InputContext *ctx)
Definition dttr_mods.h:196
void(* DTTR_Mods_OverlayVisibleChangedFn)(bool visible)
Definition dttr_mods.h:186
void(* DTTR_Mods_GameFrameAdvancedFn)()
Definition dttr_mods.h:199
DTTR_Mods_GraphicsBackend
Definition dttr_mods.h:139
@ DTTR_MODS_GRAPHICS_BACKEND_UNKNOWN
Definition dttr_mods.h:140
@ DTTR_MODS_GRAPHICS_BACKEND_SDL_GPU
Definition dttr_mods.h:141
@ DTTR_MODS_GRAPHICS_BACKEND_OPENGL
Definition dttr_mods.h:142
bool(* DTTR_Mods_BeforeEventFn)(const SDL_Event *event)
Definition dttr_mods.h:194
static bool DTTR_Mods_ContextIsCompatible(const DTTR_Mods_Context *ctx)
Definition dttr_mods.h:248
void(* DTTR_Mods_AfterPresentFn)(const DTTR_Mods_PresentContext *ctx)
Definition dttr_mods.h:182
void(* DTTR_Mods_GraphicsDeviceRestoredFn)(const DTTR_Mods_GraphicsContext *ctx)
Definition dttr_mods.h:192
void(* DTTR_Mods_WindowCreatedFn)(const DTTR_Mods_WindowContext *ctx)
Definition dttr_mods.h:187
bool(* DTTR_Mods_ShouldAdvanceGameFrameFn)()
Definition dttr_mods.h:198
void(* DTTR_Mods_FrameEndFn)(const DTTR_Mods_FrameContext *ctx)
Definition dttr_mods.h:183
void(* DTTR_Mods_RenderFn)(const DTTR_Mods_RenderContext *ctx)
Definition dttr_mods.h:176
static DTTR_Mods_WriteExceptionReportFn DTTR_Mods_GetWriteExceptionReportFn(const DTTR_Mods_API *api)
Definition dttr_mods.h:63
void(* DTTR_Mods_LateInitFn)()
Definition dttr_mods.h:105
void(* DTTR_Mods_GameFrameBlockedFn)()
Definition dttr_mods.h:200
void(* DTTR_Mods_WindowDestroyingFn)(const DTTR_Mods_WindowContext *ctx)
Definition dttr_mods.h:189
bool(* DTTR_Mods_LogIsEnabledFn)(int level)
Definition dttr_mods.h:52
void(* DTTR_Mods_LogFn)(int level, const char *file, int line, const char *fmt,...)
Definition dttr_mods.h:45
void(* DTTR_Mods_RenderGameFn)(const DTTR_Mods_RenderGameContext *ctx)
Definition dttr_mods.h:175
#define DTTR_SDK_ABI_VERSION
uint32_t flags
Definition dttr_mods.h:59
uint32_t abi_version
Definition dttr_mods.h:58
uint32_t struct_size
Definition dttr_mods.h:57
DTTR_Mods_LogFn log_unchecked
Definition dttr_mods.h:56
DTTR_Mods_LogFn log
Definition dttr_mods.h:54
DTTR_Mods_LogIsEnabledFn log_is_enabled
Definition dttr_mods.h:55
DTTR_Mods_WriteExceptionReportFn write_exception_report
Definition dttr_mods.h:60
SDL_Window * window
Definition dttr_mods.h:84
HMODULE sidecar_module
Definition dttr_mods.h:83
const char * exe_hash
Definition dttr_mods.h:86
const void * reserved[4]
Definition dttr_mods.h:91
uint32_t struct_size
Definition dttr_mods.h:89
const void * config
Definition dttr_mods.h:87
uint32_t abi_version
Definition dttr_mods.h:81
const char * loader_dir
Definition dttr_mods.h:85
DTTR_Core_Context runtime
Definition dttr_mods.h:82
const DTTR_Mods_API * api
Definition dttr_mods.h:88
EXCEPTION_RECORD exception_record
Definition dttr_mods.h:25
char dump_path[MAX_PATH]
Definition dttr_mods.h:35
char stack_trace[DTTR_MODS_EXCEPTION_REPORT_STACK_TRACE_CAPACITY]
Definition dttr_mods.h:36
DTTR_Mods_GraphicsBackend backend
Definition dttr_mods.h:148
const char * author
Definition dttr_mods.h:97
const char * version
Definition dttr_mods.h:96
const char * name
Definition dttr_mods.h:95