summaryrefslogtreecommitdiff
path: root/src/mumblemanager.cpp
blob: fa09908d62c9be4f94bfd21c09ffb1595e3ce03b (plain) (blame)
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
/* Code taken from: http://mumble.sourceforge.net/Link */

#include "mumblemanager.h"

#include "configuration.h"
#include "log.h"

#include "utils/mathutils.h"

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <ctype.h>

#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>

#include <SDL_syswm.h>

#ifndef WIN32
#include <sys/mman.h>
#endif 

MumbleManager::MumbleManager() :
    mLinkedMem(0)
{
    mMapBase[0] = mMapBase[1] = mMapBase[2] = 0.;
    init();
}

MumbleManager::~MumbleManager()
{
    if (mLinkedMem)
    {
#ifdef WIN32
        UnmapViewOfFile(mLinkedMem);
#elif defined __FreeBSD__ || defined __DragonFly__
#else
        munmap(mLinkedMem, sizeof(struct LinkedMem));
#endif
        mLinkedMem = 0;
    }
}

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 = static_cast<uint16_t>(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 __FreeBSD__ || defined __DragonFly__
    return;
#endif

    if (mLinkedMem || !config.getBoolValue("enableMumble"))
        return;

    logger->log1("MumbleManager::init");
#ifdef WIN32
    HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS,
                                         FALSE, L"MumbleLink");
    if (hMapObject == NULL)
    {
        logger->log1("MumbleManager::init cant open MumbleLink");
        return;
    }

    mLinkedMem = (LinkedMem *) MapViewOfFile(hMapObject,
        FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));

    if (mLinkedMem == NULL)
    {
        CloseHandle(hMapObject);
        hMapObject = NULL;
        logger->log1("MumbleManager::init cant map MumbleLink");
        return;
    }
#elif defined __FreeBSD__ || defined __DragonFly__
#else
    char memName[256];
    snprintf(memName, 256, "/MumbleLink.%d", getuid());

    int shmfd = shm_open(memName, O_RDWR, S_IRUSR | S_IWUSR);

    if (shmfd < 0)
    {
        logger->log1("MumbleManager::init cant open shared memory MumbleLink");
        return;
    }

    mLinkedMem = static_cast<LinkedMem *>(mmap(NULL, sizeof(struct LinkedMem),
        PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0));

    if (mLinkedMem == reinterpret_cast<void *>(-1))
    {
        mLinkedMem = NULL;
        logger->log1("MumbleManager::init cant map MumbleLink");
        return;
    }

#endif
    wcsncpy(mLinkedMemCache.name, L"ManaPlus", 256);
    wcsncpy(mLinkedMemCache.description, L"ManaPlus 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 ++;
    memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
}

void MumbleManager::setAction(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++;
    memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
}

void MumbleManager::setPos(int tileX, int tileY, 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 ++;
    memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
}

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 = static_cast<unsigned>(serverName.size());
    if (size > sizeof(mLinkedMemCache.context) - 1)
        size = sizeof(mLinkedMemCache.context) - 1;

    memset(mLinkedMemCache.context, 0, sizeof(mLinkedMemCache.context));
    memcpy(mLinkedMemCache.context, serverName.c_str(), size);
    mLinkedMemCache.context[size] = '\0';
    mLinkedMemCache.context_len = size;
    mLinkedMemCache.uiTick ++;
    memcpy(mLinkedMem, &mLinkedMemCache, sizeof(mLinkedMemCache));
}