Пример #1
0
        private DirectoryCopierFileCopyMode FireFileExistsEvent(string file1, string file2)
        {
            var eventArgs = new DirectoryCopierFileExistsEventArgs(file1, file2);

            OnFileExists?.Invoke(this, eventArgs);
            return(eventArgs.FileCopyMode);
        }
Пример #2
0
        public async Task <InstallationResult> InstallRevision(Revision revision, string topDir, IProgress <int> progress, CancellationToken cancel = default(CancellationToken))
        {
            /* Copy files and blahblah */
            var directoryCopier = new DirectoryCopier(_fs, topDir, _dirParser.InstallationPath, true);

            directoryCopier.OnProgress += (s, e) =>
                                          progress?.Report(e.Progress);

            directoryCopier.OnFileExists += (s, e) =>
                                            OnFileExists?.Invoke(s, e);

            try
            {
                await directoryCopier.Execute(cancel);
            }
            catch (OperationCanceledException)
            {
                return(InstallationResult.Cancelled);
            }

            revision.ChangeTopDirectory(topDir, _dirParser.InstallationPath);
            revision.Metadata["Size"]             = Convert.ToString(revision.CalculateSizeOnDisk(_fs));
            revision.Metadata["InstallationTime"] = DateTime.Now.ToString(CultureInfo.CurrentCulture);
            Database.AddToDb(revision);
            Database.WriteDbDisk();

            return(InstallationResult.Success);
        }
Пример #3
0
        public async Task ExecuteInstallation(int id)
        {
            var      zipTmp  = _fs.GetTempPath();
            var      tempDir = _fs.GetTempPath();
            IZipFile zip     = null;

            var progress = new Progress <int>(i => SetProgress(_currentProgressState, i));

            _revisionMgr.OnFileExists += (s, e) => OnFileExists?.Invoke(s, e);

            if (!_fs.DirectoryExists(tempDir))
            {
                _fs.CreateDirectory(tempDir);
            }

            LogInstallation("Installing revision " + id + "\n");

            _currentProgressState = ProgressState.Download;
            LogInstallation("Downloading file...\n");

            Action cleanup = delegate
            {
                if (zip != null)
                {
                    if (zip is MockZipFile)
                    {
                        _fs.DeleteDirectory(zipTmp);
                    }
                    else
                    {
                        _fs.DeleteFile(zipTmp);
                    }
                }
                _fs.DeleteDirectory(tempDir);
            };

            try
            {
                await _api.DownloadRevisionZip(id, zipTmp, progress);
            }
            catch (WebException e)
            {
                MessageBox.Show("Failed to download: " + e.Message);
                cleanup();
                return;
            }

            _currentProgressState = ProgressState.Extraction;
            LogInstallation("Extracting zip file...\n");
            zip = _fs.OpenZip(zipTmp);
            await zip.Extract(tempDir, progress);

            var parser = new TempRevisionParser(tempDir, _fs);
            var modDir = parser.FindModFolder();

            if (string.IsNullOrWhiteSpace(modDir))
            {
                const string s = "This mod cannot be installed because it does not have the appropriate file-structure.";
                LogInstallation(s);
                MessageBox.Show(s);
                cleanup();
                return;
            }

            _currentProgressState = ProgressState.Installation;
            LogInstallation("Installing files to SFM...\n");
            var tmpRev = Revision.CreateTemporaryRevisionFromFolder(id, modDir, _fs);
            await _api.FetchMetadata(tmpRev);

            var result = await _revisionMgr.InstallRevision(tmpRev, modDir, progress, CancellationSource.Token);

            /* If we don't do this the directory deletion crashes because the handle created in zip.Extract is not released properly? */
            GC.Collect();
            GC.WaitForPendingFinalizers();

            LogInstallation("Cleaning up...\n");
            cleanup();

            switch (result)
            {
            case InstallationResult.Success:
                LogInstallation("Installation cancelled by user\n");
                break;

            case InstallationResult.Cancelled:
                LogInstallation("Installation successful\n");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            LogInstallation("Done!\n");
        }