Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions DeltaTune/DeltaTune.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using DeltaTune.Discord;
using DeltaTune.Display;
using DeltaTune.Media;
using DeltaTune.Settings;
Expand All @@ -21,6 +22,7 @@ public class DeltaTune : Game
private ISettingsMenu settingsMenu;
private ISettingsService settingsService;
private ISettingsFile settingsFile;
private IDiscordService discordService;

private SpriteBatch spriteBatch;
private BitmapFont musicTitleFont;
Expand All @@ -46,7 +48,7 @@ protected override void Initialize()
{
settingsService = new SettingsService();
settingsFile = new SettingsFile(settingsService, Path.Combine(relativePathRoot, "Settings.json"));
settingsMenu = new SettingsMenu(settingsService);
settingsMenu = new SettingsMenu(settingsService, () => this.discordService);

mediaInfoService = new SystemMediaInfoService(new MediaFilter());

Expand Down Expand Up @@ -74,8 +76,11 @@ protected override void BeginRun()
windowService = new WindowService(this, graphicsDeviceManagerInstance, settingsMenu, settingsService, musicTitleFont.LineHeight);
windowService.InitializeWindow();

discordService = new DiscordService(settingsService);
discordService.UpdateState();

Vector2 WindowSizeProvider() => new Vector2(graphicsDeviceManagerInstance.GraphicsDevice.Viewport.Width, graphicsDeviceManagerInstance.GraphicsDevice.Viewport.Height);
displayService = new DisplayService(mediaInfoService, settingsService, new MediaFormatter(settingsService), musicTitleFont, WindowSizeProvider);
displayService = new DisplayService(mediaInfoService, settingsService, new MediaFormatter(settingsService), musicTitleFont, WindowSizeProvider, discordService);
displayService.BeginRun();

base.BeginRun();
Expand Down
1 change: 1 addition & 0 deletions DeltaTune/DeltaTune.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.6.1.70" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.26100.4654" />
<PackageReference Include="MonoGame.Framework.WindowsDX" Version="3.8.0.1641" />
Expand Down
51 changes: 51 additions & 0 deletions DeltaTune/Discord/DiscordService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using DeltaTune.Media;
using DeltaTune.Settings;
using DiscordRPC;

namespace DeltaTune.Discord
{
public class DiscordService : IDiscordService, IDisposable
{
private static readonly MediaInfo Default = new MediaInfo("", "", PlaybackStatus.Stopped);
private readonly ISettingsService settingsService;
private readonly DiscordRpcClient discord;

private MediaInfo lastMediaInfo = Default;

public DiscordService(ISettingsService settingsService)
{
this.settingsService = settingsService;
this.discord = new DiscordRpcClient("1446140892850290911");
this.discord.Initialize();
}

public void UpdateState()
{
if (!settingsService.EnableDiscordRichPresence.Value)
{
discord.ClearPresence();
}
else if (!lastMediaInfo.Equals(Default))
{
UpdateDisplay(lastMediaInfo);
}
}

public void UpdateDisplay(MediaInfo mediaInfo)
{
lastMediaInfo = mediaInfo;
if (!settingsService.EnableDiscordRichPresence.Value)
{
return;
}
discord.SetPresence(new RichPresence { State = mediaInfo.Artist, Details = mediaInfo.Title, Timestamps = Timestamps.Now, Assets = new Assets { LargeImageKey = "deltatune" }, Type = ActivityType.Listening, StatusDisplay = StatusDisplayType.Name });
}

public void Dispose()
{
discord.ClearPresence();
discord.Dispose();
}
}
}
10 changes: 10 additions & 0 deletions DeltaTune/Discord/IDiscordService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using DeltaTune.Media;

namespace DeltaTune.Discord
{
public interface IDiscordService
{
void UpdateState();
void UpdateDisplay(MediaInfo mediaInfo);
}
}
10 changes: 9 additions & 1 deletion DeltaTune/Display/DisplayService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using DeltaTune.Discord;
using DeltaTune.Media;
using DeltaTune.Settings;
using Microsoft.Xna.Framework;
Expand All @@ -16,20 +17,22 @@ public class DisplayService : IDisplayService
private readonly IMediaFormatter mediaFormatter;
private readonly Func<Vector2> windowSizeProvider;
private readonly BitmapFont musicTitleFont;
private readonly IDiscordService discordService;

private MediaInfo currentMediaInfo;
private IMusicTitleDisplay primaryDisplay;
private IMusicTitleDisplay secondaryDisplay;

private double lastMediaInfoUpdateTime;

public DisplayService(IMediaInfoService mediaInfoService, ISettingsService settingsService, IMediaFormatter mediaFormatter, BitmapFont musicTitleFont, Func<Vector2> windowSizeProvider)
public DisplayService(IMediaInfoService mediaInfoService, ISettingsService settingsService, IMediaFormatter mediaFormatter, BitmapFont musicTitleFont, Func<Vector2> windowSizeProvider, IDiscordService discordService)
{
this.mediaInfoService = mediaInfoService;
this.settingsService = settingsService;
this.musicTitleFont = musicTitleFont;
this.windowSizeProvider = windowSizeProvider;
this.mediaFormatter = mediaFormatter;
this.discordService = discordService;
}

public void BeginRun()
Expand All @@ -53,6 +56,11 @@ public void Update(GameTime gameTime)

bool shouldUpdateDisplayState = settingsService.ShowPlaybackStatus.Value ? titleChanged || artistChanged || statusChanged : titleChanged || artistChanged;

if (titleChanged || artistChanged)
{
discordService.UpdateDisplay(currentMediaInfo);
}

// Even if playback status shouldn't be shown, show the song title again when resuming playback
if (!shouldUpdateDisplayState && !settingsService.ShowPlaybackStatus.Value && statusChanged &&
currentMediaInfo.Status == PlaybackStatus.Playing)
Expand Down
1 change: 1 addition & 0 deletions DeltaTune/Settings/ISettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface ISettingsService
ReactiveProperty<bool> ShowPlaybackStatus { get; }
ReactiveProperty<float?> HideAutomatically { get; }
ReactiveProperty<bool> ScreenCaptureCompatibilityMode { get; }
ReactiveProperty<bool> EnableDiscordRichPresence { get; }
}
}
1 change: 1 addition & 0 deletions DeltaTune/Settings/SettingsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public SettingsFile(ISettingsService settingsService, string filePath)
settingsService.ShowPlaybackStatus.Subscribe(state => saveEvent.OnNext(Unit.Default)).AddTo(ref valueChangeDisposableBuilder);
settingsService.HideAutomatically.Subscribe(state => saveEvent.OnNext(Unit.Default)).AddTo(ref valueChangeDisposableBuilder);
settingsService.ScreenCaptureCompatibilityMode.Subscribe(state => saveEvent.OnNext(Unit.Default)).AddTo(ref valueChangeDisposableBuilder);
settingsService.EnableDiscordRichPresence.Subscribe(state => saveEvent.OnNext(Unit.Default)).AddTo(ref valueChangeDisposableBuilder);
valueChangeSubscription = valueChangeDisposableBuilder.Build();

