mirror of
https://github.com/BubbaGumpShrump/AutoTrackR2.git
synced 2025-05-17 14:45:30 +00:00
Fix crash issue for multiple kills at once.
This commit is contained in:
parent
5f2f429dcc
commit
b6508593db
2 changed files with 46 additions and 19 deletions
AutoTrackR2
|
@ -97,7 +97,7 @@ public class KillHistoryManager
|
||||||
// Check file can be written to
|
// Check file can be written to
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var fileStream = new FileStream(_killHistoryPath, FileMode.Append, FileAccess.Write, FileShare.None);
|
using var fileStream = new FileStream(_killHistoryPath, FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||||
using var writer = new StreamWriter(fileStream);
|
using var writer = new StreamWriter(fileStream);
|
||||||
writer.Write(csv.ToString());
|
writer.Write(csv.ToString());
|
||||||
|
|
||||||
|
@ -123,7 +123,7 @@ public class KillHistoryManager
|
||||||
{
|
{
|
||||||
var kills = new List<KillData>();
|
var kills = new List<KillData>();
|
||||||
|
|
||||||
using var reader = new StreamReader(_killHistoryPath);
|
using var reader = new StreamReader(new FileStream(_killHistoryPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
|
||||||
reader.ReadLine(); // Skip headers
|
reader.ReadLine(); // Skip headers
|
||||||
|
|
||||||
while (reader.Peek() >= 0)
|
while (reader.Peek() >= 0)
|
||||||
|
|
|
@ -16,6 +16,7 @@ public class KillStreakManager : IDisposable
|
||||||
private WaveOutEvent? _waveOut;
|
private WaveOutEvent? _waveOut;
|
||||||
private bool _isPlaying = false;
|
private bool _isPlaying = false;
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
|
private Task? _currentPlaybackTask;
|
||||||
|
|
||||||
public KillStreakManager(string soundsPath)
|
public KillStreakManager(string soundsPath)
|
||||||
{
|
{
|
||||||
|
@ -82,7 +83,7 @@ public class KillStreakManager : IDisposable
|
||||||
// Only start playing if not already playing
|
// Only start playing if not already playing
|
||||||
if (!_isPlaying && _soundQueue.Count > 0)
|
if (!_isPlaying && _soundQueue.Count > 0)
|
||||||
{
|
{
|
||||||
PlayNextSound();
|
_currentPlaybackTask = Task.Run(() => PlayNextSound());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +117,14 @@ public class KillStreakManager : IDisposable
|
||||||
{
|
{
|
||||||
if (_soundQueue.Count == 0 || _disposed) return;
|
if (_soundQueue.Count == 0 || _disposed) return;
|
||||||
|
|
||||||
string soundPath = _soundQueue.Dequeue();
|
string soundPath;
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (_soundQueue.Count == 0 || _disposed) return;
|
||||||
|
soundPath = _soundQueue.Dequeue();
|
||||||
|
_isPlaying = true;
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Attempting to play sound: {soundPath}");
|
Console.WriteLine($"Attempting to play sound: {soundPath}");
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -124,28 +132,34 @@ public class KillStreakManager : IDisposable
|
||||||
if (!File.Exists(soundPath))
|
if (!File.Exists(soundPath))
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Sound file not found: {soundPath}");
|
Console.WriteLine($"Sound file not found: {soundPath}");
|
||||||
_isPlaying = false;
|
lock (_lock)
|
||||||
if (_soundQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
PlayNextSound();
|
_isPlaying = false;
|
||||||
|
if (_soundQueue.Count > 0)
|
||||||
|
{
|
||||||
|
_currentPlaybackTask = Task.Run(() => PlayNextSound());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop any currently playing sound
|
// Stop any currently playing sound
|
||||||
_waveOut?.Stop();
|
lock (_lock)
|
||||||
_waveOut?.Dispose();
|
{
|
||||||
_waveOut = null;
|
_waveOut?.Stop();
|
||||||
|
_waveOut?.Dispose();
|
||||||
|
_waveOut = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new WaveOutEvent
|
// Create a new WaveOutEvent
|
||||||
_waveOut = new WaveOutEvent();
|
var waveOut = new WaveOutEvent();
|
||||||
|
|
||||||
// Create a new AudioFileReader for the MP3 file
|
// Create a new AudioFileReader for the MP3 file
|
||||||
using var audioFile = new AudioFileReader(soundPath);
|
using var audioFile = new AudioFileReader(soundPath);
|
||||||
_waveOut.Init(audioFile);
|
waveOut.Init(audioFile);
|
||||||
|
|
||||||
// Set up event handler for when playback finishes
|
// Set up event handler for when playback finishes
|
||||||
_waveOut.PlaybackStopped += (sender, e) =>
|
waveOut.PlaybackStopped += (sender, e) =>
|
||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
|
@ -154,23 +168,35 @@ public class KillStreakManager : IDisposable
|
||||||
_isPlaying = false;
|
_isPlaying = false;
|
||||||
if (_soundQueue.Count > 0)
|
if (_soundQueue.Count > 0)
|
||||||
{
|
{
|
||||||
PlayNextSound();
|
_currentPlaybackTask = Task.Run(() => PlayNextSound());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_isPlaying = true;
|
lock (_lock)
|
||||||
_waveOut.Play();
|
{
|
||||||
|
if (_disposed)
|
||||||
|
{
|
||||||
|
waveOut.Dispose();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_waveOut = waveOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
waveOut.Play();
|
||||||
|
|
||||||
Console.WriteLine($"Successfully played sound: {soundPath}");
|
Console.WriteLine($"Successfully played sound: {soundPath}");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Error playing sound {soundPath}: {ex.Message}");
|
Console.WriteLine($"Error playing sound {soundPath}: {ex.Message}");
|
||||||
_isPlaying = false;
|
lock (_lock)
|
||||||
if (_soundQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
PlayNextSound();
|
_isPlaying = false;
|
||||||
|
if (_soundQueue.Count > 0)
|
||||||
|
{
|
||||||
|
_currentPlaybackTask = Task.Run(() => PlayNextSound());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -195,6 +221,7 @@ public class KillStreakManager : IDisposable
|
||||||
_waveOut?.Stop();
|
_waveOut?.Stop();
|
||||||
_waveOut?.Dispose();
|
_waveOut?.Dispose();
|
||||||
_waveOut = null;
|
_waveOut = null;
|
||||||
|
_currentPlaybackTask?.Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue