From 481cae8d56cbd7cf2cff4025576099c41b8c0cb8 Mon Sep 17 00:00:00 2001 From: sendssf <2336526392@qq.com> Date: Sat, 6 May 2023 14:25:24 +0800 Subject: [PATCH] merge --- installer/Installer/Model.cs | 154 +++++++++++++++++++++++++------ installer/Installer/ViewModel.cs | 26 +++--- 2 files changed, 138 insertions(+), 42 deletions(-) diff --git a/installer/Installer/Model.cs b/installer/Installer/Model.cs index a95f17b..d06254d 100644 --- a/installer/Installer/Model.cs +++ b/installer/Installer/Model.cs @@ -20,6 +20,7 @@ using System.Threading.Tasks; using System.Net.Http; using System.Windows; using System.Windows.Shapes; +using System.Collections.Concurrent; //using System.Windows.Forms; using System.Threading; @@ -361,8 +362,9 @@ namespace Downloader class Program { - static List newFileName = new List(); // 新文件名 - static List updateFileName = new List(); // 更新文件名 + static ConcurrentQueue newFileName = new ConcurrentQueue(); + //static List newFileName = new List(); // 新文件名 + static ConcurrentQueue updateFileName = new ConcurrentQueue(); // 更新文件名 static List updateFailed = new List(); //更新失败的文件名 static public List UpdateFailed { @@ -502,9 +504,8 @@ namespace Downloader .Build(); // 创建 CosXmlConfig 对象 // 永久密钥访问凭证 - string secretId = "***"; //"云 API 密钥 SecretId"; - string secretKey = "***"; //"云 API 密钥 SecretKey"; - + string secretId = "AKIDvhEVXN4cv0ugIlFYiniV6Wk1McfkplYA"; //"云 API 密钥 SecretId"; + string secretKey = "YyGLGCJG4f5VsEUddnz9JSRPSSK8sYBo"; //"云 API 密钥 SecretKey"; long durationSecond = 1000; // 每次请求签名有效时长,单位为秒 QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider( @@ -654,7 +655,7 @@ namespace Downloader { MD5 = GetFileMd5Hash(System.IO.Path.Combine(Data.FilePath, pair.Key.TrimStart(new char[] { '.', '/' }))); if (MD5.Length == 0) // 文档不存在 - newFileName.Add(pair.Key); + newFileName.Enqueue(pair.Key); else if (MD5.Equals("conflict")) { if (pair.Key.Equals("THUAI6/win/CAPI/cpp/.vs/CAPI/v17/Browse.VC.db")) @@ -670,7 +671,7 @@ namespace Downloader MessageBox.Show($"检查{pair.Key}更新时遇到问题,请反馈", "读取出错", MessageBoxButton.OK, MessageBoxImage.Error); } else if (!MD5.Equals(pair.Value) && !IsUserFile(System.IO.Path.GetFileName(pair.Key))) // MD5不匹配 - updateFileName.Add(pair.Key); + updateFileName.Enqueue(pair.Key); } } @@ -736,38 +737,110 @@ namespace Downloader { try { - foreach (string filename in newFileName) + int cnt = newFileName.Count; + if (cnt <= 20) { - //Console.WriteLine(newFile + 1 + "/" + totalnew + ":开始下载" + filename); - Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); - //Console.WriteLine(filename + "下载完毕!" + Environment.NewLine); - newFile++; + while (newFileName.TryDequeue(out var filename)) + { + Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); + //Console.WriteLine(filename + "下载完毕!" + Environment.NewLine); + Interlocked.Increment(ref newFile); + } } - foreach (string filename in updateFileName) + else { - //Console.WriteLine(updateFile + 1 + "/" + totalupdate + ":开始下载" + filename); - try + const int nthread = 8; + var thrds = new List(); + for (int i = 0; i < nthread; i++) { - File.Delete(System.IO.Path.Combine(@Data.FilePath, filename)); - Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); + var thrd = new Thread(() => + { + while (newFileName.TryDequeue(out var filename)) + { + Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); + //Console.WriteLine(filename + "下载完毕!" + Environment.NewLine); + Interlocked.Increment(ref newFile); + } + }); + thrd.Start(); + thrds.Add(thrd); } - catch (System.IO.IOException) + foreach (var thrd in thrds) { - updateFailed = updateFailed.Append(filename).ToList(); + thrd.Join(); } - catch + } + // 读取 Interlocked.CompareExchange(ref newFile, 0, 0); + + int upcnt = updateFileName.Count; + if(upcnt <= 20) + { + while (newFileName.TryDequeue(out var filename)) { - if (filename.Substring(filename.Length - 4, 4).Equals(".pdf")) + try + { + File.Delete(System.IO.Path.Combine(@Data.FilePath, filename)); + Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); + } + catch (System.IO.IOException) + { + updateFailed = updateFailed.Append(filename).ToList(); + } + catch { - MessageBox.Show($"由于曾经发生过的访问冲突,下载器无法更新{filename}\n" - + $"请手动删除{filename},然后再试一次。"); + if (filename.Substring(filename.Length - 4, 4).Equals(".pdf")) + { + MessageBox.Show($"由于曾经发生过的访问冲突,下载器无法更新{filename}\n" + + $"请手动删除{filename},然后再试一次。"); + } + else + MessageBox.Show($"更新{filename}时遇到未知问题,请反馈"); + updateFailed = updateFailed.Append(filename).ToList(); } - else - MessageBox.Show($"更新{filename}时遇到未知问题,请反馈"); - updateFailed = updateFailed.Append(filename).ToList(); + Interlocked.Increment(ref newFile); + } + } + else + { + const int nthread = 8; + var thrds = new List(); + + for (int i = 0; i < nthread; i++) + { + var thrd = new Thread(() => + { + while (updateFileName.TryDequeue(out var filename)) + { + try + { + File.Delete(System.IO.Path.Combine(@Data.FilePath, filename)); + Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename.TrimStart(new char[] { '.', '/' })); + } + catch (System.IO.IOException) + { + updateFailed = updateFailed.Append(filename).ToList(); + } + catch + { + if (filename.Substring(filename.Length - 4, 4).Equals(".pdf")) + { + MessageBox.Show($"由于曾经发生过的访问冲突,下载器无法更新{filename}\n" + + $"请手动删除{filename},然后再试一次。"); + } + else + MessageBox.Show($"更新{filename}时遇到未知问题,请反馈"); + updateFailed = updateFailed.Append(filename).ToList(); + } + Interlocked.Increment(ref newFile); + } + }); + thrd.Start(); + thrds.Add(thrd); + } + foreach (var thrd in thrds) + { + thrd.Join(); } - //Console.WriteLine(filename + "下载完毕!" + Environment.NewLine); - updateFile++; } if (updateFailed.Count == 0) UpdatePlanned = false; @@ -869,7 +942,7 @@ namespace Downloader newFileName.Clear(); updateFileName.Clear(); - newFileName.Add("THUAI6.tar.gz"); + newFileName.Enqueue("THUAI6.tar.gz"); Download(); Stream? inStream = null; Stream? gzipStream = null; @@ -1373,6 +1446,29 @@ namespace Downloader sw.Close(); } } + + public class RunProgram + { + public static int StartServer() + { + Console.WriteLine("Input port: "); + string? port = Console.ReadLine(); + if(port == null || port.Length == 0) + { + return 1; + } + Process process_cmd = new Process(); + process_cmd.StartInfo.FileName = "cmd.exe"; + process_cmd.StartInfo.RedirectStandardInput = true;//是否可以输入 + process_cmd.StartInfo.RedirectStandardOutput = true;//是否可以输出 + process_cmd.StartInfo.CreateNoWindow = false;//不创建窗体 也就是隐藏窗体 + process_cmd.StartInfo.UseShellExecute = true;//是否使用系统shell执行,否 + process_cmd.Start(); + string command = Data.FilePath + "\\THUAI6\\win" + "\\win64\\Server.exe --port 8888 --studentCount 1 --trickerCount 1 --gameTimeInSecond 600 --fileName video"; + process_cmd.StandardInput.WriteLine("你要执行的命令"); + return -1; + } + } } } diff --git a/installer/Installer/ViewModel.cs b/installer/Installer/ViewModel.cs index 742fbb1..2cdc968 100644 --- a/installer/Installer/ViewModel.cs +++ b/installer/Installer/ViewModel.cs @@ -39,19 +39,19 @@ namespace starter.viewmodel.settings string currentDirectory = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName) ?? throw new Exception("Fail to get current directory"); //MessageBox.Show("更新器工作正常"); - if (!Program.Tencent_cos_download.SelfUpdateDismissed()) - { - switch (Program.Tencent_cos_download.CheckSelfVersion()) - { - case 1: - Process.Start(System.IO.Path.Combine(currentDirectory, "InstallerUpdater.exe")); - Environment.Exit(0); - break; - case -1: - MessageBox.Show("下载器更新检查出错,将继续启动现有下载器"); - break; - } - } + //if (!Program.Tencent_cos_download.SelfUpdateDismissed()) + //{ + // switch (Program.Tencent_cos_download.CheckSelfVersion()) + // { + // case 1: + // Process.Start(System.IO.Path.Combine(currentDirectory, "InstallerUpdater.exe")); + // Environment.Exit(0); + // break; + // case -1: + // MessageBox.Show("下载器更新检查出错,将继续启动现有下载器"); + // break; + // } + //} //实例化BackgroundWorker asyncDownloader = new BackgroundWorker();