From 4ce20fff515363a4e985f81dd40cc9c3bbb8dc77 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 22 Feb 2024 17:25:13 +0100 Subject: [PATCH] Added Config File Added Ignore Webpages (firefox) Added Ignore Spotify --- DiscordMediaRP/Config.cs | 27 ++++++++++++++ DiscordMediaRP/DisMediaRP.cs | 53 +++++++++++++++++++++------- DiscordMediaRP/DiscordMediaRP.csproj | 1 + DiscordMediaRP/Program.cs | 41 ++++++++++++--------- 4 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 DiscordMediaRP/Config.cs diff --git a/DiscordMediaRP/Config.cs b/DiscordMediaRP/Config.cs new file mode 100644 index 0000000..a1dae90 --- /dev/null +++ b/DiscordMediaRP/Config.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Logging; + +namespace DiscordMediaRP; + +public struct Config +{ + public required string DiscordKey; + public LogLevel? LogLevel; + public string? LargeImageKey; + public bool? UseSpotify; + public string[]? WebbrowserIgnoreSites; + + public Config WithDiscordKey(string key) + { + return this with { DiscordKey = key }; + } + + public Config WithLargeImageKey(string key) + { + return this with { LargeImageKey = key }; + } + + public Config WithLogLevel(LogLevel level) + { + return this with { LogLevel = level }; + } +} \ No newline at end of file diff --git a/DiscordMediaRP/DisMediaRP.cs b/DiscordMediaRP/DisMediaRP.cs index eabeddc..db4a269 100644 --- a/DiscordMediaRP/DisMediaRP.cs +++ b/DiscordMediaRP/DisMediaRP.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Diagnostics; +using System.Reflection; using Windows.Media; using Windows.Media.Control; using DiscordRPC; @@ -20,8 +21,9 @@ public class DisMediaRP : IDisposable private readonly DiscordRpcClient _discordRpcClient; private RichPresence _currentStatus; private bool _running = true; + private Config config; - private static RichPresence DefaultPresence(string largeImageKey) + private static RichPresence DefaultPresence(string? largeImageKey) { return new RichPresence() { @@ -29,7 +31,7 @@ public class DisMediaRP : IDisposable State = "https://github.com/C9Glax/DiscordMediaRichPresence", Assets = new() { - LargeImageKey = largeImageKey, + LargeImageKey = largeImageKey ?? "", SmallImageKey = "music", LargeImageText = "C9Glax/DiscordMediaRichPresence", SmallImageText = "https://www.flaticon.com/de/autoren/alfanz" @@ -37,15 +39,12 @@ public class DisMediaRP : IDisposable }; } - public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey) + public DisMediaRP(Config config) { - } - - public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat") - { - this._logger = logger; - this._currentStatus = DefaultPresence(largeImageKey); - this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger)); + this.config = config; + this._logger = new Logger(config.LogLevel ?? LogLevel.Information); + this._currentStatus = DefaultPresence(config.LargeImageKey); + this._discordRpcClient = new DiscordRpcClient(config.DiscordKey, logger: new DisLogger(this._logger)); this._discordRpcClient.Initialize(); this._discordRpcClient.OnError += (sender, args) => { @@ -61,7 +60,7 @@ public class DisMediaRP : IDisposable { if (mediaSession is null) { - this._currentStatus = DefaultPresence(largeImageKey); + this._currentStatus = DefaultPresence(config.LargeImageKey); } this._discordRpcClient.SetPresence(this._currentStatus); @@ -93,6 +92,9 @@ public class DisMediaRP : IDisposable this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(mediaProperties)); + if (!UseMediaSession(mediaSession)) + return; + string details = $"{mediaProperties.Title}"; if (mediaProperties.Artist.Length > 0) details += $" - {mediaProperties.Artist}"; @@ -108,6 +110,9 @@ public class DisMediaRP : IDisposable this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(playbackInfo)); + if (!UseMediaSession(mediaSession)) + return; + string playbackState = playbackInfo.PlaybackStatus switch { GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused => "pause", @@ -136,6 +141,9 @@ public class DisMediaRP : IDisposable this._logger?.LogDebug(ObjectToString(mediaSession)); this._logger?.LogDebug(ObjectToString(timelineProperties)); + if (!UseMediaSession(mediaSession)) + return; + if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch) return; @@ -165,6 +173,27 @@ public class DisMediaRP : IDisposable this._discordRpcClient.SetPresence(this._currentStatus); } + private bool UseMediaSession(MediaManager.MediaSession mediaSession) + { + string processId = mediaSession.ControlSession.SourceAppUserModelId; + if (processId == "spotify.exe") + return config.UseSpotify ?? true; + + if (processId == "firefox.exe") + { + string[] windowNames = Process.GetProcesses().Where(proc => processId.Contains(proc.ProcessName, StringComparison.InvariantCultureIgnoreCase)).Select(proc => proc.MainWindowTitle).ToArray(); + return !windowNames.Any(name => + { + foreach (string site in config.WebbrowserIgnoreSites ?? Array.Empty()) + if (name.Contains(site, StringComparison.InvariantCultureIgnoreCase)) + return true; + return false; + }); + } + + return true; + } + public void Dispose() { _mediaManager.Dispose(); diff --git a/DiscordMediaRP/DiscordMediaRP.csproj b/DiscordMediaRP/DiscordMediaRP.csproj index 23f2207..e11f693 100644 --- a/DiscordMediaRP/DiscordMediaRP.csproj +++ b/DiscordMediaRP/DiscordMediaRP.csproj @@ -11,6 +11,7 @@ + diff --git a/DiscordMediaRP/Program.cs b/DiscordMediaRP/Program.cs index 428d0cf..04a91f1 100644 --- a/DiscordMediaRP/Program.cs +++ b/DiscordMediaRP/Program.cs @@ -2,31 +2,40 @@ using DiscordMediaRP; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; + +Config? c = null; +if (File.Exists("config.json")) + c = JsonConvert.DeserializeObject(File.ReadAllText("config.json")); + +int discordKeyIndex = Array.IndexOf(args, "-d"); +if (discordKeyIndex > -1) + if (discordKeyIndex + 1 < args.Length) + if (c is null) + c = new Config() + { + DiscordKey = args[discordKeyIndex + 1] + }; + else + c = c.Value.WithDiscordKey(args[discordKeyIndex + 1]); + else + throw new IndexOutOfRangeException("No Discord ApplicationKey provided"); +else if(c is null) + throw new ArgumentNullException(nameof(Config.DiscordKey)); + int loglevelIndex = Array.IndexOf(args, "-l"); -LogLevel? level = null; if(loglevelIndex > -1) - if(loglevelIndex + 1 < args.Length) - level = loglevelIndex < args.Length ? Enum.Parse(args[loglevelIndex + 1]) : null; + if (loglevelIndex + 1 < args.Length) + c = c.Value.WithLogLevel(Enum.Parse(args[loglevelIndex + 1])); else throw new IndexOutOfRangeException(nameof(loglevelIndex)); -int discordKeyIndex = Array.IndexOf(args, "-d"); -string discordKey; -if (discordKeyIndex > -1) - if(discordKeyIndex + 1 < args.Length) - discordKey = args[discordKeyIndex + 1]; - else - throw new IndexOutOfRangeException("No Discord ApplicationKey provided"); -else - throw new ArgumentNullException(nameof(discordKey)); - int imageKeyIndex = Array.IndexOf(args, "-i"); -string imageKey = "cat"; if(imageKeyIndex > -1) if (imageKeyIndex + 1 < args.Length) - imageKey = args[imageKeyIndex + 1]; + c = c.Value.WithLargeImageKey(args[imageKeyIndex + 1]); else throw new IndexOutOfRangeException(nameof(imageKeyIndex)); -DisMediaRP _ = new (discordKey, level, imageKey); \ No newline at end of file +DisMediaRP _ = new (c.Value); \ No newline at end of file