dcomicboy

tested method

Jul 18th, 2024 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. private void HookPurgeNOSAddon(Il2CppSystem.Type purgeNOSAddonType)
  2. {
  3. try
  4. {
  5. //Il2CppSystem.Type purgeNOSAddonType = null;
  6.  
  7. // Iterate through all assemblies
  8. foreach (var assembly in Il2CppSystem.AppDomain.CurrentDomain.GetAssemblies())
  9. {
  10. try
  11. {
  12. // Iterate through all types in the assembly
  13. foreach (var type in assembly.GetTypes())
  14. {
  15. // Check if the type contains a property named "enabled" (or part of it)
  16. if (type.GetProperties((Il2CppSystem.Reflection.BindingFlags)(BindingFlags.Public | BindingFlags.Instance)).Any(p => p.Name.Contains("enabled")))
  17. {
  18. purgeNOSAddonType = type;
  19. MelonLogger.Msg($"Found matching type: {type.FullName} in assembly {assembly.FullName}");
  20. break;
  21. }
  22. }
  23. }
  24. catch (ReflectionTypeLoadException ex)
  25. {
  26. MelonLogger.Error($"ReflectionTypeLoadException for assembly {assembly.FullName}: {ex.Message}");
  27. foreach (System.Exception loaderException in ex.LoaderExceptions)
  28. {
  29. MelonLogger.Error("LoaderException: " + loaderException.Message);
  30. }
  31. }
  32. catch (System.Exception ex)
  33. {
  34. MelonLogger.Error($"Exception for assembly {assembly.FullName}: {ex.Message}");
  35. }
  36.  
  37. if (purgeNOSAddonType != null)
  38. {
  39. break;
  40. }
  41. }
  42.  
  43. if (purgeNOSAddonType == null)
  44. {
  45. MelonLogger.Error("PurgeNOSAddon type not found.");
  46. return;
  47. }
  48.  
  49. // Register the type with IL2CPP
  50. //ClassInjector.RegisterTypeInIl2Cpp(purgeNOSAddonType);
  51.  
  52. // Get Il2CppSystem.Type from System.Type
  53. Il2CppSystem.Type il2cppPurgeNOSAddonType = Il2CppSystem.Type.GetType(purgeNOSAddonType.AssemblyQualifiedName);
  54.  
  55. // Find all instances of the base type
  56. var allComponents = Resources.FindObjectsOfTypeAll(Il2CppType.Of<Component>());
  57. Component purgeNOSAddonInstance = null;
  58.  
  59. foreach (var component in allComponents)
  60. {
  61. if (component.GetIl2CppType().FullName == il2cppPurgeNOSAddonType.FullName)
  62. {
  63. purgeNOSAddonInstance = (Component)component;
  64. break;
  65. }
  66. }
  67.  
  68. if (purgeNOSAddonInstance == null)
  69. {
  70. MelonLogger.Error("Instance of PurgeNOSAddon not found in the scene.");
  71. return;
  72. }
  73.  
  74. // Get the 'enabled' property using reflection
  75. var enabledProperty = purgeNOSAddonType.GetProperty("enabled", (Il2CppSystem.Reflection.BindingFlags)(BindingFlags.Public | BindingFlags.Instance));
  76.  
  77. if (enabledProperty == null)
  78. {
  79. MelonLogger.Error("'enabled' property not found on PurgeNOSAddon.");
  80. return;
  81. }
  82.  
  83. // Set the 'enabled' property to true
  84. enabledProperty.SetValue(purgeNOSAddonInstance, true);
  85. MelonLogger.Msg("PurgeNOSAddon.enabled set to true.");
  86. }
  87. catch (ReflectionTypeLoadException ex)
  88. {
  89. MelonLogger.Error("ReflectionTypeLoadException: " + ex.Message);
  90. foreach (System.Exception loaderException in ex.LoaderExceptions)
  91. {
  92. MelonLogger.Error("LoaderException: " + loaderException.Message);
  93. }
  94. }
  95. catch (System.Exception ex)
  96. {
  97. MelonLogger.Error("An error occurred while hooking PurgeNOSAddon: " + ex.Message);
  98. }
  99. }
  100.  
  101. //WORKING FUNCTION
  102. private void HookPurgeNOSAddon2()
  103. {
  104. try
  105. {
  106. Type purgeNOSAddonType = null;
  107.  
  108. // Iterate through all assemblies
  109. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  110. {
  111. try
  112. {
  113. // Iterate through all types in the assembly
  114. foreach (var type in assembly.GetTypes())
  115. {
  116. // Check if the type contains a property named "enabled" (or part of it)
  117. if (type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Any(p => p.Name.Contains("enabled")))
  118. {
  119. purgeNOSAddonType = type;
  120. MelonLogger.Msg($"Found matching type: {type.FullName} in assembly {assembly.FullName}");
  121. break;
  122. }
  123. }
  124. }
  125. catch (ReflectionTypeLoadException ex)
  126. {
  127. MelonLogger.Error($"ReflectionTypeLoadException for assembly {assembly.FullName}: {ex.Message}");
  128. foreach (Exception loaderException in ex.LoaderExceptions)
  129. {
  130. MelonLogger.Error("LoaderException: " + loaderException.Message);
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. MelonLogger.Error($"Exception for assembly {assembly.FullName}: {ex.Message}");
  136. }
  137.  
  138. if (purgeNOSAddonType != null)
  139. {
  140. break;
  141. }
  142. }
  143.  
  144. if (purgeNOSAddonType == null)
  145. {
  146. MelonLogger.Error("PurgeNOSAddon type not found.");
  147. return;
  148. }
  149.  
  150. // Register the type with IL2CPP
  151. ClassInjector.RegisterTypeInIl2Cpp(purgeNOSAddonType);
  152.  
  153. // Get Il2CppSystem.Type from System.Type
  154. Il2CppSystem.Type il2cppPurgeNOSAddonType = Il2CppSystem.Type.GetType(purgeNOSAddonType.AssemblyQualifiedName);
  155.  
  156. // Use Il2CppObjectBase to find the instance
  157. //var purgeNOSAddonInstance = Il2CppObjectBase.FindObjectOfType(il2cppPurgeNOSAddonType);
  158. var purgeNOSAddonInstance = UnityEngine.Object.FindObjectOfType(il2cppPurgeNOSAddonType);
  159.  
  160. if (purgeNOSAddonInstance == null)
  161. {
  162. MelonLogger.Error("Instance of PurgeNOSAddon not found in the scene.");
  163. return;
  164. }
  165.  
  166. // Get the 'enabled' property using reflection
  167. var enabledProperty = purgeNOSAddonType.GetProperty("enabled", BindingFlags.Public | BindingFlags.Instance);
  168.  
  169. if (enabledProperty == null)
  170. {
  171. MelonLogger.Error("'enabled' property not found on PurgeNOSAddon.");
  172. return;
  173. }
  174.  
  175. // Set the 'enabled' property to true
  176. enabledProperty.SetValue(purgeNOSAddonInstance, true);
  177. MelonLogger.Msg("PurgeNOSAddon.enabled set to true.");
  178. }
  179. catch (ReflectionTypeLoadException ex)
  180. {
  181. MelonLogger.Error("ReflectionTypeLoadException: " + ex.Message);
  182. foreach (Exception loaderException in ex.LoaderExceptions)
  183. {
  184. MelonLogger.Error("LoaderException: " + loaderException.Message);
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. MelonLogger.Error("An error occurred while hooking PurgeNOSAddon: " + ex.Message);
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment