Example #1
0
        public void Upload(FileInformation info, byte[] raw, bool retry = true)
        {
            string fileName = info.FileName.Replace(@"\", "/");

            if (fileName != "AutoPatcher.gz" && fileName != "PList.gz")
                fileName += ".gz";

            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(Settings.Login, Settings.Password);

                byte[] data = !retry ? raw : raw;
                info.Compressed = data.Length;

                client.UploadProgressChanged += (o, e) =>
                    {
                        int value = (int)(100 * e.BytesSent / e.TotalBytesToSend);
                        progressBar2.Value = value > progressBar2.Maximum ? progressBar2.Maximum : value;

                        FileLabel.Text = fileName;
                        SizeLabel.Text = string.Format("{0} KB / {1} KB", e.BytesSent / 1024, e.TotalBytesToSend  / 1024);
                        SpeedLabel.Text = ((double) e.BytesSent/1024/_stopwatch.Elapsed.TotalSeconds).ToString("0.##") + " KB/s";
                    };

                client.UploadDataCompleted += (o, e) =>
                    {
                        _completedBytes += info.Length;

                        if (e.Error != null && retry)
                        {
                            CheckDirectory(Path.GetDirectoryName(fileName));
                            Upload(info, data, false);
                            return;
                        }

                        if (info.FileName == PatchFileName)
                        {
                            FileLabel.Text = "Complete...";
                            SizeLabel.Text = "Complete...";
                            SpeedLabel.Text = "Complete...";
                            return;
                        }

                        progressBar1.Value = (int)(_completedBytes * 100 / _totalBytes) > 100 ? 100 : (int)(_completedBytes * 100 / _totalBytes);
                        BeginUpload();
                    };

                _stopwatch = Stopwatch.StartNew();

                client.UploadDataAsync(new Uri(Settings.Host + fileName), data);
            }
        }
Example #2
0
        public FileInformation GetFileInformation(string fileName)
        {
            FileInfo info = new FileInfo(fileName);

            FileInformation file =  new FileInformation
                {
                    FileName = fileName.Remove(0, Settings.Client.Length),
                    Length = (int) info.Length,
                    Creation = info.LastWriteTime
                };

            if (file.FileName == "AutoPatcher.exe")
                file.FileName = "AutoPatcher.gz";

            return file;
        }
Example #3
0
        public bool NeedUpdate(FileInformation info)
        {
            for (int i = 0; i < OldList.Count; i++)
            {
                FileInformation old = OldList[i];
                if (old.FileName != info.FileName) continue;

                if (old.Length != info.Length) return true;
                if (old.Creation != info.Creation) return true;

                return false;
            }
            return true;
        }
Example #4
0
        private void ProcessButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessButton.Enabled = false;
                Settings.Client       = ClientTextBox.Text;
                Settings.Host         = HostTextBox.Text;
                Settings.Login        = LoginTextBox.Text;
                Settings.Password     = PasswordTextBox.Text;
                Settings.AllowCleanUp = AllowCleanCheckBox.Checked;

                OldList    = new List <FileInformation>();
                NewList    = new List <FileInformation>();
                UploadList = new Queue <FileInformation>();

                byte[] data = Download(PatchFileName);

                if (data != null)
                {
                    using (MemoryStream stream = new MemoryStream(data))
                        using (BinaryReader reader = new BinaryReader(stream))
                            ParseOld(reader);
                }

                ActionLabel.Text = "Checking Files...";
                Refresh();

                CheckFiles();

                for (int i = 0; i < NewList.Count; i++)
                {
                    FileInformation info = NewList[i];

                    if (InExcludeList(info.FileName))
                    {
                        continue;
                    }

                    if (NeedUpdate(info))
                    {
                        UploadList.Enqueue(info);
                        _totalBytes += info.Length;
                    }
                    else
                    {
                        for (int o = 0; o < OldList.Count; o++)
                        {
                            if (OldList[o].FileName != info.FileName)
                            {
                                continue;
                            }
                            NewList[i] = OldList[o];
                            break;
                        }
                    }
                }

                BeginUpload();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                ActionLabel.Text = "Error...";
            }
        }
