summaryrefslogtreecommitdiffstats
path: root/src/hl2h.cpp
blob: 0261ff2f89c173646d3e7d4ca54ea7ba2ed0d4ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// hl2h.cpp — core: logging, vector math, globals, feature logic, setup
#include "hl2h.h"
#include <cmath>
#include <cstdarg>

// ---------------------------------------------------------------------------
// logging
// ---------------------------------------------------------------------------

void logf(const char *fmt, ...) {
  va_list ap, ap2;
  va_start(ap, fmt);
  va_copy(ap2, ap);
  fprintf(stderr, "hl2hake: ");
  vfprintf(stderr, fmt, ap2);
  fprintf(stderr, "\n");
  fflush(stderr);
  va_end(ap2);
  if (FILE *f = fopen("/tmp/hl2hake.log", "a")) {
    fprintf(f, "hl2hake: ");
    vfprintf(f, fmt, ap);
    fprintf(f, "\n");
    fclose(f);
  }
  va_end(ap);
}

// ---------------------------------------------------------------------------
// globals
// ---------------------------------------------------------------------------

Module g_client, g_engine;
GameInterfaces g_iface;
NetVars g_net;
uintptr_t g_input = 0;

// ---------------------------------------------------------------------------
// vector math
// ---------------------------------------------------------------------------

static constexpr float PI_F = 3.14159265358979323846f;

QAngle vector_angles(const Vector &forward) {
  QAngle a{};
  a.z = 0;
  if (forward.y == 0 && forward.x == 0) {
    a.y = 0;
    a.x = (forward.z > 0) ? -90.f : 90.f;
  } else {
    a.y = atan2f(forward.y, forward.x) * 180.f / PI_F;
    if (a.y < 0)
      a.y += 360.f;
    a.x = atan2f(-forward.z,
                  sqrtf(forward.x * forward.x + forward.y * forward.y)) *
          180.f / PI_F;
    if (a.x < 0)
      a.x += 360.f;
  }
  return a;
}

Vector angle_forward_vector(const QAngle &angles) {
  auto DEG2RAD = [](float x) { return x * (PI_F / 180.f); };
  float sp = sinf(DEG2RAD(angles.x)), cp = cosf(DEG2RAD(angles.x));
  float sy = sinf(DEG2RAD(angles.y)), cy = cosf(DEG2RAD(angles.y));
  return {cp * cy, cp * sy, -sp};
}

float getfov(const QAngle &my_view, const QAngle &aim_ang) {
  Vector aim = angle_forward_vector(my_view);
  Vector ang = angle_forward_vector(aim_ang);
  float len = sqrtf(aim.x * aim.x + aim.y * aim.y + aim.z * aim.z);
  if (len <= 0)
    return 0;
  float dot = (aim.x * ang.x + aim.y * ang.y + aim.z * ang.z) / len;
  if (dot > 1)
    dot = 1;
  if (dot < -1)
    dot = -1;
  return acosf(dot) * (180.f / PI_F);
}

// ---------------------------------------------------------------------------
// feature logic (bhop, ABH, climb)
// ---------------------------------------------------------------------------

static int g_modified_logged = 0;

void note_modified(const CUserCmd *cmd, int flags, const float *vel) {
  // recompute usercmd CRC after modification
  fix_verified_crc(cmd->command_number, usercmd_checksum(cmd));

  if (g_modified_logged < 5) {
    logf("cmd modified: seq=%d flags=0x%X buttons=0x%X fw=%.2f "
         "vel=(%.1f,%.1f,%.1f)",
         cmd->command_number, flags, cmd->buttons, cmd->forwardmove, vel[0],
         vel[1], vel[2]);
    g_modified_logged++;
  }
}

// menu state (defined in menu.cpp, referenced here)

static bool g_logged_local = false;

static int engine_get_local_player() {
  using Fn = int (*)(void *);
  return vfunc<Fn>(g_iface.engine, 12)(g_iface.engine);
}

static void *entitylist_get(int idx) {
  using Fn = void *(*)(void *, int);
  return vfunc<Fn>(g_iface.entitylist, 3)(g_iface.entitylist, idx);
}




static void do_move(int seq) {
  if (!g_input || !g_net.ok())
    return;

  int localIdx = engine_get_local_player();
  if (localIdx <= 0 || localIdx > 64)
    return;
  if (!g_logged_local) {
    logf("local player index %d (in-game)", localIdx);
    g_logged_local = true;
  }

  void *ent = entitylist_get(localIdx);
  if (!ent)
    return;

  CUserCmd *cmd = get_usercmd(seq);
  if (!cmd)
    return;

  uintptr_t e = (uintptr_t)ent;
  uint8_t movetype = *(uint8_t *)(e + g_net.movetype);
  int flags = *(int *)(e + g_net.flags);
  bool onground = (flags & FL_ONGROUND) != 0;
  bool inwater = (flags & FL_INWATER) != 0;

  if (movetype == MOVETYPE_NOCLIP || movetype == MOVETYPE_LADDER)
    return;

  static bool lastground = false;

  if (middle_mouse_down()) {
    if (cmd->command_number % 2) {
      cmd->buttons &= ~IN_JUMP;
      cmd->buttons |= IN_USE;
    } else {
      cmd->buttons &= ~IN_USE;
      cmd->buttons |= IN_JUMP;
    }
    note_modified(cmd, flags, (float *)(e + g_net.velocity));
  } else if (cmd->buttons & IN_JUMP) {
    if (inwater && (cmd->buttons & IN_DUCK)) {
      lastground = onground;
      return;
    }
    const float *vel = (const float *)(e + g_net.velocity);
    float speed = sqrtf(vel[0] * vel[0] + vel[1] * vel[1] + vel[2] * vel[2]);
    bool moving = speed > 0.1f;
    bool backmove = false;
    if (moving) {
      QAngle moveang = vector_angles(Vector{vel[0], vel[1], vel[2]});
      moveang.x = 0.f;
      QAngle viewang{cmd->viewangles[0], cmd->viewangles[1],
                     cmd->viewangles[2]};
      viewang.x = 0.f;
      backmove = getfov(viewang, moveang) > 90.f;
    }
    if (!onground) {
      lastground = false;
      cmd->buttons &= ~IN_JUMP;
      note_modified(cmd, flags, vel);
    } else if (!lastground) {
      lastground = true;
      if (!backmove)
        cmd->forwardmove = -0.1f;
      note_modified(cmd, flags, vel);
    }
  }
}

