Beispiel #1
0
        public static async Task <bool> Download(string version, string gameId, string outputFile, CancellationToken cancellation)
        {
            // Check if the version is known
            Update update = Update.Find(version);

            string dir = Path.GetDirectoryName(outputFile);

            if (string.IsNullOrEmpty(dir))
            {
                throw new ArgumentException(@"Outputfile must be a full path", outputFile);
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            Interaction.Instance.SetProgressMaximum(updateSize != 0 ? updateSize : update.Size);
            try
            {
                using (Stream s = await OpenUrl(update, gameId))
                {
                    if (cancellation.IsCancellationRequested)
                    {
                        return(false);
                    }

                    string tmpFile = string.Concat(outputFile, ".tmp");
                    using (FileStream fs = new FileStream(tmpFile, FileMode.Create, FileAccess.Write))
                    {
                        if (isRarStream)
                        {
                            // Wrap into unrar
                            RarReader reader = RarReader.Open(s, RarOptions.None);
                            if (!reader.MoveToNextEntry())
                            {
                                return(false);
                            }
                            reader.WriteEntryTo(new WriteProgressStream(fs), cancellation);
                        }
                        else
                        {
                            int    bytesRead = -1, totalBytesRead = 0;
                            byte[] buffer = new byte[2048];
                            while (bytesRead != 0)
                            {
                                if (cancellation.IsCancellationRequested)
                                {
                                    break;
                                }

                                bytesRead = await s.ReadAsync(buffer, 0, buffer.Length);

                                fs.Write(buffer, 0, bytesRead);
                                totalBytesRead += bytesRead;

                                Interaction.Instance.ReportProgress(totalBytesRead);
                            }
                        }
                    }

                    if (cancellation.IsCancellationRequested)
                    {
                        File.Delete(tmpFile);
                        return(false);
                    }
                    if (File.Exists(outputFile))
                    {
                        File.Delete(outputFile);
                    }
                    File.Move(tmpFile, outputFile);
                    return(true);
                }
            }
            catch (WebException)
            {
                return(false);
            }
        }