Fix coding style and use Log::error instead

This commit is contained in:
Benau 2021-03-10 16:49:20 +08:00
parent d1092349f7
commit f2cc6559e6
2 changed files with 40 additions and 20 deletions

View File

@ -33,10 +33,12 @@
#include <namedpipeapi.h> #include <namedpipeapi.h>
#endif #endif
namespace RichPresenceNS { namespace RichPresenceNS
{
RichPresence* g_rich_presence = nullptr; RichPresence* g_rich_presence = nullptr;
RichPresence* RichPresence::get() { RichPresence* RichPresence::get()
{
if (g_rich_presence == nullptr) if (g_rich_presence == nullptr)
{ {
g_rich_presence = new RichPresence(); g_rich_presence = new RichPresence();
@ -44,7 +46,8 @@ RichPresence* RichPresence::get() {
return g_rich_presence; return g_rich_presence;
} }
void RichPresence::destroy() { void RichPresence::destroy()
{
if (g_rich_presence != nullptr) if (g_rich_presence != nullptr)
{ {
delete g_rich_presence; delete g_rich_presence;
@ -57,14 +60,18 @@ RichPresence::RichPresence() : m_connected(false), m_ready(false), m_last(0),
#else #else
m_socket(-1), m_socket(-1),
#endif #endif
m_thread(nullptr) { m_thread(nullptr)
{
doConnect(); doConnect();
} }
RichPresence::~RichPresence() {
RichPresence::~RichPresence()
{
terminate(); terminate();
} }
void RichPresence::terminate() { void RichPresence::terminate()
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
#ifdef WIN32 #ifdef WIN32
#define UNCLEAN m_socket != INVALID_HANDLE_VALUE #define UNCLEAN m_socket != INVALID_HANDLE_VALUE
@ -94,7 +101,8 @@ void RichPresence::terminate() {
#endif // DISABLE_RPC #endif // DISABLE_RPC
} }
bool RichPresence::doConnect() { bool RichPresence::doConnect()
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
if (std::string(UserConfigParams::m_discord_client_id) == "-1") if (std::string(UserConfigParams::m_discord_client_id) == "-1")
return false; return false;
@ -164,7 +172,8 @@ bool RichPresence::doConnect() {
#endif // DISABLE_RPC #endif // DISABLE_RPC
} }
void RichPresence::readData() { void RichPresence::readData()
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
size_t baseLength = sizeof(int32_t) * 2; size_t baseLength = sizeof(int32_t) * 2;
struct discordPacket* basePacket = (struct discordPacket*) malloc(baseLength); struct discordPacket* basePacket = (struct discordPacket*) malloc(baseLength);
@ -218,7 +227,8 @@ void RichPresence::readData() {
#endif #endif
} }
void RichPresence::finishConnection(RichPresence* self) { void RichPresence::finishConnection(RichPresence* self)
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
// We read all the data from the socket. We're clear now to handshake! // We read all the data from the socket. We're clear now to handshake!
self->handshake(); self->handshake();
@ -235,10 +245,12 @@ void RichPresence::finishConnection(RichPresence* self) {
#endif #endif
} }
bool RichPresence::tryConnect(std::string path) { bool RichPresence::tryConnect(std::string path)
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
#if !defined(WIN32) && defined(AF_UNIX) #if !defined(WIN32) && defined(AF_UNIX)
struct sockaddr_un addr = { struct sockaddr_un addr =
{
.sun_family = AF_UNIX .sun_family = AF_UNIX
}; };
memset(addr.sun_path, 0, sizeof(addr.sun_path)); memset(addr.sun_path, 0, sizeof(addr.sun_path));
@ -248,7 +260,8 @@ bool RichPresence::tryConnect(std::string path) {
// Something is probably wrong: // Something is probably wrong:
if (errno != ENOENT && errno != ECONNREFUSED) if (errno != ENOENT && errno != ECONNREFUSED)
{ {
perror("Couldn't open Discord socket!"); Log::error("RichPresence", "Couldn't read data from socket! %s",
strerror(errno));
} }
return false; return false;
} }
@ -283,7 +296,8 @@ bool RichPresence::tryConnect(std::string path) {
return m_connected; return m_connected;
} }
void RichPresence::handshake() { void RichPresence::handshake()
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
if (UserConfigParams::m_rich_presence_debug) if (UserConfigParams::m_rich_presence_debug)
Log::debug("RichPresence", "Starting handshake..."); Log::debug("RichPresence", "Starting handshake...");
@ -295,7 +309,8 @@ void RichPresence::handshake() {
#endif #endif
} }
void RichPresence::sendData(int32_t op, std::string json) { void RichPresence::sendData(int32_t op, std::string json)
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
// Handshake will make us ready: // Handshake will make us ready:
if (op != OP_HANDSHAKE && !m_ready) if (op != OP_HANDSHAKE && !m_ready)
@ -346,7 +361,8 @@ void RichPresence::sendData(int32_t op, std::string json) {
#endif #endif
} }
void RichPresence::update(bool force) { void RichPresence::update(bool force)
{
#ifndef DISABLE_RPC #ifndef DISABLE_RPC
if (STKProcess::getType() != PT_MAIN) if (STKProcess::getType() != PT_MAIN)
{ {

View File

@ -3,18 +3,22 @@
#endif #endif
#include <thread> #include <thread>
namespace RichPresenceNS { namespace RichPresenceNS
{
// There are more, but we don't need to use them // There are more, but we don't need to use them
enum OPCodes { enum OPCodes
{
OP_HANDSHAKE = 0, OP_HANDSHAKE = 0,
OP_DATA = 1, OP_DATA = 1,
}; };
struct discordPacket { struct discordPacket
{
int32_t op; int32_t op;
int32_t length; int32_t length;
char data[]; char data[];
}; };
class RichPresence { class RichPresence
{
private: private:
bool m_connected; bool m_connected;
bool m_ready; bool m_ready;