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); } }