77using UncomplicatedCustomItems . API ;
88using LabApi . Features . Wrappers ;
99using UnityEngine ;
10- using MEC ;
1110using UncomplicatedCustomItems . API . Interfaces ;
1211
1312namespace UncomplicatedCustomItems . Integrations
1413{
15- public static class ECRIntegration
14+ internal static class ECRIntegration
1615 {
1716 private static bool _isPatched = false ;
18- private static Harmony _harmonyInstance ;
17+ private static bool Found ;
1918
20- public static void Initialize ( Harmony harmonyInstance )
19+ public static void Init ( )
2120 {
22- _harmonyInstance = harmonyInstance ;
23-
2421 if ( TryPatchECRIntegration ( ) )
2522 {
26- LogManager . Silent ( "ECR Integration patched successfully on initialization." ) ;
23+ LogManager . Silent ( "ECR Integration patched." ) ;
24+ Found = true ;
2725 return ;
2826 }
2927
30- AppDomain . CurrentDomain . AssemblyLoad += OnAssemblyLoad ;
31- LogManager . Silent ( "ECR Integration waiting for Exiled.CustomRoles to load..." ) ;
32- }
33-
34- private static void OnAssemblyLoad ( object sender , AssemblyLoadEventArgs args )
35- {
36- if ( _isPatched )
37- return ;
38-
39- if ( args . LoadedAssembly . GetName ( ) . Name . Contains ( "Exiled.CustomRoles" ) )
40- {
41- LogManager . Silent ( $ "Detected Exiled.CustomRoles assembly load: { args . LoadedAssembly . FullName } ") ;
42-
43- Timing . CallDelayed ( 1f , ( ) =>
44- {
45- if ( TryPatchECRIntegration ( ) )
46- {
47- LogManager . Silent ( "ECR Integration patched successfully after assembly load." ) ;
48- AppDomain . CurrentDomain . AssemblyLoad -= OnAssemblyLoad ;
49- }
50- } ) ;
51- }
28+ if ( Found )
29+ LogManager . Silent ( $ "ECR found! :D") ;
5230 }
5331
5432 private static bool TryPatchECRIntegration ( )
@@ -61,13 +39,13 @@ private static bool TryPatchECRIntegration()
6139 MethodBase targetMethod = GetTargetMethod ( ) ;
6240 if ( targetMethod == null )
6341 {
64- LogManager . Silent ( "ECR target method not found - Exiled.CustomRoles may not be loaded yet ." ) ;
42+ LogManager . Silent ( "ECR target method not found." ) ;
6543 return false ;
6644 }
6745
6846 MethodInfo prefixMethod = typeof ( ECRIntegration ) . GetMethod ( nameof ( Prefix ) , BindingFlags . Static | BindingFlags . Public ) ;
6947
70- _harmonyInstance . Patch ( targetMethod , new HarmonyMethod ( prefixMethod ) ) ;
48+ Plugin . Instance . _harmony . Patch ( targetMethod , new HarmonyMethod ( prefixMethod ) ) ;
7149 _isPatched = true ;
7250 LogManager . Silent ( "ECR Integration successfully patched TryAddItem method." ) ;
7351 return true ;
@@ -83,27 +61,21 @@ private static MethodBase GetTargetMethod()
8361 {
8462 try
8563 {
86- Type customRoleType = AppDomain . CurrentDomain . GetAssemblies ( )
87- . SelectMany ( GetTypesFromAssembly )
88- . FirstOrDefault ( t => t . FullName == "Exiled.CustomRoles.API.Features.CustomRole" ) ;
89-
64+ Type customRoleType = AppDomain . CurrentDomain . GetAssemblies ( ) . SelectMany ( GetTypesFromAssembly ) . FirstOrDefault ( t => t . FullName == "Exiled.CustomRoles.API.Features.CustomRole" ) ;
9065 if ( customRoleType == null )
9166 return null ;
9267
93- MethodInfo method = customRoleType . GetMethod ( "TryAddItem" ,
94- BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ,
95- null ,
96- [
97- Type . GetType ( "Exiled.API.Features.Player, Exiled.API" ) ,
98- typeof ( string )
99- ] ,
100- null ) ;
68+ Type [ ] paramTypes = [
69+ Type . GetType ( "Exiled.API.Features.Player, Exiled.API" ) ,
70+ typeof ( string )
71+ ] ;
10172
73+ MethodInfo method = customRoleType . GetMethod ( "TryAddItem" , BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public , null , paramTypes , null ) ;
10274 return method ;
10375 }
10476 catch ( Exception ex )
10577 {
106- LogManager . Debug ( $ "Error in GetTargetMethod() : { ex . Message } ") ;
78+ LogManager . Debug ( $ "{ nameof ( GetTargetMethod ) } : { ex . Message } ") ;
10779 return null ;
10880 }
10981 }
@@ -163,14 +135,13 @@ public static bool Prefix(object __instance, object player, string itemName, ref
163135 }
164136 catch ( Exception ex )
165137 {
166- LogManager . Error ( $ "Error in ECRIntegration.Prefix() : { ex } ") ;
138+ LogManager . Error ( $ "{ nameof ( ECRIntegration ) } : { ex } ") ;
167139 return true ;
168140 }
169141 }
170142
171143 public static void Cleanup ( )
172144 {
173- AppDomain . CurrentDomain . AssemblyLoad -= OnAssemblyLoad ;
174145 _isPatched = false ;
175146 }
176147 }
0 commit comments