public void BackUpDataWhenUpdataVersion(CheckDataModel checkData)
 {
     dataBackUp                        = new DataBackUpWhenVersionUpdate();
     dataBackUp.playerData             = checkData.playerData;
     dataBackUp.buyRecord              = checkData.buyRecord;
     dataBackUp.chatRecords            = checkData.chatRecords;
     dataBackUp.currentMapEventsRecord = checkData.currentMapEventsRecord;
     dataBackUp.gameSettings           = checkData.gameSettings;
     dataBackUp.mapEventsRecords       = checkData.mapEventsRecords;
     dataBackUp.miniMapRecord          = checkData.miniMapRecord;
     dataBackUp.playRecords            = checkData.playRecords;
 }
Example #2
0
        /// <summary>
        /// 持久化数据
        /// </summary>
        public void PersistData()
        {
            DirectoryInfo persistDi = new DirectoryInfo(CommonData.persistDataPath);

            IEnumerator initDataCoroutine = null;

            CheckDataModel checkData = new CheckDataModel();

            MySQLiteHelper sql = MySQLiteHelper.Instance;

            // 检查数据的完整性
            bool dataComplete = CheckDataComplete();

            // 如果原始玩家数据存在的话
            if (File.Exists(CommonData.playerDataFilePath))
            {
                // 记录原始玩家数据
                checkData.playerData = DataHandler.LoadDataToSingleModelWithPath <PlayerData>(CommonData.playerDataFilePath);
                checkData.playerData.needChooseDifficulty = false;

                if (ApplicationInfo.Instance != null)
                {
                    checkData.versionUpdate = ApplicationInfo.Instance.currentVersion + 0.001f < GameManager.Instance.currentVersion;
                }
                else
                {
                    checkData.versionUpdate = true;
                }
            }

            if (checkData.versionUpdate)
            {
                if (File.Exists(CommonData.dataBaseFilePath))
                {
                    sql.GetConnectionWith(CommonData.dataBaseName);

                    sql.BeginTransaction();

                    checkData.learnedWordsInSimple = GetWordRecordsInDataBase(sql, 0);
                    checkData.learnedWordsInMedium = GetWordRecordsInDataBase(sql, 1);
                    checkData.learnedWordsInMaster = GetWordRecordsInDataBase(sql, 2);

                    sql.EndTransaction();
                    sql.CloseConnection(CommonData.dataBaseName);
                }

                checkData.playerData.isNewPlayer = true;

                if (File.Exists(CommonData.buyRecordFilePath))
                {
                    checkData.buyRecord = BuyRecord.Instance;
                }

                if (File.Exists(CommonData.chatRecordsFilePath))
                {
                    checkData.chatRecords = GameManager.Instance.gameDataCenter.chatRecords;
                }

                if (File.Exists(CommonData.mapEventsRecordFilePath))
                {
                    checkData.mapEventsRecords = GameManager.Instance.gameDataCenter.mapEventsRecords;
                }

                if (File.Exists(CommonData.gameSettingsDataFilePath))
                {
                    checkData.gameSettings = GameManager.Instance.gameDataCenter.gameSettings;
                }

                if (File.Exists(CommonData.miniMapRecordFilePath))
                {
                    checkData.miniMapRecord = GameManager.Instance.gameDataCenter.currentMapMiniMapRecord;
                }
                if (File.Exists(CommonData.currentMapEventsRecordFilePath))
                {
                    checkData.currentMapEventsRecord = GameManager.Instance.gameDataCenter.currentMapEventsRecord;
                }
                if (File.Exists(CommonData.playRecordsFilePath))
                {
                    checkData.playRecords = GameManager.Instance.gameDataCenter.allPlayRecords;
                }
            }



#if UNITY_IOS
            if (!persistDi.Exists || checkData.versionUpdate || !dataComplete)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                DataHandler.CopyDirectory(CommonData.originDataPath, CommonData.persistDataPath, true);

                if (!persistDi.Exists)
                {
                    OnNewInstall();
                }

                else if (checkData.versionUpdate || !dataComplete)
                {
                    GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;

                    OnVersionUpdate(checkData, sql);
                }

                initDataCoroutine = InitData();

                StartCoroutine(initDataCoroutine);

                return;
            }
            else if (alwaysPersistData)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                DataHandler.CopyDirectory(CommonData.originDataPath, CommonData.persistDataPath, true);

                if (!persistDi.Exists)
                {
                    OnNewInstall();
                }

                else if (checkData.versionUpdate || !dataComplete)
                {
                    GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;

                    OnVersionUpdate(checkData, sql);
                }
            }

            initDataCoroutine = InitData();

            StartCoroutine(initDataCoroutine);
#elif UNITY_ANDROID
            if (!persistDi.Exists || checkData.versionUpdate || !dataComplete)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                IEnumerator copyDataCoroutine = CopyDataForPersist(delegate {
                    if (!persistDi.Exists)
                    {
                        OnNewInstall();
                    }
                    else if (checkData.versionUpdate || !dataComplete)
                    {
                        GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;
                        OnVersionUpdate(checkData, sql);
                    }
                });
                StartCoroutine(copyDataCoroutine);
                return;
            }
            else if (alwaysPersistData)
            {
                bool dataValidate = CheckDataValidate();

                if (!dataValidate)
                {
                    return;
                }

                if (DataHandler.DirectoryExist(CommonData.persistDataPath + "/Data"))
                {
                    DataHandler.DeleteDirectory(CommonData.persistDataPath + "/Data");
                }

                GameManager.Instance.persistDataManager.BackUpDataWhenUpdataVersion(checkData);

                IEnumerator copyDataCoroutine = CopyDataForPersist(delegate {
                    if (!persistDi.Exists)
                    {
                        OnNewInstall();
                    }
                    else if (checkData.versionUpdate || !dataComplete)
                    {
                        GameManager.Instance.persistDataManager.versionUpdateWhenLoad = checkData.versionUpdate;
                        OnVersionUpdate(checkData, sql);
                    }
                });
                StartCoroutine(copyDataCoroutine);
            }
            else
            {
                initDataCoroutine = InitData();

                StartCoroutine(initDataCoroutine);
            }
#endif
        }
