summaryrefslogtreecommitdiffstats
path: root/src/world.cpp
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-03 23:21:13 +0100
committersalmonik <salmonik@tuta.io>2026-11-03 23:21:13 +0100
commit1f7c91905f69baba0aca8265c84deaba2ddac98a (patch)
tree9014a7e63e5531a05ff96c80a07ecccda927a533 /src/world.cpp
parentRemove temporary crash-stage markers (diff)
downloadhl2h-1f7c91905f69baba0aca8265c84deaba2ddac98a.tar.gz
hl2h-1f7c91905f69baba0aca8265c84deaba2ddac98a.zip
World: add world/sky material colour modulation
Port Amalgam's modulation approach: obtain IMaterialSystem from the already loaded materialsystem.so (VMaterialSystem080) and walk materials with FirstMaterial/NextMaterial/GetMaterial. ColorModulate the "World textures" group (sky names excluded) and the "SkyBox textures"/sky-named group. The walk runs only when the enabled groups or colours change, restores white when disabled, and re-applies after level changes. Before touching materials, GetNumMaterials() and the first material's name/group are sanity-checked so a wrong vtable slot logs and aborts instead of walking garbage. GUI: "World tint" card with world/sky toggles and RGB sliders; config section [worldtint].
Diffstat (limited to '')
-rw-r--r--src/world.cpp248
1 files changed, 248 insertions, 0 deletions
diff --git a/src/world.cpp b/src/world.cpp
new file mode 100644
index 0000000..eeffb99
--- /dev/null
+++ b/src/world.cpp
@@ -0,0 +1,248 @@
1// world.cpp — world/sky material colour modulation.
2//
3// Same mechanism Amalgam uses for its "World modulation": walk every material
4// in the material system and call IMaterial::ColorModulate on the ones whose
5// texture group is the world (or the skybox). This multiplies the material's
6// albedo; it does not touch lightmaps, so it is cheap and does not recompile
7// shaders.
8//
9// HL2's materialsystem.so registers "VMaterialSystem080" (the SDK 2013 name
10// for the modern layout) and exports CreateInterface, so the interface is
11// obtained exactly like VClient017/VGUI_Surface030 elsewhere in this cheat.
12//
13// Vtable slots are taken from the 2013 SDK headers:
14// IMaterialSystem: FirstMaterial=73 NextMaterial=74 InvalidMaterial=75
15// GetMaterial=76 GetNumMaterials=77
16// IMaterial: GetName=0 GetTextureGroupName=1 ColorModulate=28
17
18#include "hl2h.h"
19#include <cstdio>
20#include <cstdlib>
21#include <cstring>
22#include <dlfcn.h>
23
24namespace {
25
26constexpr int MATSYS_FirstMaterial = 73;
27constexpr int MATSYS_NextMaterial = 74;
28constexpr int MATSYS_InvalidMaterial = 75;
29constexpr int MATSYS_GetMaterial = 76;
30constexpr int MATSYS_GetNumMaterials = 77;
31
32constexpr int MAT_GetName = 0;
33constexpr int MAT_GetTextureGroupName = 1;
34constexpr int MAT_ColorModulate = 28;
35
36constexpr const char *GROUP_WORLD = "World textures";
37constexpr const char *GROUP_SKYBOX = "SkyBox textures";
38
39void *g_matsys = nullptr;
40bool g_matsys_tried = false;
41bool g_matsys_failed = false;
42bool g_matsys_logged = false;
43
44// Last state handed to the material system. Colour changes are applied when
45// this differs from the desired state, so the all-material walk runs only on
46// toggles/colour edits/level changes, never every frame.
47struct Tint {
48 bool valid;
49 int world_r, world_g, world_b;
50 int sky_r, sky_g, sky_b;
51};
52Tint g_applied{false, 255, 255, 255, 255, 255, 255};
53
54struct MatCalls {
55 using FirstFn = unsigned short (*)(void *);
56 using NextFn = unsigned short (*)(void *, unsigned short);
57 using InvalidFn = unsigned short (*)(void *);
58 using GetFn = void *(*)(void *, unsigned short);
59 using CountFn = int (*)(void *);
60 using NameFn = const char *(*)(void *);
61 using GroupFn = const char *(*)(void *);
62 using ModulateFn = void (*)(void *, float, float, float);
63};
64
65// Reject obviously bogus strings returned by a mis-mapped vtable slot instead
66// of walking thousands of materials with garbage pointers.
67bool plausible_name(const char *s) {
68 if (!s || !*s)
69 return false;
70 size_t n = strnlen(s, 512);
71 if (n == 0 || n >= 512)
72 return false;
73 for (size_t i = 0; i < n; i++) {
74 unsigned char c = (unsigned char)s[i];
75 if (c < 0x20 || c == 0x7f)
76 return false;
77 }
78 return true;
79}
80
81std::string find_loaded_module(const char *needle) {
82 FILE *maps = fopen("/proc/self/maps", "r");
83 if (!maps)
84 return {};
85 char line[1024];
86 std::string found;
87 while (fgets(line, sizeof(line), maps)) {
88 char *p = strchr(line, '/');
89 if (!p)
90 continue;
91 size_t pl = strlen(p);
92 while (pl && (p[pl - 1] == '\n' || p[pl - 1] == '\r'))
93 p[--pl] = 0;
94 if (!strstr(p, needle) || !strstr(p, ".so"))
95 continue;
96 found = p;
97 if (!strstr(p, "(deleted)"))
98 break;
99 }
100 fclose(maps);
101 return found;
102}
103
104void *material_system() {
105 if (g_matsys_tried)
106 return g_matsys;
107 g_matsys_tried = true;
108
109 std::string path = find_loaded_module("materialsystem");
110 void *h = nullptr;
111 if (!path.empty())
112 h = dlopen(path.c_str(), RTLD_NOLOAD | RTLD_LAZY | RTLD_GLOBAL);
113 // The engine always dlopens the module by name; NOLOAD only attaches to the
114 // already-mapped copy, so we never spawn a second material system.
115 if (!h)
116 h = dlopen("materialsystem.so", RTLD_NOLOAD | RTLD_LAZY | RTLD_GLOBAL);
117 if (!h) {
118 g_matsys_failed = true;
119 logf("tint: materialsystem.so not loaded");
120 return nullptr;
121 }
122 auto cf = (CreateInterfaceFn)dlsym(h, "CreateInterface");
123 if (!cf) {
124 g_matsys_failed = true;
125 logf("tint: materialsystem CreateInterface missing");
126 return nullptr;
127 }
128 static const char *names[] = {"VMaterialSystem080", "VMaterialSystem082",
129 "VMaterialSystem081", nullptr};
130 for (const char **n = names; *n; n++) {
131 int ret = 0;
132 void *ms = cf(*n, &ret);
133 if (!ms)
134 continue;
135 g_matsys = ms;
136 logf("tint: %s = %p via %s", *n, ms,
137 path.empty() ? "materialsystem.so" : path.c_str());
138 return ms;
139 }
140 g_matsys_failed = true;
141 logf("tint: material system interface not exposed");
142 return nullptr;
143}
144
145// Push one colour pair over all materials. Groups that are off get white so a
146// previously tinted world is restored.
147bool apply_tint(int world_r, int world_g, int world_b, int sky_r, int sky_g,
148 int sky_b) {
149 void *ms = material_system();
150 if (!ms)
151 return false;
152
153 auto invalid = vfunc<MatCalls::InvalidFn>(ms, MATSYS_InvalidMaterial);
154 auto first = vfunc<MatCalls::FirstFn>(ms, MATSYS_FirstMaterial);
155 auto next = vfunc<MatCalls::NextFn>(ms, MATSYS_NextMaterial);
156 auto get = vfunc<MatCalls::GetFn>(ms, MATSYS_GetMaterial);
157 auto count = vfunc<MatCalls::CountFn>(ms, MATSYS_GetNumMaterials);
158
159 int total = count(ms);
160 if (total <= 0 || total > 200000) {
161 logf("tint: GetNumMaterials() = %d (unexpected; disabled)", total);
162 g_matsys_failed = true;
163 toastf(TOAST_ERROR, "World tint: material system layout mismatch");
164 return false;
165 }
166
167 float wr = world_r / 255.0f, wg = world_g / 255.0f, wb = world_b / 255.0f;
168 float sr = sky_r / 255.0f, sg = sky_g / 255.0f, sb = sky_b / 255.0f;
169
170 unsigned short inv = invalid(ms);
171 int seen = 0, world_hits = 0, sky_hits = 0;
172 for (unsigned short h = first(ms); h != inv; h = next(ms, h)) {
173 if (++seen > total + 16) {
174 logf("tint: material walk runaway (%d > %d); stopped", seen, total);
175 break;
176 }
177 void *mat = get(ms, h);
178 if (!mat)
179 continue;
180 auto name_fn = vfunc<MatCalls::NameFn>(mat, MAT_GetName);
181 auto group_fn = vfunc<MatCalls::GroupFn>(mat, MAT_GetTextureGroupName);
182 auto mod_fn = vfunc<MatCalls::ModulateFn>(mat, MAT_ColorModulate);
183 const char *name = name_fn(mat);
184 const char *group = group_fn(mat);
185 if (!plausible_name(name) || !plausible_name(group))
186 continue;
187 // The first material's name also validates the IMaterial vtable mapping
188 // before we poke any colour state.
189 if (!g_matsys_logged) {
190 g_matsys_logged = true;
191 logf("tint: first material \"%s\" group \"%s\"", name, group);
192 }
193 bool is_world = strncmp(group, GROUP_WORLD, strlen(GROUP_WORLD)) == 0;
194 bool is_sky = strncmp(group, GROUP_SKYBOX, strlen(GROUP_SKYBOX)) == 0 ||
195 strstr(name, "sky") != nullptr;
196 if (is_world && !strstr(name, "sky")) {
197 mod_fn(mat, wr, wg, wb);
198 world_hits++;
199 } else if (is_sky) {
200 mod_fn(mat, sr, sg, sb);
201 sky_hits++;
202 }
203 }
204 logf("tint: applied world=%d(%d,%d,%d) sky=%d(%d,%d,%d) of %d materials",
205 world_hits, world_r, world_g, world_b, sky_hits, sky_r, sky_g, sky_b,
206 total);
207 return true;
208}
209
210} // namespace
211
212void world_tint_invalidate() { g_applied.valid = false; }
213
214void world_tint_update() {
215 bool on = feature_active(FEAT_WORLD_TINT);
216 bool world_on = on && g_tint_world_on;
217 bool sky_on = on && g_tint_sky_on;
218
219 int wr = world_on ? g_tint_world_rgb[0] : 255;
220 int wg = world_on ? g_tint_world_rgb[1] : 255;
221 int wb = world_on ? g_tint_world_rgb[2] : 255;
222 int sr = sky_on ? g_tint_sky_rgb[0] : 255;
223 int sg = sky_on ? g_tint_sky_rgb[1] : 255;
224 int sb = sky_on ? g_tint_sky_rgb[2] : 255;
225
226 bool white = wr == 255 && wg == 255 && wb == 255 && sr == 255 &&
227 sg == 255 && sb == 255;
228 if (g_applied.valid && g_applied.world_r == wr && g_applied.world_g == wg &&
229 g_applied.world_b == wb && g_applied.sky_r == sr &&
230 g_applied.sky_g == sg && g_applied.sky_b == sb)
231 return;
232 // Nothing was ever tinted and nothing is requested: no need to walk the
233 // material list just to write white over white.
234 if (!g_applied.valid && white) {
235 g_applied = {true, wr, wg, wb, sr, sg, sb};
236 return;
237 }
238 if (g_matsys_failed && !world_on && !sky_on) {
239 g_applied = {true, wr, wg, wb, sr, sg, sb};
240 return;
241 }
242 if (!apply_tint(wr, wg, wb, sr, sg, sb)) {
243 if (g_matsys_failed)
244 g_applied = {true, wr, wg, wb, sr, sg, sb};
245 return;
246 }
247 g_applied = {true, wr, wg, wb, sr, sg, sb};
248}