~ubuntu-branches/ubuntu/raring/mumble/raring

« back to all changes in this revision

Viewing changes to plugins/dods/dods.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Thorvald Natvig, Patrick Matthäi, Thorvald Natvig
  • Date: 2011-02-19 22:58:58 UTC
  • mfrom: (9.1.15 sid)
  • Revision ID: james.westby@ubuntu.com-20110219225858-0xlftrf4z1z4jt9e
Tags: 1.2.3-1
[ Patrick Matthäi ]
* Do not build with non existant libpulse-dev on hurd-i386.

[ Thorvald Natvig ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
*/
31
31
 
32
 
#include <stdio.h>
33
 
#include <stdlib.h>
34
 
#include <windows.h>
35
 
#include <tlhelp32.h>
36
 
#include <string>
37
 
#include <sstream>
38
 
 
39
 
#define _USE_MATH_DEFINES
40
 
#include <math.h>
41
 
 
42
 
#include "../mumble_plugin.h"
 
32
#include "../mumble_plugin_win32.h"
43
33
 
44
34
using namespace std;
45
35
 
46
 
HANDLE h;
47
36
BYTE *posptr;
48
37
BYTE *rotptr;
49
38
BYTE *stateptr;
50
39
BYTE *hostptr;
51
40
 
52
 
static DWORD getProcess(const wchar_t *exename) {
53
 
        PROCESSENTRY32 pe;
54
 
        DWORD pid = 0;
55
 
 
56
 
        pe.dwSize = sizeof(pe);
57
 
        HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
58
 
        if (hSnap != INVALID_HANDLE_VALUE) {
59
 
                BOOL ok = Process32First(hSnap, &pe);
60
 
 
61
 
                while (ok) {
62
 
                        if (wcscmp(pe.szExeFile, exename)==0) {
63
 
                                pid = pe.th32ProcessID;
64
 
                                break;
65
 
                        }
66
 
                        ok = Process32Next(hSnap, &pe);
67
 
                }
68
 
                CloseHandle(hSnap);
69
 
        }
70
 
        return pid;
71
 
}
72
 
 
73
 
static BYTE *getModuleAddr(DWORD pid, const wchar_t *modname) {
74
 
        MODULEENTRY32 me;
75
 
        BYTE *addr = NULL;
76
 
        me.dwSize = sizeof(me);
77
 
        HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
78
 
        if (hSnap != INVALID_HANDLE_VALUE) {
79
 
                BOOL ok = Module32First(hSnap, &me);
80
 
 
81
 
                while (ok) {
82
 
                        if (wcscmp(me.szModule, modname)==0) {
83
 
                                addr = me.modBaseAddr;
84
 
                                break;
85
 
                        }
86
 
                        ok = Module32Next(hSnap, &me);
87
 
                }
88
 
                CloseHandle(hSnap);
89
 
        }
90
 
        return addr;
91
 
}
92
 
 
93
 
 
94
 
static bool peekProc(VOID *base, VOID *dest, SIZE_T len) {
95
 
        SIZE_T r;
96
 
        BOOL ok=ReadProcessMemory(h, base, dest, len, &r);
97
 
        return (ok && (r == len));
98
 
}
99
 
 
100
 
static void about(HWND h) {
101
 
        ::MessageBox(h, L"Reads audio position information from Day of Defeat: Source (Build 4069). IP:Port context support.", L"Mumble DODS Plugin", MB_OK);
102
 
}
103
 
 
104
41
static bool calcout(float *pos, float *rot, float *opos, float *front, float *top) {
105
42
        float h = rot[0];
106
43
        float v = rot[1];
129
66
        return true;
130
67
}
131
68
 
132
 
static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, float *camera_pos, float *camera_front, float *camera_top, string &context, wstring &identity) {
 
69
static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, float *camera_pos, float *camera_front, float *camera_top, string &context, wstring &) {
133
70
        for (int i=0;i<3;i++)
134
71
                avatar_pos[i] = avatar_front[i] = avatar_top[i] = camera_pos[i] = camera_front[i] = camera_top[i] = 0.0f;
135
72
 
152
89
 
153
90
        sHost.assign(chHostStr);
154
91
        if (sHost.find(':')==string::npos)
 
92
                sHost.append(":27015");
155
93
 
156
 
                new_context << "<context>"
157
 
                << "<game>dods</game>"
158
 
                << "<hostport>" << sHost << "</hostport>"
159
 
                << "</context>";
 
94
        new_context << "<context>"
 
95
        << "<game>dods</game>"
 
96
        << "<hostport>" << sHost << "</hostport>"
 
97
        << "</context>";
160
98
        context = new_context.str();
161
99
 
162
100
        /* TODO
182
120
        return false;
183
121
}
184
122
 
185
 
static int trylock() {
186
 
 
187
 
        h = NULL;
 
123
static int trylock(const std::multimap<std::wstring, unsigned long long int> &pids) {
188
124
        posptr = rotptr = NULL;
189
125
 
190
 
        DWORD pid=getProcess(L"hl2.exe");
191
 
        if (!pid)
192
 
                return false;
193
 
        BYTE *mod=getModuleAddr(pid, L"client.dll");
194
 
        if (!mod)
195
 
                return false;
196
 
        BYTE *mod_engine=getModuleAddr(pid, L"engine.dll");
 
126
        if (! initialize(pids, L"hl2.exe", L"client.dll"))
 
127
                return false;
 
128
 
 
129
        BYTE *mod_engine=getModuleAddr(L"engine.dll");
197
130
        if (!mod_engine)
198
131
                return false;
199
132
 
200
 
        h=OpenProcess(PROCESS_VM_READ, false, pid);
201
 
        if (!h)
202
 
                return false;
203
 
 
204
133
        // Check if we really have DODS running
205
134
        /*
206
135
                position tuple:         client.dll+0x3f62a0  (x,y,z, float)
211
140
        */
212
141
 
213
142
        // Remember addresses for later
214
 
        posptr = mod_engine + 0x555BB4;
215
 
        rotptr = mod_engine + 0x3CDC30;
216
 
        stateptr = mod + 0x3E7BDC;
217
 
        hostptr = mod_engine + 0x3C91A4;
 
143
        posptr = pModule + 0x4183F8;
 
144
        rotptr = pModule + 0x4183B8;
 
145
        stateptr = pModule + 0x404C24;
 
146
        hostptr = mod_engine + 0x3D3E94;
218
147
 
219
148
        // Gamecheck
220
149
        char sMagic[17];
221
 
        if (!peekProc(mod + 0x3FBE49, sMagic, 17) || strncmp("DODSpectatorGUI@@", sMagic, 17)!=0)
 
150
        if (!peekProc(pModule + 0x418ED1, sMagic, 17) || strncmp("DODSpectatorGUI@@", sMagic, 17)!=0)
222
151
                return false;
223
152
 
224
153
        // Check if we can get meaningful data from it
227
156
        wstring sidentity;
228
157
        string scontext;
229
158
 
230
 
        if (fetch(apos, afront, atop, cpos, cfront, ctop, scontext, sidentity))
 
159
        if (fetch(apos, afront, atop, cpos, cfront, ctop, scontext, sidentity)) {
231
160
                return true;
232
 
 
233
 
        // If it failed clean up
234
 
        CloseHandle(h);
235
 
        h = NULL;
236
 
        return false;
237
 
}
238
 
 
239
 
static void unlock() {
240
 
        if (h) {
241
 
                CloseHandle(h);
242
 
                h = NULL;
 
161
        } else {
 
162
                generic_unlock();
 
163
                return false;
243
164
        }
244
 
        return;
245
165
}
246
166
 
247
167
static const std::wstring longdesc() {
248
 
        return std::wstring(L"Supports DODS build 4069. No identity support yet.");
 
168
        return std::wstring(L"Supports DODS build 4426. No identity support yet.");
249
169
}
250
170
 
251
 
static std::wstring description(L"Day of Defeat: Source (Build 4069)");
 
171
static std::wstring description(L"Day of Defeat: Source (Build 4426)");
252
172
static std::wstring shortname(L"Day of Defeat: Source");
253
173
 
 
174
static int trylock1() {
 
175
        return trylock(std::multimap<std::wstring, unsigned long long int>());
 
176
}
 
177
 
254
178
static MumblePlugin dodsplug = {
255
179
        MUMBLE_PLUGIN_MAGIC,
256
180
        description,
257
181
        shortname,
258
 
        about,
259
 
        NULL,
260
 
        trylock,
261
 
        unlock,
 
182
        NULL,
 
183
        NULL,
 
184
        trylock1,
 
185
        generic_unlock,
262
186
        longdesc,
263
187
        fetch
264
188
};
265
189
 
 
190
static MumblePlugin2 dodsplug2 = {
 
191
        MUMBLE_PLUGIN_MAGIC_2,
 
192
        MUMBLE_PLUGIN_VERSION,
 
193
        trylock
 
194
};
 
195
 
266
196
extern "C" __declspec(dllexport) MumblePlugin *getMumblePlugin() {
267
197
        return &dodsplug;
268
198
}
 
199
 
 
200
extern "C" __declspec(dllexport) MumblePlugin2 *getMumblePlugin2() {
 
201
        return &dodsplug2;
 
202
}