Separated web requests to comp-sv into r5net (new project); commit is not functional

This commit is contained in:
Alex 2021-08-12 14:51:20 +03:00
parent 540ce8d2b0
commit ed36428cbb
17 changed files with 534 additions and 156 deletions

View File

@ -11,28 +11,56 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "r5dedicated", "r5dedicated\
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Minhook", "external\minhook\libMinHook.vcxproj", "{F142A341-5EE0-442D-A15F-98AE9B48DBAE}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "r5net", "r5net\r5net.vcxproj", "{F04BE619-0326-4FF1-B06B-FBE882E04D5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{18F8C75E-3844-4AA6-AB93-980A08253519}.Debug|x64.ActiveCfg = Debug|x64
{18F8C75E-3844-4AA6-AB93-980A08253519}.Debug|x64.Build.0 = Debug|x64
{18F8C75E-3844-4AA6-AB93-980A08253519}.Debug|x86.ActiveCfg = Debug|Win32
{18F8C75E-3844-4AA6-AB93-980A08253519}.Debug|x86.Build.0 = Debug|Win32
{18F8C75E-3844-4AA6-AB93-980A08253519}.Release|x64.ActiveCfg = Release|x64
{18F8C75E-3844-4AA6-AB93-980A08253519}.Release|x64.Build.0 = Release|x64
{18F8C75E-3844-4AA6-AB93-980A08253519}.Release|x86.ActiveCfg = Release|Win32
{18F8C75E-3844-4AA6-AB93-980A08253519}.Release|x86.Build.0 = Release|Win32
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Debug|x64.ActiveCfg = Debug|x64
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Debug|x64.Build.0 = Debug|x64
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Debug|x86.ActiveCfg = Debug|Win32
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Debug|x86.Build.0 = Debug|Win32
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Release|x64.ActiveCfg = Release|x64
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Release|x64.Build.0 = Release|x64
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Release|x86.ActiveCfg = Release|Win32
{28CC6B4F-7A95-4933-ADA9-65E38D48516D}.Release|x86.Build.0 = Release|Win32
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Debug|x64.ActiveCfg = Debug|x64
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Debug|x64.Build.0 = Debug|x64
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Debug|x86.ActiveCfg = Debug|Win32
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Debug|x86.Build.0 = Debug|Win32
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Release|x64.ActiveCfg = Release|x64
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Release|x64.Build.0 = Release|x64
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Release|x86.ActiveCfg = Release|Win32
{71988D92-343C-49AB-B52B-0AE0E83B0401}.Release|x86.Build.0 = Release|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x64.ActiveCfg = Debug|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x64.Build.0 = Debug|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x86.ActiveCfg = Debug|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Debug|x86.Build.0 = Debug|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x64.ActiveCfg = Release|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x64.Build.0 = Release|x64
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x86.ActiveCfg = Release|Win32
{F142A341-5EE0-442D-A15F-98AE9B48DBAE}.Release|x86.Build.0 = Release|Win32
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Debug|x64.ActiveCfg = Debug|x64
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Debug|x64.Build.0 = Debug|x64
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Debug|x86.ActiveCfg = Debug|Win32
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Debug|x86.Build.0 = Debug|Win32
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Release|x64.ActiveCfg = Release|x64
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Release|x64.Build.0 = Release|x64
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Release|x86.ActiveCfg = Release|Win32
{F04BE619-0326-4FF1-B06B-FBE882E04D5E}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

59
r5dev/BuildVersion.rc Normal file
View File

@ -0,0 +1,59 @@
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

View File

@ -1,6 +1,7 @@
#pragma once
#include "serverlisting.h"
#include "gui_utility.h"
#include "r5net.h"
///////////////////////////////////////////////////////////////////////////////
// Initialization
@ -13,6 +14,7 @@ private:
public:
CCompanion();
////////////////////
// Enums //
////////////////////
@ -32,15 +34,17 @@ public:
////////////////////
// Server Browser //
////////////////////
ImVector<ServerListing*> ServerList;
ServerListing* SelectedServer;
R5Net::Client r5net;
std::vector<ServerListing> ServerList;
ImGuiTextFilter ServerBrowserFilter;
char ServerConnStringBuffer[256] = { 0 };
////////////////////
// Settings //
////////////////////
char MatchmakingServerStringBuffer[256] = { 0 };
std::string MatchmakingServerStringBuffer;
////////////////////
// Host Server //
@ -135,7 +139,6 @@ public:
void RefreshServerList();
void SendHostingPostRequest();
const nlohmann::json SendGetServerByTokenRequest(const std::string &token, const std::string &password);
void CompMenu();
void ServerBrowserSection();
void SettingsSection();

View File

@ -1,22 +0,0 @@
#pragma once
class ServerListing
{
public:
ServerListing() = default;
ServerListing(std::string name, std::string map, std::string ip, std::string port) : name(name), map(map), ip(ip), port(port)
{
// for future constructor use.
}
void Select();
std::string name;
std::string map;
std::string ip;
std::string port;
std::string password;
};

View File

@ -82,16 +82,16 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir)external\minhook\include;$(SolutionDir)external\imgui\include;$(SolutionDir)external\spdlog\include;$(SolutionDir)shared\include;$(SolutionDir)r5dev\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)external\minhook\lib\$(Configuration);$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)r5net\include;$(SolutionDir)external\minhook\include;$(SolutionDir)external\imgui\include;$(SolutionDir)external\spdlog\include;$(SolutionDir)shared\include;$(SolutionDir)r5dev\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)external\minhook\lib\$(Configuration);$(SolutionDir)r5net\lib\$(Configuration);$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\$(Configuration)\</OutDir>
<TargetName>r5detours</TargetName>
<IntDir>$(SolutionDir)build\$(ProjectName)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)external\minhook\include;$(SolutionDir)external\imgui\include;$(SolutionDir)external\spdlog\include;$(SolutionDir)shared\include;$(SolutionDir)r5dev\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)external\minhook\lib\$(Configuration);$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)r5net\include;$(SolutionDir)external\minhook\include;$(SolutionDir)external\imgui\include;$(SolutionDir)external\spdlog\include;$(SolutionDir)shared\include;$(SolutionDir)r5dev\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)external\minhook\lib\$(Configuration);$(SolutionDir)r5net\lib\$(Configuration);$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)\$(Configuration)\</IntDir>
<TargetName>r5detours</TargetName>
@ -148,7 +148,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>r5dev.def</ModuleDefinitionFile>
<AdditionalDependencies>Minhook.x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Minhook.x64.lib;r5net.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>
@ -185,7 +185,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>r5dev.def</ModuleDefinitionFile>
<AdditionalDependencies>Minhook.x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Minhook.x64.lib;r5net.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>
@ -319,7 +319,6 @@
<ClInclude Include="include\patterns.h" />
<ClInclude Include="include\pch.h" />
<ClInclude Include="include\r5dev.h" />
<ClInclude Include="include\serverlisting.h" />
<ClInclude Include="include\structs.h" />
</ItemGroup>
<ItemGroup>
@ -400,14 +399,15 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">pch.h</PrecompiledHeaderFile>
</ClCompile>
<ClCompile Include="src\serverlisting.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|x64'">pch.h</PrecompiledHeaderFile>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="r5dev.def" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\r5net\r5net.vcxproj">
<Project>{f04be619-0326-4ff1-b06b-fbe882e04d5e}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -177,9 +177,6 @@
<ClCompile Include="src\CGameConsole.cpp">
<Filter>gui\interface</Filter>
</ClCompile>
<ClCompile Include="src\serverlisting.cpp">
<Filter>gui</Filter>
</ClCompile>
<ClCompile Include="src\id3dx.cpp">
<Filter>gui</Filter>
</ClCompile>
@ -521,9 +518,6 @@
<ClInclude Include="include\CGameConsole.h">
<Filter>gui\include</Filter>
</ClInclude>
<ClInclude Include="include\serverlisting.h">
<Filter>gui\include</Filter>
</ClInclude>
<ClInclude Include="include\gameclasses.h">
<Filter>r5-sdk\include</Filter>
</ClInclude>

