/// <summary> /// Saves current profile to file /// </summary> public void SaveProfile(bool log = true) { try { IsNameValid(Name); IsUnpackDirectoryValid(UnpackDirectory); IsWorkingDirectoryValid(WorkingDirectory); IsKeyValid(BitConverter.ToString(IndexKey), "IndexKey"); IsKeyValid(BitConverter.ToString(PackKey), "PackKey"); if (HasErrors) { UserInput.ShowMessage("INVALID_PROFILE_SETTINGS"); return; } _profile.Save(); if (log) { WindowLog.Information("PROFILE_SAVED", null, Name); } } catch (ProfileNameAlreadyExistsException e) { Name = _profile.OriginalName; UserInput.ShowMessage("PROFILE_NAME_ALREADY_EXISTS"); } catch (IOException e) { WindowLog.Error("COULD_NOT_ACCESS_FILE", String.Format("{0}{1}.xml", ConstantsBase.ProfilesPath, Name), e.Message); } }
/// <summary> /// Action performed when Pack file is hit /// </summary> /// <param name="param"></param> private async void PackFileAction(object param) { if (SelectedFilter < 0) { UserInput.ShowMessage("USER_SELECT_PACK_TYPE"); return; } QueueActionType = ActionType.Pack; if (!IsItemReady()) { return; } FilesActionVm.Instance.CcFiles++; SetItemState(State.Packing | State.LongAction); //await Task.Delay(1000); //this.SetItemState(State.Ready); //WorkingListVM.Instance.ProcessQueueUponActionFinalization(this); //return; HashMismatchFiles.Clear(); ErrorList.Clear(); _successCounter = 0; var filesInDir = IOHelper.GetAllFilesFromDir(String.Format("{0}{1}", MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory, DisplayName)); var indexItems = new List <IndexItem>(); string indexFilePath = String.Format("{0}{1}", MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory, String.Format("{0}{1}", DisplayName, MainWindowVm.Instance.SelectedWorkingProfile.IndexExtension)); string packFilePath = String.Format("{0}{1}", MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory, String.Format("{0}{1}", DisplayName, MainWindowVm.Instance.SelectedWorkingProfile.PackExtension)); if (File.Exists(packFilePath)) { // File already packed, but will be overwritten WindowLog.Warning("FILE_ALREADY_PACKED_BUT_OVER", Filename); File.Delete(packFilePath); } if (File.Exists(indexFilePath)) { File.Delete(EterHelper.ReplaceWithEpkExt(indexFilePath)); } await Task.Run(() => { int counter = 0; foreach (var file in filesInDir) { //int type = -1; //string fileExtension = Path.GetExtension(file.FullName); //if (SelectedFilter.RawExtensions != null) //{ // foreach (var rawExt in SelectedFilter.RawExtensions) // if (rawExt.ToLower() == fileExtension.ToLower()) // type = 0; // foreach (var lzoExt in SelectedFilter.LzoExtensions) // if (lzoExt.ToLower() == fileExtension.ToLower()) // type = 1; // foreach (var xteaExt in SelectedFilter.XteaExtensions) // if (xteaExt.ToLower() == fileExtension.ToLower()) // type = 2; //} //if (type == -1) // type = SelectedFilter.NotIncludedExtensionsType; string toReplaceStr = String.Format("{0}{1}", MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory, DisplayName).Replace("/", "\\"); string fileName = file.FullName.Substring(file.FullName.IndexOf(toReplaceStr) + toReplaceStr.Length + 1); indexItems.Add(new IndexItem( counter, fileName, null, 0, 0, null, 0, SelectedFilter, DisplayName)); counter++; } double lastProgressValue = 0; try { EterFilesDal.BuildIndexAndPackFiles( indexItems, packFilePath, String.Format("{0}{1}\\", MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory, DisplayName), MainWindowVm.Instance.SelectedWorkingProfile.IndexKey, MainWindowVm.Instance.SelectedWorkingProfile.PackKey, (error) => { if (error != null) { ErrorList.Add(error); } }, (result, progress) => { if (result == 0) { _successCounter++; } if (((progress - lastProgressValue) >= 5)) { ActionProgress = progress; lastProgressValue = progress; } }, () => SetItemState(State.CriticalError)); } catch (OutOfMemoryException ex) { WindowLog.Warning("FILE_TOO_BIG", DisplayName); SetItemState(State.CriticalError); } catch (EterPackFileNotFoundException ex) { WindowLog.Error("ETER_EPK_FILE_NOT_FOUND", DisplayName); SetItemState(State.CriticalError); } catch (FileNotFoundException ex) { WindowLog.Error("FILE_NOT_FOUND", DisplayName, new object[] { ex.FileName }); SetItemState(State.CriticalError); } catch (UnauthorizedAccessException ex) { WindowLog.Error("COULD_NOT_ACCESS_FILE", DisplayName, DisplayName); SetItemState(State.CriticalError); } catch (System.IO.IOException ex) { WindowLog.Error("ERROR_WITH_CUSTOM_MSG", DisplayName, ex.Message); SetItemState(State.CriticalError); } }); SetItemState(ErrorList.Count > 0 ? State.ReadyWithErrors : State.Ready); ActionProgress = 100; WindowLog.Information("ETER_PACK_RESULT", DisplayName, new object[] { _successCounter, HashMismatchFiles.Count, ErrorList.Count }); AfterLongAction(param); }
/// <summary> /// Action performed when Unpack file is hit /// </summary> /// <param name="param"></param> private async void UnpackFileAction(object param) { QueueActionType = ActionType.Unpack; if (!IsItemReady()) { return; } FilesActionVm.Instance.CcFiles++; SetItemState(State.Unpacking | State.LongAction); await Task.Run(() => { // Set state to unpacking SetItemState(State.Unpacking); // Clear lists HashMismatchFiles.Clear(); ErrorList.Clear(); // Reset counters _successCounter = 0; double lastProgressValue = 0; try { // Unpack file EterFilesDal.UnpackFile( new FileInfo(EterHelper.ReplaceWithEpkExt(Path.Combine(MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory, Filename))), MainWindowVm.Instance.SelectedWorkingProfile.UnpackDirectory, MainWindowVm.Instance.SelectedWorkingProfile.IndexKey, MainWindowVm.Instance.SelectedWorkingProfile.PackKey, (operationResult, globalProgress) => { if ((globalProgress - lastProgressValue) >= 5) { ActionProgress = globalProgress; lastProgressValue = globalProgress; } if (operationResult == 0) { _successCounter++; } }, (error, hash) => { if (error != null) { ErrorList.Add(error); } if (!String.IsNullOrWhiteSpace(hash)) { HashMismatchFiles.Add(hash); } }); } catch (ErrorReadingIndexException ex) { WindowLog.Error("ETER_WRONG_INDEX_KEY", DisplayName); SetItemState(State.CriticalError); } catch (OutOfMemoryException ex) { WindowLog.Warning("FILE_TOO_BIG", DisplayName); SetItemState(State.CriticalError); } catch (EterPackFileNotFoundException ex) { WindowLog.Error("ETER_EPK_FILE_NOT_FOUND", DisplayName); SetItemState(State.CriticalError); } catch (FileNotFoundException ex) { WindowLog.Error("FILE_NOT_FOUND", DisplayName, new object[] { ex.FileName }); SetItemState(State.CriticalError); } catch (UnauthorizedAccessException ex) { WindowLog.Error("COULD_NOT_ACCESS_FILE", DisplayName, DisplayName); SetItemState(State.CriticalError); } catch (System.IO.IOException ex) { WindowLog.Error("ERROR_WITH_CUSTOM_MSG", DisplayName, ex.Message); SetItemState(State.CriticalError); } }); // If any file produced an error, set state accordingly if (ErrorList.Count > 0 && ItemState != State.CriticalError) { SetItemState(State.ReadyWithErrors); } else if (ErrorList.Count == 0 && ItemState != State.CriticalError) { SetItemState(State.Ready); } // Make sure progress bar is at 100% ActionProgress = 100; // Logging stuff if (ItemState != State.CriticalError) { // Get all failed items int failedCount = ErrorList.Count; // Were any unnamed files present? var item = ErrorList.FirstOrDefault(x => x.ErrorMotive.Contains("(no name)")); // If so, add it if (item != null) { failedCount += Convert.ToInt32(item.Arg) - 1; } WindowLog.Information("ETER_UNPACK_RESULT", DisplayName, _successCounter, HashMismatchFiles.Count, failedCount); } AfterLongAction(param); }