private string compilerErrorMessages = ""; // Displayed on the GUI private void Compile(string redBoxerClass, string blueBoxerClass) { try { compilerErrorMessages = ""; // clear any previous messages // ********** Create an instance of the C# compiler //CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CSharpCompiler.CodeCompiler codeCompiler = new CSharpCompiler.CodeCompiler(); // ********** add compiler parameters CompilerParameters compilerParams = new CompilerParameters(); compilerParams.CompilerOptions = "/target:library /optimize /warn:0"; compilerParams.GenerateExecutable = false; compilerParams.GenerateInMemory = true; compilerParams.IncludeDebugInformation = true; compilerParams.ReferencedAssemblies.Add("System.dll"); compilerParams.ReferencedAssemblies.Add("System.Core.dll"); compilerParams.ReferencedAssemblies.Add(Assembly.GetCallingAssembly().Location); string redBoxerSource = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, redBoxerClass) + ".cs"); string blueBoxerSource = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, blueBoxerClass) + ".cs"); // ********** actually compile the code ??????? THIS LINE WORKS IN UNITY EDITOR --- BUT NOT IN BUILD ?????????? CompilerResults resultsRedBoxer = codeCompiler.CompileAssemblyFromSource(compilerParams, redBoxerSource); CompilerResults resultsBlueBoxer = codeCompiler.CompileAssemblyFromSource(compilerParams, blueBoxerSource); // ********** Do we have any compiler errors if (resultsRedBoxer.Errors.Count > 0 || resultsBlueBoxer.Errors.Count > 0) { foreach (CompilerError error in resultsRedBoxer.Errors) { compilerErrorMessages = compilerErrorMessages + error.ErrorText + '\n'; } } else { // ********** get a hold of the actual assembly that was generated Assembly generatedRedAssembly = resultsRedBoxer.CompiledAssembly; Assembly generatedBlueAssembly = resultsBlueBoxer.CompiledAssembly; if (generatedRedAssembly != null && generatedBlueAssembly != null) { // get type of class Calculator from just loaded assembly Type redClassType = generatedRedAssembly.GetType(redBoxerClass); Type blueClassType = generatedBlueAssembly.GetType(blueBoxerClass); RedStrategy = (AbstractBoxingStrategy)Activator.CreateInstance(redClassType); BlueStrategy = (AbstractBoxingStrategy)Activator.CreateInstance(blueClassType); // Say success! compilerErrorMessages = "Success!"; } } } catch (Exception o) { Debug.LogError("" + o.Message + "\n" + o.Source + "\n" + o.StackTrace + "\n"); throw o; } Debug.Log(compilerErrorMessages); }
public static Assembly Compile(string source) { var options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (!assembly.IsDynamic) { options.ReferencedAssemblies.Add(assembly.Location); } } var compiler = new CSharpCompiler.CodeCompiler(); var result = compiler.CompileAssemblyFromSource(options, source); foreach (var err in result.Errors) { Debug.Log(err); } return(result.CompiledAssembly); }
public Assembly Compile(string source) { var result = provider.CompileAssemblyFromSource(param, source); if (result.Errors.Count > 0) { var msg = new StringBuilder(); foreach (CompilerError error in result.Errors) { msg.AppendFormat("Error ({0}): {1}\n", error.ErrorNumber, error.ErrorText); } throw new Exception(msg.ToString()); } return(result.CompiledAssembly); }
public Assembly Compile(string source) { //clear console errorText.text = ""; //set up compiler options CompilerParameters compilerParameters = new CompilerParameters(); compilerParameters.GenerateExecutable = false; compilerParameters.GenerateInMemory = true; //add assemblies foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (!assembly.IsDynamic) { compilerParameters.ReferencedAssemblies.Add(assembly.Location); } } //compiler code and get results CSharpCompiler.CodeCompiler virtualCompiler = new CSharpCompiler.CodeCompiler(); CompilerResults compilationResults = virtualCompiler.CompileAssemblyFromSource(compilerParameters, source); //do results have any errors? foreach (var err in compilationResults.Errors) { CompilerError e = (CompilerError)err; //cast a generic object to CompilerError //log errors to the console Debug.Log(e.ErrorText); //display errors to the console on the screen errorText.text += " " + e.ErrorText; } //return compiled (or not) code return(compilationResults.CompiledAssembly); }
protected Assembly CreateAssembly(string name, string[] code) { /* * The For now we will create one assembly for onLoad code call * on memory to cache and avoid IO problems. * To allow user add references a new keyword was created * Ex: * reference System.Data.dll * reference CustomAssembly.dll * reference Custom2 * * All the *using* and *reference* have to be befora all the statement code */ var parameters = new CompilerParameters(); var final = new List <string> { "using System;", "using RiveScript;" }; parameters.GenerateInMemory = true; parameters.GenerateExecutable = false; parameters.ReferencedAssemblies.Add("System.dll");//Add basic assemblies parameters.ReferencedAssemblies.Add("System.Core.dll"); parameters.ReferencedAssemblies.Add("UnityEngine.dll"); //parameters.ReferencedAssemblies.Add ("System.CodeDom.Compiler.dll"); //Add refernce to RiveScript assembly parameters.ReferencedAssemblies.Add(riveAssembly); //Add reference to current execution assemblie if (false == ExtensionsMethods.IsNullOrWhiteSpace(currentAssembly)) { parameters.ReferencedAssemblies.Add(currentAssembly); } IndentedTextWriter w; //Find all references and usings for (int i = 0; i < code.Length; i++) { var line = code[i]; if (ExtensionsMethods.IsNullOrWhiteSpace(line)) { continue; } line = line.Trim(); if (line.StartsWith("reference")) { var ra = line.Replace("reference", "") .Replace(" ", "") .Replace(";", ""); ra = ra.EndsWith(".dll") ? ra : (ra + ".dll"); parameters.ReferencedAssemblies.Add(ra); code[i] = ""; } else if (line.StartsWith("using") && false == line.StartsWith("using (") && false == line.StartsWith("using(")) { final.Add(line); //Early add usings code[i] = ""; } } final.Add("namespace " + ns); final.Add("{"); final.Add(" public class " + name); final.Add(" {"); final.Add(" public static string method(RiveScript rs, string[] args)"); final.Add(" {"); final.AddRange(code); final.Add(" }"); final.Add(" }"); final.Add("}"); var provider = new CSharpCompiler.CodeCompiler(); var result = provider.CompileAssemblyFromSource(parameters, String.Join(Environment.NewLine, final.ToArray())); if (result.Errors.HasErrors) { var sr = ""; foreach (CompilerError error in result.Errors) { sr += string.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText); } throw new InvalidOperationException("ERR: object " + name + " - " + sr); } return(result.CompiledAssembly); }
void CompileFiles() { //filePaths = filePaths.Where(x => File.Exists(x)).ToArray(); CompilerParameters cp = new CompilerParameters(); //options.GenerateExecutable = false; //options.GenerateInMemory = true; cp.ReferencedAssemblies.AddRange(assemblyReferences); // cp.ReferencedAssemblies.Add("system.dll"); // cp.ReferencedAssemblies.Add("system.xml.dll"); // cp.ReferencedAssemblies.Add("system.data.dll"); // cp.ReferencedAssemblies.Add("system.windows.forms.dll"); // cp.ReferencedAssemblies.Add("system.drawing.dll"); //#if UNITY_EDITOR // cp.ReferencedAssemblies.Add(@"dll\UnityEngine.dll"); // cp.ReferencedAssemblies.Add(@"Assets\Plugins\GameLibrary.dll"); // cp.ReferencedAssemblies.Add(@"dll\UnityEngine.UI.dll"); // Debug.Log("Unity Editor"); //#endif //#if UNITY_STANDALONE // cp.ReferencedAssemblies.Add(@"BeginReality_Data\Managed\UnityEngine.dll"); // cp.ReferencedAssemblies.Add(@"BeginReality_Data\Managed\GameLibrary.dll"); // cp.ReferencedAssemblies.Add(@"BeginReality_Data\Managed\UnityEngine.UI.dll"); // Debug.Log("STANDALONE"); //#endif cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; cp.GenerateExecutable = false; var compiler = new CodeCompiler(); //CompilerResults cr = compiler.CompileAssemblyFromFileBatch(cp, filePaths.ToArray()); CompilerResults cr = compiler.CompileAssemblyFromSource(cp, code.ToString()); string errors = ""; if (cr.Errors.HasErrors) { foreach (CompilerError error in cr.Errors) { ConsoleReport.LogReport("Ocorreu um erro na linha " + (error.Line - 7).ToString() + " e coluna " + error.Column.ToString()); errors = errors + error.ErrorText + " linha: " + (error.Line - 7).ToString() + "\n"; } throw new Exception(errors); } //foreach (var err in result.Errors) { // manager.logWriter.WriteLine(err); //} this.assembly = cr.CompiledAssembly; object o = assembly.CreateInstance("CSCodeEvaler"); Type t = assembly.GetType("CSCodeEvaler"); if (t != null) { MethodInfo mi = t.GetMethod("EvalCode"); mi.Invoke(o, null); } }