Skip to content

Commit

Permalink
- audio bug fix
Browse files Browse the repository at this point in the history
- background service cleanup
- minor cleanup
  • Loading branch information
pjmagee committed Jan 11, 2025
1 parent 2222600 commit bc8b578
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 85 deletions.
9 changes: 4 additions & 5 deletions src/Dota2Helper/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
Expand All @@ -19,7 +18,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using static Avalonia.Controls.Design;

namespace Dota2Helper;
Expand Down Expand Up @@ -77,8 +75,8 @@ public override async void OnFrameworkInitializationCompleted()
.AddSingleton<GsiConfigWatcher>()
.AddSingleton<GsiConfigService>()
.AddSingleton<ProfileService>()
.AddSingleton<AudioService>()
.AddSingleton<TimerAudioService>()
// .AddSingleton<AudioService>()
//.AddSingleton<TimerAudioService>()
.AddSingleton<SettingsWindow>()
.AddSingleton<LocalListener>()
.AddSingleton<SplashScreenViewModel>()
Expand All @@ -88,6 +86,7 @@ public override async void OnFrameworkInitializationCompleted()
services
.AddHostedService(sp => sp.GetRequiredService<GameTimeProvider>())
.AddHostedService(sp => sp.GetRequiredService<LocalListener>())
.AddHostedService(sp => (AudioService) sp.GetRequiredKeyedService<IAudioService>(nameof(AudioService)))
.AddHostedService(sp => sp.GetRequiredService<DemoTimeProvider>());
}
);
Expand All @@ -112,7 +111,7 @@ await Task.WhenAny(
DataContext = ServiceProvider.GetRequiredService<TimersViewModel>(),
};

var task = _host.RunAsync();
_ = _host.RunAsync();

desktop.MainWindow.Show();
splash.Close();
Expand Down
5 changes: 3 additions & 2 deletions src/Dota2Helper/Controls/AboutTabContent.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:SettingsViewModel"
x:Class="Dota2Helper.Controls.AboutTabContent" Margin="5">
<StackPanel Orientation="Vertical" Spacing="5">
<StackPanel Orientation="Vertical" Spacing="5" Margin="0,0,0,200">
<TextBlock>Dota2 Helper is created by Patrick Magee</TextBlock>
<TextBlock>Third party packages and licenses</TextBlock>
<DataGrid ItemsSource="{Binding PackageItems}"
IsReadOnly="True" Margin="5"
GridLinesVisibility="All"
HorizontalAlignment="Left"
MaxHeight="450" BorderThickness="1"
MaxHeight="500" BorderThickness="1"
Height="400"
MaxColumnWidth="500"
CanUserResizeColumns="True"
HorizontalScrollBarVisibility="Visible"
Expand Down
2 changes: 0 additions & 2 deletions src/Dota2Helper/Controls/AboutTabContent.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Dota2Helper.Controls;

Expand Down
2 changes: 0 additions & 2 deletions src/Dota2Helper/Controls/GuideTabContent.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Dota2Helper.Controls;

Expand Down
2 changes: 0 additions & 2 deletions src/Dota2Helper/Controls/IntegrationTabContent.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Dota2Helper.Controls;

Expand Down
2 changes: 0 additions & 2 deletions src/Dota2Helper/Controls/ProfilesTabContent.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Dota2Helper.Controls;

Expand Down
1 change: 0 additions & 1 deletion src/Dota2Helper/Design/DesignProfileService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Dota2Helper.Features.Audio;
using Dota2Helper.Features.Settings;
using Dota2Helper.Features.Timers;

namespace Dota2Helper.Design;
Expand Down
11 changes: 11 additions & 0 deletions src/Dota2Helper/Design/DesignSplashScreenViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Dota2Helper.ViewModels;

namespace Dota2Helper.Design;

public class DesignSplashScreenViewModel : SplashScreenViewModel
{
public DesignSplashScreenViewModel() : base(null!, null!)
{
StatusText = "GSI installed successfully";
}
}
3 changes: 0 additions & 3 deletions src/Dota2Helper/Features/About/AboutItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using Dota2Helper.Features.Settings;

namespace Dota2Helper.Features.About;

Expand Down
3 changes: 2 additions & 1 deletion src/Dota2Helper/Features/About/AboutTableData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;

namespace Dota2Helper.Features.About;

