Skip to content

Commit

Permalink
1. Update null check
Browse files Browse the repository at this point in the history
2. Rename
3. Update cache rule
4. Add "TraditionalToSimplified" function
5. Update UX
  • Loading branch information
wwh1004 committed Aug 18, 2019
1 parent 0c759d4 commit 561882f
Show file tree
Hide file tree
Showing 26 changed files with 273 additions and 185 deletions.
8 changes: 4 additions & 4 deletions NLyric/Audio/Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class Album : ITrackOrAlbum {
public int? Year => _year;

public Album(string name, string[] artists, int? trackCount, int? year) {
if (name == null)
if (name is null)
throw new ArgumentNullException(nameof(name));
if (artists == null)
if (artists is null)
throw new ArgumentNullException(nameof(artists));

_name = name;
Expand All @@ -33,7 +33,7 @@ public Album(string name, string[] artists, int? trackCount, int? year) {
/// <param name="track"></param>
/// <param name="getArtistsFromTrack">当 <see cref="Track.AlbumArtist"/> 为空时,是否从 <see cref="Track.Artist"/> 获取艺术家</param>
public Album(ATL.Track track, bool getArtistsFromTrack) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));
if (!HasAlbumInfo(track))
throw new ArgumentException(nameof(track) + " 中不存在专辑信息");
Expand All @@ -52,7 +52,7 @@ public Album(ATL.Track track, bool getArtistsFromTrack) {
}

public static bool HasAlbumInfo(ATL.Track track) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));

return !string.IsNullOrWhiteSpace(track.Album);
Expand Down
6 changes: 3 additions & 3 deletions NLyric/Audio/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class Track : ITrackOrAlbum {
public string[] Artists => _artists;

public Track(string name, string[] artists) {
if (name == null)
if (name is null)
throw new ArgumentNullException(nameof(name));
if (artists == null)
if (artists is null)
throw new ArgumentNullException(nameof(artists));

_name = name;
_artists = artists;
}

public Track(ATL.Track track) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));

_name = track.Title.GetSafeString();
Expand Down
2 changes: 1 addition & 1 deletion NLyric/Caches/AlbumCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AlbumCache(Album album, int id) : this(album.Name, id) {
}

public AlbumCache(string name, int id) {
if (name == null)
if (name is null)
throw new ArgumentNullException(nameof(name));

Name = name;
Expand Down
20 changes: 10 additions & 10 deletions NLyric/Caches/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
namespace NLyric.Caches {
public static class Extensions {
public static AlbumCache Match(this IEnumerable<AlbumCache> caches, Album album) {
if (album == null)
if (album is null)
throw new ArgumentNullException(nameof(album));

return caches.FirstOrDefault(t => IsMatched(t, album));
}

public static TrackCache Match(this IEnumerable<TrackCache> caches, Track track, Album album) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));
if (album == null)
if (album is null)
throw new ArgumentNullException(nameof(album));

return caches.FirstOrDefault(t => IsMatched(t, track, album));
}

public static TrackCache Match(this IEnumerable<TrackCache> caches, Track track, string fileName) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));
if (fileName == null)
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

return caches.FirstOrDefault(t => IsMatched(t, track, fileName));
Expand All @@ -35,25 +35,25 @@ public static LyricCache Match(this IEnumerable<LyricCache> caches, int id) {
}

public static bool IsMatched(this AlbumCache cache, Album album) {
if (album == null)
if (album is null)
throw new ArgumentNullException(nameof(album));

return cache.Name == album.Name;
}

public static bool IsMatched(this TrackCache cache, Track track, Album album) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));
if (album == null)
if (album is null)
throw new ArgumentNullException(nameof(album));

return cache.Name == track.Name && cache.AlbumName == album.Name && cache.Artists.SequenceEqual(track.Artists);
}

