Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UDPReflectionUtil.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace UnityEngine.Purchasing
  5. {
  6. internal class UDPReflectionUtils
  7. {
  8. internal const BindingFlags k_InstanceBindingFlags = BindingFlags.Public | BindingFlags.Instance;
  9. internal const BindingFlags k_StaticBindingFlags = BindingFlags.Public | BindingFlags.Static;
  10. internal const BindingFlags k_PrivateStaticBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
  11. static readonly Dictionary<Assembly, Type[]> s_assemblyTypeCache = new Dictionary<Assembly, Type[]>();
  12. static readonly Dictionary<string, Type> s_typeCache = new Dictionary<string, Type>();
  13. static readonly string[] k_whiteListedAssemblies = { "UnityEngine", "UnityEditor", "UDP", "com.unity" };
  14. internal static Type GetTypeByName(string typeName)
  15. {
  16. if (s_typeCache.ContainsKey(typeName))
  17. {
  18. return s_typeCache[typeName];
  19. }
  20. var assemblies = GetAllAssemblies();
  21. foreach (var assembly in assemblies)
  22. {
  23. if (Array.Exists(k_whiteListedAssemblies, x => assembly.FullName.StartsWith(x)))
  24. {
  25. var types = GetTypes(assembly);
  26. foreach (var type in types)
  27. {
  28. if (type.FullName == typeName)
  29. {
  30. s_typeCache[type.FullName] = type;
  31. return type;
  32. }
  33. }
  34. }
  35. }
  36. return null;
  37. }
  38. static IEnumerable<Assembly> GetAllAssemblies()
  39. {
  40. return AppDomain.CurrentDomain.GetAssemblies();
  41. }
  42. static IEnumerable<Type> GetTypes(Assembly assembly)
  43. {
  44. if (!s_assemblyTypeCache.TryGetValue(assembly, out var types))
  45. {
  46. types = assembly.GetTypes();
  47. s_assemblyTypeCache[assembly] = types;
  48. }
  49. return types;
  50. }
  51. }
  52. internal class AppStoreSettingsInterface
  53. {
  54. static Type s_typeCache;
  55. internal static Type GetClassType()
  56. {
  57. if (s_typeCache == null)
  58. {
  59. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_AppStoreSettingsType);
  60. }
  61. return s_typeCache;
  62. }
  63. internal static FieldInfo GetClientIDField()
  64. {
  65. return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsClientIDField, UDPReflectionUtils.k_InstanceBindingFlags);
  66. }
  67. internal static FieldInfo GetAppSlugField()
  68. {
  69. return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsAppSlugField, UDPReflectionUtils.k_InstanceBindingFlags);
  70. }
  71. internal static FieldInfo GetAssetPathField()
  72. {
  73. return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsAssetPathField, UDPReflectionUtils.k_StaticBindingFlags);
  74. }
  75. }
  76. internal class BuildConfigInterface
  77. {
  78. static Type s_typeCache;
  79. internal static Type GetClassType()
  80. {
  81. if (s_typeCache == null)
  82. {
  83. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_BuildConfigType);
  84. }
  85. return s_typeCache;
  86. }
  87. static FieldInfo GetApiEndpointField()
  88. {
  89. return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigApiEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
  90. }
  91. internal static string GetApiEndpoint()
  92. {
  93. return (string)GetApiEndpointField().GetValue(null);
  94. }
  95. static FieldInfo GetIdEndpointField()
  96. {
  97. return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigIdEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
  98. }
  99. internal static string GetIdEndpoint()
  100. {
  101. return (string)GetIdEndpointField().GetValue(null);
  102. }
  103. static FieldInfo GetUdpEndpointField()
  104. {
  105. return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigUdpEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
  106. }
  107. internal static string GetUdpEndpoint()
  108. {
  109. return (string)GetUdpEndpointField().GetValue(null);
  110. }
  111. static FieldInfo GetVersionField()
  112. {
  113. return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigVersionField, UDPReflectionUtils.k_StaticBindingFlags);
  114. }
  115. internal static string GetVersion()
  116. {
  117. return (string)GetVersionField().GetValue(null);
  118. }
  119. }
  120. internal class InventoryInterface
  121. {
  122. static Type s_typeCache;
  123. internal static Type GetClassType()
  124. {
  125. if (s_typeCache == null)
  126. {
  127. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_InventoryType);
  128. }
  129. return s_typeCache;
  130. }
  131. internal static MethodInfo GetProductListMethod()
  132. {
  133. return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryGetProductListMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  134. }
  135. internal static MethodInfo GetPurchaseInfoMethod()
  136. {
  137. return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryGetPurchaseInfoMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  138. }
  139. internal static MethodInfo HasPurchaseMethod()
  140. {
  141. return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryHasPurchaseMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  142. }
  143. }
  144. internal class ProductInfoInterface
  145. {
  146. static Type s_typeCache;
  147. static Type GetClassType()
  148. {
  149. if (s_typeCache == null)
  150. {
  151. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_ProductInfoType);
  152. }
  153. return s_typeCache;
  154. }
  155. public static PropertyInfo GetCurrencyProp()
  156. {
  157. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoCurrencyProp, UDPReflectionUtils.k_InstanceBindingFlags);
  158. }
  159. public static PropertyInfo GetDescriptionProp()
  160. {
  161. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoDescProp, UDPReflectionUtils.k_InstanceBindingFlags);
  162. }
  163. public static PropertyInfo GetPriceProp()
  164. {
  165. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoPriceProp, UDPReflectionUtils.k_InstanceBindingFlags);
  166. }
  167. public static PropertyInfo GetPriceAmountMicrosProp()
  168. {
  169. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductnfoPriceAmountMicrosProp, UDPReflectionUtils.k_InstanceBindingFlags);
  170. }
  171. public static PropertyInfo GetProductIdProp()
  172. {
  173. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoIdProp, UDPReflectionUtils.k_InstanceBindingFlags);
  174. }
  175. public static PropertyInfo GetTitleProp()
  176. {
  177. return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoTitleProp, UDPReflectionUtils.k_InstanceBindingFlags);
  178. }
  179. }
  180. internal class StoreServiceInterface
  181. {
  182. static Type s_typeCache;
  183. internal static Type GetClassType()
  184. {
  185. if (s_typeCache == null)
  186. {
  187. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_StoreServiceType);
  188. }
  189. return s_typeCache;
  190. }
  191. static PropertyInfo GetNameProp()
  192. {
  193. return GetClassType()?.GetProperty(UDPReflectionConsts.k_StoreServiceNameProp, UDPReflectionUtils.k_StaticBindingFlags);
  194. }
  195. internal static string GetName()
  196. {
  197. return (string)GetNameProp()?.GetValue(null, null);
  198. }
  199. internal static MethodInfo GetEnableDebugLoggingMethod()
  200. {
  201. return GetClassType().GetMethod(UDPReflectionConsts.k_StoreServiceEnableDebugLoggingMethod, UDPReflectionUtils.k_StaticBindingFlags);
  202. }
  203. }
  204. internal class UdpIapBridgeInterface
  205. {
  206. static Type s_typeCache;
  207. internal static Type GetClassType()
  208. {
  209. if (s_typeCache == null)
  210. {
  211. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_UdpIapBridgeType);
  212. }
  213. return s_typeCache;
  214. }
  215. internal static MethodInfo GetInitMethod()
  216. {
  217. return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeInitMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  218. }
  219. internal static MethodInfo GetPurchaseMethod()
  220. {
  221. return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgePurchaseMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  222. }
  223. internal static MethodInfo GetRetrieveProductsMethod()
  224. {
  225. return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeRetrieveProductsMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  226. }
  227. internal static MethodInfo GetFinishTransactionMethod()
  228. {
  229. return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeFinishTransactionMethod, UDPReflectionUtils.k_InstanceBindingFlags);
  230. }
  231. }
  232. internal class UserInfoInterface
  233. {
  234. static Type s_typeCache;
  235. internal static Type GetClassType()
  236. {
  237. if (s_typeCache == null)
  238. {
  239. s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_UserInfoType);
  240. }
  241. return s_typeCache;
  242. }
  243. internal static PropertyInfo GetChannelProp()
  244. {
  245. return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoChannelProp, UDPReflectionUtils.k_InstanceBindingFlags);
  246. }
  247. internal static PropertyInfo GetIdProp()
  248. {
  249. return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoIdProp, UDPReflectionUtils.k_InstanceBindingFlags);
  250. }
  251. internal static PropertyInfo GetLoginTokenProp()
  252. {
  253. return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoLoginTokenProp, UDPReflectionUtils.k_InstanceBindingFlags);
  254. }
  255. }
  256. }