Example #3
0
        /// <summary>
        /// 版本更新时数据处理
        /// </summary>
        /// <param name="checkData">Check data.</param>
        /// <param name="sql">Sql.</param>
        private void OnVersionUpdate(CheckDataModel checkData, MySQLiteHelper sql)
        {
            //更新原始数据
            if (checkData.chatRecords != null && checkData.chatRecords.Count > 0)
            {
                DataHandler.SaveInstanceListToFile <HLHNPCChatRecord>(checkData.chatRecords, CommonData.chatRecordsFilePath);
            }
            if (checkData.mapEventsRecords != null && checkData.mapEventsRecords.Count > 0)
            {
                DataHandler.SaveInstanceListToFile <MapEventsRecord>(checkData.mapEventsRecords, CommonData.mapEventsRecordFilePath);
            }
            if (checkData.gameSettings != null)
            {
                DataHandler.SaveInstanceDataToFile <GameSettings>(checkData.gameSettings, CommonData.gameSettingsDataFilePath);
            }
            if (checkData.miniMapRecord != null)
            {
                DataHandler.SaveInstanceDataToFile <MiniMapRecord>(checkData.miniMapRecord, CommonData.miniMapRecordFilePath);
            }
            if (checkData.currentMapEventsRecord != null)
            {
                DataHandler.SaveInstanceDataToFile <CurrentMapEventsRecord>(checkData.currentMapEventsRecord, CommonData.currentMapEventsRecordFilePath);
            }
            if (checkData.playRecords != null)
            {
                DataHandler.SaveInstanceListToFile <PlayRecord>(checkData.playRecords, CommonData.playRecordsFilePath);
            }
            if (checkData.buyRecord != null)
            {
                DataHandler.SaveInstanceDataToFile <BuyRecord>(checkData.buyRecord, CommonData.buyRecordFilePath, true);
            }

            sql.GetConnectionWith(CommonData.dataBaseName);
            sql.BeginTransaction();

            if (checkData.learnedWordsInSimple.Count > 0)
            {
                Debug.Log(checkData.learnedWordsInSimple.Count);
                UpdateWordsDataBase(sql, 0, checkData.learnedWordsInSimple);
            }

            if (checkData.learnedWordsInMedium.Count > 0)
            {
                UpdateWordsDataBase(sql, 1, checkData.learnedWordsInMedium);
            }

            if (checkData.learnedWordsInMaster.Count > 0)
            {
                UpdateWordsDataBase(sql, 2, checkData.learnedWordsInMaster);
            }


            sql.EndTransaction();

            sql.CloseConnection(CommonData.dataBaseName);

            WordType wordType = WordType.Simple;

            if (checkData.gameSettings != null)
            {
                wordType = checkData.gameSettings.wordType;
            }

            // 更新版本信息
            ApplicationInfo.Instance.currentVersion = GameManager.Instance.currentVersion;

            DataHandler.SaveInstanceDataToFile <ApplicationInfo>(ApplicationInfo.Instance, CommonData.applicationInfoFilePath);

            if (checkData.playerData == null)
            {
                GameManager.Instance.persistDataManager.SaveCompletePlayerData();
                return;
            }

            if (checkData.playerData.currentExploreStartDateString == null ||
                checkData.playerData.currentExploreStartDateString == string.Empty)
            {
                string dateString = DateTime.Now.ToShortDateString();

                Player.mainPlayer.currentExploreStartDateString = dateString;

                checkData.playerData.currentExploreStartDateString = dateString;
            }

            // 更新单词数据
            if (checkData.playerData.maxWordContinuousRightRecord > 0)
            {
                switch (wordType)
                {
                case WordType.Simple:
                    Player.mainPlayer.maxSimpleWordContinuousRightRecord    = checkData.playerData.maxWordContinuousRightRecord;
                    checkData.playerData.maxSimpleWordContinuousRightRecord = checkData.playerData.maxWordContinuousRightRecord;
                    break;

                case WordType.Medium:
                    Player.mainPlayer.maxMediumWordContinuousRightRecord    = checkData.playerData.maxWordContinuousRightRecord;
                    checkData.playerData.maxMediumWordContinuousRightRecord = checkData.playerData.maxWordContinuousRightRecord;
                    break;

                case WordType.Master:
                    Player.mainPlayer.maxMasterWordContinuousRightRecord    = checkData.playerData.maxWordContinuousRightRecord;
                    checkData.playerData.maxMasterWordContinuousRightRecord = checkData.playerData.maxWordContinuousRightRecord;
                    break;
                }
                checkData.playerData.maxWordContinuousRightRecord = 0;
            }


            if (checkData.playerData.wordContinuousRightRecord > 0)
            {
                switch (wordType)
                {
                case WordType.Simple:
                    Player.mainPlayer.simpleWordContinuousRightRecord    = checkData.playerData.wordContinuousRightRecord;
                    checkData.playerData.simpleWordContinuousRightRecord = checkData.playerData.wordContinuousRightRecord;
                    break;

                case WordType.Medium:
                    Player.mainPlayer.mediumWordContinuousRightRecord    = checkData.playerData.wordContinuousRightRecord;
                    checkData.playerData.mediumWordContinuousRightRecord = checkData.playerData.wordContinuousRightRecord;
                    break;

                case WordType.Master:
                    Player.mainPlayer.masterWordContinuousRightRecord    = checkData.playerData.wordContinuousRightRecord;
                    checkData.playerData.masterWordContinuousRightRecord = checkData.playerData.wordContinuousRightRecord;
                    break;
                }
                checkData.playerData.wordContinuousRightRecord = 0;
            }

            if (checkData.playerData.titleQualifications != null)
            {
                bool hasOldVersionTitle = false;

                for (int i = 0; i < checkData.playerData.titleQualifications.Length; i++)
                {
                    if (checkData.playerData.titleQualifications[i])
                    {
                        hasOldVersionTitle = true;
                        break;
                    }
                }

                if (checkData.playerData.titleQualificationsOfSimple == null || checkData.playerData.titleQualificationsOfSimple.Length == 0)
                {
                    checkData.playerData.titleQualificationsOfSimple = new bool[] { false, false, false, false, false, false };
                }

                if (checkData.playerData.titleQualificationsOfMedium == null || checkData.playerData.titleQualificationsOfMedium.Length == 0)
                {
                    checkData.playerData.titleQualificationsOfMedium = new bool[] { false, false, false, false, false, false };
                }

                if (checkData.playerData.titleQualificationsOfMaster == null || checkData.playerData.titleQualificationsOfMaster.Length == 0)
                {
                    checkData.playerData.titleQualificationsOfMaster = new bool[] { false, false, false, false, false, false };
                }

                Player.mainPlayer.titleQualificationsOfSimple = checkData.playerData.titleQualificationsOfSimple;
                Player.mainPlayer.titleQualificationsOfMedium = checkData.playerData.titleQualificationsOfMedium;
                Player.mainPlayer.titleQualificationsOfMaster = checkData.playerData.titleQualificationsOfMaster;

                if (hasOldVersionTitle)
                {
                    switch (wordType)
                    {
                    case WordType.Simple:
                        Player.mainPlayer.titleQualificationsOfSimple    = checkData.playerData.titleQualifications;
                        checkData.playerData.titleQualificationsOfSimple = checkData.playerData.titleQualifications;
                        break;

                    case WordType.Medium:
                        Player.mainPlayer.titleQualificationsOfMedium    = checkData.playerData.titleQualifications;
                        checkData.playerData.titleQualificationsOfMedium = checkData.playerData.titleQualifications;
                        break;

                    case WordType.Master:
                        Player.mainPlayer.titleQualificationsOfMaster    = checkData.playerData.titleQualifications;
                        checkData.playerData.titleQualificationsOfMaster = checkData.playerData.titleQualifications;
                        break;
                    }
                    checkData.playerData.titleQualifications = new bool[] { false, false, false, false, false, false };
                }
            }

            DataHandler.SaveInstanceDataToFile <PlayerData>(checkData.playerData, CommonData.playerDataFilePath, true);
        }