Exemple #1
0
        public static Package Load(IEnvironment environment, UriString packageFeed)
        {
            Package package  = null;
            var     filename = packageFeed.Filename.ToNPath();

            if (!filename.IsInitialized || filename.IsRoot)
            {
                return(package);
            }
            var   key  = filename.FileNameWithoutExtension + "_updatelastCheckTime";
            var   now  = DateTimeOffset.Now;
            NPath feed = environment.UserCachePath.Combine(packageFeed.Filename);

            if (!feed.FileExists() || now.Date > environment.UserSettings.Get <DateTimeOffset>(key).Date)
            {
                feed = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem, packageFeed, environment.UserCachePath)
                       .Catch(ex =>
                {
                    LogHelper.Warning(@"Error downloading package feed:{0} ""{1}"" Message:""{2}""", packageFeed, ex.GetType().ToString(), ex.GetExceptionMessageShort());
                    return(true);
                })
                       .RunWithReturn(true);

                if (feed.IsInitialized)
                {
                    environment.UserSettings.Set <DateTimeOffset>(key, now);
                }
            }

            if (!feed.IsInitialized)
            {
                // try from assembly resources
                feed = AssemblyResources.ToFile(ResourceType.Platform, packageFeed.Filename, environment.UserCachePath, environment);
            }

            if (feed.IsInitialized)
            {
                try
                {
                    package = feed.ReadAllText().FromJson <Package>(true, false);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
            }
            return(package);
        }
Exemple #2
0
        public static void CheckForUpdates(IApplicationManager manager)
        {
            var download = new DownloadTask(manager.TaskManager.Token, manager.Environment.FileSystem, UpdateFeedUrl, manager.Environment.UserCachePath)
                           .Catch(ex =>
            {
                LogHelper.Warning(@"Error downloading update check:{0} ""{1}""", UpdateFeedUrl, ex.GetExceptionMessageShort());
                return(true);
            });

            download.OnEnd += (thisTask, result, success, exception) =>
            {
                if (success)
                {
                    try
                    {
                        Package    package    = result.ReadAllText().FromJson <Package>(lowerCase: true, onlyPublic: false);
                        TheVersion current    = TheVersion.Parse(ApplicationInfo.Version);
                        TheVersion newVersion = package.Version;

                        var versionToSkip = manager.UserSettings.Get <TheVersion>(Constants.SkipVersionKey);
                        if (versionToSkip == newVersion)
                        {
                            LogHelper.Info("Skipping GitHub for Unity update v" + newVersion);
                            return;
                        }

                        if (newVersion <= current)
                        {
                            LogHelper.Trace("Skipping GitHub for Unity update v" + newVersion + ", we already have it");
                            return;
                        }

                        manager.TaskManager.RunInUI(() =>
                        {
                            NotifyOfNewUpdate(manager, current, package);
                        });
                    }
                    catch (Exception ex)
                    {
                        LogHelper.GetLogger <UpdateCheckWindow>().Error(ex);
                    }
                }
            };
            download.Start();
        }
Exemple #3
0
        public void QueueDownload(UriString url, NPath targetDirectory)
        {
            var download = new DownloadTask(Token, fileSystem, url, targetDirectory);

            download.OnStart += t => OnDownloadStart?.Invoke(((DownloadTask)t).Url);
            download.OnEnd   += (t, res, s, ex) =>
            {
                if (s)
                {
                    OnDownloadComplete?.Invoke(((DownloadTask)t).Url, res);
                }
                else
                {
                    OnDownloadFailed?.Invoke(((DownloadTask)t).Url, ex);
                }
            };
            // queue after hooking up events so OnDownload* gets called first
            Queue(download);
        }
Exemple #4
0
        private ITask CreateDownloadTask()
        {
            gitArchiveFilePath = installDetails.PluginDataPath.Combine("git.zip");
            gitLfsArchivePath  = installDetails.PluginDataPath.Combine("git-lfs.zip");

            var downloadGitMd5Task = new DownloadTextTask(TaskManager.Instance.Token, environment.FileSystem,
                                                          installDetails.GitZipMd5Url, installDetails.PluginDataPath);

            var downloadGitTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
                                                   installDetails.GitZipUrl, installDetails.PluginDataPath);

            var downloadGitLfsMd5Task = new DownloadTextTask(TaskManager.Instance.Token, environment.FileSystem,
                                                             installDetails.GitLfsZipMd5Url, installDetails.PluginDataPath);

            var downloadGitLfsTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
                                                      installDetails.GitLfsZipUrl, installDetails.PluginDataPath);

            return
                (downloadGitMd5Task.Then((b, s) => { downloadGitTask.ValidationHash = s; })
                 .Then(downloadGitTask)
                 .Then(downloadGitLfsMd5Task)
                 .Then((b, s) => { downloadGitLfsTask.ValidationHash = s; })
                 .Then(downloadGitLfsTask));
        }
Exemple #5
0
            public Task <DownloadData> QueueDownload(UriString url, UriString md5Url, NPath targetDirectory)
            {
                var destinationFile = targetDirectory.Combine(url.Filename);
                var destinationMd5  = targetDirectory.Combine(md5Url.Filename);
                var result          = new DownloadData(url, destinationFile);

                Action <ITask <NPath>, NPath, bool, Exception> verifyDownload = (t, res, success, ex) =>
                {
                    var count = Interlocked.Increment(ref finishedTaskCount);
                    isSuccessful &= success;
                    if (!success)
                    {
                        exception = ex;
                    }
                    if (count == queuedTasks.Count)
                    {
                        if (!isSuccessful)
                        {
                            DownloadFailed(result, exception);
                        }
                        else
                        {
                            if (!Utils.VerifyFileIntegrity(destinationFile, destinationMd5))
                            {
                                destinationMd5.Delete();
                                destinationFile.Delete();
                                DownloadFailed(result, new DownloadException($"Verification of {url} failed"));
                            }
                            else
                            {
                                DownloadComplete(result);
                            }
                        }
                    }
                };

                var md5Exists  = destinationMd5.FileExists();
                var fileExists = destinationFile.FileExists();

                if (!md5Exists)
                {
                    destinationMd5.DeleteIfExists();
                    var md5Download = new DownloadTask(cancellationToken, fs, md5Url, targetDirectory)
                                      .Catch(e => DownloadFailed(result, e));
                    md5Download.OnEnd += verifyDownload;
                    queuedTasks.Add(md5Download);
                }

                if (!fileExists)
                {
                    var fileDownload = new DownloadTask(cancellationToken, fs, url, targetDirectory)
                                       .Catch(e => DownloadFailed(result, e));
                    fileDownload.OnStart += _ => DownloadStart?.Invoke(result);
                    fileDownload.OnEnd   += verifyDownload;
                    queuedTasks.Add(fileDownload);
                }

                if (fileExists && md5Exists)
                {
                    var verification = new FuncTask <NPath>(cancellationToken, () => destinationFile);
                    verification.OnEnd += verifyDownload;
                    queuedTasks.Add(verification);
                }
                return(aggregateDownloads.Task);
            }