Exemplo n.º 1
0
        /// <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);
        }