14
r5dev/resource.h Normal file
View File

@ -0,0 +1,14 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by BuildVersion.rc
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

View File

@ -5,6 +5,7 @@
#include "patterns.h"
#include "gameclasses.h"
#include "CCompanion.h"
#include "r5net.h"
#define OVERLAY_DEBUG
@ -14,11 +15,9 @@ CCompanion* g_ServerBrowser = nullptr;
* _ccompanion.cpp
*-----------------------------------------------------------------------------*/
CCompanion::CCompanion()
CCompanion::CCompanion() : MatchmakingServerStringBuffer("r5a-comp-sv.herokuapp.com"), r5net(R5Net::Client("r5a-comp-sv.herokuapp.com"))
{
memset(MatchmakingServerStringBuffer, 0, sizeof(MatchmakingServerStringBuffer));
memset(ServerConnStringBuffer, 0, sizeof(ServerConnStringBuffer));
strcpy_s(MatchmakingServerStringBuffer, "r5a-comp-sv.herokuapp.com");
std::string path = "stbsp";
for (const auto& entry : std::filesystem::directory_iterator(path))
@ -87,19 +86,7 @@ void CCompanion::RefreshServerList()
std::cout << " [+CCompanion+] Refreshing server list with string " << MatchmakingServerStringBuffer << "\n";
#endif
bThreadLocked = true;
httplib::Client client(MatchmakingServerStringBuffer);
client.set_connection_timeout(10);
auto res = client.Get("/servers");
if (res)
{
nlohmann::json root = nlohmann::json::parse(res->body);
for (auto obj : root["servers"])
{
ServerList.push_back(
new ServerListing(obj["name"].get<std::string>(), obj["map"].get<std::string>(), obj["ip"].get<std::string>(), obj["port"].get<std::string>())
);
}
}
ServerList = r5net.GetServersList();
bThreadLocked = false;
});
@ -109,76 +96,25 @@ void CCompanion::RefreshServerList()
void CCompanion::SendHostingPostRequest()
{
httplib::Client client(MatchmakingServerStringBuffer);
client.set_connection_timeout(10);
// send a post request to "/servers/add" with a json body
nlohmann::json body = nlohmann::json::object();
body["name"] = MyServer.name;
body["map"] = std::string(GameGlobals::HostState->m_levelName);
static ConVar* hostport = GameGlobals::Cvar->FindVar("hostport"); // static since it won't move memory locations.
body["port"] = hostport->m_pzsCurrentValue; //body["port"] = MyServer.port;
body["password"] = MyServer.password;
std::string body_str = body.dump();
#ifdef OVERLAY_DEBUG
std::cout << " [+CCompanion+] Sending request now, Body: " << body_str << "\n";
#endif
httplib::Result result = client.Post("/servers/add", body_str.c_str(), body_str.length(), "application/json");
#ifdef OVERLAY_DEBUG
HostToken = "";
bool result = r5net.PostServerHost(HostRequestMessage, HostToken, ServerListing{ MyServer.name, std::string(GameGlobals::HostState->m_levelName), GameGlobals::Cvar->FindVar("hostport")->m_pzsCurrentValue, MyServer.password });
if (result)
{
std::cout << " [+CCompanion+] Request Result: " << result->body << "\n";
nlohmann::json res = nlohmann::json::parse(result->body);
if (!res["success"] && !res["err"].is_null())
HostRequestMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f);
std::stringstream msg;
msg << "Broadcasting! ";
if (!HostToken.empty())
{
HostRequestMessage = res["err"].get<std::string>();
HostRequestMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
}
else if (res["success"].get<bool>() == true)
{
std::stringstream msg;
msg << "Broadcasting! ";
HostToken = "";
if (res["token"].is_string())
{
msg << "Share the following token for people to connect: ";
HostToken = res["token"].get<std::string>();
}
HostRequestMessage = msg.str().c_str();
HostRequestMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f);
}
else
{
HostRequestMessage = "";
HostRequestMessageColor = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
msg << "Share the following token for people to connect: ";
}
HostRequestMessage = msg.str().c_str();
}
#endif
}
const nlohmann::json CCompanion::SendGetServerByTokenRequest(const std::string &token, const std::string &password)
{
httplib::Client client(MatchmakingServerStringBuffer);
client.set_connection_timeout(10);
nlohmann::json reqBody = nlohmann::json::object();
reqBody["token"] = token;
reqBody["password"] = password;
std::string reqBody_str = reqBody.dump();
httplib::Result res = client.Post("/server/byToken", reqBody_str.c_str(), reqBody_str.length(), "application/json");
if (res && !res->body.empty())
else
{
return nlohmann::json::parse(res->body);
HostRequestMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
}
return nlohmann::json::object();
}
@ -222,11 +158,11 @@ void CCompanion::ServerBrowserSection()
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 8);
ImGui::TableHeadersRow();
for (ServerListing* server : ServerList)
for (ServerListing& server : ServerList)
{
const char* name = server->name.c_str();
const char* map = server->map.c_str();
const char* port = server->port.c_str();
const char* name = server.name.c_str();
const char* map = server.map.c_str();
const char* port = server.port.c_str();
if (ServerBrowserFilter.PassFilter(name)
|| ServerBrowserFilter.PassFilter(map)
@ -243,12 +179,11 @@ void CCompanion::ServerBrowserSection()
ImGui::TableNextColumn();
std::string selectButtonText = "Connect##";
selectButtonText += (server->name + server->ip + server->map);
selectButtonText += (server.name + server.ip + server.map);
if (ImGui::Button(selectButtonText.c_str()))
{
SelectedServer = server;
server->Select();
//server->Select();
}
}
@ -370,27 +305,19 @@ void CCompanion::ServerBrowserSection()
if (ImGui::Button("Connect##PrivateServersConnectModal_ConnectButton", ImVec2(ImGui::GetWindowContentRegionWidth() / 2.f, 19)))
{
nlohmann::json response = SendGetServerByTokenRequest(PrivateServerToken, PrivateServerPassword); // Send token connect request.
if (response["success"].get<bool>()) // Was the response successful?
PrivateServerRequestMessage = "";
ServerListing server;
bool result = r5net.GetServerByToken(server, PrivateServerRequestMessage, PrivateServerToken, PrivateServerPassword); // Send token connect request.
if (!server.name.empty())
{
nlohmann::json server = response["server"].get<nlohmann::json>(); // Get server field.
if (server["ip"].is_string() && server["port"].is_string()) // Check if both field are a string.
{
std::string name = server["name"].get<std::string>(); // Please do this if you grab values from the response.
std::string ip = server["ip"].get<std::string>();
std::string port = server["port"].get<std::string>();
ConnectToServer(ip, port); // Connect to the server
PrivateServerRequestMessage = "Found Server: " + name;
PrivateServerMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f);
ImGui::CloseCurrentPopup();
}
ConnectToServer(server.ip, server.port); // Connect to the server
PrivateServerRequestMessage = "Found Server: " + server.name;
PrivateServerMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f);
ImGui::CloseCurrentPopup();
}
else
{
std::string err = response["err"].get<std::string>();
PrivateServerRequestMessage = "Error: " + err;
PrivateServerRequestMessage = "Error: " + PrivateServerRequestMessage;
PrivateServerMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
}
@ -494,7 +421,8 @@ void CCompanion::HostServerSection()
void CCompanion::SettingsSection()
{
ImGui::InputText("Matchmaking Server String", MatchmakingServerStringBuffer, IM_ARRAYSIZE(MatchmakingServerStringBuffer), 0);
ImGui::Text("In renovation");
//ImGui::InputText("Matchmaking Server String", MatchmakingServerStringBuffer, IM_ARRAYSIZE(MatchmakingServerStringBuffer), 0);
}
void CCompanion::Draw(const char* title)

