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
id: runvcpkg
with:
vcpkgArguments: 'zlib discord-rpc rapidjson openssl minhook'
vcpkgArguments: 'zlib discord-rpc nlohmann-json openssl minhook'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f'
vcpkgTriplet: 'x64-windows-static'

View File

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

View File

@@ -10,6 +10,7 @@ if (WIN32)
find_package(ZLIB REQUIRED)
find_package(OpenSSL 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
set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET})
include_directories(${VcpkgRoot}/include)
@@ -52,11 +53,11 @@ add_executable(${PROJECT_NAME}
if (WIN32)
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
add_definitions("-D_WIN32_WINNT=0x0600")
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)
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)
target_include_directories(${PROJECT_NAME} PRIVATE "include")

View File

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

View File

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