22using BmsToOsu ;
33using BmsToOsu . Converter ;
44using BmsToOsu . Entity ;
5+ using BmsToOsu . Utils ;
56using CommandLine ;
7+ using CommandLine . Text ;
68using log4net ;
79
810Logger . Config ( ) ;
1416 ".bms" , ".bml" , ".bme" , ".bmx"
1517} ;
1618
17- Parser . Default . ParseArguments < Option > ( args )
18- . WithParsed ( o =>
19+ var parser = new Parser ( with =>
20+ {
21+ with . AutoVersion = false ;
22+ with . AutoHelp = true ;
23+ with . HelpWriter = null ;
24+ } ) ;
25+
26+ var result = parser . ParseArguments < Option > ( args ) ;
27+
28+ result . WithParsed ( o =>
29+ {
30+ var osz = o . OutPath + ".osz" ;
31+
32+ // avoid removing existing folder
33+ if ( Directory . Exists ( o . OutPath ) && ! o . NoRemove )
1934 {
20- var osz = o . OutPath + ".osz" ;
35+ logger . Warn ( $ "{ o . OutPath } exists, `--no-remove` will be appended to the parameter") ;
36+ o . NoRemove = true ;
37+ }
2138
22- // avoid removing existing folder
23- if ( Directory . Exists ( o . OutPath ) && ! o . NoRemove )
24- {
25- logger . Warn ( $ "{ o . OutPath } exists, `--no-remove` will be appended to the parameter") ;
26- o . NoRemove = true ;
27- }
39+ // avoid removing after generation
40+ if ( o . NoZip && ! o . NoRemove )
41+ {
42+ logger . Warn ( "`--no-remove` is appended to the parameter" ) ;
43+ o . NoRemove = true ;
44+ }
45+
46+ // avoid duplication
47+ if ( File . Exists ( osz ) )
48+ {
49+ logger . Warn ( $ "{ osz } exists, ignoring...") ;
50+ return ;
51+ }
2852
29- // avoid removing after generation
30- if ( o . NoZip && ! o . NoRemove )
53+ var ftc = new HashSet < string > ( ) ;
54+
55+ var bms = Directory
56+ . GetFiles ( o . InputPath , "*.*" , SearchOption . AllDirectories )
57+ . Where ( f => availableBmsExt . Any ( ext => f . EndsWith ( ext , StringComparison . OrdinalIgnoreCase ) ) ) ;
58+
59+ Parallel . ForEach ( bms , fp =>
60+ {
61+ logger . Info ( $ "Processing { fp } ") ;
62+
63+ var dir = Path . GetDirectoryName ( fp ) ?? "" ;
64+
65+ BmsFileData data ;
66+
67+ try
3168 {
32- logger . Warn ( "`--no-remove` is appended to the parameter" ) ;
33- o . NoRemove = true ;
69+ data = BmsFileData . FromFile ( fp ) ;
3470 }
35-
36- // avoid duplication
37- if ( File . Exists ( osz ) )
71+ catch ( InvalidDataException )
3872 {
39- logger . Warn ( $ "{ osz } exists, ignoring...") ;
4073 return ;
4174 }
4275
43- var ftc = new HashSet < string > ( ) ;
44-
45- var bms = Directory
46- . GetFiles ( o . InputPath , "*.*" , SearchOption . AllDirectories )
47- . Where ( f => availableBmsExt . Any ( ext => f . EndsWith ( ext , StringComparison . OrdinalIgnoreCase ) ) ) ;
76+ var ( osu , ftc2 ) = data . ToOsuBeatMap ( dir ) ;
4877
49- Parallel . ForEach ( bms , fp =>
78+ lock ( ftc )
5079 {
51- logger . Info ( $ "Processing { fp } ") ;
52-
53- var dir = Path . GetDirectoryName ( fp ) ?? "" ;
80+ foreach ( var c in ftc2 ) ftc . Add ( Path . Join ( dir , c ) ) ;
81+ }
5482
55- BmsFileData data ;
83+ var dest = dir . Replace ( o . InputPath , o . OutPath ) ;
5684
57- try
58- {
59- data = BmsFileData . FromFile ( fp ) ;
60- }
61- catch ( InvalidDataException )
62- {
63- return ;
64- }
85+ Directory . CreateDirectory ( dest ) ;
86+ File . WriteAllText ( Path . Join ( dest , Path . GetFileNameWithoutExtension ( fp ) + ".osu" ) , osu ) ;
87+ } ) ;
6588
66- var ( osu , ftc2 ) = data . ToOsuBeatMap ( dir ) ;
89+ if ( ! o . NoCopy )
90+ {
91+ logger . Info ( "Copying sound files" ) ;
92+ Parallel . ForEach ( ftc , c =>
93+ {
94+ var dest = c . Replace ( o . InputPath , o . OutPath ) ;
95+ dest = Path . Join ( Path . GetDirectoryName ( dest ) , Path . GetFileName ( dest ) . Escape ( ) ) ;
6796
68- lock ( ftc )
97+ if ( ! File . Exists ( dest ) )
6998 {
70- foreach ( var c in ftc2 ) ftc . Add ( Path . Join ( dir , c ) ) ;
99+ File . Copy ( c , dest , true ) ;
71100 }
72-
73- var dest = dir . Replace ( o . InputPath , o . OutPath ) ;
74-
75- Directory . CreateDirectory ( dest ) ;
76- File . WriteAllText ( Path . Join ( dest , Path . GetFileNameWithoutExtension ( fp ) + ".osu" ) , osu ) ;
77101 } ) ;
102+ }
78103
79- if ( ! o . NoCopy )
80- {
81- logger . Info ( "Copying sound files" ) ;
82- Parallel . ForEach ( ftc , c =>
83- {
84- var dest = c . Replace ( o . InputPath , o . OutPath ) ;
85-
86- if ( ! File . Exists ( dest ) )
87- {
88- File . Copy ( c , dest , true ) ;
89- }
90- } ) ;
91- }
104+ if ( ! o . NoZip && Directory . Exists ( o . OutPath ) )
105+ {
106+ logger . Info ( $ "Creating { osz } ") ;
107+ ZipFile . CreateFromDirectory ( o . OutPath , osz , CompressionLevel . Fastest , false ) ;
108+ }
92109
93- if ( ! o . NoZip && Directory . Exists ( o . OutPath ) )
94- {
95- logger . Info ( $ "Creating { osz } ") ;
96- ZipFile . CreateFromDirectory ( o . OutPath , osz , CompressionLevel . Fastest , false ) ;
97- }
110+ if ( ! o . NoRemove )
111+ {
112+ logger . Info ( $ "Removing { o . OutPath } ") ;
113+ Directory . Delete ( o . OutPath , true ) ;
114+ }
115+ } ) ;
98116
99- if ( ! o . NoRemove )
100- {
101- logger . Info ( $ "Removing { o . OutPath } ") ;
102- Directory . Delete ( o . OutPath , true ) ;
103- }
104- } ) ;
117+ result . WithNotParsed ( errs =>
118+ {
119+ var helpText = HelpText . AutoBuild ( result , h =>
120+ {
121+ h . AutoHelp = true ;
122+ h . AutoVersion = false ;
123+ return HelpText . DefaultParsingErrorsHandler ( result , h ) ;
124+ } , e => e ) ;
125+ Console . WriteLine ( helpText ) ;
126+ } ) ;
0 commit comments