aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Michael Smith <mikesmiffy128@gmail.com> 2024-09-25 18:25:08 +0100
committerGravatar Michael Smith <mikesmiffy128@gmail.com> 2024-09-28 18:28:13 +0100
commitbc9198ba9b654117118a06399d4dbf273262501d (patch)
tree73572d7807797ff22333aeefffae19a708bdd9af
parenta62d7366a6061c1873b87ead944e2521f589b0c7 (diff)
downloadsst-bc9198ba9b654117118a06399d4dbf273262501d.tar.gz
sst-bc9198ba9b654117118a06399d4dbf273262501d.zip
Create and use macros to define accessor functions
Avoids the need to manually mess around with mem_offset() and gamedata off_ and sz_ values as often, because that's kind of annoying. Should also make the codebase a little less confusing for new players.
-rw-r--r--src/accessor.h79
-rw-r--r--src/autojump.c9
-rw-r--r--src/ent.c9
-rw-r--r--src/l4dreset.c15
-rw-r--r--src/l4dwarp.c25
5 files changed, 116 insertions, 21 deletions
diff --git a/src/accessor.h b/src/accessor.h
new file mode 100644
index 0000000..0da1209
--- /dev/null
+++ b/src/accessor.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright © 2024 Michael Smith <mikesmiffy128@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef INC_ACCESSOR_H
+#define INC_ACCESSOR_H
+
+#include "intdefs.h"
+#include "mem.h"
+
+#if defined(__GNUC__) || defined(__clang__)
+#define _ACCESSOR_UNUSED __attribute__((unused))
+#else
+#define _ACCESSOR_UNUSED
+#endif
+
+/*
+ * Defines a function to offset a pointer from a struct/class to a field based
+ * on a corresponding offset value off_<field>. Such an offset would be
+ * generally defined in gamedata. The function will be named getptr_<field>.
+ * Essentially allows easy access to an opaque thing contained with another
+ * opaque thing.
+ */
+#define DEF_PTR_ACCESSOR(type, field) \
+ _ACCESSOR_UNUSED static inline typeof(type) *getptr_##field(void *obj) { \
+ return mem_offset(obj, off_##field); \
+ }
+
+/*
+ * Does the same as DEF_PTR_ACCESSOR, and also adds direct get/get functions.
+ * Requires that the field type is complete - that is, either scalar or a fully
+ * defined struct.
+ */
+#define DEF_ACCESSORS(type, field) \
+ DEF_PTR_ACCESSOR(type, field) \
+ _ACCESSOR_UNUSED static inline typeof(type) get_##field(const void *obj) { \
+ return *getptr_##field((void *)obj); \
+ } \
+ _ACCESSOR_UNUSED static inline void set_##field(const void *obj, \
+ typeof(type) val) { \
+ *getptr_##field((void *)obj) = val; \
+ }
+
+/*
+ * Defines an array indexing function arrayidx_<class> which allows offsetting
+ * an opaque pointer by sz_<class> bytes. This size value would generally be
+ * defined in gamedata. Allows iterating over structs/classes with sizes that
+ * vary by game and are thus unknown at compile time.
+ *
+ * Note that idx is signed so this can also be used for relative pointer offsets
+ * in either direction.
+ *
+ * Note on performance: obviously doing n * size requires a multiplication by a
+ * global, which poses a bit of an optimisation challenge in loops. Based on
+ * some testing though it seems like this *should* usually be optimised into a
+ * single load of the global followed by repeated addition with no need for
+ * multiplication, given that we use LTO, so... don't worry about it! It's fine!
+ */
+#define DEF_ARRAYIDX_ACCESSOR(class) \
+ _ACCESSOR_UNUSED static inline struct class *arrayidx_##class(void *array, \
+ ssize idx) { \
+ return mem_offset(array, idx * sz_##class); \
+ }
+
+#endif
+
+// vi: sw=4 ts=4 noet tw=80 cc=80
diff --git a/src/autojump.c b/src/autojump.c
index 64ed436..bc6739f 100644
--- a/src/autojump.c
+++ b/src/autojump.c
@@ -14,6 +14,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
+#include "accessor.h"
#include "con_.h"
#include "engineapi.h"
#include "errmsg.h"
@@ -32,6 +33,8 @@ REQUIRE_GAMEDATA(off_mv)
REQUIRE_GAMEDATA(vtidx_CheckJumpButton)
REQUIRE_GLOBAL(factory_client) // note: server will never be null
+DEF_ACCESSORS(struct CMoveData *, mv)
+
DEF_CVAR(sst_autojump, "Jump upon hitting the ground while holding space", 0,
CON_REPLICATE | CON_DEMO | CON_HIDDEN)
@@ -44,7 +47,7 @@ typedef bool (*VCALLCONV CheckJumpButton_func)(void *);
static CheckJumpButton_func origsv, origcl;
static bool VCALLCONV hooksv(void *this) {
- struct CMoveData *mv = mem_loadptr(mem_offset(this, off_mv));
+ struct CMoveData *mv = get_mv(this);
int idx = handleidx(mv->playerhandle);
if (con_getvari(sst_autojump) && mv->firstrun && !justjumped[idx]) {
mv->oldbuttons &= ~IN_JUMP;
@@ -55,7 +58,7 @@ static bool VCALLCONV hooksv(void *this) {
}
static bool VCALLCONV hookcl(void *this) {
- struct CMoveData *mv = mem_loadptr(mem_offset(this, off_mv));
+ struct CMoveData *mv = get_mv(this);
// FIXME: this will stutter in the rare case where justjumped is true.
// currently doing clientside justjumped handling makes multiplayer
// prediction in general wrong, so this'll need more work to do totally
@@ -66,7 +69,7 @@ static bool VCALLCONV hookcl(void *this) {
}
static bool unprot(void *gm) {
- void **vtable = *(void ***)gm;
+ void **vtable = mem_loadptr(gm);
bool ret = os_mprot(vtable + vtidx_CheckJumpButton, sizeof(void *),
PAGE_READWRITE);
if (!ret) errmsg_errorsys("couldn't make virtual table writable");
diff --git a/src/ent.c b/src/ent.c
index 1845b1f..2eb8e51 100644
--- a/src/ent.c
+++ b/src/ent.c
@@ -14,6 +14,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
+#include "accessor.h"
#include "con_.h"
#include "dictmaptree.h"
#include "engineapi.h"
@@ -31,12 +32,16 @@
FEATURE()
DECL_VFUNC_DYN(void *, PEntityOfEntIndex, int)
+
+DEF_PTR_ACCESSOR(struct edict *, edicts)
+DEF_ARRAYIDX_ACCESSOR(edict)
+
static struct edict **edicts = 0;
struct edict *ent_getedict(int idx) {
if (edicts) {
// globalvars->edicts seems to be null when disconnected
- if_hot (*edicts) return mem_offset(*edicts, sz_edict * idx);
+ if_hot (*edicts) return arrayidx_edict(*edicts, idx);
return 0;
}
else {
@@ -177,7 +182,7 @@ INIT {
// can just call the function later.
if (has_vtidx_PEntityOfEntIndex) return true;
if (globalvars && has_off_edicts) {
- edicts = mem_offset(globalvars, off_edicts);
+ edicts = getptr_edicts(globalvars);
return true;
}
errmsg_warnx("not implemented for this engine");
diff --git a/src/l4dreset.c b/src/l4dreset.c
index a53b987..9e72f04 100644
--- a/src/l4dreset.c
+++ b/src/l4dreset.c
@@ -19,6 +19,7 @@
#include <string.h>
#include "abi.h"
+#include "accessor.h"
#include "con_.h"
#include "engineapi.h"
#include "ent.h"
@@ -75,9 +76,11 @@ DECL_VFUNC(const char *, SetIssueDetails, 1 + NVDTOR, const char *)
DECL_VFUNC(const char *, GetDisplayString, 8 + NVDTOR)
DECL_VFUNC(const char *, ExecuteCommand, 9 + NVDTOR)
+DEF_PTR_ACCESSOR(struct CUtlVector, voteissues)
+DEF_PTR_ACCESSOR(struct CUtlVector, callerrecords)
+
static struct CVoteIssue *getissue(const char *textkey) {
- struct CUtlVector *issuevec = mem_offset(*votecontroller, off_voteissues);
- struct CVoteIssue **issues = issuevec->m.mem;
+ struct CVoteIssue **issues = getptr_voteissues(*votecontroller)->m.mem;
for (int i = 0; /*i < issuevec->sz*/; ++i) { // key MUST be valid!
if (!strcmp(GetDisplayString(issues[i]), textkey)) return issues[i];
}
@@ -86,12 +89,12 @@ static struct CVoteIssue *getissue(const char *textkey) {
static inline void reset(void) {
// reset the vote cooldowns if possible (will skip L4D1). only necessary on
// versions >2045 and on map 1, but it's easiest to do unconditionally.
- // the way this is written will *hopefully* produce a nice neat lea+cmovne.
- struct CUtlVector *recordvector = mem_offset(*votecontroller,
- off_callerrecords);
// This is equivalent to CUtlVector::RemoveAll() as there's no
// destructors to call. The result as is if nobody had ever voted.
- if_random (off_callerrecords != -1) recordvector->sz = 0;
+ // the way this is written will *hopefully* produce a nice neat lea+cmovne.
+ if_random (off_callerrecords != -1) {
+ getptr_callerrecords(*votecontroller)->sz = 0;
+ }
ExecuteCommand(getissue("#L4D_vote_restart_game"));
}
diff --git a/src/l4dwarp.c b/src/l4dwarp.c
index d5f124b..9c408db 100644
--- a/src/l4dwarp.c
+++ b/src/l4dwarp.c
@@ -18,6 +18,7 @@
#define _USE_MATH_DEFINES // ... windows.
#include <math.h>
+#include "accessor.h"
#include "clientcon.h"
#include "con_.h"
#include "engineapi.h"
@@ -81,16 +82,20 @@ DECL_VFUNC_DYN(void, AddBoxOverlay2, const struct vec3f *,
const struct vec3f *, const struct vec3f *, const struct vec3f *,
const struct rgba *, const struct rgba *, float)
+DEF_ACCESSORS(struct vec3f, entpos)
+DEF_ACCESSORS(struct vec3f, eyeang)
+DEF_ACCESSORS(uint, teamnum)
+DEF_PTR_ACCESSOR(void, collision)
+
static struct vec3f warptarget(void *ent) {
- const struct vec3f *org = mem_offset(ent, off_entpos);
- const struct vec3f *ang = mem_offset(ent, off_eyeang);
+ struct vec3f org = get_entpos(ent), ang = get_eyeang(ent);
// L4D idle warps go up to 10 units behind yaw, lessening based on pitch.
- float pitch = ang->x * M_PI / 180, yaw = ang->y * M_PI / 180;
+ float pitch = ang.x * M_PI / 180, yaw = ang.y * M_PI / 180;
float shift = -10 * cosf(pitch);
return (struct vec3f){
- org->x + shift * cosf(yaw),
- org->y + shift * sinf(yaw),
- org->z
+ org.x + shift * cosf(yaw),
+ org.y + shift * sinf(yaw),
+ org.z
};
}
@@ -112,7 +117,7 @@ DEF_CCMD_HERE_UNREG(sst_l4d_testwarp, "Simulate a bot warping to you "
return;
}
void *e = ed->ent_unknown;
- if_cold (mem_loadu32(mem_offset(e, off_teamnum)) != 2) {
+ if_cold (get_teamnum(e) != 2) {
clientcon_msg(ed, "error: must be in the Survivor team");
return;
}
@@ -128,7 +133,7 @@ DEF_CCMD_HERE_UNREG(sst_l4d_testwarp, "Simulate a bot warping to you "
static const struct rgba
red_edge = {200, 0, 0, 100}, red_face = {220, 0, 0, 10},
- yellow_edge = {240, 200, 20, 100},// yellow_face = {240, 240, 20, 10},
+ yellow_edge = {240, 200, 20, 100},
green_edge = {20, 210, 50, 100}, green_face = {49, 220, 30, 10},
clear_face = {0, 0, 0, 0},
orange_line = {255, 100, 0, 255}, cyan_line = {0, 255, 255, 255};
@@ -175,7 +180,7 @@ DEF_CCMD_HERE_UNREG(sst_l4d_previewwarp, "Visualise bot warp unstuck logic "
return;
}
void *e = ed->ent_unknown;
- if_cold (mem_loadu32(mem_offset(e, off_teamnum)) != 2) {
+ if_cold (get_teamnum(e) != 2) {
clientcon_msg(ed, "error: must be in the Survivor team");
return;
}
@@ -187,7 +192,7 @@ DEF_CCMD_HERE_UNREG(sst_l4d_previewwarp, "Visualise bot warp unstuck logic "
bool success = EntityPlacementTest(e, &stuckpos, &finalpos, false,
PLAYERMASK, &filter, 0.0);
struct vec3f mins = {-16.0f, -16.0f, 0.0f};
- struct vec3f maxs = *OBBMaxs(mem_offset(ed->ent_unknown, off_collision));
+ struct vec3f maxs = *OBBMaxs(getptr_collision(ed->ent_unknown));
struct vec3f step = {maxs.x - mins.x, maxs.y - mins.y, maxs.z - mins.z};
struct failranges { struct { int neg, pos; } x, y, z; } ranges;
if (success) {