Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLOBALS_H

#include "arena.h"
#include "input.h"
#include "mod_manager.h"
#include "sigscan.h"
#include "ui_console.h"
Expand All @@ -15,7 +16,6 @@ struct globals_s {
arena_t perm;
void *module;
bool engine_inited;
bool input_inited;
uint32_t game_thread_id;
str_t game_dir;

Expand Down Expand Up @@ -45,8 +45,9 @@ struct globals_s {
uint64_t hwnd;

/* input */
bool prev_keys_down[INPUT_KEY_MAX];
bool keys_down[INPUT_KEY_MAX];
input_key_state_t keys[INPUT_KEY_MAX];
bool consumed_down[INPUT_KEY_MAX];
float wheel_delta;

/* mod manager */
mod_manager_t mod_manager;
Expand Down
35 changes: 24 additions & 11 deletions include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ enum {
INPUT_KEY_LEFT_ALT,
INPUT_KEY_RIGHT_ALT,

/* keyboard: virtual modifiers (either left or right) */
INPUT_KEY_SHIFT,
INPUT_KEY_CTRL,
INPUT_KEY_ALT,

/* keyboard: common */
INPUT_KEY_SPACE,
INPUT_KEY_ENTER,
Expand Down Expand Up @@ -209,6 +214,13 @@ struct input_event_s {
float analog_value; // ANALOG only (trigger value range: 0.0 .. 1.0, stick value range: -1.0 .. 1.0)
};

typedef struct input_key_state_s input_key_state_t;
struct input_key_state_s {
bool down;
bool pressed;
bool released;
};

#define KEYBIND_MAX_KEYS (10)
#define KEYBIND_NULL (keybind_t){.keys = {0}, .count = 0}

Expand All @@ -227,9 +239,18 @@ input_key_kind_t
input_key_from_str(str_t name);

bool
input_key_is_down(input_key_kind_t kind);
input_key_is_down(input_key_kind_t key);
bool
input_key_is_pressed(input_key_kind_t key);
bool
input_key_was_down(input_key_kind_t kind);
input_key_is_released(input_key_kind_t key);
bool
input_key_is_bindable(input_key_kind_t key);

void
input_key_lost_focus(void);
void
input_key_clear_transient_state(void);

str_t
input_key_to_str(input_key_kind_t key);
Expand Down Expand Up @@ -273,8 +294,6 @@ keybind_clear(keybind_t *bind);
bool
keybind_is_down(keybind_t bind);
bool
keybind_was_down(keybind_t bind);
bool
keybind_is_pressed(keybind_t bind);
bool
keybind_is_released(keybind_t bind);
Expand All @@ -285,12 +304,6 @@ keybind_str_is_down(str_t keybind_str)
return keybind_is_down(keybind_parse(keybind_str, KEYBIND_NULL));
}

static inline bool
keybind_str_was_down(str_t keybind_str)
{
return keybind_was_down(keybind_parse(keybind_str, KEYBIND_NULL));
}

static inline bool
keybind_str_is_pressed(str_t keybind_str)
{
Expand All @@ -305,6 +318,6 @@ keybind_str_is_released(str_t keybind_str)

/* NOTE: used for input events */
bool
input_event_covers_keybind(input_event_t *ev, keybind_t bind);
keybind_activated_by_event(keybind_t bind, input_event_t *ev);

#endif /* INPUT_H */
16 changes: 4 additions & 12 deletions include/mod_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "mod_manager.h"

#define MOD_HOST_ABI_VERSION MAKE_VERSION(1, 0, 1)
#define MOD_HOST_ABI_VERSION MAKE_VERSION(1, 1, 0)

typedef struct host_tmp_arena_s host_tmp_arena_t;
struct host_tmp_arena_s {
Expand Down Expand Up @@ -91,7 +91,8 @@ struct mod_host_api_s {
uint64_t (MOD_CALL *keybind_utf8_len) (keybind_t bind);
uint64_t (MOD_CALL *keybind_utf8_write) (keybind_t bind, void *buf, uint64_t cap);
bool (MOD_CALL *keybind_is_down) (keybind_t bind);
bool (MOD_CALL *keybind_was_down) (keybind_t bind);
bool (MOD_CALL *keybind_is_pressed) (keybind_t bind);
bool (MOD_CALL *keybind_is_released) (keybind_t bind);

/* UOBJECT CREATE/DELETE LISTENERS */

Expand Down Expand Up @@ -119,16 +120,7 @@ struct mod_host_api_s {
uint64_t (MOD_CALL *get_frame_counter)(void);
float (MOD_CALL *get_fps)(void);
};
STATIC_ASSERT(offsetof(mod_host_api_t, map_add) == 0x1e8, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, map_remove) == 0x1f0, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, map_find) == 0x1f8, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, static_load_object) == 0x200, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, static_construct_object) == 0x208, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, add_gameplay_tag) == 0x210, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, check_gameplay_tag_exists) == 0x218, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, get_frame_counter) == 0x220, "invalid host API offset");
STATIC_ASSERT(offsetof(mod_host_api_t, get_fps) == 0x228, "invalid host API offset");
STATIC_ASSERT(sizeof(mod_host_api_t) == 0x230, "invalid host API size");
STATIC_ASSERT(sizeof(mod_host_api_t) == 0x238, "invalid host API size");

