1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #include "../mumble_plugin_win32.h" // Include standard plugin header.
7 #include "../mumble_plugin_utils.h" // Include plugin header for special functions, like "escape".
8 #include <algorithm> // Include algorithm header for the game version detector
9 
fetch(float * avatar_pos,float * avatar_front,float * avatar_top,float * camera_pos,float * camera_front,float * camera_top,std::string & context,std::wstring & identity)10 static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, float *camera_pos, float *camera_front, float *camera_top, std::string &context, std::wstring &identity) {
11 	for (int i=0;i<3;i++) {
12 		avatar_pos[i] = avatar_front[i] = avatar_top[i] = camera_pos[i] = camera_front[i] = camera_top[i] = 0.0f;
13 	}
14 
15 	// Memory addresses
16 	char game_name[20];
17 	procptr_t state_address, in_game_address, avatar_pos_address, camera_pos_address, avatar_base_address, camera_front_address, camera_top_address, player_address, vehicle_address, location_address, street_address;
18 
19 #define VERSION_EQ(buf, strlit) \
20 	memcmp(buf, strlit, std::min(sizeof(buf), sizeof(strlit)-1)) == 0
21 
22 	// Steam version
23 	if (peekProc(pModule + 0x18107F8, game_name) && VERSION_EQ(game_name, "Grand Theft Auto V")) {
24 		state_address = pModule + 0x27377E0;
25 		in_game_address = pModule + 0x245F430;
26 		avatar_pos_address = pModule + 0x1F82C30;
27 		camera_pos_address = pModule + 0x1C5C560;
28 		avatar_base_address = pModule + 0x1B99760;
29 		camera_front_address = pModule + 0x1F81E20;
30 		camera_top_address = pModule + 0x1F81E10;
31 		player_address = pModule + 0x2741F7C;
32 		vehicle_address = pModule + 0x2335A00;
33 		location_address = pModule + 0x23353CB;
34 		street_address = pModule + 0x2332110;
35 	// Retail version
36 	} else if (peekProc(pModule + 0x180D4D8, game_name) && VERSION_EQ(game_name, "Grand Theft Auto V")) {
37 		state_address = pModule + 0x2733490;
38 		in_game_address = pModule + 0x1BC6687;
39 		avatar_pos_address = pModule + 0x1F7EAA0;
40 		camera_pos_address = pModule + 0x1C58630;
41 		avatar_base_address = pModule + 0x1B956C0;
42 		camera_front_address = pModule + 0x1C5A0F0;
43 		camera_top_address = pModule + 0x1F7D9F0;
44 		player_address = pModule + 0x273DBAC;
45 		vehicle_address = pModule + 0x2331890;
46 		location_address = pModule + 0x233125B;
47 		street_address = pModule + 0x232DFA0;
48 	// Unknown version
49 	} else {
50 		generic_unlock();
51 		return false;
52 	}
53 
54 	// Avatar pointer
55 	procptr_t avatar_base = peekProcPtr(avatar_base_address);
56 	if (!avatar_base) return false;
57 
58 	// Boolean value to check if game addresses retrieval is successful
59 	bool ok;
60 	// Create containers to stuff our raw data into, so we can convert it to Mumble's coordinate system
61 	float avatar_pos_corrector[3], camera_pos_corrector[3], avatar_front_corrector[3], avatar_top_corrector[3], camera_front_corrector[3], camera_top_corrector[3];
62 	// Char values for extra features
63 	char state, in_game, player[50], vehicle[50], location[50], street[50];
64 
65 	// Peekproc and assign game addresses to our containers, so we can retrieve positional data
66 	ok = peekProc(state_address, &state, 1) && // Magical state value: 0 when in single player, 2 when online and 3 when in a lobby.
67 			peekProc(in_game_address, &in_game, 1) && // 0 when loading or not in-game, 1 when in-game.
68 			peekProc(avatar_pos_address, avatar_pos_corrector, 12) && // Avatar Position values (X, Z and Y).
69 			peekProc(camera_pos_address, camera_pos_corrector, 12) && // Camera Position values (X, Z and Y).
70 			peekProc(avatar_base + 0x70, avatar_front_corrector, 12) && // Avatar Front Vector values (X, Z and Y).
71 			peekProc(avatar_base + 0x80, avatar_top_corrector, 12) && // Avatar Top Vector values (X, Z and Y).
72 			peekProc(camera_front_address, camera_front_corrector, 12) && // Camera Front Vector values (X, Z and Y).
73 			peekProc(camera_top_address, camera_top_corrector, 12) && // Camera Top Vector values (X, Z and Y).
74 			peekProc(player_address, player) && // Player nickname.
75 			peekProc(vehicle_address, vehicle) && // Vehicle name if in a vehicle, empty if not.
76 			peekProc(location_address, location) && // Location name.
77 			peekProc(street_address, street); // Street name if on a street, empty if not.
78 
79 	// This prevents the plugin from linking to the game in case something goes wrong during values retrieval from memory addresses.
80 	if (! ok)
81 		return false;
82 
83 	// State
84 	if (state != 2 || in_game == 0) { // If not in-game
85 		context.clear(); // Clear context
86 		identity.clear(); // Clear identity
87 		// Set vectors values to 0.
88 		for (int i=0;i<3;i++)
89 			avatar_pos[i] = avatar_front[i] = avatar_top[i] = camera_pos[i] =  camera_front[i] = camera_top[i] = 0.0f;
90 
91 		return true; // This tells Mumble to ignore all vectors.
92 	}
93 	/*
94 	// Begin context
95 	std::ostringstream ocontext;
96 
97 	// Host
98 	escape(host);
99 	if (strcmp(host, "") != 0 && strstr(host, "127.0.0.1") == NULL) { // Set host string as empty if "127.0.0.1" is found in it.
100 		ocontext << " {\"Host\": \"" << host << "\"}"; // Set context with IP address and port
101 	}
102 
103 	context = ocontext.str();
104 	// End context
105 	*/
106 	// Begin identity
107 	std::wostringstream oidentity;
108 	oidentity << "{";
109 
110 	// Player
111 	escape(player, sizeof(player));
112 	if (strcmp(player, "") != 0) {
113 		oidentity << std::endl;
114 		oidentity << "\"Player\": \"" << player << "\","; // Set player nickname in identity.
115 	} else {
116 		oidentity << std::endl << "\"Player\": null,";
117 	}
118 
119 	// Vehicle
120 	escape(vehicle, sizeof(vehicle));
121 	if (strcmp(vehicle, "") != 0) {
122 		oidentity << std::endl;
123 		oidentity << "\"Vehicle\": \"" << vehicle << "\","; // Set vehicle name in identity.
124 	} else {
125 		oidentity << std::endl << "\"Vehicle\": null,";
126 	}
127 
128 	// Location
129 	escape(location, sizeof(location));
130 	if (strcmp(location, "") != 0) {
131 		oidentity << std::endl;
132 		oidentity << "\"Location\": \"" << location << "\","; // Set location name in identity.
133 	} else {
134 		oidentity << std::endl << "\"Location\": null,";
135 	}
136 
137 	// Street
138 	escape(street, sizeof(street));
139 	if (strcmp(street, "") != 0) {
140 		oidentity << std::endl;
141 		oidentity << "\"Street\": \"" << street << "\""; // Set street name in identity.
142 	} else {
143 		oidentity << std::endl << "\"Street\": null";
144 	}
145 
146 	oidentity << std::endl << "}";
147 	identity = oidentity.str();
148 	// End identity
149 
150 	/*
151 	Mumble | Game
152 	X      | X
153 	Y      | Z
154 	Z      | Y
155 	*/
156 	avatar_pos[0] = avatar_pos_corrector[0];
157 	avatar_pos[1] = avatar_pos_corrector[2];
158 	avatar_pos[2] = avatar_pos_corrector[1];
159 
160 	camera_pos[0] = camera_pos_corrector[0];
161 	camera_pos[1] = camera_pos_corrector[2];
162 	camera_pos[2] = camera_pos_corrector[1];
163 
164 	avatar_front[0] = avatar_front_corrector[0];
165 	avatar_front[1] = avatar_front_corrector[2];
166 	avatar_front[2] = avatar_front_corrector[1];
167 
168 	avatar_top[0] = avatar_top_corrector[0];
169 	avatar_top[1] = avatar_top_corrector[2];
170 	avatar_top[2] = avatar_top_corrector[1];
171 
172 	camera_front[0] = camera_front_corrector[0];
173 	camera_front[1] = camera_front_corrector[2];
174 	camera_front[2] = camera_front_corrector[1];
175 
176 	camera_top[0] = camera_top_corrector[0];
177 	camera_top[1] = camera_top_corrector[2];
178 	camera_top[2] = camera_top_corrector[1];
179 
180 	return true;
181 }
182 
trylock(const std::multimap<std::wstring,unsigned long long int> & pids)183 static int trylock(const std::multimap<std::wstring, unsigned long long int> &pids) {
184 
185 	if (! initialize(pids, L"GTA5.exe")) { // Retrieve game executable's memory address
186 		return false;
187 	}
188 
189 	// Check if we can get meaningful data from it
190 	float apos[3], afront[3], atop[3], cpos[3], cfront[3], ctop[3];
191 	std::wstring sidentity;
192 	std::string scontext;
193 
194 	if (fetch(apos, afront, atop, cpos, cfront, ctop, scontext, sidentity)) {
195 		return true;
196 	} else {
197 		generic_unlock();
198 		return false;
199 	}
200 }
201 
longdesc()202 static const std::wstring longdesc() {
203 	return std::wstring(L"Supports Grand Theft Auto V version 1.38 with identity support."); // Plugin long description
204 }
205 
206 static std::wstring description(L"Grand Theft Auto V (v1.38)"); // Plugin short description
207 static std::wstring shortname(L"Grand Theft Auto V"); // Plugin short name
208 
trylock1()209 static int trylock1() {
210 	return trylock(std::multimap<std::wstring, unsigned long long int>());
211 }
212 
213 static MumblePlugin gtavplug = {
214 	MUMBLE_PLUGIN_MAGIC,
215 	description,
216 	shortname,
217 	NULL,
218 	NULL,
219 	trylock1,
220 	generic_unlock,
221 	longdesc,
222 	fetch
223 };
224 
225 static MumblePlugin2 gtavplug2 = {
226 	MUMBLE_PLUGIN_MAGIC_2,
227 	MUMBLE_PLUGIN_VERSION,
228 	trylock
229 };
230 
getMumblePlugin()231 extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin *getMumblePlugin() {
232 	return &gtavplug;
233 }
234 
getMumblePlugin2()235 extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin2 *getMumblePlugin2() {
236 	return &gtavplug2;
237 }
238