102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
mods.c File Reference
#include "../graphics/graphics_private.h"
#include "mods_private.h"
#include "sidecar_private.h"
#include <dttr_runtime.h>
#include <dttr_config.h>
#include <dttr_errors.h>
#include <dttr_log.h>
#include <dttr_path.h>
#include <kvec.h>
#include <sds.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define MOD_MAX_SHADOW_ATTEMPTS   32
#define MOD_RELOAD_STABLE_MS   500u
#define MOD_RELOAD_POLL_MS   1000u
#define MOD_WITH_OWNER(mod, call)
#define MOD_DISPATCH(field, ...)
#define MOD_OPTIONAL_EXPORTS(X)
#define LOAD_OPTIONAL_EXPORT(field, fn_type, symbol)

Functions

typedef kvec_t (loaded_mod)
static bool file_id_equal (const mod_file_id *lhs, const mod_file_id *rhs)
static mod_file_id make_mod_file_id (const WIN32_FIND_DATAA *find_data)
static bool make_mod_path (sds *out, const char *filename)
static void log_mod_deleted (const loaded_mod *mod)
static bool is_shadow_mod (const char *filename)
static const DTTR_Mods_Infoget_mod_info (DTTR_Mods_InfoFn info_fn)
static void set_mod_display_name (loaded_mod *mod, const DTTR_Mods_Info *info)
static void log_mod_info (const char *filename, const DTTR_Mods_Info *info)
static void destroy_mod_context (loaded_mod *mod)
static bool refresh_mod_context (loaded_mod *mod, const DTTR_Mods_Context *base_ctx)
static void delete_shadow_copy (loaded_mod *mod)
static int find_mod (const char *filename)
static void unload_mod (loaded_mod *mod)
static void remove_mod_at (int index)
static bool make_shadow_path (loaded_mod *mod)
static void load_optional_exports (loaded_mod *mod)
static bool prepare_mod (const char *filename, const char *source_path, const mod_file_id *source_file, loaded_mod *out)
static bool init_mod (loaded_mod *mod)
static bool should_reload_now (loaded_mod *mod, const mod_file_id *source_file, DWORD now_ms)
static void load_mod (const char *filename, const char *source_path, const mod_file_id *source_file)
static bool reload_mod (int index, const char *source_path, const mod_file_id *source_file)
static void remove_missing_mods (const bool *seen, bool initial_scan)
static void remove_all_mods (bool log_deleted)
static bool scan_mod_file (const WIN32_FIND_DATAA *find_data, DWORD now_ms, bool initial_scan, bool *seen)
static void scan_mods (bool initial_scan)
static bool resolve_mods_dir ()
static void attempt_hot_reload_mods ()
void dttr_mods_init ()
void dttr_mods_tick ()
void dttr_mods_late_init ()
void dttr_mods_frame_begin (const DTTR_Mods_FrameContext *ctx)
void dttr_mods_before_game_frame (const DTTR_Mods_FrameContext *ctx)
void dttr_mods_after_game_frame (const DTTR_Mods_FrameContext *ctx)
void dttr_mods_before_present (const DTTR_Mods_PresentContext *ctx)
void dttr_mods_after_present (const DTTR_Mods_PresentContext *ctx)
void dttr_mods_frame_end (const DTTR_Mods_FrameContext *ctx)
void dttr_mods_imgui_begin (const DTTR_Mods_RenderContext *ctx)
void dttr_mods_imgui_end (const DTTR_Mods_RenderContext *ctx)
void dttr_mods_overlay_visible_changed (bool visible)
void dttr_mods_window_created (const DTTR_Mods_WindowContext *ctx)
void dttr_mods_window_resized (const DTTR_Mods_WindowContext *ctx)
void dttr_mods_window_destroying (const DTTR_Mods_WindowContext *ctx)
void dttr_mods_graphics_device_created (const DTTR_Mods_GraphicsContext *ctx)
void dttr_mods_graphics_device_lost (const DTTR_Mods_GraphicsContext *ctx)
void dttr_mods_graphics_device_restored (const DTTR_Mods_GraphicsContext *ctx)
void dttr_mods_graphics_device_destroying (const DTTR_Mods_GraphicsContext *ctx)
static bool dispatch_event_until_consumed (const SDL_Event *event, bool before_event)
bool dttr_mods_before_event (const SDL_Event *event)
void dttr_mods_after_event (const SDL_Event *event, bool consumed)
void dttr_mods_input_mode_changed (const DTTR_Mods_InputContext *ctx)
bool dttr_mods_should_advance_game_frame ()
void dttr_mods_game_frame_advanced ()
void dttr_mods_game_frame_blocked ()
bool dttr_mods_has_render_game ()
void dttr_mods_render_game (const DTTR_Mods_RenderGameContext *ctx)
void dttr_mods_render (const DTTR_Mods_RenderContext *ctx)
bool dttr_mods_handle_event (const SDL_Event *event)
size_t dttr_mods_loaded_count ()
const char * dttr_mods_loaded_name (size_t index)
DWORD dttr_mods_loaded_elapsed_ms (size_t index)
bool dttr_mods_hot_reload_enabled ()
void dttr_mods_cleanup ()

