Implemented rpc handler and added rpc library

This commit is contained in:
Serraniel 2018-04-01 20:16:58 +02:00
parent 2add4afa24
commit 051b0713ee
2 changed files with 54 additions and 3 deletions

View file

@ -8,6 +8,7 @@ using System.Windows.Forms;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using DML.AppCore.Classes; using DML.AppCore.Classes;
using DML.Application.Classes.RPC;
using DML.Application.Dialogs; using DML.Application.Dialogs;
using DML.Client; using DML.Client;
using LiteDB; using LiteDB;
@ -26,11 +27,14 @@ namespace DML.Application.Classes
internal static LiteDatabase Database { get; set; } internal static LiteDatabase Database { get; set; }
internal static Settings Settings { get; set; } internal static Settings Settings { get; set; }
internal static JobScheduler Scheduler { get; } = new JobScheduler(); internal static JobScheduler Scheduler { get; } = new JobScheduler();
internal static RavenClient Raven = new RavenClient("https://0de964231669473e9098b9f6cc1d6278:79d9f2eb24034de199b2a37cc058e0f2@sentry.io/257114"); internal static RavenClient Raven { get; } = new RavenClient("https://0de964231669473e9098b9f6cc1d6278:79d9f2eb24034de199b2a37cc058e0f2@sentry.io/257114");
internal static bool ShuttingDown { get; private set; } = false;
internal static string DataDirectory internal static string DataDirectory
=> Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Serraniel\Discord Media Loader"); => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Serraniel\Discord Media Loader");
internal static DiscordRpc.RichPresence RpcPresence;
internal static DiscordRpc.EventHandlers RpcHandlers = new DiscordRpc.EventHandlers();
public static async Task Run(string[] paramStrings) public static async Task Run(string[] paramStrings)
{ {
try try
@ -257,6 +261,26 @@ namespace DML.Application.Classes
} }
} }
Logger.Info("Initializing RPC client");
Logger.Trace("Registering RPC handlers");
RpcHandlers.readyCallback += RpcReadyCallback;
RpcHandlers.disconnectedCallback += RpcDisconnectedCallback;
RpcHandlers.errorCallback += RpcErrorCallback;
RpcPresence.startTimestamp = DiscordRpcHelper.DateTimeToTimestamp(DateTime.UtcNow);
Logger.Debug("Calling RPC initialize");
DiscordRpc.Initialize("430025401851707393", ref RpcHandlers, true, null);
// Do not await ;)
Task.Run(async () =>
{
while (!ShuttingDown)
{
Logger.Trace("Running RPC callbacks");
await Task.Delay(5000);
}
});
splash.Close(); splash.Close();
Logger.Info("Starting scheduler..."); Logger.Info("Starting scheduler...");
@ -264,8 +288,14 @@ namespace DML.Application.Classes
System.Windows.Forms.Application.Run(new MainForm()); System.Windows.Forms.Application.Run(new MainForm());
// shutting down
ShuttingDown = true;
Logger.Info("Stopping scheduler..."); Logger.Info("Stopping scheduler...");
Scheduler.Stop(); Scheduler.Stop();
Logger.Info("Shutting down RPC client");
DiscordRpc.Shutdown();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -293,5 +323,26 @@ namespace DML.Application.Classes
Logger.Trace($"Trying to find channel in {server} by id: {id}"); Logger.Trace($"Trying to find channel in {server} by id: {id}");
return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault(); return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault();
} }
internal static void RpcUpdatePresence()
{
Logger.Debug("Updating RPC presence");
DiscordRpc.UpdatePresence(ref RpcPresence);
}
private static void RpcReadyCallback()
{
Logger.Debug("RpcReady");
}
private static void RpcDisconnectedCallback(int errorCode, string message)
{
Logger.Warn($"RPC disconnect received: {errorCode} - {message}");
}
private static void RpcErrorCallback(int errorCode, string message)
{
Logger.Error($"RPC error received: {errorCode} - {message}");
}
} }
} }

Binary file not shown.