Skip to content

Instantly share code, notes, and snippets.

@WamWooWam
Created February 28, 2018 22:24
Show Gist options
  • Save WamWooWam/a3425d36441b104812908cc0e2536892 to your computer and use it in GitHub Desktop.
Save WamWooWam/a3425d36441b104812908cc0e2536892 to your computer and use it in GitHub Desktop.
oh no
public static void RunEvalProcess(DiscordMessage invoker, string code, string langcode, DiscordEmbedBuilder builder, Action<DiscordEmbedBuilder, JToken> action)
{
Stopwatch watch = Stopwatch.StartNew();
try
{
string evalExePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Tools", "WamBotEval.exe");
if (File.Exists(evalExePath))
{
Process evalProcess = new Process();
evalProcess.StartInfo.FileName = evalExePath;
using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
{
evalProcess.StartInfo.Arguments =
$"{langcode} " +
$"{Convert.ToBase64String(Encoding.UTF8.GetBytes(code))} " +
$"{server.GetClientHandleAsString()} " +
$"\"{Directory.GetCurrentDirectory()}\" " +
$"{invoker.Id} " +
$"{invoker.ChannelId}";
evalProcess.StartInfo.UseShellExecute = false;
try
{
using (StreamReader reader = new StreamReader(server))
using (CancellationTokenSource source = new CancellationTokenSource())
{
evalProcess.Start();
string ret = string.Empty;
try
{
Task.Run(() =>
{
ret = reader.ReadLine();
}, source.Token);
}
catch { }
evalProcess.WaitForExit(10000);
if (!evalProcess.HasExited)
{
source.Cancel();
evalProcess.Kill();
builder.WithEvalError("Timeout");
return;
}
else
{
if (evalProcess.ExitCode == 0)
{
if (!string.IsNullOrEmpty(ret))
{
try
{
JToken obj = JToken.Parse(ret);
action(builder, obj);
}
catch (JsonException)
{
builder.WithEvalError("Failed to parse response");
return;
}
}
else
{
builder.WithEvalError("Process exited before returning");
return;
}
}
else
{
builder.WithEvalError($"Process exit code does not indicate success. Exited with code {evalProcess.ExitCode}");
return;
}
}
}
}
catch (Exception ex)
{
builder.WithEvalError(ex.Message);
}
}
}
else
{
builder.WithEvalError("Eval executable not found.");
}
}
finally
{
builder.AddField("Evaluation Time (ms)", watch.ElapsedMilliseconds.ToString(), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment