Ejemplo n.º 1
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="groupId">Group identifier.</param>
        /// <param name="data">Data.</param>
        public void SetValue(string groupId, string uid, IAPObjectData data)
        {
            if (_values.ContainsKey(groupId))
            {
                Dictionary <string, IAPObjectData> dict = _values[groupId];

                if (dict.ContainsKey(uid))
                {
                    dict[uid] = data;
                }
                else
                {
                    dict.Add(uid, data);
                }
            }
        }
        /// <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);
        }