Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CURL_STATICLIB
- #include "mumble_plugin_win32.h"
- #include "curl\curl.h"
- #include "nlohmann\json.hpp"
- #include <sstream>
- using json = nlohmann::json;
- using namespace std;
- static std::wstring desc(L"Echo Arena v1.0.0");
- static std::wstring name(L"Echo Arena");
- const std::string url("http://127.0.0.1/session");
- constexpr char* PLAYERS = "players";
- constexpr char* TEAMS = "teams";
- constexpr char* POSITION = "position";
- constexpr char* FORWARD = "forward";
- constexpr char* UP = "up";
- struct MemoryStruct {
- char* memory;
- size_t size;
- };
- static size_t
- WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
- {
- size_t realsize = size * nmemb;
- struct MemoryStruct* mem = (struct MemoryStruct*)userp;
- char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
- if (ptr == NULL) {
- printf("not enough memory (realloc returned NULL)\n");
- return 0;
- }
- mem->memory = ptr;
- memcpy(&(mem->memory[mem->size]), contents, realsize);
- mem->size += realsize;
- mem->memory[mem->size] = 0;
- return realsize;
- }
- const static std::wstring longDesc()
- {
- return(L"Echo Arena Positional Audio Support");
- }
- CURL* initCurl()
- {
- CURL* curl_handle;
- CURLcode res;
- /* init the curl session */
- curl_handle = curl_easy_init();
- if (!curl_handle)
- {
- std::cout << "Error initialising easy curl!...exiting\n";
- return 0;
- }
- return curl_handle;
- }
- void parseData(MemoryStruct* chunk, CURL* curl_handle)
- {
- curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
- /* send all data to this function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- /* we pass our 'chunk' struct to the callback function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)& chunk);
- }
- int fetchData(float* avatarPos, float* avatarFront, float* avatarTop)
- {
- CURL* curl_handle = initCurl();
- CURLcode res;
- /*struct MemoryStruct chunk;
- chunk.memory = (char*)malloc(1);
- chunk.size = 0;
- do
- {
- parseData(&chunk, curl_handle);
- } while (!chunk.memory);*/
- /* some servers don't like requests that are made without a user-agent
- field, so we provide one */
- curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
- curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1L);
- res = curl_easy_perform(curl_handle);
- /* check for errors */
- if (res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- return 1;
- }
- else {
- json echoJson;
- if (CURLE_HTTP_RETURNED_ERROR)
- {
- std::cout << "No webpage data exists...exiting!\n";
- return 1;
- }
- stringstream ss;
- //ss << chunk.memory;
- ss >> echoJson;
- string client_name = echoJson["client_name"].get<string>();
- for (auto& teams : echoJson[TEAMS])
- {
- if (echoJson["name"].get<string>() == client_name)
- {
- for (auto& players : teams[PLAYERS])
- {
- for (int posIndex = 0; posIndex < teams[POSITION].size(); posIndex++)
- {
- avatarPos[posIndex] = teams[POSITION][posIndex].get<float>();
- }
- for (int frontIndex = 0; frontIndex < teams[FORWARD].size(); frontIndex++)
- {
- avatarFront[frontIndex] = teams[FORWARD][frontIndex].get<float>();
- }
- for (int topIndex = 0; topIndex < teams[UP].size(); topIndex++)
- {
- avatarTop[topIndex] = teams[UP][topIndex].get<float>();
- }
- }
- }
- else
- continue;
- }
- }
- curl_easy_cleanup(curl_handle);
- //free(chunk.memory);
- curl_global_cleanup();
- }
- void resetValues(float* avatar_pos, float* avatar_front, float* avatar_top, float* camera_pos, float* camera_front, float* camera_top)
- {
- for (int i = 0; i < 3; i++)
- {
- avatar_pos[i] = avatar_front[i] = avatar_top[i] = camera_pos[i] = camera_front[i] = camera_top[i] = 0.0f;
- }
- }
- static int fetch(float* avatar_pos, float* avatar_front, float* avatar_top, float* camera_pos, float* camera_front, float* camera_top, std::string& context, std::wstring& identity) {
- resetValues(avatar_pos, avatar_front, avatar_top, camera_pos, camera_front, camera_top);
- if (fetchData(avatar_pos, avatar_front, avatar_top) == 1)
- return false;
- for (int i = 0; i < 3; i++)
- {
- camera_pos[i] = avatar_pos[i];
- camera_front[i] = avatar_front[i];
- camera_top[i] = avatar_top[i];
- }
- return true;
- }
- static int trylock(const std::multimap<std::wstring, unsigned long long int>& pids)
- {
- std::cout << "Trylock Initialising\n";
- if (!initialize(pids, L"echovr.exe"))
- {
- std::cout << "Cannot find echovr.exe\n";
- return false;
- }
- float apos[3], afront[3], atop[3], cpos[3], cfront[3], ctop[3];
- std::wstring identity;
- std::string context;
- if (fetch(apos, afront, atop, cpos, cfront, ctop, context, identity))
- {
- std::cout << "Successfully fetched API values\n";
- return true;
- }
- else
- {
- std::cout << "It's goofed somewhere!\n";
- generic_unlock();
- return false;
- }
- }
- static int trylock1()
- {
- std::cout << "Trylock1 Initialising\n";
- return trylock(std::multimap<std::wstring, unsigned long long int>());
- }
- static MumblePlugin echoPlug = {
- MUMBLE_PLUGIN_MAGIC,
- desc,
- name,
- NULL,
- NULL,
- trylock1,
- generic_unlock,
- longDesc,
- fetch
- };
- static MumblePlugin2 echoPlug2
- {
- MUMBLE_PLUGIN_MAGIC_2,
- MUMBLE_PLUGIN_VERSION,
- trylock
- };
- extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin* getMumblePlugin()
- {
- return &echoPlug;
- }
- extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin2* getMumblePlugin2()
- {
- return &echoPlug2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement