Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
properly handle timestamps + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pizzabelly committed Jul 18, 2018
1 parent 8b75fd6 commit 53295ec
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
ColumnLimit: 90
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ¯\_(ツ)_

66 changes: 34 additions & 32 deletions config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <sstream>

#define CONFIG_PATH "config.ini"

std::string setVar(std::string val, config_t *c) {
template <class T> T setVar(T val, config_t *c) {
c->changed = true;
return val;
}
Expand All @@ -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<std::string>(value, this);
} else if (key == "State" && value.compare(this->state) != 0) {
this->state = setVar<std::string>(value, this);
} else if (key == "Details" && value.compare(this->details) != 0) {
this->details = setVar<std::string>(value, this);
} else if (key == "LargeImage" && value.compare(this->large_img.key) != 0) {
this->large_img.key = setVar<std::string>(value, this);
} else if (key == "SmallImage" && value.compare(this->small_img.key) != 0) {
this->small_img.key = setVar<std::string>(value, this);
} else if (key == "LargeImageTooltip" &&
value.compare(this->large_img.text) != 0) {
this->large_img.text = setVar<std::string>(value, this);
} else if (key == "SmallImageTooltip" &&
value.compare(this->small_img.text) != 0) {
this->small_img.text = setVar<std::string>(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<long long>(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<long long>(num_value, this);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
;# | |___| (_| \__ \ |_| | | \ \| | #
;# |______\__,_|___/\__, |_| \_\_| #
;# __/ | #
;# |___/ https://github.com/Pizzabelly/EasyRP # #
;# |___/ https://github.com/Pizzabelly/EasyRP #
;# #
;# #
;# Optional Settings: SmallImage, LargeImageTooltip, SmallImageTooltip, #
Expand Down
2 changes: 1 addition & 1 deletion discord-rpc
11 changes: 9 additions & 2 deletions discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstring>
#include <ctime>
#include <iostream>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -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**
Expand Down

0 comments on commit 53295ec

Please sign in to comment.