private void DownloadAllComponents()
        {
            if (!Directory.Exists(InstallerSettings.DownloadDirectory))
            {
                Directory.CreateDirectory(InstallerSettings.DownloadDirectory);
            }

            Downloader = new ComponentDownloader();
            Downloader.OnDownloadProgressChanged += OnDownloadProgressChanged;

            List <InstallerComponent> components = InstallerComponents.Components;

            for (int i = 0; i < components.Count; i++)
            {
                InstallerComponent component = components[i];

                // if it's disabled,
                // skip it
                if (!component.Enabled)
                {
                    continue;
                }
retry:
                ChangeProgressBarValue(0);
                Log($"Downloading {component.Name}...");

                Downloader.DownloadComponent(ref component, InstallerSettings.DownloadDirectory);

                InstallerComponents.Components[i] = component;

                if (Downloader.Failed)
                {
                    Log($"Downloading {component.Name} Failed");

                    // log Exception aswell, if it exists
                    if (Downloader.Exception != null)
                    {
                        Log(Downloader.Exception.Message);
                        Log(Downloader.Exception.StackTrace);
                    }

                    // ask the user if they want to retry
                    DialogResult ret = MessageBox.Show("Download Failed, Try Again?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (ret == DialogResult.Yes)
                    {
                        // I know, I know, 'goto bad', but if you really hate this,
                        // PR me a rework of this mess which doesn't use goto
                        goto retry;
                    }

                    return;
                }
            }

            Log("Downloading completed");

            LaunchInstallComponents();
        }
        private async Task DownloadAllComponents()
        {
            var downloader = new ComponentDownloader();

            downloader.OnDownloadProgressChanged += OnDownloadProgressChanged;

            var components = InstallerSettings.InstallerComponents.Components;

            for (int i = 0; i < components.Count; i++)
            {
                InstallerComponent component = components[i];

                // make sure component is enabled
                if (!component.Enabled)
                {
                    continue;
                }

                bool useFallback = false;
retry:
                try
                {
                    ChangeProgressBarValue(0);
                    Log($"Downloading {component.Name}...");

                    await downloader.DownloadComponent(i, InstallerSettings.DownloadDirectory, useFallback);
                }
                catch (Exception e)
                {
                    Log($"Downloading {component.Name} Failed");
                    Log(e.Message);
                    Log(e.StackTrace);

                    if (component.FallbackUrls != null &&
                        !useFallback)
                    {
                        Log("Retrying With Fallback...");
                        useFallback = true;
                        goto retry;
                    }

                    // ask the user if they want to retry
                    DialogResult ret = MessageBox.Show("Download Failed, Try Again?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (ret == DialogResult.Yes)
                    {
                        useFallback = false;
                        goto retry;
                    }

                    // early exit
                    return;
                }
            }

            Log("Downloading completed");
            LaunchInstallComponents();
        }
        private long GetAverageDownloadSpeed(long currentTotal, InstallerComponent currentComponent)
        {
            // if we're at a different component,
            // reset everything
            if (currentComponent != oldComponent)
            {
                downloadQueue.Clear();
                oldComponent = currentComponent;
                downloadStopWatch.Restart();
                previousTotal = -1;
                oldAverage    = -1;

                return(oldAverage);
            }

            if (!downloadStopWatch.IsRunning)
            {
                downloadStopWatch.Start();
            }

            if (downloadStopWatch.ElapsedMilliseconds < 1000)
            {
                return(oldAverage);
            }

            if (downloadQueue.Count >= 20)
            {
                downloadQueue.Dequeue();
            }

            if (previousTotal == -1)
            {
                previousTotal = currentTotal;

                return(oldAverage);
            }

            // Enqueue average download speed since last average was enqueued
            downloadQueue.Enqueue((long)((currentTotal - previousTotal) / downloadStopWatch.Elapsed.TotalSeconds));
            previousTotal = currentTotal;

            // Total average for component is the average of average download speeds
            long average = downloadQueue.Sum() / downloadQueue.Count;

            oldAverage = average;
            downloadStopWatch.Restart();

            return(average);
        }
Exemple #4
0
        private void DownloadAllComponents()
        {
            if (!Directory.Exists(InstallerSettings.DownloadDirectory))
            {
                Directory.CreateDirectory(InstallerSettings.DownloadDirectory);
            }

            Downloader = new ComponentDownloader();
            Downloader.OnDownloadProgressChanged += OnDownloadProgressChanged;

            List <InstallerComponent> components = InstallerComponents.Components;

            for (int i = 0; i < components.Count; i++)
            {
                InstallerComponent component = components[i];

                // if it's disabled,
                // skip it
                if (!component.Enabled)
                {
                    continue;
                }

                ChangeProgressBarValue(0);
                Log($"Downloading {component.Name}...");

                Downloader.DownloadComponent(ref component, InstallerSettings.DownloadDirectory);

                InstallerComponents.Components[i] = component;

                if (Downloader.Failed)
                {
                    Log($"Downloading {component.Name} Failed");

                    // log Exception aswell, if it exists
                    if (Downloader.Exception != null)
                    {
                        Log(Downloader.Exception.Message);
                        Log(Downloader.Exception.StackTrace);
                    }
                    return;
                }
            }

            Log("Downloading completed");

            LaunchInstallComponents();
        }