From c6fb4fb64594ef556d1ac347971645ca4dcf7fcd Mon Sep 17 00:00:00 2001 From: kingsznhone <35349300+kingsznhone@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:51:56 +0800 Subject: [PATCH] v 2.2.0 Code Cleaning Update NCMDumpCLI.csproj --- NCMDump.Net.sln | 14 ++++++++ NCMDumpCLI/NCMDumpCLI.csproj | 4 +-- NCMDumpCLI/Program.cs | 2 +- NCMDumpCore/MetaInfo.cs | 11 +++--- NCMDumpCore/NCMDumpCore.csproj | 2 +- NCMDumpCore/{NCMDump.cs => NCMDumper.cs} | 25 +++++++------- NCMDumpGUI/App.xaml.cs | 42 ++++++++++++++++++++++- NCMDumpGUI/MainWindow.xaml.cs | 3 +- NCMDumpGUI/MainWindowViewModel.cs | 19 +++++----- NCMDumpGUI/NCMDumpGUI.csproj | 2 +- NCMDumpGUI/Properties/launchSettings.json | 7 ++++ README.md | 4 +++ 12 files changed, 100 insertions(+), 35 deletions(-) rename NCMDumpCore/{NCMDump.cs => NCMDumper.cs} (91%) create mode 100644 NCMDumpGUI/Properties/launchSettings.json diff --git a/NCMDump.Net.sln b/NCMDump.Net.sln index 302f551..db023a6 100644 --- a/NCMDump.Net.sln +++ b/NCMDump.Net.sln @@ -11,20 +11,34 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NCMDumpGUI", "NCMDumpGUI\NC EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Debug|Any CPU.ActiveCfg = Debug|x64 + {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Debug|Any CPU.Build.0 = Debug|x64 {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Debug|x64.ActiveCfg = Debug|x64 {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Debug|x64.Build.0 = Debug|x64 + {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Release|Any CPU.ActiveCfg = Release|x64 + {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Release|Any CPU.Build.0 = Release|x64 {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Release|x64.ActiveCfg = Release|x64 {6D29FF99-9A20-4AC2-8246-746C2AE4FB73}.Release|x64.Build.0 = Release|x64 + {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Debug|Any CPU.ActiveCfg = Debug|x64 + {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Debug|Any CPU.Build.0 = Debug|x64 {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Debug|x64.ActiveCfg = Debug|x64 {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Debug|x64.Build.0 = Debug|x64 + {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Release|Any CPU.ActiveCfg = Release|x64 + {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Release|Any CPU.Build.0 = Release|x64 {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Release|x64.ActiveCfg = Release|x64 {4CB104C3-FE3A-4CF4-9910-5FB0150B22A6}.Release|x64.Build.0 = Release|x64 + {F4B097C1-58BB-4A79-8997-8225639D2A92}.Debug|Any CPU.ActiveCfg = Debug|x64 + {F4B097C1-58BB-4A79-8997-8225639D2A92}.Debug|Any CPU.Build.0 = Debug|x64 {F4B097C1-58BB-4A79-8997-8225639D2A92}.Debug|x64.ActiveCfg = Debug|x64 {F4B097C1-58BB-4A79-8997-8225639D2A92}.Debug|x64.Build.0 = Debug|x64 + {F4B097C1-58BB-4A79-8997-8225639D2A92}.Release|Any CPU.ActiveCfg = Release|x64 + {F4B097C1-58BB-4A79-8997-8225639D2A92}.Release|Any CPU.Build.0 = Release|x64 {F4B097C1-58BB-4A79-8997-8225639D2A92}.Release|x64.ActiveCfg = Release|x64 {F4B097C1-58BB-4A79-8997-8225639D2A92}.Release|x64.Build.0 = Release|x64 EndGlobalSection diff --git a/NCMDumpCLI/NCMDumpCLI.csproj b/NCMDumpCLI/NCMDumpCLI.csproj index 24ded9a..63dc93a 100644 --- a/NCMDumpCLI/NCMDumpCLI.csproj +++ b/NCMDumpCLI/NCMDumpCLI.csproj @@ -2,14 +2,14 @@ Exe - net8.0-windows10.0.19041.0 + net8.0-windows; enable enable NCMDumpCLI NCMDump.ico x64 app.manifest - 2.0.0 + 2.2.0 x64 diff --git a/NCMDumpCLI/Program.cs b/NCMDumpCLI/Program.cs index f195fef..9be6801 100644 --- a/NCMDumpCLI/Program.cs +++ b/NCMDumpCLI/Program.cs @@ -5,7 +5,7 @@ public class NCMDumpCLI { public static void Main(string[] args) { - NCMDump Core = new(); + NCMDumper Core = new(); if (args.Length == 0) { diff --git a/NCMDumpCore/MetaInfo.cs b/NCMDumpCore/MetaInfo.cs index f60ecf4..6188ff1 100644 --- a/NCMDumpCore/MetaInfo.cs +++ b/NCMDumpCore/MetaInfo.cs @@ -1,15 +1,13 @@ -using System.Text.Json; - -namespace NCMDumpCore +namespace NCMDumpCore { public class MetaInfo { public string musicId { get; set; } public string musicName { get; set; } - public List> artist { get; set; } + public List> artist { get; set; } public string albumId { get; set; } public string album { get; set; } - public JsonElement albumPicDocId { get; set; } + public string albumPicDocId { get; set; } public string albumPic { get; set; } public int bitrate { get; set; } public string mp3DocId { get; set; } @@ -18,7 +16,8 @@ public class MetaInfo public List alias { get; set; } public List transNames { get; set; } public string format { get; set; } - public JsonElement fee { get; set; } + public float fee { get; set; } + public float volumeDelta { get; set; } public Dictionary privilege { get; set; } } } \ No newline at end of file diff --git a/NCMDumpCore/NCMDumpCore.csproj b/NCMDumpCore/NCMDumpCore.csproj index ed1cb0e..9331494 100644 --- a/NCMDumpCore/NCMDumpCore.csproj +++ b/NCMDumpCore/NCMDumpCore.csproj @@ -4,7 +4,7 @@ net8.0; enable enable - 1.7.0 + 2.2.0 x64 diff --git a/NCMDumpCore/NCMDump.cs b/NCMDumpCore/NCMDumper.cs similarity index 91% rename from NCMDumpCore/NCMDump.cs rename to NCMDumpCore/NCMDumper.cs index ceee247..66d0b2b 100644 --- a/NCMDumpCore/NCMDump.cs +++ b/NCMDumpCore/NCMDumper.cs @@ -8,18 +8,20 @@ namespace NCMDumpCore { - public class NCMDump + public class NCMDumper { private readonly int vectorSize = Vector256.Count; private readonly byte[] coreKey = { 0x68, 0x7A, 0x48, 0x52, 0x41, 0x6D, 0x73, 0x6F, 0x35, 0x6B, 0x49, 0x6E, 0x62, 0x61, 0x78, 0x57 }; private readonly byte[] metaKey = { 0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21, 0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28 }; - private bool ReadHeader(ref MemoryStream ms) + private bool VerifyHeader(ref MemoryStream ms) { + // Header Should be "CTENFDAM" Span header = stackalloc byte[8]; ms.Read(header); - // Header Should be "CTENFDAM" - return Enumerable.SequenceEqual(header.ToArray(), new byte[] { 067, 084, 069, 078, 070, 068, 065, 077 }); + long header_num = MemoryMarshal.Read(header); + return header_num == 0x4d4144464e455443; + //return Enumerable.SequenceEqual(header.ToArray(), new byte[] { 0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D }); } private byte[] ReadRC4Key(ref MemoryStream ms) @@ -47,9 +49,8 @@ private byte[] ReadRC4Key(ref MemoryStream ms) } // decrypt keybox data - using (AesCng aes = new AesCng() { Key = coreKey, Mode = CipherMode.ECB }) + using (var decrypter = new AesCng() { Key = coreKey, Mode = CipherMode.ECB }.CreateDecryptor()) { - var decrypter = aes.CreateDecryptor(); buffer = decrypter.TransformFinalBlock(buffer.ToArray(), 0, buffer.Length).Skip(17).ToArray(); // 17 = len("neteasecloudmusic") } return buffer.ToArray(); @@ -80,11 +81,9 @@ private MetaInfo ReadMeta(ref MemoryStream ms) buffer = System.Convert.FromBase64String(Encoding.ASCII.GetString(buffer.ToArray()[22..])); // decrypt meta data which is a json contains info of the song - using (AesCng aes = new AesCng() { Key = metaKey, Mode = CipherMode.ECB }) + using (var cryptor = new AesCng() { Key = metaKey, Mode = CipherMode.ECB }.CreateDecryptor()) { - var cryptor = aes.CreateDecryptor(); buffer = cryptor.TransformFinalBlock(buffer.ToArray(), 0, buffer.Length); - var MetaJsonString = Encoding.UTF8.GetString(buffer).Replace("music:", ""); MetaInfo metainfo = JsonSerializer.Deserialize(MetaJsonString); return metainfo; @@ -125,7 +124,7 @@ private void AddTag(string fileName, byte[]? ImgData, MetaInfo metainfo) //Add more infomation tagfile.Tag.Title = metainfo.musicName; - tagfile.Tag.Performers = metainfo.artist.Select(x => x[0].GetString()).ToArray(); + tagfile.Tag.Performers = metainfo.artist.Select(x => x[0]).ToArray(); tagfile.Tag.Album = metainfo.album; tagfile.Tag.Subtitle = String.Join(@";", metainfo.alias); tagfile.Save(); @@ -181,7 +180,7 @@ public async Task ConvertAsync(string path) { if (!System.IO.File.Exists(path)) { - Console.WriteLine("File Path Not Exist!"); + Console.WriteLine($"File {path} Not Exist!"); return false; } @@ -189,9 +188,9 @@ public async Task ConvertAsync(string path) MemoryStream ms = new MemoryStream(await System.IO.File.ReadAllBytesAsync(path)); //Verify Header - if (!ReadHeader(ref ms)) + if (!VerifyHeader(ref ms)) { - Console.WriteLine("Not a NCM File"); + Console.WriteLine($"{path} is not a NCM File"); return false; } diff --git a/NCMDumpGUI/App.xaml.cs b/NCMDumpGUI/App.xaml.cs index faaa9f5..8eb8cc7 100644 --- a/NCMDumpGUI/App.xaml.cs +++ b/NCMDumpGUI/App.xaml.cs @@ -1,6 +1,9 @@ using Microsoft.Extensions.DependencyInjection; using NCMDumpCore; using System; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; using System.Windows; namespace NCMDumpGUI @@ -8,6 +11,7 @@ namespace NCMDumpGUI public partial class App : Application { private IServiceProvider _serviceProvider; + private ObservableCollection NCMCollection = new(); protected override void OnStartup(StartupEventArgs e) { @@ -15,13 +19,20 @@ protected override void OnStartup(StartupEventArgs e) var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); + + OnDrop(e.Args.ToArray()); + if (NCMCollection.Count > 0) + { + MainWindowViewModel vm = _serviceProvider.GetRequiredService(); + vm.NCMCollection = NCMCollection; + } var mainWindow = _serviceProvider.GetRequiredService(); mainWindow.Show(); } private void ConfigureServices(IServiceCollection services) { - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); } @@ -35,5 +46,34 @@ protected override void OnExit(ExitEventArgs e) base.OnExit(e); } + + private void OnDrop(string[] args) + { + foreach (string _path in args) + { + if (new DirectoryInfo(_path).Exists) + { + WalkThrough(new DirectoryInfo(_path)); + } + else if (new FileInfo(_path).Exists) + { + if (_path.EndsWith(@".ncm") && !NCMCollection.Any(x => x.FilePath == _path)) + NCMCollection.Add(new NCMProcessStatus(_path, "Await")); + } + } + } + + private void WalkThrough(DirectoryInfo dir) + { + foreach (DirectoryInfo d in dir.GetDirectories()) + { + WalkThrough(d); + } + foreach (FileInfo f in dir.EnumerateFiles()) + { + if (f.FullName.EndsWith(@".ncm") && !NCMCollection.Any(x => x.FilePath == f.FullName)) + NCMCollection.Add(new NCMProcessStatus(f.FullName, "Await")); + } + } } } \ No newline at end of file diff --git a/NCMDumpGUI/MainWindow.xaml.cs b/NCMDumpGUI/MainWindow.xaml.cs index 538b154..edb4b97 100644 --- a/NCMDumpGUI/MainWindow.xaml.cs +++ b/NCMDumpGUI/MainWindow.xaml.cs @@ -14,7 +14,7 @@ public MainWindow(MainWindowViewModel _vm) VM = _vm; this.DataContext = VM; InitializeComponent(); - + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000, 0)) { SystemThemeWatcher.Watch(this); @@ -42,6 +42,7 @@ private void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e) private void WorkingList_Drop(object sender, System.Windows.DragEventArgs e) { string[] args = (string[])e.Data.GetData(DataFormats.FileDrop); + if (args is not null && args.Length != 0) { VM.OnDrop(args); diff --git a/NCMDumpGUI/MainWindowViewModel.cs b/NCMDumpGUI/MainWindowViewModel.cs index a1f609b..ceb1a2a 100644 --- a/NCMDumpGUI/MainWindowViewModel.cs +++ b/NCMDumpGUI/MainWindowViewModel.cs @@ -17,7 +17,7 @@ namespace NCMDumpGUI [ObservableObject] public partial class MainWindowViewModel { - private readonly NCMDump Core; + private readonly NCMDumper Core; private bool _willDeleteNCM; @@ -27,22 +27,22 @@ public bool WillDeleteNCM set => SetProperty(ref _willDeleteNCM, value); } - private string _ApplicationTitle; + private string _applicationTitle; public string ApplicationTitle { - get => _ApplicationTitle; - set => _ApplicationTitle = value; + get => _applicationTitle; + set => SetProperty(ref _applicationTitle, value); } public ObservableCollection NCMCollection { get; set; } - public MainWindowViewModel(NCMDump _core) + public MainWindowViewModel(NCMDumper _core) { Core = _core; WillDeleteNCM = true; ApplicationTitle = "NCMDump.NET"; - NCMCollection = new ObservableCollection(); + NCMCollection = new(); AddFolderCommand = new RelayCommand(FolderDialog); AddFileCommand = new RelayCommand(FileDialog); ClearCommand = new RelayCommand(ClearList); @@ -148,7 +148,7 @@ private void FolderDialog() private void FileDialog() { - Microsoft.Win32.OpenFileDialog ofp = new Microsoft.Win32.OpenFileDialog(); + Microsoft.Win32.OpenFileDialog ofp = new(); ofp.Multiselect = true; ofp.Filter = "NCM File(*.ncm)|*.ncm"; if (ofp.ShowDialog() == true) @@ -161,12 +161,13 @@ private void FileDialog() } } - private void SwitchTheme() { + private void SwitchTheme() + { var appTheme = ApplicationThemeManager.GetAppTheme(); ApplicationTheme newTheme = appTheme == ApplicationTheme.Dark ? ApplicationTheme.Light : ApplicationTheme.Dark; WindowBackdropType backdrop = WindowBackdropType.Acrylic; - if (newTheme ==ApplicationTheme.Dark) + if (newTheme == ApplicationTheme.Dark) { if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000, 0)) { diff --git a/NCMDumpGUI/NCMDumpGUI.csproj b/NCMDumpGUI/NCMDumpGUI.csproj index 32cfac2..f1e5d54 100644 --- a/NCMDumpGUI/NCMDumpGUI.csproj +++ b/NCMDumpGUI/NCMDumpGUI.csproj @@ -6,7 +6,7 @@ enable True NCMDump.ico - 2.1.0 + 2.2.0 x64 diff --git a/NCMDumpGUI/Properties/launchSettings.json b/NCMDumpGUI/Properties/launchSettings.json new file mode 100644 index 0000000..1a1c9aa --- /dev/null +++ b/NCMDumpGUI/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "NCMDumpGUI": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 02f8df6..8cf0011 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ Keep ID3 tags and cover image. ## Changelog +### v2.2.0 2024.03.24 + +Code Cleaning. + ### v2.1.0 2023.11.19 Fix some UI problem on Windows 10.