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,34 @@ 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+ if ( TryMatchTemplateProject ( templateModules , proj . BaseDirectory , modulePath , out var matchedModule ) )
105+ proj . Add ( matchedModule ) ;
106+ else
107+ proj . Add ( new ProjectModule { Path = modulePath } ) ;
108+ }
109+
99110 proj . OutputDirectory = outDir ;
100111 foreach ( var path in probePaths )
101112 proj . ProbePaths . Add ( path ) ;
@@ -120,6 +131,52 @@ static int Main(string[] args) {
120131 }
121132 }
122133
134+ static bool TryMatchTemplateProject ( List < ProjectModule > templateModules , string baseDirectory , string modulePath , out ProjectModule matchedModule ) {
135+ var matchedToTemplate = false ;
136+ matchedModule = null ;
137+
138+ foreach ( var templateModule in templateModules ) {
139+ var templatePath = templateModule . Path ;
140+ if ( templatePath . StartsWith ( @".\" , StringComparison . Ordinal ) )
141+ templatePath = templatePath . Substring ( 2 ) ;
142+
143+ if ( modulePath . Equals ( templatePath , StringComparison . OrdinalIgnoreCase ) )
144+ matchedToTemplate = true ;
145+
146+ if ( modulePath . Equals ( Path . Combine ( baseDirectory , templatePath ) , StringComparison . OrdinalIgnoreCase ) )
147+ matchedToTemplate = true ;
148+
149+ if ( matchedToTemplate )
150+ matchedModule = templateModule ;
151+ }
152+
153+ return matchedToTemplate ;
154+ }
155+
156+ static void LoadTemplateProject ( string templatePath , ConfuserProject proj , List < ProjectModule > templateModules ) {
157+ var templateProj = new ConfuserProject ( ) ;
158+ var xmlDoc = new XmlDocument ( ) ;
159+ xmlDoc . Load ( templatePath ) ;
160+ templateProj . Load ( xmlDoc ) ;
161+
162+ foreach ( var rule in templateProj . Rules )
163+ proj . Rules . Add ( rule ) ;
164+
165+ proj . Packer = templateProj . Packer ;
166+
167+ foreach ( string pluginPath in templateProj . PluginPaths )
168+ proj . PluginPaths . Add ( pluginPath ) ;
169+
170+ foreach ( string probePath in templateProj . ProbePaths )
171+ proj . ProbePaths . Add ( probePath ) ;
172+
173+ foreach ( var templateModule in templateProj )
174+ if ( templateModule . IsExternal )
175+ proj . Add ( templateModule ) ;
176+ else
177+ templateModules . Add ( templateModule ) ;
178+ }
179+
123180 static int RunProject ( ConfuserParameters parameters ) {
124181 var logger = new ConsoleLogger ( ) ;
125182 parameters . Logger = logger ;
0 commit comments