| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 | using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Runtime.CompilerServices;using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;using InABox.Core;using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.CSharp.Scripting;using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;using Microsoft.CodeAnalysis.Scripting;using Microsoft.CodeAnalysis.Scripting.Hosting;using RoslynPad.Roslyn;namespace InABox.Scripting;public class ScriptProperty : Dictionary<string, object>{    public ScriptProperty(string name, object? value)    {        Name = name;        Value = value;    }    public string Name { get; set; }    public object? Value { get; set; }}public class CompileException : Exception{    public CompileException() : base("Unable to compile script!") { }}public class ScriptDocument : INotifyPropertyChanged{    private string _result;    private string _text = "";    private bool? compiled;    private object? obj;    private Type? type;    static ScriptDocument()    {        DefaultAssemblies = new FluentList<Assembly>()            .Add(typeof(object).Assembly)            .Add(typeof(Regex).Assembly)            .Add(typeof(List<>).Assembly)            .Add(typeof(Enumerable).Assembly)            .Add(typeof(Bitmap).Assembly)            .Add(typeof(Expression).Assembly)            .Add(typeof(DataTable).Assembly);    }    public ScriptDocument(string text)    {        if (Host == null)            Initialize();        Text = text;        Properties = new List<ScriptProperty>();    }    private static Task<RoslynHost> _hostTask;    private static RoslynHost? _host;    public static RoslynHost Host => _host ?? InitializeHost().Result;    public static FluentList<Assembly> DefaultAssemblies { get; }    public Script<object> Script { get; private set; }    public string Text    {        get => _text;        set => SetProperty(ref _text, value);    }    public DocumentId Id { get; set; }    public string Result    {        get => _result;        private set => SetProperty(ref _result, value);    }    private static MethodInfo HasSubmissionResult { get; } =        typeof(Compilation).GetMethod(nameof(HasSubmissionResult), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)            ?? throw new NullReferenceException();    private static PrintOptions PrintOptions { get; } = new() { MemberDisplayFormat = MemberDisplayFormat.SeparateLines };    public List<ScriptProperty> Properties { get; }    public event PropertyChangedEventHandler? PropertyChanged;    private static IEnumerable<MetadataReference> CompilationReferences;    private static Task<RoslynHost> InitializeHost()    {        _hostTask ??= Task.Run(() =>        {            using var profiler = new Profiler(true, "ScriptDocument");            var assemblies = new HashSet<Assembly>();            var typelist = CoreUtils.TypeList(                AppDomain.CurrentDomain.GetAssemblies(),                x =>                {                    if (x.IsClass && !x.IsGenericType && x.IsSubclassOf(typeof(BaseObject)))                    {                        var module = x.Assembly.Modules.FirstOrDefault();                        if(module != null && !module.FullyQualifiedName.Equals("<Unknown>"))                        {                            assemblies.Add(x.Assembly);                            return true;                        }                    }                    return false;                });            DefaultAssemblies.AddRange(assemblies);            var references = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "refs"), "*.dll")                .Select(x => MetadataReference.CreateFromFile(x)).ToArray();            var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll").Where(                x => !Path.GetFileName(x).ToLower().StartsWith("gsdll")                     && !Path.GetFileName(x).ToLower().StartsWith("pdfium")                     && !Path.GetFileName(x).ToLower().StartsWith("ikvm-native")                     && !Path.GetFileName(x).ToLower().StartsWith("sqlite.interop")                     && !Path.GetFileName(x).ToLower().StartsWith("microsoft.codeanalysis")            );            var hostReferences = RoslynHostReferences.NamespaceDefault.With(                typeNamespaceImports: typelist                //, assemblyReferences: DefaultAssemblies                , assemblyPathReferences: files,                references: references            );            CompilationReferences = RoslynHostReferences.NamespaceDefault.With(                typeNamespaceImports: typelist                , assemblyReferences: DefaultAssemblies                , assemblyPathReferences: files            ).GetReferences();            _host = new RoslynHost(                DefaultAssemblies.ToArray(),                hostReferences            );            return _host;        });        return _hostTask;    }    public static void Initialize()    {        if(_hostTask is null)        {            InitializeHost();        }    }    public bool Compile()    {        Result = null;        compiled = null;        Script = CSharpScript.Create(Text, ScriptOptions.Default            .AddReferences(CompilationReferences)            .AddImports(Host.DefaultImports));        var compilation = Script.GetCompilation();        var hasResult = (bool)HasSubmissionResult.Invoke(compilation, null);        var diagnostics = Script.Compile();        if (diagnostics.Any(t => t.Severity == DiagnosticSeverity.Error))        {            var result = new List<string>();            var errors = diagnostics.Select(FormatObject).Where(x => x.StartsWith("CSDiagnostic("));            foreach (var error in errors)                result.Add(                    error.Split(new[] { Environment.NewLine }, StringSplitOptions.None).First().Replace("CSDiagnostic(", "").Replace(") {", ""));            Result = string.Join(Environment.NewLine, result);            return false;        }        return true;    }    public void SetValue(string name, object value)    {        var prop = Properties.FirstOrDefault(x => x.Name.Equals(name));        if (prop == null)            Properties.Add(new ScriptProperty(name, value));        else            prop.Value = value;    }    public object? GetValue(string name, object? defaultvalue = null)    {        var prop = Properties.FirstOrDefault(x => x.Name.Equals(name));        return prop != null ? prop.Value : defaultvalue;    }    private Type? GetClassType(string className = "Module")    {        if (!compiled.HasValue)        {            compiled = false;            var stream = new MemoryStream();            var emitResult = Script.GetCompilation().Emit(stream);            if (emitResult.Success)            {                var asm = Assembly.Load(stream.ToArray());                type = asm.GetTypes().Where(x => x.Name.Equals(className)).FirstOrDefault();                if (type != null)                {                    obj = Activator.CreateInstance(type);                    compiled = true;                }            }        }        return type;    }    public object? GetObject(string className = "Module")    {        GetClassType(className);        return obj;    }    public MethodInfo? GetMethod(string className = "Module", string methodName = "Execute")    {        var type = GetClassType(className);        if (compiled == true && type != null)        {            return type.GetMethod(methodName);        }        else        {            return null;        }    }    public bool Execute(string classname = "Module", string methodname = "Execute", object?[]? parameters = null, bool defaultResult = false)    {        var result = defaultResult;        var type = GetClassType(classname);        var obj = GetObject(classname);        var method = GetMethod(classname, methodname);        if (compiled == true && type != null && method != null)        {            foreach (var property in Properties)            {                var prop = type.GetProperty(property.Name);                prop?.SetValue(obj, property.Value);            }            if (method.ReturnType == typeof(bool))            {                result = (bool)(method.Invoke(obj, parameters ?? []) ?? false);            }            else            {                method.Invoke(obj, parameters ?? []);                result = true;            }            if (result)            {                foreach (var property in Properties)                {                    var prop = type.GetProperty(property.Name);                    if (prop != null)                        property.Value = prop.GetValue(obj);                }            }        }        return result;    }    private static string FormatException(Exception ex)    {        return CSharpObjectFormatter.Instance.FormatException(ex);    }    private static string FormatObject(object o)    {        return CSharpObjectFormatter.Instance.FormatObject(o, PrintOptions);    }    protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)    {        if (!EqualityComparer<T>.Default.Equals(field, value))        {            field = value;            // ReSharper disable once ExplicitCallerInfoArgument            OnPropertyChanged(propertyName);            return true;        }        return false;    }    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)    {        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));    }    public static bool RunCustomModule(DataModel model, Dictionary<string, object[]> selected, string code)    {        var script = new ScriptDocument(code);        if (!script.Compile())        {            throw new CompileException();        }        script.SetValue("Data", selected);        script.SetValue("Model", model);        script.Execute(methodname: "BeforeLoad");        var tableNames = model.DefaultTableNames.ToList();        script.Execute(methodname: "CheckTables", parameters: new[] { tableNames });        model.LoadModel(tableNames);        return script.Execute();    }}
 |