Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.CodeDom.Compiler;
- using System.Reflection;
- using Microsoft.CSharp;
- namespace ScriptInterpreter
- {
- public class Interpreter
- {
- public static string AssemblyPlaceholder = "ASSEMBLY_PASTE";
- public static string IOClassPlaceholder = "CLASS_PASTE";
- public static string MethodPlaceholder = "METHOD_PASTE";
- public static string IOClassRegionName = "IO_CLASSES";
- public Interpreter()
- {
- source = File.ReadAllText(@"GeneralSourcePlaceholder.cs");
- Dictionary<string, string> providerOptions = new Dictionary<string, string> { { "CompilerVersion", "v3.5" } };
- provider = new CSharpCodeProvider(providerOptions);
- compilerParams = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = true };
- usedAssemblies = new List<string>();
- }
- public void ClearUsings()
- {
- usedAssemblies.Clear();
- }
- public void AddAssembly(string assemblyName)
- {
- usedAssemblies.Add("using " + assemblyName + ";");
- }
- public void LoadAssemblies()
- {
- string assemblyString = string.Join("\n", usedAssemblies.ToArray());
- sourceWithAssemblies = source.Replace(AssemblyPlaceholder, assemblyString);
- }
- public void LoadIOClasses(string IOClassesPath)
- {
- StreamReader reader = new StreamReader(IOClassesPath);
- string IOClassesSource = "";
- string currLine = reader.ReadLine();
- while (!reader.EndOfStream)
- {
- if (currLine.Contains("#region"))
- {
- // Found region, make sure it's the same as in IOClassRegionName
- string regionName = string.Join("", currLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where((str) => str != "#region").ToArray());
- if (regionName == IOClassRegionName)
- {
- // Good! Next x lines contain our classes and stuff, until we hit #endregion, that is
- currLine = reader.ReadLine();
- while (!reader.EndOfStream && !currLine.Contains("#endregion"))
- {
- IOClassesSource += currLine;
- currLine = reader.ReadLine();
- }
- break;
- }
- }
- currLine = reader.ReadLine();
- }
- sourceWithAssembliesAndClasses = sourceWithAssemblies.Replace(IOClassPlaceholder, IOClassesSource);
- }
- public void LoadMethod(string methodPath)
- {
- sourceWithAssembliesAndClassesAndMethod = sourceWithAssembliesAndClasses.Replace(MethodPlaceholder, File.ReadAllText(methodPath));
- }
- public void Compile()
- {
- results = provider.CompileAssemblyFromSource(compilerParams, sourceWithAssembliesAndClassesAndMethod);
- if (results.Errors.Count != 0)
- {
- string errorMessage = "Could not compile assembly!\nErrors:\n\n";
- foreach (CompilerError error in results.Errors)
- {
- errorMessage += String.Format("Line {0}: Error code {1}, {2}\n", error.Line, error.ErrorNumber, error.ErrorText);
- }
- throw new Exception(errorMessage);
- }
- }
- public object ExecuteMethod(object input)
- {
- object compiledAssembly = results.CompiledAssembly.CreateInstance("ScriptInterpreter.Interpreter");
- MethodInfo methodInfo = compiledAssembly.GetType().GetMethod("GetOutput");
- object output = methodInfo.Invoke(compiledAssembly, new object[] { input });
- return null;
- }
- string source;
- string sourceWithAssemblies;
- string sourceWithAssembliesAndClasses;
- string sourceWithAssembliesAndClassesAndMethod;
- List<string> usedAssemblies;
- CSharpCodeProvider provider;
- CompilerParameters compilerParams;
- CompilerResults results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment