22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . IO ;
5+ using System . Linq ;
56using System . Xml ;
67using Confuser . Core ;
78using Confuser . Core . Project ;
@@ -78,24 +79,48 @@ static int Main(string[] args) {
7879 }
7980
8081 var proj = new ConfuserProject ( ) ;
82+ var templateModules = new List < ProjectModule > ( ) ;
8183
8284 if ( Path . GetExtension ( files [ files . Count - 1 ] ) == ".crproj" ) {
83- var templateProj = new ConfuserProject ( ) ;
84- var xmlDoc = new XmlDocument ( ) ;
85- xmlDoc . Load ( files [ files . Count - 1 ] ) ;
86- templateProj . Load ( xmlDoc ) ;
85+ LoadTemplateProject ( files [ files . Count - 1 ] , proj , templateModules ) ;
8786 files . RemoveAt ( files . Count - 1 ) ;
88-
89- foreach ( var rule in templateProj . Rules )
90- proj . Rules . Add ( rule ) ;
9187 }
9288
9389 // Generate a ConfuserProject for input modules
9490 // Assuming first file = main module
95- foreach ( var input in files )
96- proj . Add ( new ProjectModule { Path = input } ) ;
97-
9891 proj . BaseDirectory = Path . GetDirectoryName ( files [ 0 ] ) ;
92+ if ( string . IsNullOrWhiteSpace ( proj . BaseDirectory ) ) {
93+ WriteLineWithColor ( ConsoleColor . Red , "Failed to identify base directory for main assembly." ) ;
94+ PrintUsage ( ) ;
95+ return - 1 ;
96+ }
97+
98+ foreach ( var input in files ) {
99+ string modulePath = input ;
100+ if ( modulePath . StartsWith ( proj . BaseDirectory , StringComparison . OrdinalIgnoreCase ) ) {
101+ modulePath = modulePath . Substring ( proj . BaseDirectory . Length + 1 ) ;
102+ }
103+
104+ var matchedToTemplate = false ;
105+ foreach ( var templateModule in templateModules ) {
106+ var templatePath = templateModule . Path ;
107+ if ( templatePath . StartsWith ( @".\" , StringComparison . Ordinal ) )
108+ templatePath = templatePath . Substring ( 2 ) ;
109+
110+ if ( modulePath . Equals ( templatePath , StringComparison . OrdinalIgnoreCase ) )
111+ matchedToTemplate = true ;
112+
113+ if ( modulePath . Equals ( Path . Combine ( proj . BaseDirectory , templatePath ) , StringComparison . OrdinalIgnoreCase ) )
114+ matchedToTemplate = true ;
115+
116+ if ( matchedToTemplate )
117+ proj . Add ( templateModule ) ;
118+ }
119+
120+ if ( ! matchedToTemplate )
121+ proj . Add ( new ProjectModule { Path = modulePath } ) ;
122+ }
123+
99124 proj . OutputDirectory = outDir ;
100125 foreach ( var path in probePaths )
101126 proj . ProbePaths . Add ( path ) ;
@@ -120,6 +145,30 @@ static int Main(string[] args) {
120145 }
121146 }
122147
148+ static void LoadTemplateProject ( string templatePath , ConfuserProject proj , List < ProjectModule > templateModules ) {
149+ var templateProj = new ConfuserProject ( ) ;
150+ var xmlDoc = new XmlDocument ( ) ;
151+ xmlDoc . Load ( templatePath ) ;
152+ templateProj . Load ( xmlDoc ) ;
153+
154+ foreach ( var rule in templateProj . Rules )
155+ proj . Rules . Add ( rule ) ;
156+
157+ proj . Packer = templateProj . Packer ;
158+
159+ foreach ( string pluginPath in templateProj . PluginPaths )
160+ proj . PluginPaths . Add ( pluginPath ) ;
161+
162+ foreach ( string probePath in templateProj . ProbePaths )
163+ proj . ProbePaths . Add ( probePath ) ;
164+
165+ foreach ( var templateModule in templateProj )
166+ if ( templateModule . IsExternal )
167+ proj . Add ( templateModule ) ;
168+ else
169+ templateModules . Add ( templateModule ) ;
170+ }
171+
123172 static int RunProject ( ConfuserParameters parameters ) {
124173 var logger = new ConsoleLogger ( ) ;
125174 parameters . Logger = logger ;
0 commit comments