Get basic Lua interpreter set up

This commit is contained in:
Daniel_I_Am 2020-09-03 23:14:08 +02:00
parent 0636205e05
commit ebbf2c858a
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
6 changed files with 25 additions and 8 deletions

View File

@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.6" />
<PackageReference Include="NeoLua" Version="1.3.11" />
<PackageReference Include="NLog" Version="4.7.2" /> <PackageReference Include="NLog" Version="4.7.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.4" /> <PackageReference Include="NLog.Extensions.Logging" Version="1.6.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />

View File

@ -2,6 +2,7 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Models; using ChaosBot.Models;
using ChaosBot.Services.ProgrammingLanguageInterpreter;
using Discord.Commands; using Discord.Commands;
namespace ChaosBot.Discord.Services namespace ChaosBot.Discord.Services
@ -30,6 +31,7 @@ namespace ChaosBot.Discord.Services
} }
else else
{ {
if (!ProgrammingLanguageInterpreterFacade.TryInterpret(customCommand))
throw new NotImplementedException($"No support for command type ${customCommand.Type}"); throw new NotImplementedException($"No support for command type ${customCommand.Type}");
} }

View File

@ -2,6 +2,6 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{ {
public interface IProgrammingLanguageInterpreter public interface IProgrammingLanguageInterpreter
{ {
void Interpret(string content); void Interpret(string content, string command);
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System.Text.RegularExpressions;
using Neo.IronLua;
using NLog; using NLog;
namespace ChaosBot.Services.ProgrammingLanguageInterpreter namespace ChaosBot.Services.ProgrammingLanguageInterpreter
@ -7,9 +8,20 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{ {
private static readonly ILogger Logger = Program.GetLogger(); 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");
}
}
} }
} }
} }

View File

@ -4,12 +4,14 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
{ {
public static class ProgrammingLanguageInterpreterFacade public static class ProgrammingLanguageInterpreterFacade
{ {
public static void Interpret(CustomCommand customCommand) public static bool TryInterpret(CustomCommand customCommand)
{ {
IProgrammingLanguageInterpreter interpreter = IProgrammingLanguageInterpreter interpreter =
ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type); ProgrammingLanguageInterpreterFactory.GetInterpreter(customCommand.Type);
if (interpreter == null) return false;
interpreter.Interpret(customCommand.Content); interpreter.Interpret(customCommand.Content, customCommand.Command);
return true;
} }
} }
} }

View File

@ -11,7 +11,7 @@ namespace ChaosBot.Services.ProgrammingLanguageInterpreter
case CustomCommandType.CustomLua: case CustomCommandType.CustomLua:
return new LuaProgrammingLanguageInterpreter(); return new LuaProgrammingLanguageInterpreter();
default: default:
return new LuaProgrammingLanguageInterpreter(); return null;
} }
} }
} }