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

Commit

Permalink
WIP refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pizzabelly committed Jul 14, 2018
1 parent c079846 commit be30077
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 57 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
build/
# EasyRP specific files
build*/
libdiscord-rpc.a
discord-rpc.dll
discord-rpc.lib

# Compiled Object files
*.slo
Expand All @@ -17,10 +19,6 @@ discord-rpc.dll
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
Expand Down
66 changes: 38 additions & 28 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#include <sstream>
#include <iostream>
#include <regex>
#include <fstream>
#include <bits/stl_algo.h>
#include <algorithm>
#include "config.hpp"

// config_t instance for holding info for the presence
config_t config;
#define CONFIG_PATH "config.ini"

// check and set the global presence config
bool setPresenceVariables(std::string configPath)
bool config_t::update()
{
// open config file
std::ifstream configFile(configPath);
std::ifstream configFile(CONFIG_PATH);

// bool that is true if any parsed value is different
bool changed = false;
bool changed = true;

// parse config file
// "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(configFile, line);)
{
// if line is ini comment (;) skip it
if (line.front() == ';') continue;

// if line is ini comment (;), whitespace, or '[', skip it
char first = line.front();
if (first == ';' || first == ' ' || first == '[') continue;

std::istringstream line_stream;
line_stream.str(line);

Expand All @@ -35,29 +35,39 @@ bool setPresenceVariables(std::string configPath)
if (std::getline(line_stream, value))
{
if (isspace(value.front())) value.erase(0, 1);
if (key == "ClientID" && value.compare(config.clientId) != 0) { config.clientId = value; changed = true; }
else if (key == "State" && value.compare(config.state) != 0) { config.state = value; changed = true; }
else if (key == "Details" && value.compare(config.details) != 0) { config.details = value; changed = true; }
else if (key == "LargeImage" && value.compare(config.largeImg.key) !=0 ) { config.largeImg.key = value; changed = true; }
else if (key == "SmallImage" && value.compare(config.smallImg.key) != 0) { config.smallImg.key = value; changed = true; }
else if (key == "LargeImageTooltip" && value.compare(config.largeImg.text) != 0) { config.largeImg.text = value; changed = true; }
else if (key == "SmallImageTooltip" && value.compare(config.smallImg.text) != 0) { config.smallImg.text = value; changed = true; }
else if (key == "StartTimestamp" && value.compare(std::to_string(config.startTime)) != 0) { config.startTime = std::strtoll(value.c_str(), NULL, 10); changed = true; }
else if (key == "EndTimestamp" && value.compare(std::to_string(config.endTime)) != 0) { config.endTime = std::strtoll(value.c_str(), NULL, 10); changed = true; }
if (key == "ClientID" && value.compare(this->clientId) != 0)
this->clientId = value;
else if (key == "State" && value.compare(this->state) != 0)
this->state = value;
else if (key == "Details" && value.compare(this->details) != 0)
this->details = value;
else if (key == "LargeImage" && value.compare(this->largeImg.key) !=0)
this->largeImg.key = value;
else if (key == "SmallImage" && value.compare(this->smallImg.key) != 0)
this->smallImg.key = value;
else if (key == "LargeImageTooltip" && value.compare(this->largeImg.text) != 0)
this->largeImg.text = value;
else if (key == "SmallImageTooltip" && value.compare(this->smallImg.text) != 0)
this->smallImg.text = value;
else if (key == "StartTimestamp" && value.compare(std::to_string(this->startTime)) != 0)
this->startTime = std::strtoll(value.c_str(), NULL, 10);
else if (key == "EndTimestamp" && value.compare(std::to_string(this->endTime)) != 0)
this->endTime = std::strtoll(value.c_str(), NULL, 10);
else changed = false;
}
}
}
return changed;
}

// print values for the current settings from the config file
void printVariables(struct config_t* c)
void config_t::print()
{
printf("\nCurrent Presence (%s) :", c->clientId.c_str());
printf("\nState: %s", c->state.c_str());
printf("\nDetails: %s", c->details.c_str());
printf("\nLarge Image: '%s' with toolip, '%s'", c->largeImg.key.c_str(), c->largeImg.text.c_str());
printf("\nSmall Image: '%s' with toolip, '%s'", c->smallImg.key.c_str(), c->smallImg.text.c_str());
printf("\nStart Time: %lld", c->startTime);
printf("\nEnd Time: %lld\n", c->endTime);
printf("\nCurrent Presence (%s) :", this->clientId.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);
}
9 changes: 4 additions & 5 deletions config.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include <iostream>

bool setPresenceVariables(std::string configPath);

void printVariables(struct config_t* c);

struct pimage_t {
std::string key;
std::string text;
};

//struct type to hold info about the games configuration
// struct type to hold info about the games configuration
struct config_t
{
// id for the discord developer app
Expand All @@ -26,6 +22,9 @@ struct config_t
// timestamps for game; note: these usually have to be within about 24 hours :(
long long startTime;
long long endTime;

bool update();
void print();
};

extern struct config_t config;
22 changes: 13 additions & 9 deletions discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
#include "discord_rpc.h"
#include "config.hpp"

// shutdown discord-rpc
void Shutdown(int sig)
{
printf("\nshutting down...\n");
Discord_Shutdown();
exit(sig);
}

// handle discord ready event
static void handleDiscordReady(const DiscordUser* u)
{
printf("\nDiscord: ready\n");
printf("\nDisplaying Presence for %s#%s\n", u->username, u->discriminator);
}

// handle discord disconnected event
Expand All @@ -23,14 +31,7 @@ static void handleDiscordDisconnected(int errcode, const char* message)
static void handleDiscordError(int errcode, const char* message)
{
printf("\nDiscord: error (%d: %s)\n", errcode, message);
}

// shutdown discord-rpc
void Shutdown(int sig)
{
printf("\nshutting down...\n");
Discord_Shutdown();
exit(sig);
Shutdown(1);
}

// update discord rich presence
Expand Down Expand Up @@ -75,6 +76,9 @@ void updatePresence(config_t* c)

// actaully update the presence
Discord_UpdatePresence(&discordPresence);

// handle callbacks
Discord_RunCallbacks();
}

// initialize discord rich presence
Expand Down
9 changes: 6 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ int main(void)
signal(SIGBREAK, Shutdown);
#endif

// main config instance
config_t config;
config.update();

// start discord-rpc
setPresenceVariables(CONFIG_PATH);
InitDiscord(config.clientId);

// loop to keep program running also to check for updated config
do
{
if (setPresenceVariables(CONFIG_PATH) || force_update)
if (config.update() || force_update)
{
// print and set variables for the presence
printVariables(&config);
config.print();
updatePresence(&config);
}
if (force_update) force_update = false;
Expand Down
17 changes: 10 additions & 7 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
project('EasyRP', 'cpp',
version : '0.3',
version : '2.1',
default_options : ['warning_level=3'])

threads_dep = dependency('threads')
rpc_inc = include_directories('discord-rpc/include')
if build_machine.system() == 'Windows'
rpc_dep = meson.get_compiler('cpp').find_library('discord-rpc', dirs: meson.current_source_dir())
compiler = meson.get_compiler('cpp')

threads = dependency('threads')
if host_machine.system() == 'Windows'
discordrpc = compiler.find_library('discord-rpc', dirs: meson.source_root())
else
rpc_dep = meson.get_compiler('cpp').find_library('libdiscord-rpc', dirs: meson.current_source_dir())
discordrpc = compiler.find_library('libdiscord-rpc', dirs: meson.source_root())
endif

rpc_inc = include_directories('discord-rpc/include')

exe = executable('easyrp', 'main.cpp', 'config.cpp', 'discord.cpp',
include_directories : rpc_inc,
dependencies : [threads_dep, rpc_dep],
dependencies : [threads, discordrpc],
install : false)

test('basic', exe)

0 comments on commit be30077

Please sign in to comment.