ObjectExtensions.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  7. using UnityEngine.UI;
  8. using XUnity.AutoTranslator.Plugin.Core.Constants;
  9. using XUnity.AutoTranslator.Plugin.Core.Utilities;
  10. namespace XUnity.AutoTranslator.Plugin.Core.Extensions
  11. {
  12. public static class ObjectExtensions
  13. {
  14. private static readonly string RichTextPropertyName = "richText";
  15. private static readonly object Sync = new object();
  16. private static readonly WeakDictionary<object, object> DynamicFields = new WeakDictionary<object, object>();
  17. public static bool IsKnownType( this object ui )
  18. {
  19. if( ui == null ) return false;
  20. var type = ui.GetType();
  21. return ui is Text
  22. || ui is UnityEngine.GUIContent
  23. || ( Types.UILabel != null && Types.UILabel.IsAssignableFrom( type ) )
  24. || ( Types.TMP_Text != null && Types.TMP_Text.IsAssignableFrom( type ) )
  25. || ( Types.AdvCommand != null && Types.AdvCommand.IsAssignableFrom( type ) );
  26. }
  27. public static bool SupportsStabilization( this object ui )
  28. {
  29. if( ui == null ) return false;
  30. var type = ui.GetType();
  31. return ui is Text
  32. || ( Types.UILabel != null && Types.UILabel.IsAssignableFrom( type ) )
  33. || ( Types.TMP_Text != null && Types.TMP_Text.IsAssignableFrom( type ) );
  34. }
  35. public static bool SupportsRichText( this object ui )
  36. {
  37. if( ui == null ) return false;
  38. var type = ui.GetType();
  39. return ( ui as Text )?.supportRichText == true
  40. || ( Types.TMP_Text != null && Types.TMP_Text.IsAssignableFrom( type ) && Equals( type.GetProperty( RichTextPropertyName )?.GetValue( ui, null ), true ) )
  41. || ( Types.AdvCommand != null && Types.AdvCommand.IsAssignableFrom( type ) )
  42. || ( Types.UguiNovelText != null && Types.UguiNovelText.IsAssignableFrom( type ) );
  43. }
  44. public static bool IsSpammingComponent( this object ui )
  45. {
  46. if( ui == null ) return false;
  47. return ui is UnityEngine.GUIContent;
  48. }
  49. public static bool IsNGUI( this object ui )
  50. {
  51. if( ui == null ) return false;
  52. var type = ui.GetType();
  53. return Types.UILabel != null && Types.UILabel.IsAssignableFrom( type );
  54. }
  55. public static TranslationInfo GetTranslationInfo( this object obj, bool isAwakening )
  56. {
  57. if( !Settings.EnableObjectTracking ) return null;
  58. if( !obj.SupportsStabilization() ) return null;
  59. var info = obj.Get<TranslationInfo>();
  60. info.IsAwake = info.IsAwake || isAwakening;
  61. return info;
  62. }
  63. public static T Get<T>( this object obj )
  64. where T : new()
  65. {
  66. lock( Sync )
  67. {
  68. if( DynamicFields.TryGetValue( obj, out object value ) )
  69. {
  70. return (T)value;
  71. }
  72. else
  73. {
  74. var t = new T();
  75. DynamicFields[ obj ] = t;
  76. return t;
  77. }
  78. }
  79. }
  80. public static void Cull()
  81. {
  82. lock( Sync )
  83. {
  84. DynamicFields.RemoveCollectedEntries();
  85. }
  86. }
  87. public static List<KeyValuePair<object, object>> GetAllRegisteredObjects()
  88. {
  89. lock( Sync )
  90. {
  91. return DynamicFields.ToList();
  92. }
  93. }
  94. public static void Remove( object obj )
  95. {
  96. lock( Sync )
  97. {
  98. DynamicFields.Remove( obj );
  99. }
  100. }
  101. }
  102. }