// ---------------------------------------------------------------------------
// CreateMove hook (cdecl — game virtuals use cdecl, not __thiscall)
// ---------------------------------------------------------------------------

using CreateMoveFn = bool (*)(void *, int, float, bool);
static CreateMoveFn oCreateMove = nullptr;

static bool hkdCreateMove(void *self, int seq, float frametime, bool active) {
  bool ret = oCreateMove(self, seq, frametime, active);
  do_move(seq);
  return ret;
}

static void install_hook() {
  void **vt = VTABLE(g_iface.client);
  constexpr size_t SLOTS = 256;
  void **newvt = new void *[SLOTS];
  memcpy(newvt, vt, SLOTS * sizeof(void *));
  oCreateMove = (CreateMoveFn)vt[21];
  newvt[21] = (void *)&hkdCreateMove;
  VTABLE(g_iface.client) = newvt;
  logf("hook installed: CreateMove %p -> %p (orig %p)", vt[21],
       (void *)&hkdCreateMove, (void *)oCreateMove);
}

// ---------------------------------------------------------------------------
// setup thread
// ---------------------------------------------------------------------------

extern void find_modules();
extern void resolve_netvars();
extern bool find_input_global();
extern void init_x11();
extern void init_conmsg();
extern void install_menu();

static bool is_hl2_process() {
  char buf[512];
  ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  if (n <= 0)
    return false;
  buf[n] = 0;
  for (ssize_t i = n - 1; i >= 0; i--)
    if (buf[i] == '/')
      return strcmp(buf + i + 1, "hl2_linux") == 0;
  return false;
}

static void *setup_thread(void *) {
  for (int i = 0; i < 6000; i++) {
    find_modules();
    if (g_client.valid() && g_engine.valid())
      break;
    usleep(200 * 1000);
  }
  if (!g_client.valid() || !g_engine.valid()) {
    logf("client.so/engine.so never appeared");
    return nullptr;
  }
  logf("client.so: %d mapped range(s), %s", (int)g_client.ranges.size(),
       g_client.path.c_str());
  logf("engine.so: %d mapped range(s), %s", (int)g_engine.ranges.size(),
       g_engine.path.c_str());

  // resolve factories via dlopen handles
  void *ch = dlopen(g_client.path.c_str(), RTLD_NOLOAD | RTLD_LAZY | RTLD_GLOBAL);
  void *eh = dlopen(g_engine.path.c_str(), RTLD_NOLOAD | RTLD_LAZY | RTLD_GLOBAL);
  auto cf = (CreateInterfaceFn)dlsym(ch, "CreateInterface");
  auto ef = (CreateInterfaceFn)dlsym(eh, "CreateInterface");
  if (!cf || !ef) {
    logf("CreateInterface not found");
    return nullptr;
  }

  for (int i = 0; i < 300; i++) {
    if (!g_iface.client)
      g_iface.client = cf("VClient017", nullptr);
    if (!g_iface.entitylist)
      g_iface.entitylist = cf("VClientEntityList003", nullptr);
    if (!g_iface.engine)
      g_iface.engine = ef("VEngineClient014", nullptr);
    if (g_iface.client && g_iface.entitylist && g_iface.engine)
      break;
    usleep(100 * 1000);
  }
  if (!g_iface.client || !g_iface.entitylist || !g_iface.engine) {
    logf("interfaces unresolved: VClient017=%p VClientEntityList003=%p "
         "VEngineClient014=%p",
         g_iface.client, g_iface.entitylist, g_iface.engine);
    return nullptr;
  }
  logf("interfaces: VClient017=%p VClientEntityList003=%p "
       "VEngineClient014=%p",
       g_iface.client, g_iface.entitylist, g_iface.engine);

  resolve_netvars();
  if (!find_input_global())
    return nullptr;

  init_conmsg();
  install_hook();
  install_menu();
  return nullptr;
}

// ---------------------------------------------------------------------------
// constructor
// ---------------------------------------------------------------------------

__attribute__((constructor)) static void hl2hake_load() {
  if (!is_hl2_process())
    return;
  logf("loaded (pid %d)", getpid());
  pthread_t t;
  if (pthread_create(&t, nullptr, setup_thread, nullptr) == 0)
    pthread_detach(t);
  else
    logf("pthread_create failed");
}