1
0

PurchasingFactory.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine.Purchasing.Extension;
  5. namespace UnityEngine.Purchasing
  6. {
  7. /// <summary>
  8. /// Manages instantiation of specific store services based on provided <c>IPurchasingModule</c>s.
  9. /// </summary>
  10. internal class PurchasingFactory : IPurchasingBinder, IExtensionProvider
  11. {
  12. private Dictionary<Type, IStoreConfiguration> m_ConfigMap = new Dictionary<Type, IStoreConfiguration>();
  13. private Dictionary<Type, IStoreExtension> m_ExtensionMap = new Dictionary<Type, IStoreExtension>();
  14. private IStore m_Store;
  15. private ICatalogProvider m_CatalogProvider;
  16. public PurchasingFactory(IPurchasingModule first, params IPurchasingModule[] remainingModules)
  17. {
  18. first.Configure(this);
  19. foreach (var module in remainingModules)
  20. module.Configure(this);
  21. }
  22. public string storeName { get; private set; }
  23. public IStore service
  24. {
  25. get
  26. {
  27. if (m_Store != null)
  28. return m_Store;
  29. throw new InvalidOperationException("No impl available!");
  30. }
  31. set { m_Store = value; }
  32. }
  33. public void RegisterStore(string name, IStore s)
  34. {
  35. // We use the first store that supports our current
  36. // platform.
  37. if (m_Store == null && s != null)
  38. {
  39. storeName = name;
  40. service = s;
  41. }
  42. }
  43. public void RegisterExtension<T>(T instance) where T : IStoreExtension
  44. {
  45. m_ExtensionMap[typeof(T)] = instance;
  46. }
  47. public void RegisterConfiguration<T>(T instance) where T : IStoreConfiguration
  48. {
  49. m_ConfigMap[typeof(T)] = instance;
  50. }
  51. /// <summary>
  52. /// Get the specified <c>IStoreConfiguration</c>.
  53. ///
  54. /// If the store implements the requested interface,
  55. /// it will be returned.
  56. /// </summary>
  57. public T GetConfig<T>() where T : IStoreConfiguration
  58. {
  59. if (service is T)
  60. return (T)service;
  61. var type = typeof(T);
  62. if (m_ConfigMap.ContainsKey(type))
  63. return (T)m_ConfigMap[type];
  64. throw new ArgumentException("No binding for config type " + type);
  65. }
  66. /// Get the specified <c>IStoreExtension</c>.
  67. ///
  68. /// If the store implements the requested interface,
  69. /// it will be returned.
  70. public T GetExtension<T>() where T : IStoreExtension
  71. {
  72. if (service is T)
  73. {
  74. return (T)service;
  75. }
  76. var t = typeof(T);
  77. if (m_ExtensionMap.ContainsKey(t))
  78. {
  79. return (T)m_ExtensionMap[t];
  80. }
  81. throw new ArgumentException("No binding for type " + t);
  82. }
  83. public void SetCatalogProvider(ICatalogProvider provider)
  84. {
  85. m_CatalogProvider = provider;
  86. }
  87. public void SetCatalogProviderFunction(Action<Action<HashSet<ProductDefinition>>> func)
  88. {
  89. m_CatalogProvider = new SimpleCatalogProvider(func);
  90. }
  91. internal ICatalogProvider GetCatalogProvider()
  92. {
  93. return m_CatalogProvider;
  94. }
  95. }
  96. }