// Use this for initialization void Start() { //Setup compiler CSharpCompiler.CodeCompiler compiler = new CSharpCompiler.CodeCompiler(); System.CodeDom.Compiler.CompilerParameters options = new System.CodeDom.Compiler.CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.OutputAssembly = "MyAssembly.dll"; //-- Add ALL assemblies to compiler. This is a security issue as user would have access to System.IO.File to delete data... //var domain = System.AppDomain.CurrentDomain; //string[] assemblyReferences = domain.GetAssemblies().Select(a => a.Location).ToArray(); //options.ReferencedAssemblies.AddRange(assemblyReferences); //-- Add only some specific assemblies options.ReferencedAssemblies.Add("UnityEngine"); //Add UnityEngine assembly options.ReferencedAssemblies.Add("Assembly-CSharp"); //Add Assembly which holds all our scripts after build (THIS script is also located in this assembly) //Compile var result = compiler.CompileAssemblyFromFileBatch(options, new[] { Application.streamingAssetsPath + "/BasicExampleScript.cs" //Add other scripts here, separated by commas }); //Create instances for all classes we just compiled //foreach (var type in result.CompiledAssembly.GetTypes()) //{ // if (typeof(Component).IsAssignableFrom(type)) this.gameObject.AddComponent(type); //If type is a MonoBehaviour, add it to the gameobject // else System.Activator.CreateInstance(type); //Otherwise create a new instance of the class, using the default class constructor //} //Add specific MonoBehaviour from our compiled scripts Type _mb = result.CompiledAssembly.GetType("MyMonoBehaviour"); this.gameObject.AddComponent(_mb); Debug.Log(result.CompiledAssembly.GetName()); //Create an instance of a specific class Type _classType = result.CompiledAssembly.GetType("SomePublicClass"); var classInstance = Activator.CreateInstance(_classType); //Since SomePublicClass uses IMyClass interface (see below), we can cast to it :) IMyClass myClassInstance = (IMyClass)classInstance; myClassInstance.DoSomething(); //...and call a function defined in IMyClass Debug.Log("Sum:" + myClassInstance.Sum(40, 2)); //Another function in SomePublicClass which returns an int }
void CompileFiles() { filePaths = filePaths.Where(x => File.Exists(x)).ToArray(); var options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.ReferencedAssemblies.AddRange(assemblyReferences); var compiler = new CodeCompiler(); var result = compiler.CompileAssemblyFromFileBatch(options, filePaths.ToArray()); foreach (var err in result.Errors) { manager.logWriter.WriteLine(err); } this.assembly = result.CompiledAssembly; }
void CompileFiles() { filePaths = filePaths.Where(x => File.Exists(x)).ToArray(); var options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.ReferencedAssemblies.AddRange(assemblyReferences); var compiler = new CodeCompiler(); var result = compiler.CompileAssemblyFromFileBatch(options, filePaths.ToArray()); // To stop coroutine at the present task if (result.Errors.Count > 0) { VirtualScriptEditor.Instance.isThereErrors = true; VirtualScriptEditor.Instance.errors = ""; } bool pair = false; foreach (var err in result.Errors) { //manager.logWriter.WriteLine(err); UnityErrorTextWriter errorWriter = new UnityErrorTextWriter(); errorWriter.Write(err); if (pair) { VirtualScriptEditor.Instance.errors += err + "\n"; } pair = !pair; } this.assembly = result.CompiledAssembly; }
// Token: 0x06000D55 RID: 3413 RVA: 0x0005AF34 File Offset: 0x00059134 public static string ResolveScriptAssembly(string codeDir, ModContainer mod) { DirectoryInfo directoryInfo = new DirectoryInfo(codeDir); if (!directoryInfo.Exists) { MLog.Error("Code directory " + codeDir + " does not exist!"); return(string.Empty); } string assemblyPath = ModPaths.GetAssemblyPath(mod, directoryInfo.Name); if (File.Exists(assemblyPath)) { return(assemblyPath); } CompilerParameters compilerParameters = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = false, OutputAssembly = assemblyPath }; compilerParameters.ReferencedAssemblies.AddRange((from a in AppDomain.CurrentDomain.GetAssemblies().Where(delegate(Assembly a) { bool result; try { result = !string.IsNullOrEmpty(a.Location); } catch (NotSupportedException) { result = false; } return(result); }) select a.Location).ToArray <string>()); string[] array = (from f in directoryInfo.GetFiles("*.cs", SearchOption.AllDirectories) select f.FullName).ToArray <string>(); if (array.Length == 0) { MLog.Error("Code directory " + codeDir + " does not contain any source files!"); } CSharpCompiler.CodeCompiler codeCompiler = new CSharpCompiler.CodeCompiler(); CompilerResults compilerResults = codeCompiler.CompileAssemblyFromFileBatch(compilerParameters, array); foreach (object obj in compilerResults.Errors) { CompilerError compilerError = (CompilerError)obj; string message = compilerError.ToString(); if (compilerError.IsWarning) { MLog.Warn(message); } else { MLog.Error(message); } } if (compilerResults.Errors.HasErrors) { MLog.Error("There were errors compiling the ScriptAssembly at " + codeDir + "!"); } return(assemblyPath); }