Expand All @@ -22,7 +23,7 @@ IEnumerable<PackageItem> GetPackageItems()
{
var dir = Directory.GetCurrentDirectory();
var json = File.ReadAllText(Path.Combine(dir, "packages.json"));
return System.Text.Json.JsonSerializer.Deserialize<List<PackageItem>>(json)!;
return JsonSerializer.Deserialize<List<PackageItem>>(json)!;
}
catch(Exception e)
{
Expand Down
51 changes: 30 additions & 21 deletions src/Dota2Helper/Features/Audio/AudioService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dota2Helper.Features.Settings;
using Microsoft.Extensions.Hosting;
using NAudio.Wave;

namespace Dota2Helper.Features.Audio;

public class AudioService : BackgroundWorker, IAudioService, IDisposable
public class AudioService(SettingsService settingsService) : BackgroundService, IAudioService
{
readonly SettingsService _settingsService;
readonly WaveOutEvent _mediaPlayer;

ConcurrentQueue<string> AudioQueue { get; } = new();

public AudioService(SettingsService settingsService)
{
_settingsService = settingsService;
_mediaPlayer = new WaveOutEvent();
RunWorkerAsync();
}
private readonly WaveOutEvent _mediaPlayer = new();
private ConcurrentQueue<string> AudioQueue { get; } = new();

public void Play(string audioFile)
{
Expand All @@ -31,28 +23,45 @@ public void Play(string audioFile)
}
}

protected override async void OnDoWork(DoWorkEventArgs e)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!CancellationPending)
while (!stoppingToken.IsCancellationRequested)
{
while (AudioQueue.TryDequeue(out var audioFile))
{
try
{
using (var audioFileReader = new AudioFileReader(audioFile))
var audioFileReader = new AudioFileReader(audioFile);

try
{
_mediaPlayer.Init(audioFileReader);
_mediaPlayer.Volume = (float)(_settingsService.Settings.Volume / 100.0);
_mediaPlayer.Volume = (float)(settingsService.Settings.Volume / 100.0);
_mediaPlayer.Play();

while (_mediaPlayer.PlaybackState == PlaybackState.Playing)
{
await Task.Delay(100, stoppingToken);
}
}
finally
{
await audioFileReader.DisposeAsync();
}
}
catch (Exception)
catch (Exception e)
{

Debug.WriteLine(e.Message);
}
}

await Task.Delay(1000);
await Task.Delay(1000, stoppingToken);
}
}

public override void Dispose()
{
_mediaPlayer?.Dispose();
base.Dispose();
}
}
17 changes: 7 additions & 10 deletions src/Dota2Helper/Features/Audio/TimerAudioService.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using Dota2Helper.Features.Settings;
using Dota2Helper.Features.TimeProvider;
using Microsoft.Extensions.DependencyInjection;

namespace Dota2Helper.Features.Audio;

public class TimerAudioService(
ITimeProvider timeProvider,
SettingsService settingsService,
AudioService audioService) : IAudioService
public class TimerAudioService(ITimeProvider timeProvider, SettingsService settingsService, [FromKeyedServices(nameof(AudioService))] IAudioService audioService) : IAudioService
{
public void Play(string audioFile)
{
if (timeProvider.ProviderType == ProviderType.Real)
switch (timeProvider.ProviderType)
{
audioService.Play(audioFile);
}
else if (timeProvider.ProviderType == ProviderType.Demo && !settingsService.Settings.DemoMuted)
{
audioService.Play(audioFile);
case ProviderType.Real:
case ProviderType.Demo when !settingsService.Settings.DemoMuted:
audioService.Play(audioFile);
break;
}
}
}
6 changes: 3 additions & 3 deletions src/Dota2Helper/Features/Http/LocalListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.Json;
Expand Down Expand Up @@ -113,7 +113,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
Debug.WriteLine(e.Message);
}

await Task.Delay(100, stoppingToken);
Expand All @@ -131,7 +131,7 @@ public override void Dispose()
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
Debug.WriteLine(e.Message);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Dota2Helper/Features/Settings/Profile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Dota2Helper.Features.Timers;

namespace Dota2Helper.Features.Settings;

Expand Down
1 change: 0 additions & 1 deletion src/Dota2Helper/Features/Settings/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Dota2Helper.Features.Timers;
Expand Down
1 change: 0 additions & 1 deletion src/Dota2Helper/Features/Settings/SettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Dota2Helper.Features.Settings;
Expand Down
4 changes: 3 additions & 1 deletion src/Dota2Helper/Features/TimeProvider/GameTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GameTimeProvider(SettingsService settingsService, RealGameTimeProvi

Process? _dota2;
ITimeProvider? _current;
private readonly SemaphoreSlim _signal = new(0);

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Expand Down Expand Up @@ -47,12 +48,13 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_dota2 = null;
_current = demoTimeProvider;
_signal.Release();
};
}
}
}

await Task.Delay(2000, stoppingToken);
await _signal.WaitAsync(2000, stoppingToken);
}
}
}
1 change: 0 additions & 1 deletion src/Dota2Helper/Features/Timers/ProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Dota2Helper.Features.Audio;
using Dota2Helper.Features.Settings;
using Dota2Helper.ViewModels;

Expand Down
3 changes: 0 additions & 3 deletions src/Dota2Helper/ViewModels/DotaTimerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;
using Dota2Helper.Features.Audio;
using Dota2Helper.Features.Settings;
using Dota2Helper.Features.Timers;
using Microsoft.Extensions.DependencyInjection;

namespace Dota2Helper.ViewModels;

Expand Down
8 changes: 2 additions & 6 deletions src/Dota2Helper/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.ComponentModel;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using Dota2Helper.Features;
using Dota2Helper.Features.About;
using Dota2Helper.Features.Audio;
using Dota2Helper.Features.Gsi;
using Dota2Helper.Features.Http;
using Dota2Helper.Features.Settings;
using Dota2Helper.Features.Timers;
using Dota2Helper.Views;
using Microsoft.Extensions.DependencyInjection;

namespace Dota2Helper.ViewModels;
Expand Down
10 changes: 0 additions & 10 deletions src/Dota2Helper/ViewModels/SplashScreenViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Metadata;
using CommunityToolkit.Mvvm.Input;
using Dota2Helper.Features.Gsi;
using Dota2Helper.Features.Http;

namespace Dota2Helper.ViewModels;

public class SplashScreenDesignViewModel : SplashScreenViewModel
{
public SplashScreenDesignViewModel() : base(null!, null!)
{
StatusText = "GSI installed successfully";
}
}

public class SplashScreenViewModel : ViewModelBase
{
Expand Down
Loading

0 comments on commit bc8b578

Please sign in to comment.