Skip to content

Commit 8e02f61

Browse files
committed
完善 customattributes.md文档
1 parent 7a453ce commit 8e02f61

5 files changed

Lines changed: 278 additions & 117 deletions

File tree

docs/manual/customattributes.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# Obfuz CustomAttributes
2+
3+
Obfuz提供了多种`CustomAttribute`,方便直接在代码中配置混淆规则。目前支持的CustomAttribute有:
4+
5+
- ObfuzIgnoreAttribute
6+
- EncryptFieldAttribute
7+
8+
## ObfuzIgnoreAttribute
9+
10+
### 定义
11+
12+
ObfuzIgnoreAttribute的代码实现如下:
13+
14+
```csharp
15+
16+
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
17+
public class ObfuzIgnoreAttribute : Attribute
18+
{
19+
public ObfuzScope Scope { get; set; }
20+
21+
public bool InheritByNestedTypes { get; set; } = true;
22+
23+
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
24+
{
25+
this.Scope = scope;
26+
}
27+
}
28+
29+
```
30+
31+
|参数|描述|
32+
|-|-|
33+
|scope|混淆作用范围。类型为ObfuzScope,默认值为Obfuzscope.All,即会禁用类型名、字段、函数、property、event及子类型的所有混淆|
34+
|InheritByNestedTypes|是否被嵌套成员类继承。默认为true。此参数只对类型有效。|
35+
36+
枚举类ObfuzScope的实现如下:
37+
38+
```csharp
39+
public enum ObfuzScope
40+
{
41+
None = 0x0,
42+
TypeName = 0x1,
43+
Field = 0x2,
44+
MethodName = 0x4,
45+
MethodParameter = 0x8,
46+
MethodBody = 0x10,
47+
Method = MethodName | MethodParameter | MethodBody,
48+
PropertyName = 020,
49+
PropertyGetter = 0x40,
50+
PropertySetter = 0x80,
51+
Property = PropertyName | PropertyGetter | PropertySetter,
52+
EventName = 0x100,
53+
EventAdd = 0x200,
54+
EventRemove = 0x400,
55+
EventFire = 0x800,
56+
Event = EventName | EventAdd | EventRemove,
57+
Module = 0x1000,
58+
All = TypeName | Field | Method | Property | Event,
59+
}
60+
61+
```
62+
63+
### 作用目标
64+
65+
`ObfuzIgnoreAttribute`可以添加到类型、函数、字段、Property、event上,被添加的元数据会被禁用所有混淆操作。
66+
67+
|[ObfuzIgnore]添加的目标|描述|
68+
|-|-|
69+
|类型|该类型及所有内嵌子类型、成员字段、函数、Property、event都不会被混淆|
70+
|函数|该函数名及参数及函数体都不会被混淆。注意!函数代码中引用的符号如果被混淆或者加密,则仍然会被替换为混淆后的版本。显然必须这么做才能保证正确性。|
71+
|字段|会被禁用符号混淆、字段加密在内的所有可作用于字段的混淆操作|
72+
|property|会被禁用符号混淆在内的所有可作用于Property的混淆操作,但仍然会混淆getter和setter函数。如果想禁止混淆getter和setter函数需要在函数上添加`[ObfuzIgnore]`|
73+
|event|会被禁用符号混淆在内的所有可作用于event的混淆操作,但仍然会混淆add、remove函数。如果想禁止混淆这些函数需要在函数上添加`[ObfuzIgnore]`|
74+
75+
### 其他说明
76+
77+
`[ObfuzIgnore]`不能用于Assembly对象,例如`[assembly: ObfuzIgnore]`不会任何效果。想对某个程序集禁用混淆,
78+
只需要将它从混淆列表`AssemblySettings.AssemblyToObfuscate`移除即可。
79+
80+
`[ObfuzIgnore]`会在最后的RemoveObfuzAttributesPass中被移除,因此它只会出现在源码和原始程序集中,不会出现在最终的混淆后的程序集中。
81+
82+
### 示例代码
83+
84+
```csharp
85+
86+
// 本类型及嵌套类型的类型名、字段、函数、property、event都不会被混淆
87+
[ObfuzIgnore]
88+
public class TestObfuzIgnoreAll
89+
{
90+
public int x;
91+
public int Value { get; set; }
92+
93+
public void Run()
94+
{
95+
Console.WriteLine("TestObfuzIgnore.Run");
96+
}
97+
98+
public event Action Handler;
99+
100+
public class NestedType1
101+
{
102+
public int x;
103+
public int Value { get; set; }
104+
105+
public void Run()
106+
{
107+
Console.WriteLine("TestObfuzIgnore.Run");
108+
}
109+
110+
public event Action Handler;
111+
112+
public class NestedType2
113+
{
114+
public int x;
115+
public int Value { get; set; }
116+
117+
public void Run()
118+
{
119+
Console.WriteLine("TestObfuzIgnore.Run");
120+
}
121+
122+
public event Action Handler;
123+
}
124+
}
125+
}
126+
127+
// 本类型及嵌套类型的类型名、字段、函数、property、event都会被混淆
128+
[ObfuzIgnore(ObfuzScope.None)]
129+
public class TestObfuzIgnoreNone
130+
{
131+
public int x;
132+
public int Value { get; set; }
133+
134+
public void Run()
135+
{
136+
Console.WriteLine("TestObfuzIgnore.Run");
137+
}
138+
139+
public event Action Handler;
140+
141+
public class NestedType1
142+
{
143+
public int x;
144+
public int Value { get; set; }
145+
146+
public void Run()
147+
{
148+
Console.WriteLine("TestObfuzIgnore.Run");
149+
}
150+
151+
public event Action Handler;
152+
153+
public class NestedType2
154+
{
155+
public int x;
156+
public int Value { get; set; }
157+
158+
public void Run()
159+
{
160+
Console.WriteLine("TestObfuzIgnore.Run");
161+
}
162+
163+
public event Action Handler;
164+
}
165+
}
166+
}
167+
168+
169+
// 此类型及嵌套子类型的类型名不会被混淆,它们的字段、property、event、函数全部会被混淆
170+
[ObfuzIgnore(ObfuzScope.TypeName)]
171+
public class TestObfuzIgnoreTypeName
172+
{
173+
public int x; // 被混淆
174+
public int Value { get; set; } // 被混淆
175+
176+
public void Run() // 被混淆
177+
{
178+
Console.WriteLine("TestObfuzIgnore.Run");
179+
}
180+
181+
public event Action Handler; // 被混淆
182+
183+
public class NestedType1 // 不会被混淆
184+
{
185+
public int x; // 被混淆
186+
public int Value { get; set; } // 被混淆
187+
188+
public void Run() // 被混淆
189+
{
190+
Console.WriteLine("TestObfuzIgnore.Run");
191+
}
192+
193+
public event Action Handler; // 被混淆
194+
195+
public class NestedType2 // 被混淆
196+
{
197+
public int x; // 被混淆
198+
public int Value { get; set; } // 被混淆
199+
200+
public void Run() // 被混淆
201+
{
202+
Console.WriteLine("TestObfuzIgnore.Run");
203+
}
204+
205+
public event Action Handler; // 被混淆
206+
}
207+
}
208+
}
209+
210+
// 此类型及嵌套类型的字段不会被混淆,但类型名及method、event、property仍然会被混淆
211+
[ObfuzIgnore(ObfuzScope.Field)]
212+
public class TestObfuzIgnoreField
213+
{
214+
public int x;
215+
public int Value { get; set; }
216+
217+
public void Run()
218+
{
219+
Console.WriteLine("TestObfuzIgnore.Run");
220+
}
221+
222+
public event Action Handler;
223+
224+
public class NestedType1
225+
{
226+
public int x;
227+
public int Value { get; set; }
228+
229+
public void Run()
230+
{
231+
Console.WriteLine("TestObfuzIgnore.Run");
232+
}
233+
234+
public event Action Handler;
235+
236+
public class NestedType2
237+
{
238+
public int x;
239+
public int Value { get; set; }
240+
241+
public void Run()
242+
{
243+
Console.WriteLine("TestObfuzIgnore.Run");
244+
}
245+
246+
public event Action Handler;
247+
}
248+
}
249+
}
250+
```
251+
252+
## EncryptFieldAttribute
253+
254+
EncryptFieldAttribute提供代码中便捷地标记某个字段为加密字段的办法。
255+
256+
它的优先级高于Obfuscation Pass规则和`[ObfuzIgnore]`。只要对某个字段添加了`[EncryptField]`特性,即使字段及所在类型有`[ObfuzIgnore]`特性,仍然会被加密。
257+
258+
示例代码:
259+
260+
```csharp
261+
262+
[ObfuzIgnore]
263+
class A
264+
{
265+
[EncryptField]
266+
public int x1; // 变量x1仍然会被加密,无视类型上的[ObfuzIgnore]
267+
268+
[ObfuzIgnore]
269+
[EncryptField]
270+
public int x2; // 变量x2仍然会被加密,无视字段上的[ObfuzIgnore]
271+
272+
public int y; // 变量y不会加密,也不会被释加任何混淆或加密Pass
273+
}
274+
275+
```

docs/manual/field-encryption.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ MonoBehaviour、ScriptableObject及NewtonsoftJson之类的序列化库深度依
3737

3838
## EncryptFieldAttribute
3939

40-
EncryptFieldAttribute提供代码中便捷地标记某个字段为加密字段的办法。
40+
EncryptFieldAttribute提供代码中便捷地标记某个字段为加密字段的办法,详细文档见[Obfuz CustomAttributes](./customattributes)
4141

4242
它的优先级高于Obfuscation Pass规则和`[ObfuzIgnore]`。只要对某个字段添加了`[EncryptField]`特性,即使字段及所在类型有`[ObfuzIgnore]`特性,仍然会被加密。
4343

docs/manual/obfuzignore.md

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)