コード例 #1
0
        /// <summary>
        /// Updates WSAPM to the current version.
        /// </summary>
        /// <returns>True if an update is available, otherwise false.</returns>
        public bool UpdateWsapm()
        {
            var updateStatus = CheckForUpdates();

            if (!updateStatus.HasValue || !updateStatus.Value)
            {
                return(false); // No update available.
            }
            var fileName = SimpleFileDownloader.Download(this.UpdateInformation.DownloadUri, true, true);

            if (!string.IsNullOrEmpty(fileName))
            {
                // Start update...
                Process.Start(fileName);

                // ...and exit application.
                Environment.Exit(0);
                return(true);
            }
            else
            {
                // Something went wrong while downloading update.
                return(false);
            }
        }
コード例 #2
0
 public void IOExceptionThrownIfIOExceptionOccursOnRead()
 {
     this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Throws <IOException>();
     using (IFileDownloader downloader = new SimpleFileDownloader()) {
         Assert.Throws <IOException>(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
     }
 }
コード例 #3
0
        public void NormalDownloadTest()
        {
            double lastPercent = 0;

            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e) {
                if (e.ActualPosition != null)
                {
                    Assert.GreaterOrEqual((long)e.ActualPosition, 0);
                    Assert.LessOrEqual((long)e.ActualPosition, this.remoteLength);
                }

                if (e.Percent != null)
                {
                    Assert.GreaterOrEqual(e.Percent, 0);
                    Assert.LessOrEqual(e.Percent, 100);
                    Assert.GreaterOrEqual(e.Percent, lastPercent);
                    lastPercent = (double)e.Percent;
                }

                if (e.Length != null)
                {
                    Assert.GreaterOrEqual(e.Length, 0);
                    Assert.LessOrEqual(e.Length, this.remoteLength);
                }
            };

            using (IFileDownloader downloader = new SimpleFileDownloader())
            {
                downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
                Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
            }
        }
コード例 #4
0
        public void AbortWhileDownloadTest()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e)
            {
                Assert.AreEqual(null, e.Completed);
            };
            try
            {
                Task            t;
                IFileDownloader downloader = new SimpleFileDownloader();
                t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg));
                t.Wait(100);
                this.transmissionEvent.ReportProgress(new TransmissionProgressEventArgs()
                {
                    Aborting = true
                });
                t.Wait();
                Assert.Fail();
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf(typeof(AbortException), e.InnerException);
                Assert.True(this.transmissionEvent.Status.Aborted.GetValueOrDefault());
                Assert.AreEqual(false, this.transmissionEvent.Status.Aborting);
                return;
            }

            Assert.Fail();
        }
コード例 #5
0
 public void ServerFailedExceptionTest()
 {
     this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Throws <CmisConnectionException>();
     using (IFileDownloader downloader = new SimpleFileDownloader())
     {
         downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
     }
 }
コード例 #6
0
 public void DownloadWithThreeUpdate()
 {
     SetUp(2 * 1024 * 1024 + 1);
     this.hashAlg = new SHA1Reuse();
     using (IFileDownloader downloader = new SimpleFileDownloader()) {
         int count = 0;
         downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg, (byte[] checksum, long length) => { ++count; });
         Assert.AreEqual(3, count);
         Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
         Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
         Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
     }
 }
コード例 #7
0
        public void DisposeWhileDownload()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            try {
                Task t;
                using (IFileDownloader downloader = new SimpleFileDownloader()) {
                    t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
                }

                t.Wait();
                Assert.Fail();
            } catch (AggregateException e) {
                Assert.IsInstanceOf(typeof(ObjectDisposedException), e.InnerException);
            }
        }
コード例 #8
0
        public void NormalDownload()
        {
            double lastPercent = 0;

            this.transmission.AddPositionConstraint(Is.LessThanOrEqualTo(this.remoteLength));
            this.transmission.AddLengthConstraint(Is.EqualTo(this.remoteLength).Or.EqualTo(0));

            this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Percent))
                {
                    Assert.That(t.Percent, Is.Null.Or.GreaterThanOrEqualTo(lastPercent));
                    lastPercent = t.Percent.GetValueOrDefault();
                }
            };

            using (IFileDownloader downloader = new SimpleFileDownloader()) {
                downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);
                Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
            }
        }
コード例 #9
0
        public void AbortWhileDownload()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                Assert.That((sender as Transmission).Status, Is.Not.EqualTo(TransmissionStatus.FINISHED));
            };

            try {
                Task            t;
                IFileDownloader downloader = new SimpleFileDownloader();
                t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
                t.Wait(100);
                this.transmission.Abort();
                t.Wait();
                Assert.Fail();
            } catch (AggregateException e) {
                Assert.IsInstanceOf(typeof(AbortException), e.InnerException);
                Assert.That(this.transmission.Status, Is.EqualTo(TransmissionStatus.ABORTED));
                return;
            }

            Assert.Fail();
        }
