From 3549c67868474f568b45e17b85dafacf6cfc9dde Mon Sep 17 00:00:00 2001 From: Pizzabelly Date: Mon, 16 Jul 2018 17:49:51 -0400 Subject: [PATCH] polish some things + clang-format --- .clang-format | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 14 +++--- config.cpp | 88 ++++++++++++++++++++++---------------- config.hpp | 23 +++++----- config.ini | 5 ++- discord.cpp | 77 +++++++++++++++++---------------- discord.hpp | 4 +- main.cpp | 29 ++++++------- meson.build | 8 +++- 9 files changed, 246 insertions(+), 117 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..938b825 --- /dev/null +++ b/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +RawStringFormats: + - Delimiter: pb + Language: TextProto + BasedOnStyle: google +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never +... + diff --git a/README.md b/README.md index 80cd126..89d7d52 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@ # EasyRP (Custom Discord Rich Presence) -EasyRP is a small program to use the DiscordRPC library without any knowledge of programming. -Instead just use an easy conifg file +EasyRP is a small program to use the discord-rpc, to make a custom rich presence/game on discord. +using just an easy config file. **Usage** - First you need to register a Rich Presence application with discord - Go here https://discordapp.com/developers/applications/me - Make a new application **The name of the app will be the main name for the rich presence** - Enable rich presence for your app and add some assets -- Download the latest release of EasyRP from here https://github.com/Pizzabelly/EasyRP/releases +- Download the latest release of EasyRP from here https://github.com/Pizzabelly/EasyRP/releases - Edit the config file with the information from your newly registered app -- Run [EasyRP.exe](https://github.com/Pizzabelly/EasyRP/releases) (it should open a cmd window) - - It should report errors from your config file (if there are any) -- Go into discord settings and add the [EasyRP.exe](https://github.com/Pizzabelly/EasyRP/releases) as a game -- Discord should automatically change the path of [EasyRP.exe](https://github.com/Pizzabelly/EasyRP/releases) to your Discord Rich Presence +- Run [easyrp application binary](https://github.com/Pizzabelly/EasyRP/releases) (it should open a cmd window) + - It *should* report errors from your config file (if there are any) +- Discord should show the game on your profile + - if not, add the exe as a game on discord and the file path should change to your presence - You can edit the config any time while the program is running to change the presence diff --git a/config.cpp b/config.cpp index 44c9aeb..5c67830 100644 --- a/config.cpp +++ b/config.cpp @@ -1,65 +1,81 @@ -#include -#include -#include -#include #include "config.hpp" +#include +#include +#include +#include #define CONFIG_PATH "config.ini" -std::string setVar(std::string val, config_t* c) -{ - c->changed = true; - return val; +std::string setVar(std::string val, config_t *c) { + c->changed = true; + return val; } -// check and set the global presence config -void config_t::update() -{ +// check and set the global presence config +void config_t::update() { // open config file std::ifstream config_file(CONFIG_PATH); // "parse" config file // this is super specific and is NOT proper ini parsing // but it works, saves memory and avoids massive dependencies - for (std::string line; std::getline(config_file, line);) - { + for (std::string line; std::getline(config_file, line);) { // if line is ini comment (;), whitespace, or '[', skip it char first = line.front(); - if (first == ';' || first == ' ' || first == '[') continue; + if (first == ';' || first == ' ' || first == '[') + continue; std::istringstream line_stream; line_stream.str(line); std::string key; - if (std::getline(line_stream, key, '=')) - { - key.erase(std::remove_if(key.begin(), key.end(), ::isspace), key.end()); + if (std::getline(line_stream, key, '=')) { + key.erase(std::remove_if(key.begin(), key.end(), ::isspace), + key.end()); std::string value; - if (std::getline(line_stream, value)) - { - if (isspace(value.front())) value.erase(0, 1); - if (key == "ClientID" && value.compare(this->clientId) != 0) this->clientId = setVar(value, this); - else if (key == "State" && value.compare(this->state) != 0) this->state = setVar(value, this); - else if (key == "Details" && value.compare(this->details) != 0) this->details = setVar(value, this); - else if (key == "LargeImage" && value.compare(this->largeImg.key) !=0) this->largeImg.key = setVar(value, this); - else if (key == "SmallImage" && value.compare(this->smallImg.key) != 0) this->smallImg.key = setVar(value, this); - else if (key == "LargeImageTooltip" && value.compare(this->largeImg.text) != 0) this->largeImg.text = setVar(value, this); - else if (key == "SmallImageTooltip" && value.compare(this->smallImg.text) != 0) this->smallImg.text = setVar(value, this); - else if (key == "StartTimestamp" && value.compare(std::to_string(this->startTime)) != 0) this->startTime = std::strtoll(setVar(value, this).c_str(), NULL, 10); - else if (key == "EndTimestamp" && value.compare(std::to_string(this->endTime)) != 0) this->endTime = std::strtoll(setVar(value, this).c_str(), NULL, 10); + if (std::getline(line_stream, value)) { + if (isspace(value.front())) + value.erase(0, 1); + if (key == "ClientID" && value.compare(this->client_id) != 0) + this->client_id = setVar(value, this); + else if (key == "State" && value.compare(this->state) != 0) + this->state = setVar(value, this); + else if (key == "Details" && value.compare(this->details) != 0) + this->details = setVar(value, this); + else if (key == "LargeImage" && + value.compare(this->large_img.key) != 0) + this->large_img.key = setVar(value, this); + else if (key == "SmallImage" && + value.compare(this->small_img.key) != 0) + this->small_img.key = setVar(value, this); + else if (key == "LargeImageTooltip" && + value.compare(this->large_img.text) != 0) + this->large_img.text = setVar(value, this); + else if (key == "SmallImageTooltip" && + value.compare(this->small_img.text) != 0) + this->small_img.text = setVar(value, this); + else if (key == "StartTimestamp" && + value.compare(std::to_string(this->start_time)) != 0) + this->start_time = + std::strtoll(setVar(value, this).c_str(), NULL, 10); + else if (key == "EndTimestamp" && + value.compare(std::to_string(this->end_time)) != 0) + this->end_time = + std::strtoll(setVar(value, this).c_str(), NULL, 10); } } } } // print values for the current settings from the config file -void config_t::print() -{ - printf("\nCurrent Presence (%s) :", this->clientId.c_str()); +void config_t::print() { + printf("\nCurrent Presence (%s) :", this->client_id.c_str()); printf("\nState: %s", this->state.c_str()); printf("\nDetails: %s", this->details.c_str()); - printf("\nLarge Image: '%s' with toolip, '%s'", this->largeImg.key.c_str(), this->largeImg.text.c_str()); - printf("\nSmall Image: '%s' with toolip, '%s'", this->smallImg.key.c_str(), this->smallImg.text.c_str()); - printf("\nStart Time: %lld", this->startTime); - printf("\nEnd Time: %lld\n", this->endTime); + printf("\nLarge Image: '%s' with toolip, '%s'", this->large_img.key.c_str(), + this->large_img.text.c_str()); + printf("\nSmall Image: '%s' with toolip, '%s'", this->small_img.key.c_str(), + this->small_img.text.c_str()); + printf("\nStart Time: %lld", this->start_time); + printf("\nEnd Time: %lld\n", this->end_time); } diff --git a/config.hpp b/config.hpp index a6f81fd..fdeb245 100644 --- a/config.hpp +++ b/config.hpp @@ -6,28 +6,27 @@ struct pimage_t { }; // struct type to hold info about the games configuration -struct config_t -{ - // id for the discord developer app - std::string clientId; +struct config_t { + // id for the discord developer app + std::string client_id; // text to show for now playing std::string details; std::string state; // images to show for now playing - pimage_t smallImg; - pimage_t largeImg; + pimage_t small_img; + pimage_t large_img; // timestamps for game; note: these usually have to be within about 24 hours :( - long long startTime = 0; - long long endTime = 0; + long long start_time = 0; + long long end_time = 0; - // true if presence needs to be updated - bool changed = true; + // true if presence needs to be updated + bool changed = true; - void update(); - void print(); + void update(); + void print(); }; extern struct config_t config; diff --git a/config.ini b/config.ini index ba10926..76afbc4 100644 --- a/config.ini +++ b/config.ini @@ -6,14 +6,15 @@ ;# | |___| (_| \__ \ |_| | | \ \| | # ;# |______\__,_|___/\__, |_| \_\_| # ;# __/ | # -;# |___/ # +;# |___/ https://github.com/Pizzabelly/EasyRP # # ;# # ;# # ;# Optional Settings: SmallImage, LargeImageTooltip, SmallImageTooltip, # ;# StartTimestamp, EndTimestamp # ;# *ALL OTHERS ARE REQUIRED* # ;# # -;# https://github.com/Pizzabelly/EasyRP # +;# timestamps are in unix time -> https://www.epochconverter.com/ # +;# # ;######################################################################## [Identifiers] diff --git a/discord.cpp b/discord.cpp index 9eb4db2..cb1098a 100644 --- a/discord.cpp +++ b/discord.cpp @@ -1,87 +1,87 @@ +#include "config.hpp" +#include #include #include -#include #include +#include #include -#include -#include "config.hpp" #define DISCORD_DISABLE_IO_THREAD #include "discord_rpc.h" // shutdown discord-rpc -void Shutdown(int sig) -{ +void Shutdown(int sig) { printf("\nshutting down...\n"); Discord_Shutdown(); exit(sig); } // handle discord ready event -static void handleDiscordReady(const DiscordUser* u) -{ +static void handleDiscordReady(const DiscordUser *u) { printf("\nDisplaying Presence for %s#%s\n", u->username, u->discriminator); } // handle discord disconnected event -static void handleDiscordDisconnected(int errcode, const char* message) -{ +static void handleDiscordDisconnected(int errcode, const char *message) { printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); } // handle discord error event -static void handleDiscordError(int errcode, const char* message) -{ +static void handleDiscordError(int errcode, const char *message) { printf("\nDiscord: error (%d: %s)\n", errcode, message); Shutdown(1); } // update discord rich presence -void updatePresence(config_t* c) -{ +void updatePresence(config_t *c) { // set required variables DiscordRichPresence discordPresence; memset(&discordPresence, 0, sizeof(discordPresence)); - // make sure required parameters are set, if not dont update untill they are corrected + // make sure required parameters are set, if not dont update untill they are + // corrected // TODO: find the actual character limit for state and details - if (c->state.length() < 1 || c->state.length() > 100) - { - printf("\nState parameter is too long or not set\nPress any key to exit...\n"); + if (c->state.length() < 1 || c->state.length() > 100) { + printf("\nState parameter is too long or not set\nPress any key to " + "exit...\n"); return; } - if (c->details.length() < 1 || c->details.length() > 100) - { - printf("\nDetails parameter is too long or not set\nPress any key to exit...\n"); + if (c->details.length() < 1 || c->details.length() > 100) { + printf("\nDetails parameter is too long or not set\nPress any key to " + "exit...\n"); return; } - if (c->largeImg.key.length() < 1 || c->largeImg.key.length() > 100) - { + if (c->large_img.key.length() < 1 || c->large_img.key.length() > 100) { printf("\nLargeImage parameter not set\nPress any key to exit...\n"); return; } discordPresence.state = c->state.c_str(); - discordPresence.largeImageKey = c->largeImg.key.c_str(); + discordPresence.largeImageKey = c->large_img.key.c_str(); char buffer[256]; sprintf(buffer, "%s", c->details.c_str()); discordPresence.details = buffer; - if (c->startTime >= 0) discordPresence.startTimestamp = (int64_t)c->startTime; - if (c->endTime >= 0) discordPresence.endTimestamp = (int64_t)c->endTime; + if (c->start_time >= 0) + discordPresence.startTimestamp = (int64_t)c->start_time; + if (c->end_time >= 0) + discordPresence.endTimestamp = (int64_t)c->end_time; - // make sure not to set the optional variables if they are not defined in the config - if (c->smallImg.key.length() >= 1) discordPresence.smallImageKey = c->smallImg.key.c_str(); - if (c->smallImg.text.length() >= 1) discordPresence.smallImageText = c->smallImg.text.c_str(); - if (c->largeImg.text.length() >= 1) discordPresence.largeImageText = c->largeImg.text.c_str(); + // make sure not to set the optional variables if they are not defined in + // the config + if (c->small_img.key.length() >= 1) + discordPresence.smallImageKey = c->small_img.key.c_str(); + if (c->small_img.text.length() >= 1) + discordPresence.smallImageText = c->small_img.text.c_str(); + if (c->large_img.text.length() >= 1) + discordPresence.largeImageText = c->large_img.text.c_str(); // actaully update the presence Discord_UpdatePresence(&discordPresence); } -void refreshDiscord() -{ - // manually handle this +void refreshDiscord() { + // manually handle this Discord_UpdateConnection(); // handle callbacks @@ -89,18 +89,17 @@ void refreshDiscord() } // initialize discord rich presence -void initDiscord(std::string clientId) -{ +void initDiscord(std::string client_id) { DiscordEventHandlers handlers; memset(&handlers, 0, sizeof(handlers)); handlers.ready = handleDiscordReady; handlers.errored = handleDiscordError; handlers.disconnected = handleDiscordDisconnected; - if (clientId.length() < 1 || clientId.compare("123456789012345678") == 0) - { - printf("ClientID not correct (or not set).\n Unless by god you somehow got 123456789012345678 as your clientid please change this to the one you registered on the website"); + if (client_id.length() < 1 || client_id.compare("123456789012345678") == 0) { + printf("ClientID not correct (or not set).\n Unless by god you somehow " + "got 123456789012345678 as your clientid please change this to " + "the one you registered on the website"); Shutdown(1); } - Discord_Initialize(clientId.c_str(), &handlers, 1, NULL); + Discord_Initialize(client_id.c_str(), &handlers, 1, NULL); } - diff --git a/discord.hpp b/discord.hpp index a1c62b6..1d0367d 100644 --- a/discord.hpp +++ b/discord.hpp @@ -1,6 +1,6 @@ -void updatePresence(config_t* c); +void updatePresence(config_t *c); -void initDiscord(std::string clientID); +void initDiscord(std::string client_id); void refreshDiscord(); diff --git a/main.cpp b/main.cpp index 6900794..9d52f17 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,13 @@ -#include -#include -#include -#include #include "config.hpp" #include "discord.hpp" +#include +#include +#include +#include #define CONFIG_PATH "config.ini" -// loop to keep the program running as well as checking the config file for updates -int main(void) -{ +int main(void) { // define when to shutdown signal(SIGINT, Shutdown); signal(SIGTERM, Shutdown); @@ -17,28 +15,25 @@ int main(void) signal(SIGBREAK, Shutdown); #endif - // main config instance - config_t config; + // main config instance + config_t config; config.update(); // start discord-rpc - initDiscord(config.clientId); - + initDiscord(config.client_id); + // loop to keep program running also to check for updated config - while(true) - { + while (true) { config.update(); - if (config.changed) - { + if (config.changed) { // print and set variables for the presence config.print(); updatePresence(&config); config.changed = false; } - refreshDiscord(); + refreshDiscord(); std::this_thread::sleep_for(std::chrono::seconds(1)); } return 0; } - diff --git a/meson.build b/meson.build index 211b2b1..659f095 100644 --- a/meson.build +++ b/meson.build @@ -1,18 +1,22 @@ project('EasyRP', 'cpp', - version : '2.1', + version : '3.0', default_options : ['warning_level=3']) compiler = meson.get_compiler('cpp') if host_machine.system() == 'Windows' + windows = import('windows') + win_resources = windows.compile_resources('EasyRP.rc') discordrpc = compiler.find_library('discord-rpc', dirs: meson.source_root()) + src = ['main.cpp', 'config.cpp', 'discord.cpp', win_resources] else discordrpc = compiler.find_library('libdiscord-rpc', dirs: meson.source_root()) + src = ['main.cpp', 'config.cpp', 'discord.cpp'] endif rpc_inc = include_directories('discord-rpc/include') -exe = executable('easyrp', 'main.cpp', 'config.cpp', 'discord.cpp', +exe = executable('easyrp', src, include_directories : rpc_inc, dependencies : [discordrpc], install : false)