11using Autofac ;
22using BitMono . API . Configuration ;
33using BitMono . API . Protecting ;
4+ using BitMono . API . Protecting . Resolvers ;
45using BitMono . CLI . Modules ;
56using BitMono . Core . Configuration . Extensions ;
67using BitMono . Host ;
1415using System . Collections . Generic ;
1516using System . Diagnostics ;
1617using System . IO ;
18+ using System . Linq ;
1719using System . Reflection ;
1820using System . Threading ;
1921using System . Threading . Tasks ;
@@ -28,53 +30,73 @@ public class Program
2830
2931 private static async Task Main ( string [ ] args )
3032 {
31- var moduleFileName = await new CLIBitMonoModuleFileResolver ( args ) . ResolveAsync ( ) ;
32- if ( string . IsNullOrWhiteSpace ( moduleFileName ) )
33+ try
3334 {
34- Console . WriteLine ( "Please, specify file, drag-and-drop it in BitMono CLI" ) ;
35- Console . ReadLine ( ) ;
36- return ;
37- }
35+ var moduleFileName = await new CLIBitMonoModuleFileResolver ( args ) . ResolveAsync ( ) ;
36+ if ( string . IsNullOrWhiteSpace ( moduleFileName ) )
37+ {
38+ Console . WriteLine ( "Please, specify file, drag-and-drop it in BitMono CLI" ) ;
39+ Console . ReadLine ( ) ;
40+ return ;
41+ }
3842
39- var domainBaseDirectory = AppDomain . CurrentDomain . BaseDirectory ;
40- var protectionsFile = Path . Combine ( domainBaseDirectory , Protections ) ;
41- var externalComponentsFile = Path . Combine ( domainBaseDirectory , ExternalComponents ) ;
42- var externalComponentsModuleDefMD = ModuleDefMD . Load ( externalComponentsFile ) ;
43- Assembly . LoadFrom ( protectionsFile ) ;
43+ var domainBaseDirectory = AppDomain . CurrentDomain . BaseDirectory ;
44+ var protectionsFile = Path . Combine ( domainBaseDirectory , Protections ) ;
45+ var externalComponentsFile = Path . Combine ( domainBaseDirectory , ExternalComponents ) ;
46+ var externalComponentsModuleDefMD = ModuleDefMD . Load ( externalComponentsFile ) ;
47+ Assembly . LoadFrom ( protectionsFile ) ;
4448
45- var moduleFileBaseDirectory = Path . GetDirectoryName ( moduleFileName ) ;
46- var libsDirectoryName = Path . Combine ( moduleFileBaseDirectory , "libs" ) ;
47- var outputDirectoryName = Path . Combine ( moduleFileBaseDirectory , "output" ) ;
48- Directory . CreateDirectory ( libsDirectoryName ) ;
49- Directory . CreateDirectory ( outputDirectoryName ) ;
49+ var moduleFileBaseDirectory = Path . GetDirectoryName ( moduleFileName ) ;
50+ var dependenciesDirectoryName = Path . Combine ( moduleFileBaseDirectory , "libs" ) ;
51+ var outputDirectoryName = Path . Combine ( moduleFileBaseDirectory , "output" ) ;
52+ Directory . CreateDirectory ( dependenciesDirectoryName ) ;
53+ Directory . CreateDirectory ( outputDirectoryName ) ;
5054
51- var serviceProvider = new BitMonoApplication ( ) . RegisterModule ( new BitMonoModule ( configureLogger =>
52- {
53- configureLogger . WriteTo . Async ( configure => configure . Console (
54- outputTemplate : "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}][{SourceContext}] {Message:lj}{NewLine}{Exception}" ) ) ;
55- } ) ) . Build ( ) ;
55+ var serviceProvider = new BitMonoApplication ( ) . RegisterModule ( new BitMonoModule ( configureLogger =>
56+ {
57+ configureLogger . WriteTo . Async ( configure => configure . Console (
58+ outputTemplate : "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}][{SourceContext}] {Message:lj}{NewLine}{Exception}" ) ) ;
59+ } ) ) . Build ( ) ;
5660
57- var obfuscationConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoObfuscationConfiguration > ( ) ;
58- var protectionsConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoProtectionsConfiguration > ( ) ;
59- var appSettingsConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoAppSettingsConfiguration > ( ) ;
60- var bitMonoContext = await new BitMonoContextCreator ( obfuscationConfiguration ) . CreateAsync ( outputDirectoryName , libsDirectoryName ) ;
61- bitMonoContext . ModuleFileName = moduleFileName ;
61+ var obfuscationConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoObfuscationConfiguration > ( ) ;
62+ var protectionsConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoProtectionsConfiguration > ( ) ;
63+ var appSettingsConfiguration = serviceProvider . LifetimeScope . Resolve < IBitMonoAppSettingsConfiguration > ( ) ;
64+ var dnlibDefFeatureObfuscationAttributeHavingResolver = serviceProvider . LifetimeScope . Resolve < IDnlibDefFeatureObfuscationAttributeHavingResolver > ( ) ;
65+ var dependencies = Directory . GetFiles ( dependenciesDirectoryName ) ;
66+ var dependeciesData = new List < byte [ ] > ( ) ;
67+ for ( int i = 0 ; i < dependencies . Length ; i ++ )
68+ {
69+ dependeciesData . Add ( File . ReadAllBytes ( dependencies [ i ] ) ) ;
70+ }
71+ var bitMonoContext = await new BitMonoContextCreator ( obfuscationConfiguration ) . CreateAsync ( outputDirectoryName , dependeciesData ) ;
72+ bitMonoContext . ModuleFileName = moduleFileName ;
6273
63- var protections = serviceProvider . LifetimeScope . Resolve < ICollection < IProtection > > ( ) ;
64- var moduleFileBytes = File . ReadAllBytes ( bitMonoContext . ModuleFileName ) ;
65- var logger = serviceProvider . LifetimeScope . Resolve < ILogger > ( ) . ForContext < Program > ( ) ;
66- var protectionSettings = protectionsConfiguration . GetProtectionSettings ( ) ;
67- await new BitMonoEngine ( serviceProvider , new CLIModuleDefMDWriter ( ) , new ModuleDefMDCreator ( moduleFileBytes ) , logger )
68- . ObfuscateAsync ( bitMonoContext , externalComponentsModuleDefMD , protections , protectionSettings , CancellationToken . Token ) ;
74+ var protections = serviceProvider . LifetimeScope . Resolve < ICollection < IProtection > > ( ) . ToList ( ) ;
75+ var moduleFileBytes = File . ReadAllBytes ( bitMonoContext . ModuleFileName ) ;
76+ var logger = serviceProvider . LifetimeScope . Resolve < ILogger > ( ) . ForContext < Program > ( ) ;
77+ var protectionSettings = protectionsConfiguration . GetProtectionSettings ( ) ;
78+ await new BitMonoEngine (
79+ new CLIModuleDefMDWriter ( ) ,
80+ new ModuleDefMDCreator ( moduleFileBytes ) ,
81+ dnlibDefFeatureObfuscationAttributeHavingResolver ,
82+ obfuscationConfiguration ,
83+ logger )
84+ . ObfuscateAsync ( bitMonoContext , externalComponentsModuleDefMD , protections , protectionSettings , CancellationToken . Token ) ;
6985
70- if ( obfuscationConfiguration . Configuration . GetValue < bool > ( nameof ( Obfuscation . OpenFileDestinationInFileExplorer ) ) )
71- {
72- Process . Start ( bitMonoContext . OutputPath ) ;
73- }
86+ if ( obfuscationConfiguration . Configuration . GetValue < bool > ( nameof ( Obfuscation . OpenFileDestinationInFileExplorer ) ) )
87+ {
88+ Process . Start ( bitMonoContext . OutputPath ) ;
89+ }
7490
75- await new TipsNotifier ( appSettingsConfiguration , logger ) . NotifyAsync ( ) ;
91+ await new TipsNotifier ( appSettingsConfiguration , logger ) . NotifyAsync ( ) ;
7692
77- await serviceProvider . DisposeAsync ( ) ;
93+ await serviceProvider . DisposeAsync ( ) ;
94+ }
95+ catch ( Exception ex )
96+ {
97+ Console . WriteLine ( "Something went wrong! " + ex . ToString ( ) ) ;
98+ }
99+
78100 Console . ReadLine ( ) ;
79101 }
80102}
0 commit comments