diff --git a/.gitignore b/.gitignore index 069d1a8..342d115 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /cmake-build-debug/ /cmake-build-release/ /.idea/ +!/cmake-build-release/discord-rpc.lib +!/cmake-build-debug/discord-rpc.lib diff --git a/CMakeLists.txt b/CMakeLists.txt index e3c8a6d..99869e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,11 @@ cmake_minimum_required(VERSION 3.15) +include_directories(${PROJECT_SOURCE_DIR}/include) + project(BeamNG-MP-Launcher) set(CMAKE_CXX_STANDARD 14) -add_executable(BeamNG-MP-Launcher main.cpp enet.h proxy.cpp Security.cpp http.cpp) +add_executable(BeamNG-MP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp) + +target_link_libraries(BeamNG-MP-Launcher discord-rpc) \ No newline at end of file diff --git a/Discord.cpp b/Discord.cpp new file mode 100644 index 0000000..7b677f8 --- /dev/null +++ b/Discord.cpp @@ -0,0 +1,150 @@ +/// +/// Created by Anonymous275 on 3/25/2020 +/// + +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include "include/discord_rpc.h" +#include +#include +#include + +static const char* APPLICATION_ID = "345229890980937739"; +static int FrustrationLevel = 0; +static int64_t StartTime; +static int SendPresence = 1; +static std::vector DiscordInfo; + +static void updateDiscordPresence() +{ + if (SendPresence) { + char buffer[256]; + DiscordRichPresence discordPresence; + memset(&discordPresence, 0, sizeof(discordPresence)); + discordPresence.state = "West of House"; + sprintf(buffer, "Frustration level: %d", FrustrationLevel); + discordPresence.details = buffer; + discordPresence.startTimestamp = StartTime; + discordPresence.endTimestamp = time(0) + 5 * 60; + discordPresence.largeImageKey = "canary-large"; + discordPresence.smallImageKey = "ptb-small"; + discordPresence.partyId = "party1234"; + discordPresence.partySize = 1; + discordPresence.partyMax = 6; + discordPresence.matchSecret = "xyzzy"; + discordPresence.joinSecret = "join"; + discordPresence.spectateSecret = "look"; + discordPresence.instance = 0; + Discord_UpdatePresence(&discordPresence); + } + else { + Discord_ClearPresence(); + } +} + +static void handleDiscordReady(const DiscordUser* connectedUser) +{ + /*printf("\nDiscord: connected to user %s#%s - %s\n", + connectedUser->username, + connectedUser->discriminator, + connectedUser->userId);*/ + DiscordInfo.emplace_back(connectedUser->username); + DiscordInfo.emplace_back(connectedUser->discriminator); + DiscordInfo.emplace_back(connectedUser->userId); +} + +static void handleDiscordDisconnected(int errcode, const char* message) +{ + printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); +} + +static void handleDiscordError(int errcode, const char* message) +{ + printf("\nDiscord: error (%d: %s)\n", errcode, message); +} + +static void handleDiscordJoin(const char* secret) +{ + printf("\nDiscord: join (%s)\n", secret); +} + +static void handleDiscordSpectate(const char* secret) +{ + printf("\nDiscord: spectate (%s)\n", secret); +} + +static void handleDiscordJoinRequest(const DiscordUser* request) +{ + /* int response = -1; + char yn[4]; + printf("\nDiscord: join request from %s#%s - %s\n", + request->username, + request->discriminator, + request->userId); + do { + printf("Accept? (y/n)"); + if (!prompt(yn, sizeof(yn))) { + break; + } + + if (!yn[0]) { + continue; + } + + if (yn[0] == 'y') { + response = DISCORD_REPLY_YES; + break; + } + + if (yn[0] == 'n') { + response = DISCORD_REPLY_NO; + break; + } + } while (1); + if (response != -1) { + Discord_Respond(request->userId, response); + }*/ +} + +static void discordInit() +{ + DiscordEventHandlers handlers; + memset(&handlers, 0, sizeof(handlers)); + handlers.ready = handleDiscordReady; + handlers.disconnected = handleDiscordDisconnected; + handlers.errored = handleDiscordError; + handlers.joinGame = handleDiscordJoin; + handlers.spectateGame = handleDiscordSpectate; + handlers.joinRequest = handleDiscordJoinRequest; + Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL); +} + +static std::vector Loop() +{ + char line[512]; + char* space; + + StartTime = time(0); + + while (DiscordInfo.size() < 3) { + //updateDiscordPresence(); + +#ifdef DISCORD_DISABLE_IO_THREAD + Discord_UpdateConnection(); +#endif + Discord_RunCallbacks(); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + return DiscordInfo; +} + +std::vector Discord_Main() +{ + discordInit(); + return Loop(); + //Discord_Shutdown(); +} + diff --git a/http.cpp b/http.cpp index fda2a44..46ec187 100644 --- a/http.cpp +++ b/http.cpp @@ -6,7 +6,6 @@ #include #include #include -#include std::string HTTP_REQUEST(){ diff --git a/include/discord_register.h b/include/discord_register.h new file mode 100644 index 0000000..16fb42f --- /dev/null +++ b/include/discord_register.h @@ -0,0 +1,26 @@ +#pragma once + +#if defined(DISCORD_DYNAMIC_LIB) +#if defined(_WIN32) +#if defined(DISCORD_BUILDING_SDK) +#define DISCORD_EXPORT __declspec(dllexport) +#else +#define DISCORD_EXPORT __declspec(dllimport) +#endif +#else +#define DISCORD_EXPORT __attribute__((visibility("default"))) +#endif +#else +#define DISCORD_EXPORT +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command); +DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId, const char* steamId); + +#ifdef __cplusplus +} +#endif diff --git a/include/discord_rpc.h b/include/discord_rpc.h new file mode 100644 index 0000000..3e1441e --- /dev/null +++ b/include/discord_rpc.h @@ -0,0 +1,87 @@ +#pragma once +#include + +// clang-format off + +#if defined(DISCORD_DYNAMIC_LIB) +# if defined(_WIN32) +# if defined(DISCORD_BUILDING_SDK) +# define DISCORD_EXPORT __declspec(dllexport) +# else +# define DISCORD_EXPORT __declspec(dllimport) +# endif +# else +# define DISCORD_EXPORT __attribute__((visibility("default"))) +# endif +#else +# define DISCORD_EXPORT +#endif + +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct DiscordRichPresence { + const char* state; /* max 128 bytes */ + const char* details; /* max 128 bytes */ + int64_t startTimestamp; + int64_t endTimestamp; + const char* largeImageKey; /* max 32 bytes */ + const char* largeImageText; /* max 128 bytes */ + const char* smallImageKey; /* max 32 bytes */ + const char* smallImageText; /* max 128 bytes */ + const char* partyId; /* max 128 bytes */ + int partySize; + int partyMax; + const char* matchSecret; /* max 128 bytes */ + const char* joinSecret; /* max 128 bytes */ + const char* spectateSecret; /* max 128 bytes */ + int8_t instance; +} DiscordRichPresence; + +typedef struct DiscordUser { + const char* userId; + const char* username; + const char* discriminator; + const char* avatar; +} DiscordUser; + +typedef struct DiscordEventHandlers { + void (*ready)(const DiscordUser* request); + void (*disconnected)(int errorCode, const char* message); + void (*errored)(int errorCode, const char* message); + void (*joinGame)(const char* joinSecret); + void (*spectateGame)(const char* spectateSecret); + void (*joinRequest)(const DiscordUser* request); +} DiscordEventHandlers; + +#define DISCORD_REPLY_NO 0 +#define DISCORD_REPLY_YES 1 +#define DISCORD_REPLY_IGNORE 2 + +DISCORD_EXPORT void Discord_Initialize(const char* applicationId, + DiscordEventHandlers* handlers, + int autoRegister, + const char* optionalSteamId); +DISCORD_EXPORT void Discord_Shutdown(void); + +/* checks for incoming messages, dispatches callbacks */ +DISCORD_EXPORT void Discord_RunCallbacks(void); + +/* If you disable the lib starting its own io thread, you'll need to call this from your own */ +#ifdef DISCORD_DISABLE_IO_THREAD +DISCORD_EXPORT void Discord_UpdateConnection(void); +#endif + +DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence); +DISCORD_EXPORT void Discord_ClearPresence(void); + +DISCORD_EXPORT void Discord_Respond(const char* userid, /* DISCORD_REPLY_ */ int reply); + +DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* handlers); + +#ifdef __cplusplus +} /* extern "C" */ +#endif diff --git a/enet.h b/include/enet.h similarity index 100% rename from enet.h rename to include/enet.h diff --git a/main.cpp b/main.cpp index abe381f..cd7bad7 100644 --- a/main.cpp +++ b/main.cpp @@ -14,18 +14,24 @@ void Exit(const std::string& Msg){ std::cin.ignore(); exit(-1); } + void ProxyStart(); std::string HTTP_REQUEST(); +std::vector Discord_Main(); int main() { //Security std::vector Data = Check(); std::cout << "You own BeamNG on this machine!" << std::endl; + //std::cout << Data.at(2) << "\\BeamNG.drive.exe"; + + std::cout << "Name : " << Discord_Main().at(0) << std::endl; + std::cout << "Discriminator : " << Discord_Main().at(1) << std::endl; + std::cout << "Unique ID : " << Discord_Main().at(2) << std::endl; + - //std::cout << Data.at(1) << "\\BeamNG.drive.exe"; //std::cout << "\nHTTP TEST :\n\n"; - //std::cout << HTTP_REQUEST(); /// Update, Mods ect... diff --git a/proxy.cpp b/proxy.cpp index 9c9ace4..d5371da 100644 --- a/proxy.cpp +++ b/proxy.cpp @@ -3,7 +3,7 @@ //// #define ENET_IMPLEMENTATION -#include "enet.h" +#include "include/enet.h" #include #include #include