in #U5b9e#U8df5#U9879#U76ee/2019_MSC_#U9ec4#U91d1#U70b9/#U5fae#U8f6f#U9ec4#U91d1#U70b9#U7a0b#U5e8f#U5de5#U5177/OfflineGame/GameMaster/LocalManager/BotRun/Bot.cs [45:116]
public async Task<BotRunResult> RunAsync(string historyString, int roundNum)
{
return await Task.Run(() =>
{
// 运行Bot
var f = new Process();
if (Path.GetExtension(FilePath).Equals(".py", StringComparison.OrdinalIgnoreCase))
{
f.StartInfo.FileName = "cmd.exe";
f.StartInfo.Arguments = $"/C python \"{FilePath}\"";
}
else if (Path.GetExtension(FilePath).Equals(".jl", StringComparison.OrdinalIgnoreCase))
{
f.StartInfo.FileName = "cmd.exe";
f.StartInfo.Arguments = $"/C julia \"{FilePath}\"";
}
else
{
f.StartInfo.FileName = FilePath;
}
f.StartInfo.WorkingDirectory = Path.GetDirectoryName(FilePath);
f.StartInfo.UseShellExecute = false;
f.StartInfo.RedirectStandardError = true;
f.StartInfo.RedirectStandardInput = true;
f.StartInfo.RedirectStandardOutput = true;
f.EnableRaisingEvents = true;
f.StartInfo.CreateNoWindow = true;
LogRunInfo("start", roundNum);
f.Start();
// 在标准输入上输入历史数据
try
{
f.StandardInput.Write(historyString);
f.StandardInput.Close();
}
catch (Exception)
{
}
if (!f.WaitForExit(TIMEOUT))
{
// Bot执行超时将被杀掉,并认为输出了无效值
try
{
LogRunInfo("kill", roundNum);
f.Kill();
}
catch (Exception)
{
}
return new BotRunResult(this, 0, 0);
}
else
{
// Bot执行结束时,在标准输出上读取制表符分割的两个数
LogRunInfo("exit", roundNum);
var line = f.StandardOutput.ReadLine();
var error = f.StandardError.ReadLine();
if (!string.IsNullOrEmpty(error))
{
LogRunInfo($"[error] {error}", roundNum);
}
LogRunInfo($"输出 {line}", roundNum);
return new BotRunResult(this, ParseDoubleValues(line));
}
}).ConfigureAwait(false);
}