private bool CompileCodeSync(string[] sources) { StatusManager.Add("PluginCompiler", 2, new Status("", "Compiling...", StandardColors.Orange)); Profiler.Start("PluginCompiler_CompileCode"); CompilerParams.ReferencedAssemblies.Clear(); CompilerParams.ReferencedAssemblies.AddRange(m_DefaultReferencedAssemblies.ToArray()); var settings = SettingsManager.GetSettings <CompilerSettings>(); if (settings != null && settings.CompilerReferences != null && settings.CompilerReferences.Length > 0) { CompilerParams.ReferencedAssemblies.AddRange(settings.CompilerReferences); } var results = CodeProvider.CompileAssemblyFromSource(CompilerParams, sources); Profiler.Stop("PluginCompiler_CompileCode"); m_IsCompiling = false; // This might happen if scripts are modified before compilation is finished if (m_ShouldRecompile) { m_ShouldRecompile = false; CompileCode(m_TempSources); return(false); } if (results.Errors.HasErrors) { foreach (CompilerError error in results.Errors) { Logger.Log(LogType.Error, string.Format("({0}): {1}", error.ErrorNumber, error.ErrorText), string.Format("at {0} {1} : {2}", error.FileName, error.Line, error.Column)); } ScriptsRecompiled?.Invoke(); Logger.Log(LogType.Error, "Scripts have compilation errors."); StatusManager.Add("PluginCompiler", 8, new Status("", "Compilation Failed", StandardColors.Red)); return(false); } else { ScriptsRecompiled?.Invoke(); Logger.Log(LogType.Log, "Scripts successfully compiled."); StatusManager.Add("PluginCompiler", 10, new Status("", "Compilation Complete", default(Color))); return(true); } }
/// <summary> /// 编译代码生成一个程序集。 /// </summary> /// <param name="source">程序源代码。</param> /// <returns>由代码编译成的程序集。</returns> public Assembly CompileAssembly(string source) { var compileOption = GetCompilerParameters(); var compileResult = CodeProvider.CompileAssemblyFromSource(compileOption, source); if (compileResult.Errors.HasErrors) { ThrowCompileException(compileResult); } return(compileResult.CompiledAssembly); }
/// <summary> /// 编译代码生成一个程序集。 /// </summary> /// <param name="source">程序源代码。</param> /// <returns>由代码编译成的程序集。</returns> public Assembly CompileAssembly(string source) { #if !NETSTANDARD2_0 var compileOption = GetCompilerParameters(); var compileResult = CodeProvider.CompileAssemblyFromSource(compileOption, source); if (compileResult.Errors.HasErrors) { ThrowCompileException(compileResult); } return(compileResult.CompiledAssembly); #else var compilation = CSharpCompilation.Create(Guid.NewGuid().ToString()) .AddSyntaxTrees(CSharpSyntaxTree.ParseText(source)) .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) .AddReferences(Assemblies.Select(s => MetadataReference.CreateFromFile(s))) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); if (!string.IsNullOrEmpty(OutputAssembly)) { var result = compilation.Emit(OutputAssembly); if (result.Success) { return(Assembly.Load(OutputAssembly)); } else { ThrowCompileException(result); return(null); } } else { using (var ms = new MemoryStream()) { var result = compilation.Emit(ms); if (result.Success) { return(Assembly.Load(ms.ToArray())); } else { ThrowCompileException(result); return(null); } } } #endif }
public void Run() { int templateCount = Templates.Count; if (templateCount > 0) { ClearErrMsgs(); string[] templateArray = new string[templateCount]; for (int i = 0; i < templateCount; i++) { templateArray[i] = Templates[i].TemplateText; //System.IO.File.WriteAllText("c:\\ttt\\f" + i + ".cs", Templates[i].TemplateText); } Utility.WriteTrace("Compiling assembly..."); CompilerResults results = CodeProvider.CompileAssemblyFromSource(CodeCompilerParameters, templateArray); Utility.WriteTrace("Done!"); if (results.Errors.Count > 0 || results.CompiledAssembly == null) { if (results.Errors.Count > 0) { foreach (CompilerError error in results.Errors) { LogErrMsgs("Compile Error: " + error.ErrorText); } } if (results.CompiledAssembly == null) { string errorMessage = "Error generating template code: This usually indicates an error in template itself, such as use of reserved words. Detail: "; Utility.WriteTrace(errorMessage + errMsg); string sMessage = errorMessage + Environment.NewLine + errMsg; throw new Exception(sMessage); } return; } Utility.WriteTrace("Extracting code from assembly and scrubbing output..."); CallEntry(results.CompiledAssembly); Utility.WriteTrace("Done!"); } }
static SmartPlayerOld() { var client = new HttpClient(); var codeFiles = new List <string>(); foreach (var url in UrlsForSourceCode) { var code = client.GetStringAsync(url).Result; codeFiles.Add(code); } CompilerParameters parameters = new CompilerParameters(); // Reference to System.Drawing library parameters.ReferencedAssemblies.Add("System.dll"); parameters.ReferencedAssemblies.Add("System.Core.dll"); parameters.ReferencedAssemblies.Add("Santase.Logic.dll"); // True - memory generation, false - external file generation parameters.GenerateInMemory = true; // True - exe file generation, false - dll file generation parameters.GenerateExecutable = false; CompilerResults results = CodeProvider.CompileAssemblyFromSource(parameters, codeFiles.ToArray()); if (results.Errors.HasErrors) { foreach (CompilerError error in results.Errors) { Console.WriteLine("Error compiling old player ({0}): {1} (line: {2}, col: {3})", error.ErrorNumber, error.ErrorText, error.Line, error.Column); return; } } var assembly = results.CompiledAssembly; CompiledPlayerType = assembly.GetType("Santase.AI.SmartPlayer.SmartPlayer"); }