mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Server scripting.
This commit is contained in:
4
Robust.Shared.Scripting/Properties/AssemblyInfo.cs
Normal file
4
Robust.Shared.Scripting/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Robust.Server")]
|
||||
[assembly: InternalsVisibleTo("Robust.Client")]
|
||||
27
Robust.Shared.Scripting/Robust.Shared.Scripting.csproj
Normal file
27
Robust.Shared.Scripting/Robust.Shared.Scripting.csproj
Normal file
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\MSBuild\Robust.Properties.targets" />
|
||||
<PropertyGroup>
|
||||
<!-- Work around https://github.com/dotnet/project-system/issues/4314 -->
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
<LangVersion>8</LangVersion>
|
||||
<IsPackable>false</IsPackable>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<OutputPath>../bin/Shared.Maths</OutputPath>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
<Platforms>x64</Platforms>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<Import Project="..\MSBuild\Robust.DefineConstants.targets" />
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="3.5.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.5.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Robust.Shared.Maths\Robust.Shared.Maths.csproj" />
|
||||
<ProjectReference Include="..\Robust.Shared\Robust.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
148
Robust.Shared.Scripting/ScriptInstanceShared.cs
Normal file
148
Robust.Shared.Scripting/ScriptInstanceShared.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Lidgren.Network;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Classification;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using Robust.Shared.Interfaces.Reflection;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Utility;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
|
||||
namespace Robust.Shared.Scripting
|
||||
{
|
||||
internal static class ScriptInstanceShared
|
||||
{
|
||||
public static CSharpParseOptions ParseOptions { get; } =
|
||||
new CSharpParseOptions(kind: SourceCodeKind.Script, languageVersion: LanguageVersion.Latest);
|
||||
|
||||
private static readonly Func<Script, bool> _hasReturnValue;
|
||||
|
||||
private static readonly string[] _defaultImports =
|
||||
{
|
||||
"System",
|
||||
"System.Linq",
|
||||
"System.Collections.Generic",
|
||||
"Robust.Shared.IoC",
|
||||
"Robust.Shared.Maths",
|
||||
"Robust.Shared.GameObjects",
|
||||
"Robust.Shared.Interfaces.GameObjects"
|
||||
};
|
||||
|
||||
static ScriptInstanceShared()
|
||||
{
|
||||
// This is the (internal) method that csi seems to use.
|
||||
// Because it is internal and I can't find an alternative, reflection it is.
|
||||
// TODO: Find a way that doesn't need me to reflect into Roslyn internals.
|
||||
var method = typeof(Script).GetMethod("HasReturnValue", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if (method == null)
|
||||
{
|
||||
// Fallback path in case they remove that.
|
||||
// The method literally has a // TODO: remove
|
||||
_hasReturnValue = _ => true;
|
||||
return;
|
||||
}
|
||||
|
||||
_hasReturnValue = (Func<Script, bool>) Delegate.CreateDelegate(typeof(Func<Script, bool>), method);
|
||||
|
||||
// Run this async so that Roslyn can "warm up" in another thread while you're typing in your first line,
|
||||
// so the hang when you hit enter is less bad.
|
||||
Task.Run(async () =>
|
||||
{
|
||||
const string code =
|
||||
"var x = 5 + 5; var y = (object) \"foobar\"; void Foo(object a) { } Foo(y); Foo(x)";
|
||||
|
||||
var script = await CSharpScript.RunAsync(code);
|
||||
var msg = new FormattedMessage();
|
||||
// Even run the syntax highlighter!
|
||||
AddWithSyntaxHighlighting(script.Script, msg, code, new AdhocWorkspace());
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does nothing, but will invoke the static constructor so Roslyn can warm up.
|
||||
/// </summary>
|
||||
public static void InitDummy()
|
||||
{
|
||||
// Nada.
|
||||
}
|
||||
|
||||
public static bool HasReturnValue(Script script)
|
||||
{
|
||||
return _hasReturnValue(script);
|
||||
}
|
||||
|
||||
public static void AddWithSyntaxHighlighting(Script script, FormattedMessage msg, string code,
|
||||
Workspace workspace)
|
||||
{
|
||||
var compilation = script.GetCompilation();
|
||||
var model = compilation.GetSemanticModel(compilation.SyntaxTrees.First());
|
||||
|
||||
var classified = Classifier.GetClassifiedSpans(model, TextSpan.FromBounds(0, code.Length), workspace);
|
||||
|
||||
var current = 0;
|
||||
foreach (var span in classified)
|
||||
{
|
||||
var start = span.TextSpan.Start;
|
||||
if (start > current)
|
||||
{
|
||||
msg.AddText(code[current..start]);
|
||||
}
|
||||
|
||||
if (current > start)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: there are probably issues with multiple classifications overlapping the same text here.
|
||||
// Too lazy to fix.
|
||||
var src = code[span.TextSpan.Start..span.TextSpan.End];
|
||||
var color = span.ClassificationType switch
|
||||
{
|
||||
ClassificationTypeNames.Comment => Color.FromHex("#57A64A"),
|
||||
ClassificationTypeNames.NumericLiteral => Color.FromHex("#b5cea8"),
|
||||
ClassificationTypeNames.StringLiteral => Color.FromHex("#D69D85"),
|
||||
ClassificationTypeNames.Keyword => Color.FromHex("#569CD6"),
|
||||
ClassificationTypeNames.StaticSymbol => Color.FromHex("#4EC9B0"),
|
||||
ClassificationTypeNames.ClassName => Color.FromHex("#4EC9B0"),
|
||||
ClassificationTypeNames.StructName => Color.FromHex("#4EC9B0"),
|
||||
ClassificationTypeNames.InterfaceName => Color.FromHex("#B8D7A3"),
|
||||
ClassificationTypeNames.EnumName => Color.FromHex("#B8D7A3"),
|
||||
_ => Color.FromHex("#D4D4D4")
|
||||
};
|
||||
|
||||
msg.PushColor(color);
|
||||
msg.AddText(src);
|
||||
msg.Pop();
|
||||
current = span.TextSpan.End;
|
||||
}
|
||||
|
||||
msg.AddText(code[current..]);
|
||||
}
|
||||
|
||||
private static IEnumerable<Assembly> GetDefaultReferences(IReflectionManager reflectionManager)
|
||||
{
|
||||
var list = new List<Assembly>();
|
||||
|
||||
list.AddRange(reflectionManager.Assemblies);
|
||||
list.Add(typeof(YamlDocument).Assembly); // YamlDotNet
|
||||
list.Add(typeof(NetPeer).Assembly); // Lidgren
|
||||
list.Add(typeof(Vector2).Assembly); // Robust.Shared.Maths
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static ScriptOptions GetScriptOptions(IReflectionManager reflectionManager)
|
||||
{
|
||||
return ScriptOptions.Default
|
||||
.AddImports(_defaultImports)
|
||||
.AddReferences(GetDefaultReferences(reflectionManager));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user