Exemplo n.º 1
0
        public void GetNextFile(out string file, out long fileSize, out Stream source, out Stream target, out Action <string, Stream, Stream> finishMethod, out Action <long> updateMethod, out long totalBytesRead)
        {
            file           = null;
            fileSize       = 0;
            source         = null;
            target         = null;
            totalBytesRead = 0;
            finishMethod   = ValidateAndCloseFile;
            updateMethod   = IterrimUpdateMethod;

            if (_startTime == DateTime.MinValue)
            {
                _startTime = DateTime.UtcNow;
            }

            if (_preProcessComplete == false)
            {
                try
                {
                    RunPreProcessing();
                }
                catch (Exception ex)
                {
                    Utils.Logging.Logger.Error("Error while preparing the destination directory.\nFile transfer aborted.", ex);
                    Status = TransferStatus.Aborting;
                    Task.Run(() =>
                    {
                        System.Windows.MessageBox.Show(string.Format("Error while preparing the destination directory.\nFile transfer aborted.", ex.Message), "Transfer failed", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.OK);
                    });
                    return;
                }
            }

            if (_isPausedMidStream)
            {
                file               = _pausedFile;
                source             = _pausedSourceStream;
                target             = _pausedTargetStream;
                totalBytesRead     = _pausedBytesRead;
                _isPausedMidStream = false;
            }
            else if (_workQueue.Any())
            {
                while (source == null || target == null)
                {
                    file     = null;
                    fileSize = 0;
                    Stream srcStrm = null;
                    Stream dstStrm = null;
                    string appId   = Application.AppId;
                    if (_workQueue.Any() == false)
                    {
                        return;
                    }
                    _currentWorkItem = _workQueue.Dequeue();
                    file             = _currentWorkItem.Item1;
                    fileSize         = _currentWorkItem.Item2;
                    var theFile = file;
                    try
                    {
                        bool acceptCompressedFiles = ((_target is SteamArchive) && ((SteamArchive)_target).CompressNewGames);
                        int  bufferSize            = Convert.ToInt32(Math.Min(fileSize, MAX_BUFFER_SIZE));
                        if (bufferSize <= 0)
                        {
                            bufferSize = FALLBACK_BUFFER_SIZE;
                        }

                        srcStrm = _source.GetFileStream(appId, file, acceptCompressedFiles, GetIsValidated(), bufferSize);
                        dstStrm = _target.GetFileStream(Application.InstallDir, file, GetIsValidated(), bufferSize);

                        source = srcStrm;
                        target = dstStrm;
                    }
                    catch (Exception ex)
                    {
                        Utils.Logging.Logger.Debug(string.Format("Error openning file streams for {0}.", file), ex);
                        var retry = RetryFile(_currentWorkItem);

                        file     = null;
                        fileSize = 0;
                        srcStrm?.Dispose();
                        dstStrm?.Dispose();
                        source = null;
                        target = null;

                        //If the retry failed, then give up
                        if (!retry)
                        {
                            return;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void GetNextFile(out string file, out Stream source, out Stream target, out Action <string, Stream, Stream> finishMethod, out Action <long> updateMethod, out long totalBytesRead)
        {
            file           = null;
            source         = null;
            target         = null;
            totalBytesRead = 0;
            finishMethod   = ValidateAndCloseFile;
            updateMethod   = IterrimUpdateMethod;

            if (_startTime == DateTime.MinValue)
            {
                _startTime = DateTime.UtcNow;
            }

            if (_preProcessComplete == false)
            {
                try
                {
                    RunPreProcessing();
                }
                catch (Exception ex)
                {
                    Utils.Logging.Logger.Error("Error while preparing the destination directory.\nFile transfer aborted.", ex);
                    Status = TransferStatus.Aborting;
                    Task.Run(() =>
                    {
                        System.Windows.MessageBox.Show(string.Format("Error while preparing the destination directory.\nFile transfer aborted.", ex.Message), "Transfer failed", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.OK);
                    });
                    return;
                }
            }

            if (_isPausedMidStream)
            {
                file               = _pausedFile;
                source             = _pausedSourceStream;
                target             = _pausedTargetStream;
                totalBytesRead     = _pausedBytesRead;
                _isPausedMidStream = false;
            }
            else if (_workQueue.Any())
            {
                while (source == null || target == null)
                {
                    file = null;
                    Stream srcStrm = null;
                    Stream dstStrm = null;
                    string appId   = Application.AppId;
                    if (_workQueue.Any() == false)
                    {
                        return;
                    }
                    file = _workQueue.Dequeue();
                    var theFile = file;
                    try
                    {
                        bool acceptCompressedFiles = ((_target is SteamArchive) && ((SteamArchive)_target).CompressNewGames);
                        srcStrm = _source.GetFileStream(appId, file, acceptCompressedFiles);

                        dstStrm = _target.GetFileStream(Application.InstallDir, file);
                        source  = srcStrm;
                        target  = dstStrm;
                        _retriedList.RemoveAll(x => theFile == x);
                    }
                    catch (Exception ex)
                    {
                        _workQueue.Enqueue(file);
                        Utils.Logging.Logger.Debug(string.Format("Error open file streams for {0}, retry queued, moving on to another file.", file), ex);
                        //try again later
                        if (_retriedList.Contains(theFile) == false)
                        {
                            _retriedList.Add(theFile);
                        }
                        else if (_retriedList.First() == theFile)
                        {
                            //If we get to the first file, and the list size is the same time as the last time we got here, clear the work queue, and give up
                            if (_lastRetriedListLength == _retriedList.Count)
                            {
                                if (Progress < 0.9f)
                                {
                                    Utils.Logging.Logger.Error("Two times through the retry list and no progress, less than 90% complete, so just aborting.", ex);
                                    Status = TransferStatus.Aborting;
                                    Task.Run(() =>
                                    {
                                        System.Windows.MessageBox.Show(string.Format("{0} files failed to transfer for {1}.\nFile transfer aborted.", _lastRetriedListLength, Application.GameName), "Transfer failed", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.OK);
                                    });
                                }
                                else
                                {
                                    Utils.Logging.Logger.Error(string.Format("Two times through the retry list and no progress, giving up on the transfer, but almost done, so finishing normally with a warning.", file), ex);
                                    Task.Run(() =>
                                    {
                                        System.Windows.MessageBox.Show(string.Format("{0} files failed to transfer for {1}.\nYou will need to validate the game after restarting Steam.", _lastRetriedListLength, Application.GameName), "Transfer Incomplete", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.OK);
                                    });
                                }

                                _workQueue.Clear();
                                file = null;
                                srcStrm?.Dispose();
                                dstStrm?.Dispose();
                                source = null;
                                target = null;
                                return;
                            }

                            _lastRetriedListLength = _retriedList.Count;
                        }

                        file = null;
                        srcStrm?.Dispose();
                        dstStrm?.Dispose();
                        source = null;
                        target = null;
                    }
                }
            }
        }