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
|
/*
* Code taken from: http://mumble.sourceforge.net/Link
*
* All code listed below is in the public domain and can be used, shared or
* modified freely
*
* Copyright (C) 2011-2020 The ManaPlus Developers
* Copyright (C) 2020-2023 The ManaVerse Developers
*/
#ifdef USE_MUMBLE
#include "mumblemanager.h"
#include "configuration.h"
#include "logger.h"
#include "utils/mathutils.h"
#if defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/param.h>
#endif // defined(__NetBSD__) || defined(__OpenBSD__)
#include <wchar.h>
#include <cctype>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
PRAGMA48(GCC diagnostic push)
PRAGMA48(GCC diagnostic ignored "-Wshadow")
#include <SDL_syswm.h>
PRAGMA48(GCC diagnostic pop)
#ifndef WIN32
#include <sys/mman.h>
#endif // WIN32
#include "debug.h"
#ifdef USE_MUMBLE
MumbleManager *mumbleManager = nullptr;
#endif // USE_MUMBLE
MumbleManager::MumbleManager() :
mLinkedMem(nullptr),
mLinkedMemCache()
{
mMapBase[0] = mMapBase[1] = mMapBase[2] = 0.;
init();
}
MumbleManager::~MumbleManager()
{
if (mLinkedMem)
{
#ifdef WIN32
UnmapViewOfFile(mLinkedMem);
#elif defined BSD4_4
#else // WIN32
munmap(mLinkedMem, sizeof(LinkedMem));
#endif // WIN32
mLinkedMem = nullptr;
}
}
uint16_t MumbleManager::getMapId(std::string mapName)
{
uint16_t res = 0;
if (mapName.size() != 5 || mapName[3] != '-')
{
res = getCrc16(mapName);
}
else
{
mapName = mapName.substr(0, 3) + mapName[4];
res = CAST_U16(atoi(mapName.c_str()));
}
return res;
}
void MumbleManager::setMapBase(uint16_t mapid)
{
mMapBase[0] = 10000.0F * (mapid & 0x1F);
mapid >>= 5;
mMapBase[1] = 1000.0F * (mapid & 0x3F);
mapid >>= 6;
mMapBase[2] = 10000.0F * (mapid & 0x1F);
}
void MumbleManager::init()
{
#if defined BSD4_4
return;
#endif // defined BSD4_4
if (mLinkedMem || !config.getBoolValue("enableMumble"))
return;
logger->log1("MumbleManager::init");
#ifdef WIN32
HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS,
FALSE, L"MumbleLink");
if (!hMapObject)
{
logger->log1("MumbleManager::init can't open MumbleLink");
return;
}
mLinkedMem = reinterpret_cast<LinkedMem *>(MapViewOfFile(hMapObject,
FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem)));
if (!mLinkedMem)
{
CloseHandle(hMapObject);
hMapObject = nullptr;
logger->log1("MumbleManager::init can't map MumbleLink");
return;
}
#elif defined BSD4_4
#else // WIN32
char memName[256];
snprintf(memName, sizeof(memName), "/MumbleLink.%u", getuid());
const int shmfd = shm_open(memName, O_RDWR, S_IRUSR | S_IWUSR);
if (shmfd < 0)
{
logger->log1("MumbleManager::init can't"
" open shared memory MumbleLink");
return;
}
mLinkedMem = static_cast<LinkedMem *>(mmap(nullptr,
sizeof(LinkedMem), PROT_READ | PROT_WRITE,
MAP_SHARED, shmfd, 0));
if (mLinkedMem == reinterpret_cast<void *>(-1))
{
mLinkedMem = nullptr;
logger->log1("MumbleManager::init can't map MumbleLink");
return;
}
#endif // WIN32
wcsncpy(mLinkedMemCache.name, L"ManaVerse", 256);
wcsncpy(mLinkedMemCache.description, L"ManaVerse Plugin", 2048);
mLinkedMemCache.uiVersion = 2;
// Left handed coordinate system.
// X positive towards "left".
// Y positive towards "up".
// Z positive towards "into screen".
//
// 1 unit = 1 meter
// Unit vector pointing out of the avatars eyes
// (here Front looks into scene).
/* no way to look "up", 2d */
mLinkedMemCache.fAvatarFront[1] = 0.0F;
// Unit vector pointing out of the top of the avatars head
// (here Top looks straight up).
/* no way to change this in tmw */
mLinkedMemCache.fAvatarTop[0] = 0.0F;
mLinkedMemCache.fAvatarTop[1] = 1.0F;
mLinkedMemCache.fAvatarTop[2] = 0.0F;
mLinkedMemCache.fCameraFront[0] = 0.0F;
mLinkedMemCache.fCameraFront[1] = 0.0F;
mLinkedMemCache.fCameraFront[2] = 1.0F;
mLinkedMemCache.fCameraTop[0] = 0.0F;
mLinkedMemCache.fCameraTop[1] = 1.0F;
mLinkedMemCache.fCameraTop[2] = 0.0F;
mLinkedMemCache.uiTick++;
}
void MumbleManager::setPlayer(const std::string &userName)
{
if (!mLinkedMem)
return;
// Identifier which uniquely identifies a certain player in a context
// (e.g. the ingame Name).
mbstowcs(mLinkedMemCache.identity, userName.c_str(), 256);
mLinkedMemCache.uiTick ++;
PRAGMA8(GCC diagnostic push)
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
PRAGMA8(GCC diagnostic pop)
}
void MumbleManager::setAction(const int action)
{
if (!mLinkedMem)
return;
switch (action)
{
case 0: /* STAND */
case 1: /* WALK */
case 2: /* ATTACK */
case 5: /* HURT */
mLinkedMemCache.fAvatarPosition[1] = 1.5F;
break;
case 3: /* SIT */
mLinkedMemCache.fAvatarPosition[1] = 1.0F;
break;
case 4: /* DEAD */
default:
mLinkedMemCache.fAvatarPosition[1] = 0.0F;
break;
}
mLinkedMemCache.fAvatarPosition[1] += mMapBase[1];
mLinkedMemCache.fCameraPosition[1] = mLinkedMemCache.fAvatarPosition[1];
mLinkedMemCache.uiTick++;
PRAGMA8(GCC diagnostic push)
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
PRAGMA8(GCC diagnostic pop)
}
void MumbleManager::setPos(const int tileX, const int tileY,
const int direction)
{
if (!mLinkedMem)
return;
// Position of the avatar (here standing slightly off the origin)
// lm->fAvatarPosition
/* tmw coordinates work exactly the other way round */
mLinkedMemCache.fAvatarPosition[0] = static_cast<float>(tileX)
+ mMapBase[0];
mLinkedMemCache.fAvatarPosition[2] = static_cast<float>(tileY)
+ mMapBase[2];
// Same as avatar but for the camera.
// lm->fCameraPosition, fCameraFront, fCameraTop
// Same as avatar but for the camera.
mLinkedMemCache.fCameraPosition[0] = mLinkedMemCache.fAvatarPosition[0];
mLinkedMemCache.fCameraPosition[2] = mLinkedMemCache.fAvatarPosition[2];
// Unit vector pointing out of the avatars eyes
// (here Front looks into scene).
switch (direction)
{
case 4: /* UP */
mLinkedMemCache.fAvatarFront[0] = 0.0F;
mLinkedMemCache.fAvatarFront[2] = 1.0F;
break;
case 1: /* DOWN */
mLinkedMemCache.fAvatarFront[0] = 0.0F;
mLinkedMemCache.fAvatarFront[2] = -1.0F;
break;
case 2: /* LEFT */
mLinkedMemCache.fAvatarFront[0] = 1.0F;
mLinkedMemCache.fAvatarFront[2] = 0.0F;
break;
case 8: /* RIGHT */
mLinkedMemCache.fAvatarFront[0] = -1.0F;
mLinkedMemCache.fAvatarFront[2] = 0.0F;
break;
default:
break;
}
mLinkedMemCache.uiTick ++;
PRAGMA8(GCC diagnostic push)
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
PRAGMA8(GCC diagnostic pop)
}
void MumbleManager::setMap(const std::string &mapName)
{
if (!mLinkedMem)
return;
// Context should be equal for players which should be able to hear each
// other positional and differ for those who shouldn't
// (e.g. it could contain the server+port and team)
setMapBase(getMapId(mapName));
setAction(0); /* update y coordinate */
}
void MumbleManager::setServer(const std::string &serverName)
{
if (!mLinkedMem)
return;
unsigned size = CAST_U32(serverName.size());
if (size > sizeof(mLinkedMemCache.context) - 1)
size = sizeof(mLinkedMemCache.context) - 1;
memset(mLinkedMemCache.context, 0, sizeof(mLinkedMemCache.context));
PRAGMA8(GCC diagnostic push)
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
memcpy(mLinkedMemCache.context, serverName.c_str(), size);
PRAGMA8(GCC diagnostic pop)
mLinkedMemCache.context[size] = '\0';
mLinkedMemCache.context_len = size;
mLinkedMemCache.uiTick ++;
PRAGMA8(GCC diagnostic push)
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
PRAGMA8(GCC diagnostic pop)
}
#endif // USE_MUMBLE
|