Exemple #1
0
        public async Task ExecutesCorrectly()
        {
            var fs          = new MockFileSystem();
            var api         = new MockAPIConnector(fs);
            var revisionMgr = new RevisionManager(new ConfigFile("C:\\SFM"), fs);
            var ids         = new List <int>()
            {
                1337
            };
            var installationPresenter = new InstallationPresenter(api, fs, revisionMgr, ids);

            /* Create fake revision to download */
            fs.CreateDirectory("C:\\tmp");
            fs.CreateDirectory("C:\\tmp\\models");
            fs.CreateFile("C:\\tmp\\models\\model.ext");
            fs.CreateDirectory("C:\\tmp\\materials");
            fs.CreateFile("C:\\tmp\\materials\\material.ext");
            var revision = Revision.CreateTemporaryRevisionFromFolder(1337, "C:\\tmp", fs);

            api.AddRevision("C:\\tmp", revision);

            await installationPresenter.Execute();

            Assert.IsTrue(revisionMgr.VerifyInstalled(revision, null));
        }
Exemple #2
0
        public void CreatesTempRevisionCorrectly()
        {
            var fs = new MockFileSystem();

            var rev1 = Util.CreateFakeTempRevision(fs);
            var rev2 = Revision.CreateTemporaryRevisionFromFolder(1337, "C:\\tmp", fs);

            Assert.AreEqual(rev1.Files.Count, rev2.Files.Count);
            foreach (var file in rev1.Files)
            {
                Assert.IsTrue(rev2.Files.Any(x => x.Path == file.Path));
            }
        }
        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");
        }