@@ -3,14 +3,21 @@ use rayon::prelude::*;
3
3
use std:: env;
4
4
use std:: ffi:: OsStr ;
5
5
use std:: fs;
6
+ use std:: io:: { BufRead , BufReader } ;
6
7
use std:: path:: { Path , PathBuf } ;
7
- pub use tf_demo_parser:: { Demo , DemoParser , Parse , ParseError , ParserState , Stream } ;
8
+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
9
+ use std:: sync:: Arc ;
10
+ use tf_demo_parser:: { Demo , DemoParser , Parse , ParseError , ParserState , Stream } ;
8
11
9
12
#[ global_allocator]
10
13
static ALLOC : jemallocator:: Jemalloc = jemallocator:: Jemalloc ;
11
14
12
15
fn main ( ) -> Result < ( ) , MainError > {
13
16
better_panic:: install ( ) ;
17
+ rayon:: ThreadPoolBuilder :: new ( )
18
+ . num_threads ( 40 )
19
+ . build_global ( )
20
+ . unwrap ( ) ;
14
21
15
22
let args: Vec < _ > = env:: args ( ) . collect ( ) ;
16
23
if args. len ( ) < 2 {
@@ -24,39 +31,57 @@ fn main() -> Result<(), MainError> {
24
31
. unwrap_or_default ( ) ;
25
32
26
33
let files = gather_dir ( path) ?;
34
+ let total = files. len ( ) ;
27
35
println ! ( "found {} demo files" , files. len( ) ) ;
28
36
29
- let failures = files. par_iter ( ) . filter_map ( |entry| {
30
- let file = fs:: read ( & entry) . unwrap ( ) ;
31
- let demo = Demo :: new ( file) ;
32
- let parser = if all {
33
- DemoParser :: new_all ( demo. get_stream ( ) )
34
- } else {
35
- DemoParser :: new ( demo. get_stream ( ) )
36
- } ;
37
- if let Err ( e) = parser. parse ( ) {
38
- eprintln ! ( "{}: {}" , entry. to_str( ) . unwrap( ) , e) ;
39
- Some ( entry)
40
- } else {
41
- None
42
- }
43
- } ) ;
44
- println ! ( "Found {} failures" , failures. count( ) ) ;
37
+ let count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
38
+ let failures: Vec < _ > = files
39
+ . par_iter ( )
40
+ . filter_map ( |entry| {
41
+ let file = fs:: read ( & entry) . unwrap ( ) ;
42
+ let demo = Demo :: new ( file) ;
43
+ let parser = if all {
44
+ DemoParser :: new_all ( demo. get_stream ( ) )
45
+ } else {
46
+ DemoParser :: new ( demo. get_stream ( ) )
47
+ } ;
48
+ let done = count. fetch_add ( 1 , Ordering :: AcqRel ) ;
49
+ println ! ( "{}/{}" , done + 1 , total) ;
50
+ if let Err ( e) = parser. parse ( ) {
51
+ eprintln ! ( "{}: {}" , entry. to_str( ) . unwrap( ) , e) ;
52
+ Some ( entry)
53
+ } else {
54
+ None
55
+ }
56
+ } )
57
+ . collect ( ) ;
58
+ println ! ( "Found {} failures" , failures. len( ) ) ;
59
+ for failed in failures {
60
+ println ! ( "{}" , failed. to_str( ) . unwrap( ) ) ;
61
+ }
45
62
Ok ( ( ) )
46
63
}
47
64
48
65
fn gather_dir ( path : impl AsRef < Path > ) -> Result < Vec < PathBuf > , MainError > {
49
66
let mut files = Vec :: with_capacity ( 512 ) ;
50
67
51
- for res in fs:: read_dir ( path) ? {
52
- let entry = res?;
68
+ let meta = fs:: metadata ( path. as_ref ( ) ) ?;
69
+ if meta. is_file ( ) {
70
+ let file = fs:: File :: open ( path) ?;
71
+ for line in BufReader :: new ( file) . lines ( ) {
72
+ files. push ( line?. into ( ) )
73
+ }
74
+ } else {
75
+ for res in fs:: read_dir ( path) ? {
76
+ let entry = res?;
53
77
54
- if entry. file_type ( ) ?. is_dir ( ) {
55
- files. extend_from_slice ( & gather_dir ( entry. path ( ) ) ?) ;
56
- } else {
57
- let entry_path = entry. path ( ) ;
58
- if entry_path. extension ( ) == Some ( OsStr :: new ( "dem" ) ) {
59
- files. push ( entry. path ( ) ) ;
78
+ if entry. file_type ( ) ?. is_dir ( ) {
79
+ files. extend_from_slice ( & gather_dir ( entry. path ( ) ) ?) ;
80
+ } else {
81
+ let entry_path = entry. path ( ) ;
82
+ if entry_path. extension ( ) == Some ( OsStr :: new ( "dem" ) ) {
83
+ files. push ( entry. path ( ) ) ;
84
+ }
60
85
}
61
86
}
62
87
}
0 commit comments