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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2020-2023 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#define MUMBLE_ALLOW_DEPRECATED_LEGACY_PLUGIN_API
#include "mumble_legacy_plugin.h"
#include "mumble_positional_audio_utils.h"
#ifdef OS_LINUX
# include "ProcessLinux.h"
#endif
#include "ProcessWindows.h"
#include <cstring>
#include <memory>
#include <sstream>
std::unique_ptr< ProcessBase > proc;
#ifdef OS_WINDOWS
static constexpr bool isWin32 = true;
#else
static bool isWin32 = false;
#endif
#include "client.h"
#include "common.h"
#include "engine.h"
static inline void anglesToVectors(const float (&angles)[3], float *front, float *top) {
// Calculate pitch
float pitchSin, pitchCos;
sinCos(degreesToRadians(angles[0]), pitchSin, pitchCos);
// Calculate yaw
float yawSin, yawCos;
sinCos(degreesToRadians(angles[1]), yawSin, yawCos);
// Calculate roll
float rollSin, rollCos;
sinCos(degreesToRadians(angles[2]), rollSin, rollCos);
// Calculate front vector
front[0] = pitchCos * yawCos;
front[1] = -pitchSin;
front[2] = pitchCos * yawSin;
// Calculate top vector
top[0] = (rollCos * pitchSin * yawCos + -rollSin * -yawSin);
top[1] = rollCos * pitchCos;
top[2] = (rollCos * pitchSin * yawSin + -rollSin * yawCos);
}
procptr_t localClient, localPlayer;
int32_t signOnStateOffset, deadFlagOffset, rotationOffset, originPositionOffset, eyesPositionOffsetOffset,
netInfoOffset, levelNameOffset;
static int fetch(float *avatarPos, float *avatarFront, float *avatarTop, float *cameraPos, float *cameraFront,
float *cameraTop, std::string &context, std::wstring &identity) {
const auto signOnState = proc->peek< uint32_t >(localClient + signOnStateOffset);
// 0: SIGNONSTATE_NONE
// 1: SIGNONSTATE_CHALLENGE
// 2: SIGNONSTATE_CONNECTED
// 3: SIGNONSTATE_NEW
// 4: SIGNONSTATE_PRESPAWN
// 5: SIGNONSTATE_SPAWN
// 6: SIGNONSTATE_FULL
// 7: SIGNONSTATE_CHANGELEVEL
if (signOnState != 6) {
return false;
}
const auto isPlayerDead = proc->peek< bool >(localPlayer + deadFlagOffset);
if (isPlayerDead) {
context.clear();
identity.clear();
for (uint8_t i = 0; i < 3; ++i) {
avatarPos[i] = avatarFront[i] = avatarTop[i] = cameraPos[i] = cameraFront[i] = cameraTop[i] = 0.0f;
}
return true;
}
float rotation[3];
if (!proc->peek(localPlayer + rotationOffset, rotation, sizeof(rotation))) {
return false;
}
float originPosition[3];
if (!proc->peek(localPlayer + originPositionOffset, originPosition, sizeof(originPosition))) {
return false;
}
float eyesPositionOffset[3];
if (!proc->peek(localPlayer + eyesPositionOffsetOffset, eyesPositionOffset, sizeof(eyesPositionOffset))) {
return false;
}
NetInfo ni;
if (!proc->peek(localClient + netInfoOffset, ni)) {
return false;
}
std::string host;
// 0: NA_NULL
// 1: NA_LOOPBACK
// 2: NA_BROADCAST
// 3: NA_IP
switch (ni.type) {
case 3: {
std::ostringstream ss;
for (uint8_t i = 0; i < 4; ++i) {
ss << std::to_string(ni.ip[i]);
if (i < 3) {
ss << ".";
}
}
ss << ":" << networkToHost(ni.port);
host = ss.str();
break;
}
default:
return true;
}
// 40 is the size of the char array in the engine's code
char map[40];
if (!proc->peek(localClient + levelNameOffset, map, sizeof(map))) {
map[0] = '\0';
}
// Begin context
std::ostringstream ocontext;
ocontext << " {\"Host\": \"" << host << "\"}"; // Set context with server's IP address and port.
context = ocontext.str();
// End context
// Begin identity
std::wostringstream oidentity;
oidentity << "{";
// Map
escape(map, sizeof(map));
if (strcmp(map, "") != 0) {
oidentity << std::endl << "\"Map\": \"" << map << "\""; // Set map name in identity.
} else {
oidentity << std::endl << "\"Map\": null";
}
oidentity << std::endl << "}";
identity = oidentity.str();
// End identity
// Mumble | Game
// X | X
// Y | Z
// Z | Y
//
// originPosition corresponds to the origin of the player model,
// usually the point (i.e. feet) where it touches the ground.
// eyesPositionOffset is the difference between the eyes and the origin position.
// We use the eyes' position because they are closer to the mouth.
avatarPos[0] = originPosition[0] + eyesPositionOffset[0];
avatarPos[1] = originPosition[2] + eyesPositionOffset[2];
avatarPos[2] = originPosition[1] + eyesPositionOffset[1];
anglesToVectors(rotation, avatarFront, avatarTop);
// Sync camera vectors with avatar ones.
//
// This is not ideal, but it's not an issue unless 3rd person mode is enabled.
// The command to set it is "thirdperson" and requires "sv_cheats" to be enabled.
// For reference, the functions that return what we need are MainViewOrigin() and MainViewAngles().
for (uint8_t i = 0; i < 3; ++i) {
cameraPos[i] = avatarPos[i] /= 39.37f; // Convert from inches to meters.
cameraFront[i] = avatarFront[i];
cameraTop[i] = avatarTop[i];
}
return true;
}
static bool tryInit(const std::multimap< std::wstring, unsigned long long int > &pids) {
const std::vector< std::string > names{
#ifdef OS_LINUX
"hl2_linux",
#endif
"left4dead2.exe", "left4dead.exe"
};
for (const auto &name : names) {
const auto id = ProcessBase::find(name, pids);
if (!id) {
continue;
}
#ifdef OS_WINDOWS
proc.reset(new ProcessWindows(id, name));
#else
isWin32 = HostLinux::isWine(id);
if (isWin32) {
proc.reset(new ProcessWindows(id, name));
} else {
proc.reset(new ProcessLinux(id, name));
}
#endif
if (proc->isOk()) {
return true;
}
}
return false;
}
static int tryLock(const std::multimap< std::wstring, unsigned long long int > &pids) {
if (!tryInit(pids)) {
return false;
}
const auto modules = proc->modules();
auto iter = modules.find(isWin32 ? "engine.dll" : "engine.so");
if (iter == modules.cend()) {
return false;
}
const auto engine = iter->second.baseAddress();
if (!engine) {
return false;
}
iter = modules.find(isWin32 ? "client.dll" : "client.so");
if (iter == modules.cend()) {
return false;
}
const auto client = iter->second.baseAddress();
if (!client) {
return false;
}
const auto engineInterfaces = getInterfaces(engine);
const auto engineClient = getInterfaceAddress("VEngineClient013", engineInterfaces);
if (!engineClient) {
return false;
}
localClient = getLocalClient(engineClient);
if (!localClient) {
return false;
}
signOnStateOffset = getSignOnStateOffset(engineClient);
if (!signOnStateOffset) {
return false;
}
const auto signOnState = proc->peek< uint32_t >(localClient + signOnStateOffset);
if (signOnState != 6) {
return false;
}
const auto clientInterfaces = getInterfaces(client);
const auto clientEntityList = getInterfaceAddress("VClientEntityList003", clientInterfaces);
if (!clientEntityList) {
return false;
}
localPlayer = getLocalPlayer(localClient, clientEntityList, engineClient);
if (!localPlayer) {
return false;
}
// "pl" is the offset to the internal player class.
const auto pl = getDataVarFromEntity("pl", localPlayer);
if (!pl) {
return false;
}
// "deadflag" is the offset relative to "pl".
//
// For some reason it's missing from Left 4 Dead 2 on Linux, along with many other datavars we don't need.
// The offset (0x4) has never changed across the various engine versions released during the years (10+), thus we
// force it.
const auto deadflag = getDataVarFromEntity("deadflag", localPlayer);
deadFlagOffset = pl + (deadflag ? deadflag : 0x4);
originPositionOffset = getDataVarFromEntity("m_vecAbsOrigin", localPlayer);
if (!originPositionOffset) {
return false;
}
eyesPositionOffsetOffset = getDataVarFromEntity("m_vecViewOffset", localPlayer);
if (!eyesPositionOffsetOffset) {
return false;
}
rotationOffset = getDataVarFromEntity("m_angAbsRotation", localPlayer);
if (!rotationOffset) {
return false;
}
netInfoOffset = getNetInfoOffset(localClient, engineClient);
if (!netInfoOffset) {
return false;
}
levelNameOffset = getLevelNameOffset(engineClient);
if (!levelNameOffset) {
return false;
}
return true;
}
static const std::wstring longDesc() {
return std::wstring(L"Supports Source Engine games with context and identity support.");
}
static std::wstring description(L"Source Engine");
static std::wstring shortName(L"Source Engine");
static int tryLock1() {
return tryLock(std::multimap< std::wstring, unsigned long long int >());
}
static void nullUnlock() {
}
static MumblePlugin sePlug = { MUMBLE_PLUGIN_MAGIC, description, shortName, nullptr, nullptr, tryLock1,
nullUnlock, longDesc, fetch };
static MumblePlugin2 sePlug2 = { MUMBLE_PLUGIN_MAGIC_2, MUMBLE_PLUGIN_VERSION, tryLock };
extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin *getMumblePlugin() {
return &sePlug;
}
extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin2 *getMumblePlugin2() {
return &sePlug2;
}
|