2024-02-22 02:46:19 +01:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Windows.Media;
|
|
|
|
|
using Windows.Media.Control;
|
|
|
|
|
using DiscordRPC;
|
|
|
|
|
using GlaxLogger;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using WindowsMediaController;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
|
|
|
|
|
|
|
|
|
namespace DiscordMediaRP;
|
|
|
|
|
|
|
|
|
|
//https://discord.com/developers/docs/rich-presence/how-to
|
|
|
|
|
//https://github.com/Lachee/discord-rpc-csharp?tab=readme-ov-file
|
|
|
|
|
//https://github.com/DubyaDude/WindowsMediaController
|
|
|
|
|
public class DisMediaRP : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger? _logger;
|
|
|
|
|
private readonly MediaManager _mediaManager = new();
|
|
|
|
|
private readonly DiscordRpcClient _discordRpcClient;
|
2024-02-22 03:55:10 +01:00
|
|
|
|
private RichPresence _currentStatus;
|
|
|
|
|
private bool _running = true;
|
|
|
|
|
|
|
|
|
|
private static RichPresence DefaultPresence(string largeImageKey)
|
2024-02-22 03:24:18 +01:00
|
|
|
|
{
|
2024-02-22 03:55:10 +01:00
|
|
|
|
return new RichPresence()
|
2024-02-22 03:24:18 +01:00
|
|
|
|
{
|
2024-02-22 03:55:10 +01:00
|
|
|
|
Details = "hewwo :3",
|
|
|
|
|
State = "https://github.com/C9Glax/DiscordMediaRichPresence",
|
|
|
|
|
Assets = new()
|
|
|
|
|
{
|
|
|
|
|
LargeImageKey = largeImageKey,
|
|
|
|
|
SmallImageKey = "music",
|
|
|
|
|
LargeImageText = "C9Glax/DiscordMediaRichPresence",
|
|
|
|
|
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
2024-02-22 03:55:10 +01:00
|
|
|
|
public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
|
2024-02-22 02:46:19 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 03:55:10 +01:00
|
|
|
|
public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat")
|
2024-02-22 02:46:19 +01:00
|
|
|
|
{
|
|
|
|
|
this._logger = logger;
|
2024-02-22 03:55:10 +01:00
|
|
|
|
this._currentStatus = DefaultPresence(largeImageKey);
|
2024-02-22 02:46:19 +01:00
|
|
|
|
this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger));
|
|
|
|
|
this._discordRpcClient.Initialize();
|
|
|
|
|
this._discordRpcClient.OnError += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogError("Discord RPC encountered an error:\n{args}", args);
|
|
|
|
|
this.Dispose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this._mediaManager.Start();
|
|
|
|
|
this._mediaManager.OnAnyMediaPropertyChanged += MediaPropertyChanged;
|
|
|
|
|
this._mediaManager.OnAnyPlaybackStateChanged += PlaybackStateChanged;
|
|
|
|
|
this._mediaManager.OnAnyTimelinePropertyChanged += TimelinePropertyChanged;
|
2024-02-22 03:55:10 +01:00
|
|
|
|
this._mediaManager.OnFocusedSessionChanged += mediaSession =>
|
|
|
|
|
{
|
|
|
|
|
if (mediaSession is null)
|
|
|
|
|
{
|
|
|
|
|
this._currentStatus = DefaultPresence(largeImageKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
};
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this._mediaManager.GetFocusedSession() is not null)
|
|
|
|
|
{
|
2024-02-22 03:46:19 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.MediaPropertyChanged(this._mediaManager.GetFocusedSession(),
|
|
|
|
|
this._mediaManager.GetFocusedSession().ControlSession.TryGetMediaPropertiesAsync().GetResults());
|
|
|
|
|
}
|
|
|
|
|
catch (System.Runtime.InteropServices.COMException e)
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogError("Could not fetch MediaProperties\n{e}", e);
|
|
|
|
|
}
|
2024-02-22 02:46:19 +01:00
|
|
|
|
this.PlaybackStateChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetPlaybackInfo());
|
|
|
|
|
this.TimelinePropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetTimelineProperties());
|
2024-02-22 03:55:10 +01:00
|
|
|
|
}else
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
|
|
|
|
while(_running)
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MediaPropertyChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties)
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaProperties));
|
|
|
|
|
|
|
|
|
|
string details = $"{mediaProperties.Title}";
|
|
|
|
|
if (mediaProperties.Artist.Length > 0)
|
|
|
|
|
details += $" - {mediaProperties.Artist}";
|
|
|
|
|
if (mediaProperties.AlbumTitle.Length > 0)
|
|
|
|
|
details += $" - Album: {mediaProperties.AlbumTitle}";
|
|
|
|
|
this._currentStatus.Details = details;
|
|
|
|
|
|
2024-02-22 03:55:10 +01:00
|
|
|
|
this.PlaybackStateChanged(mediaSession, mediaSession.ControlSession.GetPlaybackInfo());
|
2024-02-22 02:46:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaybackStateChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(playbackInfo));
|
|
|
|
|
|
2024-02-22 03:24:18 +01:00
|
|
|
|
string playbackState = playbackInfo.PlaybackStatus switch
|
2024-02-22 02:46:19 +01:00
|
|
|
|
{
|
2024-02-22 03:24:18 +01:00
|
|
|
|
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused => "pause",
|
|
|
|
|
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing => "play",
|
|
|
|
|
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Stopped => "stop",
|
|
|
|
|
_ => "music"
|
2024-02-22 02:46:19 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string? repeatMode = playbackInfo.AutoRepeatMode switch
|
|
|
|
|
{
|
|
|
|
|
MediaPlaybackAutoRepeatMode.Track => "\ud83d\udd02",
|
|
|
|
|
MediaPlaybackAutoRepeatMode.List => "\ud83d\udd01",
|
|
|
|
|
_ => null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string? shuffle = (playbackInfo.IsShuffleActive ?? false) ? "\ud83d\udd00" : null;
|
|
|
|
|
|
2024-02-22 03:24:18 +01:00
|
|
|
|
this._currentStatus.State = string.Join(' ', repeatMode, shuffle);
|
|
|
|
|
this._currentStatus.Assets.SmallImageKey = playbackState;
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimelinePropertyChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionTimelineProperties timelineProperties)
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(timelineProperties));
|
|
|
|
|
|
|
|
|
|
if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch)
|
|
|
|
|
return;
|
2024-02-22 05:51:14 +01:00
|
|
|
|
|
|
|
|
|
GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo =
|
|
|
|
|
mediaSession.ControlSession.GetPlaybackInfo();
|
|
|
|
|
|
|
|
|
|
string? repeatMode = playbackInfo.AutoRepeatMode switch
|
|
|
|
|
{
|
|
|
|
|
MediaPlaybackAutoRepeatMode.Track => "\ud83d\udd02",
|
|
|
|
|
MediaPlaybackAutoRepeatMode.List => "\ud83d\udd01",
|
|
|
|
|
_ => null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string? shuffle = (playbackInfo.IsShuffleActive ?? false) ? "\ud83d\udd00" : null;
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
2024-02-22 05:52:55 +01:00
|
|
|
|
this._currentStatus.State = string.Join(' ', repeatMode, shuffle, $"{timelineProperties.Position:hh\\:mm\\:ss}/{timelineProperties.EndTime:hh\\:mm\\:ss}");
|
2024-02-22 02:46:19 +01:00
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_mediaManager.Dispose();
|
|
|
|
|
_discordRpcClient.Dispose();
|
|
|
|
|
_running = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string? ObjectToString(object? obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj is null)
|
|
|
|
|
return null;
|
|
|
|
|
string? ns = obj.GetType().Namespace;
|
|
|
|
|
if (ns == "System.Collections.Generic")
|
|
|
|
|
{
|
|
|
|
|
IReadOnlyCollection<string> i = (IReadOnlyCollection<string>) obj;
|
|
|
|
|
return string.Join(", ", i);
|
|
|
|
|
}
|
|
|
|
|
if (ns is null || (ns != "Windows.Media.Control" && ns != "WindowsMediaController"))
|
|
|
|
|
return obj.ToString();
|
|
|
|
|
|
|
|
|
|
FieldInfo[] fieldInfos = obj.GetType().GetFields();
|
|
|
|
|
PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
|
|
|
|
|
return
|
|
|
|
|
$"{obj.GetType().FullName}\n" +
|
|
|
|
|
$"Fields:\n\t" +
|
|
|
|
|
$"{string.Join("\n\t", fieldInfos.Select(f => $"{f.Name} {f.GetValue(obj)}"))}\n" +
|
|
|
|
|
$"Properties:\n\t" +
|
|
|
|
|
$"{string.Join("\n\t", propertyInfos.Select(p => $"{p.Name} {ObjectToString(p.GetValue(obj))}"))}";
|
|
|
|
|
}
|
|
|
|
|
}
|