Add mumble support

This commit is contained in:
L0L022 2016-08-03 17:08:45 +02:00
parent 2b5c49d17b
commit 8dc0a1de1b
5 changed files with 203 additions and 0 deletions

View File

@ -102,4 +102,8 @@ else()
target_link_libraries(OpenSpades Xext)
endif()
if(UNIX)
target_link_libraries(OpenSpades rt)
endif()
#install(TARGETS OpenSpades DESTINATION bin)

View File

@ -346,6 +346,14 @@ namespace spades {
renderer->RegisterImage("Gfx/HurtRing2.png");
audioDevice->RegisterSound("Sounds/Feedback/Chat.wav");
if (mumbleLink.init())
SPLog("Mumble linked");
else
SPLog("Mumble link failed");
mumbleLink.setContext(hostname.asString(false));
mumbleLink.setIdentity(playerName);
SPLog("Started connecting to '%s'", hostname.asString(true).c_str());
net.reset(new NetClient(this));
net->Connect(hostname);
@ -434,6 +442,7 @@ namespace spades {
if(world){
UpdateWorld(dt);
mumbleLink.update(world->GetLocalPlayer());
}else{
renderer->SetFogColor(MakeVector3(0.f, 0.f, 0.f));
}

View File

@ -32,6 +32,7 @@
#include <Gui/View.h>
#include <memory>
#include <Core/Stopwatch.h>
#include "MumbleLink.h"
namespace spades {
class IStream;
@ -115,6 +116,8 @@ namespace spades {
int frameToRendererInit;
float timeSinceInit;
MumbleLink mumbleLink;
// view/drawing state for some world objects
std::vector<Handle<ClientPlayer>> clientPlayers;

View File

@ -0,0 +1,146 @@
#include "MumbleLink.h"
#include <cassert>
#include <cstring>
#ifdef WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
MumbleLink::MumbleLink() : metre_per_block(0.63), mumbleLinkedMemory(nullptr) {}
MumbleLink::~MumbleLink() {
#ifdef WIN32
UnmapViewOfFile(mumbleLinkedMemory);
if (obj != nullptr)
CloseHandle(obj);
#else
munmap(mumbleLinkedMemory, sizeof(*mumbleLinkedMemory));
if (fd > 0)
close(fd);
#endif
}
void MumbleLink::set_mumble_vector3(float mumble_vec[3],
const spades::Vector3 &vec) {
mumble_vec[0] = vec.x;
mumble_vec[1] = vec.z;
mumble_vec[2] = vec.y;
}
bool MumbleLink::init() {
assert(mumbleLinkedMemory == nullptr);
#ifdef WIN32
obj = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");
if (obj == nullptr)
return false;
mumbleLinkedMemory = static_cast<MumbleLinkedMemory *>(MapViewOfFile(
obj, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(*mumbleLinkedMemory)));
if (mumbleLinkedMemory == nullptr) {
CloseHandle(obj);
obj = nullptr;
return false;
}
#else
std::string name = "/MumbleLink." + std::to_string(getuid());
fd = shm_open(name.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
return false;
}
mumbleLinkedMemory = static_cast<MumbleLinkedMemory *>(
(mmap(nullptr, sizeof(*mumbleLinkedMemory), PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0)));
if (mumbleLinkedMemory == MAP_FAILED) {
mumbleLinkedMemory = nullptr;
return false;
}
#endif
return true;
}
void MumbleLink::setContext(const std::string &context) {
if (mumbleLinkedMemory == nullptr)
return;
size_t len(std::min(256, static_cast<int>(context.size())));
std::memcpy(mumbleLinkedMemory->context, context.c_str(), len);
mumbleLinkedMemory->context_len = len;
}
void MumbleLink::setIdentity(const std::string &identity) {
if (mumbleLinkedMemory == nullptr)
return;
std::wcsncpy(mumbleLinkedMemory->identity,
std::wstring(identity.begin(), identity.end()).c_str(), 256);
}
void MumbleLink::update(spades::client::Player *player) {
if (mumbleLinkedMemory == nullptr or player == nullptr)
return;
if (mumbleLinkedMemory->uiVersion != 2) {
wcsncpy(mumbleLinkedMemory->name, L"OpenSpades", 256);
wcsncpy(mumbleLinkedMemory->description, L"OpenSpades Link plugin.", 2048);
mumbleLinkedMemory->uiVersion = 2;
}
mumbleLinkedMemory->uiTick++;
// Left handed coordinate system.
// X positive towards "right".
// Y positive towards "up".
// Z positive towards "front".
//
// 1 unit = 1 meter
// spades::Vector3 playerFront(player->GetFront());
// spades::Vector3 playerTop(player->GetUp());
// spades::Vector3 playerPosition(player->GetPosition() * metre_per_block);
// Unit vector pointing out of the avatar's eyes aka "At"-vector.
// mumbleLinkedMemory->fAvatarFront[0] = playerFront.x;
// mumbleLinkedMemory->fAvatarFront[1] = playerFront.z;
// mumbleLinkedMemory->fAvatarFront[2] = playerFront.y;
set_mumble_vector3(mumbleLinkedMemory->fAvatarFront, player->GetFront());
// Unit vector pointing out of the top of the avatar's head aka "Up"-vector
// (here Top points straight up).
// mumbleLinkedMemory->fAvatarTop[0] = playerTop.x;
// mumbleLinkedMemory->fAvatarTop[1] = playerTop.z;
// mumbleLinkedMemory->fAvatarTop[2] = playerTop.y;
set_mumble_vector3(mumbleLinkedMemory->fAvatarTop, player->GetUp());
// Position of the avatar (here standing slightly off the origin)
// mumbleLinkedMemory->fAvatarPosition[0] = playerPosition.x;
// mumbleLinkedMemory->fAvatarPosition[1] = playerPosition.z;
// mumbleLinkedMemory->fAvatarPosition[2] = playerPosition.y;
set_mumble_vector3(mumbleLinkedMemory->fAvatarPosition,
player->GetPosition() * metre_per_block);
// Same as avatar but for the camera.
// mumbleLinkedMemory->fCameraPosition[0] =
// mumbleLinkedMemory->fAvatarPosition[0];
// mumbleLinkedMemory->fCameraPosition[1] =
// mumbleLinkedMemory->fAvatarPosition[1];
// mumbleLinkedMemory->fCameraPosition[2] =
// mumbleLinkedMemory->fAvatarPosition[2];
set_mumble_vector3(mumbleLinkedMemory->fCameraPosition,
player->GetPosition() * metre_per_block);
set_mumble_vector3(mumbleLinkedMemory->fCameraFront, player->GetFront());
set_mumble_vector3(mumbleLinkedMemory->fCameraTop, player->GetUp());
// mumbleLinkedMemory->fCameraFront[0] = mumbleLinkedMemory->fAvatarFront[0];
// mumbleLinkedMemory->fCameraFront[1] = mumbleLinkedMemory->fAvatarFront[1];
// mumbleLinkedMemory->fCameraFront[2] = mumbleLinkedMemory->fAvatarFront[2];
// mumbleLinkedMemory->fCameraTop[0] = mumbleLinkedMemory->fAvatarTop[0];
// mumbleLinkedMemory->fCameraTop[1] = mumbleLinkedMemory->fAvatarTop[1];
// mumbleLinkedMemory->fCameraTop[2] = mumbleLinkedMemory->fAvatarTop[2];
}

View File

@ -0,0 +1,41 @@
#include "Player.h"
#ifdef WIN32
#include <windef.h>
#endif
struct MumbleLinkedMemory {
uint32_t uiVersion;
uint32_t uiTick;
float fAvatarPosition[3];
float fAvatarFront[3];
float fAvatarTop[3];
wchar_t name[256];
float fCameraPosition[3];
float fCameraFront[3];
float fCameraTop[3];
wchar_t identity[256];
uint32_t context_len;
unsigned char context[256];
wchar_t description[2048];
};
class MumbleLink {
const float metre_per_block;
MumbleLinkedMemory *mumbleLinkedMemory;
#ifdef _WIN32
HANDLE obj;
#else
int fd;
#endif
void set_mumble_vector3(float mumble_vec[3], const spades::Vector3 &vec);
public:
MumbleLink();
~MumbleLink();
bool init();
void setContext(const std::string &context);
void setIdentity(const std::string &identity);
void update(spades::client::Player *player);
};