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.

ConfigurationBuilder.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Purchasing.Extension;
  5. namespace UnityEngine.Purchasing
  6. {
  7. /// <summary>
  8. /// Maps store specific Product identifiers to one
  9. /// or more store identifiers.
  10. ///
  11. /// The name is deliberately terse for use as a collection initializer.
  12. /// </summary>
  13. public class IDs : IEnumerable<KeyValuePair<string, string>>
  14. {
  15. private readonly Dictionary<string, string> m_Dic = new Dictionary<string, string>();
  16. /// <summary>
  17. /// Returns an enumerator that iterates through the collection.
  18. /// </summary>
  19. /// <returns> An IEnumerator object that can be used to iterate through the collection. </returns>
  20. IEnumerator IEnumerable.GetEnumerator()
  21. {
  22. return m_Dic.GetEnumerator();
  23. }
  24. /// <summary>
  25. /// Add a product identifier to a list of store names with string.
  26. /// </summary>
  27. /// <param name="id"> Product identifier. </param>
  28. /// <param name="stores"> List of stores by string, to which we the id will be mapped to. </param>
  29. public void Add(string id, params string[] stores)
  30. {
  31. foreach (var store in stores)
  32. {
  33. m_Dic[store] = id;
  34. }
  35. }
  36. /// <summary>
  37. /// Add a product identifier to a list of store names with non strings such as Enums.
  38. /// </summary>
  39. /// <param name="id"> Product identifier. </param>
  40. /// <param name="stores"> List of stores by other object, to which we the id will be mapped to. </param>
  41. public void Add(string id, params object[] stores)
  42. {
  43. foreach (var store in stores)
  44. {
  45. m_Dic[store.ToString()] = id;
  46. }
  47. }
  48. internal string SpecificIDForStore(string store, string defaultValue)
  49. {
  50. if (m_Dic.ContainsKey(store))
  51. {
  52. return m_Dic[store];
  53. }
  54. return defaultValue;
  55. }
  56. /// <summary>
  57. /// Retrieve an Enumerator with which can be used to iterate through the internal map structure.
  58. /// </summary>
  59. /// <returns> Enumerator as a Key/Value pair. </returns>
  60. public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
  61. {
  62. return m_Dic.GetEnumerator();
  63. }
  64. }
  65. /// <summary>
  66. /// Builds configuration for Unity Purchasing,
  67. /// consisting of products and store specific configuration details.
  68. /// </summary>
  69. public class ConfigurationBuilder
  70. {
  71. internal ConfigurationBuilder(PurchasingFactory factory)
  72. {
  73. this.factory = factory;
  74. }
  75. /// <summary>
  76. /// Whether or not the project will use the catalog stored on the cloud or the one cached locally.
  77. /// </summary>
  78. /// <value> True if the project will use the catalog stored on the cloud. </value>
  79. public bool useCatalogProvider
  80. {
  81. get;
  82. set;
  83. }
  84. /// <summary>
  85. /// Whether or not unavailable products should be logged as warnings
  86. /// </summary>
  87. /// Default is <c>true</c>.
  88. public bool logUnavailableProducts { get; set; } = true;
  89. /// <summary>
  90. /// The set of products in the catalog.
  91. /// </summary>
  92. public HashSet<ProductDefinition> products { get; } = new HashSet<ProductDefinition>();
  93. internal PurchasingFactory factory { get; }
  94. /// <summary>
  95. /// Configure the store as specified by the template parameter.
  96. /// </summary>
  97. /// <typeparam name="T"> Implementation of <c>IStoreConfiguration</c> </typeparam>
  98. /// <returns> The store configuration as an object. </returns>
  99. public T Configure<T>() where T : IStoreConfiguration
  100. {
  101. return factory.GetConfig<T>();
  102. }
  103. /// <summary>
  104. /// Create an instance of the configuration builder.
  105. /// </summary>
  106. /// <param name="first"> The first purchasing module. </param>
  107. /// <param name="rest"> The remaining purchasing modules, excluding the one passes as first. </param>
  108. /// <returns> The instance of the configuration builder as specified. </returns>
  109. public static ConfigurationBuilder Instance(IPurchasingModule first, params IPurchasingModule[] rest)
  110. {
  111. var factory = new PurchasingFactory(first, rest);
  112. return new ConfigurationBuilder(factory);
  113. }
  114. /// <summary>
  115. /// Add a product to the configuration builder.
  116. /// </summary>
  117. /// <param name="id"> The id of the product. </param>
  118. /// <param name="type"> The type of the product. </param>
  119. /// <returns> The instance of the configuration builder with the new product added. </returns>
  120. public ConfigurationBuilder AddProduct(string id, ProductType type)
  121. {
  122. return AddProduct(id, type, null);
  123. }
  124. /// <summary>
  125. /// Add a product to the configuration builder.
  126. /// </summary>
  127. /// <param name="id"> The id of the product. </param>
  128. /// <param name="type"> The type of the product. </param>
  129. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  130. /// <returns> The instance of the configuration builder with the new product added. </returns>
  131. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs)
  132. {
  133. return AddProduct(id, type, storeIDs, (IEnumerable<PayoutDefinition>)null);
  134. }
  135. /// <summary>
  136. /// Add a product to the configuration builder.
  137. /// </summary>
  138. /// <param name="id"> The id of the product. </param>
  139. /// <param name="type"> The type of the product. </param>
  140. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  141. /// <param name="payout"> The payout definition of the product. </param>
  142. /// <returns> The instance of the configuration builder with the new product added. </returns>
  143. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, PayoutDefinition payout)
  144. {
  145. return AddProduct(id, type, storeIDs, new List<PayoutDefinition> { payout });
  146. }
  147. /// <summary>
  148. /// Add a product to the configuration builder.
  149. /// </summary>
  150. /// <param name="id"> The id of the product. </param>
  151. /// <param name="type"> The type of the product. </param>
  152. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  153. /// <param name="payouts"> The enumerator of the payout definitions of the product. </param>
  154. /// <returns> The instance of the configuration builder with the new product added. </returns>
  155. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, IEnumerable<PayoutDefinition> payouts)
  156. {
  157. var specificId = id;
  158. // Extract our store specific ID if present, according to the current store.
  159. if (storeIDs != null)
  160. {
  161. specificId = storeIDs.SpecificIDForStore(factory.storeName, id);
  162. }
  163. var product = new ProductDefinition(id, specificId, type);
  164. product.SetPayouts(payouts);
  165. products.Add(product);
  166. return this;
  167. }
  168. /// <summary>
  169. /// Add multiple products to the configuration builder.
  170. /// </summary>
  171. /// <param name="products"> The enumerator of the product definitions to be added. </param>
  172. /// <returns> The instance of the configuration builder with the new product added. </returns>
  173. public ConfigurationBuilder AddProducts(IEnumerable<ProductDefinition> products)
  174. {
  175. foreach (var product in products)
  176. {
  177. this.products.Add(product);
  178. }
  179. return this;
  180. }
  181. }
  182. }