// Use this for initialization void Awake() { //Check if instance already exists if (instance == null) { //if not, set instance to this // Check the settings list if (settings != null) { instance = this; IAPInventoryManager.Log("Initialing..."); /// DEBUG // PlayerPrefs.DeleteAll(); /// DEBUG // Init IAPInventoryManager IAPInventoryManager.Create(settings); IAPInventoryManager.OnCurrencyUpdated += handleOnCurrencyUpdated; IAPInventoryManager.OnAbilityUpdated += handleOnAbilityUpdated; IAPInventoryManager.OnInventoryUpdated += handleOnInventoryUpdated; IAPInventoryManager.OnCurrencyNotEnough += handleOnCurrencyNotEnough; IAPInventoryManager.OnPackageUpdated += handleOnPackageUpdated; IAPInventoryManager.OnGameLevelUpdated += handleOnGameLevelUpdated; // Init the IAPManager if (settings.packageList != null && settings.fetchDataOnStart) { // Add events IAPInventoryManager.OnIAPInitialized += handleOnIAPInitialized; // IAPManager.OnIAPInitializeFailed+=handleOnIAPInitializeFailed; IAPInventoryManager.OnIAPProcessPurchase += handleOnIAPProcessPurchase; IAPInventoryManager.OnIAPPurchaseDeferred += handleOnIAPProcessDeferred; IAPInventoryManager.OnIAPPurchaseFailed += handleOnIAPPurchaseFailed; IAPInventoryManager.OnIAPPurchaseStart += handleOnIAPPurchaseStart; IAPInventoryManager.OnIAPTransactionsRestored += handleOnIAPTransactionsRestored; IAPInventoryManager.InitIAPManager(); } DontDestroyOnLoad(gameObject); } else { IAPInventoryManager.Log("Error: Please check your config."); } } else if (instance != this) { Destroy(gameObject); } }
/////////////////////////////////////////////////////////////// // Private //--- IIAPDelegate private static void handleOnIAPInitialized(Dictionary <string, IAPProduct> products) { _initing = false; if (products != null) { // Loop Settings // Debug.Log("handleOnIAPInitialized " + OnIAPInitialized); List <IAPPackage> packages = IAPInventoryManager.GetPackageList(); foreach (IAPPackage package in packages) { //Debug.Log("Loop package: " + package); if (products.ContainsKey(package.uid)) { //Get the products IAPProduct product = products[package.uid]; // Set the title and description if (string.IsNullOrEmpty(package.title)) { package.title = product.title; } if (string.IsNullOrEmpty(package.description)) { package.description = product.description; } package.priceString = product.priceString; package.isoCurrencyCode = product.isoCurrencyCode; package.product = product; // Debug.LogFormat("uid: {0} hasReceipt {1} {2} amount {3}", package.uid, package.product.hasReceipt, package.product.rawProduct.hasReceipt, package.amount); // Check Subsciprtion Valid if (product.productType == IAPProductType.Subscription) { //Debug.Log("Subsciprtion: " + product.id + " receipt: " + product.hasReceipt); if (product.subscriptionInfo.isSubscribed) { package.Unlock(); } else { package.Lock(); } } } } } IAPManager.OnIAPInitialized -= handleOnIAPInitialized; if (OnIAPInitialized != null) { OnIAPInitialized.Invoke(products); } }
private void handleOnCurrencyNotEnough(IAPCurrency currency) { IAPInventoryManager.ShowErrorDialog(settings.uiSettings.currencyErrorString.Replace("%currency_title%", currency.title), handleCancelButtonPressed); OnCurrencyNotEnough.Invoke(currency); }
/// <summary> /// Create the IAPInventoryManager instance. Call this before using any method. /// </summary> /// <param name="settings">Settings.</param> public static IAPInventoryManager Create(IAPSettings settings) { // Create instance of the IAPManager if (_instance == null) { // Check if settings valid if (settings != null) { _packageSettings = settings.packageList; _uiSettings = settings.uiSettings; _advancedSetting = settings.advancedSettings; // Create datastore switch (settings.databaseSettings.datastoreType) { case IAPDatastoreType.PlayerPref: _datastore = new IAPPlayerPrefDatastore(settings.databaseSettings.playerPrefPrefix); break; case IAPDatastoreType.TextFile: _datastore = new IAPTextFileDatastore(settings.databaseSettings.datastorePath); break; case IAPDatastoreType.XORBinaryFile: _datastore = new IAPBinaryFileDatastore(settings.databaseSettings.datastorePath); break; case IAPDatastoreType.AESEncryptedFile: string hash = settings.databaseSettings.cryptoHash; string salt = SystemInfo.deviceUniqueIdentifier.Substring(0, 8); string key = PlayerPrefs.GetString("RANDOM_KEY"); if (string.IsNullOrEmpty(key)) { key = IAPDatastore.Md5(DateTime.Now.ToString()).Substring(0, 16); PlayerPrefs.SetString("RANDOM_KEY", key); } _datastore = new IAPEncryptedFileDatastore(settings.databaseSettings.datastorePath, hash, key, salt); break; } _datastore.Build(); // Read the data from database Dictionary <string, IAPObjectData> currencyListData = _datastore.GetGroup("currencyList"); Dictionary <string, IAPObjectData> abilityListData = _datastore.GetGroup("abilityList"); Dictionary <string, IAPObjectData> inventoryListData = _datastore.GetGroup("inventoryList"); Dictionary <string, IAPObjectData> packageListData = _datastore.GetGroup("inventoryList"); Dictionary <string, IAPObjectData> gameListData = _datastore.GetGroup("gameList"); // Create the object context list _currencyIndex = new Dictionary <string, IAPCurrency>(); _inventoryIndex = new Dictionary <string, IAPInventory>(); _abilityIndex = new Dictionary <string, IAPAbility>(); _packageIndex = new Dictionary <string, IAPPackage>(); _gameIndex = new Dictionary <string, IAPGameLevel>(); _inventoryTagDictionary = new Dictionary <string, List <IAPInventory> >(); _abilityTagDictionary = new Dictionary <string, List <IAPAbility> >(); _packageTagDictionary = new Dictionary <string, List <IAPPackage> >(); _tags = settings.tagList; // check if need to save bool needSave = false; // Tags if (settings.tagList != null) { // Loop the settings and fill in the list foreach (string s in IAPSettings.DefaultTag) { _inventoryTagDictionary.Add(s, new List <IAPInventory>()); _packageTagDictionary.Add(s, new List <IAPPackage>()); _abilityTagDictionary.Add(s, new List <IAPAbility>()); } } // Check if currency list and inventory list valid if (settings.currencyList != null) { // Loop the settings and fill in the list foreach (IAPCurrencySetting s in settings.currencyList) { if (_currencyIndex.ContainsKey(s.uid)) { Log("Currency identify [" + s.uid + "] already exisit"); } else { IAPCurrency currency; // check if have saved data if (currencyListData.ContainsKey(s.uid)) { currency = new IAPCurrency(s.uid, s, currencyListData[s.uid], handleCurrencySave, handleCurrencyNotEnough); } else { // No saved data IAPObjectData d = s.data; currency = new IAPCurrency(s.uid, s, d, handleCurrencySave, handleCurrencyNotEnough); currencyListData.Add(s.uid, d); needSave = true; } _currencyIndex.Add(s.uid, currency); } } } // Check if ability list and inventory list valid if (settings.abilityList != null) { // Loop the settings and fill in the list foreach (IAPAbilitySetting s in settings.abilityList) { if (_abilityIndex.ContainsKey(s.uid)) { Log("Currency identify [" + s.uid + "] already exisit"); } else { IAPAbility ability; // Debug.Log("ability "+s.data); // check if have saved data if (abilityListData.ContainsKey(s.uid)) { ability = new IAPAbility(s.uid, s, abilityListData[s.uid], handleAbilitySave); } else { IAPObjectData d = s.data; ability = new IAPAbility(s.uid, s, d, handleAbilitySave); abilityListData.Add(s.uid, d); needSave = true; } // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; if (tag == "Ability") { ability.tags = ability.tags | 4; _abilityTagDictionary[tag].Add(ability); } else { int index = 1 << i; if ((index & s.tags) > 0) { if (!_abilityTagDictionary.ContainsKey(tag)) { _abilityTagDictionary.Add(tag, new List <IAPAbility>()); } _abilityTagDictionary[tag].Add(ability); } } } _abilityIndex.Add(s.uid, ability); } } } // Check if inventory list and inventory list valid if (settings.inventoryList != null) { foreach (IAPInventorySetting s in settings.inventoryList) { if (_inventoryIndex.ContainsKey(s.uid)) { Log("inventory identify [" + s.uid + "] already exisit"); } else { IAPInventory inventory; if (inventoryListData.ContainsKey(s.uid)) { inventory = new IAPInventory(s.uid, s, inventoryListData[s.uid], handleInventorySave); } else { IAPObjectData d = s.data; inventory = new IAPInventory(s.uid, s, d, handleInventorySave); inventoryListData.Add(s.uid, d); needSave = true; } // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; int index = 1 << i; if ((index & s.tags) > 0) { if (!_inventoryTagDictionary.ContainsKey(tag)) { _inventoryTagDictionary.Add(tag, new List <IAPInventory>()); } _inventoryTagDictionary[tag].Add(inventory); } } _inventoryIndex.Add(s.uid, inventory); } } } // Check if game list and inventory list valid if (settings.gameList != null) { // Loop the settings and fill in the list foreach (IAPGameLevelSetting s in settings.gameList) { if (_gameIndex.ContainsKey(s.uid)) { Log("Game identify [" + s.uid + "] already exisit"); } else { IAPGameLevel game; // Debug.LogFormat("Game {0}",s.uid); // check if have saved data if (gameListData.ContainsKey(s.uid)) { game = new IAPGameLevel(s.uid, s, gameListData[s.uid], handleGameLevelSave); } else { IAPObjectData d = s.data; game = new IAPGameLevel(s.uid, s, d, handleGameLevelSave); gameListData.Add(s.uid, d); needSave = true; } _gameIndex.Add(s.uid, game); } } } // Check if package list valid if (settings.packageList != null) { foreach (IAPPackageSetting s in settings.packageList) { if (_packageIndex.ContainsKey(s.productId)) { Log("Product ID [" + s.productId + "] already exisit"); } else { IAPPackage package; if (packageListData.ContainsKey(s.productId)) { package = new IAPPackage(s.productId, s, packageListData[s.productId], handlePackageSave); } else { IAPObjectData d = s.data; package = new IAPPackage(s.productId, s, d, handlePackageSave); inventoryListData.Add(s.productId, d); needSave = true; } _packageIndex.Add(s.productId, package); // Fill the tags for (int i = 0; i < _tags.Count; i++) { string tag = _tags[i]; if (tag == "IAP") { package.tags = package.tags | 1; _packageTagDictionary[tag].Add(package); } else if (tag == "IAP_Fetch" && package.fetchFromStore) { package.tags = package.tags | 2; _packageTagDictionary[tag].Add(package); } else { int index = 1 << i; if ((index & s.tags) > 0) { if (!_packageTagDictionary.ContainsKey(tag)) { _packageTagDictionary.Add(tag, new List <IAPPackage>()); } _packageTagDictionary[tag].Add(package); } } } } } } // Create instance _instance = new IAPInventoryManager(); // Save change if (needSave) { Save(); } } return(null); } return(_instance); }