Macro Definition Documentation

◆ LOAD_OPTIONAL_EXPORT

#define LOAD_OPTIONAL_EXPORT ( field,
fn_type,
symbol )
Value:
mod->field = (fn_type)GetProcAddress(mod->handle, symbol);

Referenced by load_optional_exports().

◆ MOD_DISPATCH

◆ MOD_MAX_SHADOW_ATTEMPTS

#define MOD_MAX_SHADOW_ATTEMPTS   32

Definition at line 17 of file mods.c.

Referenced by make_shadow_path().

◆ MOD_OPTIONAL_EXPORTS

#define MOD_OPTIONAL_EXPORTS ( X)

Definition at line 130 of file mods.c.

Referenced by load_optional_exports().

◆ MOD_RELOAD_POLL_MS

#define MOD_RELOAD_POLL_MS   1000u

Definition at line 19 of file mods.c.

Referenced by attempt_hot_reload_mods().

◆ MOD_RELOAD_STABLE_MS

#define MOD_RELOAD_STABLE_MS   500u

Definition at line 18 of file mods.c.

Referenced by should_reload_now().

◆ MOD_WITH_OWNER

#define MOD_WITH_OWNER ( mod,
call )
Value:
do { \
void *previous_owner = DTTR_Core_HookSetOwner((mod)->hook_owner); \
call; \
DTTR_Core_HookSetOwner(previous_owner); \
} while (0)
void * DTTR_Core_HookSetOwner(void *owner)

Definition at line 113 of file mods.c.

Referenced by dispatch_event_until_consumed(), dttr_mods_should_advance_game_frame(), dttr_mods_tick(), and unload_mod().

Function Documentation

◆ attempt_hot_reload_mods()

void attempt_hot_reload_mods ( )
static

Definition at line 578 of file mods.c.

References dttr_config, DWORD, MOD_RELOAD_POLL_MS, and scan_mods().

Referenced by dttr_mods_tick().

◆ delete_shadow_copy()

void delete_shadow_copy ( loaded_mod * mod)
static

Definition at line 173 of file mods.c.

References loaded_mod::shadow_path.

Referenced by prepare_mod(), and unload_mod().

◆ destroy_mod_context()

void destroy_mod_context ( loaded_mod * mod)
static

Definition at line 94 of file mods.c.

References loaded_mod::context, free, and NULL.

Referenced by unload_mod().

◆ dispatch_event_until_consumed()

bool dispatch_event_until_consumed ( const SDL_Event * event,
bool before_event )
static

◆ dttr_mods_after_event()

void dttr_mods_after_event ( const SDL_Event * event,
bool consumed )

Definition at line 708 of file mods.c.

References event, and MOD_DISPATCH.

◆ dttr_mods_after_game_frame()

void dttr_mods_after_game_frame ( const DTTR_Mods_FrameContext * ctx)

Definition at line 630 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_after_present()

void dttr_mods_after_present ( const DTTR_Mods_PresentContext * ctx)

Definition at line 638 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_before_event()

bool dttr_mods_before_event ( const SDL_Event * event)

Definition at line 704 of file mods.c.

References dispatch_event_until_consumed(), and event.

Referenced by dttr_sidecar_handle_sdl_event().

◆ dttr_mods_before_game_frame()

void dttr_mods_before_game_frame ( const DTTR_Mods_FrameContext * ctx)

Definition at line 626 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_before_present()

void dttr_mods_before_present ( const DTTR_Mods_PresentContext * ctx)

Definition at line 634 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_cleanup()

void dttr_mods_cleanup ( )

Definition at line 788 of file mods.c.

References remove_all_mods().

Referenced by cleanup_runtime().

◆ dttr_mods_frame_begin()

void dttr_mods_frame_begin ( const DTTR_Mods_FrameContext * ctx)

Definition at line 622 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_frame_end()

void dttr_mods_frame_end ( const DTTR_Mods_FrameContext * ctx)

Definition at line 642 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_game_frame_advanced()

void dttr_mods_game_frame_advanced ( )

Definition at line 733 of file mods.c.

References MOD_DISPATCH.

Referenced by tick_main_loop().

◆ dttr_mods_game_frame_blocked()

