HooksSetup.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Harmony;
  8. using UnityEngine;
  9. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  10. using XUnity.AutoTranslator.Plugin.Core.Constants;
  11. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  12. using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
  13. using XUnity.AutoTranslator.Plugin.Core.Hooks.TextMeshPro;
  14. using XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI;
  15. using XUnity.AutoTranslator.Plugin.Core.IMGUI;
  16. namespace XUnity.AutoTranslator.Plugin.Core.Hooks
  17. {
  18. public static class HooksSetup
  19. {
  20. public static void InstallHooks()
  21. {
  22. var harmony = HarmonyInstance.Create( "gravydevsupreme.xunity.autotranslator" );
  23. bool success = false;
  24. try
  25. {
  26. if( Settings.EnableUGUI || Settings.EnableUtage )
  27. {
  28. success = SetupHook( KnownEvents.OnUnableToTranslateUGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
  29. if( !success )
  30. {
  31. harmony.PatchAll( UGUIHooks.All );
  32. }
  33. }
  34. }
  35. catch( Exception e )
  36. {
  37. Logger.Current.Error( e, "An error occurred while setting up hooks for UGUI." );
  38. }
  39. try
  40. {
  41. if( Settings.EnableTextMeshPro )
  42. {
  43. success = SetupHook( KnownEvents.OnUnableToTranslateTextMeshPro, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
  44. if( !success )
  45. {
  46. harmony.PatchAll( TextMeshProHooks.All );
  47. }
  48. }
  49. }
  50. catch( Exception e )
  51. {
  52. Logger.Current.Error( e, "An error occurred while setting up hooks for TextMeshPro." );
  53. }
  54. try
  55. {
  56. if( Settings.EnableNGUI )
  57. {
  58. success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
  59. if( !success )
  60. {
  61. harmony.PatchAll( NGUIHooks.All );
  62. }
  63. }
  64. }
  65. catch( Exception e )
  66. {
  67. Logger.Current.Error( e, "An error occurred while setting up hooks for NGUI." );
  68. }
  69. try
  70. {
  71. if( Settings.EnableIMGUI )
  72. {
  73. success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
  74. if( !success )
  75. {
  76. harmony.PatchAll( IMGUIHooks.All );
  77. // This wont work in "newer" unity versions!
  78. try
  79. {
  80. harmony.PatchType( typeof( DoButtonGridHook ) );
  81. }
  82. catch { }
  83. }
  84. }
  85. }
  86. catch( Exception e )
  87. {
  88. Logger.Current.Error( e, "An error occurred while setting up hooks for IMGUI." );
  89. }
  90. try
  91. {
  92. if( Settings.EnableUtage )
  93. {
  94. harmony.PatchAll( UtageHooks.All );
  95. }
  96. }
  97. catch( Exception e )
  98. {
  99. Logger.Current.Error( e, "An error occurred while setting up hooks for Utage." );
  100. }
  101. }
  102. public static bool SetupHook( string eventName, Func<object, string, string> callback )
  103. {
  104. if( !Settings.AllowPluginHookOverride ) return false;
  105. var objects = GameObject.FindObjectsOfType<GameObject>();
  106. foreach( var gameObject in objects )
  107. {
  108. if( gameObject != null )
  109. {
  110. var components = gameObject.GetComponents<Component>();
  111. foreach( var component in components )
  112. {
  113. if( component != null )
  114. {
  115. var e = component.GetType().GetEvent( eventName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
  116. if( e != null )
  117. {
  118. var addMethod = e.GetAddMethod();
  119. if( addMethod != null )
  120. {
  121. try
  122. {
  123. if( addMethod.IsStatic )
  124. {
  125. addMethod.Invoke( null, new object[] { callback } );
  126. }
  127. else
  128. {
  129. addMethod.Invoke( component, new object[] { callback } );
  130. }
  131. Logger.Current.Info( eventName + " was hooked by external plugin." );
  132. return true;
  133. }
  134. catch { }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. }
  144. }