From 31cd89fd201764efd2d46f4a3c2a031dae5444e9 Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Thu, 3 Mar 2022 16:12:55 +0200 Subject: [PATCH] switched to nlohmann_json --- .github/workflows/cmake-windows.yml | 2 +- .github/workflows/release-build.yml | 2 +- CMakeLists.txt | 5 +-- include/Json.h | 7 ++-- src/Network/Login.cpp | 54 +++++++++++++---------------- src/Network/Update.cpp | 12 +++---- 6 files changed, 36 insertions(+), 46 deletions(-) diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 0863ec1..8e3a853 100644 --- a/.github/workflows/cmake-windows.yml +++ b/.github/workflows/cmake-windows.yml @@ -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' diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml index ada98ea..5a3fce9 100644 --- a/.github/workflows/release-build.yml +++ b/.github/workflows/release-build.yml @@ -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' diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c3f34e..9fc46b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/include/Json.h b/include/Json.h index 26763d3..b341884 100644 --- a/include/Json.h +++ b/include/Json.h @@ -4,7 +4,6 @@ /// #pragma once -#include -#include -#include -namespace Json = rapidjson; +#include +using Json = nlohmann::json; + diff --git a/src/Network/Login.cpp b/src/Network/Login.cpp index 964d1c9..d0163e2 100644 --- a/src/Network/Login.cpp +++ b/src/Network/Login.cpp @@ -5,12 +5,11 @@ #include "Launcher.h" #include "Logger.h" -#include #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()){ LoginAuth = true; - if(!d["private_key"].IsNull()){ - UpdateKey(d["private_key"].GetString()); + if(!d["private_key"].is_null()){ + UpdateKey(d["private_key"].get()); } - if(!d["public_key"].IsNull()){ - PublicKey = d["public_key"].GetString(); + if(!d["public_key"].is_null()){ + PublicKey = d["public_key"].get(); } 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 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()){ LoginAuth = true; - UpdateKey(d["private_key"].GetString()); - PublicKey = d["public_key"].GetString(); - UserRole = d["role"].GetString(); - //info(Role); + UpdateKey(d["private_key"].get()); + PublicKey = d["public_key"].get(); + UserRole = d["role"].get(); + 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(""); } diff --git a/src/Network/Update.cpp b/src/Network/Update.cpp index 79a4787..f9b29b7 100644 --- a/src/Network/Update.cpp +++ b/src/Network/Update.cpp @@ -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 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;