Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Sep 2, 2023
1 parent 6652cf4 commit 5f67582
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
14 changes: 7 additions & 7 deletions MusicDecrypto.Library/DecryptoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected DecryptoBase(
_buffer = buffer;
_reader = new(buffer);
_oldName = name;
if (warn is not null) OnWarn += warn;
if (warn is not null) Warn += warn;
if (matchConfirm is not null) OnRequestMatch += matchConfirm;
_audioType = type;
_buffer.ResetPosition();
Expand Down Expand Up @@ -85,13 +85,13 @@ private async ValueTask<Info> GetMetadataAsync(bool firstRun = true)
file.GetTag(TagTypes.Id3v1).CopyTo(file.GetTag(TagTypes.FlacMetadata), false);
file.RemoveTags(TagTypes.Id3v1 | TagTypes.Id3v2);
modified = true;
RaiseWarn("Detected and converted non-standard ID3 tags.");
OnWarn("Detected and converted non-standard ID3 tags.");
}

if (file.GetTag(TagTypes.Xiph) is TagLib.Ogg.XiphComment meta)
{
var mqa = meta.GetField("MQAENCODER");
if (mqa.Length > 0) RaiseWarn("Detected MQA-encoded FLAC stream.");
if (mqa.Length > 0) OnWarn("Detected MQA-encoded FLAC stream.");
}
}

Expand All @@ -105,20 +105,20 @@ private async ValueTask<Info> GetMetadataAsync(bool firstRun = true)
tag.Album,
tag.Pictures.Length > 0 ? tag.Pictures[0].Data.Data : null);
}
else RaiseWarn("Reading tags from DFF files is not supported.");
else OnWarn("Reading tags from DFF files is not supported.");

return new Info((_newBaseName ?? Path.GetFileNameWithoutExtension(_oldName)) + _audioType.GetExtension());
}
protected virtual ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
=> ValueTask.FromResult(false); // return whether metadata is modified

public delegate void WarnHandler(string message);
private event WarnHandler? OnWarn;
public void RaiseWarn(string message) => OnWarn?.Invoke(message);
private event WarnHandler? Warn;
protected void OnWarn(string message) => Warn?.Invoke(message);

public delegate ValueTask<bool> MatchRequestHandler(string message, IEnumerable<MatchInfo> properties);
private readonly MatchRequestHandler? OnRequestMatch;
public async ValueTask<bool> RequestMatchAsync((string, string, string) local, (string, string, string) online)
protected async ValueTask<bool> RequestMatchAsync((string, string, string) local, (string, string, string) online)
=> OnRequestMatch is not null && await OnRequestMatch.Invoke(
"Metadata matching confirmation",
ImmutableArray.Create<MatchInfo>(
Expand Down
1 change: 0 additions & 1 deletion MusicDecrypto.Library/Vendor/Kugou/Decrypto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using MusicDecrypto.Library.Numerics;

namespace MusicDecrypto.Library.Vendor.Kugou;
Expand Down
10 changes: 5 additions & 5 deletions MusicDecrypto.Library/Vendor/NetEase/Decrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public Decrypto(MarshalMemoryStream buffer, string name, WarnHandler? warn) : ba
}
catch (NullFileChunkException)
{
RaiseWarn("File does not contain metadata.");
OnWarn("File does not contain metadata.");
}
catch
{
RaiseWarn("Metadata seems corrupted.");
OnWarn("Metadata seems corrupted.");
}

// Skip ahead
Expand All @@ -120,7 +120,7 @@ public Decrypto(MarshalMemoryStream buffer, string name, WarnHandler? warn) : ba
catch (NullFileChunkException)
{
_coverBuffer = null;
RaiseWarn("File does not contain cover image. Will try to get from server.");
OnWarn("File does not contain cover image. Will try to get from server.");
}

// Set offset
Expand All @@ -140,15 +140,15 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
var coverUri = _metadata?.AlbumPic;
if (!Uri.IsWellFormedUriString(coverUri, UriKind.Absolute))
{
RaiseWarn("File does not contain cover link.");
OnWarn("File does not contain cover link.");
throw new InvalidDataException();
}
using var httpClient = new HttpClient();
_coverBuffer = await httpClient.GetByteArrayAsync(coverUri);
}
catch
{
RaiseWarn("Failed to download cover image.");
OnWarn("Failed to download cover image.");
}
}

Expand Down
12 changes: 6 additions & 6 deletions MusicDecrypto.Library/Vendor/Tencent/QmcDecrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
if (!string.IsNullOrEmpty(tag.Title) && tag.AlbumArtists.Length > 0)
{
_newBaseName = string.Join(" - ", tag.AlbumArtists[0], tag.Title);
RaiseWarn($"New filename “{_newBaseName}");
OnWarn($"New filename “{_newBaseName}");
}
else if (!string.IsNullOrEmpty(tag.Title) && tag.Performers.Length > 0)
{
_newBaseName = string.Join(" - ", tag.Performers[0], tag.Title);
RaiseWarn($"New filename “{_newBaseName}");
OnWarn($"New filename “{_newBaseName}");
}
else RaiseWarn("Detected hashed filename but failed to determine new name.");
else OnWarn("Detected hashed filename but failed to determine new name.");
}

using var _client = new ApiClient();
Expand All @@ -205,7 +205,7 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
if (_id != 0)
{
try { meta = (await _client.GetTracksInfoAsync(_id))?[0]; }
catch { RaiseWarn("Failed to retrieve metadata regarding the ID."); }
catch { OnWarn("Failed to retrieve metadata regarding the ID."); }
}
else if (!(string.IsNullOrEmpty(tag.Performers[0]) && string.IsNullOrEmpty(tag.Album) || string.IsNullOrEmpty(tag.Title)))
{
Expand All @@ -219,7 +219,7 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
meta = await FindMatchedTrackAsync(tag, results);
}
catch { }
finally { if (meta is null) RaiseWarn("Failed to match tracks online."); }
finally { if (meta == null) OnWarn("Failed to match tracks online."); }
}

if (meta is not null)
Expand Down Expand Up @@ -267,7 +267,7 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
}
catch
{
RaiseWarn("Failed to fetch album cover.");
OnWarn("Failed to fetch album cover.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions MusicDecrypto.Library/Vendor/Xiami/Cipher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MusicDecrypto.Library.Numerics;
using System;
using System.Numerics;
using System;
using MusicDecrypto.Library.Numerics;

namespace MusicDecrypto.Library.Vendor.Xiami;

Expand Down

0 comments on commit 5f67582

Please sign in to comment.