switched to nlohmann_json

This commit is contained in:
Anonymous275
2022-03-03 16:12:55 +02:00
parent 19d7120b13
commit 31cd89fd20
6 changed files with 36 additions and 46 deletions

View File

@@ -18,7 +18,7 @@ jobs:
uses: lukka/run-vcpkg@v7 uses: lukka/run-vcpkg@v7
id: runvcpkg id: runvcpkg
with: with:
vcpkgArguments: 'zlib discord-rpc rapidjson openssl minhook' vcpkgArguments: 'zlib discord-rpc nlohmann-json openssl minhook'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg' vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f' vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f'
vcpkgTriplet: 'x64-windows-static' vcpkgTriplet: 'x64-windows-static'

View File

@@ -42,7 +42,7 @@ jobs:
uses: lukka/run-vcpkg@main uses: lukka/run-vcpkg@main
id: runvcpkg id: runvcpkg
with: with:
vcpkgArguments: 'zlib discord-rpc rapidjson openssl minhook' vcpkgArguments: 'zlib discord-rpc nlohmann-json openssl minhook'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg' vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f' vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f'
vcpkgTriplet: 'x64-windows-static' vcpkgTriplet: 'x64-windows-static'

View File

@@ -10,6 +10,7 @@ if (WIN32)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
find_package(minhook CONFIG REQUIRED) find_package(minhook CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
#-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static #-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static
set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}) set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET})
include_directories(${VcpkgRoot}/include) include_directories(${VcpkgRoot}/include)
@@ -52,11 +53,11 @@ add_executable(${PROJECT_NAME}
if (WIN32) if (WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE ${VcpkgRoot}/lib/discord-rpc.lib target_link_libraries(${PROJECT_NAME} PRIVATE ${VcpkgRoot}/lib/discord-rpc.lib
ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp comsuppw minhook::minhook) ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp comsuppw minhook::minhook nlohmann_json nlohmann_json::nlohmann_json)
else(WIN32) #MINGW else(WIN32) #MINGW
add_definitions("-D_WIN32_WINNT=0x0600") add_definitions("-D_WIN32_WINNT=0x0600")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static")
target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp comsuppw minhook::minhook) target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp comsuppw minhook::minhook nlohmann_json nlohmann_json::nlohmann_json)
endif(WIN32) endif(WIN32)
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)
target_include_directories(${PROJECT_NAME} PRIVATE "include") target_include_directories(${PROJECT_NAME} PRIVATE "include")

View File

@@ -4,7 +4,6 @@
/// ///
#pragma once #pragma once
#include <rapidjson/stringbuffer.h> #include <nlohmann/json.hpp>
#include <rapidjson/document.h> using Json = nlohmann::json;
#include <rapidjson/writer.h>
namespace Json = rapidjson;

View File

@@ -5,12 +5,11 @@
#include "Launcher.h" #include "Launcher.h"
#include "Logger.h" #include "Logger.h"
#include <fstream>
#include "Http.h" #include "Http.h"
#include "Json.h" #include "Json.h"
void UpdateKey(const char* newKey){ void UpdateKey(const std::string& newKey){
if(newKey){ if(!newKey.empty()){
std::ofstream Key("key"); std::ofstream Key("key");
if(Key.is_open()){ if(Key.is_open()){
Key << newKey; Key << newKey;
@@ -38,41 +37,37 @@ std::string GetFail(const std::string& R){
std::string Launcher::Login(const std::string& fields) { std::string Launcher::Login(const std::string& fields) {
if(fields == "LO"){ if(fields == "LO"){
LoginAuth = false; LoginAuth = false;
UpdateKey(nullptr); UpdateKey("");
return ""; return "";
} }
LOG(INFO) << "Attempting to authenticate..."; LOG(INFO) << "Attempting to authenticate...";
std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields); std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
Json::Document d; Json d = Json::parse(Buffer, nullptr, false);
d.Parse(Buffer.c_str());
if(Buffer == "-1"){ if(Buffer == "-1"){
return GetFail("Failed to communicate with the auth system!"); return GetFail("Failed to communicate with the auth system!");
} }
if (Buffer.at(0) != '{' || d.HasParseError()) { if (Buffer.at(0) != '{' || d.is_discarded()) {
LOG(ERROR) << Buffer; LOG(ERROR) << Buffer;
return GetFail("Invalid answer from authentication servers, please try again later!"); return GetFail("Invalid answer from authentication servers, please try again later!");
} }
if(!d["success"].IsNull() && d["success"].GetBool()){ if(!d["success"].is_null() && d["success"].get<bool>()){
LoginAuth = true; LoginAuth = true;
if(!d["private_key"].IsNull()){ if(!d["private_key"].is_null()){
UpdateKey(d["private_key"].GetString()); UpdateKey(d["private_key"].get<std::string>());
} }
if(!d["public_key"].IsNull()){ if(!d["public_key"].is_null()){
PublicKey = d["public_key"].GetString(); PublicKey = d["public_key"].get<std::string>();
} }
LOG(INFO) << "Authentication successful!"; LOG(INFO) << "Authentication successful!";
}else LOG(WARNING) << "Authentication failed!"; }else LOG(WARNING) << "Authentication failed!";
if(!d["message"].IsNull()) { if(!d["message"].is_null()) {
d.RemoveMember("private_key"); d.erase("private_key");
d.RemoveMember("public_key"); d.erase("public_key");
Json::StringBuffer buffer; return d;
Json::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
return buffer.GetString();
} }
return GetFail("Invalid message parsing!"); return GetFail("Invalid message parsing!");
} }
@@ -88,28 +83,27 @@ void Launcher::CheckKey() {
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}"); Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}");
Json::Document d; Json d = Json::parse(Buffer, nullptr, false);
d.Parse(Buffer.c_str()); if (Buffer == "-1" || Buffer.at(0) != '{' || d.is_discarded()) {
if (Buffer == "-1" || Buffer.at(0) != '{' || d.HasParseError()) {
LOG(DEBUG) << Buffer; LOG(DEBUG) << Buffer;
LOG(FATAL) << "Invalid answer from authentication servers, please try again later!"; LOG(FATAL) << "Invalid answer from authentication servers, please try again later!";
throw ShutdownException("Fatal Error"); throw ShutdownException("Fatal Error");
} }
if(d["success"].GetBool()){ if(d["success"].get<bool>()){
LoginAuth = true; LoginAuth = true;
UpdateKey(d["private_key"].GetString()); UpdateKey(d["private_key"].get<std::string>());
PublicKey = d["public_key"].GetString(); PublicKey = d["public_key"].get<std::string>();
UserRole = d["role"].GetString(); UserRole = d["role"].get<std::string>();
//info(Role); LOG(INFO) << "Auto-Authentication was successful";
}else{ }else{
LOG(WARNING) << "Auto-Authentication unsuccessful please re-login!"; LOG(WARNING) << "Auto-Authentication unsuccessful please re-login!";
UpdateKey(nullptr); UpdateKey("");
} }
}else{ }else{
LOG(WARNING) << "Could not open saved key!"; LOG(WARNING) << "Could not open saved key!";
UpdateKey(nullptr); UpdateKey("");
} }
}else UpdateKey(nullptr); }else UpdateKey("");
} }

View File

@@ -107,17 +107,13 @@ void Launcher::EnableMP() {
std::string Data(Size, 0); std::string Data(Size, 0);
db.read(&Data[0], std::streamsize(Size)); db.read(&Data[0], std::streamsize(Size));
db.close(); db.close();
Json::Document d; Json d = Json::parse(Data, nullptr, false);
d.Parse(Data.c_str()); if(Data.at(0) != '{' || d.is_discarded())return;
if(Data.at(0) != '{' || d.HasParseError())return; if(!d["mods"].is_null() && !d["mods"]["multiplayerbeammp"].is_null()){
if(!d["mods"].IsNull() && !d["mods"]["multiplayerbeammp"].IsNull()){
d["mods"]["multiplayerbeammp"]["active"] = true; d["mods"]["multiplayerbeammp"]["active"] = true;
Json::StringBuffer buffer;
Json::Writer<Json::StringBuffer> writer(buffer);
d.Accept(writer);
std::ofstream ofs(File); std::ofstream ofs(File);
if(ofs.is_open()){ if(ofs.is_open()){
ofs << buffer.GetString(); ofs << std::setw(4) << d;
ofs.close(); ofs.close();
} else { } else {
LOG(ERROR) << "Failed to write " << File; LOG(ERROR) << "Failed to write " << File;