View File

@ -1,10 +0,0 @@
#include "pch.h"
#include "serverlisting.h"
#include "CCompanion.h"
void ServerListing::Select()
{
std::stringstream cmd;
cmd << "connect " << this->ip << ":" << this->port;
g_ServerBrowser->ProcessCommand(cmd.str().c_str());
}

26
r5net/include/pch.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#pragma message("Precompiling r5net headers.\n")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <thread>
#include <fstream>
#include <stdio.h>
#include <filesystem>
#include <sstream>
#include <shlobj.h>
#include <objbase.h>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <d3d11.h>
#include <string>
#include <Psapi.h>
#include <vector>
#include <cstring>
///
#include "httplib.h"
#include "json.hpp"

31
r5net/include/r5net.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include "serverlisting.h"
namespace R5Net
{
struct Config
{
std::string MOTD;
int SERVER_TTL;
int MIN_REQUIRED_VERSION;
};
class Client
{
httplib::Client m_HttpClient;
public:
Client(std::string serverString) : m_HttpClient(serverString.c_str())
{
m_HttpClient.set_connection_timeout(10);
}
std::vector<ServerListing> GetServersList();
bool PostServerHost(std::string& outMessage, std::string& outToken, const ServerListing& serverListing);
bool GetServerByToken(ServerListing& outServer, std::string& outError, const std::string& token, const std::string& password = "");
};
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "pch.h"
struct ServerListing
{
std::string name;
std::string map;
std::string ip;
std::string port;
std::string password;
};

177
r5net/r5net.vcxproj Normal file
View File

@ -0,0 +1,177 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\pch.h" />
<ClInclude Include="include\r5net.h" />
<ClInclude Include="include\serverlisting.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="src\r5net.cpp" />
<ClCompile Include="src\serverlisting.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{f04be619-0326-4ff1-b06b-fbe882e04d5e}</ProjectGuid>
<RootNamespace>r5net</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)lib\$(Configuration)</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)\$(Configuration)</IntDir>
<IncludePath>$(SolutionDir)shared\include;$(ProjectDir)include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(ProjectDir)lib\$(Configuration)</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)\$(Configuration)</IntDir>
<IncludePath>$(SolutionDir)shared\include;$(ProjectDir)include;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>
</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>
</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="include">
<UniqueIdentifier>{676c0758-86e4-4159-8aaf-17969db2d381}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{a3728292-4416-44be-a05c-5642a99dbbad}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\r5net.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="include\serverlisting.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="include\pch.h">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\r5net.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\serverlisting.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\pch.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
</Project>

3
r5net/src/pch.cpp Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#include "pch.h"

98
r5net/src/r5net.cpp Normal file
View File

@ -0,0 +1,98 @@
// r5net.cpp : Defines the functions for the static library.
//
#include "pch.h"
#include "r5net.h"
using namespace R5Net;
std::vector<ServerListing> Client::GetServersList()
{
std::vector<ServerListing> list;
auto res = m_HttpClient.Get("/servers");
if (res)
{
nlohmann::json root = nlohmann::json::parse(res->body);
for (auto obj : root["servers"])
{
list.push_back(
ServerListing{ obj["name"].get<std::string>(), obj["map"].get<std::string>(), obj["ip"].get<std::string>(), obj["port"].get<std::string>() }
);
}
}
return list;
}
bool Client::PostServerHost(std::string& outMessage, std::string& outToken, const ServerListing& serverListing)
{
nlohmann::json reqBody = nlohmann::json::object();
reqBody["name"] = serverListing.name;
reqBody["map"] = serverListing.map;
reqBody["port"] = serverListing.port;
reqBody["password"] = serverListing.password;
std::string reqBodyStr = reqBody.dump();
auto res = m_HttpClient.Post("/servers/add", reqBodyStr.c_str(), reqBodyStr.length(), "application/json");
nlohmann::json resBody = nlohmann::json::parse(res->body);
if (res && resBody["success"].is_boolean() && resBody["success"])
{
outMessage = "Broadcasting!";
if (resBody["token"].is_string())
outToken = resBody["token"].get<std::string>();
else
outToken = "";
return true;
}
else
{
if (resBody["err"].is_string())
outMessage = resBody["err"].get<std::string>();
else
outMessage = "An unknown error occured!";
return false;
}
}
bool R5Net::Client::GetServerByToken(ServerListing& outServer, std::string& outError, const std::string& token, const std::string& password)
{
nlohmann::json reqBody = nlohmann::json::object();
reqBody["token"] = token;
reqBody["password"] = password;
httplib::Result res = m_HttpClient.Post("/server/byToken", reqBody.dump().c_str(), reqBody.dump().length(), "application/json");
std::cout << "YEEEEEEEEEEEEEE" << res->body << "\n";
nlohmann::json resBody = nlohmann::json::parse(res->body);
if (res && resBody["success"].is_boolean() && resBody["success"])
{
outServer = ServerListing{
reqBody["name"].get<std::string>(),
reqBody["map"].get<std::string>(),
reqBody["ip"].get<std::string>(),
reqBody["port"].get<std::string>()
};
return true;
}
else
{
if (resBody["err"].is_string())
outError = resBody["err"].get<std::string>();
else
outError = "";
outServer = ServerListing{};
return false;
}
}

View File

@ -0,0 +1,3 @@
#include "pch.h"
#include "serverlisting.h"