From 514a83523393a0be8ad312ca1139e93f5e29c59b Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 26 Sep 2020 18:27:24 +0200 Subject: [PATCH] Store my changes --- ChaosBot/Discord/Services/CommandHandler.cs | 2 +- .../IProgrammingLanguageInterpreter.cs | 5 +++- .../LuaProgrammingLanguageInterpreter.cs | 29 ++++++++++++------- .../ProgrammingLanguageInterpreterFacade.cs | 22 +++++++++++++- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index 4ed20bd..1c370ea 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -192,7 +192,7 @@ namespace ChaosBot.Discord.Services { if (!command.IsSpecified) { - LoggingFacade.Error($"Command execution failed [{context.User.Username}#{context.User.Discriminator} -> {command.Value.Name}]"); + LoggingFacade.Error($"Command execution failed [{context.User.Username}#{context.User.Discriminator} -> {context.Message.Content.Split(" ").First()}]"); return; } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs index a9828fd..f43d14c 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs @@ -1,7 +1,10 @@ +using Discord.Commands; + namespace ChaosBot.Services.ProgrammingLanguageInterpreter { public interface IProgrammingLanguageInterpreter { - string Interpret(string content, string command); + string Interpret(SocketCommandContext context, string content, string command); + void StopExecution(); } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs index 8da885d..c87c056 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs @@ -1,6 +1,8 @@ using System; +using System.Linq; using System.Text; using System.Text.RegularExpressions; +using Discord.Commands; using Neo.IronLua; using NLog; @@ -10,37 +12,42 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter { private static readonly ILogger Logger = Program.GetLogger(); private readonly StringBuilder _outputBuilder = new StringBuilder(); + private Lua _lua; - public string Interpret(string content, string command) + public string Interpret(SocketCommandContext context, string content, string command) { - using (Lua lua = new Lua()) + using (_lua = new Lua()) { // This needs to be dynamic if we want to call // functions from within the lua environment // This is a runtime type check - dynamic env = lua.CreateEnvironment(); + dynamic env = _lua.CreateEnvironment(); ParamsDelegate printDel = Print; env.print = printDel; - env.dochunk(content, $"{command}.lua"); + env.context = context; + + string code = content; + + env.dochunk(code, $"{command}.lua"); return _outputBuilder.ToString(); } } + public void StopExecution() + { + _lua.Dispose(); + } + private delegate void ParamsDelegate(params object[] parameters); private void Print(params object[] parameters) { - StringBuilder sb = new StringBuilder(); + string str = string.Join(" ", from param in parameters select param == null ? string.Empty : param.ToString()); - foreach (object parameter in parameters) - { - sb.Append(parameter); - } - - _outputBuilder.Append(sb); + _outputBuilder.Append(str); } } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs index e48d28a..94ede93 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs @@ -1,4 +1,6 @@ using System; +using System.Threading; +using System.Threading.Tasks; using ChaosBot.Models; using Discord.Commands; @@ -19,7 +21,25 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter try { - string output = interpreter.Interpret(customCommand.Content, customCommand.Command); + Thread taskThread = null; + Task task = Task.Run(() => + { + taskThread = Thread.CurrentThread; + Thread.CurrentThread.Priority = ThreadPriority.Lowest; + return interpreter.Interpret(context, customCommand.Content, customCommand.Command); + }); + + const int timeout = 1000; + bool isTaskCompleted = task.Wait(TimeSpan.FromMilliseconds(timeout)); + + if (!isTaskCompleted) + { + interpreter.StopExecution(); + taskThread.Abort(); + throw new TimeoutException($"Command '{customCommand.Command}' took more than {timeout}ms to run. It has been killed."); + } + + string output = task.Result; if (output.Length > 0) context.Channel.SendMessageAsync(output);