public static bool IsMatched(this TrackCache cache, Track track, string fileName) {
if (track == null)
if (track is null)
throw new ArgumentNullException(nameof(track));
if (fileName == null)
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

return cache.Name == track.Name && cache.FileName == fileName && cache.Artists.SequenceEqual(track.Artists);
Expand Down
2 changes: 1 addition & 1 deletion NLyric/Caches/LyricCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LyricCache(NcmLyric lyric, string checkSum) : this(lyric.Id, lyric.IsAbso
}

public LyricCache(int id, bool isAbsoluteMusic, int rawVersion, int translatedVersion, string checkSum) {
if (checkSum == null)
if (checkSum is null)
throw new ArgumentNullException(nameof(checkSum));

Id = id;
Expand Down
6 changes: 3 additions & 3 deletions NLyric/Caches/TrackCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public TrackCache(Track track, string fileName, int id) : this(track.Name, track
}

public TrackCache(string name, string[] artists, string albumName, string fileName, int id) {
if (name == null)
if (name is null)
throw new ArgumentNullException(nameof(name));
if (artists == null)
if (artists is null)
throw new ArgumentNullException(nameof(artists));
if (albumName == null && fileName == null)
if (albumName is null && fileName is null)
throw new ArgumentException($"{nameof(albumName)}{nameof(fileName)} 不能同时为 null");

Name = name;
Expand Down
38 changes: 38 additions & 0 deletions NLyric/ChineseConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace NLyric {
internal static class ChineseConverter {
private static readonly Dictionary<char, char> _traditionalToSimplifiedMap;

static ChineseConverter() {
Assembly assembly;

assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("NLyric.TraditionalToSimplified.map"))
using (BinaryReader reader = new BinaryReader(stream)) {
int count;

count = (int)stream.Length / 4;
_traditionalToSimplifiedMap = new Dictionary<char, char>(count);
for (int i = 0; i < count; i++)
_traditionalToSimplifiedMap.Add((char)reader.ReadUInt16(), (char)reader.ReadUInt16());
}
}

public static string TraditionalToSimplified(string s) {
if (s is null)
return null;

StringBuilder sb;

sb = new StringBuilder(s);
for (int i = 0; i < sb.Length; i++)
if (_traditionalToSimplifiedMap.TryGetValue(sb[i], out char c))
sb[i] = c;
return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion NLyric/Crc32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static Crc32() {
}

public static uint Compute(byte[] data) {
if (data == null)
if (data is null)
throw new ArgumentNullException(nameof(data));

uint crc32;
Expand Down
2 changes: 1 addition & 1 deletion NLyric/DictionaryComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal sealed class DictionaryComparer<TKey, TValue> : IComparer<TKey> where T
private readonly Dictionary<TKey, TValue> _dictionary;

public DictionaryComparer(Dictionary<TKey, TValue> dictionary) {
if (dictionary == null)
if (dictionary is null)
throw new ArgumentNullException(nameof(dictionary));

_dictionary = dictionary;
Expand Down
12 changes: 5 additions & 7 deletions NLyric/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ private Logger() {
}

public void LogNewLine() {
lock (_syncRoot)
Console.WriteLine();
LogInfo(string.Empty, ConsoleColor.Gray);
}

public void LogInfo(string value) {
lock (_syncRoot)
Console.WriteLine(value);
LogInfo(value, ConsoleColor.Gray);
}

public void LogWarning(string value) {
Expand All @@ -41,14 +39,14 @@ public void LogInfo(string value, ConsoleColor color) {
}

public void LogException(Exception value) {
if (value == null)
if (value is null)
throw new ArgumentNullException(nameof(value));

LogError(ExceptionToString(value));
}

private static string ExceptionToString(Exception exception) {
if (exception == null)
if (exception is null)
throw new ArgumentNullException(nameof(exception));

StringBuilder sb;
Expand All @@ -65,7 +63,7 @@ private static void DumpException(Exception exception, StringBuilder sb) {
sb.AppendLine("StackTrace: " + Environment.NewLine + exception.StackTrace);
sb.AppendLine("TargetSite: " + Environment.NewLine + exception.TargetSite.ToString());
sb.AppendLine("----------------------------------------");
if (exception.InnerException != null)
if (!(exception.InnerException is null))
DumpException(exception.InnerException, sb);
}
}
Expand Down
36 changes: 22 additions & 14 deletions NLyric/Lyrics/Lrc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public sealed class Lrc {
private string _album;
private string _by;
private TimeSpan? _offset;
private readonly Dictionary<TimeSpan, string> _lyrics = new Dictionary<TimeSpan, string>();
private Dictionary<TimeSpan, string> _lyrics = new Dictionary<TimeSpan, string>();

public string Title {
get => _title;
set {
if (value == null) {
if (value is null) {
_title = value;
return;
}
Expand All @@ -33,7 +33,7 @@ public string Title {
public string Artist {
get => _artist;
set {
if (value == null) {
if (value is null) {
_artist = value;
return;
}
Expand All @@ -45,7 +45,7 @@ public string Artist {
public string Album {
get => _album;
set {
if (value == null) {
if (value is null) {
_album = value;
return;
}
Expand All @@ -57,7 +57,7 @@ public string Album {
public string By {
get => _by;
set {
if (value == null) {
if (value is null) {
_by = value;
return;
}
Expand All @@ -68,10 +68,18 @@ public string By {

public TimeSpan? Offset {
get => _offset;
set => _offset = (value == null || value.Value.Ticks == 0) ? null : value;
set => _offset = (value is null || value.Value.Ticks == 0) ? null : value;
}

public Dictionary<TimeSpan, string> Lyrics => _lyrics;
public Dictionary<TimeSpan, string> Lyrics {
get => _lyrics;
set {
if (value is null)
throw new ArgumentNullException(nameof(value));

_lyrics = value;
}
}

public static Lrc Parse(string text) {
if (string.IsNullOrEmpty(text))
Expand All @@ -83,7 +91,7 @@ public static Lrc Parse(string text) {
using (StringReader reader = new StringReader(text)) {
string line;

while ((line = reader.ReadLine()) != null)
while (!((line = reader.ReadLine()) is null))
if (!TryParseLine(line.Trim(), lrc))
throw new FormatException();
}
Expand All @@ -100,7 +108,7 @@ public static Lrc UnsafeParse(string text) {
using (StringReader reader = new StringReader(text)) {
string line;

while ((line = reader.ReadLine()) != null)
while (!((line = reader.ReadLine()) is null))
TryParseLine(line.Trim(), lrc);
}
return lrc;
Expand Down Expand Up @@ -164,15 +172,15 @@ public override string ToString() {
StringBuilder sb;

sb = new StringBuilder();
if (_title != null)
if (!(_title is null))
AppendLine(sb, TI, _title);
if (_artist != null)
if (!(_artist is null))
AppendLine(sb, AR, _artist);
if (_album != null)
if (!(_album is null))
AppendLine(sb, AL, _album);
if (_by != null)
if (!(_by is null))
AppendLine(sb, BY, _by);
if (_offset != null)
if (!(_offset is null))
AppendLine(sb, OFFSET, ((long)_offset.Value.TotalMilliseconds).ToString());
foreach (KeyValuePair<TimeSpan, string> lyric in _lyrics)
sb.AppendLine($"[{TimeSpanToLyricString(lyric.Key)}]{lyric.Value}");
Expand Down
12 changes: 9 additions & 3 deletions NLyric/NLyric.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>NLyric</AssemblyName>
<Title>NLyric</Title>
<Product>NLyric</Product>
<Copyright>Copyright © 2019 Wwh</Copyright>
<AssemblyVersion>2.0.2.0</AssemblyVersion>
<FileVersion>2.0.2.0</FileVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
<RootNamespace>NLyric</RootNamespace>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="TraditionalToSimplified.map" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TraditionalToSimplified.map" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="z440.atl.core" Version="2.9.0" />
Expand Down
Loading

0 comments on commit 561882f

Please sign in to comment.