2020-10-25 20:57:34 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Mono.CSharp;
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
// Thanks to ManlyMarco for most of this
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
namespace UnityExplorer.Core.CSharp
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-10-27 00:54:08 +11:00
|
|
|
|
public class ScriptEvaluator : Evaluator, IDisposable
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
|
|
|
|
private static readonly HashSet<string> StdLib = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
|
|
|
|
|
{
|
|
|
|
|
"mscorlib", "System.Core", "System", "System.Xml"
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-25 18:39:35 +11:00
|
|
|
|
internal static TextWriter _textWriter;
|
|
|
|
|
internal static StreamReportPrinter _reportPrinter;
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
public ScriptEvaluator(TextWriter tw) : base(BuildContext(tw))
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2021-03-25 18:39:35 +11:00
|
|
|
|
_textWriter = tw;
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
|
|
|
|
ImportAppdomainAssemblies(ReferenceAssembly);
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;
|
2021-03-25 18:39:35 +11:00
|
|
|
|
_textWriter.Dispose();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
string name = args.LoadedAssembly.GetName().Name;
|
2021-03-25 18:39:35 +11:00
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
if (StdLib.Contains(name))
|
|
|
|
|
return;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
ReferenceAssembly(args.LoadedAssembly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CompilerContext BuildContext(TextWriter tw)
|
|
|
|
|
{
|
2021-03-25 18:39:35 +11:00
|
|
|
|
_reportPrinter = new StreamReportPrinter(tw);
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
|
|
|
|
var settings = new CompilerSettings
|
|
|
|
|
{
|
|
|
|
|
Version = LanguageVersion.Experimental,
|
|
|
|
|
GenerateDebugInfo = false,
|
|
|
|
|
StdLib = true,
|
|
|
|
|
Target = Target.Library,
|
|
|
|
|
WarningLevel = 0,
|
|
|
|
|
EnhancedWarnings = false
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-25 18:39:35 +11:00
|
|
|
|
return new CompilerContext(settings, _reportPrinter);
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ImportAppdomainAssemblies(Action<Assembly> import)
|
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
|
|
|
|
string name = assembly.GetName().Name;
|
|
|
|
|
if (StdLib.Contains(name))
|
|
|
|
|
continue;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
import(assembly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|