private static void Download(bool isMain, bool isAll) { Tencent_cos_download Downloader = new Tencent_cos_download(); bool skip1 = true, skip2 = true, skipped1 = false, skipped2 = false; if (isAll) { if (isMain && updateFileName.Exists(f => Path.GetFileName(f) == "player.cpp")) { Console.Write("确认更新player.cpp文件? y/n:"); Console.ReadLine(); if (Console.Read() == 'y') { skip1 = false; } } if (!isMain && updateFileName.Exists(f => Path.GetFileName(f) == "server.playback")) { Console.Write("确认更新server.playback文件? y/n:"); Console.ReadLine(); if (Console.Read() == 'y') { skip1 = false; } } if (!isMain && updateFileName.Exists(f => Path.GetFileName(f) == "ClientConfig.json")) { Console.Write("确认更新ClientConfig.json文件? y/n:"); Console.ReadLine(); if (Console.Read() == 'y') { skip2 = false; } } } int newFile = 0, updateFile = 0; int totalnew = newFileName.Count, totalupdate = updateFileName.Count; if (newFileName.Count > 0 || updateFileName.Count > 0) { try { foreach (string Filename in newFileName) { Console.WriteLine(newFile + 1 + "/" + totalnew + ": 开始下载" + Filename); Downloader.download(System.IO.Path.Combine(Data.FilePath, Filename), Filename); Console.WriteLine(Filename + "下载完毕!" + Environment.NewLine); newFile++; } foreach (string Filename in updateFileName) { if (skip1 && (Path.GetFileName(Filename) == "player.cpp" || Path.GetFileName(Filename) == "server.playback")) { skipped1 = true; continue; } if (skip2 && Path.GetFileName(Filename) == "ClientConfig.json") { skipped2 = true; continue; } Console.WriteLine(updateFile + 1 + "/" + totalupdate + ": 开始更新" + Filename); File.Delete(System.IO.Path.Combine(Data.FilePath, Filename)); Downloader.download(System.IO.Path.Combine(Data.FilePath, Filename), Filename); Console.WriteLine(Filename + "更新完毕!" + Environment.NewLine); updateFile++; } } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx.ToString() + Environment.NewLine); return; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosClientException: " + serverEx.ToString() + Environment.NewLine); return; } catch (System.Exception) { throw; } Console.WriteLine($"下载成功!共下载{newFile}个新文件,更新{updateFile}个文件"); if (skipped1 && isMain) { Console.WriteLine(Environment.NewLine + $"自动跳过了player.cpp文件"); } else if (skipped1 && !isMain) { Console.WriteLine(Environment.NewLine + $"自动跳过了server.playback文件"); } if (skipped2) { Console.WriteLine($"自动跳过了ClientConfig.json文件" + Environment.NewLine); } } else { Console.WriteLine("当前平台已是最新版本!" + Environment.NewLine); } newFileName.Clear(); updateFileName.Clear(); }
private async void button4_Click(object sender, EventArgs e) { string json, MD5; int newFile = 0, updateFile = 0; newFileName.Clear(); updateFileName.Clear(); Tencent_cos_download Downloader = new Tencent_cos_download(); try { if (File.Exists(System.IO.Path.Combine(this.textBox1.Text, "clientForWinmd5list.json"))) { File.Delete(System.IO.Path.Combine(this.textBox1.Text, "clientForWinmd5list.json")); await Task.Run(() => { Downloader.download(System.IO.Path.Combine(this.textBox1.Text, "clientForWinmd5list.json"), "clientForWinmd5list.json"); }); } else { await Task.Run(() => { Downloader.download(System.IO.Path.Combine(this.textBox1.Text, "clientForWinmd5list.json"), "clientForWinmd5list.json"); }); } } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 this.textBox2.AppendText("CosClientException: " + clientEx.ToString() + Environment.NewLine); return; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 this.textBox2.AppendText("CosClientException: " + serverEx.ToString() + Environment.NewLine); return; } using (StreamReader r = new StreamReader(System.IO.Path.Combine(this.textBox1.Text, "clientForWinmd5list.json"))) json = r.ReadToEnd(); json = json.Replace("\r", string.Empty).Replace("\n", string.Empty); Dictionary <string, string> jsonDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); await Task.Run(() => { foreach (KeyValuePair <string, string> pair in jsonDict) { MD5 = GetFileMd5Hash(System.IO.Path.Combine(this.textBox1.Text, pair.Key)); if (MD5.Length == 0) { newFileName.Add(pair.Key); } else if (MD5 != pair.Value) { updateFileName.Add(pair.Key); } } }); newFile = newFileName.Count(); updateFile = updateFileName.Count(); if (this.textBox2.Text.Length != 0) { this.textBox2.AppendText("----------------------" + Environment.NewLine); } if (newFile + updateFile == 0) { this.textBox2.AppendText("暂无新unity文件!" + Environment.NewLine); newFileName.Clear(); updateFileName.Clear(); } else { this.textBox2.AppendText($"发现{newFile}个新unity文件:" + Environment.NewLine); foreach (string filename in newFileName) { this.textBox2.AppendText(filename + Environment.NewLine); } this.textBox2.AppendText(Environment.NewLine + $"发现{updateFile}个unity文件更新:" + Environment.NewLine); foreach (string filename in updateFileName) { this.textBox2.AppendText(filename + Environment.NewLine); } this.textBox2.AppendText(Environment.NewLine + "请点击下载按钮选择文件" + Environment.NewLine + Environment.NewLine); } Checked = true; }
private static void Check(bool isMain, bool isAll) { string json, MD5, jsonName, typeName; int newFile = 0, updateFile = 0; newFileName.Clear(); updateFileName.Clear(); if (isMain) { jsonName = "md5list.json"; typeName = "核心"; } else { jsonName = "ClientForLinuxmd5list.json"; typeName = "unity"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { jsonName = "ClientForLinuxmd5list.json"; Console.WriteLine("unity for linux:"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { jsonName = "ClientForMacmd5list.json"; Console.WriteLine("unity for mac:"); } else { Console.WriteLine("unity for windows:"); } } Tencent_cos_download Downloader = new Tencent_cos_download(); try { if (File.Exists(System.IO.Path.Combine(Data.FilePath, jsonName))) { File.Delete(System.IO.Path.Combine(Data.FilePath, jsonName)); Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName); //await Task.Run(() => { Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName)); }); } else { Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName); } //await Task.Run(() => { Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName)); }); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx.ToString() + Environment.NewLine); return; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosClientException: " + serverEx.ToString() + Environment.NewLine); return; } using (StreamReader r = new StreamReader(System.IO.Path.Combine(Data.FilePath, jsonName))) json = r.ReadToEnd(); json = json.Replace("\r", string.Empty).Replace("\n", string.Empty); Dictionary <string, string> jsonDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); foreach (KeyValuePair <string, string> pair in jsonDict) //await { MD5 = GetFileMd5Hash(System.IO.Path.Combine(Data.FilePath, pair.Key)); if (MD5.Length == 0) { newFileName.Add(pair.Key); } else if (MD5 != pair.Value) { updateFileName.Add(pair.Key); } } newFile = newFileName.Count; updateFile = updateFileName.Count; Console.WriteLine("----------------------" + Environment.NewLine); if (newFile + updateFile == 0) { Console.WriteLine("当前平台已是最新版本!" + Environment.NewLine); newFileName.Clear(); updateFileName.Clear(); } else { Console.WriteLine($"发现{newFile}个新{typeName}文件:" + Environment.NewLine); foreach (string filename in newFileName) { Console.WriteLine(filename); } Console.WriteLine(Environment.NewLine + $"发现{updateFile}个{typeName}文件更新:" + Environment.NewLine); foreach (string filename in updateFileName) { Console.WriteLine(filename); } Console.Write(Environment.NewLine + "是否下载新文件? y/n:"); if (Console.Read() != 'y') { Console.WriteLine("下载取消!"); } else { Download(isMain, isAll); } } }
private async void btn_Click(object sender, EventArgs e) { if (!Checked) { await Task.Run(() => { this.button3_Click(sender, e); }); } else { 文件列表 f2 = new 文件列表(newFileName, updateFileName); f2.StartPosition = FormStartPosition.CenterParent; f2.ShowDialog(); if (f2.DialogResult == DialogResult.Yes) { List <string>[] FileNames = (List <string>[])f2.Tag; SelectedNewFile = FileNames[0]; SelectedUpdateFile = FileNames[1]; Tencent_cos_download Downloader = new Tencent_cos_download(); bool cancelled = false; int newFile = 0, updateFile = 0; int totalnew = SelectedNewFile.Count, totalupdate = SelectedUpdateFile.Count; MessageBoxButtons mes = MessageBoxButtons.OKCancel; if (SelectedNewFile.Count > 0 || SelectedUpdateFile.Count > 0) { DialogResult dialogResult = MessageBox.Show($"共有{SelectedNewFile.Count}个新文件,{SelectedUpdateFile.Count}个更新", "提示", mes); if (dialogResult != DialogResult.OK) { SelectedNewFile.Clear(); SelectedUpdateFile.Clear(); return; } try { await Task.Run(async() => { foreach (string Filename in SelectedNewFile) { if (token.IsCancellationRequested) { cancelled = true; tokenSource = new CancellationTokenSource(); token = tokenSource.Token; return; } this.textBox2.AppendText(newFile + 1 + "/" + totalnew + ": 开始下载" + Filename + Environment.NewLine); await Task.Run(() => { Downloader.download(System.IO.Path.Combine(this.textBox1.Text, Filename), Filename); }); this.textBox2.AppendText(Filename + "下载完毕!" + Environment.NewLine); newFile++; } foreach (string Filename in SelectedUpdateFile) { if (token.IsCancellationRequested) { cancelled = true; tokenSource = new CancellationTokenSource(); token = tokenSource.Token; return; } this.textBox2.AppendText(updateFile + 1 + "/" + totalupdate + ": 开始更新" + Filename + Environment.NewLine); File.Delete(System.IO.Path.Combine(this.textBox1.Text, Filename)); await Task.Run(() => { Downloader.download(System.IO.Path.Combine(this.textBox1.Text, Filename), Filename); }); this.textBox2.AppendText(Filename + "更新完毕!" + Environment.NewLine); updateFile++; } }, token); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 this.textBox2.AppendText("CosClientException: " + clientEx.ToString() + Environment.NewLine); return; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 this.textBox2.AppendText("CosClientException: " + serverEx.ToString() + Environment.NewLine); return; } catch (System.Exception) { throw; } if (!cancelled) { this.textBox2.AppendText($"下载成功!共下载{newFile}个新文件,更新{updateFile}个文件" + Environment.NewLine); } else { this.textBox2.AppendText($"下载取消,共下载{newFile}个新文件,更新{updateFile}个文件" + Environment.NewLine); } cancelled = false; } else { this.textBox2.AppendText("当前平台已是最新版本!" + Environment.NewLine); } SelectedNewFile.Clear(); SelectedUpdateFile.Clear(); newFileName.Clear(); updateFileName.Clear(); } } }