using System; using System.Collections; using System.Collections.Generic; using UnityEngine.Purchasing.Extension; namespace UnityEngine.Purchasing { /// /// Maps store specific Product identifiers to one /// or more store identifiers. /// /// The name is deliberately terse for use as a collection initializer. /// public class IDs : IEnumerable> { private readonly Dictionary m_Dic = new Dictionary(); /// /// Returns an enumerator that iterates through the collection. /// /// An IEnumerator object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() { return m_Dic.GetEnumerator(); } /// /// Add a product identifier to a list of store names with string. /// /// Product identifier. /// List of stores by string, to which we the id will be mapped to. public void Add(string id, params string[] stores) { foreach (var store in stores) { m_Dic[store] = id; } } /// /// Add a product identifier to a list of store names with non strings such as Enums. /// /// Product identifier. /// List of stores by other object, to which we the id will be mapped to. public void Add(string id, params object[] stores) { foreach (var store in stores) { m_Dic[store.ToString()] = id; } } internal string SpecificIDForStore(string store, string defaultValue) { if (m_Dic.ContainsKey(store)) { return m_Dic[store]; } return defaultValue; } /// /// Retrieve an Enumerator with which can be used to iterate through the internal map structure. /// /// Enumerator as a Key/Value pair. public IEnumerator> GetEnumerator() { return m_Dic.GetEnumerator(); } } /// /// Builds configuration for Unity Purchasing, /// consisting of products and store specific configuration details. /// public class ConfigurationBuilder { internal ConfigurationBuilder(PurchasingFactory factory) { this.factory = factory; } /// /// Whether or not the project will use the catalog stored on the cloud or the one cached locally. /// /// True if the project will use the catalog stored on the cloud. public bool useCatalogProvider { get; set; } /// /// The set of products in the catalog. /// public HashSet products { get; } = new HashSet(); internal PurchasingFactory factory { get; } /// /// Configure the store as specified by the template parameter. /// /// Implementation of IStoreConfiguration /// The store configuration as an object. public T Configure() where T : IStoreConfiguration { return factory.GetConfig(); } /// /// Create an instance of the configuration builder. /// /// The first purchasing module. /// The remaining purchasing modules, excluding the one passes as first. /// The instance of the configuration builder as specified. public static ConfigurationBuilder Instance(IPurchasingModule first, params IPurchasingModule[] rest) { var factory = new PurchasingFactory(first, rest); return new ConfigurationBuilder(factory); } /// /// Add a product to the configuration builder. /// /// The id of the product. /// The type of the product. /// The instance of the configuration builder with the new product added. public ConfigurationBuilder AddProduct(string id, ProductType type) { return AddProduct(id, type, null); } /// /// Add a product to the configuration builder. /// /// The id of the product. /// The type of the product. /// The object representing store IDs the product is to be added to. /// The instance of the configuration builder with the new product added. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs) { return AddProduct(id, type, storeIDs, (IEnumerable)null); } /// /// Add a product to the configuration builder. /// /// The id of the product. /// The type of the product. /// The object representing store IDs the product is to be added to. /// The payout definition of the product. /// The instance of the configuration builder with the new product added. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, PayoutDefinition payout) { return AddProduct(id, type, storeIDs, new List { payout }); } /// /// Add a product to the configuration builder. /// /// The id of the product. /// The type of the product. /// The object representing store IDs the product is to be added to. /// The enumerator of the payout definitions of the product. /// The instance of the configuration builder with the new product added. public ConfigurationBuilder AddProduct(string id, ProductType type, IDs storeIDs, IEnumerable payouts) { var specificId = id; // Extract our store specific ID if present, according to the current store. if (storeIDs != null) { specificId = storeIDs.SpecificIDForStore(factory.storeName, id); } var product = new ProductDefinition(id, specificId, type); product.SetPayouts(payouts); products.Add(product); return this; } /// /// Add multiple products to the configuration builder. /// /// The enumerator of the product definitions to be added. /// The instance of the configuration builder with the new product added. public ConfigurationBuilder AddProducts(IEnumerable products) { foreach (var product in products) { this.products.Add(product); } return this; } } }