Added Discord Hook

This commit is contained in:
Anonymous275 2020-03-25 22:00:06 +02:00
parent 4b3eef5f21
commit 7f29cc7a3c
9 changed files with 279 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/cmake-build-debug/
/cmake-build-release/
/.idea/
!/cmake-build-release/discord-rpc.lib
!/cmake-build-debug/discord-rpc.lib

View File

@ -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)

150
Discord.cpp Normal file
View File

@ -0,0 +1,150 @@
///
/// Created by Anonymous275 on 3/25/2020
///
#define _CRT_SECURE_NO_WARNINGS
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
#include "include/discord_rpc.h"
#include <chrono>
#include <thread>
#include <vector>
static const char* APPLICATION_ID = "345229890980937739";
static int FrustrationLevel = 0;
static int64_t StartTime;
static int SendPresence = 1;
static std::vector<std::string> 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<std::string> 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<std::string> Discord_Main()
{
discordInit();
return Loop();
//Discord_Shutdown();
}

View File

@ -6,7 +6,6 @@
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
std::string HTTP_REQUEST(){

View File

@ -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

87
include/discord_rpc.h Normal file
View File

@ -0,0 +1,87 @@
#pragma once
#include <stdint.h>
// 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

View File

@ -14,18 +14,24 @@ void Exit(const std::string& Msg){
std::cin.ignore();
exit(-1);
}
void ProxyStart();
std::string HTTP_REQUEST();
std::vector<std::string> Discord_Main();
int main()
{
//Security
std::vector<std::string> 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...

View File

@ -3,7 +3,7 @@
////
#define ENET_IMPLEMENTATION
#include "enet.h"
#include "include/enet.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <cstdio>