ConfigurationBuilder.cs 8.2 KB

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