From ebbf2c858acf3e220357405c4deffcd1d75694a5 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 3 Sep 2020 23:14:08 +0200 Subject: [PATCH] Get basic Lua interpreter set up --- ChaosBot/ChaosBot.csproj | 1 + .../Discord/Services/CustomCommandHandler.cs | 4 +++- .../IProgrammingLanguageInterpreter.cs | 2 +- .../LuaProgrammingLanguageInterpreter.cs | 18 +++++++++++++++--- .../ProgrammingLanguageInterpreterFacade.cs | 6 ++++-- .../ProgrammingLanguageInterpreterFactory.cs | 2 +- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ChaosBot/ChaosBot.csproj b/ChaosBot/ChaosBot.csproj index c1053c2..c1612dd 100644 --- a/ChaosBot/ChaosBot.csproj +++ b/ChaosBot/ChaosBot.csproj @@ -16,6 +16,7 @@ + diff --git a/ChaosBot/Discord/Services/CustomCommandHandler.cs b/ChaosBot/Discord/Services/CustomCommandHandler.cs index 00b536a..c406ac8 100644 --- a/ChaosBot/Discord/Services/CustomCommandHandler.cs +++ b/ChaosBot/Discord/Services/CustomCommandHandler.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using System.Threading.Tasks; using ChaosBot.Models; +using ChaosBot.Services.ProgrammingLanguageInterpreter; using Discord.Commands; namespace ChaosBot.Discord.Services @@ -30,7 +31,8 @@ namespace ChaosBot.Discord.Services } else { - throw new NotImplementedException($"No support for command type ${customCommand.Type}"); + if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand)) + throw new NotImplementedException($"No support for command type ${customCommand.Type}"); } return true; diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs index 10372de..eca64a5 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/IProgrammingLanguageInterpreter.cs @@ -2,6 +2,6 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter { public interface IProgrammingLanguageInterpreter { - void Interpret(string content); + void Interpret(string content, string command); } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs index 9ea1aba..a7e5363 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/LuaProgrammingLanguageInterpreter.cs @@ -1,4 +1,5 @@ -using System; +using System.Text.RegularExpressions; +using Neo.IronLua; using NLog; namespace ChaosBot.Services.ProgrammingLanguageInterpreter @@ -7,9 +8,20 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter { private static readonly ILogger Logger = Program.GetLogger(); - public void Interpret(string content) + public void Interpret(string content, string command) { - throw new NotImplementedException(); + using (Lua 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(); + + foreach (string line in (new Regex("\n")).Split(content)) + { + env.dochunk(line, $"{command}.lua"); + } + } } } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs index dd88949..3746908 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFacade.cs @@ -4,12 +4,14 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter { public static class ProgrammingLanguageInterpreterFacade { - public static void Interpret(CustomCommand customCommand) + public static bool TryInterpret(CustomCommand customCommand) { IProgrammingLanguageInterpreter interpreter = ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type); + if (interpreter == null) return false; - interpreter.Interpret(customCommand.Content); + interpreter.Interpret(customCommand.Content, customCommand.Command); + return true; } } } diff --git a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFactory.cs b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFactory.cs index de2b345..d27cbc2 100644 --- a/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFactory.cs +++ b/ChaosBot/Services/ProgrammingLanguageInterpreter/ProgrammingLanguageInterpreterFactory.cs @@ -11,7 +11,7 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter case CustomCommandType.CustomLua: return new LuaProgrammingLanguageInterpreter(); default: - return new LuaProgrammingLanguageInterpreter(); + return null; } } }