From d881ed69e262c6bca1d17b82525719fb3210a314 Mon Sep 17 00:00:00 2001 From: Felipe Cotti Date: Sat, 30 Nov 2024 18:17:52 -0300 Subject: [PATCH] Performed some basic linting. --- .../AudioConverter/MP3AudioConverter.cs | 127 +++++------ src/Plpext.Core/AudioPlayer/AudioPlayer.cs | 39 +--- .../FileStorage/DiskFileStorage.cs | 56 +++-- src/Plpext.Core/Interfaces/IAudioConverter.cs | 5 - src/Plpext.Core/Interfaces/IAudioPlayer.cs | 5 - src/Plpext.Core/Interfaces/IFileStorage.cs | 14 +- src/Plpext.Core/Interfaces/IMP3Parser.cs | 14 +- src/Plpext.Core/Interfaces/IPackExtractor.cs | 8 +- src/Plpext.Core/MP3Parser/MP3Parser.cs | 24 +- src/Plpext.Core/Models/AudioFile.cs | 23 +- src/Plpext.Core/Models/Enums.cs | 8 +- src/Plpext.Core/Models/EventArgs.cs | 12 +- src/Plpext.Core/Models/MP3File.cs | 15 +- src/Plpext.Core/Models/PlaybackProgress.cs | 19 +- .../PackExtractor/CorePackExtractor.cs | 29 +-- src/Plpext/Plpext.UI/App.axaml | 21 +- .../Controls/AudioPlayerControl.axaml | 111 +++++---- .../DependencyInjection/Container.cs | 73 +++--- src/Plpext/Plpext.UI/Plpext.UI.csproj | 26 ++- src/Plpext/Plpext.UI/Program.cs | 37 ++- .../Services/FileLoader/FileLoaderService.cs | 3 +- .../PlatformStorage/PlatformStorageService.cs | 7 +- .../Plpext.UI/Styles/AudioPlayerControl.axaml | 108 ++++----- src/Plpext/Plpext.UI/Styles/Border.axaml | 18 +- src/Plpext/Plpext.UI/Styles/Button.axaml | 34 +-- src/Plpext/Plpext.UI/Styles/TextBox.axaml | 11 +- src/Plpext/Plpext.UI/Styles/Window.axaml | 5 +- .../ViewModels/AudioPlayerViewModel.cs | 214 +++++++++--------- .../ViewModels/MainWindowViewModel.cs | 44 ++-- .../Plpext.UI/ViewModels/ViewModelBase.cs | 9 +- src/Plpext/Plpext.UI/Views/MainWindow.axaml | 85 +++---- src/Plpext/Plpext.UI/velopack.json | 6 - src/Plpext/Plpext.sln | 10 - 33 files changed, 580 insertions(+), 640 deletions(-) delete mode 100644 src/Plpext/Plpext.UI/velopack.json diff --git a/src/Plpext.Core/AudioConverter/MP3AudioConverter.cs b/src/Plpext.Core/AudioConverter/MP3AudioConverter.cs index 317b27e..1ad51eb 100644 --- a/src/Plpext.Core/AudioConverter/MP3AudioConverter.cs +++ b/src/Plpext.Core/AudioConverter/MP3AudioConverter.cs @@ -2,85 +2,80 @@ using Plpext.Core.Interfaces; using Plpext.Core.Models; -namespace Plpext.Core.AudioConverter -{ - public class MP3AudioConverter : IAudioConverter - { - private readonly IMP3Parser _parser; +namespace Plpext.Core.AudioConverter; - public MP3AudioConverter(IMP3Parser parser) +public class MP3AudioConverter : IAudioConverter +{ + private readonly IMP3Parser _parser; + + public MP3AudioConverter(IMP3Parser parser) + { + _parser = parser; + } + public async Task ConvertAudioAsync(ReadOnlyMemory mp3Input, CancellationToken cancellationToken) + { + var baseFile = await _parser.ParseIntoMP3(mp3Input, cancellationToken); + byte[] pcmData = null!; + await using var mp3Stream = new MP3Stream(new MemoryStream(baseFile.Data.ToArray())); + await using var pcmStream = new MemoryStream(); + var buffer = new byte[4096]; + int bytesReturned = 1; + int totalBytesRead = 0; + + try { - _parser = parser; - } - public async Task ConvertAudioAsync(ReadOnlyMemory file, CancellationToken cancellationToken) - { - var baseFile = await _parser.ParseIntoMP3(file, cancellationToken); - byte[] pcmData = null!; - using var mp3Stream = new MP3Stream(new MemoryStream(baseFile.Data.ToArray())); - using var pcmStream = new MemoryStream(); - var buffer = new byte[4096]; - int bytesReturned = 1; - int totalBytesRead = 0; - - try + while (bytesReturned > 0) { - while (bytesReturned > 0) + try { - try - { - bytesReturned = await mp3Stream.ReadAsync(buffer, 0, buffer.Length); - } - catch (IndexOutOfRangeException ex) - { - Console.WriteLine($"{ex.Message}"); - Console.WriteLine($"File reached a corrupted/non-compliant portion. Total bytes read: {totalBytesRead} | File: {baseFile.Name}"); - break; - } - catch (NullReferenceException ex) - { - Console.WriteLine($"{ex.Message}"); - Console.WriteLine($"File reached a corrupted/non-compliant portion. Total bytes read: {totalBytesRead} | File: {baseFile.Name}"); - break; - } - totalBytesRead += bytesReturned; - await pcmStream.WriteAsync(buffer, 0, bytesReturned); + bytesReturned = await mp3Stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); } - } - catch (Exception ex) - { - Console.WriteLine("Exception details:"); - Console.WriteLine($"Message: {ex.Message}"); - Console.WriteLine($"StackTrace: {ex.StackTrace}"); - if (ex.InnerException != null) + catch (IndexOutOfRangeException) { - Console.WriteLine($"InnerException: {ex.InnerException.Message}"); - Console.WriteLine($"InnerException StackTrace: {ex.InnerException.StackTrace}"); + //File reached a corrupted/non-compliant portion. + break; } + catch (NullReferenceException) + { + //File reached a corrupted/non-compliant portion that caused MP3Sharp to throw a NRE. + break; + } + totalBytesRead += bytesReturned; + await pcmStream.WriteAsync(buffer, 0, bytesReturned, cancellationToken); } + pcmData = ResampleToMono(pcmStream.ToArray()); - + var audioDuration = (double)(pcmData.Length / 2) / mp3Stream.Frequency; - return new AudioFile() { Name = baseFile.Name, MP3Data = file, Data = pcmData, Duration = TimeSpan.FromSeconds(audioDuration), Format = AudioFormat.Mono16, Frequency = mp3Stream.Frequency }; + return new AudioFile() { Name = baseFile.Name, MP3Data = mp3Input, Data = pcmData, Duration = TimeSpan.FromSeconds(audioDuration), Format = AudioFormat.Mono16, Frequency = mp3Stream.Frequency }; } - - private static byte[] ResampleToMono(Span data) + catch (Exception) { - byte[] newData = new byte[data.Length / 2]; - - for (int i = 0; i < data.Length / 4; ++i) - { - int HI = 1; - int LO = 0; - short left = (short)((data[i * 4 + HI] << 8) | (data[i * 4 + LO] & 0xff)); - short right = (short)((data[i * 4 + 2 + HI] << 8) | (data[i * 4 + 2 + LO] & 0xff)); - int avg = (left + right) / 2; - - newData[i * 2 + HI] = (byte)((avg >> 8) & 0xff); - newData[i * 2 + LO] = (byte)((avg & 0xff)); - } - - return newData; + //An exception here would be something new. } + return new AudioFile() { Name = $"${baseFile.Name} failed extraction", Data = ReadOnlyMemory.Empty, MP3Data = ReadOnlyMemory.Empty, Duration = TimeSpan.FromSeconds(0), Format = AudioFormat.Unknown, Frequency = 0 }; + } + + /* This is pretty cursed, but it works. + I don't remember where I got it from the first time, though. I think LLMs weren't a thing yet. + */ + private static byte[] ResampleToMono(Span data) + { + byte[] newData = new byte[data.Length / 2]; + + for (int i = 0; i < data.Length / 4; ++i) + { + int HI = 1; + int LO = 0; + short left = (short)((data[i * 4 + HI] << 8) | (data[i * 4 + LO] & 0xff)); + short right = (short)((data[i * 4 + 2 + HI] << 8) | (data[i * 4 + 2 + LO] & 0xff)); + int avg = (left + right) / 2; + + newData[i * 2 + HI] = (byte)((avg >> 8) & 0xff); + newData[i * 2 + LO] = (byte)((avg & 0xff)); + } + + return newData; } } diff --git a/src/Plpext.Core/AudioPlayer/AudioPlayer.cs b/src/Plpext.Core/AudioPlayer/AudioPlayer.cs index 71dfb14..94643fd 100644 --- a/src/Plpext.Core/AudioPlayer/AudioPlayer.cs +++ b/src/Plpext.Core/AudioPlayer/AudioPlayer.cs @@ -1,13 +1,6 @@ using OpenTK.Audio.OpenAL; using Plpext.Core.Interfaces; using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml.Linq; namespace Plpext.Core.AudioPlayer; @@ -35,19 +28,18 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable { int bufferId = AL.GenBuffer(); int sourceId = AL.GenSource(); - + _currentBufferId = bufferId; _currentSourceId = sourceId; AL.BufferData(bufferId, ALFormat.Mono16, input.Data.Span, input.Frequency); AL.Source(sourceId, ALSourcei.Buffer, bufferId); - if(autoStart) + if (autoStart) return await Start(); return true; } - catch (Exception ex) + catch (Exception) { - Console.WriteLine($"init error: {ex}"); await CleanupPlaybackResources(); return false; } @@ -63,7 +55,6 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable while (!cancellationToken.IsCancellationRequested) { var sourceState = (ALSourceState)AL.GetSource(_currentSourceId.Value, ALGetSourcei.SourceState); - Console.WriteLine($"source state: {sourceState}"); if (sourceState == ALSourceState.Stopped) break; if (State != PlaybackState.Paused) @@ -73,8 +64,6 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable ((double)(_audioFile.Data.Length - (_audioFile.Data.Length - bytesPlayed)) / 2) / _audioFile.Frequency ); - Console.WriteLine($"Progress: {currentPosition.TotalSeconds:F2}s / {_audioFile.Duration.TotalSeconds:F2}s"); - OnProgressUpdated?.Invoke(this, new() { CurrentPosition = currentPosition, @@ -86,15 +75,14 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable } catch (OperationCanceledException) { - Console.WriteLine("Playback cancelled"); + //An operation cancelled here is expected behavior. } - catch (Exception ex) + catch (Exception) { - Console.WriteLine($"Monitor error: {ex}"); + //There is little to be done if we get any other exception during monitoring. Finish up playback monitoring. } finally { - Console.WriteLine("Monitor task ending"); OnProgressUpdated?.Invoke(this, new() { CurrentPosition = _audioFile.Duration, @@ -108,9 +96,9 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable { lock (_lock) { + //If we try and start/resume playback without a proper source id or audio file, we can't go on. if (_currentSourceId == null || _audioFile == null) { - Console.WriteLine("Cannot start - no source or audio file"); return Task.FromResult(false); } @@ -137,16 +125,13 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable State = PlaybackState.Playing; OnPlaybackStarted?.Invoke(this, new PlaybackStartedEventArgs { AudioFile = _audioFile }); - Console.WriteLine($"Rewind"); AL.SourceRewind(_currentSourceId.Value); - Console.WriteLine($"Play"); AL.SourcePlay(_currentSourceId.Value); _playbackTask = Task.Run(() => MonitorPlaybackAsync(_playbackCts.Token)); return Task.FromResult(true); } - catch (Exception ex) + catch (Exception) { - Console.WriteLine($"Start error: {ex}"); return Task.FromResult(false); } } @@ -177,7 +162,6 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable public void Stop() { - Console.WriteLine($"STOP called"); lock (_lock) { _playbackCts?.Cancel(); @@ -190,7 +174,8 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable _playbackCts?.Cancel(); State = PlaybackState.Stopped; } - OnPlaybackStopped?.Invoke(this, new() { AudioFile = _audioFile }); + if(_audioFile is not null) + OnPlaybackStopped?.Invoke(this, new() { AudioFile = _audioFile }); } private Task CleanupPlaybackResources() @@ -209,9 +194,9 @@ public sealed class AudioPlayer : IAudioPlayer, IDisposable _currentBufferId = null; } } - catch (Exception ex) + catch (Exception) { - Console.WriteLine($"Error during cleanup: {ex}"); + //Things might be pretty broken at this point if this exception pops. } return Task.CompletedTask; } diff --git a/src/Plpext.Core/FileStorage/DiskFileStorage.cs b/src/Plpext.Core/FileStorage/DiskFileStorage.cs index 06a7b70..824878c 100644 --- a/src/Plpext.Core/FileStorage/DiskFileStorage.cs +++ b/src/Plpext.Core/FileStorage/DiskFileStorage.cs @@ -1,45 +1,41 @@ using Plpext.Core.Interfaces; using Plpext.Core.Models; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Plpext.Core.FileStorage +namespace Plpext.Core.FileStorage; + +public class DiskFileStorage : IFileStorage { - public class DiskFileStorage : IFileStorage + public async Task SaveFilesAsync(IEnumerable files, string targetPath) { - public async Task SaveFilesAsync(IEnumerable files, string targetPath) + HashSet names = []; + try { - HashSet names = new HashSet(); + if (!Directory.Exists(targetPath)) + Directory.CreateDirectory(targetPath); + } + catch (Exception) + { + return; + } + + foreach (var file in files) + { + var finalFileName = TryAddName(names, string.Join("_", file.Name.Split(Path.GetInvalidFileNameChars()))); try { - if (!Directory.Exists(targetPath)) - Directory.CreateDirectory(targetPath); + await File.Create(Path.Combine(targetPath, $"{finalFileName}.mp3")).WriteAsync(file.Data); } - catch (Exception e) + catch (Exception) { - Debug.WriteLine(e); - return; + //We can proceed with the next ones. } - - foreach (var file in files) - { - var finalFileName = TryAddName(names, string.Join("_", file.Name.Split(Path.GetInvalidFileNameChars()))); - try - { - await File.Create(Path.Combine(targetPath, $"{finalFileName}.mp3")).WriteAsync(file.Data); - } - catch (Exception e) { Debug.WriteLine(e); } - } - } - private static string TryAddName(HashSet names, string newName) - { - if (!names.Add(newName)) - return TryAddName(names, newName + "_"); - return newName; } } + private static string TryAddName(HashSet names, string newName) + { + if (!names.Add(newName)) + return TryAddName(names, newName + "_"); + return newName; + } } diff --git a/src/Plpext.Core/Interfaces/IAudioConverter.cs b/src/Plpext.Core/Interfaces/IAudioConverter.cs index ac12c53..81c1be1 100644 --- a/src/Plpext.Core/Interfaces/IAudioConverter.cs +++ b/src/Plpext.Core/Interfaces/IAudioConverter.cs @@ -1,9 +1,4 @@ using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Plpext.Core.Interfaces; public interface IAudioConverter diff --git a/src/Plpext.Core/Interfaces/IAudioPlayer.cs b/src/Plpext.Core/Interfaces/IAudioPlayer.cs index 9ae75da..eaf0bcf 100644 --- a/src/Plpext.Core/Interfaces/IAudioPlayer.cs +++ b/src/Plpext.Core/Interfaces/IAudioPlayer.cs @@ -1,9 +1,4 @@ using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Plpext.Core.Interfaces; diff --git a/src/Plpext.Core/Interfaces/IFileStorage.cs b/src/Plpext.Core/Interfaces/IFileStorage.cs index 2228e9c..f05ea9a 100644 --- a/src/Plpext.Core/Interfaces/IFileStorage.cs +++ b/src/Plpext.Core/Interfaces/IFileStorage.cs @@ -1,14 +1,8 @@ using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Plpext.Core.Interfaces +namespace Plpext.Core.Interfaces; + +public interface IFileStorage { - public interface IFileStorage - { - Task SaveFilesAsync(IEnumerable files, string targetPath); - } + Task SaveFilesAsync(IEnumerable files, string targetPath); } diff --git a/src/Plpext.Core/Interfaces/IMP3Parser.cs b/src/Plpext.Core/Interfaces/IMP3Parser.cs index 643e1c5..faa6a8d 100644 --- a/src/Plpext.Core/Interfaces/IMP3Parser.cs +++ b/src/Plpext.Core/Interfaces/IMP3Parser.cs @@ -1,14 +1,8 @@ using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Plpext.Core.Interfaces +namespace Plpext.Core.Interfaces; + +public interface IMP3Parser { - public interface IMP3Parser - { - Task ParseIntoMP3(ReadOnlyMemory data, CancellationToken cancellationToken); - } + Task ParseIntoMP3(ReadOnlyMemory data, CancellationToken cancellationToken); } diff --git a/src/Plpext.Core/Interfaces/IPackExtractor.cs b/src/Plpext.Core/Interfaces/IPackExtractor.cs index 5f4e5e5..24bf32b 100644 --- a/src/Plpext.Core/Interfaces/IPackExtractor.cs +++ b/src/Plpext.Core/Interfaces/IPackExtractor.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plpext.Core.Interfaces; +namespace Plpext.Core.Interfaces; public interface IPackExtractor { diff --git a/src/Plpext.Core/MP3Parser/MP3Parser.cs b/src/Plpext.Core/MP3Parser/MP3Parser.cs index 53b8fa3..7e92366 100644 --- a/src/Plpext.Core/MP3Parser/MP3Parser.cs +++ b/src/Plpext.Core/MP3Parser/MP3Parser.cs @@ -1,21 +1,15 @@ -using MP3Sharp; -using Plpext.Core.Interfaces; +using Plpext.Core.Interfaces; using Plpext.Core.Models; -using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; -namespace Plpext.Core.MP3Parser +namespace Plpext.Core.MP3Parser; + +public class MP3Parser : IMP3Parser { - public class MP3Parser : IMP3Parser + public Task ParseIntoMP3(ReadOnlyMemory data, CancellationToken cancellationToken) { - public Task ParseIntoMP3(ReadOnlyMemory data, CancellationToken cancellationToken) - { - var fileName = Encoding.Latin1.GetString(data.Slice(start: 24, length: 260).Span).Split('\0')[0].Normalize().Trim(); - var fileData = data[284..]; - return Task.FromResult(new MP3File() { Name = fileName, Data = fileData }); - } + var fileName = Encoding.Latin1.GetString(data.Slice(start: 24, length: 260).Span).Split('\0')[0].Normalize().Trim(); + var fileData = data[284..]; + return Task.FromResult(new MP3File() { Name = fileName, Data = fileData }); } -} +} \ No newline at end of file diff --git a/src/Plpext.Core/Models/AudioFile.cs b/src/Plpext.Core/Models/AudioFile.cs index a979428..401ad02 100644 --- a/src/Plpext.Core/Models/AudioFile.cs +++ b/src/Plpext.Core/Models/AudioFile.cs @@ -1,18 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Plpext.Core.Models; -namespace Plpext.Core.Models +public record AudioFile { - public record AudioFile - { - public required string Name { get; init; } - public ReadOnlyMemory MP3Data { get; init; } - public ReadOnlyMemory Data { get; init; } - public TimeSpan Duration { get; init; } - public AudioFormat Format { get; init; } - public int Frequency { get; init; } - } + public required string Name { get; init; } + public ReadOnlyMemory MP3Data { get; init; } + public ReadOnlyMemory Data { get; init; } + public TimeSpan Duration { get; init; } + public AudioFormat Format { get; init; } + public int Frequency { get; init; } } diff --git a/src/Plpext.Core/Models/Enums.cs b/src/Plpext.Core/Models/Enums.cs index 9777353..bb22e4a 100644 --- a/src/Plpext.Core/Models/Enums.cs +++ b/src/Plpext.Core/Models/Enums.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plpext.Core.Models; +namespace Plpext.Core.Models; public enum AudioFormat { diff --git a/src/Plpext.Core/Models/EventArgs.cs b/src/Plpext.Core/Models/EventArgs.cs index 9911b36..63685ae 100644 --- a/src/Plpext.Core/Models/EventArgs.cs +++ b/src/Plpext.Core/Models/EventArgs.cs @@ -1,17 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plpext.Core.Models; +namespace Plpext.Core.Models; public class PlaybackStoppedEventArgs : EventArgs { - public AudioFile AudioFile { get; init; } + public required AudioFile AudioFile { get; init; } } public class PlaybackStartedEventArgs : EventArgs { - public AudioFile AudioFile { get; init; } + public required AudioFile AudioFile { get; init; } } diff --git a/src/Plpext.Core/Models/MP3File.cs b/src/Plpext.Core/Models/MP3File.cs index b1c8016..c00c4aa 100644 --- a/src/Plpext.Core/Models/MP3File.cs +++ b/src/Plpext.Core/Models/MP3File.cs @@ -1,14 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Plpext.Core.Models; -namespace Plpext.Core.Models +public record MP3File { - public record MP3File - { - public required string Name { get; init; } - public ReadOnlyMemory Data { get; init; } - } + public required string Name { get; init; } + public ReadOnlyMemory Data { get; init; } } diff --git a/src/Plpext.Core/Models/PlaybackProgress.cs b/src/Plpext.Core/Models/PlaybackProgress.cs index 2156736..f0eb4eb 100644 --- a/src/Plpext.Core/Models/PlaybackProgress.cs +++ b/src/Plpext.Core/Models/PlaybackProgress.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Plpext.Core.Models; -namespace Plpext.Core.Models +public record PlaybackProgress { - public record PlaybackProgress - { - public TimeSpan CurrentPosition { get; init; } - public TimeSpan TotalDuration { get; init; } - public double ProgressPercentage => TotalDuration.TotalSeconds > 0 ? (CurrentPosition.TotalSeconds / TotalDuration.TotalSeconds) * 100 : 0; - } -} + public TimeSpan CurrentPosition { get; init; } + public TimeSpan TotalDuration { get; init; } + public double ProgressPercentage => TotalDuration.TotalSeconds > 0 ? (CurrentPosition.TotalSeconds / TotalDuration.TotalSeconds) * 100 : 0; +} \ No newline at end of file diff --git a/src/Plpext.Core/PackExtractor/CorePackExtractor.cs b/src/Plpext.Core/PackExtractor/CorePackExtractor.cs index 0d66c82..b550a14 100644 --- a/src/Plpext.Core/PackExtractor/CorePackExtractor.cs +++ b/src/Plpext.Core/PackExtractor/CorePackExtractor.cs @@ -1,10 +1,5 @@ using Plpext.Core.Interfaces; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Plpext.Core.PackExtractor { @@ -13,24 +8,16 @@ namespace Plpext.Core.PackExtractor private static readonly byte[] filePattern = { 0x53, 0x4E, 0x44, 0x55, 0x00 }; public async Task>> GetFileListAsync(string filePath, CancellationToken cancellationToken) { - try - { - ReadOnlyMemory file = await File.ReadAllBytesAsync(filePath, cancellationToken); - var fileIndexes = FindFileIndexes(file.Span); + ReadOnlyMemory file = await File.ReadAllBytesAsync(filePath, cancellationToken); + var fileIndexes = FindFileIndexes(file.Span); - var result = new List>(); - for(int i = 0; i < fileIndexes.Count - 1; ++i) - { - var nextFile = file.Slice(start: fileIndexes[i], length: fileIndexes[i + 1] - fileIndexes[i]); - result.Add(nextFile); - } - return result; - } - catch(Exception e) + var result = new List>(); + for (int i = 0; i < fileIndexes.Count - 1; ++i) { - Debug.WriteLine(e); - throw; + var nextFile = file.Slice(start: fileIndexes[i], length: fileIndexes[i + 1] - fileIndexes[i]); + result.Add(nextFile); } + return result; } private static List FindFileIndexes(ReadOnlySpan file) @@ -54,7 +41,9 @@ namespace Plpext.Core.PackExtractor idx += filePattern.Length; } else + { idx += skipTable[file[idx + j]]; + } } result.Add(file.Length - 1); return result; diff --git a/src/Plpext/Plpext.UI/App.axaml b/src/Plpext/Plpext.UI/App.axaml index 74f1108..849ff57 100644 --- a/src/Plpext/Plpext.UI/App.axaml +++ b/src/Plpext/Plpext.UI/App.axaml @@ -1,20 +1,21 @@ - - + + - - + + - + - - + + \ No newline at end of file diff --git a/src/Plpext/Plpext.UI/Controls/AudioPlayerControl.axaml b/src/Plpext/Plpext.UI/Controls/AudioPlayerControl.axaml index d9b76f0..53ff631 100644 --- a/src/Plpext/Plpext.UI/Controls/AudioPlayerControl.axaml +++ b/src/Plpext/Plpext.UI/Controls/AudioPlayerControl.axaml @@ -2,81 +2,102 @@ xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="using:Plpext.UI" - xmlns:vm="clr-namespace:Plpext.UI.ViewModels" - xmlns:cv="clr-namespace:Plpext.UI.Controls.Converters"> - - + xmlns:cv="clr-namespace:Plpext.UI.Controls.Converters" + xmlns:vm="clr-namespace:Plpext.UI.ViewModels"> - - - - - - + + + + + + + - - + - + - - - + - - + + - + - - - + + - + diff --git a/src/Plpext/Plpext.UI/DependencyInjection/Container.cs b/src/Plpext/Plpext.UI/DependencyInjection/Container.cs index 8e54c30..753b5d4 100644 --- a/src/Plpext/Plpext.UI/DependencyInjection/Container.cs +++ b/src/Plpext/Plpext.UI/DependencyInjection/Container.cs @@ -7,10 +7,6 @@ using Plpext.Core.PackExtractor; using Plpext.UI.ViewModels; using Serilog; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Plpext.Core.AudioConverter; using Plpext.Core.AudioPlayer; using Plpext.UI.Services; @@ -19,45 +15,42 @@ using Plpext.UI.Services.FileLoader; using Plpext.UI.Services.PlatformStorage; using Plpext.UI.Views; -namespace Plpext.UI.DependencyInjection +namespace Plpext.UI.DependencyInjection; + +public static class Container { - public static class Container + private static IServiceProvider? _container; + public static IServiceProvider Services { - private static IServiceProvider? _container; - public static IServiceProvider Services - { - get => _container ?? Register(); - } + get => _container ?? Register(); + } - private static IServiceProvider Register() - { - var hostBuilder = Host - .CreateDefaultBuilder() - .UseSerilog((context, loggerConfiguration) => - { - loggerConfiguration.WriteTo.Debug(); - }) - .ConfigureServices((context, services) => - { - services.AddSingleton(); - services.AddSingleton(); + private static IServiceProvider Register() + { + var hostBuilder = Host + .CreateDefaultBuilder() + .UseSerilog((context, loggerConfiguration) => + { + loggerConfiguration.WriteTo.Debug(); + }) + .ConfigureServices((context, services) => + { + services.AddSingleton(); + services.AddSingleton(); - services.AddTransient(); - - services.AddSingleton(); - services.AddScoped(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - }) - .Build(); - hostBuilder.Start(); - _container = hostBuilder.Services; - return _container; - } + services.AddSingleton(); + services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + }) + .Build(); + hostBuilder.Start(); + _container = hostBuilder.Services; + return _container; } } diff --git a/src/Plpext/Plpext.UI/Plpext.UI.csproj b/src/Plpext/Plpext.UI/Plpext.UI.csproj index 01f6edd..0fc00da 100644 --- a/src/Plpext/Plpext.UI/Plpext.UI.csproj +++ b/src/Plpext/Plpext.UI/Plpext.UI.csproj @@ -12,10 +12,16 @@ true - + + win-x86 + + + win-x64 + + + - @@ -45,15 +51,21 @@ + + + + + + @@ -62,13 +74,15 @@ + + - - - + + + @@ -78,6 +92,6 @@ - + diff --git a/src/Plpext/Plpext.UI/Program.cs b/src/Plpext/Plpext.UI/Program.cs index 215ccb2..46cb5b1 100644 --- a/src/Plpext/Plpext.UI/Program.cs +++ b/src/Plpext/Plpext.UI/Program.cs @@ -2,25 +2,24 @@ using Avalonia; using Velopack; -namespace Plpext.UI -{ - internal sealed class Program - { - // Initialization code. Don't use any Avalonia, third-party APIs or any - // SynchronizationContext-reliant code before AppMain is called: things aren't initialized - // yet and stuff might break. - [STAThread] - public static void Main(string[] args) - { - VelopackApp.Build().Run(); - BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); - } +namespace Plpext.UI; - // Avalonia configuration, don't remove; also used by visual designer. - public static AppBuilder BuildAvaloniaApp() - => AppBuilder.Configure() - .UsePlatformDetect() - .WithInterFont() - .LogToTrace(); +internal sealed class Program +{ + // Initialization code. Don't use any Avalonia, third-party APIs or any + // SynchronizationContext-reliant code before AppMain is called: things aren't initialized + // yet and stuff might break. + [STAThread] + public static void Main(string[] args) + { + VelopackApp.Build().Run(); + BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); } + + // Avalonia configuration, don't remove; also used by visual designer. + public static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .WithInterFont() + .LogToTrace(); } diff --git a/src/Plpext/Plpext.UI/Services/FileLoader/FileLoaderService.cs b/src/Plpext/Plpext.UI/Services/FileLoader/FileLoaderService.cs index f1321d9..6c73cf7 100644 --- a/src/Plpext/Plpext.UI/Services/FileLoader/FileLoaderService.cs +++ b/src/Plpext/Plpext.UI/Services/FileLoader/FileLoaderService.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading.Tasks; using Plpext.Core.AudioPlayer; using Plpext.Core.Interfaces; -using Plpext.Core.Models; using Plpext.UI.ViewModels; namespace Plpext.UI.Services.FileLoader; @@ -14,7 +13,7 @@ public class FileLoaderService : IFileLoaderService private readonly IPackExtractor _packExtractor; private readonly IAudioConverter _audioConverter; private IEnumerable> _currentFile = null!; - + public FileLoaderService(IPackExtractor packExtractor, IAudioConverter audioConverter) { _packExtractor = packExtractor; diff --git a/src/Plpext/Plpext.UI/Services/PlatformStorage/PlatformStorageService.cs b/src/Plpext/Plpext.UI/Services/PlatformStorage/PlatformStorageService.cs index a980a6d..a7e9901 100644 --- a/src/Plpext/Plpext.UI/Services/PlatformStorage/PlatformStorageService.cs +++ b/src/Plpext/Plpext.UI/Services/PlatformStorage/PlatformStorageService.cs @@ -1,9 +1,6 @@ using Avalonia; using Avalonia.Controls.ApplicationLifetimes; -using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Avalonia.Platform.Storage; @@ -21,12 +18,12 @@ namespace Plpext.UI.Services.PlatformStorage { AllowMultiple = false, Title = "Select Plus Library Pack", - FileTypeFilter = [new ("Plus Library Pack"){Patterns = ["*.plp"]}], + FileTypeFilter = [new("Plus Library Pack") { Patterns = ["*.plp"] }], }); return filePath.Any() ? filePath[0].Path.AbsolutePath : string.Empty; } - + public async Task GetTargetFolderPath() { if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop || diff --git a/src/Plpext/Plpext.UI/Styles/AudioPlayerControl.axaml b/src/Plpext/Plpext.UI/Styles/AudioPlayerControl.axaml index 5625227..593acc2 100644 --- a/src/Plpext/Plpext.UI/Styles/AudioPlayerControl.axaml +++ b/src/Plpext/Plpext.UI/Styles/AudioPlayerControl.axaml @@ -1,86 +1,92 @@ - + - + - - - - - - - + + + + + + + + TotalDuration="22"> - + - + diff --git a/src/Plpext/Plpext.UI/Styles/Border.axaml b/src/Plpext/Plpext.UI/Styles/Border.axaml index 8a8eb0c..a00f7d6 100644 --- a/src/Plpext/Plpext.UI/Styles/Border.axaml +++ b/src/Plpext/Plpext.UI/Styles/Border.axaml @@ -1,15 +1,17 @@ - + - + - + diff --git a/src/Plpext/Plpext.UI/Styles/Button.axaml b/src/Plpext/Plpext.UI/Styles/Button.axaml index d27bedd..8dcac2c 100644 --- a/src/Plpext/Plpext.UI/Styles/Button.axaml +++ b/src/Plpext/Plpext.UI/Styles/Button.axaml @@ -1,30 +1,32 @@ - +