forked from mkaring/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConfuserProject.cs
More file actions
741 lines (638 loc) · 22.2 KB
/
Copy pathConfuserProject.cs
File metadata and controls
741 lines (638 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using dnlib.DotNet;
namespace Confuser.Core.Project {
/// <summary>
/// A module description in a Confuser project.
/// </summary>
public class ProjectModule {
/// <summary>
/// Initializes a new instance of the <see cref="ProjectModule" /> class.
/// </summary>
public ProjectModule() {
Rules = new List<Rule>();
}
/// <summary>
/// Gets the path to the module.
/// </summary>
public string Path { get; set; }
/// <summary>
/// Indicates whether this module is external and should not be obfuscated.
/// </summary>
public bool IsExternal { get; set; }
/// <summary>
/// Gets or sets the path to the strong name private key for signing.
/// </summary>
/// <value>The path to the strong name private key, or null if not necessary.</value>
/// <remarks>This is also used as the identity key when doing strong name signing.</remarks>
public string SNKeyPath { get; set; }
/// <summary>
/// Gets or sets the password of the strong name private key.
/// </summary>
/// <value>The password of the strong name private key, or null if not necessary.</value>
/// <remarks>This is the password for the key in <see cref="SNKeyPath"/></remarks>
public string SNKeyPassword { get; set; }
/// <summary>
/// Gets or sets if the generated assembly should be delayed signed.
/// </summary>
public bool SNDelaySig { get; set; }
/// <summary>
/// Gets or sets the path to the strong name public key for signing.
/// </summary>
/// <value>The path to the strong name public key, or null if not necessary.</value>
/// <remarks>
/// This is only used in enhanced strong name signing and is the public part of the identity key.
/// The private part of the key
/// </remarks>
public string SNPubKeyPath { get; set; }
/// <summary>
/// Gets or sets the path to the strong name private key used for enhanced signing.
/// </summary>
/// <value>The path to the strong name private key used for enhanced signing, or null if not necessary.</value>
public string SNSigKeyPath { get; set; }
/// <summary>
/// Gets or sets the password of the strong name private key.
/// </summary>
/// <value>The password of the strong name private key, or null if not necessary.</value>
public string SNSigKeyPassword { get; set; }
/// <summary>
/// Gets or sets the path to the strong name public key used for enhanced signing.
/// </summary>
/// <value>The path to the strong name public key used for enhanced signing, or null if not necessary.</value>
public string SNPubSigKeyPath { get; set; }
/// <summary>
/// Gets a list of protection rules applies to the module.
/// </summary>
/// <value>A list of protection rules.</value>
public IList<Rule> Rules { get; private set; }
/// <summary>
/// Resolves the module from the path.
/// </summary>
/// <param name="basePath">
/// The base path for the relative module path,
/// or null if the module path is absolute or relative to current directory.
/// </param>
/// <param name="context">The resolved module's context.</param>
/// <returns>The resolved module.</returns>
public ModuleDefMD Resolve(string basePath, ModuleContext context = null) {
if (basePath == null)
return ModuleDefMD.Load(Path, context);
return ModuleDefMD.Load(System.IO.Path.Combine(basePath, Path), context);
}
/// <summary>
/// Read the raw bytes of the module from the path.
/// </summary>
/// <param name="basePath">
/// The base path for the relative module path,
/// or null if the module path is absolute or relative to current directory.
/// </param>
/// <returns>The loaded module.</returns>
public byte[] LoadRaw(string basePath) {
if (basePath == null)
return File.ReadAllBytes(Path);
return File.ReadAllBytes(System.IO.Path.Combine(basePath, Path));
}
/// <summary>
/// Saves the module description as XML element.
/// </summary>
/// <param name="xmlDoc">The root XML document.</param>
/// <returns>The serialized module description.</returns>
internal XmlElement Save(XmlDocument xmlDoc) {
XmlElement elem = xmlDoc.CreateElement("module", ConfuserProject.Namespace);
XmlAttribute nameAttr = xmlDoc.CreateAttribute("path");
nameAttr.Value = Path;
elem.Attributes.Append(nameAttr);
if (IsExternal) {
XmlAttribute extAttr = xmlDoc.CreateAttribute("external");
extAttr.Value = IsExternal ? "true" : "false";
elem.Attributes.Append(extAttr);
}
if (SNKeyPath != null) {
XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snKey");
snKeyAttr.Value = SNKeyPath;
elem.Attributes.Append(snKeyAttr);
}
if (SNKeyPassword != null) {
XmlAttribute snKeyPassAttr = xmlDoc.CreateAttribute("snKeyPass");
snKeyPassAttr.Value = SNKeyPassword;
elem.Attributes.Append(snKeyPassAttr);
}
if (SNDelaySig) {
XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snDelaySig");
snKeyAttr.Value = SNDelaySig ? "true" : "false";
elem.Attributes.Append(snKeyAttr);
}
if (SNPubKeyPath != null) {
XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snPubKey");
snKeyAttr.Value = SNPubKeyPath;
elem.Attributes.Append(snKeyAttr);
}
if (SNSigKeyPath != null) {
XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snSigKey");
snKeyAttr.Value = SNSigKeyPath;
elem.Attributes.Append(snKeyAttr);
}
if (SNSigKeyPassword != null) {
XmlAttribute snKeyPassAttr = xmlDoc.CreateAttribute("snSigKeyPass");
snKeyPassAttr.Value = SNSigKeyPassword;
elem.Attributes.Append(snKeyPassAttr);
}
if (SNPubSigKeyPath != null) {
XmlAttribute snKeyAttr = xmlDoc.CreateAttribute("snPubSigKey");
snKeyAttr.Value = SNPubSigKeyPath;
elem.Attributes.Append(snKeyAttr);
}
foreach (Rule i in Rules)
elem.AppendChild(i.Save(xmlDoc));
return elem;
}
/// <summary>
/// Loads the module description from XML element.
/// </summary>
/// <param name="elem">The serialized module description.</param>
internal void Load(XmlElement elem) {
Path = elem.Attributes["path"].Value;
if (elem.Attributes["external"] != null)
IsExternal = bool.Parse(elem.Attributes["external"].Value);
else
IsExternal = false;
if (elem.Attributes["snKey"] != null)
SNKeyPath = elem.Attributes["snKey"].Value.NullIfEmpty();
else
SNKeyPath = null;
if (elem.Attributes["snKeyPass"] != null)
SNKeyPassword = elem.Attributes["snKeyPass"].Value.NullIfEmpty();
else
SNKeyPassword = null;
bool delaySig = false;
if (elem.Attributes["snDelaySig"] != null)
bool.TryParse(elem.Attributes["snDelaySig"].Value, out delaySig);
SNDelaySig = delaySig;
if (elem.Attributes["snPubKey"] != null)
SNPubKeyPath = elem.Attributes["snPubKey"].Value.NullIfEmpty();
else
SNPubKeyPath = null;
if (elem.Attributes["snSigKey"] != null)
SNSigKeyPath = elem.Attributes["snSigKey"].Value.NullIfEmpty();
else
SNSigKeyPath = null;
if (elem.Attributes["snSigKeyPass"] != null)
SNSigKeyPassword = elem.Attributes["snSigKeyPass"].Value.NullIfEmpty();
else
SNSigKeyPassword = null;
if (elem.Attributes["snPubSigKey"] != null)
SNPubSigKeyPath = elem.Attributes["snPubSigKey"].Value.NullIfEmpty();
else
SNPubSigKeyPath = null;
Rules.Clear();
foreach (XmlElement i in elem.ChildNodes.OfType<XmlElement>()) {
var rule = new Rule();
rule.Load(i);
Rules.Add(rule);
}
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="string" /> that represents this instance.</returns>
public override string ToString() {
return Path;
}
/// <summary>
/// Clones this instance.
/// </summary>
/// <returns>A duplicated module.</returns>
public ProjectModule Clone() {
var ret = new ProjectModule();
ret.Path = Path;
ret.IsExternal = IsExternal;
ret.SNKeyPath = SNKeyPath;
ret.SNPubKeyPath = SNPubKeyPath;
ret.SNDelaySig = SNDelaySig;
ret.SNPubSigKeyPath = SNPubSigKeyPath;
ret.SNSigKeyPath = SNSigKeyPath;
ret.SNKeyPassword = SNKeyPassword;
ret.SNSigKeyPassword = SNSigKeyPassword;
foreach (var r in Rules)
ret.Rules.Add(r.Clone());
return ret;
}
}
/// <summary>
/// Indicates add or remove the protection from the active protections
/// </summary>
public enum SettingItemAction {
/// <summary>
/// Add the protection to the active protections
/// </summary>
Add,
/// <summary>
/// Remove the protection from the active protections
/// </summary>
Remove
}
/// <summary>
/// A <see cref="ConfuserComponent" /> setting within a rule.
/// </summary>
/// <typeparam name="T"><see cref="Protection" /> or <see cref="Packer" /></typeparam>
public class SettingItem<T> : Dictionary<string, string> {
/// <summary>
/// Initialize this setting item instance
/// </summary>
/// <param name="id">The protection id</param>
/// <param name="action">The action to take</param>
public SettingItem(string id = null, SettingItemAction action = SettingItemAction.Add) {
Id = id;
Action = action;
}
/// <summary>
/// The identifier of component
/// </summary>
/// <value>The identifier of component.</value>
/// <seealso cref="ConfuserComponent.Id" />
public string Id { get; set; }
/// <summary>
/// Gets or sets the action of component.
/// </summary>
/// <value>The action of component.</value>
public SettingItemAction Action { get; set; }
/// <summary>
/// Saves the setting description as XML element.
/// </summary>
/// <param name="xmlDoc">The root XML document.</param>
/// <returns>The setting module description.</returns>
internal XmlElement Save(XmlDocument xmlDoc) {
XmlElement elem = xmlDoc.CreateElement(typeof(T) == typeof(Packer) ? "packer" : "protection", ConfuserProject.Namespace);
XmlAttribute idAttr = xmlDoc.CreateAttribute("id");
idAttr.Value = Id;
elem.Attributes.Append(idAttr);
if (Action != SettingItemAction.Add) {
XmlAttribute pAttr = xmlDoc.CreateAttribute("action");
pAttr.Value = Action.ToString().ToLower();
elem.Attributes.Append(pAttr);
}
foreach (var i in this) {
XmlElement arg = xmlDoc.CreateElement("argument", ConfuserProject.Namespace);
XmlAttribute nameAttr = xmlDoc.CreateAttribute("name");
nameAttr.Value = i.Key;
arg.Attributes.Append(nameAttr);
XmlAttribute valAttr = xmlDoc.CreateAttribute("value");
valAttr.Value = i.Value;
arg.Attributes.Append(valAttr);
elem.AppendChild(arg);
}
return elem;
}
/// <summary>
/// Loads the setting description from XML element.
/// </summary>
/// <param name="elem">The serialized setting description.</param>
internal void Load(XmlElement elem) {
Id = elem.Attributes["id"].Value;
if (elem.Attributes["action"] != null)
Action = (SettingItemAction)Enum.Parse(typeof(SettingItemAction), elem.Attributes["action"].Value, true);
else
Action = SettingItemAction.Add;
Clear();
foreach (XmlElement i in elem.ChildNodes.OfType<XmlElement>())
Add(i.Attributes["name"].Value, i.Attributes["value"].Value);
}
/// <summary>
/// Clones this instance.
/// </summary>
/// <returns>A duplicated setting item.</returns>
public SettingItem<T> Clone() {
var item = new SettingItem<T>(Id, Action);
foreach (var entry in this)
item.Add(entry.Key, entry.Value);
return item;
}
}
/// <summary>
/// A rule that control how <see cref="Protection" />s are applied to module
/// </summary>
public class Rule : List<SettingItem<Protection>> {
/// <summary>
/// Initialize this rule instance
/// </summary>
/// <param name="pattern">The pattern</param>
/// <param name="preset">The preset</param>
/// <param name="inherit">Inherits protection</param>
public Rule(string pattern = "true", ProtectionPreset preset = ProtectionPreset.None, bool inherit = false) {
Pattern = pattern;
Preset = preset;
Inherit = inherit;
}
/// <summary>
/// Gets or sets the pattern that determine the target components of the rule.
/// </summary>
/// <value>The pattern expression.</value>
public string Pattern { get; set; }
/// <summary>
/// Gets or sets the protection preset this rule uses.
/// </summary>
/// <value>The protection preset.</value>
public ProtectionPreset Preset { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Rule" /> inherits settings from earlier rules.
/// </summary>
/// <value><c>true</c> if it inherits settings; otherwise, <c>false</c>.</value>
public bool Inherit { get; set; }
/// <summary>
/// Saves the rule description as XML element.
/// </summary>
/// <param name="xmlDoc">The root XML document.</param>
/// <returns>The serialized rule description.</returns>
internal XmlElement Save(XmlDocument xmlDoc) {
XmlElement elem = xmlDoc.CreateElement("rule", ConfuserProject.Namespace);
XmlAttribute ruleAttr = xmlDoc.CreateAttribute("pattern");
ruleAttr.Value = Pattern;
elem.Attributes.Append(ruleAttr);
if (Preset != ProtectionPreset.None) {
XmlAttribute pAttr = xmlDoc.CreateAttribute("preset");
pAttr.Value = Preset.ToString().ToLower();
elem.Attributes.Append(pAttr);
}
if (Inherit != true) {
XmlAttribute attr = xmlDoc.CreateAttribute("inherit");
attr.Value = Inherit.ToString().ToLower();
elem.Attributes.Append(attr);
}
foreach (var i in this)
elem.AppendChild(i.Save(xmlDoc));
return elem;
}
/// <summary>
/// Loads the rule description from XML element.
/// </summary>
/// <param name="elem">The serialized module description.</param>
internal void Load(XmlElement elem) {
Pattern = elem.Attributes["pattern"].Value;
if (elem.Attributes["preset"] != null)
Preset = (ProtectionPreset)Enum.Parse(typeof(ProtectionPreset), elem.Attributes["preset"].Value, true);
else
Preset = ProtectionPreset.None;
if (elem.Attributes["inherit"] != null)
Inherit = bool.Parse(elem.Attributes["inherit"].Value);
else
Inherit = true;
Clear();
foreach (XmlElement i in elem.ChildNodes.OfType<XmlElement>()) {
var x = new SettingItem<Protection>();
x.Load(i);
Add(x);
}
}
/// <summary>
/// Clones this instance.
/// </summary>
/// <returns>A duplicated rule.</returns>
public Rule Clone() {
var ret = new Rule();
ret.Preset = Preset;
ret.Pattern = Pattern;
ret.Inherit = Inherit;
foreach (var i in this) {
var item = new SettingItem<Protection>();
item.Id = i.Id;
item.Action = i.Action;
foreach (string j in i.Keys)
item.Add(j, i[j]);
ret.Add(item);
}
return ret;
}
}
/// <summary>
/// The exception that is thrown when there exists schema errors in the project XML.
/// </summary>
public class ProjectValidationException : Exception {
/// <summary>
/// Initializes a new instance of the <see cref="ProjectValidationException" /> class.
/// </summary>
/// <param name="exceptions">The list of schema exceptions.</param>
internal ProjectValidationException(List<XmlSchemaException> exceptions)
: base(exceptions[0].Message) {
Errors = exceptions;
}
/// <summary>
/// Gets the schema exceptions.
/// </summary>
/// <value>A list of schema exceptions.</value>
public IList<XmlSchemaException> Errors { get; private set; }
}
/// <summary>
/// Represent a project of Confuser.
/// </summary>
public class ConfuserProject : List<ProjectModule> {
/// <summary>
/// The namespace of Confuser project schema
/// </summary>
public const string Namespace = "http://confuser.codeplex.com";
/// <summary>
/// The schema of project XML.
/// </summary>
public static readonly XmlSchema Schema = XmlSchema.Read(typeof(ConfuserProject).Assembly.GetManifestResourceStream("Confuser.Core.Project.ConfuserPrj.xsd"), null);
/// <summary>
/// Initializes a new instance of the <see cref="ConfuserProject" /> class.
/// </summary>
public ConfuserProject() {
ProbePaths = new List<string>();
PluginPaths = new List<string>();
Rules = new List<Rule>();
}
/// <summary>
/// Gets or sets the seed of pseudo-random generator used in process of protection.
/// </summary>
/// <value>The random seed.</value>
public string Seed { get; set; }
/// <summary>
/// Gets or sets a value indicating whether debug symbols are generated.
/// </summary>
/// <value><c>true</c> if debug symbols are generated; otherwise, <c>false</c>.</value>
public bool Debug { get; set; }
/// <summary>
/// Gets or sets the output directory.
/// </summary>
/// <value>The output directory.</value>
public string OutputDirectory { get; set; }
/// <summary>
/// Gets or sets the base directory of the project.
/// </summary>
/// <value>The base directory.</value>
public string BaseDirectory { get; set; }
/// <summary>
/// Gets a list of protection rules that applies globally.
/// </summary>
/// <value>A list of protection rules.</value>
public IList<Rule> Rules { get; private set; }
/// <summary>
/// Gets or sets the packer used to pack up the output.
/// </summary>
/// <value>The packer.</value>
public SettingItem<Packer> Packer { get; set; }
/// <summary>
/// Gets a list of paths that used to resolve assemblies.
/// </summary>
/// <value>The list of paths.</value>
public IList<string> ProbePaths { get; private set; }
/// <summary>
/// Gets a list of paths to plugin.
/// </summary>
/// <value>The list of plugins.</value>
public IList<string> PluginPaths { get; private set; }
/// <summary>
/// Saves the project as XML document.
/// </summary>
/// <returns>The serialized project XML.</returns>
public XmlDocument Save() {
var xmlDoc = new XmlDocument();
xmlDoc.Schemas.Add(Schema);
XmlElement elem = xmlDoc.CreateElement("project", Namespace);
XmlAttribute outputAttr = xmlDoc.CreateAttribute("outputDir");
outputAttr.Value = OutputDirectory;
elem.Attributes.Append(outputAttr);
XmlAttribute baseAttr = xmlDoc.CreateAttribute("baseDir");
baseAttr.Value = BaseDirectory;
elem.Attributes.Append(baseAttr);
if (Seed != null) {
XmlAttribute seedAttr = xmlDoc.CreateAttribute("seed");
seedAttr.Value = Seed;
elem.Attributes.Append(seedAttr);
}
if (Debug) {
XmlAttribute debugAttr = xmlDoc.CreateAttribute("debug");
debugAttr.Value = Debug.ToString().ToLower();
elem.Attributes.Append(debugAttr);
}
foreach (Rule i in Rules)
elem.AppendChild(i.Save(xmlDoc));
if (Packer != null)
elem.AppendChild(Packer.Save(xmlDoc));
foreach (ProjectModule i in this)
elem.AppendChild(i.Save(xmlDoc));
foreach (string i in ProbePaths) {
XmlElement path = xmlDoc.CreateElement("probePath", Namespace);
path.InnerText = i;
elem.AppendChild(path);
}
foreach (string i in PluginPaths) {
XmlElement path = xmlDoc.CreateElement("plugin", Namespace);
path.InnerText = i;
elem.AppendChild(path);
}
xmlDoc.AppendChild(elem);
return xmlDoc;
}
/// <summary>
/// Loads the project from specified XML document.
/// </summary>
/// <param name="doc">The XML document storing the project.</param>
/// <exception cref="Confuser.Core.Project.ProjectValidationException">
/// The project XML contains schema errors.
/// </exception>
public void Load(XmlDocument doc, string baseDirRoot = null) {
doc.Schemas.Add(Schema);
var exceptions = new List<XmlSchemaException>();
doc.Validate((sender, e) => {
if (e.Severity != XmlSeverityType.Error) return;
exceptions.Add(e.Exception);
});
if (exceptions.Count > 0) {
throw new ProjectValidationException(exceptions);
}
XmlElement docElem = doc.DocumentElement;
OutputDirectory = docElem.Attributes["outputDir"].Value;
BaseDirectory = docElem.Attributes["baseDir"].Value;
if (!string.IsNullOrEmpty(baseDirRoot))
{
BaseDirectory = Path.Combine(baseDirRoot, BaseDirectory);
}
if (docElem.Attributes["seed"] != null)
Seed = docElem.Attributes["seed"].Value.NullIfEmpty();
else
Seed = null;
if (docElem.Attributes["debug"] != null)
Debug = bool.Parse(docElem.Attributes["debug"].Value);
else
Debug = false;
Packer = null;
Clear();
ProbePaths.Clear();
PluginPaths.Clear();
Rules.Clear();
foreach (XmlElement i in docElem.ChildNodes.OfType<XmlElement>()) {
if (i.Name == "rule") {
var rule = new Rule();
rule.Load(i);
Rules.Add(rule);
}
else if (i.Name == "packer") {
Packer = new SettingItem<Packer>();
Packer.Load(i);
}
else if (i.Name == "probePath") {
ProbePaths.Add(i.InnerText);
}
else if (i.Name == "plugin") {
PluginPaths.Add(i.InnerText);
}
else {
AddModule(i);
}
}
}
internal void AddModule(XmlElement elem) {
if (IsWildcard(elem.Attributes["path"].Value)) {
BatchLoadModules(elem);
}
else {
var asm = new ProjectModule();
asm.Load(elem);
Add(asm);
}
}
internal bool IsWildcard(string path) {
return !string.IsNullOrEmpty(path) && path.Contains(@"*");
}
internal bool BatchLoadModules(XmlElement elem) {
string wildCardPath = elem.Attributes["path"].Value;
string[] files = Directory.GetFiles(BaseDirectory, wildCardPath, SearchOption.TopDirectoryOnly);
if (files.Length <= 0)
{
return false;
}
var asmPrototype = new ProjectModule();
asmPrototype.Load(elem);
foreach (string fileName in files) {
var moduleEntry = asmPrototype.Clone();
moduleEntry.Path = fileName;
Add(moduleEntry);
}
return true;
}
/// <summary>
/// Clones this instance.
/// </summary>
/// <returns>A duplicated project.</returns>
public ConfuserProject Clone() {
var ret = new ConfuserProject();
ret.Seed = Seed;
ret.Debug = Debug;
ret.OutputDirectory = OutputDirectory;
ret.BaseDirectory = BaseDirectory;
ret.Packer = Packer == null ? null : Packer.Clone();
ret.ProbePaths = new List<string>(ProbePaths);
ret.PluginPaths = new List<string>(PluginPaths);
foreach (var module in this)
ret.Add(module.Clone());
foreach (var r in Rules)
ret.Rules.Add(r);
return ret;
}
}
}