public CompilationException Add(string content) { CompilationException exception = new CompilationException(); var(tree, formartter, errors) = content; exception.Formatter = formartter; exception.Diagnostics.AddRange(errors); if (exception.Diagnostics.Count != 0) { exception.ErrorFlag = ComplieError.Syntax; exception.Message = "语法错误,请仔细检查脚本代码!"; NError log = new NError(); log.Handler(exception.Diagnostics); log.Write(); exception.Log = log.Buffer.ToString(); } else { Trees.Add(tree); } SyntaxExceptions.Add(exception); return(exception); }
/// <summary> /// 获取编译后的程序集 /// </summary> /// <returns></returns> public Assembly GetAssembly() { if (ComplierInfos.Trees.Count == 0) { return(null); } if (Name == default) { Name = Guid.NewGuid().ToString("N"); } var result = StreamComplier(ComplierInfos); Assembly assembly = result.Assembly; if (result.Compilation != null) { if (assembly == default || assembly == null) { Exception.Diagnostics.AddRange(result.Errors); Exception.ErrorFlag = ComplieError.Assembly; Exception.Message = "发生错误,无法生成程序集!"; NError logError = new NError(); logError.Handler(result.Compilation, Exception.Diagnostics); Exception.Log = logError.Buffer.ToString(); if (NError.Enabled) { logError.Write(); } } else { NSucceed logSucceed = new NSucceed(); logSucceed.Handler(result.Compilation); Exception.ErrorFlag = ComplieError.None; Exception.Log = logSucceed.Buffer.ToString(); if (NSucceed.Enabled) { logSucceed.Write(); } } } return(assembly); }
/// <summary> /// 获取编译后的程序集 /// </summary> /// <returns></returns> public Assembly GetAssembly() { if (SyntaxInfos.Trees.Count == 0) { return(null); } if (AssemblyName == default) { AssemblyName = Guid.NewGuid().ToString("N"); } var result = StreamComplier(); Assembly assembly = result.Assembly; if (result.Compilation != null) { if (assembly == default || assembly == null) { bool CS0104SHUT = false; bool CS0234SHUT = false; bool CSO246SHUT = false; var tempCache = SyntaxInfos.TreeCodeMapping; SyntaxOption option = new SyntaxOption(); foreach (var item in result.Errors) { if (item.Id == "CS0104") { CS0104SHUT = true; var tempTree = item.Location.SourceTree; var tempCode = tempCache[tempTree]; var(str1, str2) = CS0104Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage()); var sets = SyntaxInfos.TreeUsingMapping[tempTree]; if (sets.Contains(str2)) { tempCache[tempTree] = tempCode.Replace($"using {str1};", ""); } else if (sets.Contains(str1)) { tempCache[tempTree] = tempCode.Replace($"using {str2};", ""); } else { tempCache[tempTree] = tempCode.Replace($"using {str1};", ""); } } else if (item.Id == "CS0234") { CS0234SHUT = true; var tempResult = CS0234Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage()); UsingDefaultCache.Remove(tempResult); var tempTree = item.Location.SourceTree; var tempCode = tempCache[tempTree]; tempCache[tempTree] = tempCode.Replace($"using {tempResult};", ""); } else if (item.Id == "CS0246") { CSO246SHUT = true; var tempTree = item.Location.SourceTree; var tempCode = tempCache[tempTree]; var formart = item.Descriptor.MessageFormat.ToString(); CS0246Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage()); foreach (var @using in CS0246Helper.GetUsings(formart, tempCode)) { UsingDefaultCache.Remove(@using); tempCache[tempTree] = tempCode.Replace($"using {@using};", ""); } } } if (CS0104SHUT || CS0234SHUT || CSO246SHUT) { ErrorRetryCount += 1; if (ErrorRetryCount < 2) { foreach (var item in tempCache) { option.Add(tempCache[item.Key], SyntaxInfos.TreeUsingMapping[item.Key]); } SyntaxInfos = option; return(GetAssembly()); } } ComplieException.Diagnostics.AddRange(result.Errors); ComplieException.ErrorFlag = ComplieError.Complie; ComplieException.Message = "发生错误,无法生成程序集!"; NError logError = new NError(); logError.Handler(result.Compilation, ComplieException.Diagnostics); ComplieException.Log = logError.Buffer.ToString(); if (NError.Enabled) { logError.Write(); } } else { if (_domain != DomainManagment.Default) { DomainCache.Add(result.Assembly, _domain); } NSucceed logSucceed = new NSucceed(); logSucceed.Handler(result.Compilation); ComplieException.ErrorFlag = ComplieError.None; ComplieException.Log = logSucceed.Buffer.ToString(); if (NSucceed.Enabled) { logSucceed.Write(); } } } return(assembly); }
/// <summary> /// 使用内存流进行脚本编译 /// </summary> /// <param name="sourceContent">脚本内容</param> /// <param name="errorAction">发生错误执行委托</param> /// <returns></returns> public static Assembly StreamComplier(string sourceContent, AssemblyDomain domain, out string formatContent, ref List <Diagnostic> diagnostics) { lock (domain) { var(tree, typeNames, formatter, errors) = GetTreeInfo(sourceContent.Trim()); formatContent = formatter; diagnostics.AddRange(errors); if (diagnostics.Count() != 0) { return(null); } if (domain.ClassMapping.ContainsKey(typeNames[0])) { return(domain.ClassMapping[typeNames[0]]); } //创建语言编译 CSharpCompilation compilation = CSharpCompilation.Create( typeNames[0], options: new CSharpCompilationOptions( outputKind: OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release, allowUnsafe: true), syntaxTrees: new[] { tree }, references: domain.References); //编译并生成程序集 using (MemoryStream stream = new MemoryStream()) { var fileResult = compilation.Emit(stream); if (fileResult.Success) { stream.Position = 0; var result = domain.LoadFromStream(stream); domain.CacheAssembly(result, stream); if (NSucceed.Enabled) { NSucceed logSucceed = new NSucceed(); logSucceed.WrapperCode(formatter); logSucceed.Handler(compilation, result); } return(result); } else { diagnostics.AddRange(fileResult.Diagnostics); if (NError.Enabled) { NError logError = new NError(); logError.WrapperCode(formatter); logError.Handler(compilation, fileResult.Diagnostics); logError.Write(); } } } } return(null); }