11use crate :: command:: OutputDirectoryField ;
22use crate :: diff:: TILDiff ;
3+ use crate :: helper:: path_to_type_libraries;
34use binaryninja:: background_task:: BackgroundTask ;
45use binaryninja:: binary_view:: BinaryView ;
56use binaryninja:: command:: Command ;
67use binaryninja:: interaction:: { Form , FormInputField } ;
7- use binaryninja:: types:: TypeLibrary ;
88use std:: path:: PathBuf ;
99use std:: thread;
1010
1111pub struct InputFileAField ;
1212
1313impl InputFileAField {
1414 pub fn field ( ) -> FormInputField {
15- FormInputField :: OpenFileName {
16- prompt : "Library A" . to_string ( ) ,
17- // TODO: This is called extension but is really a filter.
18- extension : Some ( "*.bntl" . to_string ( ) ) ,
15+ FormInputField :: DirectoryName {
16+ prompt : "Directory A" . to_string ( ) ,
1917 default : None ,
2018 value : None ,
2119 }
2220 }
2321
2422 pub fn from_form ( form : & Form ) -> Option < PathBuf > {
25- let field = form. get_field_with_name ( "Library A" ) ?;
23+ let field = form. get_field_with_name ( "Directory A" ) ?;
2624 let field_value = field. try_value_string ( ) ?;
2725 Some ( PathBuf :: from ( field_value) )
2826 }
@@ -32,17 +30,15 @@ pub struct InputFileBField;
3230
3331impl InputFileBField {
3432 pub fn field ( ) -> FormInputField {
35- FormInputField :: OpenFileName {
36- prompt : "Library B" . to_string ( ) ,
37- // TODO: This is called extension but is really a filter.
38- extension : Some ( "*.bntl" . to_string ( ) ) ,
33+ FormInputField :: DirectoryName {
34+ prompt : "Directory B" . to_string ( ) ,
3935 default : None ,
4036 value : None ,
4137 }
4238 }
4339
4440 pub fn from_form ( form : & Form ) -> Option < PathBuf > {
45- let field = form. get_field_with_name ( "Library B" ) ?;
41+ let field = form. get_field_with_name ( "Directory B" ) ?;
4642 let field_value = field. try_value_string ( ) ?;
4743 Some ( PathBuf :: from ( field_value) )
4844 }
@@ -63,30 +59,39 @@ impl Diff {
6359 let b_path = InputFileBField :: from_form ( & form) . unwrap ( ) ;
6460 let output_dir = OutputDirectoryField :: from_form ( & form) . unwrap ( ) ;
6561
66- let _bg_task = BackgroundTask :: new ( "Diffing type libraries..." , false ) . enter ( ) ;
67- let Some ( type_lib_a) = TypeLibrary :: load_from_file ( & a_path) else {
68- tracing:: error!( "Failed to load type library: {}" , a_path. display( ) ) ;
69- return ;
70- } ;
71- let Some ( type_lib_b) = TypeLibrary :: load_from_file ( & b_path) else {
72- tracing:: error!( "Failed to load type library: {}" , b_path. display( ) ) ;
73- return ;
74- } ;
62+ let bg_task = BackgroundTask :: new ( "Diffing type libraries..." , true ) . enter ( ) ;
63+
64+ let b_libraries = path_to_type_libraries ( & a_path) ;
65+ let a_libraries = path_to_type_libraries ( & b_path) ;
66+ // TODO: Make this parallel
67+ for a_lib in & a_libraries {
68+ for b_lib in & b_libraries {
69+ if bg_task. is_cancelled ( ) {
70+ return ;
71+ }
7572
76- let diff_result = match TILDiff :: new ( ) . diff ( ( & a_path, & type_lib_a) , ( & b_path, & type_lib_b) )
77- {
78- Ok ( diff_result) => diff_result,
79- Err ( err) => {
80- tracing:: error!( "Failed to diff type libraries: {}" , err) ;
81- return ;
73+ if a_lib. name ( ) != b_lib. name ( ) {
74+ continue ;
75+ }
76+
77+ bg_task. set_progress_text ( & format ! ( "Diffing '{}'..." , a_lib. name( ) ) ) ;
78+ let diff_result = match TILDiff :: new ( ) . diff_with_dependencies (
79+ ( & a_lib, a_libraries. clone ( ) ) ,
80+ ( & b_lib, b_libraries. clone ( ) ) ,
81+ ) {
82+ Ok ( diff_result) => diff_result,
83+ Err ( err) => {
84+ tracing:: error!( "Failed to diff type libraries: {}" , err) ;
85+ continue ;
86+ }
87+ } ;
88+ tracing:: info!( "Similarity Ratio: {}" , diff_result. ratio) ;
89+
90+ let output_path = output_dir. join ( a_lib. name ( ) ) . with_extension ( "diff" ) ;
91+ std:: fs:: write ( & output_path, diff_result. diff ) . unwrap ( ) ;
92+ tracing:: info!( "Diff written to: {}" , output_path. display( ) ) ;
8293 }
83- } ;
84- tracing:: info!( "Similarity Ratio: {}" , diff_result. ratio) ;
85- let output_path = output_dir
86- . join ( type_lib_a. dependency_name ( ) )
87- . with_extension ( "diff" ) ;
88- std:: fs:: write ( & output_path, diff_result. diff ) . unwrap ( ) ;
89- tracing:: info!( "Diff written to: {}" , output_path. display( ) ) ;
94+ }
9095 }
9196}
9297
0 commit comments