Skip to content

Commit

Permalink
Merge pull request #559 from CrosRoad95/feature/luau
Browse files Browse the repository at this point in the history
Add luau transpiler
  • Loading branch information
NanoBob authored Nov 1, 2024
2 parents d0f27ef + d4acc2e commit 671db45
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 7 deletions.
2 changes: 2 additions & 0 deletions SlipeServer.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.IO;
using System.Threading;
using SlipeServer.Example;
using SlipeServer.Scripting.Luau;

namespace SlipeServer.Console;

Expand Down Expand Up @@ -108,6 +109,7 @@ public Program(string[] args)
});
builder.AddLua();
builder.AddLuauTranspiler();
builder.AddPhysics();
builder.AddParachuteResource();
builder.AddLuaControllers();
Expand Down
8 changes: 7 additions & 1 deletion SlipeServer.Console/test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
if(isSlipeServer)then
type Point = { x: number, y: number }

local p: Point = { x = 1, y = 2 }

print("Point with luau:", p.x, p.y);

if(isSlipeServer)then
local object = createObject(321, 5, 5, 5)
setElementPosition(object, 50, 50, 250)
setElementRotation(object, 180, 180, 90)
Expand Down
1 change: 1 addition & 0 deletions SlipeServer.Example/SlipeServer.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ProjectReference Include="..\SlipeServer.ConfigurationProviders\SlipeServer.ConfigurationProviders.csproj" />
<ProjectReference Include="..\SlipeServer.LuaControllers\SlipeServer.LuaControllers.csproj" />
<ProjectReference Include="..\SlipeServer.Lua\SlipeServer.Lua.csproj" />
<ProjectReference Include="..\SlipeServer.Luau\SlipeServer.Luau.csproj" />
<ProjectReference Include="..\SlipeServer.Physics\SlipeServer.Physics.csproj" />
<ProjectReference Include="..\SlipeServer.Server\SlipeServer.Server.csproj" />
<ProjectReference Include="..\SlipeServer.SourceGenerators\SlipeServer.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down
9 changes: 7 additions & 2 deletions SlipeServer.Lua/LuaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ public class LuaService
private readonly MtaServer server;
private readonly ILogger logger;
private readonly RootElement root;
private readonly ScriptTransformationPipeline scriptTransformationPipeline;
private readonly Dictionary<string, Script> scripts = [];
private readonly Dictionary<string, LuaMethod> methods = [];
private readonly LuaTranslator translator = new();

public LuaService(MtaServer server, ILogger logger, RootElement root)
public LuaService(MtaServer server, ILogger logger, RootElement root, ScriptTransformationPipeline scriptTransformationPipeline)
{
this.server = server;
this.logger = logger;
this.root = root;
this.scriptTransformationPipeline = scriptTransformationPipeline;
}

public void LoadDefinitions(object methodSet)
Expand Down Expand Up @@ -132,7 +134,10 @@ public void LoadScript(string identifier, string code)
LoadGlobals(script);
LoadDefinitions(script);

script.DoString(code, codeFriendlyName: identifier);
using var ms = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(code));
var stream = this.scriptTransformationPipeline.Transform(ms, "lua");

script.DoStream(stream, codeFriendlyName: identifier);
}

public void LoadScript(string identifier, string[] codes)
Expand Down
5 changes: 2 additions & 3 deletions SlipeServer.Lua/LuaServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ public static class LuaServiceCollectionExtensions
{
public static void AddLua(this IServiceCollection services)
{
services.AddSingleton<IScriptEventRuntime, ScriptEventRuntime>();
services.AddSingleton<IScriptInputRuntime, ScriptInputRuntime>();
services.AddScripting();
services.AddSingleton<LuaService>();
}

public static void AddLua<T>(this IServiceCollection services) where T : class, IScriptEventRuntime
{
services.AddSingleton<IScriptEventRuntime, T>();
services.AddScripting<T>();
services.AddSingleton<LuaService>();
}
}
42 changes: 42 additions & 0 deletions SlipeServer.Luau/LuauToLuaTransform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using Loretta.CodeAnalysis;
using Loretta.CodeAnalysis.Lua;
using Loretta.CodeAnalysis.Lua.Syntax;
using Loretta.CodeAnalysis.Text;

namespace SlipeServer.Scripting.Luau;

internal sealed class LuauToLuaTransform : IScriptTransform
{
public Stream Transform(Stream data, string lang)
{
if (lang != "lua")
return data;

SourceText sourceText = SourceText.From(data, Encoding.UTF8);

var syntaxTree = LuaSyntaxTree.ParseText(sourceText, options: new LuaParseOptions(LuaSyntaxOptions.Luau), path: "script.lua");

var root = syntaxTree.GetRoot();

var rewriter = new TypeAnnotationRemover();
var strippedRoot = rewriter.Visit(root);

var outData = new MemoryStream();
var writer = new StreamWriter(outData);
strippedRoot.WriteTo(writer);
writer.Flush();
return outData;
}

class TypeAnnotationRemover : LuaSyntaxRewriter
{
public override SyntaxNode? Visit(SyntaxNode? node)
{
if (node is TypeDeclarationStatementSyntax or TypeBindingSyntax)
return null;

return base.Visit(node);
}
}
}
16 changes: 16 additions & 0 deletions SlipeServer.Luau/ServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SlipeServer.Server.ServerBuilders;

