diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 46021cc..0e9cadd 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 openssl' + vcpkgArguments: 'zlib discord-rpc openssl' 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 470c800..68fa249 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 openssl' + vcpkgArguments: 'zlib discord-rpc openssl' vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg' vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f' vcpkgTriplet: 'x64-windows-static' diff --git a/.gitmodules b/.gitmodules index 5eda04d..0c68433 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,6 +13,3 @@ [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 b3b90f9..b6c6881 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,13 +45,14 @@ add_executable(${PROJECT_NAME} if (WIN32) - target_link_libraries(${PROJECT_NAME} PRIVATE - ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp) + 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) else(WIN32) #MINGW add_definitions("-D_WIN32_WINNT=0x0600") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") - target_link_libraries(${PROJECT_NAME} ssl crypto ws2_32 ssp crypt32 z Dbghelp) + target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp) endif(WIN32) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) target_include_directories(${PROJECT_NAME} PRIVATE "include") target_include_directories(${PROJECT_NAME} PRIVATE "include/rapidjson/include") +target_include_directories(${PROJECT_NAME} PRIVATE "include/discord-rpc/include") diff --git a/include/Launcher.h b/include/Launcher.h index 56540fa..4d122c2 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -25,9 +25,6 @@ public: //constructors public: //available functions static void StaticAbort(Launcher* Instance = nullptr); std::string Login(const std::string& fields); - static bool Terminated() noexcept; - static void setExit(bool exit); - static bool getExit(); void RunDiscordRPC(); void QueryRegistry(); void WaitForGame(); @@ -35,12 +32,17 @@ public: //available functions void LaunchGame(); void CheckKey(); void SetupMOD(); -public: //Getters +public: //Getters and Setters + void setDiscordMessage(const std::string& message); + static void setExit(bool exit) noexcept; const std::string& getFullVersion(); + static bool Terminated() noexcept; const std::string& getUserRole(); const std::string& getVersion(); + static bool getExit() noexcept; private: //functions std::string GetLocalAppdata(); + void UpdatePresence(); void AdminRelaunch(); void RichPresence(); void WindowsInit(); diff --git a/include/discord-rpc b/include/discord-rpc deleted file mode 160000 index 7c41a8e..0000000 --- a/include/discord-rpc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7c41a8ec1969b9b4b0c2360c6b632ebd16a92b69 diff --git a/src/Discord.cpp b/src/Discord.cpp index f3995a7..d4ef1f8 100644 --- a/src/Discord.cpp +++ b/src/Discord.cpp @@ -3,23 +3,50 @@ /// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. /// +#include #include "Launcher.h" #include "Logger.h" +#include + +void handleReady(const DiscordUser* u) {} +void handleDisconnected(int errcode, const char* message) {} +void handleError(int errcode, const char* message) { + LOG(ERROR) << "Discord error: " << message; +} + +void Launcher::UpdatePresence() { + auto currentTime = std::time(nullptr); + DiscordRichPresence discordPresence; + memset(&discordPresence, 0, sizeof(discordPresence)); + discordPresence.state = DiscordMessage.c_str(); + discordPresence.largeImageKey = "mainlogo"; + discordPresence.startTimestamp = currentTime - (currentTime - DiscordTime); + discordPresence.endTimestamp = 0; + DiscordTime = currentTime; + Discord_UpdatePresence(&discordPresence); +} + +void Launcher::setDiscordMessage(const std::string& message) { + DiscordMessage = message; + UpdatePresence(); +} void Launcher::RichPresence() { - /*Discord_Initialize("629743237988352010", nullptr, 1, nullptr); + DiscordEventHandlers handlers; + memset(&handlers, 0, sizeof(handlers)); + handlers.ready = handleReady; + handlers.errored = handleError; + handlers.disconnected = handleDisconnected; + Discord_Initialize("629743237988352010", &handlers, 1, nullptr); + UpdatePresence(); while(!Shutdown.load()) { - DiscordRichPresence discordPresence; - memset(&discordPresence, 0, sizeof(discordPresence)); - discordPresence.state = DiscordMessage.c_str(); - discordPresence.startTimestamp = DiscordTime; - discordPresence.largeImageKey = "mainlogo"; - Discord_UpdatePresence(&discordPresence); + Discord_RunCallbacks(); std::this_thread::sleep_for(std::chrono::seconds(1)); } - Discord_ClearPresence();*/ + Discord_ClearPresence(); + Discord_Shutdown(); } void Launcher::RunDiscordRPC() { - //DiscordRPC = std::thread(&Launcher::RichPresence, this); -} \ No newline at end of file + DiscordRPC = std::thread(&Launcher::RichPresence, this); +} diff --git a/src/Launcher.cpp b/src/Launcher.cpp index 01c6710..c769167 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -20,6 +20,7 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) { std::atomic Launcher::Shutdown{false}, Launcher::Exit{false}; Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") { Launcher::StaticAbort(this); + DiscordTime = std::time(nullptr); Log::Init(); WindowsInit(); SetUnhandledExceptionFilter(CrashHandler); @@ -111,6 +112,7 @@ void Launcher::WaitForGame() { throw ShutdownException("Fatal Error"); } LOG(INFO) << "Game found! PID " << GamePID; + setDiscordMessage("In menus"); //TODO: Inject then start IPC while(!Shutdown.load() && BeamNG::GetProcessID() != 0) { std::this_thread::sleep_for(std::chrono::seconds(2)); @@ -193,10 +195,10 @@ bool Launcher::Terminated() noexcept { return Shutdown.load(); } -bool Launcher::getExit() { +bool Launcher::getExit() noexcept { return Exit.load(); } -void Launcher::setExit(bool exit) { +void Launcher::setExit(bool exit) noexcept { Exit.store(exit); }