const mod_host_api_t *
mod_host_api_get(void);
Expand Down
6 changes: 4 additions & 2 deletions include/signatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

#include "unreal_funcs.inc"

bool
scan_user_module_signatures(void);
void
signatures_init(void);
void
enable_input_hooks(void);

#endif /* SIGNATURES_H */
4 changes: 4 additions & 0 deletions include/unreal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "str.h"
#include "types.h"

#define FNAME_NONE (fname_t){0}

#define FNAME_MAX_BLOCK_BITS 13
#define FNAME_BLOCK_OFFSET_BITS 16
#define FNAME_MAX_BLOCKS (1 << FNAME_MAX_BLOCK_BITS)
Expand Down Expand Up @@ -3128,6 +3130,8 @@ uclass_t *
unreal_uclass_find(str_t name, bool ignore_case, bool exact_match);
uobject_t *
unreal_uobject_find_first_of(uclass_t *cls);
uobject_t *
unreal_uobject_find_first_of_by_name(uclass_t *cls, str_t name, bool ignore_case, bool exact_match);
uclass_t *
unreal_uobject_find_class_by_full_name(str_t full_name);
uobject_t *
Expand Down
4 changes: 3 additions & 1 deletion mod/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "mod_sdk.h"
#include "mod_unreal.h"

static bool
mod_init(const mod_host_api_t *host, mod_t m)
{
mod_sdk_init(host, m);
unreal_cache_objects();

MOD_LOG_INFO("Hello from example mod!!!");
return true;
Expand All @@ -18,4 +20,4 @@ MOD_ENTRY()
};

return &api;
}
}
31 changes: 14 additions & 17 deletions mod/mod_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ enum {
INPUT_KEY_LEFT_ALT,
INPUT_KEY_RIGHT_ALT,

/* keyboard: virtual modifiers (either left or right) */
INPUT_KEY_SHIFT,
INPUT_KEY_CTRL,
INPUT_KEY_ALT,

/* keyboard: common */
INPUT_KEY_SPACE,
INPUT_KEY_ENTER,
Expand Down Expand Up @@ -191,10 +196,10 @@ enum {
INPUT_KEY_MOUSE_WHEEL_DOWN,

/* gamepad: buttons */
INPUT_KEY_GAMEPAD_FACE_BOTTOM, // A / cross
INPUT_KEY_GAMEPAD_FACE_RIGHT, // B / circle
INPUT_KEY_GAMEPAD_FACE_LEFT, // X / square
INPUT_KEY_GAMEPAD_FACE_TOP, // Y / triangle
INPUT_KEY_GAMEPAD_FACE_BOTTOM, // A / cross
INPUT_KEY_GAMEPAD_FACE_RIGHT, // B / circle
INPUT_KEY_GAMEPAD_FACE_LEFT, // X / square
INPUT_KEY_GAMEPAD_FACE_TOP, // Y / triangle
INPUT_KEY_GAMEPAD_LB,
INPUT_KEY_GAMEPAD_RB,
INPUT_KEY_GAMEPAD_LT,
Expand Down Expand Up @@ -240,7 +245,7 @@ struct input_event_s {
#ifdef __cplusplus
# define KEYBIND_NULL (keybind_t){}
#else
# define KEYBIND_NULL (keybind_t){0}
# define KEYBIND_NULL (keybind_t){.keys = {0}, .count = 0}
#endif

typedef struct keybind_s keybind_t;
Expand Down Expand Up @@ -377,7 +382,7 @@ typedef void (MOD_CALL *mod_deinit_fn_t)(mod_t m);

typedef void (MOD_CALL *mod_cmd_fn_t)(mod_t m, str_t name, str_t args, void *user);

#define MOD_ABI_VERSION MAKE_VERSION(1, 0, 1)
#define MOD_ABI_VERSION MAKE_VERSION(1, 1, 0)

typedef struct mod_api_s mod_api_t;
struct mod_api_s {
Expand Down Expand Up @@ -468,7 +473,8 @@ struct mod_host_api_s {
uint64_t (MOD_CALL *keybind_utf8_len) (keybind_t bind);
uint64_t (MOD_CALL *keybind_utf8_write) (keybind_t bind, void *buf, uint64_t cap);
bool (MOD_CALL *keybind_is_down) (keybind_t bind);
bool (MOD_CALL *keybind_was_down) (keybind_t bind);
bool (MOD_CALL *keybind_is_pressed) (keybind_t bind);
bool (MOD_CALL *keybind_is_released) (keybind_t bind);

/* UOBJECT CREATE/DELETE LISTENERS */

Expand Down Expand Up @@ -496,16 +502,7 @@ struct mod_host_api_s {
uint64_t (MOD_CALL *get_frame_counter)(void);
float (MOD_CALL *get_fps)(void);
};
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, map_add) == 0x1e8, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, map_remove) == 0x1f0, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, map_find) == 0x1f8, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, static_load_object) == 0x200, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, static_construct_object) == 0x208, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, add_gameplay_tag) == 0x210, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, check_gameplay_tag_exists) == 0x218, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, get_frame_counter) == 0x220, "invalid host API offset");
MOD_STATIC_ASSERT(offsetof(mod_host_api_t, get_fps) == 0x228, "invalid host API offset");
MOD_STATIC_ASSERT(sizeof(mod_host_api_t) == 0x230, "invalid host API size");
MOD_STATIC_ASSERT(sizeof(mod_host_api_t) == 0x238, "invalid host API size");

#ifdef __cplusplus
}
Expand Down
57 changes: 32 additions & 25 deletions mod/mod_sdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,23 +562,18 @@ mod_keybind_is_down(keybind_t bind)
return (host) ? host->keybind_is_down(bind) : false;
}

bool
mod_keybind_was_down(keybind_t bind)
{
const mod_host_api_t *host = mod_sdk_host();
return (host) ? host->keybind_was_down(bind) : false;
}

bool
mod_keybind_is_pressed(keybind_t bind)
{
return mod_keybind_is_down(bind) && !mod_keybind_was_down(bind);
const mod_host_api_t *host = mod_sdk_host();
return (host) ? host->keybind_is_pressed(bind) : false;
}

bool
mod_keybind_is_released(keybind_t bind)
{
return !mod_keybind_is_down(bind) && mod_keybind_was_down(bind);
const mod_host_api_t *host = mod_sdk_host();
return (host) ? host->keybind_is_released(bind) : false;
}