namespace SlipeServer.Scripting.Luau;

public static class ServerBuilderExtensions
{
public static ServerBuilder AddLuauTranspiler(this ServerBuilder builder)
{
builder.ConfigureServices(services =>
{
services.AddLuauTranspiler();
});

return builder;
}
}
12 changes: 12 additions & 0 deletions SlipeServer.Luau/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;

namespace SlipeServer.Scripting.Luau;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddLuauTranspiler(this IServiceCollection services)
{
services.AddSingleton<IScriptTransform, LuauToLuaTransform>();
return services;
}
}
19 changes: 19 additions & 0 deletions SlipeServer.Luau/SlipeServer.Luau.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SlipeServer.Scripting\SlipeServer.Scripting.csproj" />
<ProjectReference Include="..\SlipeServer.Server\SlipeServer.Server.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Loretta.CodeAnalysis.Lua" Version="0.2.13-nightly.2" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions SlipeServer.Scripting/ScriptTransformationPipeline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SlipeServer.Scripting;

public interface IScriptTransform
{
Stream Transform(Stream data, string lang);
}

public sealed class ScriptTransformationPipeline
{
private readonly List<IScriptTransform> scriptTransforms;

public ScriptTransformationPipeline(IEnumerable<IScriptTransform> scriptTransforms)
{
this.scriptTransforms = scriptTransforms.ToList();
}

public void Add(IScriptTransform scriptTransform)
{
this.scriptTransforms.Add(scriptTransform);
}

public Stream Transform(Stream data, string lang)
{
var transformedData = data;

foreach (var transformation in this.scriptTransforms)
{
transformedData = transformation.Transform(transformedData, lang);
transformedData.Position = 0;
}

return transformedData;

}
}
20 changes: 20 additions & 0 deletions SlipeServer.Scripting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace SlipeServer.Scripting;

public static class ServiceCollectionExtensions
{
public static void AddScripting(this IServiceCollection services)
{
services.TryAddSingleton<ScriptTransformationPipeline>();
services.AddSingleton<IScriptEventRuntime, ScriptEventRuntime>();
services.AddSingleton<IScriptInputRuntime, ScriptInputRuntime>();
}

public static void AddScripting<T>(this IServiceCollection services) where T : class, IScriptEventRuntime
{
services.TryAddSingleton<ScriptTransformationPipeline>();
services.AddSingleton<IScriptEventRuntime, T>();
}
}
25 changes: 24 additions & 1 deletion SlipeServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlipeServer.Hosting", "Slip
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlipeServer.WebHostBuilderExample", "SlipeServer.WebHostBuilderExample\SlipeServer.WebHostBuilderExample.csproj", "{ED9BDF71-BEDA-4C55-B6FA-360B3A5259E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlipeServer.Example", "SlipeServer.Example\SlipeServer.Example.csproj", "{E11F25F8-8DBD-412C-9504-B42CA8083D96}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlipeServer.Example", "SlipeServer.Example\SlipeServer.Example.csproj", "{E11F25F8-8DBD-412C-9504-B42CA8083D96}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlipeServer.Luau", "SlipeServer.Luau\SlipeServer.Luau.csproj", "{882D841E-E5E8-4876-BB19-05B58C4B3AE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -450,6 +452,26 @@ Global
{E11F25F8-8DBD-412C-9504-B42CA8083D96}.Release|x64.Build.0 = Release|Any CPU
{E11F25F8-8DBD-412C-9504-B42CA8083D96}.Release|x86.ActiveCfg = Release|Any CPU
{E11F25F8-8DBD-412C-9504-B42CA8083D96}.Release|x86.Build.0 = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|ARM32.ActiveCfg = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|ARM32.Build.0 = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|ARM64.Build.0 = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|x64.ActiveCfg = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|x64.Build.0 = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|x86.ActiveCfg = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Debug|x86.Build.0 = Debug|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|Any CPU.Build.0 = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|ARM32.ActiveCfg = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|ARM32.Build.0 = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|ARM64.ActiveCfg = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|ARM64.Build.0 = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|x64.ActiveCfg = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|x64.Build.0 = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|x86.ActiveCfg = Release|Any CPU
{882D841E-E5E8-4876-BB19-05B58C4B3AE8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -479,6 +501,7 @@ Global
{DB94A24C-3B3C-4C12-8B87-55F6503960E7} = {7287CD6F-078B-470F-B508-74A442D98B64}
{ED9BDF71-BEDA-4C55-B6FA-360B3A5259E8} = {9A8750C3-0CC3-411D-A234-7869E2D54ECD}
{E11F25F8-8DBD-412C-9504-B42CA8083D96} = {9A8750C3-0CC3-411D-A234-7869E2D54ECD}
{882D841E-E5E8-4876-BB19-05B58C4B3AE8} = {DEAEE2A1-118A-4ED7-8CC5-7C446ED97AE3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E5DA52B5-6C04-4C6F-B5E3-3A7441C58986}
Expand Down

0 comments on commit 671db45

Please sign in to comment.