Files
SirHumza--orbisRPC/plugin/plugin.c
T
SirHumza 6dbfa1512c Harden networking, lifecycle, and build validation
Add BearSSL integration, strict utility handling, WebSocket validation,
plugin lifecycle cleanup, host regression tests, and reproducible GoldHEN
build support.
2026-08-27 20:38:39 +02:00

116 lines
4.2 KiB
C

/* plugin.c - orbisRPC as a GoldHEN plugin (.prx loaded into the game process).
*
* GoldHEN loads plugins per game title via /data/GoldHEN/plugins.ini. A
* plugin runs inside the game's process, so:
* - we KNOW which game is running: sys_sdk_proc_info() -> procInfo.titleid
* - the game has full network access (this is the whole point)
* - it auto-starts at every boot/jailbreak with zero PC involvement
*
* plugin_load() resolves the titleid to a display name and starts the shared
* daemon thread (daemon.c) which handles token refresh, the Discord gateway
* and presence. plugin_unload() stops it cleanly.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <orbis/libkernel.h>
#include "Common.h"
#include "daemon.h"
#include "detect.h"
/* Only post presence for real game titles (CUSA/PPSA/etc). System apps
* (NPXS..., shell) launching would otherwise post bogus "playing" state. */
static int is_game_titleid(const char *t){
static const char *pfx[] = { "CUSA", "PPSA", "PCSE", "PCJS", "EPSA", "PCSB", NULL };
for(int i=0; pfx[i]; i++) if(strncmp(t, pfx[i], 4)==0) return 1;
return 0;
}
#define attr_public __attribute__((visibility("default")))
#define attr_module_hidden __attribute__((weak)) __attribute__((visibility("hidden")))
attr_public const char *g_pluginName = "orbisrpc";
attr_public const char *g_pluginDesc = "Discord Rich Presence for PS4";
attr_public const char *g_pluginAuth = "orbisRPC";
attr_public uint32_t g_pluginVersion = 0x00000100; /* 1.00 */
struct proc_info procInfo;
static OrbisPthread s_daemon_thread;
static int s_daemon_started = 0;
static void *daemon_thread(void *args){
/* args = display name of the game we are loaded into */
char name[128];
strncpy(name, (const char *)args, sizeof name - 1);
name[sizeof name - 1] = 0;
daemon_clear_stop();
daemon_run(name);
return NULL;
}
int32_t attr_public plugin_load(int32_t argc, const char *argv[]){
(void)argc; (void)argv;
klog("[orbisrpc] <%s\\Ver.0x%08x> %s\n", g_pluginName, g_pluginVersion, __func__);
klog("[orbisrpc] Plugin Author(s): %s\n", g_pluginAuth);
if(sys_sdk_proc_info(&procInfo) != 0){
klog("[orbisrpc] sys_sdk_proc_info failed; not starting\n");
return 0;
}
klog("[orbisrpc] loaded into process: pid=%d name=%s titleid=%s\n",
procInfo.pid, procInfo.name, procInfo.titleid);
if(!is_game_titleid(procInfo.titleid)){
klog("[orbisrpc] not a game title (system app?); not starting daemon\n");
return 0;
}
/* Resolve a display name for the title we are loaded into. */
static char game_name[128];
detect_name_for_title(procInfo.titleid, game_name, sizeof game_name);
klog("[orbisrpc] game: %s\n", game_name);
/* explicit fat stack: TLS handshakes + JSON buffers overflow the
* small default plugin-thread stacks */
OrbisPthread thread;
OrbisPthreadAttr attr;
memset(&attr, 0, sizeof attr);
if(scePthreadAttrInit(&attr) == 0 &&
scePthreadAttrSetstacksize(&attr, 256*1024) == 0){
if(scePthreadCreate(&s_daemon_thread, &attr, daemon_thread, game_name, "orbisrpc_daemon") == 0)
s_daemon_started = 1;
else
klog("[orbisrpc] daemon thread creation failed\\n");
}else{
klog("[orbisrpc] attr init failed; falling back to default stack\n");
if(scePthreadCreate(&s_daemon_thread, NULL, daemon_thread, game_name, "orbisrpc_daemon2") == 0)
s_daemon_started = 1;
else
klog("[orbisrpc] daemon thread creation failed\\n");
}
return 0;
}
int32_t attr_public plugin_unload(int32_t argc, const char *argv[]){
(void)argc; (void)argv;
klog("[orbisrpc] <%s\\Ver.0x%08x> %s\n", g_pluginName, g_pluginVersion, __func__);
daemon_request_stop();
if(s_daemon_started){
int rc = scePthreadJoin(s_daemon_thread, NULL);
klog("[orbisrpc] daemon thread joined rc=%d\\n", rc);
s_daemon_started = 0;
}
return 0;
}
int32_t attr_module_hidden module_start(int64_t argc, const void *args){
(void)argc; (void)args;
return 0;
}
int32_t attr_module_hidden module_stop(int64_t argc, const void *args){
(void)argc; (void)args;
return 0;
}