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 || x.Alias == name);
                if (existing != null) return existing;
            }

            if (scanner != null && scanner.HasResource(name)) return null;
            
           
            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);
                    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) {
                        down.Alias = name;
                        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;
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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);
            }
        }
        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;
            }
        }
Ejemplo n.º 5
0
 private async Task<bool> DownloadUrl(string desc, string url, string filePathTarget)
 {
     Status = desc;
     var wfd = new WebFileDownload(url, filePathTarget, paths.Cache);
     wfd.Start();
     Download = wfd;
     var dlTask = Download?.WaitHandle.AsTask(TimeSpan.FromMinutes(30));
     if (dlTask != null) await dlTask;
     if (Download?.IsComplete == false)
     {
         Status = $"Download of {Download.Name} has failed";
         return false;
     }
     return true;
 }
Ejemplo n.º 6
0
        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);
            }
        }