Skip to content

Commit e430a55

Browse files
committed
Clarify semantics of ObfuscationAttribute marking
If a pattern exists: it will be evaluated at the members indivdually if ApplyToMember. If a pattern does not exist: it will apply at the item and the members would inherit them if ApplyToMember.
1 parent 9020740 commit e430a55

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

Confuser.Core/ObfAttrMarker.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class ProtectionSettingsStack {
3838
readonly ObfAttrParser parser;
3939
ProtectionSettings settings;
4040

41+
enum ApplyInfoType {
42+
CurrentInfoOnly,
43+
CurrentInfoInherits,
44+
ParentInfo
45+
}
46+
4147
class PopHolder : IDisposable {
4248
ProtectionSettingsStack parent;
4349

@@ -76,21 +82,20 @@ public IDisposable Apply(IDnlibDef target, IEnumerable<ProtectionSettingsInfo> i
7682
var infoArray = infos.ToArray();
7783

7884
if (stack.Count > 0) {
79-
foreach (var i in stack.Skip(1).Reverse())
80-
ApplyInfo(target, settings, i.Item2.Where(info => info.Condition != null), false);
81-
ApplyInfo(target, settings, stack.Peek().Item2, false);
85+
foreach (var i in stack.Reverse())
86+
ApplyInfo(target, settings, i.Item2, ApplyInfoType.ParentInfo);
8287
}
8388

8489
IDisposable result;
8590
if (infoArray.Length != 0) {
8691
var originalSettings = this.settings;
8792

8893
// the settings that would apply to members
89-
ApplyInfo(target, settings, infoArray, false);
94+
ApplyInfo(target, settings, infoArray, ApplyInfoType.CurrentInfoInherits);
9095
this.settings = new ProtectionSettings(settings);
9196

9297
// the settings that would apply to itself
93-
ApplyInfo(target, settings, infoArray, true);
98+
ApplyInfo(target, settings, infoArray, ApplyInfoType.CurrentInfoOnly);
9499
stack.Push(Tuple.Create(originalSettings, infoArray));
95100

96101
result = new PopHolder(this);
@@ -102,21 +107,23 @@ public IDisposable Apply(IDnlibDef target, IEnumerable<ProtectionSettingsInfo> i
102107
return result;
103108
}
104109

105-
void ApplyInfo(IDnlibDef context, ProtectionSettings settings, IEnumerable<ProtectionSettingsInfo> infos, bool current) {
110+
void ApplyInfo(IDnlibDef context, ProtectionSettings settings, IEnumerable<ProtectionSettingsInfo> infos, ApplyInfoType type) {
106111
foreach (var info in infos) {
107112
if (info.Condition != null && !(bool)info.Condition.Evaluate(context))
108113
continue;
109114

110-
if (info.Exclude) {
111-
if (current)
112-
settings.Clear();
113-
else if (info.ApplyToMember)
115+
if (info.Condition == null && info.Exclude) {
116+
if (type == ApplyInfoType.CurrentInfoOnly ||
117+
(type == ApplyInfoType.CurrentInfoInherits && info.ApplyToMember)) {
114118
settings.Clear();
115-
continue;
119+
}
116120
}
117-
118-
if ((info.ApplyToMember || current || info.Condition != null) && !string.IsNullOrEmpty(info.Settings)) {
119-
parser.ParseProtectionString(settings, info.Settings);
121+
if (!string.IsNullOrEmpty(info.Settings)) {
122+
if ((type == ApplyInfoType.ParentInfo && info.Condition != null && info.ApplyToMember) ||
123+
type == ApplyInfoType.CurrentInfoOnly ||
124+
(type == ApplyInfoType.CurrentInfoInherits && info.Condition == null && info.ApplyToMember)) {
125+
parser.ParseProtectionString(settings, info.Settings);
126+
}
120127
}
121128
}
122129
}
@@ -232,7 +239,7 @@ ProtectionSettingsInfo ToInfo(Rule rule, PatternExpression expr) {
232239
info.Condition = expr;
233240

234241
info.Exclude = false;
235-
info.ApplyToMember = false;
242+
info.ApplyToMember = true;
236243

237244
var settings = new StringBuilder();
238245
foreach (var item in rule) {

0 commit comments

Comments
 (0)