|
|
|
@ -18,24 +18,33 @@ public class DisMediaRP : IDisposable
|
|
|
|
|
private readonly ILogger? _logger;
|
|
|
|
|
private readonly MediaManager _mediaManager = new();
|
|
|
|
|
private readonly DiscordRpcClient _discordRpcClient;
|
|
|
|
|
private readonly RichPresence _currentStatus = new()
|
|
|
|
|
{
|
|
|
|
|
Assets = new()
|
|
|
|
|
{
|
|
|
|
|
LargeImageText = "C9Glax/DiscordMediaRichPresence",
|
|
|
|
|
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
private RichPresence _currentStatus;
|
|
|
|
|
private bool _running = true;
|
|
|
|
|
|
|
|
|
|
public DisMediaRP(string applicationId, LogLevel? logLevel, string? largeImageKey = null) : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
|
|
|
|
|
private static RichPresence DefaultPresence(string largeImageKey)
|
|
|
|
|
{
|
|
|
|
|
return new RichPresence()
|
|
|
|
|
{
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisMediaRP(string applicationId, ILogger? logger = null, string? largeImageKey = null)
|
|
|
|
|
public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat")
|
|
|
|
|
{
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
this._currentStatus.Assets.LargeImageKey = largeImageKey ?? "cat";
|
|
|
|
|
this._currentStatus = DefaultPresence(largeImageKey);
|
|
|
|
|
this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger));
|
|
|
|
|
this._discordRpcClient.Initialize();
|
|
|
|
|
this._discordRpcClient.OnError += (sender, args) =>
|
|
|
|
@ -48,14 +57,32 @@ public class DisMediaRP : IDisposable
|
|
|
|
|
this._mediaManager.OnAnyMediaPropertyChanged += MediaPropertyChanged;
|
|
|
|
|
this._mediaManager.OnAnyPlaybackStateChanged += PlaybackStateChanged;
|
|
|
|
|
this._mediaManager.OnAnyTimelinePropertyChanged += TimelinePropertyChanged;
|
|
|
|
|
this._mediaManager.OnFocusedSessionChanged += mediaSession =>
|
|
|
|
|
{
|
|
|
|
|
if (mediaSession is null)
|
|
|
|
|
{
|
|
|
|
|
this._currentStatus = DefaultPresence(largeImageKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this._mediaManager.GetFocusedSession() is not null)
|
|
|
|
|
{
|
|
|
|
|
this.MediaPropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.TryGetMediaPropertiesAsync().GetResults());
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
this.PlaybackStateChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetPlaybackInfo());
|
|
|
|
|
this.TimelinePropertyChanged(this._mediaManager.GetFocusedSession(), this._mediaManager.GetFocusedSession().ControlSession.GetTimelineProperties());
|
|
|
|
|
}
|
|
|
|
|
}else
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
|
|
|
|
|
while(_running)
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
@ -65,9 +92,6 @@ public class DisMediaRP : IDisposable
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaProperties));
|
|
|
|
|
if (mediaSession != this._mediaManager.GetFocusedSession())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string details = $"{mediaProperties.Title}";
|
|
|
|
|
if (mediaProperties.Artist.Length > 0)
|
|
|
|
@ -76,16 +100,13 @@ public class DisMediaRP : IDisposable
|
|
|
|
|
details += $" - Album: {mediaProperties.AlbumTitle}";
|
|
|
|
|
this._currentStatus.Details = details;
|
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
this.PlaybackStateChanged(mediaSession, mediaSession.ControlSession.GetPlaybackInfo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaybackStateChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(playbackInfo));
|
|
|
|
|
if (mediaSession != this._mediaManager.GetFocusedSession())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string playbackState = playbackInfo.PlaybackStatus switch
|
|
|
|
|
{
|
|
|
|
@ -114,14 +135,32 @@ public class DisMediaRP : IDisposable
|
|
|
|
|
{
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(mediaSession));
|
|
|
|
|
this._logger?.LogDebug(ObjectToString(timelineProperties));
|
|
|
|
|
if (mediaSession != this._mediaManager.GetFocusedSession())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this._currentStatus.Timestamps = new Timestamps(DateTime.Now.Subtract(timelineProperties.Position),
|
|
|
|
|
DateTime.Now.Add(timelineProperties.EndTime - timelineProperties.Position));
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
this._currentStatus.State = string.Join(' ', repeatMode, shuffle, $"{timelineProperties.Position:hh\\:mm\\:ss}/{timelineProperties.EndTime:hh\\:mm\\:ss}");
|
|
|
|
|
|
|
|
|
|
if (mediaSession.ControlSession.GetPlaybackInfo().PlaybackStatus is
|
|
|
|
|
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
|
|
|
|
|
this._currentStatus.Timestamps = new Timestamps()
|
|
|
|
|
{
|
|
|
|
|
End = DateTime.UtcNow.Add(timelineProperties.EndTime - timelineProperties.Position)
|
|
|
|
|
};
|
|
|
|
|
else
|
|
|
|
|
this._currentStatus.Timestamps = new Timestamps();
|
|
|
|
|
|
|
|
|
|
this._discordRpcClient.SetPresence(this._currentStatus);
|
|
|
|
|
}
|
|
|
|
|