void dttr_mods_game_frame_blocked ( )

Definition at line 737 of file mods.c.

References MOD_DISPATCH.

Referenced by tick_main_loop().

◆ dttr_mods_graphics_device_created()

void dttr_mods_graphics_device_created ( const DTTR_Mods_GraphicsContext * ctx)

Definition at line 670 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_graphics_device_destroying()

void dttr_mods_graphics_device_destroying ( const DTTR_Mods_GraphicsContext * ctx)

Definition at line 682 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_graphics_device_lost()

void dttr_mods_graphics_device_lost ( const DTTR_Mods_GraphicsContext * ctx)

Definition at line 674 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_graphics_device_restored()

void dttr_mods_graphics_device_restored ( const DTTR_Mods_GraphicsContext * ctx)

Definition at line 678 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_handle_event()

bool dttr_mods_handle_event ( const SDL_Event * event)

Definition at line 759 of file mods.c.

References dispatch_event_until_consumed(), and event.

Referenced by dttr_sidecar_handle_sdl_event().

◆ dttr_mods_has_render_game()

bool dttr_mods_has_render_game ( )

Definition at line 741 of file mods.c.

◆ dttr_mods_hot_reload_enabled()

bool dttr_mods_hot_reload_enabled ( )

Definition at line 784 of file mods.c.

References dttr_config.

◆ dttr_mods_imgui_begin()

void dttr_mods_imgui_begin ( const DTTR_Mods_RenderContext * ctx)

Definition at line 646 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_imgui_end()

void dttr_mods_imgui_end ( const DTTR_Mods_RenderContext * ctx)

Definition at line 650 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_init()

void dttr_mods_init ( )

Definition at line 592 of file mods.c.

References DTTR_LOG_INFO, resolve_mods_dir(), and scan_mods().

Referenced by DTTR_Hook_WinMainCallback().

◆ dttr_mods_input_mode_changed()

void dttr_mods_input_mode_changed ( const DTTR_Mods_InputContext * ctx)

Definition at line 712 of file mods.c.

References ctx, and MOD_DISPATCH.

Referenced by DTTR_Hook_WinMainCallback().

◆ dttr_mods_late_init()

void dttr_mods_late_init ( )

Definition at line 618 of file mods.c.

References MOD_DISPATCH.

Referenced by DTTR_Hook_WinMainCallback().

◆ dttr_mods_loaded_count()

size_t dttr_mods_loaded_count ( )

Definition at line 763 of file mods.c.

◆ dttr_mods_loaded_elapsed_ms()

DWORD dttr_mods_loaded_elapsed_ms ( size_t index)

Definition at line 776 of file mods.c.

References DWORD.

◆ dttr_mods_loaded_name()

const char * dttr_mods_loaded_name ( size_t index)

Definition at line 767 of file mods.c.

References loaded_mod::display_name, loaded_mod::filename, and NULL.

◆ dttr_mods_overlay_visible_changed()

void dttr_mods_overlay_visible_changed ( bool visible)

Definition at line 654 of file mods.c.

References MOD_DISPATCH.

Referenced by DTTR_Hook_WinMainCallback().

◆ dttr_mods_render()

void dttr_mods_render ( const DTTR_Mods_RenderContext * ctx)

Definition at line 755 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_render_game()

void dttr_mods_render_game ( const DTTR_Mods_RenderGameContext * ctx)

Definition at line 751 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_should_advance_game_frame()

bool dttr_mods_should_advance_game_frame ( )

Definition at line 716 of file mods.c.

References MOD_WITH_OWNER, and loaded_mod::should_advance_game_frame.

Referenced by tick_main_loop().

◆ dttr_mods_tick()

void dttr_mods_tick ( )

Definition at line 605 of file mods.c.

References attempt_hot_reload_mods(), MOD_WITH_OWNER, and loaded_mod::tick.

Referenced by tick_main_loop().

◆ dttr_mods_window_created()

void dttr_mods_window_created ( const DTTR_Mods_WindowContext * ctx)

Definition at line 658 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_window_destroying()

void dttr_mods_window_destroying ( const DTTR_Mods_WindowContext * ctx)

Definition at line 666 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ dttr_mods_window_resized()

void dttr_mods_window_resized ( const DTTR_Mods_WindowContext * ctx)

Definition at line 662 of file mods.c.

References ctx, and MOD_DISPATCH.

◆ file_id_equal()

bool file_id_equal ( const mod_file_id * lhs,
const mod_file_id * rhs )
static

Definition at line 43 of file mods.c.

References mod_file_id::size_high, mod_file_id::size_low, and mod_file_id::write_time.

Referenced by should_reload_now().

◆ find_mod()

