public void FindsModFolder()
        {
            var fs = new MockFileSystem();

            fs.CreateDirectory("C:\\temp");
            fs.CreateDirectory("C:\\temp\\myCoolMod");
            fs.CreateDirectory("C:\\temp\\myCoolMod\\materials");
            fs.CreateDirectory("C:\\temp\\myCoolMod\\models");

            var parser = new TempRevisionParser("C:\\temp", fs);
            var dir    = parser.FindModFolder();

            Assert.AreEqual("C:\\temp\\myCoolMod", dir);
        }
Esempio n. 2
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");
        }