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.
 
 
 

181 regels
5.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG;
  4. using SUISS.Core;
  5. using UnityEngine;
  6. public class IAPPackManager
  7. {
  8. private IAPPackManager()
  9. {
  10. }
  11. public IList<string> AllPackNames
  12. {
  13. get
  14. {
  15. if (this._fileCache == null)
  16. {
  17. this.LoadPacksFromDisk();
  18. }
  19. return this._allPackNames.AsReadOnly();
  20. }
  21. }
  22. public static IAPPackManager Instance
  23. {
  24. get
  25. {
  26. if (IAPPackManager._instance == null)
  27. {
  28. IAPPackManager._instance = new IAPPackManager();
  29. }
  30. return IAPPackManager._instance;
  31. }
  32. }
  33. public static bool StoreHasIAPPack(string packName)
  34. {
  35. IAPPack pack = IAPPackManager.Instance.CreatePack(packName);
  36. return SingletonMonobehaviour<CIGStoreManager>.Instance.Store != null && SingletonMonobehaviour<CIGStoreManager>.Instance.Store.FindProduct((CIG3StoreProduct p) => p.GameProductName == pack.Name) != null;
  37. }
  38. public static int LevelsInPack(string packName)
  39. {
  40. int result = 5;
  41. if (SingletonMonobehaviour<CIGGameState>.Instance.Level == Mathf.Max(SingletonMonobehaviour<CIGGameState>.Instance.MaxLevel, 150))
  42. {
  43. return result;
  44. }
  45. IAPPack iappack = IAPPackManager.Instance.CreatePack(packName);
  46. LevelUpItem levelUpItem = iappack.FindPackItem<LevelUpItem>();
  47. if (levelUpItem != null)
  48. {
  49. result = levelUpItem.Levels;
  50. }
  51. else
  52. {
  53. UnityEngine.Debug.LogWarning("IAP level pack does not contain LevelUpItem, assuming 5 in the interface");
  54. }
  55. return result;
  56. }
  57. public IAPPack CreatePack(string packName)
  58. {
  59. return IAPPack.Create(packName, this.GetPackProperties(packName));
  60. }
  61. private Dictionary<string, object> GetPackProperties(string packName)
  62. {
  63. if (this._fileCache == null)
  64. {
  65. this.LoadPacksFromDisk();
  66. }
  67. if (!this._fileCache.ContainsKey("Packs"))
  68. {
  69. return new Dictionary<string, object>();
  70. }
  71. Dictionary<string, object> dictionary = this._fileCache["Packs"] as Dictionary<string, object>;
  72. if (!dictionary.ContainsKey(packName))
  73. {
  74. return new Dictionary<string, object>();
  75. }
  76. Dictionary<string, object> dictionary2 = dictionary[packName] as Dictionary<string, object>;
  77. if (dictionary2 == null)
  78. {
  79. return new Dictionary<string, object>();
  80. }
  81. return dictionary2;
  82. }
  83. private string[] GetAllPackNames()
  84. {
  85. string[] array = new string[this.AllPackNames.Count];
  86. this.AllPackNames.CopyTo(array, 0);
  87. return array;
  88. }
  89. private void LoadPacksFromDisk()
  90. {
  91. bool flag;
  92. this._fileCache = this.LoadJSONFile("iappacks.properties", out flag);
  93. this._allPackNames = new List<string>();
  94. if (!flag)
  95. {
  96. UnityEngine.Debug.LogError(string.Format("There was an error loading/parsing JSON file '{0}'.", "iappacks.properties"));
  97. return;
  98. }
  99. try
  100. {
  101. Dictionary<string, object> dictionary = this._fileCache["Packs"] as Dictionary<string, object>;
  102. foreach (KeyValuePair<string, object> keyValuePair in dictionary)
  103. {
  104. this._allPackNames.Add(keyValuePair.Key);
  105. }
  106. }
  107. catch (Exception exception)
  108. {
  109. UnityEngine.Debug.LogException(exception);
  110. }
  111. }
  112. private Dictionary<string, object> LoadJSONFile(string file, out bool success)
  113. {
  114. TextAsset textAsset = Resources.Load<TextAsset>(file);
  115. if (textAsset == null)
  116. {
  117. UnityEngine.Debug.LogError(string.Format("Can't load text asset '{0}'.", file));
  118. success = false;
  119. return new Dictionary<string, object>();
  120. }
  121. object obj = Json.Decode(textAsset.text);
  122. if (obj == null)
  123. {
  124. UnityEngine.Debug.LogError(string.Format("Can't load/parse JSON file '{0}'.", file));
  125. success = false;
  126. return new Dictionary<string, object>();
  127. }
  128. if (!(obj is Dictionary<string, object>))
  129. {
  130. UnityEngine.Debug.LogError(string.Format("I expected to find a Dictionary<string, object> as root while decoding JSON file '{0}', but I found a '{1}'.", file, obj.GetType()));
  131. success = false;
  132. return new Dictionary<string, object>();
  133. }
  134. Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
  135. if (!dictionary.ContainsKey("Packs"))
  136. {
  137. UnityEngine.Debug.LogError(string.Format("Did not find an object with the key '{0}' while decoding JSON file '{1}'", "Packs", file));
  138. success = false;
  139. return new Dictionary<string, object>();
  140. }
  141. if (!(dictionary["Packs"] is Dictionary<string, object>))
  142. {
  143. UnityEngine.Debug.LogError(string.Format("Expected to find an object of type Dictionary<string, object> for the key '{0}', but I found a '{1}'.", "Packs", dictionary["Packs"].GetType()));
  144. success = false;
  145. return new Dictionary<string, object>();
  146. }
  147. success = true;
  148. return dictionary;
  149. }
  150. public const string File = "iappacks.properties";
  151. public const string PacksKey = "Packs";
  152. public const string Pack5Level = "pack_5_levels";
  153. public const string Pack4x = "pack_4x";
  154. public const string Pack2x = "pack_2x";
  155. public const string PackTimeboost12 = "pack_timeboost_12";
  156. public const string PackTimeboost24 = "pack_timeboost_24";
  157. private List<string> _allPackNames;
  158. private Dictionary<string, object> _fileCache;
  159. private static IAPPackManager _instance;
  160. }