ConfigurationBuilder.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /// Whether or not the project will use the catalog stored on the cloud or the one cached locally.
  74. /// </summary>
  75. /// <value> True if the project will use the catalog stored on the cloud. </value>
  76. public bool useCatalogProvider
  77. {
  78. get;
  79. set;
  80. }
  81. /// <summary>
  82. /// The set of products in the catalog.
  83. /// </summary>
  84. public HashSet<ProductDefinition> products
  85. {
  86. get { return m_Products; }
  87. }
  88. internal PurchasingFactory factory
  89. {
  90. get { return m_Factory; }
  91. }
  92. /// <summary>
  93. /// Configure the store as specified by the template parameter.
  94. /// </summary>
  95. /// <typeparam name="T"> Implementation of <c>IStoreConfiguration</c> </typeparam>
  96. /// <returns> The store configuration as an object. </returns>
  97. public T Configure<T>() where T : IStoreConfiguration
  98. {
  99. return m_Factory.GetConfig<T>();
  100. }
  101. /// <summary>
  102. /// Create an instance of the configuration builder.
  103. /// </summary>
  104. /// <param name="first"> The first purchasing module. </param>
  105. /// <param name="rest"> The remaining purchasing modules, excluding the one passes as first. </param>
  106. /// <returns> The instance of the configuration builder as specified. </returns>
  107. public static ConfigurationBuilder Instance(IPurchasingModule first, params IPurchasingModule[] rest)
  108. {
  109. PurchasingFactory factory = new PurchasingFactory(first, rest);
  110. return new ConfigurationBuilder(factory);
  111. }
  112. /// <summary>
  113. /// Add a product to the configuration builder.
  114. /// </summary>
  115. /// <param name="id"> The id of the product. </param>
  116. /// <param name="type"> The type of the product. </param>
  117. /// <returns> The instance of the configuration builder with the new product added. </returns>
  118. public ConfigurationBuilder AddProduct(string id, ProductType type)
  119. {
  120. return AddProduct(id, type, null);
  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. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  128. /// <returns> The instance of the configuration builder with the new product added. </returns>
  129. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs)
  130. {
  131. return AddProduct(id, type, storeIDs, (IEnumerable<PayoutDefinition>)null);
  132. }
  133. /// <summary>
  134. /// Add a product to the configuration builder.
  135. /// </summary>
  136. /// <param name="id"> The id of the product. </param>
  137. /// <param name="type"> The type of the product. </param>
  138. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  139. /// <param name="payout"> The payout definition of the product. </param>
  140. /// <returns> The instance of the configuration builder with the new product added. </returns>
  141. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, PayoutDefinition payout)
  142. {
  143. return AddProduct(id, type, storeIDs, new List<PayoutDefinition> { payout });
  144. }
  145. /// <summary>
  146. /// Add a product to the configuration builder.
  147. /// </summary>
  148. /// <param name="id"> The id of the product. </param>
  149. /// <param name="type"> The type of the product. </param>
  150. /// <param name="storeIDs"> The object representing store IDs the product is to be added to. </param>
  151. /// <param name="payouts"> The enumerator of the payout definitions of the product. </param>
  152. /// <returns> The instance of the configuration builder with the new product added. </returns>
  153. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, IEnumerable<PayoutDefinition> payouts)
  154. {
  155. var specificId = id;
  156. // Extract our store specific ID if present, according to the current store.
  157. if (storeIDs != null)
  158. specificId = storeIDs.SpecificIDForStore(factory.storeName, id);
  159. var product = new ProductDefinition(id, specificId, type);
  160. product.SetPayouts(payouts);
  161. m_Products.Add(product);
  162. return this;
  163. }
  164. /// <summary>
  165. /// Add multiple products to the configuration builder.
  166. /// </summary>
  167. /// <param name="products"> The enumerator of the product definitions to be added. </param>
  168. /// <returns> The instance of the configuration builder with the new product added. </returns>
  169. public ConfigurationBuilder AddProducts(IEnumerable<ProductDefinition> products)
  170. {
  171. foreach (var product in products)
  172. {
  173. m_Products.Add(product);
  174. }
  175. return this;
  176. }
  177. }
  178. }