예제 #1
0
        public Download GetAndSwitchEngine(string version)
        {
            lock (downloads) {
                downloads.RemoveAll(x => x.IsAborted || x.IsComplete != null); // remove already completed downloads from list}
                var existing = downloads.SingleOrDefault(x => x.Name == version);
                if (existing != null)
                {
                    return(existing);
                }

                if (SpringPaths.HasEngineVersion(version))
                {
                    SpringPaths.SetEnginePath(SpringPaths.GetEngineFolderByVersion(version));
                    return(null);
                }
                else
                {
                    var down = new EngineDownload(version, SpringPaths);
                    downloads.Add(down);
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                    down.Start();
                    return(down);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Private method that contains the actual logic for adding a download
        /// </summary>
        /// <param name="item"></param>
        private void InternalAddDownload(IDownloadItem item)
        {
            // Check dirs
            if (!Directory.Exists(Config.Instance.GetInProgressDownloadsFolder()))
            {
                Directory.CreateDirectory(Config.Instance.GetInProgressDownloadsFolder());
            }
            if (!Directory.Exists(Config.Instance.GetCompleteDownloadsFolder()))
            {
                Directory.CreateDirectory(Config.Instance.GetCompleteDownloadsFolder());
            }

            // Create and store downloader
            IDownloader dl = DownloaderFactory.CreateFrom(item.DownloadUri);

            dl.SetItem(item);

            // Register events
            dl.DownloadProgressChanged += IDownloader_DownloadProgressChanged;
            dl.DownloadStatusChanged   += IDownloader_DownloadStatusChanged;

            downloads[dl.CurrentDownloadItem] = dl;
            // Fire event
            DownloadAdded?.Invoke(this, new DownloadEventArgs(dl.CurrentDownloadItem));

            UpdateActiveDownloads();
            SaveDownloads();
        }
        /// <summary>
        /// Downloads specified video.
        /// </summary>
        /// <param name="video">The video to download.</param>
        /// <param name="upgradeAudio">If true, only the audio will be downloaded and it will be merged with the local video file.</param>
        /// <param name="callback">The method to call once download is completed.</param>
        public async Task DownloadVideoAsync(string url, string destination, string description, EventHandler <DownloadCompletedEventArgs> callback, DownloadAction action, DownloadOptions options, object data)
        {
            if (IsDownloadDuplicate(url))
            {
                return;
            }

            Directory.CreateDirectory(Path.GetDirectoryName(destination));

            // Add DownloadItem right away before doing any async work.
            string       DestinationNoExt = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination));
            DownloadItem DownloadInfo     = new DownloadItem(url, destination, DestinationNoExt, description, action, callback, options, data);

            Application.Current.Dispatcher.Invoke(() => {
                downloadsList.Insert(0, DownloadInfo);
                // Notify UI of new download to show window.
                DownloadAdded?.Invoke(this, new EventArgs());
            });

            if (downloadsList.Where(d => d.Status == DownloadStatus.Downloading || d.Status == DownloadStatus.Initializing).Count() < Options.SimultaneousDownloads)
            {
                await StartDownloadAsync(DownloadInfo).ConfigureAwait(false);
            }
        }
예제 #4
0
        public Download GetResource(DownloadType type, string name)
        {
            if (name == "zk:dev" || name == "Zero-K $VERSION")
            {
                return(null);
            }
            lock (downloads)
            {
                downloads.RemoveAll(x => x.IsAborted || x.IsComplete != null); // remove already completed downloads from list}
                var existing = downloads.FirstOrDefault(x => x.Name == name || x.Alias == name);
                if (existing != null)
                {
                    return(existing);
                }
            }

            if (scanner?.HasResource(name) == true)
            {
                return(null);
            }
            if (SpringPaths.HasEngineVersion(name))
            {
                return(null);
            }


            // check rapid to determine type
            if (type == DownloadType.NOTKNOWN)
            {
                if (packageDownloader.GetByInternalName(name) != null || packageDownloader.GetByTag(name) != null)
                {
                    type = DownloadType.RAPID;
                }
                else
                {
                    packageDownloader.LoadMasterAndVersions().Wait();
                    if (packageDownloader.GetByInternalName(name) != null || packageDownloader.GetByTag(name) != null)
                    {
                        type = DownloadType.RAPID;
                    }
                    else
                    {
                        type = DownloadType.MAP;
                    }
                }
            }


            lock (downloads)
            {
                if (type == DownloadType.DEMO)
                {
                    var target     = new Uri(name);
                    var targetName = target.Segments.Last();
                    var filePath   = Utils.MakePath(SpringPaths.WritableDirectory, "demos", targetName);
                    if (File.Exists(filePath))
                    {
                        return(null);
                    }
                    var down = new WebFileDownload(name, filePath, null);
                    down.DownloadType = type;
                    downloads.Add(down);
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down)); //create dowload bar (handled by MainWindow.cs)
                    down.Start();
                    return(down);
                }


                if (type == DownloadType.MAP || type == DownloadType.MISSION)
                {
                    if (torrentDownloader == null)
                    {
                        torrentDownloader = new TorrentDownloader(this);                            //lazy initialization
                    }
                    var down = torrentDownloader.DownloadTorrent(name);
                    if (down != null)
                    {
                        down.DownloadType = type;
                        downloads.Add(down);
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }

                if (type == DownloadType.RAPID)
                {
                    var down = packageDownloader.GetPackageDownload(name);
                    if (down != null)
                    {
                        down.DownloadType = type;
                        down.Alias        = name;
                        downloads.Add(down);
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }



                if (type == DownloadType.ENGINE)
                {
                    var down = new EngineDownload(name, SpringPaths);
                    down.DownloadType = type;
                    downloads.Add(down);
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                    down.Start();
                    return(down);
                }

                return(null);
            }
        }
 protected void OnDownloadAdded()
 {
     DownloadAdded?.Invoke(this);
     DownloadsUpdated?.Invoke(this);
 }
예제 #6
0
        public Download GetResource(DownloadType type, string name)
        {
            lock (downloads) {
                downloads.RemoveAll(x => x.IsAborted || x.IsComplete != null); // remove already completed downloads from list}
                var existing = downloads.FirstOrDefault(x => x.Name == name);
                if (existing != null)
                {
                    return(existing);
                }
            }

            if (scanner != null && scanner.HasResource(name))
            {
                return(null);
            }

            if (type == DownloadType.MOD || type == DownloadType.UNKNOWN)
            {
                packageDownloader.LoadMasterAndVersions(false).Wait();
            }

            lock (downloads) {
                if (type == DownloadType.DEMO)
                {
                    var target     = new Uri(name);
                    var targetName = target.Segments.Last();
                    var filePath   = Utils.MakePath(SpringPaths.WritableDirectory, "demos", targetName);
                    if (File.Exists(filePath))
                    {
                        return(null);
                    }
                    try {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    } catch {}
                    var down = new WebFileDownload(name, filePath, null);
                    downloads.Add(down);
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down)); //create dowload bar (handled by MainWindow.cs)
                    down.Start();
                    return(down);
                }

                if (type == DownloadType.MOD || type == DownloadType.UNKNOWN)
                {
                    var down = packageDownloader.GetPackageDownload(name);
                    if (down != null)
                    {
                        downloads.Add(down);
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }

                if (type == DownloadType.MAP || type == DownloadType.MOD || type == DownloadType.UNKNOWN || type == DownloadType.MISSION)
                {
                    if (torrentDownloader == null)
                    {
                        torrentDownloader = new TorrentDownloader(this);                            //lazy initialization
                    }
                    var down = torrentDownloader.DownloadTorrent(name);
                    if (down != null)
                    {
                        downloads.Add(down);
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }

                if (type == DownloadType.GAME)
                {
                    throw new ApplicationException(string.Format("{0} download not supported in this version", type));
                }

                return(null);
            }
        }
예제 #7
0
 protected virtual void OnDownloadAdded(Downloader d, bool willStart)
 {
     DownloadAdded?.Invoke(this, new DownloaderEventArgs(d, willStart));
 }
        public Download GetResource(DownloadType type, string name)
        {
            if (name.StartsWith("rapid://")) // note this is not super clean as supplied name might be used for tracking.
            {
                name = name.Substring(8);
                type = DownloadType.RAPID;
            }

            if (name == "zk:dev" || name == "Zero-K $VERSION")
            {
                return(null);
            }
            lock (locker)
            {
                // remove already completed downloads from list
                foreach (var d in downloads.Values.ToList())
                {
                    if (d != null && (d.IsAborted || d.IsComplete != null))
                    {
                        Download dummy;
                        downloads.TryRemove(d.Name, out dummy);
                    }
                }


                var existing = downloads.Values.FirstOrDefault(x => x != null && (x.Name == name || x.Alias == name));
                if (existing != null)
                {
                    return(existing);
                }

                if (scanner?.HasResource(name) == true)
                {
                    return(null);
                }
                if (SpringPaths.HasEngineVersion(name))
                {
                    return(null);
                }


                // check rapid to determine type
                if (type == DownloadType.NOTKNOWN)
                {
                    if (packageDownloader.GetByInternalName(name) != null || packageDownloader.GetByTag(name) != null)
                    {
                        type = DownloadType.RAPID;
                    }
                    else
                    {
                        packageDownloader.LoadMasterAndVersions().Wait();
                        if (packageDownloader.GetByInternalName(name) != null || packageDownloader.GetByTag(name) != null)
                        {
                            type = DownloadType.RAPID;
                        }
                        else
                        {
                            type = DownloadType.MAP;
                        }
                    }
                }



                if (type == DownloadType.DEMO)
                {
                    var target     = new Uri(name);
                    var targetName = target.Segments.Last();
                    var filePath   = Utils.MakePath(SpringPaths.WritableDirectory, "demos", targetName);
                    if (File.Exists(filePath))
                    {
                        return(null);
                    }
                    var down = new WebFileDownload(name, filePath, null);
                    down.DownloadType    = type;
                    downloads[down.Name] = down;
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down)); //create download bar (handled by MainWindow.cs)
                    down.Start();
                    return(down);
                }


                if (type == DownloadType.MAP || type == DownloadType.MISSION)
                {
                    if (torrentDownloader == null)
                    {
                        torrentDownloader = new TorrentDownloader(this);                            //lazy initialization
                    }
                    var down = torrentDownloader.DownloadTorrent(name);
                    if (down != null)
                    {
                        down.DownloadType    = type;
                        downloads[down.Name] = down;
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }

                if (type == DownloadType.RAPID)
                {
                    var down = packageDownloader.GetPackageDownload(name);
                    if (down != null)
                    {
                        down.DownloadType    = type;
                        down.Alias           = name;
                        downloads[down.Name] = down;
                        DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                        return(down);
                    }
                }

                if (type == DownloadType.ENGINE)
                {
                    var down = new EngineDownload(name, SpringPaths);
                    down.DownloadType    = type;
                    downloads[down.Name] = down;
                    DownloadAdded.RaiseAsyncEvent(this, new EventArgs <Download>(down));
                    down.Start();
                    return(down);
                }

                return(null);
            }
        }