From f023737033644bcedb4a558ab0c66c04ed88b513 Mon Sep 17 00:00:00 2001 From: sendssf <2336526392@qq.com> Date: Mon, 24 Apr 2023 21:48:38 +0800 Subject: [PATCH 1/5] Fix: fix run.sh bugs --- dependency/shell/run.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dependency/shell/run.sh b/dependency/shell/run.sh index b84bfbc..5b157af 100644 --- a/dependency/shell/run.sh +++ b/dependency/shell/run.sh @@ -9,9 +9,9 @@ do for i in {1..4} do j=$((i - 1)) - if [-f "./python/player$i.py"]; then + if [ -f "./python/player$i.py"]; then nice -0 python3 ./python/player$i.py -I 127.0.0.1 -P 8888 -p $j - elif [-f "./capi$i"]; then + elif [ -f "./capi$i"]; then nice -0 ./capi$i -I 127.0.0.1 -P 8888 -p $j else echo "ERROR. $i is not found." @@ -21,9 +21,9 @@ do for i in {5..5} do j=$((i - 1)) - if [-f "./python/player$i.py"]; then + if [ -f "./python/player$i.py"]; then nice -0 python3 ./python/player$i.py -I 127.0.0.1 -P 8888 -p $j - elif [-f "./capi$i"]; then + elif [ -f "./capi$i"]; then nice -0 ./capi$i -I 127.0.0.1 -P 8888 -p $j else echo "ERROR. $i is not found." From 5e0d948d628dfa489486428efd1c674d56b635ad Mon Sep 17 00:00:00 2001 From: sendssf <2336526392@qq.com> Date: Sat, 29 Apr 2023 22:13:08 +0800 Subject: [PATCH 2/5] merge --- dependency/shell/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dependency/shell/run.sh b/dependency/shell/run.sh index 5b157af..9c586b8 100644 --- a/dependency/shell/run.sh +++ b/dependency/shell/run.sh @@ -1,6 +1,6 @@ #!/usr/local -nice -10 ./Server/Server --port 8888 --studentCount 4 --trickerCount 1 --gameTimeInSecond 600 --url $URL --token $TOKEN +nice -10 ./Server --port 8888 --studentCount 4 --trickerCount 1 --gameTimeInSecond 600 --url $URL --token $TOKEN sleep 5 for k in {1..2} do 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 3/5] 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(); From f02707c9a360408474c2650c4ebf7ff78d115bc5 Mon Sep 17 00:00:00 2001 From: sendssf <2336526392@qq.com> Date: Sat, 6 May 2023 21:48:31 +0800 Subject: [PATCH 4/5] Chore: Add launch function and multiple thread --- installer/Installer/Model.cs | 414 +++++++++++++++++++++++++++++-- installer/Installer/ViewModel.cs | 33 +-- 2 files changed, 413 insertions(+), 34 deletions(-) diff --git a/installer/Installer/Model.cs b/installer/Installer/Model.cs index d06254d..e1b49eb 100644 --- a/installer/Installer/Model.cs +++ b/installer/Installer/Model.cs @@ -199,7 +199,113 @@ namespace starter.viewmodel.settings { if (Tencent_cos_download.CheckAlreadyDownload()) { - Process.Start(System.IO.Path.Combine(Data.FilePath, startName)); + //Process.Start(System.IO.Path.Combine(Data.FilePath, startName)); + switch (RunProgram.RunInfo.mode) + { + case RunProgram.RunMode.ServerOnly: + RunProgram.StartServer(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + break; + case RunProgram.RunMode.ServerForDebugOnly: + RunProgram.StartServerForDebug(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + break; + case RunProgram.RunMode.GUIAttendGameOnly: + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, + false, RunProgram.RunInfo.occupation, RunProgram.RunInfo.type); + break; + case RunProgram.RunMode.GUIVisit: + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, 0, true, 1, 1); + break; + case RunProgram.RunMode.GUIAndAICpp: + RunProgram.StartServerForDebug(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunCpp(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, + false, RunProgram.RunInfo.occupation, RunProgram.RunInfo.type); + break; + case RunProgram.RunMode.GUIAndAIPython: + RunProgram.StartServerForDebug(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunPython(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, + false, RunProgram.RunInfo.occupation, RunProgram.RunInfo.type); + break; + case RunProgram.RunMode.ServerAndCpp: + RunProgram.StartServer(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunCpp(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + break; + case RunProgram.RunMode.ServerAndPython: + RunProgram.StartServer(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunPython(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + break; + case RunProgram.RunMode.ServerAndCppVisit: + RunProgram.StartServer(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunCpp(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, true, 0, 1); + break; + case RunProgram.RunMode.ServerAndPythonVisit: + RunProgram.StartServer(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunPython(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, true, 0, 1); + break; + case RunProgram.RunMode.ServerDebugAndCppVisit: + RunProgram.StartServerForDebug(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunCpp(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, true, 0, 1); + break; + case RunProgram.RunMode.ServerDebugAndPythonVisit: + RunProgram.StartServerForDebug(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.gameTimeSec, RunProgram.RunInfo.playbackFileName); + Task.Delay(100); + RunProgram.RunPython(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.studentCount, + RunProgram.RunInfo.trickerCount, RunProgram.RunInfo.saveDebugLog, RunProgram.RunInfo.showDebugLog, + RunProgram.RunInfo.warningOnly, RunProgram.RunInfo.playerId, RunProgram.RunInfo.filePath); + RunProgram.RunInfo.playerId = null; + RunProgram.RunInfo.filePath = null; + RunProgram.RunGUIClient(RunProgram.RunInfo.IP, RunProgram.RunInfo.port, RunProgram.RunInfo.characterID, true, 0, 1); + break; + } return true; } else @@ -773,7 +879,7 @@ namespace Downloader // 读取 Interlocked.CompareExchange(ref newFile, 0, 0); int upcnt = updateFileName.Count; - if(upcnt <= 20) + if (upcnt <= 20) { while (newFileName.TryDequeue(out var filename)) { @@ -1449,24 +1555,292 @@ namespace Downloader 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; + public enum RunMode + { + ServerOnly, //只启动Server + ServerForDebugOnly, //只启动ServerForDebug + GUIAttendGameOnly, //只用GUIClient参与游戏 + GUIVisit, //只用GUIClient观战 + GUIAndAICpp, //用GUI参与游戏并且让cpp的AI同时参与 + GUIAndAIPython, //用GUI参与游戏并且让python的AI同时参与 + ServerAndCpp, //只运行Server和cpp, + ServerAndPython, //只运行Server和python + ServerAndCppVisit, //运行Server和Cpp并用GUI观战 + ServerAndPythonVisit, //运行Server和python并用GUI观战 + ServerDebugAndCpp, //运行ServerForDebug... + ServerDebugAndPython, + ServerDebugAndCppVisit, + ServerDebugAndPythonVisit, + } + + public class RunInfo //UI需要在调用Launch函数前保证其中数据已经更新并有效 + { + static public RunMode mode; + static public string? IP; + static public int port; + static public int studentCount; + static public int trickerCount; + static public int gameTimeSec; + static public string? playbackFileName; + static public int characterID; + static public int type; + static public bool saveDebugLog; + static public bool showDebugLog; + static public bool warningOnly; + static public bool visiting; + static public int occupation; + static public List? playerId = new List(); //两者长度必须与studentCount + trickerCount一致 + static public List? filePath = new List(); + } + + /// + /// 运行cmd命令 + /// 会显示命令窗口 + /// + /// 指定应用程序的完整路径 + /// 执行命令行参数 + static bool RunCmd(string cmdExe, string cmdStr) + { + bool result = false; + try + { + using (Process myPro = new Process()) + { + //指定启动进程是调用的应用程序和命令行参数 + ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr); + myPro.StartInfo = psi; + myPro.Start(); + //myPro.WaitForExitAsync(); + result = true; + } + } + catch + { + + } + return result; + } + + /// + /// 启动Server + /// 会显示命令窗口 + /// + /// 指定Server运行的IPV4地址 + /// 指定Server运行的端口 + /// 指定学生人数 + /// 指定捣蛋鬼人数 + /// 指定游戏最大时长 + /// 指定回放文件名称 + public static int StartServer(string? IP, int port, int studentCount, int trickerCount, int gameTimeSec, string? playbackFileName) + { + if (System.Diagnostics.Process.GetProcessesByName("Server").ToList().Count > 0) + { + System.Diagnostics.Process.GetProcessesByName("Server")[0].Kill(); + } + string cmdExe = $"{Data.FilePath}\\THUAI6\\win\\win64\\Server.exe"; + string cmdPara = $"--ip {IP} --port {port} --studentCount {studentCount} --trickerCount {trickerCount}" + + $" --gameTimeInSecond {gameTimeSec} --fileName {playbackFileName}"; + RunCmd(cmdExe, cmdPara); + return 0; + } + + /// + /// 启动Debug用的Server + /// 会显示命令窗口 + /// + /// 指定Server运行的IPV4地址 + /// 指定Server运行的端口 + /// 指定学生人数 + /// 指定捣蛋鬼人数 + /// 指定游戏最大时长 + /// 指定回放文件名称 + public static int StartServerForDebug(string? IP, int port, int studentCount, int trickerCount, int gameTimeSec, string? playbackFileName) + { + if (System.Diagnostics.Process.GetProcessesByName("Server").ToList().Count > 0) + { + System.Diagnostics.Process.GetProcessesByName("Server")[0].Kill(); + } + string cmdExe = $"{Data.FilePath}\\THUAI6\\win\\win64\\Debug\\Server.exe"; + string cmdPara = $"--ip {IP} --port {port} --studentCount {studentCount} --trickerCount {trickerCount} " + + $"--gameTimeInSecond {gameTimeSec} --fileName {playbackFileName}"; + RunCmd(cmdExe, cmdPara); + return 0; + } + + /// + /// 启动cpp,在这之前要先启动Server,可能还需要一些延时 + /// 会显示命令窗口 + /// + /// 指定Server运行的IPV4地址 + /// 指定Server运行的端口 + /// 指定学生人数 + /// 指定捣蛋鬼人数 + /// 是否保存Debug日志文件 + /// 是否将日志输出到屏幕上 + /// 在showDebugLog == true时,是否只输出警告或者报错(不影响日志保存) + /// 默认为空,如果不为空,playerIp将会按照顺序代替默认从0~3的IP + public static int RunCpp(string? IP, int port, int studentCount, int trickerCount, bool saveDebugLog, + bool showDebugLog, bool warningOnly, List? playerIp = null, List? filePath = null) + { + string cmdExe; + string cmdBase = $"-I {IP} -P {port} "; + for (int i = 0; i < studentCount; i++) + { + string cmdPara; + if (playerIp == null) + { + cmdPara = cmdBase + $" -p {i}"; + } + else + { + cmdPara = cmdBase + $" -p {playerIp[i]}"; + } + if (saveDebugLog) + { + cmdPara = cmdPara + " -d"; + } + if (showDebugLog) + { + cmdPara = cmdPara + " -o"; + } + if (warningOnly) + { + cmdPara = cmdPara + " -w"; + } + if (filePath == null) + { + cmdExe = $"{Data.FilePath}\\THUAI6\\win\\CAPI\\cpp\\x64\\Debug\\API.exe"; + } + else + { + cmdExe = filePath[i]; + } + RunCmd(cmdExe, cmdPara); + } + + if (trickerCount != 0) + { + string cmdPara; + cmdPara = cmdBase + " -p 4"; + if (saveDebugLog) + { + cmdPara = cmdPara + " -d"; + } + if (showDebugLog) + { + cmdPara = cmdPara + " -o"; + } + if (warningOnly) + { + cmdPara = cmdPara + " -w"; + } + if (filePath == null) + { + cmdExe = $"{Data.FilePath}\\THUAI6\\win\\CAPI\\cpp\\x64\\Debug\\API.exe"; + } + else + { + cmdExe = filePath[studentCount]; + } + RunCmd(cmdExe, cmdPara); + } + return 0; + } + + /// + /// 启动客户端 + /// 会显示命令窗口 + /// + /// 指定Server运行的IPV4地址 + /// 指定Server运行的端口 + /// 进入游戏时的角色ID 0~4,0~3为学生,4为捣蛋鬼 + /// 是否观战,当值为true时,characterID无效 + /// 指定参加游戏时的角色职业,详情见说明文档 + /// 角色类别,type = 1 表示学生,type = 2 表示捣蛋鬼 + public static int RunGUIClient(string? IP, int port, int characterID, bool visiting, int occupation = 1, int type = 1) + { + string cmdExe = $"{Data.FilePath}\\THUAI6\\win\\win64\\Client.exe"; + string cmdBase = $"--port {port} --ip {IP} --type {type} --occupation {occupation}"; + if (visiting) + { + cmdBase = cmdBase + " --characterID 200000"; + } + else + { + cmdBase = cmdBase + $" --characterID {characterID}"; + } + cmdBase = cmdBase + " --cl"; + RunCmd(cmdExe, cmdBase); + return 0; + } + + //启动python,参数同RunCpp + public static int RunPython(string? IP, int port, int studentCount, int trickerCount, bool saveDebugLog, + bool showDebugLog, bool warningOnly, List? playerIp = null, List? filePath = null) + { + string cmdExe = $"python"; + string cmdBase; + for (int i = 0; i < studentCount; i++) + { + string cmdPara; + if (filePath == null) + { + cmdBase = $" {Data.FilePath}\\THUAI6\\win\\CAPI\\python\\PyAPI\\main.py -I {IP} -P {port} "; + } + else + { + cmdBase = " " + filePath[i] + $" -I {IP} -P {port} "; + } + if (playerIp == null) + { + cmdPara = cmdBase + $" -p {i}"; + } + else + { + cmdPara = cmdBase + $" -p {playerIp[i]}"; + } + if (saveDebugLog) + { + cmdPara = cmdPara + " -d"; + } + if (showDebugLog) + { + cmdPara = cmdPara + " -o"; + } + if (warningOnly) + { + cmdPara = cmdPara + " -w"; + } + RunCmd(cmdExe, cmdPara); + } + + if (trickerCount != 0) + { + string cmdPara; + if (filePath == null) + { + cmdBase = $" {Data.FilePath}\\THUAI6\\win\\CAPI\\python\\PyAPI\\main.py -I {IP} -P {port} "; + } + else + { + cmdBase = " " + filePath[studentCount] + $" -I {IP} -P {port} "; + } + cmdPara = cmdBase + " -p 4"; + if (saveDebugLog) + { + cmdPara = cmdPara + " -d"; + } + if (showDebugLog) + { + cmdPara = cmdPara + " -o"; + } + if (warningOnly) + { + cmdPara = cmdPara + " -w"; + } + RunCmd(cmdExe, cmdPara); + } + return 0; } } } diff --git a/installer/Installer/ViewModel.cs b/installer/Installer/ViewModel.cs index 2cdc968..ed25391 100644 --- a/installer/Installer/ViewModel.cs +++ b/installer/Installer/ViewModel.cs @@ -38,20 +38,20 @@ namespace starter.viewmodel.settings Status = SettingsModel.Status.working; 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; - // } - //} + 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; + } + } //实例化BackgroundWorker asyncDownloader = new BackgroundWorker(); @@ -85,6 +85,11 @@ namespace starter.viewmodel.settings //若有更新,将启动键改为更新键; //相应地,使用login界面启动; //结构:上方为登录框架,下方有“修改选手包”按钮 + + //下面几行是用来运行测试的代码 + //Program.RunProgram.StartServerForDebug("0.0.0.0",8888,4,1,600,"video"); + //Program.RunProgram.RunCpp("127.0.0.1",8888,4,1,false,true,false); + //Program.RunProgram.RunGUIClient("127.0.0.1", 8888, 0, true); } else { From 04c96f10dbd75c32dba4079771b03d29f18075d2 Mon Sep 17 00:00:00 2001 From: sendssf <2336526392@qq.com> Date: Sat, 6 May 2023 21:50:42 +0800 Subject: [PATCH 5/5] Chore: Add launch function and multiple thread --- installer/Installer/Model.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/installer/Installer/Model.cs b/installer/Installer/Model.cs index e1b49eb..7e70113 100644 --- a/installer/Installer/Model.cs +++ b/installer/Installer/Model.cs @@ -610,8 +610,8 @@ namespace Downloader .Build(); // 创建 CosXmlConfig 对象 // 永久密钥访问凭证 - string secretId = "AKIDvhEVXN4cv0ugIlFYiniV6Wk1McfkplYA"; //"云 API 密钥 SecretId"; - string secretKey = "YyGLGCJG4f5VsEUddnz9JSRPSSK8sYBo"; //"云 API 密钥 SecretKey"; + string secretId = "***"; //"云 API 密钥 SecretId"; + string secretKey = "***"; //"云 API 密钥 SecretKey"; long durationSecond = 1000; // 每次请求签名有效时长,单位为秒 QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(