clean up discord rpc a bit

This commit is contained in:
fgsfds 2020-06-09 20:46:26 +03:00
parent dc17d19cd1
commit 01e6061276
6 changed files with 95 additions and 114 deletions

View file

@ -427,6 +427,6 @@ void render_game(void) {
D_8032CE78 = 0; D_8032CE78 = 0;
#ifdef DISCORDRPC #ifdef DISCORDRPC
discordUpdateRichPresence(); discord_update_rich_presence();
#endif #endif
} }

View file

@ -15,6 +15,8 @@ struct AllocOnlyPool
}; };
struct MemoryPool; struct MemoryPool;
struct MarioAnimation;
struct Animation;
#ifndef INCLUDED_FROM_MEMORY_C #ifndef INCLUDED_FROM_MEMORY_C
// Declaring this variable extern puts it in the wrong place in the bss order // Declaring this variable extern puts it in the wrong place in the bss order
@ -52,6 +54,7 @@ void *mem_pool_alloc(struct MemoryPool *pool, u32 size);
void mem_pool_free(struct MemoryPool *pool, void *addr); void mem_pool_free(struct MemoryPool *pool, void *addr);
void *alloc_display_list(u32 size); void *alloc_display_list(u32 size);
void func_80278A78(struct MarioAnimation *a, void *b, struct Animation *target); void func_80278A78(struct MarioAnimation *a, void *b, struct Animation *target);
s32 load_patchable_table(struct MarioAnimation *a, u32 b); s32 load_patchable_table(struct MarioAnimation *a, u32 b);

View file

@ -2,8 +2,10 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "macros.h"
#include "PR/ultratypes.h" #include "PR/ultratypes.h"
#include "memory.h" #include "game/memory.h"
#include "pc/configfile.h" #include "pc/configfile.h"
#include "discordrpc.h" #include "discordrpc.h"
@ -11,57 +13,57 @@
// Thanks Microsoft for being non posix compliant // Thanks Microsoft for being non posix compliant
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> # include <windows.h>
#define DISCORDLIBEXT ".dll" # define DISCORDLIBEXT ".dll"
#define dlopen(lib, flag) LoadLibrary(TEXT(lib)) # define dlopen(lib, flag) LoadLibrary(TEXT(lib))
#define dlerror() "" # define dlerror() ""
#define dlsym(handle, func) GetProcAddress(handle, func) # define dlsym(handle, func) (void *)GetProcAddress(handle, func)
#define dlclose(handle) FreeLibrary(handle) # define dlclose(handle) FreeLibrary(handle)
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include <dlfcn.h> # include <dlfcn.h>
#define DISCORDLIBEXT ".dylib" # define DISCORDLIBEXT ".dylib"
#elif defined(__linux__) || defined(__FreeBSD__) // lets make the bold assumption for FreeBSD #elif defined(__linux__) || defined(__FreeBSD__) // lets make the bold assumption for FreeBSD
#include <dlfcn.h> # include <dlfcn.h>
#define DISCORDLIBEXT ".so" # define DISCORDLIBEXT ".so"
#else #else
#error Unknown System # error Unknown System
#endif #endif
#define DISCORDLIB DISCORDLIBFILE DISCORDLIBEXT
#define DISCORDLIB DISCORDLIBFILE DISCORDLIBEXT
#define DISCORD_APP_ID "709083908708237342" #define DISCORD_APP_ID "709083908708237342"
#define DISCORD_UPDATE_RATE 5 #define DISCORD_UPDATE_RATE 5
time_t lastUpdatedTime;
DiscordRichPresence discordRichPresence;
bool initd = false;
void* handle;
void (*Discord_Initialize)(const char*, DiscordEventHandlers*, int, const char*);
void (*Discord_Shutdown)(void);
void (*Discord_ClearPresence)(void);
void (*Discord_UpdatePresence)(DiscordEventHandlers*);
extern s16 gCurrCourseNum; extern s16 gCurrCourseNum;
extern s16 gCurrActNum; extern s16 gCurrActNum;
s16 lastCourseNum = -1;
s16 lastActNum = -1;
extern u8 seg2_course_name_table[]; extern u8 seg2_course_name_table[];
extern u8 seg2_act_name_table[]; extern u8 seg2_act_name_table[];
static time_t lastUpdatedTime;
static DiscordRichPresence discordRichPresence;
static bool initd = false;
static void* handle;
void (*Discord_Initialize)(const char *, DiscordEventHandlers *, int, const char *);
void (*Discord_Shutdown)(void);
void (*Discord_ClearPresence)(void);
void (*Discord_UpdatePresence)(DiscordRichPresence *);
static s16 lastCourseNum = -1;
static s16 lastActNum = -1;
#ifdef VERSION_EU #ifdef VERSION_EU
extern s32 gInGameLanguage; extern s32 gInGameLanguage;
#endif #endif
char stage[188]; static char stage[188];
char act[188]; static char act[188];
char smallImageKey[5]; static char smallImageKey[5];
char largeImageKey[5]; static char largeImageKey[5];
char charset[0xFF+1] = { static const char charset[0xFF+1] = {
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 7 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 7
' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', // 15 ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', // 15
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 23 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 23
@ -96,31 +98,29 @@ char charset[0xFF+1] = {
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' // 255 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' // 255
}; };
void convertstring(const u8 *str, char* output) static void convertstring(const u8 *str, char* output) {
{
s32 strPos = 0; s32 strPos = 0;
bool capitalizeChar = true; bool capitalizeChar = true;
while (str[strPos] != 0xFF) while (str[strPos] != 0xFF) {
{ if (str[strPos] < 0xFF) {
if (str[strPos] < 0xFF)
{
output[strPos] = charset[str[strPos]]; output[strPos] = charset[str[strPos]];
// if the char is a letter we can capatalize it // if the char is a letter we can capatalize it
if (capitalizeChar && 0x0A <= str[strPos] && str[strPos] <= 0x23) if (capitalizeChar && 0x0A <= str[strPos] && str[strPos] <= 0x23) {
{
output[strPos] -= ('a' - 'A'); output[strPos] -= ('a' - 'A');
capitalizeChar = false; capitalizeChar = false;
} }
} else {
output[strPos] = ' ';
} }
else output[strPos] = ' ';
switch (output[strPos]) // decide if the next character should be capitalized // decide if the next character should be capitalized
{ switch (output[strPos]) {
case ' ': case ' ':
if (str[strPos] != 158) fprintf(stdout, "Unknown Character (%i)\n", str[strPos]); // inform that an unknown char was found if (str[strPos] != 158)
fprintf(stdout, "Unknown Character (%i)\n", str[strPos]); // inform that an unknown char was found
case '-': case '-':
capitalizeChar = true; capitalizeChar = true;
break; break;
@ -135,29 +135,24 @@ void convertstring(const u8 *str, char* output)
output[strPos] = '\0'; output[strPos] = '\0';
} }
void OnReady( const DiscordUser* user ) static void on_ready(UNUSED const DiscordUser* user) {
{ discord_reset();
discordReset();
} }
void InitializeDiscord() static void init_discord(void) {
{
DiscordEventHandlers handlers; DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers)); memset(&handlers, 0, sizeof(handlers));
handlers.ready = OnReady; handlers.ready = on_ready;
Discord_Initialize(DISCORD_APP_ID, &handlers, false, ""); Discord_Initialize(DISCORD_APP_ID, &handlers, false, "");
initd = true; initd = true;
} }
void SetDetails() static void set_details(void) {
{ if (lastCourseNum != gCurrCourseNum) {
if (lastCourseNum != gCurrCourseNum)
{
// If we are in in Course 0 we are in the castle which doesn't have a string // If we are in in Course 0 we are in the castle which doesn't have a string
if (gCurrCourseNum) if (gCurrCourseNum) {
{
void **courseNameTbl; void **courseNameTbl;
#ifndef VERSION_EU #ifndef VERSION_EU
@ -178,22 +173,20 @@ void SetDetails()
u8 *courseName = segmented_to_virtual(courseNameTbl[gCurrCourseNum - 1]); u8 *courseName = segmented_to_virtual(courseNameTbl[gCurrCourseNum - 1]);
convertstring(&courseName[3], stage); convertstring(&courseName[3], stage);
} else {
strcpy(stage, "Peach's Castle");
} }
else strcpy(stage, "Peach's Castle");
lastCourseNum = gCurrCourseNum; lastCourseNum = gCurrCourseNum;
} }
} }
void SetState() static void set_state(void) {
{ if (lastActNum != gCurrActNum || lastCourseNum != gCurrCourseNum) {
if (lastActNum != gCurrActNum || lastCourseNum != gCurrCourseNum)
{
// when exiting a stage the act doesn't get reset // when exiting a stage the act doesn't get reset
if (gCurrActNum && gCurrCourseNum) if (gCurrActNum && gCurrCourseNum) {
{ // any stage over 19 is a special stage without acts
if (gCurrCourseNum < 19) // any stage over 19 is a special stage without acts if (gCurrCourseNum < 19) {
{
void **actNameTbl; void **actNameTbl;
#ifndef VERSION_EU #ifndef VERSION_EU
actNameTbl = segmented_to_virtual(seg2_act_name_table); actNameTbl = segmented_to_virtual(seg2_act_name_table);
@ -213,34 +206,29 @@ void SetState()
u8 *actName = actName = segmented_to_virtual(actNameTbl[(gCurrCourseNum - 1) * 6 + gCurrActNum - 1]); u8 *actName = actName = segmented_to_virtual(actNameTbl[(gCurrCourseNum - 1) * 6 + gCurrActNum - 1]);
convertstring(actName, act); convertstring(actName, act);
} } else {
else
{
act[0] = '\0'; act[0] = '\0';
gCurrActNum = 0; gCurrActNum = 0;
} }
} else {
act[0] = '\0';
} }
else act[0] = '\0';
lastActNum = gCurrActNum; lastActNum = gCurrActNum;
} }
} }
void SetLogo() void set_logo(void) {
{
if (lastCourseNum) if (lastCourseNum)
{
snprintf(largeImageKey, sizeof(largeImageKey), "%d", lastCourseNum); snprintf(largeImageKey, sizeof(largeImageKey), "%d", lastCourseNum);
} else
else strcpy(largeImageKey, "0"); strcpy(largeImageKey, "0");
/* /*
if (lastActNum) if (lastActNum)
{
snprintf(smallImageKey, sizeof(largeImageKey), "%d", lastActNum); snprintf(smallImageKey, sizeof(largeImageKey), "%d", lastActNum);
} else
else smallImageKey[0] = '\0'; smallImageKey[0] = '\0';
*/ */
discordRichPresence.largeImageKey = largeImageKey; discordRichPresence.largeImageKey = largeImageKey;
@ -249,35 +237,28 @@ void SetLogo()
//discordRichPresence.smallImageText = ""; //discordRichPresence.smallImageText = "";
} }
void discordUpdateRichPresence() void discord_update_rich_presence(void) {
{
if (!configDiscordRPC || !initd) return; if (!configDiscordRPC || !initd) return;
if (time(NULL) < lastUpdatedTime + DISCORD_UPDATE_RATE) return; if (time(NULL) < lastUpdatedTime + DISCORD_UPDATE_RATE) return;
lastUpdatedTime = time(NULL); lastUpdatedTime = time(NULL);
SetState(); set_state();
SetDetails(); set_details();
SetLogo(); set_logo();
Discord_UpdatePresence(&discordRichPresence); Discord_UpdatePresence(&discordRichPresence);
} }
void discordShutdown() void discord_shutdown(void) {
{ if (handle) {
if (handle)
{
Discord_ClearPresence(); Discord_ClearPresence();
Discord_Shutdown(); Discord_Shutdown();
dlclose(handle); dlclose(handle);
} }
}
}; void discord_init(void) {
if (configDiscordRPC) {
void discordInit()
{
if (configDiscordRPC)
{
handle = dlopen(DISCORDLIB, RTLD_LAZY); handle = dlopen(DISCORDLIB, RTLD_LAZY);
if (!handle) { if (!handle) {
fprintf(stderr, "Unable to load Discord\n%s\n", dlerror()); fprintf(stderr, "Unable to load Discord\n%s\n", dlerror());
@ -289,23 +270,20 @@ void discordInit()
Discord_ClearPresence = dlsym(handle, "Discord_ClearPresence"); Discord_ClearPresence = dlsym(handle, "Discord_ClearPresence");
Discord_UpdatePresence = dlsym(handle, "Discord_UpdatePresence"); Discord_UpdatePresence = dlsym(handle, "Discord_UpdatePresence");
InitializeDiscord(); init_discord();
discordRichPresence.details = stage; discordRichPresence.details = stage;
discordRichPresence.state = act; discordRichPresence.state = act;
lastUpdatedTime = 0; lastUpdatedTime = 0;
} }
};
void discordReset()
{
memset( &discordRichPresence, 0, sizeof( discordRichPresence ) );
SetState();
SetDetails();
SetLogo();
(*Discord_UpdatePresence)(&discordRichPresence);
} }
void discord_reset(void) {
memset( &discordRichPresence, 0, sizeof( discordRichPresence ) );
set_state();
set_details();
set_logo();
Discord_UpdatePresence(&discordRichPresence);
}

View file

@ -41,9 +41,9 @@ typedef struct DiscordEventHandlers {
#define DISCORD_REPLY_YES 1 #define DISCORD_REPLY_YES 1
#define DISCORD_REPLY_IGNORE 2 #define DISCORD_REPLY_IGNORE 2
void discordUpdateRichPresence(); void discord_update_rich_presence(void);
void discordShutdown(); void discord_shutdown(void);
void discordInit(); void discord_init(void);
void discordReset(); void discord_reset(void);
#endif // DISCORDRPC_H #endif // DISCORDRPC_H

View file

@ -199,7 +199,7 @@ static void gfx_sdl_init(void) {
static void gfx_sdl_main_loop(void (*run_one_game_iter)(void)) { static void gfx_sdl_main_loop(void (*run_one_game_iter)(void)) {
Uint32 t; Uint32 t;
#ifdef DISCORDRPC #ifdef DISCORDRPC
discordInit(); discord_init();
#endif #endif
while (1) { while (1) {
t = SDL_GetTicks(); t = SDL_GetTicks();
@ -266,7 +266,7 @@ static void gfx_sdl_handle_events(void) {
break; break;
case SDL_QUIT: case SDL_QUIT:
#ifdef DISCORDRPC #ifdef DISCORDRPC
discordShutdown(); discord_shutdown();
#endif #endif
game_exit(); game_exit();
break; break;

View file

@ -183,7 +183,7 @@ void main_func(void) {
#ifdef EXTERNAL_DATA #ifdef EXTERNAL_DATA
// precache data if needed // precache data if needed
if (configPrecacheRes) { if (configPrecacheRes) {
printf("precaching data\n"); fprintf(stdout, "precaching data\n");
fflush(stdout); fflush(stdout);
gfx_precache_textures(); gfx_precache_textures();
} }