static void setCurrentRPCApp(dRPCApplication app, bool reInit = true) { pPrint($"[I] Setting RPresence to: '{app.sAppName}'...", ConsoleColor.DarkYellow); if (dRpcClient != null && dRpcClient.IsInitialized) { if (reInit) { pPrint("[I] Closing current RPC instance...", ConsoleColor.Yellow); dRpcClient.ClearPresence(); dRpcClient.Deinitialize(); pPrint("[I] Creating a new RPC instance...", ConsoleColor.Yellow); dRpcClient = new DiscordRpcClient(app.sAppId); dRpcClient.Logger = new ConsoleLoggerFormatted() { Level = lLogLevel }; dRpcClient.Initialize(); } RichPresence rp = new RichPresence(); Assets assets = new Assets(); assets.LargeImageKey = app.sLargeImgKey; assets.SmallImageKey = app.sSmallImgKey; assets.LargeImageText = app.sLargeImgText; assets.SmallImageText = app.sSmallImgText; rp.Assets = assets; rp.Details = app.sDetails; rp.State = app.sState; pPrint("[I] Updating Rich Presence...", ConsoleColor.Yellow); dRpcClient.SetPresence(rp); dRPCActiveApp = app; } }
static Task setRPCApps() { if (Directory.Exists(Environment.CurrentDirectory + "\\apps")) { if (Directory.GetFiles(Environment.CurrentDirectory + "\\apps").Length > 0) { foreach (var jsonfile in Directory.GetFiles(Environment.CurrentDirectory + "\\apps")) { if (jsonfile.EndsWith(".json")) { dRPCApplication dRPCAppDeserialized = JsonConvert.DeserializeObject <dRPCApplication>(File.ReadAllText(jsonfile)); dRPCAppList.Add(dRPCAppDeserialized); pPrint($"[I] File {Path.GetFileName(jsonfile)} found!", ConsoleColor.Green); } } } else { pPrint("[E] There are no applications found in the 'apps' directory!", ConsoleColor.Red); pPrint("Press [ENTER] To exit.", ConsoleColor.Gray); Console.ReadLine(); Environment.Exit(1); } } else { pPrint("[E] Directory 'apps' does not appear to exist!", ConsoleColor.Red); pPrint("Press [ENTER] To exit.", ConsoleColor.Gray); Console.ReadLine(); Environment.Exit(1); } return(Task.CompletedTask); }
static void rpcProcessHandler() { for (; ;) { foreach (var app in dRPCAppList) { if (isProcessRunning(app.sProcessName)) { if (dRPCActiveApp != app) { pPrint($"[I] Found new Application '{app.sAppName}' (Process: '{app.sProcessName}.exe')", ConsoleColor.DarkYellow); } if (dRpcClient == null && dRPCActiveApp == null && bFirstInit) { dRpcClient = new DiscordRpcClient(app.sAppId); dRpcClient.Initialize(); dRpcClient.Logger = new ConsoleLoggerFormatted() { Level = lLogLevel }; setCurrentRPCApp(app, false); bFirstInit = false; } else if (dRPCActiveApp != app && dRPCActiveApp == null) { setCurrentRPCApp(app); } } if (dRPCActiveApp == app && !isProcessRunning(app.sProcessName)) { dRpcClient.ClearPresence(); dRPCActiveApp = null; pPrint($"[I] Application '{app.sAppName}' (Process: '{app.sProcessName}.exe') appears to be closed. Clearing Presence...", ConsoleColor.DarkYellow); } } Thread.Sleep(cfg.rpcThreadUpdateInt); } }