Advertisement
Guest User

Untitled

a guest
Oct 26th, 2019
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. #define CURL_STATICLIB
  2. #include "mumble_plugin_win32.h"
  3. #include "curl\curl.h"
  4. #include "nlohmann\json.hpp"
  5. #include <sstream>
  6.  
  7. using json = nlohmann::json;
  8.  
  9. using namespace std;
  10.  
  11. static std::wstring desc(L"Echo Arena v1.0.0");
  12. static std::wstring name(L"Echo Arena");
  13. const std::string url("http://127.0.0.1/session");
  14.  
  15. constexpr char* PLAYERS = "players";
  16. constexpr char* TEAMS = "teams";
  17. constexpr char* POSITION = "position";
  18. constexpr char* FORWARD = "forward";
  19. constexpr char* UP = "up";
  20.  
  21. struct MemoryStruct {
  22.     char* memory;
  23.     size_t size;
  24. };
  25.  
  26.  
  27. static size_t
  28. WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
  29. {
  30.     size_t realsize = size * nmemb;
  31.     struct MemoryStruct* mem = (struct MemoryStruct*)userp;
  32.  
  33.     char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
  34.     if (ptr == NULL) {
  35.         printf("not enough memory (realloc returned NULL)\n");
  36.         return 0;
  37.     }
  38.  
  39.     mem->memory = ptr;
  40.     memcpy(&(mem->memory[mem->size]), contents, realsize);
  41.     mem->size += realsize;
  42.     mem->memory[mem->size] = 0;
  43.  
  44.     return realsize;
  45. }
  46.  
  47. const static std::wstring longDesc()
  48. {
  49.     return(L"Echo Arena Positional Audio Support");
  50. }
  51.  
  52. CURL* initCurl()
  53. {
  54.     CURL* curl_handle;
  55.     CURLcode res;
  56.  
  57.     /* init the curl session */
  58.     curl_handle = curl_easy_init();
  59.     if (!curl_handle)
  60.     {
  61.         std::cout << "Error initialising easy curl!...exiting\n";
  62.         return 0;
  63.     }
  64.  
  65.     return curl_handle;
  66.  
  67. }
  68.  
  69. void parseData(MemoryStruct* chunk, CURL* curl_handle)
  70. {
  71.     curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
  72.  
  73.     /* send all data to this function  */
  74.     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  75.  
  76.     /* we pass our 'chunk' struct to the callback function */
  77.     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)& chunk);
  78.  
  79. }
  80.  
  81. int fetchData(float* avatarPos, float* avatarFront, float* avatarTop)
  82. {
  83.     CURL* curl_handle = initCurl();
  84.  
  85.     CURLcode res;
  86.  
  87.  
  88.     /*struct MemoryStruct chunk;
  89.  
  90.     chunk.memory = (char*)malloc(1);
  91.     chunk.size = 0;
  92.     do
  93.     {
  94.         parseData(&chunk, curl_handle);
  95.     } while (!chunk.memory);*/
  96.  
  97.     /* some servers don't like requests that are made without a user-agent
  98.     field, so we provide one */
  99.     curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  100.     curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1L);
  101.  
  102.     res = curl_easy_perform(curl_handle);
  103.  
  104.     /* check for errors */
  105.     if (res != CURLE_OK) {
  106.         fprintf(stderr, "curl_easy_perform() failed: %s\n",
  107.             curl_easy_strerror(res));
  108.  
  109.         return 1;
  110.     }
  111.     else {
  112.  
  113.         json echoJson;
  114.  
  115.         if (CURLE_HTTP_RETURNED_ERROR)
  116.         {
  117.             std::cout << "No webpage data exists...exiting!\n";
  118.             return 1;
  119.         }
  120.  
  121.         stringstream ss;
  122.         //ss << chunk.memory;
  123.  
  124.         ss >> echoJson;
  125.  
  126.         string client_name = echoJson["client_name"].get<string>();
  127.  
  128.         for (auto& teams : echoJson[TEAMS])
  129.         {
  130.             if (echoJson["name"].get<string>() == client_name)
  131.             {
  132.  
  133.                 for (auto& players : teams[PLAYERS])
  134.                 {
  135.                     for (int posIndex = 0; posIndex < teams[POSITION].size(); posIndex++)
  136.                     {
  137.                         avatarPos[posIndex] = teams[POSITION][posIndex].get<float>();
  138.                     }
  139.  
  140.                     for (int frontIndex = 0; frontIndex < teams[FORWARD].size(); frontIndex++)
  141.                     {
  142.                         avatarFront[frontIndex] = teams[FORWARD][frontIndex].get<float>();
  143.                     }
  144.  
  145.                     for (int topIndex = 0; topIndex < teams[UP].size(); topIndex++)
  146.                     {
  147.                         avatarTop[topIndex] = teams[UP][topIndex].get<float>();
  148.                     }
  149.  
  150.                 }
  151.  
  152.             }
  153.             else
  154.                 continue;
  155.         }
  156.     }
  157.  
  158.     curl_easy_cleanup(curl_handle);
  159.  
  160.     //free(chunk.memory);
  161.     curl_global_cleanup();
  162. }
  163.  
  164.  
  165. void resetValues(float* avatar_pos, float* avatar_front, float* avatar_top, float* camera_pos, float* camera_front, float* camera_top)
  166. {
  167.     for (int i = 0; i < 3; i++)
  168.     {
  169.         avatar_pos[i] = avatar_front[i] = avatar_top[i] = camera_pos[i] = camera_front[i] = camera_top[i] = 0.0f;
  170.     }
  171. }
  172.  
  173. 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) {
  174.     resetValues(avatar_pos, avatar_front, avatar_top, camera_pos, camera_front, camera_top);
  175.  
  176.     if (fetchData(avatar_pos, avatar_front, avatar_top) == 1)
  177.         return false;
  178.  
  179.     for (int i = 0; i < 3; i++)
  180.     {
  181.         camera_pos[i] = avatar_pos[i];
  182.         camera_front[i] = avatar_front[i];
  183.         camera_top[i] = avatar_top[i];
  184.  
  185.     }
  186.  
  187.     return true;
  188. }
  189.  
  190. static int trylock(const std::multimap<std::wstring, unsigned long long int>& pids)
  191. {
  192.     std::cout << "Trylock Initialising\n";
  193.     if (!initialize(pids, L"echovr.exe"))
  194.     {
  195.         std::cout << "Cannot find echovr.exe\n";
  196.         return false;
  197.     }
  198.  
  199.     float apos[3], afront[3], atop[3], cpos[3], cfront[3], ctop[3];
  200.     std::wstring identity;
  201.     std::string context;
  202.  
  203.     if (fetch(apos, afront, atop, cpos, cfront, ctop, context, identity))
  204.     {
  205.         std::cout << "Successfully fetched API values\n";
  206.         return true;
  207.     }
  208.  
  209.     else
  210.     {
  211.         std::cout << "It's goofed somewhere!\n";
  212.         generic_unlock();
  213.         return false;
  214.     }
  215.    
  216. }
  217.  
  218. static int trylock1()
  219. {
  220.     std::cout << "Trylock1 Initialising\n";
  221.     return trylock(std::multimap<std::wstring, unsigned long long int>());
  222.    
  223. }
  224.  
  225. static MumblePlugin echoPlug = {
  226.     MUMBLE_PLUGIN_MAGIC,
  227.     desc,
  228.     name,
  229.     NULL,
  230.     NULL,
  231.     trylock1,
  232.     generic_unlock,
  233.     longDesc,
  234.     fetch
  235. };
  236.  
  237. static MumblePlugin2 echoPlug2
  238. {
  239.     MUMBLE_PLUGIN_MAGIC_2,
  240.     MUMBLE_PLUGIN_VERSION,
  241.     trylock
  242. };
  243.  
  244. extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin* getMumblePlugin()
  245. {
  246.     return &echoPlug;
  247. }
  248. extern "C" MUMBLE_PLUGIN_EXPORT MumblePlugin2* getMumblePlugin2()
  249. {
  250.     return &echoPlug2;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement