replace static linking with dynamically loaded libraries

This commit is contained in:
Jan200101 2020-06-09 17:15:49 +02:00
parent f1ba90d25b
commit 5933742f82
No known key found for this signature in database
GPG key ID: 5B71B1D78B882E05
8 changed files with 79 additions and 52 deletions

View file

@ -457,12 +457,12 @@ GODDARD_O_FILES := $(foreach file,$(GODDARD_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
RPC_LIBS :=
ifeq ($(DISCORDRPC),1)
ifeq ($(WINDOWS_BUILD),1)
RPC_LIBS := src/pc/discord/libs/libdiscord-rpc-win.a
RPC_LIBS := src/pc/discord/lib/libdiscord-rpc.dll
else ifeq ($(OSX_BUILD),1)
# needs testing
RPC_LIBS := src/pc/discord/libs/libdiscord-rpc-osx.a
RPC_LIBS := src/pc/discord/lib/libdiscord-rpc.dylib
else
RPC_LIBS := src/pc/discord/libs/libdiscord-rpc-unix.a
RPC_LIBS := src/pc/discord/lib/libdiscord-rpc.so
endif
endif
@ -620,6 +620,9 @@ ifeq ($(OSX_BUILD),1)
LDFLAGS := -lm -framework OpenGL `$(SDLCONFIG) --libs` -no-pie -lpthread `pkg-config --libs libusb-1.0 glfw3 glew`
else
LDFLAGS := $(BITS) -march=$(TARGET_ARCH) -lm -lGL `$(SDLCONFIG) --libs` -no-pie -lpthread
ifeq ($(DISCORDRPC),1)
LDFLAGS += -ldl -Wl,-rpath .
endif
endif
endif # End of LDFLAGS
@ -655,8 +658,6 @@ ZEROTERM = $(PYTHON) $(TOOLS_DIR)/zeroterm.py
all: $(EXE)
ifeq ($(EXTERNAL_DATA),1)
# thank you apple very cool
ifeq ($(HOST_OS),Darwin)
CP := gcp
@ -664,6 +665,8 @@ else
CP := cp
endif
ifeq ($(EXTERNAL_DATA),1)
# depend on resources as well
all: res
@ -697,6 +700,9 @@ test: $(ROM)
load: $(ROM)
$(LOADER) $(LOADER_FLAGS) $<
$(BUILD_DIR)/$(RPC_LIBS):
@$(CP) -f $(RPC_LIBS) $(BUILD_DIR)
libultra: $(BUILD_DIR)/libultra.a
asm/boot.s: $(BUILD_DIR)/lib/bin/ipl3_font.bin
@ -956,8 +962,8 @@ $(BUILD_DIR)/%.o: %.s
$(EXE): $(O_FILES) $(MIO0_FILES:.mio0=.o) $(SOUND_OBJ_FILES) $(ULTRA_O_FILES) $(GODDARD_O_FILES) $(RPC_LIBS)
$(LD) -L $(BUILD_DIR) -o $@ $(O_FILES) $(SOUND_OBJ_FILES) $(ULTRA_O_FILES) $(GODDARD_O_FILES) $(RPC_LIBS) $(LDFLAGS)
$(EXE): $(O_FILES) $(MIO0_FILES:.mio0=.o) $(SOUND_OBJ_FILES) $(ULTRA_O_FILES) $(GODDARD_O_FILES) $(BUILD_DIR)/$(RPC_LIBS)
$(LD) -L $(BUILD_DIR) -o $@ $(O_FILES) $(SOUND_OBJ_FILES) $(ULTRA_O_FILES) $(GODDARD_O_FILES) $(LDFLAGS)
.PHONY: all clean distclean default diff test load libultra res
.PRECIOUS: $(BUILD_DIR)/bin/%.elf $(SOUND_BIN_DIR)/%.ctl $(SOUND_BIN_DIR)/%.tbl $(SOUND_SAMPLE_TABLES) $(SOUND_BIN_DIR)/%.s $(BUILD_DIR)/%

View file

@ -23,6 +23,10 @@
#include "gfx_dimensions.h"
#ifdef DISCORDRPC
#include "pc/discord/discordrpc.h"
#endif
struct SpawnInfo gPlayerSpawnInfos[1];
struct GraphNode *D_8033A160[0x100];
struct Area gAreaData[8];

View file

@ -7,12 +7,42 @@
#include "pc/configfile.h"
#include "discordrpc.h"
#define DISCORDLIBFILE "libdiscord-rpc"
// Thanks Microsoft for being non posix compliant
#if defined(_WIN32)
#include <windows.h>
#define DISCORDLIBEXT ".dll"
#define itoa(int, str) itoa(int, str, "10")
#define dlopen(lib, flag) LoadLibrary(TEXT(lib))
#define dlerror() ""
#define dlsym(handle, func) GetProcAddress(handle, func)
#define dlclose(handle) FreeLibrary(handle)
#elif defined(__APPLE__)
#include <dlfcn.h>
#define DISCORDLIBEXT ".dylib"
#elif defined(__linux__) || defined(__FreeBSD__) // lets make the bold assumption for FreeBSD
#include <dlfcn.h>
#define DISCORDLIBEXT ".so"
#else
#error Unknown System
#endif
#define DISCORDLIB DISCORDLIBFILE DISCORDLIBEXT
#define DISCORD_APP_ID "709083908708237342"
#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 gCurrActNum;
@ -91,7 +121,7 @@ void convertstring(const u8 *str, char* output)
switch (output[strPos]) // decide if the next character should be capitalized
{
case ' ':
if (str[strPos] != 158) printf(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 '-':
capitalizeChar = true;
break;
@ -106,6 +136,7 @@ void convertstring(const u8 *str, char* output)
output[strPos] = '\0';
}
#ifndef _WIN32
void reverse(char s[])
{
int i, j;
@ -132,6 +163,7 @@ void itoa(int n, char s[])
s[i] = '\0';
reverse(s);
}
#endif
void OnReady( const DiscordUser* user )
{
@ -142,9 +174,11 @@ void InitializeDiscord()
{
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = OnReady;
handlers.ready = OnReady;
Discord_Initialize( DISCORD_APP_ID, &handlers, false, "" );
(*Discord_Initialize)(DISCORD_APP_ID, &handlers, false, "");
initd = true;
}
void SetDetails()
@ -247,7 +281,7 @@ void SetLogo()
void discordUpdateRichPresence()
{
if (!configDiscordRPC) return;
if (!configDiscordRPC || !initd) return;
if (time(NULL) < lastUpdatedTime + DISCORD_UPDATE_RATE) return;
lastUpdatedTime = time(NULL);
@ -260,13 +294,31 @@ void discordUpdateRichPresence()
void discordShutdown()
{
Discord_Shutdown();
if (handle)
{
(*Discord_ClearPresence)();
(*Discord_Shutdown)();
dlclose(handle);
}
};
void discordInit()
{
if (configDiscordRPC)
{
handle = dlopen(DISCORDLIB, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Unable to load Discord\n%s\n", dlerror());
return;
}
Discord_Initialize = dlsym(handle, "Discord_Initialize");
Discord_Shutdown = dlsym(handle, "Discord_Shutdown");
Discord_ClearPresence = dlsym(handle, "Discord_ClearPresence");
Discord_UpdatePresence = dlsym(handle, "Discord_UpdatePresence");
InitializeDiscord();
discordRichPresence.details = stage;
@ -284,6 +336,6 @@ void discordReset()
SetState();
SetDetails();
SetLogo();
Discord_UpdatePresence( &discordRichPresence );
(*Discord_UpdatePresence)(&discordRichPresence);
}

View file

@ -3,24 +3,6 @@
#include <stdint.h>
// clang-format off
#if defined(DISCORD_DYNAMIC_LIB)
# if defined(_WIN32)
# if defined(DISCORD_BUILDING_SDK)
# define DISCORD_EXPORT __declspec(dllexport)
# else
# define DISCORD_EXPORT __declspec(dllimport)
# endif
# else
# define DISCORD_EXPORT __attribute__((visibility("default")))
# endif
#else
# define DISCORD_EXPORT
#endif
// clang-format on
typedef struct DiscordRichPresence {
const char* state; /* max 128 bytes */
const char* details; /* max 128 bytes */
@ -59,27 +41,6 @@ typedef struct DiscordEventHandlers {
#define DISCORD_REPLY_YES 1
#define DISCORD_REPLY_IGNORE 2
DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
DiscordEventHandlers* handlers,
int autoRegister,
const char* optionalSteamId);
DISCORD_EXPORT void Discord_Shutdown(void);
/* checks for incoming messages, dispatches callbacks */
DISCORD_EXPORT void Discord_RunCallbacks(void);
/* If you disable the lib starting its own io thread, you'll need to call this from your own */
#ifdef DISCORD_DISABLE_IO_THREAD
DISCORD_EXPORT void Discord_UpdateConnection(void);
#endif
DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence);
DISCORD_EXPORT void Discord_ClearPresence(void);
DISCORD_EXPORT void Discord_Respond(const char* userid, /* DISCORD_REPLY_ */ int reply);
DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* handlers);
void discordUpdateRichPresence();
void discordShutdown();
void discordInit();

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -32,6 +32,10 @@
#include "src/pc/controller/controller_keyboard.h"
#ifdef DISCORDRPC
#include "pc/discord/discordrpc.h"
#endif
// TODO: figure out if this shit even works
#ifdef VERSION_EU
# define FRAMERATE 25