BridgeUtils.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using TapSDK.Core.Internal.Log;
  6. namespace TapSDK.Core.Internal.Utils {
  7. public static class BridgeUtils {
  8. public static bool IsSupportMobilePlatform => Application.platform == RuntimePlatform.Android ||
  9. Application.platform == RuntimePlatform.IPhonePlayer;
  10. public static bool IsSupportStandalonePlatform => Application.platform == RuntimePlatform.OSXPlayer ||
  11. Application.platform == RuntimePlatform.WindowsPlayer ||
  12. Application.platform == RuntimePlatform.LinuxPlayer;
  13. public static object CreateBridgeImplementation(Type interfaceType, string startWith) {
  14. TapLog.Log($"[TapTap] 开始查找实现类: interfaceType={interfaceType.FullName}, startWith={startWith}, 当前平台={Application.platform}");
  15. // 跳过初始化直接使用 TapLoom会在子线程被TapSDK.Core.BridgeCallback.Invoke 初始化
  16. TapLoom.Initialize();
  17. // 获取所有程序集
  18. var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  19. TapLog.Log($"[TapTap] 已加载的程序集总数: {allAssemblies.Length}");
  20. // 查找以 startWith 开头的程序集
  21. var matchingAssemblies = allAssemblies
  22. .Where(assembly => assembly.GetName().FullName.StartsWith(startWith))
  23. .ToList();
  24. TapLog.Log($"[TapTap] 找到匹配 '{startWith}' 的程序集数量: {matchingAssemblies.Count}");
  25. // 打印匹配的程序集名称
  26. foreach (var assembly in matchingAssemblies) {
  27. TapLog.Log($"[TapTap] 匹配的程序集: {assembly.GetName().FullName}");
  28. }
  29. // 如果没有找到匹配的程序集,打印所有TapSDK相关的程序集
  30. if (matchingAssemblies.Count == 0) {
  31. var tapAssemblies = allAssemblies
  32. .Where(assembly => assembly.GetName().FullName.Contains("TapSDK") ||
  33. assembly.GetName().FullName.Contains("TapTap"))
  34. .ToList();
  35. TapLog.Log($"[TapTap] 未找到匹配的程序集,但找到 {tapAssemblies.Count} 个相关程序集:");
  36. foreach (var assembly in tapAssemblies) {
  37. TapLog.Log($"[TapTap] - {assembly.GetName().FullName}");
  38. }
  39. }
  40. // 从匹配的程序集中查找实现指定接口的类
  41. List<Type> allCandidateTypes = new List<Type>();
  42. foreach (var assembly in matchingAssemblies) {
  43. try {
  44. var types = assembly.GetTypes()
  45. .Where(type => type.IsClass && interfaceType.IsAssignableFrom(type))
  46. .ToList();
  47. TapLog.Log($"[TapTap] 在程序集 {assembly.GetName().Name} 中找到 {types.Count} 个实现 {interfaceType.Name} 的类");
  48. foreach (var type in types) {
  49. TapLog.Log($"[TapTap] - {type.FullName} (IsPublic: {type.IsPublic}, IsAbstract: {type.IsAbstract})");
  50. allCandidateTypes.Add(type);
  51. }
  52. }
  53. catch (Exception ex) {
  54. TapLog.Error($"[TapTap] 获取程序集 {assembly.GetName().Name} 中的类型时出错: {ex.Message}");
  55. }
  56. }
  57. // 使用原始逻辑查找实现类
  58. Type bridgeImplementationType = null;
  59. try {
  60. bridgeImplementationType = matchingAssemblies
  61. .SelectMany(assembly => {
  62. try {
  63. return assembly.GetTypes();
  64. } catch {
  65. return Type.EmptyTypes;
  66. }
  67. })
  68. .SingleOrDefault(clazz => interfaceType.IsAssignableFrom(clazz) && clazz.IsClass);
  69. TapLog.Log($"[TapTap] SingleOrDefault 查找结果: {(bridgeImplementationType != null ? bridgeImplementationType.FullName : "null")}");
  70. // 如果使用 SingleOrDefault 没找到,尝试使用 FirstOrDefault
  71. if (bridgeImplementationType == null && allCandidateTypes.Count > 0) {
  72. TapLog.Log($"[TapTap] SingleOrDefault 未找到实现,但有 {allCandidateTypes.Count} 个候选类型,尝试使用 FirstOrDefault");
  73. bridgeImplementationType = allCandidateTypes.FirstOrDefault();
  74. TapLog.Log($"[TapTap] FirstOrDefault 查找结果: {(bridgeImplementationType != null ? bridgeImplementationType.FullName : "null")}");
  75. }
  76. // 如果找到多个实现,可能是 SingleOrDefault 失败的原因
  77. if (allCandidateTypes.Count > 1) {
  78. TapLog.Warning($"[TapTap] 找到多个实现 {interfaceType.Name} 的类,这可能导致 SingleOrDefault 返回 null");
  79. foreach (var type in allCandidateTypes) {
  80. TapLog.Warning($"[TapTap] - {type.FullName}");
  81. }
  82. }
  83. }
  84. catch (Exception ex) {
  85. TapLog.Error($"[TapTap] 在查找实现类时发生异常: {ex.Message}\n{ex.StackTrace}");
  86. }
  87. if (bridgeImplementationType == null) {
  88. TapLog.Warning($"[TapTap] TapSDK 无法为 {interfaceType} 找到平台 {Application.platform} 上的实现类。");
  89. // 尝试在所有程序集中查找实现(不限制命名空间前缀)
  90. if (matchingAssemblies.Count == 0) {
  91. TapLog.Log("[TapTap] 尝试在所有程序集中查找实现...");
  92. List<Type> implementationsInAllAssemblies = new List<Type>();
  93. foreach (var assembly in allAssemblies) {
  94. try {
  95. var types = assembly.GetTypes()
  96. .Where(type => type.IsClass && !type.IsAbstract && interfaceType.IsAssignableFrom(type))
  97. .ToList();
  98. if (types.Count > 0) {
  99. TapLog.Log($"[TapTap] 在程序集 {assembly.GetName().Name} 中找到 {types.Count} 个实现");
  100. implementationsInAllAssemblies.AddRange(types);
  101. }
  102. }
  103. catch { /* 忽略错误 */ }
  104. }
  105. if (implementationsInAllAssemblies.Count > 0) {
  106. TapLog.Log($"[TapTap] 在所有程序集中找到 {implementationsInAllAssemblies.Count} 个实现:");
  107. foreach (var type in implementationsInAllAssemblies) {
  108. TapLog.Log($"[TapTap] - {type.FullName} (在程序集 {type.Assembly.GetName().Name} 中)");
  109. }
  110. }
  111. }
  112. return null;
  113. }
  114. try {
  115. TapLog.Log($"[TapTap] 创建 {bridgeImplementationType.FullName} 的实例");
  116. return Activator.CreateInstance(bridgeImplementationType);
  117. }
  118. catch (Exception ex) {
  119. TapLog.Error($"[TapTap] 创建实例时出错: {ex.Message}");
  120. return null;
  121. }
  122. }
  123. }
  124. }