bool
Expand All @@ -591,16 +586,6 @@ mod_keybind_str_is_down(str_t keybind_str)
return mod_keybind_is_down(bind);
}

bool
mod_keybind_str_was_down(str_t keybind_str)
{
keybind_t bind = {0};
if (!mod_keybind_parse(keybind_str, &bind)) {
return false;
}
return mod_keybind_was_down(bind);
}

bool
mod_keybind_str_is_pressed(str_t keybind_str)
{
Expand All @@ -621,17 +606,39 @@ mod_keybind_str_is_released(str_t keybind_str)
return mod_keybind_is_released(bind);
}

static bool
mod_keybind_key_matches_event(input_key_kind_t bind_key, input_key_kind_t event_key)
{
switch (bind_key) {
case INPUT_KEY_CTRL: return event_key == INPUT_KEY_LEFT_CTRL || event_key == INPUT_KEY_RIGHT_CTRL;
case INPUT_KEY_SHIFT: return event_key == INPUT_KEY_LEFT_SHIFT || event_key == INPUT_KEY_RIGHT_SHIFT;
case INPUT_KEY_ALT: return event_key == INPUT_KEY_LEFT_ALT || event_key == INPUT_KEY_RIGHT_ALT;
default: return bind_key == event_key;
}
}

bool
mod_input_event_covers_keybind(input_event_t *ev, keybind_t bind)
mod_keybind_activated_by_event(keybind_t bind, input_event_t *ev)
{
bool has_event_key = false;
if (ev->kind != INPUT_EVENT_KEY_DOWN &&
ev->kind != INPUT_EVENT_MOUSE_DOWN &&
ev->kind != INPUT_EVENT_MOUSE_DBLCLICK) {
return false;
}

if (ev->is_repeat) {
return false;
}

bool involved = false;
for (int i = 0; i < bind.count; ++i) {
if (bind.keys[i] == ev->key) {
has_event_key = true;
if (mod_keybind_key_matches_event(bind.keys[i], ev->key)) {
involved = true;
break;
}
}
return has_event_key;

return involved && mod_keybind_is_down(bind);
}

/* ========================================== HOOKING / SIGNATURE SCANNING ========================================== */
Expand Down Expand Up @@ -2956,4 +2963,4 @@ ini_split_lines(mod_arena_t arena, str_t text)
mod_scratch_end(tmp);

return out_array;
}
}
8 changes: 1 addition & 7 deletions mod/mod_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,6 @@ mod_keybind_to_str(keybind_t bind, mod_arena_t arena);
/* Returns whether the keybind is down in the current input state. */
bool
mod_keybind_is_down(keybind_t bind);
/* Returns whether the keybind was down in the previous input state. */
bool
mod_keybind_was_down(keybind_t bind);
/* Returns true on the frame a keybind changes from up to down. */
bool
mod_keybind_is_pressed(keybind_t bind);
Expand All @@ -347,9 +344,6 @@ mod_keybind_is_released(keybind_t bind);
/* Parses a keybind string and checks its current state. Invalid strings return false. */
bool
mod_keybind_str_is_down(str_t keybind_str);
/* Parses a keybind string and checks its previous state. Invalid strings return false. */
bool
mod_keybind_str_was_down(str_t keybind_str);
/* Parses a keybind string and checks for an up to down transition. */
bool
mod_keybind_str_is_pressed(str_t keybind_str);
Expand All @@ -358,7 +352,7 @@ bool
mod_keybind_str_is_released(str_t keybind_str);
/* Returns whether the event key appears in the bind. It does not check event kind or other key states. */
bool
mod_input_event_covers_keybind(input_event_t *ev, keybind_t bind);
mod_keybind_activated_by_event(keybind_t bind, input_event_t *ev);

/* ========================================== HOOKING / SIGNATURE SCANNING ========================================== */

Expand Down
Loading
Loading