HarmonyInstanceExtensions.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Harmony;
  6. namespace XUnity.AutoTranslator.Plugin.Core.Extensions
  7. {
  8. internal static class HarmonyInstanceExtensions
  9. {
  10. public static void PatchAll( this HarmonyInstance instance, IEnumerable<Type> types )
  11. {
  12. foreach( var type in types )
  13. {
  14. instance.PatchType( type );
  15. }
  16. }
  17. public static void PatchType( this HarmonyInstance instance, Type type )
  18. {
  19. try
  20. {
  21. var parentMethodInfos = type.GetHarmonyMethods();
  22. if( parentMethodInfos != null && parentMethodInfos.Count() > 0 )
  23. {
  24. var info = HarmonyMethod.Merge( parentMethodInfos );
  25. var processor = new PatchProcessor( instance, type, info );
  26. processor.Patch();
  27. }
  28. }
  29. catch( Exception e )
  30. {
  31. Logger.Current.Warn( e, "An error occurred while patching a property/method on a class. Failing class: " + type.Name );
  32. }
  33. }
  34. }
  35. }