@@ -24,19 +24,21 @@ private static void GetPatches(Assembly module, ref Dictionary<Type, List<PatchV
2424 try
2525 {
2626 Type [ ] types = module . GetTypes ( ) ;
27-
28- for ( int i = 0 ; i < types . Length ; i ++ )
27+ int typeCount = types . Length ;
28+ for ( int i = 0 ; i < typeCount ; i ++ )
2929 {
3030 Type type = types [ i ] ;
3131
32- if ( ! type . IsAbstract && PatchType . IsAssignableFrom ( type ) )
32+ if ( type . IsAbstract || ! PatchType . IsAssignableFrom ( type ) )
3333 {
34- List < PatchValidationAttribute > validators = GetValidationRules ( type . GetCustomAttributes ( PatchValidationType , true )
35- . Concat ( type . Assembly . GetCustomAttributes ( PatchValidationType , true ) ) . ToArray ( ) ) ;
36-
37- patchTypes . Add ( type , validators ) ;
38- Interface . Oxide . RootLogger . WriteDebug ( LogType . Info , Logging . LogEvent . Patch , "Patcher" , $ "Found { validators . Count } total validators for patch { type . Name } ") ;
34+ continue ;
3935 }
36+
37+ List < PatchValidationAttribute > validators = GetValidationRules ( type . GetCustomAttributes ( PatchValidationType , true )
38+ . Concat ( type . Assembly . GetCustomAttributes ( PatchValidationType , true ) ) . ToArray ( ) ) ;
39+
40+ patchTypes . Add ( type , validators ) ;
41+ Interface . Oxide . RootLogger . WriteDebug ( LogType . Info , Logging . LogEvent . Patch , "Patcher" , $ "Found { validators . Count } total validators for patch { type . Name } ") ;
4042 }
4143 }
4244 catch ( Exception e )
@@ -47,7 +49,8 @@ private static void GetPatches(Assembly module, ref Dictionary<Type, List<PatchV
4749
4850 private static void GetPatches ( Assembly [ ] modules , ref Dictionary < Type , List < PatchValidationAttribute > > patchTypes )
4951 {
50- for ( int i = 0 ; i < modules . Length ; i ++ )
52+ int moduleCount = modules . Length ;
53+ for ( int i = 0 ; i < moduleCount ; i ++ )
5154 {
5255 GetPatches ( modules [ i ] , ref patchTypes ) ;
5356 }
@@ -63,23 +66,27 @@ public static bool Run(AssemblyDefinition module)
6366 }
6467
6568 PatchContext context = new PatchContext ( module ) ;
66- foreach ( var kv in Patches )
69+ foreach ( KeyValuePair < Type , List < PatchValidationAttribute > > kv in Patches )
6770 {
6871
6972 Type patchType = kv . Key ;
7073 List < PatchValidationAttribute > validators = kv . Value ;
7174 context . PatchValidators = validators ;
7275 bool failed = false ;
73- for ( int n = 0 ; n < validators . Count ; n ++ )
76+
77+ int validatorCount = validators . Count ;
78+ for ( int i = 0 ; i < validatorCount ; i ++ )
7479 {
75- PatchValidationAttribute valid = validators [ n ] ;
76- bool pass = valid . Validate ( module ) ;
80+ PatchValidationAttribute patchValidationAttribute = validators [ i ] ;
81+ bool pass = patchValidationAttribute . Validate ( module ) ;
7782 // Interface.Oxide.RootLogger.WriteDebug(LogType.Info, Logging.LogEvent.Patch, "Patcher", $"Validation {valid.GetType().Name}: {(pass ? "passed" : "failed")}");
78- if ( ! pass )
83+ if ( pass )
7984 {
80- failed = true ;
81- break ;
85+ continue ;
8286 }
87+
88+ failed = true ;
89+ break ;
8390 }
8491
8592 if ( failed )
@@ -96,10 +103,10 @@ public static bool Run(AssemblyDefinition module)
96103 Interface . Oxide . RootLogger . WriteDebug ( LogType . Info , Logging . LogEvent . Patch , "Patcher" ,
97104 $ "{ patchType . Name } has applied { context . ContextPatches } patches to { module . Name ? . Name ?? module . FullName } ") ;
98105 }
99- catch ( Exception e )
106+ catch ( Exception exception )
100107 {
101108 Interface . Oxide . RootLogger . WriteDebug ( LogType . Error , Logging . LogEvent . Patch , "Patcher" ,
102- $ "{ patchType . Name } has applied { context . ContextPatches } patches to { module . Name ? . Name ?? module . FullName } but threw a error", e ) ;
109+ $ "{ patchType . Name } has applied { context . ContextPatches } patches to { module . Name ? . Name ?? module . FullName } but threw a error", exception ) ;
103110 }
104111 }
105112
@@ -110,25 +117,20 @@ public static byte[] Run(byte[] data, out bool patched)
110117 {
111118 try
112119 {
113- using ( MemoryStream inStream = new MemoryStream ( data ) )
114- {
115- AssemblyDefinition assembly = AssemblyDefinition . ReadAssembly ( inStream ) ;
120+ using MemoryStream inStream = new ( data ) ;
121+ using AssemblyDefinition assembly = AssemblyDefinition . ReadAssembly ( inStream ) ;
116122
117- if ( Run ( assembly ) )
118- {
119- using ( MemoryStream outStream = new MemoryStream ( ) )
120- {
121- assembly . Write ( outStream ) ;
122- patched = true ;
123- return outStream . ToArray ( ) ;
124- }
125- }
123+ if ( Run ( assembly ) )
124+ {
125+ using MemoryStream outStream = new ( ) ;
126+ assembly . Write ( outStream ) ;
127+ patched = true ;
128+ return outStream . ToArray ( ) ;
126129 }
127-
128130 }
129- catch ( Exception e )
131+ catch ( Exception exception )
130132 {
131- Interface . Oxide . RootLogger . WriteDebug ( LogType . Error , Logging . LogEvent . Patch , "Patcher" , $ "Failed to patch", e ) ;
133+ Interface . Oxide . RootLogger . WriteDebug ( LogType . Error , Logging . LogEvent . Patch , "Patcher" , $ "Failed to patch", exception ) ;
132134 }
133135
134136 patched = false ;
@@ -137,15 +139,17 @@ public static byte[] Run(byte[] data, out bool patched)
137139
138140 public static List < PatchValidationAttribute > GetValidationRules ( object [ ] attributes )
139141 {
140- List < PatchValidationAttribute > validators = new List < PatchValidationAttribute > ( ) ;
142+ List < PatchValidationAttribute > validators = new ( ) ;
141143
142- for ( int i = 0 ; i < attributes . Length ; i ++ )
144+ int attributeCount = attributes . Length ;
145+ for ( int i = 0 ; i < attributeCount ; i ++ )
143146 {
144- Attribute a = attributes [ i ] as Attribute ;
145- if ( a is PatchValidationAttribute valid )
147+ if ( attributes [ i ] is not PatchValidationAttribute patchValidationAttribute )
146148 {
147- validators . Add ( valid ) ;
149+ continue ;
148150 }
151+
152+ validators . Add ( patchValidationAttribute ) ;
149153 }
150154
151155 return validators ;
0 commit comments