@@ -19,7 +19,8 @@ public static void Execute(Context ctx)
19
19
20
20
foreach ( var TypeDef in ctx . Module . Types . ToArray ( ) )
21
21
foreach ( var MethodDef in TypeDef . Methods . Where ( x => x . HasBody && ContainsSwitch ( x ) ) . ToArray ( ) )
22
- try {
22
+ try
23
+ {
23
24
var blocks = new Blocks ( MethodDef ) ;
24
25
blocks . MethodBlocks . GetAllBlocks ( ) ;
25
26
blocks . RemoveDeadBlocks ( ) ;
@@ -30,10 +31,13 @@ public static void Execute(Context ctx)
30
31
CfDeob . Initialize ( blocks ) ;
31
32
CfDeob . Deobfuscate ( ) ;
32
33
blocks . RepartitionBlocks ( ) ;
33
- blocks . GetCode ( out var instructions , out var exceptionHandlers ) ;
34
+ blocks . GetCode ( out
35
+ var instructions , out
36
+ var exceptionHandlers ) ;
34
37
DotNetUtils . RestoreBody ( MethodDef , instructions , exceptionHandlers ) ;
35
38
}
36
- catch {
39
+ catch
40
+ {
37
41
// ignored
38
42
}
39
43
}
@@ -43,86 +47,70 @@ private static void SimplifyArithmetic(Context ctx)
43
47
var logger = ctx . Options . Logger ;
44
48
45
49
foreach ( var type in ctx . Module . Types )
46
- foreach ( var method in type . Methods . Where ( x =>
47
- x . HasBody && x . Body . HasInstructions && x . Body . Instructions . Count >= 3 ) ) {
48
- var Instructions = method . Body . Instructions ;
50
+ foreach ( var method in type . Methods . Where ( x => x . HasBody && x . Body . HasInstructions && x . Body . Instructions . Count >= 3 ) )
51
+ {
52
+ var Instructions = method . Body . Instructions ;
49
53
50
- for ( var i = 1 ; i < Instructions . Count ; i ++ ) {
51
- if ( i + 1 >= Instructions . Count ) continue ;
54
+ for ( var i = 1 ; i < Instructions . Count ; i ++ )
55
+ {
56
+ if ( i + 1 >= Instructions . Count ) continue ;
52
57
53
- var prevInstr = Instructions [ i - 1 ] ;
54
- var curInstr = Instructions [ i ] ;
55
- var nextInstr = Instructions [ i + 1 ] ;
56
-
57
- // Too lazy to strip these two into one function
58
- #region Ldsfld
59
- if ( curInstr . OpCode == OpCodes . Brtrue &&
60
- nextInstr . OpCode == OpCodes . Pop &&
61
- prevInstr . OpCode == OpCodes . Ldsfld ) {
62
- /*if (prevInstr.Operand.ToString().Contains("System.Boolean")) {
63
- logger.Info($"Brtrue with Boolean: {method.FullName}");
64
- prevInstr.OpCode = OpCodes.Nop;
65
- curInstr.OpCode = OpCodes.Br_S;
66
- FixedMethods++;
67
- }
68
- else*/ {
69
- logger . Info ( $ "Brtrue: { method . FullName } ") ;
70
- prevInstr . OpCode = OpCodes . Nop ;
71
- curInstr . OpCode = OpCodes . Br_S ;
72
- FixedMethods ++ ;
73
- }
74
- }
58
+ var prevInstr = Instructions [ i - 1 ] ;
59
+ var curInstr = Instructions [ i ] ;
60
+ var nextInstr = Instructions [ i + 1 ] ;
75
61
76
- else if ( curInstr . OpCode == OpCodes . Brfalse &&
77
- nextInstr . OpCode == OpCodes . Pop &&
78
- prevInstr . OpCode == OpCodes . Ldsfld ) {
79
- /*if (prevInstr.Operand.ToString().Contains("System.Boolean")) {
80
- logger.Info($"Brfalse with Boolean: {method.FullName}");
62
+ // Too lazy to strip these two into one function, mess
63
+ #region Ldsfld
64
+ if ( curInstr . OpCode == OpCodes . Brtrue &&
65
+ nextInstr . OpCode == OpCodes . Pop &&
66
+ prevInstr . OpCode == OpCodes . Ldsfld ) {
67
+ logger . Info ( $ "Brtrue: { method . FullName } ") ;
81
68
prevInstr . OpCode = OpCodes . Nop ;
82
69
curInstr . OpCode = OpCodes . Br_S ;
83
70
FixedMethods ++ ;
84
71
}
85
- else*/ {
72
+ else if ( curInstr . OpCode == OpCodes . Brfalse &&
73
+ nextInstr . OpCode == OpCodes . Pop &&
74
+ prevInstr . OpCode == OpCodes . Ldsfld ) {
86
75
logger . Info ( $ "Brfalse: { method . FullName } ") ;
87
76
prevInstr . OpCode = OpCodes . Nop ;
88
77
curInstr . OpCode = OpCodes . Br_S ;
89
78
FixedMethods ++ ;
90
79
}
91
- }
92
- #endregion
93
- #region Call
94
- if ( curInstr . OpCode == OpCodes . Brtrue &&
95
- nextInstr . OpCode == OpCodes . Pop &&
96
- prevInstr . OpCode == OpCodes . Call ) {
97
- if ( prevInstr . Operand . ToString ( ) . Contains ( "System.Boolean" ) ) {
98
- logger . Info ( $ "Call with Boolean: { method . FullName } ") ;
99
- prevInstr . OpCode = OpCodes . Nop ;
100
- curInstr . OpCode = OpCodes . Br_S ;
101
- }
102
- else {
103
- logger . Info ( $ "Call: { method . FullName } ") ;
104
- prevInstr . OpCode = OpCodes . Nop ;
105
- curInstr . OpCode = OpCodes . Nop ;
106
- }
107
- }
80
+ #endregion
108
81
109
- else if ( curInstr . OpCode == OpCodes . Brfalse &&
110
- nextInstr . OpCode == OpCodes . Pop &&
111
- prevInstr . OpCode == OpCodes . Call ) {
112
- if ( prevInstr . Operand . ToString ( ) . Contains ( "System.Boolean" ) ) {
113
- logger . Info ( $ "Call with Boolean: { method . FullName } ") ;
114
- prevInstr . OpCode = OpCodes . Nop ;
115
- curInstr . OpCode = OpCodes . Nop ;
82
+ #region Call
83
+ if ( curInstr . OpCode == OpCodes . Brtrue &&
84
+ nextInstr . OpCode == OpCodes . Pop &&
85
+ prevInstr . OpCode == OpCodes . Call ) {
86
+ if ( prevInstr . Operand . ToString ( ) . Contains ( "System.Boolean" ) ) {
87
+ logger . Info ( $ "Call with Boolean: { method . FullName } ") ;
88
+ prevInstr . OpCode = OpCodes . Nop ;
89
+ curInstr . OpCode = OpCodes . Br_S ;
90
+ }
91
+ else {
92
+ logger . Info ( $ "Call: { method . FullName } ") ;
93
+ prevInstr . OpCode = OpCodes . Nop ;
94
+ curInstr . OpCode = OpCodes . Nop ;
95
+ }
116
96
}
117
- else {
118
- logger . Info ( $ "Call: { method . FullName } ") ;
119
- prevInstr . OpCode = OpCodes . Nop ;
120
- curInstr . OpCode = OpCodes . Br_S ;
97
+ else if ( curInstr . OpCode == OpCodes . Brfalse &&
98
+ nextInstr . OpCode == OpCodes . Pop &&
99
+ prevInstr . OpCode == OpCodes . Call ) {
100
+ if ( prevInstr . Operand . ToString ( ) . Contains ( "System.Boolean" ) ) {
101
+ logger . Info ( $ "Call with Boolean: { method . FullName } ") ;
102
+ prevInstr . OpCode = OpCodes . Nop ;
103
+ curInstr . OpCode = OpCodes . Nop ;
104
+ }
105
+ else {
106
+ logger . Info ( $ "Call: { method . FullName } ") ;
107
+ prevInstr . OpCode = OpCodes . Nop ;
108
+ curInstr . OpCode = OpCodes . Br_S ;
109
+ }
121
110
}
111
+ #endregion
122
112
}
123
- #endregion
124
113
}
125
- }
126
114
}
127
115
128
116
private static bool ContainsSwitch ( MethodDef method ) => method . Body . Instructions . Any ( t => t . OpCode == OpCodes . Switch ) ;
0 commit comments