Example #5
0
        public void Download(FileInformation info)
        {
            string fileName = info.FileName.Replace(@"\", "/");

            if (fileName != "PList.gz")
            {
                fileName += ".gz";
            }

            try
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadProgressChanged += (o, e) =>
                    {
                        _currentBytes = e.BytesReceived;

                        int value = (int)(100 * _currentBytes / _currentFile.Length);
                        progressBar2.Value = value > progressBar2.Maximum ? progressBar2.Maximum : value;

                        FileLabel.Text  = fileName;
                        SizeLabel.Text  = string.Format("{0} KB / {1} KB", _currentBytes / 1024, _currentFile.Length / 1024);
                        SpeedLabel.Text = (_currentBytes / 1024F / _stopwatch.Elapsed.TotalSeconds).ToString("#,##0.##") + "KB/s";
                    };
                    client.DownloadDataCompleted += (o, e) =>
                    {
                        if (e.Error != null)
                        {
                            File.AppendAllText(@".\Error.txt",
                                               string.Format("[{0}] {1}{2}", DateTime.Now, info.FileName + " could not be downloaded. (" + e.Error.Message + ")", Environment.NewLine));
                        }
                        else
                        {
                            _currentCount++;
                            _completedBytes += _currentBytes;
                            _currentBytes    = 0;
                            _stopwatch.Stop();

                            if (!Directory.Exists(Settings.Client + Path.GetDirectoryName(info.FileName)))
                            {
                                Directory.CreateDirectory(Settings.Client + Path.GetDirectoryName(info.FileName));
                            }

                            File.WriteAllBytes(Settings.Client + info.FileName, e.Result);
                            File.SetLastWriteTime(Settings.Client + info.FileName, info.Creation);
                        }
                        BeginDownload();
                    };

                    client.Credentials = new NetworkCredential(Settings.Login, Settings.Password);

                    progressBar1.Value = (int)(_completedBytes * 100 / _totalBytes) > 100 ? 100 : (int)(_completedBytes * 100 / _totalBytes);

                    _stopwatch = Stopwatch.StartNew();
                    client.DownloadDataAsync(new Uri(Settings.Host + fileName));
                }
            }
            catch
            {
                MessageBox.Show(string.Format("Failed to download file: {0}", fileName));
            }
        }
Example #6
0
        private void ProcessButton_Click(object sender, EventArgs e)
        {
            try
            {
                // 配置文件参数保存
                ProcessButton.Enabled = false;
                Settings.Client       = ClientTextBox.Text;
                Settings.Host         = HostTextBox.Text;
                Settings.Login        = LoginTextBox.Text;
                Settings.Password     = PasswordTextBox.Text;
                Settings.AllowCleanUp = AllowCleanCheckBox.Checked;

                // 旧列表、新列表、更新列表
                OldList    = new List <FileInformation>();
                NewList    = new List <FileInformation>();
                UploadList = new Queue <FileInformation>();

                // 从服务器上下载补丁列表文件
                byte[] data = Download(PatchFileName);

                // 根据文件生成 旧列表
                if (data != null)
                {
                    using (MemoryStream stream = new MemoryStream(data))
                        using (BinaryReader reader = new BinaryReader(stream))
                            ParseOld(reader);
                }

                ActionLabel.Text = "检查文件...";
                Refresh();

                // 根据本地文件生成 新列表
                CheckFiles();

                // 遍历[新列表]且与[旧列表]对比,生成[更新列表]
                for (int i = 0; i < NewList.Count; i++)
                {
                    // 遍历[新列表]内文件信息
                    FileInformation info = NewList[i];

                    // 文件存在[排除列表]中,跳过本次循环
                    if (InExcludeList(info.FileName))
                    {
                        continue;
                    }

                    // 文件是否需要更新
                    if (NeedUpdate(info))
                    {
                        // 加入到[更新列表]中
                        UploadList.Enqueue(info);
                        _totalBytes += info.Length;
                    }
                    else
                    {
                        for (int o = 0; o < OldList.Count; o++)
                        {
                            if (OldList[o].FileName != info.FileName)
                            {
                                continue;
                            }
                            NewList[i] = OldList[o];
                            break;
                        }
                    }
                }


                BeginUpload();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                ActionLabel.Text = "Error...";
            }
        }