123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
-
- namespace UnityEngine.Purchasing
- {
- internal class UDPReflectionUtils
- {
- internal const BindingFlags k_InstanceBindingFlags = BindingFlags.Public | BindingFlags.Instance;
- internal const BindingFlags k_StaticBindingFlags = BindingFlags.Public | BindingFlags.Static;
- internal const BindingFlags k_PrivateStaticBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
-
- static readonly Dictionary<Assembly, Type[]> s_assemblyTypeCache = new Dictionary<Assembly, Type[]>();
- static readonly Dictionary<string, Type> s_typeCache = new Dictionary<string, Type>();
- static readonly string[] k_whiteListedAssemblies = { "UnityEngine", "UnityEditor", "UDP", "com.unity" };
-
- internal static Type GetTypeByName(string typeName)
- {
- if (s_typeCache.ContainsKey(typeName))
- {
- return s_typeCache[typeName];
- }
-
- var assemblies = GetAllAssemblies();
-
- foreach (var assembly in assemblies)
- {
- if (Array.Exists(k_whiteListedAssemblies, x => assembly.FullName.StartsWith(x)))
- {
- var types = GetTypes(assembly);
- foreach (var type in types)
- {
- if (type.FullName == typeName)
- {
- s_typeCache[type.FullName] = type;
- return type;
- }
- }
- }
- }
- return null;
- }
-
- static IEnumerable<Assembly> GetAllAssemblies()
- {
- return AppDomain.CurrentDomain.GetAssemblies();
- }
-
- static IEnumerable<Type> GetTypes(Assembly assembly)
- {
- if (!s_assemblyTypeCache.TryGetValue(assembly, out var types))
- {
- types = assembly.GetTypes();
- s_assemblyTypeCache[assembly] = types;
- }
-
- return types;
- }
- }
-
- internal class AppStoreSettingsInterface
- {
- static Type s_typeCache;
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_AppStoreSettingsType);
- }
- return s_typeCache;
- }
-
- internal static FieldInfo GetClientIDField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsClientIDField, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static FieldInfo GetAppSlugField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsAppSlugField, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static FieldInfo GetAssetPathField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_AppStoreSettingsAssetPathField, UDPReflectionUtils.k_StaticBindingFlags);
- }
- }
-
- internal class BuildConfigInterface
- {
- static Type s_typeCache;
-
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_BuildConfigType);
- }
- return s_typeCache;
- }
-
- static FieldInfo GetApiEndpointField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigApiEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
- }
-
- internal static string GetApiEndpoint()
- {
- return (string)GetApiEndpointField().GetValue(null);
- }
-
- static FieldInfo GetIdEndpointField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigIdEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
- }
-
- internal static string GetIdEndpoint()
- {
- return (string)GetIdEndpointField().GetValue(null);
- }
-
- static FieldInfo GetUdpEndpointField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigUdpEndpointField, UDPReflectionUtils.k_StaticBindingFlags);
- }
-
- internal static string GetUdpEndpoint()
- {
- return (string)GetUdpEndpointField().GetValue(null);
- }
-
- static FieldInfo GetVersionField()
- {
- return GetClassType().GetField(UDPReflectionConsts.k_BuildConfigVersionField, UDPReflectionUtils.k_StaticBindingFlags);
- }
-
- internal static string GetVersion()
- {
- return (string)GetVersionField().GetValue(null);
- }
- }
-
- internal class InventoryInterface
- {
- static Type s_typeCache;
-
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_InventoryType);
- }
- return s_typeCache;
- }
-
- internal static MethodInfo GetProductListMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryGetProductListMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static MethodInfo GetPurchaseInfoMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryGetPurchaseInfoMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static MethodInfo HasPurchaseMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_InventoryHasPurchaseMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
- }
-
- internal class ProductInfoInterface
- {
- static Type s_typeCache;
-
- static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_ProductInfoType);
- }
- return s_typeCache;
- }
-
- public static PropertyInfo GetCurrencyProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoCurrencyProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- public static PropertyInfo GetDescriptionProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoDescProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- public static PropertyInfo GetPriceProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoPriceProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- public static PropertyInfo GetPriceAmountMicrosProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductnfoPriceAmountMicrosProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- public static PropertyInfo GetProductIdProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoIdProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- public static PropertyInfo GetTitleProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_ProductInfoTitleProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
- }
-
- internal class StoreServiceInterface
- {
- static Type s_typeCache;
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_StoreServiceType);
- }
- return s_typeCache;
- }
-
- static PropertyInfo GetNameProp()
- {
- return GetClassType()?.GetProperty(UDPReflectionConsts.k_StoreServiceNameProp, UDPReflectionUtils.k_StaticBindingFlags);
- }
-
- internal static string GetName()
- {
- return (string)GetNameProp()?.GetValue(null, null);
- }
-
- internal static MethodInfo GetEnableDebugLoggingMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_StoreServiceEnableDebugLoggingMethod, UDPReflectionUtils.k_StaticBindingFlags);
- }
- }
-
- internal class UdpIapBridgeInterface
- {
- static Type s_typeCache;
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_UdpIapBridgeType);
- }
- return s_typeCache;
- }
-
- internal static MethodInfo GetInitMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeInitMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static MethodInfo GetPurchaseMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgePurchaseMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static MethodInfo GetRetrieveProductsMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeRetrieveProductsMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static MethodInfo GetFinishTransactionMethod()
- {
- return GetClassType().GetMethod(UDPReflectionConsts.k_UdpIapBridgeFinishTransactionMethod, UDPReflectionUtils.k_InstanceBindingFlags);
- }
- }
-
- internal class UserInfoInterface
- {
- static Type s_typeCache;
- internal static Type GetClassType()
- {
- if (s_typeCache == null)
- {
- s_typeCache = UDPReflectionUtils.GetTypeByName(UDPReflectionConsts.k_UserInfoType);
- }
- return s_typeCache;
- }
-
- internal static PropertyInfo GetChannelProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoChannelProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static PropertyInfo GetIdProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoIdProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
-
- internal static PropertyInfo GetLoginTokenProp()
- {
- return GetClassType().GetProperty(UDPReflectionConsts.k_UserInfoLoginTokenProp, UDPReflectionUtils.k_InstanceBindingFlags);
- }
- }
- }
|