示例#1
0
        private static CompilerResults CompileAssembly(CSharpCompiler.CodeCompiler cSharpCompiler, CompilerParameters parameters)
        {
            var cs      = Directory.GetFiles($"{RimDumperMod.RootDir}/CustomParsers", "*.cs", SearchOption.AllDirectories);
            var results = cSharpCompiler.CompileAssemblyFromFileBatch(parameters, cs);

            return(results);
        }
        public void Compile()
        {
            if (string.IsNullOrEmpty(SourceCode))
            {
                return;
            }

            ClearLogs();

            CompiledStartMethod  = null;
            CompiledUpdateMethod = null;

            try
            {
                string[] assemblyNames = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).Select(a => a.FullName).ToArray();

                CompilerParameters compiler =
                    new CompilerParameters
                {
                    GenerateInMemory      = true,
                    TreatWarningsAsErrors = false
                };

                compiler.ReferencedAssemblies.AddRange(assemblyNames);

                CompilerResults results = new CSharpCompiler.CodeCompiler().CompileAssemblyFromSource(compiler, SourceCode);



                if (results.Errors.Count > 0)
                {
                    Log("CSharpCompiler Error");

                    foreach (CompilerError ce in results.Errors)
                    {
                        Log(ce.ToString());
                    }
                }
                else
                {
                    string className = new StringReader(SourceCode).ReadLine().Remove(0, 2);
                    Console.WriteLine("Class name : " + className);

                    Assembly assembly = results.CompiledAssembly;
                    Type     type     = assembly.GetType(className);

                    CompiledStartMethod  = type.GetMethod("Start", BindingFlags.Static | BindingFlags.NonPublic);
                    CompiledUpdateMethod = type.GetMethod("Update", BindingFlags.Static | BindingFlags.NonPublic);

                    RunStartMethod = true;
                    Running        = true;
                }
            }
            catch (Exception e)
            {
                Log("Compile method error catch");
                ErrorOutput(e);
            }
        }
示例#3
0
        public MCS()
        {
            compiler = new CSharpCompiler.CodeCompiler();
            options  = new System.CodeDom.Compiler.CompilerParameters();
            options.GenerateExecutable = false;
            options.GenerateInMemory   = true;
            options.OutputAssembly     = "ScriptAssembly.dll";
            //options.ReferencedAssemblies.AddRange(ReferencedAssemblies.assemblies);

            //Reference all assemblies, except System.IO
            string[] assemblyReferences = System.AppDomain.CurrentDomain.GetAssemblies().Where(e => e.FullName.Contains("System.IO") == false).Select(a => a.Location).ToArray();
            options.ReferencedAssemblies.AddRange(assemblyReferences);

            assemblies = new Dictionary <string, Assembly>();
        }
        private static Assembly CompileCode(string code)
        {
#if !UNITY_EDITOR && ENABLE_IL2CPP
            throw new Exception("Code compilation is not supported on AOT platforms such as IL2CPP and requires JIT (Mono).");
#else
            CSharpCompiler.CodeCompiler compiler       = new CSharpCompiler.CodeCompiler();
            CompilerParameters          compilerParams = new CompilerParameters();
            Assembly[] allLoadedAssemblies             = AppDomain.CurrentDomain.GetAssemblies();

            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory   = true;
            for (int i = 0; i < allLoadedAssemblies.Length; i++)
            {
                if (!allLoadedAssemblies[i].IsDynamic)
                {
                    string dllName = allLoadedAssemblies[i].Location;
                    compilerParams.ReferencedAssemblies.Add(dllName);
                }
            }

            CompilerResults compiledCode = compiler.CompileAssemblyFromSource(compilerParams, code);

            if (compiledCode.Errors.HasErrors)
            {
                string errorMessage = "Code Compilation Failure";
                for (int i = 0; i < compiledCode.Errors.Count; i++)
                {
                    errorMessage += $"\n{compiledCode.Errors[i].ErrorNumber} - {compiledCode.Errors[i].ErrorText}";
                }

                throw new ArgumentException(errorMessage);
            }

            return(compiledCode.CompiledAssembly);
#endif
        }
示例#5
0
        public static Assembly CompileSource(string[] sources, bool isSource, bool GenerateInMemory = true)
        {
            if (CodeCompiler == null)
            {
                CodeCompiler = new CSharpCompiler.CodeCompiler();
            }
            var compilerparams = new CompilerParameters();

            //add all references to assembly - need to use Assembly resolver for Dynamicly created
            //assemblies, as assembly.Location will fail for them
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    compilerparams.ReferencedAssemblies.Add(assembly.Location);
                }
                catch
                {
                    if (DynamicAssemblyResolver.ContainsKey(assembly.FullName))
                    {
                        compilerparams.ReferencedAssemblies.Add(assembly.GetName().FullName);
                    }
                }
            }

            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory   = GenerateInMemory;

            AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
            {
                if (DynamicAssemblyResolver.ContainsKey(e.Name))
                {
                    //UnityEngine.Debug.Log("resolved assembly for:" + e.Name);
                    return(DynamicAssemblyResolver[e.Name]);
                }
                else
                {
                    return(null);
                }
            };

            // Compile the source
            CompilerResults result;

            if (isSource)
            {
                result = CodeCompiler.CompileAssemblyFromSourceBatch(compilerparams, sources);
            }
            else
            {
                result = CodeCompiler.CompileAssemblyFromFileBatch(compilerparams, sources);
            }


            if (result.CompiledAssembly != null)
            {
                if (!DynamicAssemblyResolver.ContainsKey(result.CompiledAssembly.FullName))
                {
                    DynamicAssemblyResolver.Add(result.CompiledAssembly.FullName, result.CompiledAssembly);
                }
            }

            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 the assembly
            return(result.CompiledAssembly);
        }
        public static Assembly CompileSource(string[] sources, bool isSource, bool GenerateInMemory = true)
        {
            if (CodeCompiler == null)
                CodeCompiler = new CSharpCompiler.CodeCompiler();
            var compilerparams = new CompilerParameters();

            //add all references to assembly - need to use Assembly resolver for Dynamicly created
            //assemblies, as assembly.Location will fail for them
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    compilerparams.ReferencedAssemblies.Add(assembly.Location);
                }
                catch
                {
                    if (DynamicAssemblyResolver.ContainsKey(assembly.FullName))
                        compilerparams.ReferencedAssemblies.Add(assembly.GetName().FullName);
                }
            }

            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory = GenerateInMemory;

            AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
            {
                if (DynamicAssemblyResolver.ContainsKey(e.Name))
                {
                    //UnityEngine.Debug.Log("resolved assembly for:" + e.Name);
                    return DynamicAssemblyResolver[e.Name];
                }
                else
                    return null;
            };

            // Compile the source
            CompilerResults result;
            if (isSource)
                result = CodeCompiler.CompileAssemblyFromSourceBatch(compilerparams, sources);
            else
                result = CodeCompiler.CompileAssemblyFromFileBatch(compilerparams, sources);

            if (result.CompiledAssembly != null)
            {
                if (!DynamicAssemblyResolver.ContainsKey(result.CompiledAssembly.FullName))
                    DynamicAssemblyResolver.Add(result.CompiledAssembly.FullName, result.CompiledAssembly);
            }

            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 the assembly
            return result.CompiledAssembly;
        }