Augment
Loading...
Searching...
No Matches
augment.hpp
1// Augment Copyright (C) 2026 Liam
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#pragma once
17
18#ifndef AUGMENT_HPP
19#define AUGMENT_HPP
20
21#include <stdint.h>
22#include "augment/augment_export.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* TYPES */
29
33typedef enum AugmentPhase {
39 AUGMENT_PHASE_BEFORE = 0,
40
46 AUGMENT_PHASE_AFTER = 1,
47
53 AUGMENT_PHASE_REPLACE = 2,
54} AugmentPhase;
55
59typedef struct AugmentCtx {
60 void* self;
61 void** args;
62 void* ret;
64 void* user;
67
74typedef void (*AugmentFn)(AugmentCtx* ctx, void* userdata);
75
82typedef struct AugmentContract {
83 const char* const* affects;
85
86 const char* const* reads;
87 int n_reads;
88
89 const char* const* writes;
92
96typedef struct AugmentRegOpts {
98 const char* tag;
99 const char* augment_id;
102
122#define AUGMENT_HOOK(sym, fn) \
123 static struct _AugmentAutoHook_##sym { \
124 _AugmentAutoHook_##sym() { \
125 static const auto _fn = fn; \
126 augment_register( \
127 #sym, \
128 AUGMENT_PHASE_BEFORE, \
129 [](AugmentCtx* ctx, void*) { _fn(ctx); }, \
130 nullptr, nullptr); \
131 } \
132 } _augment_autohook_instance_##sym
133
144AUGMENT_API void augment_register_instance(const char* class_name, void* ptr);
145
154AUGMENT_API void augment_unregister_instance(const char* class_name, void* ptr);
155
165AUGMENT_API void* augment_get_instance(const char* class_name, int index);
166
173AUGMENT_API int augment_instance_count(const char* class_name);
174
175/* API */
176
177using AugmentLogFn = void(*)(const char* tag, const char* msg);
178AUGMENT_API void augment_set_logger(AugmentLogFn fn);
179
180AUGMENT_API void augment_invoke(const char* symbol, AugmentCtx* ctx);
181AUGMENT_API void* augment_before(const char* symbol, AugmentCtx* ctx);
182AUGMENT_API void augment_after(const char* symbol, AugmentCtx* ctx);
183AUGMENT_API int augment_enter(const char* symbol, AugmentCtx* ctx);
184AUGMENT_API int augment_register(
185 const char* symbol,
186 AugmentPhase phase,
187 AugmentFn fn,
188 void* userdata,
189 const AugmentRegOpts* opts // nullable, all fields optional
190);
191AUGMENT_API void augment_register_ptr(
192 const char* symbol,
193 void* target_ptr
194);
195
196AUGMENT_API void augment_unregister(const char* augment_id);
197AUGMENT_API void augment_install_all(void);
198AUGMENT_API void augment_clear(void);
199AUGMENT_API const char* augment_inspect(const char* symbol);
200AUGMENT_API void* augment_resolve(const char* symbol);
201AUGMENT_API int augment_self_test(void);
202
203#if AUGMENT_FFI
204AUGMENT_API void augment_register_signature(const char* symbol, int is_member,
205 const char* rtype, const char** atypes,
206 unsigned nargs);
207AUGMENT_API void augment_register_struct(const char* name, const char** member_kinds,
208 unsigned n);
209AUGMENT_API int augment_load_signatures(const char* path);
210AUGMENT_API int augment_field_offset(const char* field);
211AUGMENT_API void* augment_make_closure(const char* symbol);
212AUGMENT_API int augment_call(const char* symbol, void** args, unsigned nargs, void* ret_out, int instance_index = -1);
213#else
214inline void augment_register_signature(const char*, int, const char*, const char**, unsigned) {}
215inline void augment_register_struct(const char*, const char**, unsigned) {}
216inline int augment_load_signatures(const char*) { return 0; }
217inline int augment_field_offset(const char*) { return -1; }
218inline void* augment_make_closure(const char*) { return nullptr; }
219inline int augment_call(const char*, void**, unsigned, void*, int = -1) { return 0; }
220#endif
221
222typedef struct AugmentArg { const char* name; const char* kind; const char* view; } AugmentArg;
223typedef struct AugmentField { const char* name; unsigned offset; const char* kind; int len; const char* view; } AugmentField;
224
225AUGMENT_API int augment_manifest_load(const char* path);
226
227AUGMENT_API int augment_fn_count(const char* flat);
228AUGMENT_API const char* augment_fn_mangled(const char* flat, int i);
229AUGMENT_API const char* augment_fn_loc(const char* flat, int i);
230AUGMENT_API const char* augment_resolve_sig(const char* flat, const char* sig);
231
232AUGMENT_API int augment_fn_params(const char* mangled, const AugmentArg** out);
233AUGMENT_API int augment_struct_fields(const char* name, const AugmentField** out);
234typedef struct AugmentEnumVal { const char* name; long long value; } AugmentEnumVal;
235AUGMENT_API int augment_enum_values(const char* name, const AugmentEnumVal** out);
236AUGMENT_API int augment_global_addr(const char* name, const char** kind_out, void** addr_out);
237
238AUGMENT_API const char* augment_fn_self_view(const char* mangled);
239AUGMENT_API const char* augment_fn_ret(const char* mangled);
240
241AUGMENT_API void augment_mem_read(void* base, int offset, const char* kind, void* out);
242AUGMENT_API void augment_mem_write(void* base, int offset, const char* kind, const void* in);
243AUGMENT_API int augment_mem_read_str(void* base, int offset, int cap, char* out);
244AUGMENT_API void augment_mem_write_str(void* base, int offset, int cap, const char* s);
245
246#ifdef __cplusplus
247}
248#endif
249
250#endif /* AUGMENT_HPP */
Definition augment.hpp:222
Declares what a hook touches, for automated conflict detection.
Definition augment.hpp:82
const char *const * writes
Symbols/fields this hook writes.
Definition augment.hpp:89
int n_reads
Number of entries in reads.
Definition augment.hpp:87
int n_writes
Number of entries in writes.
Definition augment.hpp:90
int n_affects
Number of entries in affects.
Definition augment.hpp:84
const char *const * reads
Symbols/fields this hook reads.
Definition augment.hpp:86
const char *const * affects
Symbols/fields this hook affects (side effects).
Definition augment.hpp:83
Context passed to a hooked function's callback.
Definition augment.hpp:59
void * self
this pointer
Definition augment.hpp:60
int arg_count
number of arguments
Definition augment.hpp:65
int cancelled
set nonzero in before to skip original
Definition augment.hpp:63
void ** args
argument array, index 0 = first param
Definition augment.hpp:61
void * user
internal, do not touch
Definition augment.hpp:64
void * ret
return value slot
Definition augment.hpp:62
Definition augment.hpp:234
Definition augment.hpp:223
Options used when registering a hook.
Definition augment.hpp:96
const char * tag
Human-readable label for logs / debug.
Definition augment.hpp:98
AugmentContract contract
Declared read/write/affects surface, for conflict detection.
Definition augment.hpp:100
int priority
Execution order relative to other hooks (lower runs first).
Definition augment.hpp:97
const char * augment_id
Unique identifier for this hook registration.
Definition augment.hpp:99