From 6e45304aeae584904b39ea71645ced2c9856b475 Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Tue, 18 Jan 2022 13:58:49 +0200 Subject: [PATCH] added discord-rpc --- .github/workflows/cmake-windows.yml | 2 +- .gitmodules | 3 +++ CMakeLists.txt | 10 ++++----- include/Launcher.h | 17 +++++++++++---- include/discord-rpc | 1 + src/Config.cpp | 33 +++++++++++++++-------------- src/Discord.cpp | 27 +++++++++++++++++++++++ src/Launcher.cpp | 7 ++++++ src/Login.cpp | 2 +- src/main.cpp | 4 +++- 10 files changed, 78 insertions(+), 28 deletions(-) create mode 160000 include/discord-rpc create mode 100644 src/Discord.cpp diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index ca95075..46021cc 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: 'discord-rpc zlib rapidjson openssl' + vcpkgArguments: 'zlib openssl' vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg' vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f' vcpkgTriplet: 'x64-windows-static' diff --git a/.gitmodules b/.gitmodules index 0c68433..5eda04d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "include/easyloggingpp"] path = include/easyloggingpp url = https://github.com/amrayn/easyloggingpp.git +[submodule "include/discord-rpc"] + path = include/discord-rpc + url = https://github.com/discord/discord-rpc.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d46af3..6acb4de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ if (WIN32) endif(WIN32) set(wxUSE_STL ON) +set(BUILD_EXAMPLES OFF) set(wxBUILD_SHARED OFF) set(build_static_lib ON) set(wxBUILD_MSVC_MULTIPROC ON) @@ -23,6 +24,7 @@ set(wxBUILD_USE_STATIC_RUNTIME ON) add_subdirectory(include/wxWidgets) add_subdirectory(include/rapidjson) +add_subdirectory(include/discord-rpc) add_subdirectory(include/cpp-httplib) add_subdirectory(include/tomlplusplus) @@ -38,15 +40,13 @@ add_executable(${PROJECT_NAME} src/gui/gifs.cpp src/gui/gifs.h src/Network/Http.cpp include/Http.h src/Login.cpp src/Config.cpp + src/Discord.cpp ) if (WIN32) - target_link_libraries(${PROJECT_NAME} PRIVATE ${VcpkgRoot}/lib/discord-rpc.lib - ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base) -elseif (UNIX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s") - target_link_libraries(${PROJECT_NAME} wx::net wx::core wx::base) + target_link_libraries(${PROJECT_NAME} PRIVATE + ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base) else(WIN32) #MINGW add_definitions("-D_WIN32_WINNT=0x0600") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") diff --git a/include/Launcher.h b/include/Launcher.h index 8b6fbcb..0b3c9f0 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -5,27 +5,36 @@ #pragma once #include +#include class Launcher { public: //constructors Launcher(int argc, char* argv[]); + ~Launcher(); public: //available functions std::string Login(const std::string& fields); - void checkLocalKey(); + void runDiscordRPC(); void loadConfig(); void launchGame(); + void checkKey(); public: //Getters const std::string& getFullVersion(); const std::string& getWorkingDir(); const std::string& getUserRole(); const std::string& getVersion(); private: //functions + void richPresence(); void WindowsInit(); private: //variables - std::string DirPath; - std::string UserRole; - std::string PublicKey; + bool EnableUI = true; + bool Shutdown = false; + std::string DirPath{}; bool LoginAuth = false; + std::string UserRole{}; + std::string PublicKey{}; + std::thread DiscordRPC{}; + std::string DiscordMessage{}; std::string Version{"3.0"}; + std::string TargetBuild{"default"}; std::string FullVersion{Version + ".0"}; }; diff --git a/include/discord-rpc b/include/discord-rpc new file mode 160000 index 0000000..e4c0c56 --- /dev/null +++ b/include/discord-rpc @@ -0,0 +1 @@ +Subproject commit e4c0c569eca9b888010ad904d3633e782f5cd680 diff --git a/src/Config.cpp b/src/Config.cpp index 804786d..3d286f3 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -11,27 +11,28 @@ namespace fs = std::filesystem; + + void Launcher::loadConfig() { - if(fs::exists("Launcher.cfg")){ - std::ifstream cfg("Launcher.cfg"); - if(cfg.is_open()) { - auto Size = fs::file_size("Launcher.cfg"); - std::string Buffer(Size, 0); - cfg.read(&Buffer[0], std::streamsize(Size)); - cfg.close(); - toml::table config = toml::parse(cfg); - LOG(INFO) << "Parsing"; - if(config["Port"].is_value()) { - /*auto Port = config["Port"].as_integer()->get(); - LOG(INFO) << Port;*/ - LOG(INFO) << "Got port"; - } - }else LOG(FATAL) << "Failed to open Launcher.cfg!"; + if(fs::exists("Launcher.cfg")) { + toml::table config = toml::parse_file("Launcher.cfg"); + auto ui = config["UI"]; + auto build = config["Build"]; + if(ui.is_boolean()) { + EnableUI = ui.as_boolean()->get(); + } else LOG(ERROR) << "Failed to get 'UI' boolean from config"; + + //Default -1 / Release 1 / EA 2 / Dev 3 / Custom 3 + if(build.is_string()) { + TargetBuild = build.as_string()->get(); + for(char& c : TargetBuild)c = char(tolower(c)); + } else LOG(ERROR) << "Failed to get 'Build' string from config"; + } else { std::ofstream cfg("Launcher.cfg"); if(cfg.is_open()){ cfg << - R"(Port = 4444 + R"(UI = true Build = "Default" )"; cfg.close(); diff --git a/src/Discord.cpp b/src/Discord.cpp new file mode 100644 index 0000000..e55ea6f --- /dev/null +++ b/src/Discord.cpp @@ -0,0 +1,27 @@ +/// +/// Created by Anonymous275 on 1/18/22 +/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. +/// + +#include +#include "Launcher.h" + +void Launcher::richPresence() { + Discord_Initialize("629743237988352010", nullptr, 1,nullptr); + while(!Shutdown) { + DiscordRichPresence discordPresence; + memset(&discordPresence, 0, sizeof(discordPresence)); + if(DiscordMessage.empty()) DiscordMessage = "Playing with friends!"; + discordPresence.state = DiscordMessage.c_str(); + discordPresence.startTimestamp = 0; + discordPresence.largeImageKey = "mainlogo"; + Discord_UpdatePresence(&discordPresence); + Discord_RunCallbacks(); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + Discord_ClearPresence(); +} + +void Launcher::runDiscordRPC() { + DiscordRPC = std::thread(&Launcher::richPresence, this); +} \ No newline at end of file diff --git a/src/Launcher.cpp b/src/Launcher.cpp index 1797084..a713192 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -15,6 +15,12 @@ Launcher::Launcher(int argc, char* argv[]) : DirPath(argv[0]) { WindowsInit(); } +Launcher::~Launcher() { + if(DiscordRPC.joinable()) { + DiscordRPC.join(); + } +} + void Launcher::launchGame() { ShellExecuteA(nullptr, nullptr, "steam://rungameid/284160", nullptr, nullptr, SW_SHOWNORMAL); ShowWindow(GetConsoleWindow(), HIDE_WINDOW); @@ -43,3 +49,4 @@ const std::string& Launcher::getUserRole() { } + diff --git a/src/Login.cpp b/src/Login.cpp index 409b4c8..21234b8 100644 --- a/src/Login.cpp +++ b/src/Login.cpp @@ -77,7 +77,7 @@ std::string Launcher::Login(const std::string& fields) { return GetFail("Invalid message parsing!"); } -void Launcher::checkLocalKey() { +void Launcher::checkKey() { if(fs::exists("key") && fs::file_size("key") < 100){ std::ifstream Key("key"); if(Key.is_open()) { diff --git a/src/main.cpp b/src/main.cpp index ba7c5ed..b7fbd73 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,9 @@ #include "Logger.h" int main(int argc, char* argv[]) { Launcher launcher(argc, argv); - launcher.checkLocalKey(); launcher.loadConfig(); + launcher.checkKey(); + //UI call + return 0; }