summaryrefslogtreecommitdiffstats
path: root/src/netvars.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/netvars.cpp')
-rw-r--r--src/netvars.cpp185
1 files changed, 185 insertions, 0 deletions
diff --git a/src/netvars.cpp b/src/netvars.cpp
new file mode 100644
index 0000000..41070c7
--- /dev/null
+++ b/src/netvars.cpp
@@ -0,0 +1,185 @@
1// netvars.cpp — ClientClass walk, RecvTable resolution, movetype from pred-TD
2#include "hl2h.h"
3
4// RecvTable layout: pProps@0, nProps@4, pDecoder@8, pNetTableName@0xC
5// RecvProp layout: pVarName@0, pArrayProp@0x18, pDataTable@0x28, offset@0x2C
6// ClientClass (2004-era): CreateFn@0, CreateEventFn@4, Name@8, RecvTable@0xC, Next@0x10
7
8static bool module_string(uintptr_t p, char *out, size_t outn) {
9 if (!g_client.contains(p))
10 return false;
11 const char *s = (const char *)p;
12 size_t i = 0;
13 while (i < outn - 1) {
14 unsigned char ch = (unsigned char)s[i];
15 if (ch == 0)
16 break;
17 if (ch < 0x20 || ch > 0x7E)
18 return false;
19 out[i] = (char)ch;
20 i++;
21 }
22 if (i == 0 || i >= outn - 1)
23 return false;
24 out[i] = 0;
25 return true;
26}
27
28static int g_dumped_tables = 0;
29
30static void collect_netvars(uintptr_t table, uintptr_t base_off, int depth) {
31 if (depth > 8)
32 return;
33 uintptr_t props = *(uintptr_t *)(table + 0x00);
34 int32_t nprops = *(int32_t *)(table + 0x04);
35 if (!props || !g_client.contains(props) || nprops <= 0 || nprops > 512)
36 return;
37
38 char tname[64];
39 if (!module_string(*(uintptr_t *)(table + 0x0C), tname, sizeof(tname)))
40 return;
41 if (strncmp(tname, "DT_", 3) != 0)
42 return;
43
44 bool dump = g_dumped_tables < 2 &&
45 (strcmp(tname, "DT_BaseEntity") == 0 ||
46 strcmp(tname, "DT_BasePlayer") == 0);
47 if (dump) {
48 g_dumped_tables++;
49 logf("dump %s: %d props @ table %#lx (base_off %#lx)", tname, nprops,
50 (unsigned long)table, (unsigned long)base_off);
51 }
52
53 for (int32_t i = 0; i < nprops; i++) {
54 uintptr_t prop = props + (size_t)i * 0x3C;
55 if (!g_client.contains(prop))
56 return;
57 uintptr_t pname = *(uintptr_t *)(prop + 0x00);
58 int32_t ptype = *(int32_t *)(prop + 0x04);
59 int32_t poff = *(int32_t *)(prop + 0x2C);
60 uintptr_t parray = *(uintptr_t *)(prop + 0x18);
61 uintptr_t ptable = *(uintptr_t *)(prop + 0x28);
62
63 char pname2[64];
64 if (!module_string(pname, pname2, sizeof(pname2)))
65 continue;
66
67 if (dump)
68 logf(" [%s] %s type=%d off=%d(0x%x) table=%s array=%s", tname, pname2,
69 ptype, poff, poff, ptable ? "yes" : "no", parray ? "yes" : "no");
70
71 if (ptable) {
72 collect_netvars(ptable, base_off + (uint32_t)poff, depth + 1);
73 continue;
74 }
75
76 if (strcmp(pname2, "m_fFlags") == 0 && g_net.flags < 0) {
77 g_net.flags = (int)(base_off + (uint32_t)poff);
78 } else if (strcmp(pname2, "m_vecVelocity") == 0 && g_net.velocity < 0) {
79 if (parray && g_client.contains(parray)) {
80 int32_t eoff = *(int32_t *)(parray + 0x2C);
81 g_net.velocity = (int)(base_off + (uint32_t)(eoff ? eoff : poff));
82 } else {
83 g_net.velocity = (int)(base_off + (uint32_t)poff);
84 }
85 }
86 if (g_net.ok())
87 return;
88 }
89}
90
91// prediction datamap scan for m_MoveType (FIELD_CHARACTER, fieldOffset in range)
92static bool resolve_movetype() {
93 static const char needle[] = "m_MoveType";
94 const size_t nlen = sizeof(needle);
95
96 uintptr_t str_va = 0;
97 for (auto &r : g_client.ranges) {
98 const uint8_t *base = (const uint8_t *)r.lo;
99 size_t span = r.hi - r.lo;
100 for (size_t i = 0; i + nlen <= span; i++) {
101 if (memcmp(base + i, needle, nlen) == 0) {
102 str_va = r.lo + i;
103 break;
104 }
105 }
106 if (str_va)
107 break;
108 }
109 if (!str_va) {
110 logf("m_MoveType string not found");
111 return false;
112 }
113
114 uint32_t offs[16];
115 int counts[16] = {};
116 int ncand = 0;
117 const uint32_t target = (uint32_t)str_va;
118 for (auto &r : g_client.ranges) {
119 const uint8_t *base = (const uint8_t *)r.lo;
120 size_t span = r.hi - r.lo;
121 for (size_t off = 4; off + 8 <= span; off += 4) {
122 if (*(const uint32_t *)(base + off) != target)
123 continue;
124 uint32_t ftype = *(const uint32_t *)(base + off - 4);
125 int32_t foff = *(const int32_t *)(base + off + 4);
126 if (ftype != 8 || foff < 0x40 || foff > 0x400)
127 continue;
128 int k;
129 for (k = 0; k < ncand; k++)
130 if (offs[k] == (uint32_t)foff)
131 break;
132 if (k == ncand && ncand < 16) {
133 offs[ncand] = (uint32_t)foff;
134 ncand++;
135 }
136 if (k < ncand)
137 counts[k]++;
138 }
139 }
140 if (ncand == 0) {
141 logf("m_MoveType typedescription not found");
142 return false;
143 }
144 int best = 0;
145 for (int k = 1; k < ncand; k++)
146 if (counts[k] > counts[best])
147 best = k;
148 g_net.movetype = (int)offs[best];
149 return true;
150}
151
152void resolve_netvars() {
153 using GetAllClassesFn = void *(*)(void *);
154 uintptr_t head =
155 (uintptr_t)vfunc<GetAllClassesFn>(g_iface.client, 8)(g_iface.client);
156 if (!g_client.contains(head)) {
157 logf("GetAllClasses returned %p, outside client module", (void *)head);
158 return;
159 }
160 int classes = 0;
161 for (uintptr_t cc = head; cc && g_client.contains(cc) && classes < 1024;
162 cc = *(uintptr_t *)(cc + 0x10)) {
163 classes++;
164 char name[64];
165 if (!module_string(*(uintptr_t *)(cc + 0x08), name, sizeof(name)))
166 break;
167 collect_netvars(*(uintptr_t *)(cc + 0x0C), 0, 0);
168 if (g_net.flags >= 0 && g_net.velocity >= 0)
169 break;
170 }
171 logf("client classes walked: %d", classes);
172
173 if (g_net.movetype < 0 && !resolve_movetype()) {
174 // movetype unresolved — bhop still works, just no noclip/ladder guard
175 logf("movetype guard disabled");
176 }
177
178 if (g_net.ok()) {
179 logf("netvars: m_fFlags=+0x%X m_vecVelocity[0]=+0x%X m_MoveType=+0x%X",
180 g_net.flags, g_net.velocity, g_net.movetype);
181 } else {
182 logf("netvar resolution incomplete: flags=%d velocity=%d movetype=%d",
183 g_net.flags, g_net.velocity, g_net.movetype);
184 }
185}