/// <summary> /// Updates the Index view window content /// </summary> private void UpdateIndexList() { if (_indexDetails == null) { _indexDetails = new ObservableImmutableList <IndexItem>(); } _indexDetails.Clear(); var tempList = EterFilesDal.ReadIndexFile( Path.Combine(MainWindowVm.Instance.SelectedWorkingProfile.WorkingDirectory, Filename), MainWindowVm.Instance.SelectedWorkingProfile.IndexKey, DisplayName); foreach (var item in tempList) { _indexDetails.Add(item); } NumberOfFiles = IndexDetails.Count; }
/// <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); }
/// <summary> /// Initializes virtual view /// </summary> private async void InitializeVirtualView() { await Task.Run(() => { // Clear list VirtualTreeViewItems.Clear(); // Create first item VirtualTreeViewItems.Add(new TreeItemFolderVm(null) { DisplayName = "Virtual view" }); // Get all files var files = IOHelper.GetAllFilesFromDir(_workingDir) .Where(x => String.Equals(x.Extension, _indexExtension, StringComparison.CurrentCultureIgnoreCase)).Reverse(); // File counters double fileCounter = 0; double totalFileCount = files.Count(); // Loop through all files foreach (var file in files) { // Get all items var items = EterFilesDal.ReadIndexFile(file.FullName, _indexKey, Path.GetFileNameWithoutExtension(file.Name)); if (items == null) { continue; } // Loop through all items of the index file foreach (var item in items) { // Replace all backslashes with forward slashes var path = item.Filename.Replace("\\", "/"); // Split by slash var tokens = path.Split(new[] { '/' }); var currentTreeItem = VirtualTreeViewItems.First() as TreeItemFolderVm; // If first layer item if (tokens.Count() == 1) { currentTreeItem.Children.Add(new TreeItemFileVm() { DisplayName = item.Filename, Parent = currentTreeItem, EterFileParent = item.ParentFile, Fullname = item.Filename }); } else { // Token counter int counter = 0; // Holds a reference to the current full path string currentFullpath = ""; // Loop through tokens foreach (var token in tokens) { counter++; currentFullpath += token + "/"; // Check if item already exists var itemInTreeView = currentTreeItem.Children.FirstOrDefault( x => String.Equals(token, x.DisplayName, StringComparison.CurrentCultureIgnoreCase)); // Create item if not already there if (itemInTreeView == null) { // If last one if (counter == tokens.Count()) { currentTreeItem.Children.Add(new TreeItemFileVm() { DisplayName = token, Parent = currentTreeItem, EterFileParent = item.ParentFile, Fullname = item.Filename }); } else { var obj = new TreeItemFolderVm() { DisplayName = token, Parent = currentTreeItem, Fullname = currentFullpath }; currentTreeItem.Children.Add(obj); currentTreeItem = obj; } } else { currentTreeItem = itemInTreeView as TreeItemFolderVm; } } } } State = String.Format("Processing global hash table... {0}%", Math.Round(fileCounter++ / totalFileCount * 100, 2)); } State = "Sorting list..."; VirtualTreeViewItems = SortItemsByFolder(VirtualTreeViewItems); (VirtualTreeViewItems.First() as TreeItemFolderVm).IsExpanded = true; State = "All files have been processed."; }); }