int find_mod ( const char * filename)
static

Definition at line 182 of file mods.c.

Referenced by scan_mod_file().

◆ get_mod_info()

const DTTR_Mods_Info * get_mod_info ( DTTR_Mods_InfoFn info_fn)
static

Definition at line 71 of file mods.c.

References NULL.

Referenced by init_mod().

◆ init_mod()

◆ is_shadow_mod()

bool is_shadow_mod ( const char * filename)
static

Definition at line 66 of file mods.c.

References DTTR_MODS_SHADOW_PREFIX.

Referenced by scan_mod_file().

◆ kvec_t()

◆ load_mod()

void load_mod ( const char * filename,
const char * source_path,
const mod_file_id * source_file )
static

Definition at line 396 of file mods.c.

References DTTR_LOG_INFO, DTTR_LOG_WARN, init_mod(), MODS_MAX, and prepare_mod().

Referenced by scan_mod_file().

◆ load_optional_exports()

void load_optional_exports ( loaded_mod * mod)
static

Definition at line 289 of file mods.c.

References LOAD_OPTIONAL_EXPORT, and MOD_OPTIONAL_EXPORTS.

Referenced by prepare_mod().

◆ log_mod_deleted()

void log_mod_deleted ( const loaded_mod * mod)
static

Definition at line 61 of file mods.c.

References DTTR_LOG_INFO, and loaded_mod::filename.

Referenced by remove_all_mods(), and remove_missing_mods().

◆ log_mod_info()

void log_mod_info ( const char * filename,
const DTTR_Mods_Info * info )
static

Definition at line 80 of file mods.c.

References DTTR_Mods_Info::author, DTTR_LOG_INFO, DTTR_Mods_Info::name, and DTTR_Mods_Info::version.

Referenced by init_mod().

◆ make_mod_file_id()

mod_file_id make_mod_file_id ( const WIN32_FIND_DATAA * find_data)
static

Definition at line 48 of file mods.c.

Referenced by scan_mod_file().

◆ make_mod_path()

bool make_mod_path ( sds * out,
const char * filename )
static

Definition at line 56 of file mods.c.

References DTTR_Path_AppendSegment().

Referenced by make_shadow_path(), scan_mod_file(), and scan_mods().

◆ make_shadow_path()

◆ prepare_mod()

◆ refresh_mod_context()

bool refresh_mod_context ( loaded_mod * mod,
const DTTR_Mods_Context * base_ctx )
static

Definition at line 100 of file mods.c.

References loaded_mod::context, DTTR_LOG_WARN, and loaded_mod::filename.

Referenced by init_mod().

◆ reload_mod()

bool reload_mod ( int index,
const char * source_path,
const mod_file_id * source_file )
static

Definition at line 419 of file mods.c.

References DTTR_LOG_INFO, loaded_mod::filename, init_mod(), prepare_mod(), remove_mod_at(), and unload_mod().

Referenced by scan_mod_file().

◆ remove_all_mods()

void remove_all_mods ( bool log_deleted)
static

Definition at line 454 of file mods.c.

References log_mod_deleted(), and remove_mod_at().

Referenced by dttr_mods_cleanup(), and scan_mods().

◆ remove_missing_mods()

void remove_missing_mods ( const bool * seen,
bool initial_scan )
static

Definition at line 439 of file mods.c.

References log_mod_deleted(), and remove_mod_at().

Referenced by scan_mods().

◆ remove_mod_at()

void remove_mod_at ( int index)
static

Definition at line 225 of file mods.c.

References unload_mod().

Referenced by reload_mod(), remove_all_mods(), and remove_missing_mods().

◆ resolve_mods_dir()

bool resolve_mods_dir ( )
static

Definition at line 558 of file mods.c.

References dttr_loader_dir, DTTR_LOG_INFO, DTTR_Path_AppendSegment(), DTTR_Path_CopySds(), and DWORD.

Referenced by dttr_mods_init().

◆ scan_mod_file()

bool scan_mod_file ( const WIN32_FIND_DATAA * find_data,
DWORD now_ms,
bool initial_scan,
bool * seen )
static

◆ scan_mods()

void scan_mods ( bool initial_scan)
static

◆ set_mod_display_name()

void set_mod_display_name ( loaded_mod * mod,
const DTTR_Mods_Info * info )
static

Definition at line 75 of file mods.c.

References loaded_mod::display_name, DTTR_Path_CopyString(), loaded_mod::filename, and DTTR_Mods_Info::name.

Referenced by init_mod().

◆ should_reload_now()

bool should_reload_now ( loaded_mod * mod,
const mod_file_id * source_file,
DWORD now_ms )
static

◆ unload_mod()