2011-08-16 22:07:30 +03:00
|
|
|
/*
|
2012-01-31 00:15:02 +03:00
|
|
|
* 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
|
|
|
|
|
*
|
2024-03-29 02:16:00 +01:00
|
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers
|
2011-08-16 22:07:30 +03:00
|
|
|
*/
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2012-10-14 23:38:39 +03:00
|
|
|
#ifdef USE_MUMBLE
|
|
|
|
|
|
2011-01-02 01:48:38 +02:00
|
|
|
#include "mumblemanager.h"
|
|
|
|
|
|
|
|
|
|
#include "configuration.h"
|
2011-08-12 03:47:28 +03:00
|
|
|
#include "logger.h"
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
#include "utils/mathutils.h"
|
|
|
|
|
|
2019-08-09 20:24:00 +03:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
2013-06-03 20:50:20 +03:00
|
|
|
#include <sys/param.h>
|
2019-08-09 20:24:00 +03:00
|
|
|
#endif // defined(__NetBSD__) || defined(__OpenBSD__)
|
2011-01-02 01:48:38 +02:00
|
|
|
#include <wchar.h>
|
2013-10-08 11:44:02 +03:00
|
|
|
#include <cctype>
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2017-06-20 03:06:00 +03:00
|
|
|
PRAGMA48(GCC diagnostic push)
|
|
|
|
|
PRAGMA48(GCC diagnostic ignored "-Wshadow")
|
2013-06-03 21:28:49 +03:00
|
|
|
#include <SDL_syswm.h>
|
2017-06-20 03:06:00 +03:00
|
|
|
PRAGMA48(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
|
#include <sys/mman.h>
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // WIN32
|
2011-05-24 00:38:44 +03:00
|
|
|
|
|
|
|
|
#include "debug.h"
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2014-05-29 01:08:33 +03:00
|
|
|
#ifdef USE_MUMBLE
|
|
|
|
|
MumbleManager *mumbleManager = nullptr;
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // USE_MUMBLE
|
2014-05-29 01:08:33 +03:00
|
|
|
|
2011-01-02 01:48:38 +02:00
|
|
|
MumbleManager::MumbleManager() :
|
2013-04-20 16:40:24 +03:00
|
|
|
mLinkedMem(nullptr),
|
|
|
|
|
mLinkedMemCache()
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
mMapBase[0] = mMapBase[1] = mMapBase[2] = 0.;
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MumbleManager::~MumbleManager()
|
|
|
|
|
{
|
|
|
|
|
if (mLinkedMem)
|
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
UnmapViewOfFile(mLinkedMem);
|
2011-06-30 19:32:54 +03:00
|
|
|
#elif defined BSD4_4
|
2016-09-23 22:01:44 +03:00
|
|
|
#else // WIN32
|
|
|
|
|
|
2017-07-06 20:50:20 +03:00
|
|
|
munmap(mLinkedMem, sizeof(LinkedMem));
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // WIN32
|
|
|
|
|
|
2011-11-08 00:44:17 +03:00
|
|
|
mLinkedMem = nullptr;
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 12:49:08 +03:00
|
|
|
uint16_t MumbleManager::getMapId(std::string mapName)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
uint16_t res = 0;
|
|
|
|
|
if (mapName.size() != 5 || mapName[3] != '-')
|
|
|
|
|
{
|
|
|
|
|
res = getCrc16(mapName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mapName = mapName.substr(0, 3) + mapName[4];
|
2016-02-07 16:18:13 +03:00
|
|
|
res = CAST_U16(atoi(mapName.c_str()));
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MumbleManager::setMapBase(uint16_t mapid)
|
|
|
|
|
{
|
2013-09-29 17:24:52 +03:00
|
|
|
mMapBase[0] = 10000.0F * (mapid & 0x1F);
|
2011-01-02 01:48:38 +02:00
|
|
|
mapid >>= 5;
|
2013-09-29 17:24:52 +03:00
|
|
|
mMapBase[1] = 1000.0F * (mapid & 0x3F);
|
2011-01-02 01:48:38 +02:00
|
|
|
mapid >>= 6;
|
2013-09-29 17:24:52 +03:00
|
|
|
mMapBase[2] = 10000.0F * (mapid & 0x1F);
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MumbleManager::init()
|
|
|
|
|
{
|
2011-06-30 19:32:54 +03:00
|
|
|
#if defined BSD4_4
|
2011-01-02 01:48:38 +02:00
|
|
|
return;
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // defined BSD4_4
|
|
|
|
|
|
2011-01-02 01:48:38 +02:00
|
|
|
if (mLinkedMem || !config.getBoolValue("enableMumble"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
logger->log1("MumbleManager::init");
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS,
|
|
|
|
|
FALSE, L"MumbleLink");
|
2011-11-07 19:34:52 +03:00
|
|
|
if (!hMapObject)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
2011-05-22 15:48:23 +03:00
|
|
|
logger->log1("MumbleManager::init can't open MumbleLink");
|
2011-01-02 01:48:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 17:06:02 +03:00
|
|
|
mLinkedMem = reinterpret_cast<LinkedMem *>(MapViewOfFile(hMapObject,
|
|
|
|
|
FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem)));
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2011-11-07 19:34:52 +03:00
|
|
|
if (!mLinkedMem)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
CloseHandle(hMapObject);
|
2011-11-07 19:34:52 +03:00
|
|
|
hMapObject = nullptr;
|
2011-05-22 15:48:23 +03:00
|
|
|
logger->log1("MumbleManager::init can't map MumbleLink");
|
2011-01-02 01:48:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2011-06-30 19:32:54 +03:00
|
|
|
#elif defined BSD4_4
|
2016-09-23 22:01:44 +03:00
|
|
|
#else // WIN32
|
|
|
|
|
|
2011-01-02 01:48:38 +02:00
|
|
|
char memName[256];
|
2013-04-25 22:18:26 +03:00
|
|
|
snprintf(memName, sizeof(memName), "/MumbleLink.%u", getuid());
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2012-09-02 14:05:30 +03:00
|
|
|
const int shmfd = shm_open(memName, O_RDWR, S_IRUSR | S_IWUSR);
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
if (shmfd < 0)
|
|
|
|
|
{
|
2011-05-24 19:28:25 +03:00
|
|
|
logger->log1("MumbleManager::init can't"
|
|
|
|
|
" open shared memory MumbleLink");
|
2011-01-02 01:48:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-09 03:16:39 +03:00
|
|
|
mLinkedMem = static_cast<LinkedMem *>(mmap(nullptr,
|
2017-07-06 20:50:20 +03:00
|
|
|
sizeof(LinkedMem), PROT_READ | PROT_WRITE,
|
2011-11-09 03:16:39 +03:00
|
|
|
MAP_SHARED, shmfd, 0));
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2011-03-18 17:48:29 +02:00
|
|
|
if (mLinkedMem == reinterpret_cast<void *>(-1))
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
2011-11-07 19:34:52 +03:00
|
|
|
mLinkedMem = nullptr;
|
2011-05-22 15:48:23 +03:00
|
|
|
logger->log1("MumbleManager::init can't map MumbleLink");
|
2011-01-02 01:48:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // WIN32
|
|
|
|
|
|
2024-03-29 02:16:00 +01:00
|
|
|
wcsncpy(mLinkedMemCache.name, L"ManaPlus", 256);
|
|
|
|
|
wcsncpy(mLinkedMemCache.description, L"ManaPlus Plugin", 2048);
|
2011-01-02 01:48:38 +02:00
|
|
|
mLinkedMemCache.uiVersion = 2;
|
|
|
|
|
|
|
|
|
|
// Left handed coordinate system.
|
|
|
|
|
// X positive towards "left".
|
|
|
|
|
// Y positive towards "up".
|
|
|
|
|
// Z positive towards "into screen".
|
|
|
|
|
//
|
|
|
|
|
// 1 unit = 1 meter
|
|
|
|
|
|
2013-04-14 11:27:04 +03:00
|
|
|
// Unit vector pointing out of the avatars eyes
|
2013-04-12 18:39:00 +03:00
|
|
|
// (here Front looks into scene).
|
2011-01-02 01:48:38 +02:00
|
|
|
/* no way to look "up", 2d */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarFront[1] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
// Unit vector pointing out of the top of the avatars head
|
|
|
|
|
// (here Top looks straight up).
|
|
|
|
|
/* no way to change this in tmw */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarTop[0] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarTop[1] = 1.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarTop[2] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fCameraFront[0] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fCameraFront[1] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fCameraFront[2] = 1.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fCameraTop[0] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fCameraTop[1] = 1.0F;
|
|
|
|
|
mLinkedMemCache.fCameraTop[2] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
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 ++;
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic push)
|
|
|
|
|
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
|
2011-01-02 01:48:38 +02:00
|
|
|
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-02 14:05:30 +03:00
|
|
|
void MumbleManager::setAction(const int action)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
if (!mLinkedMem)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-02-08 00:49:41 +03:00
|
|
|
switch (action)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
case 0: /* STAND */
|
|
|
|
|
case 1: /* WALK */
|
|
|
|
|
case 2: /* ATTACK */
|
|
|
|
|
case 5: /* HURT */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarPosition[1] = 1.5F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
case 3: /* SIT */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarPosition[1] = 1.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
2011-09-14 01:46:13 +03:00
|
|
|
case 4: /* DEAD */
|
2011-01-02 01:48:38 +02:00
|
|
|
default:
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarPosition[1] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
mLinkedMemCache.fAvatarPosition[1] += mMapBase[1];
|
|
|
|
|
mLinkedMemCache.fCameraPosition[1] = mLinkedMemCache.fAvatarPosition[1];
|
|
|
|
|
|
|
|
|
|
mLinkedMemCache.uiTick++;
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic push)
|
|
|
|
|
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
|
2011-01-02 01:48:38 +02:00
|
|
|
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-02 14:05:30 +03:00
|
|
|
void MumbleManager::setPos(const int tileX, const int tileY,
|
|
|
|
|
const int direction)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
if (!mLinkedMem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Position of the avatar (here standing slightly off the origin)
|
|
|
|
|
// lm->fAvatarPosition
|
|
|
|
|
|
|
|
|
|
/* tmw coordinates work exactly the other way round */
|
2011-03-18 22:13:37 +02:00
|
|
|
mLinkedMemCache.fAvatarPosition[0] = static_cast<float>(tileX)
|
|
|
|
|
+ mMapBase[0];
|
|
|
|
|
mLinkedMemCache.fAvatarPosition[2] = static_cast<float>(tileY)
|
|
|
|
|
+ mMapBase[2];
|
2011-01-02 01:48:38 +02:00
|
|
|
|
|
|
|
|
// 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).
|
2012-02-08 00:49:41 +03:00
|
|
|
switch (direction)
|
2011-01-02 01:48:38 +02:00
|
|
|
{
|
|
|
|
|
case 4: /* UP */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarFront[0] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarFront[2] = 1.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
case 1: /* DOWN */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarFront[0] = 0.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarFront[2] = -1.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
case 2: /* LEFT */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarFront[0] = 1.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarFront[2] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
case 8: /* RIGHT */
|
2013-09-29 17:24:52 +03:00
|
|
|
mLinkedMemCache.fAvatarFront[0] = -1.0F;
|
|
|
|
|
mLinkedMemCache.fAvatarFront[2] = 0.0F;
|
2011-01-02 01:48:38 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLinkedMemCache.uiTick ++;
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic push)
|
|
|
|
|
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
|
2011-01-02 01:48:38 +02:00
|
|
|
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2016-02-07 16:18:13 +03:00
|
|
|
unsigned size = CAST_U32(serverName.size());
|
2011-01-02 01:48:38 +02:00
|
|
|
if (size > sizeof(mLinkedMemCache.context) - 1)
|
|
|
|
|
size = sizeof(mLinkedMemCache.context) - 1;
|
|
|
|
|
|
|
|
|
|
memset(mLinkedMemCache.context, 0, sizeof(mLinkedMemCache.context));
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic push)
|
|
|
|
|
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
|
2011-01-02 01:48:38 +02:00
|
|
|
memcpy(mLinkedMemCache.context, serverName.c_str(), size);
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
mLinkedMemCache.context[size] = '\0';
|
|
|
|
|
mLinkedMemCache.context_len = size;
|
|
|
|
|
mLinkedMemCache.uiTick ++;
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic push)
|
|
|
|
|
PRAGMA8(GCC diagnostic ignored "-Wclass-memaccess")
|
2011-01-02 01:48:38 +02:00
|
|
|
memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
|
2017-06-20 01:43:28 +03:00
|
|
|
PRAGMA8(GCC diagnostic pop)
|
2011-01-02 01:48:38 +02:00
|
|
|
}
|
2012-10-14 23:38:39 +03:00
|
|
|
|
2016-09-23 22:01:44 +03:00
|
|
|
#endif // USE_MUMBLE
|