saveSubscription = saveEvent.DebounceFrame(1).Subscribe(_ => Save());
Expand Down
3 changes: 3 additions & 0 deletions DeltaTune/Settings/SettingsFileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SettingsFileModel
public bool ShowPlaybackStatus { get; set; } = false;
public float? HideAutomatically { get; set; } = 2.5f;
public bool ScreenCaptureCompatibilityMode { get; set; } = false;
public bool EnableDiscordRichPresence { get; set; } = true;

public void FromSettings(ISettingsService settingsService)
{
Expand All @@ -21,6 +22,7 @@ public void FromSettings(ISettingsService settingsService)
ShowPlaybackStatus = settingsService.ShowPlaybackStatus.Value;
HideAutomatically = settingsService.HideAutomatically.Value;
ScreenCaptureCompatibilityMode = settingsService.ScreenCaptureCompatibilityMode.Value;
EnableDiscordRichPresence = settingsService.EnableDiscordRichPresence.Value;
}

public void ToSettings(ISettingsService settingsService)
Expand All @@ -32,6 +34,7 @@ public void ToSettings(ISettingsService settingsService)
settingsService.ShowPlaybackStatus.Value = ShowPlaybackStatus;
settingsService.HideAutomatically.Value = HideAutomatically;
settingsService.ScreenCaptureCompatibilityMode.Value = ScreenCaptureCompatibilityMode;
settingsService.EnableDiscordRichPresence.Value = EnableDiscordRichPresence;
}
}
}
15 changes: 14 additions & 1 deletion DeltaTune/Settings/SettingsMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using DeltaTune.Discord;
using DeltaTune.Window;
using SharpDX;

Expand All @@ -11,11 +13,13 @@ public class SettingsMenu : ISettingsMenu
private static readonly float[] hideAutomaticallyDelayOptions = new[] { 1f, 2.5f, 5f, 7.5f, 10f };

private readonly ISettingsService settingsService;
private readonly Func<IDiscordService> discord;
private ContextMenuStrip settingsMenuStrip;

public SettingsMenu(ISettingsService settingsService)
public SettingsMenu(ISettingsService settingsService, Func<IDiscordService> discord)
{
this.settingsService = settingsService;
this.discord = discord;
}

public ContextMenuStrip GetSettingsMenu()
Expand Down Expand Up @@ -173,6 +177,15 @@ private ToolStripMenuItem GetBehaviorMenuItem()
showPlaybackStatusItem.Checked = settingsService.ShowPlaybackStatus.Value;
showPlaybackStatusItem.Click += (sender, args) => settingsService.ShowPlaybackStatus.Value = !settingsService.ShowPlaybackStatus.Value;
behaviorItem.DropDownItems.Add(showPlaybackStatusItem);

ToolStripMenuItem enableDiscordItem = new ToolStripMenuItem();
enableDiscordItem.Text = "Enable Discord Rich Presence";
enableDiscordItem.Checked = settingsService.EnableDiscordRichPresence.Value;
enableDiscordItem.Click += (sender, args) => {
settingsService.EnableDiscordRichPresence.Value = !settingsService.EnableDiscordRichPresence.Value;
discord.Invoke()?.UpdateState();
};
behaviorItem.DropDownItems.Add(enableDiscordItem);

ToolStripMenuItem screenCaptureCompatItem = new ToolStripMenuItem();
screenCaptureCompatItem.Text = "Streamer Mode";
Expand Down
1 change: 1 addition & 0 deletions DeltaTune/Settings/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public class SettingsService : ISettingsService
public ReactiveProperty<bool> ShowPlaybackStatus { get; } = new ReactiveProperty<bool>(false);
public ReactiveProperty<float?> HideAutomatically { get; } = new ReactiveProperty<float?>(2.5f);
public ReactiveProperty<bool> ScreenCaptureCompatibilityMode { get; } = new ReactiveProperty<bool>(false);
public ReactiveProperty<bool> EnableDiscordRichPresence { get; } = new ReactiveProperty<bool>(true);
}
}