コード例 #10
0
        public static async Task DoDownloadAndInstallProcDump(IProgress <int> progress = null,
                                                              CancellationToken ct     = default(CancellationToken))
        {
            //Download File
            progress?.Report(5);

            const string downloadLink = @"https://download.sysinternals.com/files/Procdump.zip";

            var tempFileName = Path.GetTempFileName();

            try
            {
                var downloadFileAsync = new SimpleFileDownloader(downloadLink, tempFileName);
                if (progress != null)
                {
                    downloadFileAsync.ProgressChanged += (sender, args) =>
                    {
                        progress.Report(Convert.ToInt32(Math.Floor(70 * (downloadFileAsync.DownloadProgress / 100))));
                    }
                }
                ;
                await downloadFileAsync.DownloadAsync(ct);
            }
            catch (AggregateException)
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                throw;
            }

            //Extract File
            progress?.Report(65);

            Progress <double> extractProgress = null;

            if (progress != null)
            {
                extractProgress = new Progress <double>();
                extractProgress.ProgressChanged += (o, ea) =>
                {
                    progress.Report(70 + Convert.ToInt32(Math.Floor(20 * (ea / 100))));
                };
            }
            var tempDir = Path.Combine(Path.GetTempPath(), "Celeste_Launcher_ProcDump");

            if (Directory.Exists(tempDir))
            {
                Misc.CleanUpFiles(tempDir, "*.*");
            }

            try
            {
                await ZipUtils.ExtractZipFile(tempFileName, tempDir);
            }
            catch (AggregateException)
            {
                Misc.CleanUpFiles(tempDir, "*.*");
                throw;
            }
            finally
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }
            }

            //Move File
            progress?.Report(90);

            var destinationDir = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;

            try
            {
                Misc.MoveFiles(tempDir, destinationDir);
            }
            finally
            {
                Misc.CleanUpFiles(tempDir, "*.*");
            }

            //
            progress?.Report(100);
        }
コード例 #11
0
        public static async Task DownloadAndInstallUpdate(bool isSteam         = false, IProgress <int> progress = null,
                                                          CancellationToken ct = default(CancellationToken))
        {
            Misc.CleanUpFiles(Directory.GetCurrentDirectory(), "*.old");

            var versionService = new LauncherVersionService();
            var newVersion     = await versionService.GetLatestVersion().ConfigureAwait(false);

            progress?.Report(3);

            if (newVersion.Version <= Assembly.GetExecutingAssembly().GetName().Version)
            {
                return;
            }

            ct.ThrowIfCancellationRequested();


            //Download File
            progress?.Report(5);

            var tempFileName = Path.GetTempFileName();

            try
            {
                var downloadFileAsync = new SimpleFileDownloader(newVersion.Url, tempFileName);
                if (progress != null)
                {
                    downloadFileAsync.ProgressChanged += (sender, args) =>
                    {
                        progress.Report(Convert.ToInt32(Math.Floor(70 * (downloadFileAsync.DownloadProgress / 100))));
                    }
                }
                ;
                await downloadFileAsync.DownloadAsync(ct);
            }
            catch (AggregateException)
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                throw;
            }

            //Extract File
            progress?.Report(65);

            Progress <double> extractProgress = null;

            if (progress != null)
            {
                extractProgress = new Progress <double>();
                extractProgress.ProgressChanged += (o, ea) =>
                {
                    progress.Report(70 + Convert.ToInt32(Math.Floor(20 * (ea / 100))));
                };
            }
            var tempDir = Path.Combine(Path.GetTempPath(), $"Celeste_Launcher_v{newVersion.Version}");

            if (Directory.Exists(tempDir))
            {
                Misc.CleanUpFiles(tempDir, "*.*");
            }

            try
            {
                await ZipUtils.ExtractZipFile(tempFileName, tempDir, extractProgress, ct);
            }
            catch (AggregateException)
            {
                Misc.CleanUpFiles(tempDir, "*.*");
                throw;
            }
            finally
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }
            }

            //Move File
            progress?.Report(90);

            var destinationDir = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;

            try
            {
                Misc.MoveFiles(tempDir, destinationDir);
            }
            finally
            {
                Misc.CleanUpFiles(tempDir, "*.*");
            }

            //isSteam Version
            if (isSteam)
            {
                progress?.Report(95);
                Steam.ConvertToSteam(destinationDir);
            }

            //Clean Old File
            progress?.Report(97);
            Misc.CleanUpFiles(Directory.GetCurrentDirectory(), "*.old");

            //
            progress?.Report(100);
        }