From 53295ecfbc5d8a132054922aabb53c956c71a759 Mon Sep 17 00:00:00 2001 From: Pizzabelly Date: Tue, 17 Jul 2018 23:40:59 -0400 Subject: [PATCH] properly handle timestamps + small fixes --- .clang-format | 2 +- README.md | 8 +++++++ config.cpp | 66 ++++++++++++++++++++++++++------------------------- config.hpp | 3 ++- config.ini | 2 +- discord-rpc | 2 +- discord.cpp | 11 +++++++-- meson.build | 2 +- readme.txt | 2 +- 9 files changed, 58 insertions(+), 40 deletions(-) diff --git a/.clang-format b/.clang-format index 938b825..5005d8d 100644 --- a/.clang-format +++ b/.clang-format @@ -44,7 +44,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 90 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false diff --git a/README.md b/README.md index 3e6fcad..37bedd6 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,11 @@ using just an easy config file. - 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 (make sure to save the file) + +**Timestamps** +The Start and End timestamps are in epoch/unix time. +Your desired values can be found [here](https://www.epochconverter.com/). +For elapsed time set only the StartTimestamp. For remaining time set both. +Though discord seems to only care about hours/minutes/seconds. +As it doesnt go above 24hrs either way ¯\_(ツ)_/¯ + diff --git a/config.cpp b/config.cpp index 5c67830..56af419 100644 --- a/config.cpp +++ b/config.cpp @@ -2,11 +2,12 @@ #include #include #include +#include #include #define CONFIG_PATH "config.ini" -std::string setVar(std::string val, config_t *c) { +template T setVar(T val, config_t *c) { c->changed = true; return val; } @@ -30,38 +31,39 @@ void config_t::update() { std::string key; if (std::getline(line_stream, key, '=')) { - key.erase(std::remove_if(key.begin(), key.end(), ::isspace), - key.end()); + 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->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); + if (!std::getline(line_stream, value)) + value = ""; + if (isspace(value.front()) && isspace(value.back())) + 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); + } + + // special conditions for timestamps to avoid bad values + else if (key == "StartTimestamp") { + long long num_value = std::strtoll(value.c_str(), NULL, 10); + if (num_value != this->start_time) + this->start_time = setVar(num_value, this); + } else if (key == "EndTimestamp") { + long long num_value = std::strtoll(value.c_str(), NULL, 10); + if (num_value != this->end_time) + this->end_time = setVar(num_value, this); } } } diff --git a/config.hpp b/config.hpp index fdeb245..f2c2dea 100644 --- a/config.hpp +++ b/config.hpp @@ -18,7 +18,8 @@ struct config_t { pimage_t small_img; pimage_t large_img; - // timestamps for game; note: these usually have to be within about 24 hours :( + // timestamps for game; note: these usually have to be within about 24 hours + // :( long long start_time = 0; long long end_time = 0; diff --git a/config.ini b/config.ini index 379baf0..582b53a 100644 --- a/config.ini +++ b/config.ini @@ -6,7 +6,7 @@ ;# | |___| (_| \__ \ |_| | | \ \| | # ;# |______\__,_|___/\__, |_| \_\_| # ;# __/ | # -;# |___/ https://github.com/Pizzabelly/EasyRP # # +;# |___/ https://github.com/Pizzabelly/EasyRP # ;# # ;# # ;# Optional Settings: SmallImage, LargeImageTooltip, SmallImageTooltip, # diff --git a/discord-rpc b/discord-rpc index a3ad6af..7c41a8e 160000 --- a/discord-rpc +++ b/discord-rpc @@ -1 +1 @@ -Subproject commit a3ad6afee236237f931badd54a673433fa8c1bfe +Subproject commit 7c41a8ec1969b9b4b0c2360c6b632ebd16a92b69 diff --git a/discord.cpp b/discord.cpp index 1ceb6e2..10255f5 100644 --- a/discord.cpp +++ b/discord.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -60,9 +61,15 @@ void updatePresence(config_t *c) { sprintf(buffer, "%s", c->details.c_str()); discordPresence.details = buffer; - if (c->start_time >= 0) + if (c->start_time == LLONG_MAX || c->start_time == LLONG_MIN || + c->end_time == LLONG_MAX || c->end_time == LLONG_MIN) { + printf("wew!, one (or both) of your timestamps is WAY too big"); + return; + } + + if (c->start_time >= 0 && c->start_time != 0LL) discordPresence.startTimestamp = (int64_t)c->start_time; - if (c->end_time >= 0) + if (c->end_time >= 0 && c->end_time != 0LL) discordPresence.endTimestamp = (int64_t)c->end_time; // make sure not to set the optional variables if they are not defined in diff --git a/meson.build b/meson.build index 98d2f6c..d19c534 100644 --- a/meson.build +++ b/meson.build @@ -4,7 +4,7 @@ project('EasyRP', 'cpp', compiler = meson.get_compiler('cpp') -if host_machine.system() == 'Windows' +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()) diff --git a/readme.txt b/readme.txt index ec1d01a..79b0a4f 100644 --- a/readme.txt +++ b/readme.txt @@ -1,4 +1,4 @@ -EasyRP (Custom Discord Rich Presence) +EasyRP (Custom Discord Rich Presence) https://github.com/Pizzabelly/EasyRP 1. 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**