/// <summary> /// 验证Project的完整性 /// (验证1个Project的完整性。 /// 如果Project是完整的,代表这个Project是有效的; /// 如果Project是不完整的,代表Project文件还没有同步完,或者是Project文件已损坏) /// </summary> /// <param name="_projectBaseData">要验证的Project</param> /// <returns>Project是否是完整的</returns> public bool VerifyProjectIntegrity(ProjectBaseData _projectBaseData) { if (_projectBaseData == null || _projectBaseData.Id < 0 || _projectBaseData.Name == null || _projectBaseData.Name == "") { return(false); } else { return(true); } }
/// <summary> /// 保存数据(Project、Bug、Record) /// </summary> /// <param name="_oldProjectFolderPath">旧的Project文件夹的路径</param> /// <param name="_newProjectFolderPath">新的Project文件夹的路径</param> /// <param name="_modeType">项目的类型</param> /// <param name="_projectBaseData">读取到的项目数据</param> /// <param name="_bugBaseDatas">读取到的Bug数据</param> /// <param name="_recordBaseDatas">读取到的Record数据</param> public void SaveData(string _oldProjectFolderPath, string _newProjectFolderPath, ModeType _modeType, ProjectBaseData _projectBaseData, List <BugBaseData> _bugBaseDatas, List <RecordBaseData> _recordBaseDatas) { /* 保存Project */ //修改项目的类型 _projectBaseData.ModeType = (int)_modeType; //把ProjectBaseData转换为json string _projectJsonText = JsonMapper.ToJson(_projectBaseData); //Project文件的路径 string _projectFilePath = _newProjectFolderPath + "/Project.bugs"; //把json文件保存到[项目名.bugs]文件里 File.WriteAllText(_projectFilePath, _projectJsonText, Encoding.Default); /* 保存Bug */ //如果是[默认]模式 if (_modeType == ModeType.Default) { //把BugBaseData转换为json string _bugsJsonText = JsonMapper.ToJson(_bugBaseDatas); //Bug文件的路径(文件夹+文件名+后缀) string _bugsFilePath = _newProjectFolderPath + "/Bug/Bugs.json"; //把json文件保存到[Bugs.json]文件里 File.WriteAllText(_bugsFilePath, _bugsJsonText, Encoding.Default); } //如果是[协同合作]模式 else if (_modeType == ModeType.Collaboration) { for (int i = 0; i < _bugBaseDatas.Count; i++) { if (_bugBaseDatas[i] != null) { //取到BugBaseDatas BugBaseData _bugBaseData = _bugBaseDatas[i]; //把BugBaseData转换为json string _bugJsonText = JsonMapper.ToJson(_bugBaseData); //Bug文件的路径(文件夹+文件名+后缀) string _bugFilePath = _newProjectFolderPath + "/Bug/Bug - " + _bugBaseData.Id + ".json"; //把json文件保存到[Bug - BugId.json]文件里 File.WriteAllText(_bugFilePath, _bugJsonText, Encoding.Default); } } } /* 保存Record */ //如果是[默认]模式 if (_modeType == ModeType.Default) { //把RecordBaseData转换为json string _recordsJsonText = JsonMapper.ToJson(_recordBaseDatas); //Record文件的路径(文件夹+文件名+后缀) string _recordsFilePath = _newProjectFolderPath + "/Record/Records.json"; //把json文件保存到[Records.json]文件里 File.WriteAllText(_recordsFilePath, _recordsJsonText, Encoding.Default); } //如果是[协同合作]模式 else if (_modeType == ModeType.Collaboration) { for (int i = 0; i < _recordBaseDatas.Count; i++) { if (_recordBaseDatas[i] != null) { //取到RecordBaseDatas RecordBaseData _recordBaseData = _recordBaseDatas[i]; //把RecordBaseData转换为json string _recordJsonText = JsonMapper.ToJson(_recordBaseData); //Record文件的路径(文件夹+文件名+后缀) string _recordFilePath = _newProjectFolderPath + "/Record/Record - " + _recordBaseData.Id + ".json"; //把json文件保存到[Record - RecordId.json]文件里 File.WriteAllText(_recordFilePath, _recordJsonText, Encoding.Default); } } } /* 保存图片 */ //取到旧的项目的Image文件夹 DirectoryInfo _imageDirectoryInfo = new DirectoryInfo(_oldProjectFolderPath + "/Image"); if (_imageDirectoryInfo.Exists == true) { //遍历所有的Image FileInfo[] _imageFileInfos = _imageDirectoryInfo.GetFiles(); //复制所有的Image for (int i = 0; i < _imageFileInfos.Length; i++) { //取到Image FileInfo _imageFileInfo = _imageFileInfos[i]; //取到二进制数据 byte[] _oldImage = File.ReadAllBytes(_imageFileInfo.FullName); //保存二进制数据到新文件中 File.WriteAllBytes(_newProjectFolderPath + "/Image/" + _imageFileInfo.Name, _oldImage); } } /* 保存Backup文件 */ //取到旧的项目的Backup文件夹 DirectoryInfo _backupDirectoryInfo = new DirectoryInfo(_oldProjectFolderPath + "/Backup"); if (_backupDirectoryInfo.Exists == true) { /* Project文件夹 */ //Backup/Project文件夹 DirectoryInfo _projectBackupDirectoryInfo = new DirectoryInfo(_oldProjectFolderPath + "/Backup/Project"); //如果文件夹存在 if (_projectBackupDirectoryInfo.Exists == true) { //遍历所有的Backup/Project FileInfo[] _projectBackupFileInfos = _projectBackupDirectoryInfo.GetFiles(); //复制所有的Backup for (int i = 0; i < _projectBackupFileInfos.Length; i++) { //取到Backup FileInfo _projectBackupFileInfo = _projectBackupFileInfos[i]; //取到二进制数据 string _oldProjectBackup = File.ReadAllText(_projectBackupFileInfo.FullName); //保存二进制数据到新文件中 File.WriteAllText(_newProjectFolderPath + "/Backup/Project/" + _projectBackupFileInfo.Name, _oldProjectBackup); } } /* Bug文件夹 */ //Backup/Bug文件夹 DirectoryInfo _bugBackupDirectoryInfo = new DirectoryInfo(_oldProjectFolderPath + "/Backup/Bug"); //如果文件夹存在 if (_bugBackupDirectoryInfo.Exists == true) { //遍历所有的Backup/Bug FileInfo[] _bugBackupFileInfos = _bugBackupDirectoryInfo.GetFiles(); //复制所有的Backup for (int i = 0; i < _bugBackupFileInfos.Length; i++) { //取到Backup FileInfo _bugBackupFileInfo = _bugBackupFileInfos[i]; //取到二进制数据 string _oldBugBackup = File.ReadAllText(_bugBackupFileInfo.FullName); //保存二进制数据到新文件中 File.WriteAllText(_newProjectFolderPath + "/Backup/Bug/" + _bugBackupFileInfo.Name, _oldBugBackup); } } /* Record文件夹 */ //Backup/Record文件夹 DirectoryInfo _recordBackupDirectoryInfo = new DirectoryInfo(_oldProjectFolderPath + "/Backup/Record"); //如果文件夹存在 if (_recordBackupDirectoryInfo.Exists == true) { //遍历所有的Backup/Record FileInfo[] _recordBackupFileInfos = _recordBackupDirectoryInfo.GetFiles(); //复制所有的Backup for (int i = 0; i < _recordBackupFileInfos.Length; i++) { //取到Backup FileInfo _recordBackupFileInfo = _recordBackupFileInfos[i]; //取到二进制数据 string _oldRecordBackup = File.ReadAllText(_recordBackupFileInfo.FullName); //保存二进制数据到新文件中 File.WriteAllText(_newProjectFolderPath + "/Backup/Record/" + _recordBackupFileInfo.Name, _oldRecordBackup); } } } }
/// <summary> /// 读取数据(Project、Bug、Record) /// </summary> /// <param name="_projectFilePath">项目文件的路径</param> /// <param name="_projectBaseData">读取到的项目数据</param> /// <param name="_bugBaseDatas">读取到的Bug数据</param> /// <param name="_recordBaseDatas">读取到的Record数据</param> private void LoadData(string _projectFilePath, out ProjectBaseData _projectBaseData, out List <BugBaseData> _bugBaseDatas, out List <RecordBaseData> _recordBaseDatas) { /*获取Project的文件信息*/ FileInfo _projectFileInfo = new FileInfo(_projectFilePath); /* 读取Project数据 */ //读取[项目名.bugs]的Json文本中的内容 string _projectJsonText = File.ReadAllText(_projectFilePath); //然后把Json文本解析成ProjectBaseData对象 _projectBaseData = JsonMapper.ToObject <ProjectBaseData>(_projectJsonText); /* 读取Bug数据 */ _bugBaseDatas = new List <BugBaseData>(); //如果是[默认]模式 if ((ModeType)_projectBaseData.ModeType == ModeType.Default) { //读取[Bug]的Json文本中的内容 string _bugsJsonText = File.ReadAllText(_projectFileInfo.DirectoryName + "/Bug/Bugs.json"); //然后把Json文本解析成BugBaseData对象 _bugBaseDatas = JsonMapper.ToObject <List <BugBaseData> >(_bugsJsonText); } //如果是[协同合作]模式 else if ((ModeType)_projectBaseData.ModeType == ModeType.Collaboration) { //取到Bug文件夹 的信息 DirectoryInfo _bugDirectoryInfo = new DirectoryInfo(_projectFileInfo.DirectoryName + "/Bug"); //获取到Bug文件夹内所有的文件 的信息 FileInfo[] _bugFileInfos = _bugDirectoryInfo.GetFiles(); //遍历所有的Bug文件 for (int i = 0; i < _bugFileInfos.Length; i++) { //取到Bug文件的名字 string _bugFileName = Path.GetFileNameWithoutExtension(_bugFileInfos[i].FullName); //把[Bug文件的名字]转换为[BugId] _bugFileName = _bugFileName.Replace("Bug - ", ""); long _bugId = -1; bool _isParseOk = long.TryParse(_bugFileName, out _bugId);//把string转换为long //如果转换成功 if (_isParseOk == true) { //就读取这个Bug string _bugJsonText = File.ReadAllText(_bugFileInfos[i].FullName); //然后把Json文本解析成BugBaseData对象 BugBaseData _bugBaseData = JsonMapper.ToObject <BugBaseData>(_bugJsonText); //把BugBaseData对象,加入到列表中 _bugBaseDatas.Add(_bugBaseData); } } } /* 读取Record数据 */ _recordBaseDatas = new List <RecordBaseData>(); //如果是[默认]模式 if ((ModeType)_projectBaseData.ModeType == ModeType.Default) { //读取[Record]的Json文本中的内容 string _recordsJsonText = File.ReadAllText(_projectFileInfo.DirectoryName + "/Record/Records.json"); //然后把Json文本解析成RecordBaseData对象 _recordBaseDatas = JsonMapper.ToObject <List <RecordBaseData> >(_recordsJsonText); } //如果是[协同合作]模式 else if ((ModeType)_projectBaseData.ModeType == ModeType.Collaboration) { //取到Record文件夹 的信息 DirectoryInfo _recordDirectoryInfo = new DirectoryInfo(_projectFileInfo.DirectoryName + "/Record"); //获取到Record文件夹内所有的文件 的信息 FileInfo[] _recordFileInfos = _recordDirectoryInfo.GetFiles(); //遍历所有的Record文件 for (int i = 0; i < _recordFileInfos.Length; i++) { //取到Record文件的名字 string _recordFileName = Path.GetFileNameWithoutExtension(_recordFileInfos[i].FullName); //把[Record文件的名字]转换为[RecordId] _recordFileName = _recordFileName.Replace("Record - ", ""); long _recordId = -1; bool _isParseOk = long.TryParse(_recordFileName, out _recordId);//把string转换为long //如果转换成功 if (_isParseOk == true) { //就读取这个Record string _recordJsonText = File.ReadAllText(_recordFileInfos[i].FullName); //然后把Json文本解析成RecordBaseData对象 RecordBaseData _recordBaseData = JsonMapper.ToObject <RecordBaseData>(_recordJsonText); //把RecordBaseData对象,加入到列表中 _recordBaseDatas.Add(_recordBaseData); } } } }
/// <summary> /// 修复项目 /// </summary> /// <param name="_projectFilePath">项目文件的路径</param> /// <param name="_saveFolderPath">保存文件夹的路径</param> /// <returns>转换后的文件夹</returns> public string Repair(string _projectFilePath, string _saveFolderPath) { try { /* 读取所有的Backup文件 */ List <ProjectBaseData> _projectFileDatas; //项目文件的路径 List <BugBaseData[]> _bugFileDatas; //读取到的项目数据 List <RecordBaseData[]> _recordFileDatas; //读取到的Record数据 LoadData(_projectFilePath, out _projectFileDatas, out _bugFileDatas, out _recordFileDatas); /* 修复数据 */ //修复的数据 ProjectBaseData _projectBaseData = null; //修复的Project List <BugBaseData> _bugBaseDatas = new List <BugBaseData>(); //修复的Bug List <RecordBaseData> _recordBaseDatas = new List <RecordBaseData>(); //修复的Record /* 修复Project数据 */ //从最后一个开始 for (int i = _projectFileDatas.Count - 1; i >= 0; i--) { //进行验证 bool _isVerifyOk = VerifyProjectIntegrity(_projectFileDatas[i]); //如果验证成功 if (_isVerifyOk == true) { _projectBaseData = _projectFileDatas[i]; break; } } /* 修复Bug数据 */ List <BugBaseData> _allBugBaseDatas = new List <BugBaseData>(); //从最后一个开始 for (int i = _bugFileDatas.Count - 1; i >= 0; i--) { if (_bugFileDatas[i] != null) { //从后到前,保存所有的数据 for (int j = 0; j < _bugFileDatas[i].Length; j++) { _allBugBaseDatas.Add(_bugFileDatas[i][j]); } } } //遍历所有的数据 Dictionary <long, BugBaseData> _repairBugBaseDatas = new Dictionary <long, BugBaseData>();//所有修复好的数据 for (int i = 0; i < _allBugBaseDatas.Count; i++) { if (_allBugBaseDatas[i] != null) { BugBaseData _bugBaseData = null; bool _isHave = _repairBugBaseDatas.TryGetValue(_allBugBaseDatas[i].Id, out _bugBaseData); //如果这个Bug还不存在 if (_isHave == false) { //验证Bug的完整性 if (VerifyBugIntegrity(_allBugBaseDatas[i]) == true) { //加入到字典中 _repairBugBaseDatas.Add(_allBugBaseDatas[i].Id, _allBugBaseDatas[i]); } } } } //把修复的数据加到列表中 foreach (BugBaseData _bugBaseData in _repairBugBaseDatas.Values) { _bugBaseDatas.Add(_bugBaseData); } /* 修复Record数据 */ List <RecordBaseData> _allRecordBaseDatas = new List <RecordBaseData>(); //从最后一个开始 for (int i = _recordFileDatas.Count - 1; i >= 0; i--) { if (_recordFileDatas[i] != null) { //从后到前,保存所有的数据 for (int j = 0; j < _recordFileDatas[i].Length; j++) { _allRecordBaseDatas.Add(_recordFileDatas[i][j]); } } } //遍历所有的数据 Dictionary <long, RecordBaseData> _repairRecordBaseDatas = new Dictionary <long, RecordBaseData>();//所有修复好的数据 for (int i = 0; i < _allRecordBaseDatas.Count; i++) { if (_allRecordBaseDatas[i] != null) { RecordBaseData _recordBaseData = null; bool _isHave = _repairRecordBaseDatas.TryGetValue(_allRecordBaseDatas[i].Id, out _recordBaseData); //如果这个Record还不存在 if (_isHave == false) { //验证Record的完整性 if (VerifyRecordIntegrity(_allRecordBaseDatas[i]) == true) { //加入到字典中 _repairRecordBaseDatas.Add(_allRecordBaseDatas[i].Id, _allRecordBaseDatas[i]); } } } } //把修复的数据加到列表中 foreach (RecordBaseData _recordBaseData in _repairRecordBaseDatas.Values) { _recordBaseDatas.Add(_recordBaseData); } /* 创建文件夹 */ string _newProjectFolderPath = AppManager.Systems.ConvertSystem.CreateFolders(_saveFolderPath, ToolType.Repair); string _oldProjectFolderPath = new FileInfo(_projectFilePath).DirectoryName; /* 保存数据 */ //如果没有项目 if (_projectBaseData == null) { _projectBaseData = new ProjectBaseData(); _projectBaseData.ModeType = 1; _projectBaseData.Id = DateTimeTool.DateTimeToLong(DateTime.UtcNow, TimeFormatType.YearMonthDayHourMinuteSecondMillisecond); _projectBaseData.Name = " "; //尝试给Project附上名字 FileInfo _oldProjectFileInfo = new FileInfo(_projectFilePath); if (_oldProjectFileInfo.Exists == true) { _projectBaseData.Name = Path.GetFileNameWithoutExtension(_oldProjectFileInfo.FullName); } } //保存数据 AppManager.Systems.ConvertSystem.SaveData(_oldProjectFolderPath, _newProjectFolderPath, (ModeType)_projectBaseData.ModeType, _projectBaseData, _bugBaseDatas, _recordBaseDatas); //返回值 return(_newProjectFolderPath); } catch (Exception e) { return(""); } }