using System; using System.Collections.Generic; using System.Text; using System.CodeDom; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; using System.Reflection; using Microsoft.CSharp; using System.Security; using System.Security.Permissions; using System.IO; namespace DarkWynter.Engine.Compiler { public class CSharpCompiler { public static object Execute(string source, string className, string functionName, object[] parameters) { CompilerResults compilerResults = CompileCode(source); if (compilerResults.Errors.Count > 0) { throw new Exception("Not a valid assembly"); } return Execute(compilerResults.CompiledAssembly, className, functionName, parameters); } public static object Execute(Assembly a, // string className, // string functionName, object[] parameters, ref object callingObject) { object obj = null; Type type = a.GetType(); foreach (Type tempType in a.GetTypes()) { type = tempType; } try { foreach (MethodInfo info in type.GetMethods()) { Array tempArray = info.GetParameters(); if (info.GetParameters().Length == 0) { if (obj == null) { obj = Activator.CreateInstance(type); info.Invoke(obj, null); } } } } catch { } return obj; } public static object Execute(Assembly assembly, string className, string functionName, object[] parameters) { object o = null; return Execute(assembly, parameters, ref o); } public static CompilerResults CompileCode(String source) { CodeDomProvider provider = new CSharpCodeProvider(); CompilerParameters compilerParameters = new CompilerParameters(); // Generate a class library. compilerParameters.GenerateExecutable = false; compilerParameters.GenerateInMemory = true; // Generate debug information. compilerParameters.IncludeDebugInformation = false; // Add an assembly reference. Assembly assembly = Assembly.GetExecutingAssembly(); foreach (AssemblyName assemblyName in Assembly.GetEntryAssembly().GetReferencedAssemblies()) { if (assemblyName.Name == "DWEngine" || assemblyName.Name == "DWStream") { compilerParameters.ReferencedAssemblies.Add(assemblyName.Name + ".dll"); } } // compilerParameters.ReferencedAssemblies.Add("DarkWynterGame" + ".dll"); // Set the level at which the compiler // should start displaying warnings. compilerParameters.WarningLevel = 3; // Set whether to treat all warnings as errors. compilerParameters.TreatWarningsAsErrors = false; // Set compiler argument to optimize output. compilerParameters.CompilerOptions = "/optimize"; // Invoke compilation. CompilerResults compilerResults = provider.CompileAssemblyFromSource( compilerParameters, new string[] { source }); return compilerResults; //} } } }