Inject the csi directly into my master.

This commit is contained in:
Pieter-Jan Briers
2021-02-23 01:39:33 +01:00
parent 6bd5814f4a
commit f0366531ef
3 changed files with 188 additions and 41 deletions

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;
using Lidgren.Network;
using Microsoft.CodeAnalysis;
@@ -23,6 +25,7 @@ namespace Robust.Shared.Scripting
new(kind: SourceCodeKind.Script, languageVersion: LanguageVersion.Latest);
private static readonly Func<Script, bool> _hasReturnValue;
private static readonly Func<Diagnostic, IReadOnlyList<object?>?> _getDiagnosticArguments;
private static readonly string[] _defaultImports =
{
@@ -47,10 +50,23 @@ namespace Robust.Shared.Scripting
// Fallback path in case they remove that.
// The method literally has a // TODO: remove
_hasReturnValue = _ => true;
return;
}
else
{
_hasReturnValue = (Func<Script, bool>) Delegate.CreateDelegate(typeof(Func<Script, bool>), method);
}
_hasReturnValue = (Func<Script, bool>) Delegate.CreateDelegate(typeof(Func<Script, bool>), method);
// Also internal and we need it.
var prop = typeof(Diagnostic).GetProperty("Arguments", BindingFlags.Instance | BindingFlags.NonPublic);
if (prop == null)
{
_getDiagnosticArguments = _ => null;
}
else
{
var moment = prop.GetMethod!;
_getDiagnosticArguments = moment.CreateDelegate<Func<Diagnostic, IReadOnlyList<object?>?>>();
}
// 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.
@@ -79,6 +95,11 @@ namespace Robust.Shared.Scripting
return _hasReturnValue(script);
}
public static IReadOnlyList<object?>? GetDiagnosticArgs(Diagnostic diag)
{
return _getDiagnosticArguments(diag);
}
public static void AddWithSyntaxHighlighting(Script script, FormattedMessage msg, string code,
Workspace workspace)
{
@@ -139,6 +160,60 @@ namespace Robust.Shared.Scripting
return list;
}
private static IEnumerable<Assembly> GetAutoImportAssemblies(IReflectionManager refl)
{
return GetDefaultReferences(refl).Union(
AssemblyLoadContext.Default.Assemblies.Where(c => c.GetName().Name!.StartsWith("System."))
);
}
public static bool CalcAutoImports(
IReflectionManager refl,
IEnumerable<Diagnostic> diags,
[NotNullWhen(true)] out HashSet<string>? found)
{
var missing = new List<string>();
foreach (var diag in diags.Where(c => c.Id == "CS0103" || c.Id == "CS0246"))
{
var args = GetDiagnosticArgs(diag);
if (args == null)
{
found = null;
return false;
}
missing.Add((string) args[0]!);
}
if (missing.Count == 0)
{
found = null;
return false;
}
found = new HashSet<string>();
var assemblies = ScriptInstanceShared.GetAutoImportAssemblies(refl).ToArray();
foreach (var m in missing)
{
foreach (var assembly in assemblies)
{
foreach (var type in assembly.DefinedTypes)
{
if (type.IsPublic && type.Name == m)
{
found.Add(type.Namespace!);
goto nextMissing;
}
}
}
nextMissing: ;
}
return true;
}
public static ScriptOptions GetScriptOptions(IReflectionManager reflectionManager)
{
return ScriptOptions.Default