Esempio n. 1
0
        public static Assembly Compile(string[] files)
        {
            var outputName = Mathf.Abs(DateTime.Now.GetHashCode()) + ".dll";
            var options    = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory   = false,
                OutputAssembly     = outputName
            };

            options.ReferencedAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies()
                                                  .Where(a => !a.IsDynamic)
                                                  .Select(a => a.Location)
                                                  .Where(s => !string.IsNullOrEmpty(s))
                                                  .ToArray());

            var compiler = new CodeCompiler();
            var result   = compiler.CompileAssemblyFromFileBatch(options, files);

            if (result.Errors.Count > 0)
            {
                Console.instance.Print("There were compilation errors.");
                foreach (CompilerError resultError in result.Errors)
                {
                    Console.instance.Print($"[{resultError.Line}]: {resultError.ErrorText}");
                }
            }

            return(result.CompiledAssembly);
        }
Esempio n. 2
0
        public static void CSRun(string input)
        {
            var @class = "CSRUN" + Mathf.Abs(DateTime.Now.GetHashCode());
            var code   = @"
                            using System;
                            using System.Collections.Generic;
                            using System.Linq;
                            using System.Text;
                            using UnityEngine;
                            using HarmonyLib;
                            using System.Reflection;
                            using System.Threading;
                            public class %CLASS%
                            {
                                public static void Run()
                                {
                                    try
                                    {
                                        %CODE%
                                    }
                                    catch (Exception e)
                                    {
                                        Console.instance.Print(e.Message);
                                    }
                                } 
                            }
                        ".Replace("%CLASS%", @class)
                         .Replace("%CODE%", input);

            try
            {
                var options = new CompilerParameters
                {
                    GenerateExecutable = false,
                    GenerateInMemory   = true
                };

                options.ReferencedAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies()
                                                      .Where(a => !a.IsDynamic)
                                                      .Select(a => a.Location)
                                                      .Where(s => !string.IsNullOrEmpty(s))
                                                      .ToArray());

                var compiler = new CodeCompiler();
                var result   = compiler.CompileAssemblyFromSource(options, code);

                if (result.Errors.Count > 0)
                {
                    Logger.Log("There were compilation errors.");
                    foreach (CompilerError resultError in result.Errors)
                    {
                        Logger.Log($"[{resultError.Line}]: {resultError.ErrorText}");
                    }
                }
                else
                {
                    Logger.Log("Compilation success.");
                    try
                    {
                        result.CompiledAssembly.GetType(@class)?.GetMethod("Run")?.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        Logger.Log("An exception occured: " + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Log(e.Message);
            }
        }