1
0

Compare commits

..

No commits in common. "e62e6a4af0eb32c2f147b760c253f1c539b138a8" and "de1a99c501ae485b08aac4aa7c1b14cbc35aa8f8" have entirely different histories.

View File

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