TapTapSDK.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Linq;
  4. using TapSDK.Core.Internal;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using System.Reflection;
  8. using TapSDK.Core.Internal.Init;
  9. using TapSDK.Core.Internal.Log;
  10. using System.ComponentModel;
  11. namespace TapSDK.Core {
  12. public class TapTapSDK {
  13. public static readonly string Version = "4.8.2";
  14. public static string SDKPlatform = "TapSDK-Unity";
  15. public static TapTapSdkOptions taptapSdkOptions;
  16. private static ITapCorePlatform platformWrapper;
  17. private static bool disableDurationStatistics;
  18. public static bool DisableDurationStatistics {
  19. get => disableDurationStatistics;
  20. set {
  21. disableDurationStatistics = value;
  22. }
  23. }
  24. static TapTapSDK() {
  25. platformWrapper = PlatformTypeUtils.CreatePlatformImplementationObject(typeof(ITapCorePlatform),
  26. "TapSDK.Core") as ITapCorePlatform;
  27. }
  28. public static void Init(TapTapSdkOptions coreOption)
  29. {
  30. if (coreOption == null)
  31. throw new ArgumentException("[TapSDK] options is null!");
  32. TapTapSDK.taptapSdkOptions = coreOption;
  33. TapLog.Enabled = coreOption.enableLog;
  34. platformWrapper?.Init(coreOption);
  35. // 初始化各个模块
  36. Type[] initTaskTypes = GetInitTypeList();
  37. if (initTaskTypes != null)
  38. {
  39. List<IInitTask> initTasks = new List<IInitTask>();
  40. foreach (Type initTaskType in initTaskTypes)
  41. {
  42. initTasks.Add(Activator.CreateInstance(initTaskType) as IInitTask);
  43. }
  44. initTasks = initTasks.OrderBy(task => task.Order).ToList();
  45. foreach (IInitTask task in initTasks)
  46. {
  47. TapLogger.Debug($"Init: {task.GetType().Name}");
  48. task.Init(coreOption);
  49. }
  50. }
  51. TapTapEvent.Init(HandleEventOptions(coreOption));
  52. }
  53. public static void Init(TapTapSdkOptions coreOption, TapTapSdkBaseOptions[] otherOptions)
  54. {
  55. if (coreOption == null)
  56. throw new ArgumentException("[TapSDK] options is null!");
  57. TapTapSDK.taptapSdkOptions = coreOption;
  58. TapLog.Enabled = coreOption.enableLog;
  59. platformWrapper?.Init(coreOption, otherOptions);
  60. Type[] initTaskTypes = GetInitTypeList();
  61. if (initTaskTypes != null)
  62. {
  63. List<IInitTask> initTasks = new List<IInitTask>();
  64. foreach (Type initTaskType in initTaskTypes)
  65. {
  66. initTasks.Add(Activator.CreateInstance(initTaskType) as IInitTask);
  67. }
  68. initTasks = initTasks.OrderBy(task => task.Order).ToList();
  69. foreach (IInitTask task in initTasks)
  70. {
  71. TapLog.Log($"Init: {task.GetType().Name}");
  72. task.Init(coreOption, otherOptions);
  73. }
  74. }
  75. TapTapEvent.Init(HandleEventOptions(coreOption, otherOptions));
  76. }
  77. /// <summary>
  78. /// 通过初始化属性设置 TapEvent 属性,兼容旧版本
  79. /// </summary>
  80. /// <param name="coreOption"></param>
  81. /// <param name="otherOptions"></param>
  82. /// <returns>TapEvent 属性</returns>
  83. private static TapTapEventOptions HandleEventOptions(TapTapSdkOptions coreOption, TapTapSdkBaseOptions[] otherOptions = null)
  84. {
  85. TapTapEventOptions tapEventOptions = null;
  86. if (otherOptions != null && otherOptions.Length > 0)
  87. {
  88. foreach (TapTapSdkBaseOptions otherOption in otherOptions)
  89. {
  90. if (otherOption is TapTapEventOptions option)
  91. {
  92. tapEventOptions = option;
  93. }
  94. }
  95. }
  96. if (tapEventOptions == null)
  97. {
  98. tapEventOptions = new TapTapEventOptions();
  99. if (coreOption != null)
  100. {
  101. tapEventOptions.channel = coreOption.channel;
  102. tapEventOptions.disableAutoLogDeviceLogin = coreOption.disableAutoLogDeviceLogin;
  103. tapEventOptions.enableAutoIAPEvent = coreOption.enableAutoIAPEvent;
  104. tapEventOptions.overrideBuiltInParameters = coreOption.overrideBuiltInParameters;
  105. tapEventOptions.propertiesJson = coreOption.propertiesJson;
  106. }
  107. }
  108. return tapEventOptions;
  109. }
  110. // UpdateLanguage 方法
  111. public static void UpdateLanguage(TapTapLanguageType language)
  112. {
  113. platformWrapper?.UpdateLanguage(language);
  114. }
  115. // 是否通过 PC 启动器唤起游戏
  116. public static Task<bool> IsLaunchedFromTapTapPC()
  117. {
  118. return platformWrapper?.IsLaunchedFromTapTapPC();
  119. }
  120. private static Type[] GetInitTypeList(){
  121. Type interfaceType = typeof(IInitTask);
  122. Type[] initTaskTypes = AppDomain.CurrentDomain.GetAssemblies()
  123. .Where(asssembly => asssembly.GetName().FullName.StartsWith("TapSDK"))
  124. .SelectMany(assembly => assembly.GetTypes())
  125. .Where(clazz => interfaceType.IsAssignableFrom(clazz) && clazz.IsClass)
  126. .ToArray();
  127. return initTaskTypes;
  128. }
  129. }
  130. }