-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
123 lines (106 loc) · 3.89 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using CommandLine;
using GoldenCudgel.Chain;
using GoldenCudgel.Entities;
using GoldenCudgel.Utils;
namespace GoldenCudgel;
public class Program
{
public static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args).WithParsed(Run);
}
private static void Run(Options options)
{
var fileInfoList = FileUtils.ReadFileList(options.Path);
if (fileInfoList.Count == 0)
{
Console.WriteLine("No such file found.");
return;
}
//创建单独的写入目录
var directoryInfo = fileInfoList[0].Directory?.Parent;
if (directoryInfo?.GetDirectories("convert").Length == 0)
{
directoryInfo?.CreateSubdirectory("convert");
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Find {fileInfoList.Count} songs.");
if (fileInfoList.Count > 50)
{
//最多用4个线程处理
int processorCount = Environment.ProcessorCount > 4
? 4
: Environment.ProcessorCount;
Task[] tasks = new Task[processorCount];
for (var i = 0; i < tasks.Length; i++)
{
var i1 = i;
tasks[i] = new Task(() =>
{
int songCount = fileInfoList.Count % processorCount == 0
? fileInfoList.Count / processorCount
: fileInfoList.Count / processorCount + 1;
ProcessFile(fileInfoList.Skip(i1 * songCount).Take(songCount).ToList());
});
}
foreach (var task in tasks)
{
task.Start();
}
Task.WaitAll(tasks.ToArray());
}
else
{
ProcessFile(fileInfoList);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Done!");
}
private static void ProcessFile(List<FileInfo> fileInfoList)
{
var headerHandler = AssembleChain();
foreach (var fileInfo in fileInfoList)
{
var ncmObject = new NcmObject
{
FileName = fileInfo.Name
};
using var fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
headerHandler.Handle(fileInfo, fs, ncmObject);
fs.Close();
Console.WriteLine(ncmObject.ToString());
}
}
private static HeaderHandler AssembleChain()
{
var headerHandler = new HeaderHandler();
var jumpHandler = new JumpHandler();
var rc4LengthHandler = new Rc4LengthHandler();
var rc4ContentHandler = new Rc4ContentHandler();
var metaLengthHandler = new MetaLengthHandler();
var metaContentHandler = new MetaContentHandler();
var checkHandler = new CheckHandler();
var jump2Handler = new Jump2Handler();
var albumImageLengthHandler = new AlbumImageLengthHandler();
var albumImageHandler = new AlbumImageHandler();
var musicDataHandler = new MusicDataHandler();
var fileCreateHandler = new FileCreateHandler();
headerHandler.SetNext(jumpHandler)
.SetNext(rc4LengthHandler)
.SetNext(rc4ContentHandler)
.SetNext(metaLengthHandler)
.SetNext(metaContentHandler)
.SetNext(checkHandler)
.SetNext(jump2Handler)
.SetNext(albumImageLengthHandler)
.SetNext(albumImageHandler)
.SetNext(musicDataHandler)
.SetNext(fileCreateHandler);
return headerHandler;
}
private class Options
{
[Option('p', "path", Required = true, HelpText = "网易云音乐下载目录")]
public string Path { get; set; }
}
}