Merge branch 'feature/RPC' into develop
* feature/RPC: Revert "Added discord rpc and demo repository" Implemented basic rpc setup Implemented rpc handler and added rpc library Added helper file Added basic RPC wrapper into project Disabled update check on debug versions Added discord rpc and demo repository
This commit is contained in:
commit
c36acc0750
|
@ -8,6 +8,7 @@ using System.Windows.Forms;
|
|||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using DML.AppCore.Classes;
|
||||
using DML.Application.Classes.RPC;
|
||||
using DML.Application.Dialogs;
|
||||
using DML.Client;
|
||||
using LiteDB;
|
||||
|
@ -26,11 +27,14 @@ namespace DML.Application.Classes
|
|||
internal static LiteDatabase Database { get; set; }
|
||||
internal static Settings Settings { get; set; }
|
||||
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
|
||||
=> 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)
|
||||
{
|
||||
try
|
||||
|
@ -175,7 +179,7 @@ namespace DML.Application.Classes
|
|||
if (!string.IsNullOrEmpty(Settings.LoginToken))
|
||||
{
|
||||
Logger.Debug("Trying to login with last known token...");
|
||||
loggedIn= await DMLClient.Login(Settings.LoginToken);
|
||||
loggedIn = await DMLClient.Login(Settings.LoginToken);
|
||||
}
|
||||
|
||||
if (!loggedIn)
|
||||
|
@ -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();
|
||||
|
||||
Logger.Info("Starting scheduler...");
|
||||
|
@ -264,8 +288,14 @@ namespace DML.Application.Classes
|
|||
|
||||
System.Windows.Forms.Application.Run(new MainForm());
|
||||
|
||||
// shutting down
|
||||
ShuttingDown = true;
|
||||
|
||||
Logger.Info("Stopping scheduler...");
|
||||
Scheduler.Stop();
|
||||
|
||||
Logger.Info("Shutting down RPC client");
|
||||
DiscordRpc.Shutdown();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -293,5 +323,26 @@ namespace DML.Application.Classes
|
|||
Logger.Trace($"Trying to find channel in {server} by id: {id}");
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
59
Discord Media Loader.Application/Classes/RPC/DiscordRpc.cs
Normal file
59
Discord Media Loader.Application/Classes/RPC/DiscordRpc.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DML.Application.Classes.RPC
|
||||
{
|
||||
// https://github.com/discordapp/discord-rpc/blob/master/examples/button-clicker/Assets/DiscordRpc.cs
|
||||
// Give that man a cookie ^.^
|
||||
|
||||
public class DiscordRpc
|
||||
{
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void ReadyCallback();
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void DisconnectedCallback(int errorCode, string message);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void ErrorCallback(int errorCode, string message);
|
||||
|
||||
public struct EventHandlers
|
||||
{
|
||||
public ReadyCallback readyCallback;
|
||||
public DisconnectedCallback disconnectedCallback;
|
||||
public ErrorCallback errorCallback;
|
||||
}
|
||||
|
||||
// Values explanation and example: https://discordapp.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields
|
||||
[System.Serializable]
|
||||
public struct RichPresence
|
||||
{
|
||||
public string state; /* max 128 bytes */
|
||||
public string details; /* max 128 bytes */
|
||||
public long startTimestamp;
|
||||
public long endTimestamp;
|
||||
public string largeImageKey; /* max 32 bytes */
|
||||
public string largeImageText; /* max 128 bytes */
|
||||
public string smallImageKey; /* max 32 bytes */
|
||||
public string smallImageText; /* max 128 bytes */
|
||||
public string partyId; /* max 128 bytes */
|
||||
public int partySize;
|
||||
public int partyMax;
|
||||
public string matchSecret; /* max 128 bytes */
|
||||
public string joinSecret; /* max 128 bytes */
|
||||
public string spectateSecret; /* max 128 bytes */
|
||||
public bool instance;
|
||||
}
|
||||
|
||||
[DllImport(RpcWrapper.Dll, EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);
|
||||
|
||||
[DllImport(RpcWrapper.Dll, EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern void UpdatePresence(ref RichPresence presence);
|
||||
|
||||
[DllImport(RpcWrapper.Dll, EntryPoint = "Discord_RunCallbacks", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern void RunCallbacks();
|
||||
|
||||
[DllImport(RpcWrapper.Dll, EntryPoint = "Discord_Shutdown", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern void Shutdown();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DML.Application.Classes.RPC
|
||||
{
|
||||
public static class DiscordRpcHelper
|
||||
{
|
||||
public static long DateTimeToTimestamp(DateTime dt)
|
||||
{
|
||||
return (dt.Ticks - 621355968000000000) / 10000000;
|
||||
}
|
||||
}
|
||||
}
|
29
Discord Media Loader.Application/Classes/RPC/RpcWrapper.cs
Normal file
29
Discord Media Loader.Application/Classes/RPC/RpcWrapper.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DML.Application.Classes.RPC
|
||||
{
|
||||
public class RpcWrapper
|
||||
{
|
||||
public const string Dll = "discord-rpc-win32";
|
||||
|
||||
public RpcWrapper()
|
||||
{
|
||||
if (!File.Exists(Dll + ".dll"))
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Missing " + Dll + ".dll\n\n" +
|
||||
"Grab it from the release on GitHub or from the DiscordRpcDemo/lib/ folder in the repo then put it alongside DiscordRpcDemo.exe.\n\n" +
|
||||
"https://github.com/nostrenz/cshap-discord-rpc-demo"
|
||||
);
|
||||
|
||||
System.Windows.Forms.Application.Exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -161,6 +161,9 @@
|
|||
<Compile Include="Classes\Core.cs" />
|
||||
<Compile Include="Classes\Job.cs" />
|
||||
<Compile Include="Classes\JobScheduler.cs" />
|
||||
<Compile Include="Classes\RPC\DiscordRpc.cs" />
|
||||
<Compile Include="Classes\RPC\DiscordRpcHelper.cs" />
|
||||
<Compile Include="Classes\RPC\RpcWrapper.cs" />
|
||||
<Compile Include="Dialogs\LoginDialog.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
|
|
@ -8,6 +8,7 @@ using Discord;
|
|||
using Discord.WebSocket;
|
||||
using DML.AppCore.Classes;
|
||||
using DML.Application.Classes;
|
||||
using DML.Application.Classes.RPC;
|
||||
using DML.Client;
|
||||
using static SweetLib.Utils.Logger.Logger;
|
||||
|
||||
|
@ -23,6 +24,8 @@ namespace DML.Application
|
|||
public partial class MainForm : Form
|
||||
{
|
||||
private bool IsInitialized { get; set; } = false;
|
||||
private DiscordRpc.RichPresence Presence { get; }
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
@ -248,6 +251,15 @@ namespace DML.Application
|
|||
pgbProgress.Value = progress;
|
||||
|
||||
lbProgress.Text = $"Scanned: {scanned} Downloaded: {done} Open: {totalAttachments - done}";
|
||||
|
||||
Core.RpcPresence.details = "Downloading media files";
|
||||
Core.RpcPresence.state = $"{done} / {totalAttachments} ({pgbProgress.Value}%)";
|
||||
Core.RpcPresence.largeImageKey = "main";
|
||||
Core.RpcPresence.largeImageText = "Visit discordmedialoader.net";
|
||||
Core.RpcPresence.smallImageKey = "author";
|
||||
Core.RpcPresence.smallImageText = "Made by Serraniel";
|
||||
|
||||
Core.RpcUpdatePresence();
|
||||
}
|
||||
|
||||
private void aboutToolStripMenuItem_Click(object sender, System.EventArgs e)
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace Discord_Media_Loader
|
|||
|
||||
private async void FrmSplash_Shown(object sender, EventArgs e)
|
||||
{
|
||||
#if !DEBUG
|
||||
UseWaitCursor = true;
|
||||
try
|
||||
{
|
||||
|
@ -52,6 +53,7 @@ namespace Discord_Media_Loader
|
|||
{
|
||||
UseWaitCursor = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
DialogResult = DialogResult.OK;
|
||||
}
|
||||
|
|
BIN
Discord Media Loader/bin/Debug/discord-rpc-win32.dll
Normal file
BIN
Discord Media Loader/bin/Debug/discord-rpc-win32.dll
Normal file
Binary file not shown.
Loading…
Reference in a new issue