Guest User

Interpreter

a guest
Dec 24th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.CodeDom.Compiler;
  6. using System.Reflection;
  7. using Microsoft.CSharp;
  8.  
  9. namespace ScriptInterpreter
  10. {
  11.     public class Interpreter
  12.     {
  13.         public static string AssemblyPlaceholder = "ASSEMBLY_PASTE";
  14.         public static string IOClassPlaceholder = "CLASS_PASTE";
  15.         public static string MethodPlaceholder = "METHOD_PASTE";
  16.         public static string IOClassRegionName = "IO_CLASSES";
  17.  
  18.         public Interpreter()
  19.         {
  20.             source = File.ReadAllText(@"GeneralSourcePlaceholder.cs");
  21.  
  22.             Dictionary<string, string> providerOptions = new Dictionary<string, string> { { "CompilerVersion", "v3.5" } };
  23.             provider = new CSharpCodeProvider(providerOptions);
  24.             compilerParams = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = true };
  25.  
  26.             usedAssemblies = new List<string>();
  27.         }
  28.  
  29.         public void ClearUsings()
  30.         {
  31.             usedAssemblies.Clear();
  32.         }
  33.  
  34.         public void AddAssembly(string assemblyName)
  35.         {
  36.             usedAssemblies.Add("using " + assemblyName + ";");
  37.         }
  38.  
  39.         public void LoadAssemblies()
  40.         {
  41.             string assemblyString = string.Join("\n", usedAssemblies.ToArray());
  42.  
  43.             sourceWithAssemblies = source.Replace(AssemblyPlaceholder, assemblyString);
  44.         }
  45.  
  46.         public void LoadIOClasses(string IOClassesPath)
  47.         {
  48.             StreamReader reader = new StreamReader(IOClassesPath);
  49.  
  50.             string IOClassesSource = "";
  51.  
  52.             string currLine = reader.ReadLine();
  53.             while (!reader.EndOfStream)
  54.             {
  55.                 if (currLine.Contains("#region"))
  56.                 {
  57.                     // Found region, make sure it's the same as in IOClassRegionName
  58.                     string regionName = string.Join("", currLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where((str) => str != "#region").ToArray());
  59.  
  60.                     if (regionName == IOClassRegionName)
  61.                     {
  62.                         // Good! Next x lines contain our classes and stuff, until we hit #endregion, that is
  63.                         currLine = reader.ReadLine();
  64.                         while (!reader.EndOfStream && !currLine.Contains("#endregion"))
  65.                         {
  66.                             IOClassesSource += currLine;
  67.                             currLine = reader.ReadLine();
  68.                         }
  69.  
  70.                         break;
  71.                     }
  72.                 }
  73.  
  74.                 currLine = reader.ReadLine();
  75.             }
  76.  
  77.             sourceWithAssembliesAndClasses = sourceWithAssemblies.Replace(IOClassPlaceholder, IOClassesSource);
  78.         }
  79.  
  80.         public void LoadMethod(string methodPath)
  81.         {
  82.             sourceWithAssembliesAndClassesAndMethod = sourceWithAssembliesAndClasses.Replace(MethodPlaceholder, File.ReadAllText(methodPath));
  83.         }
  84.  
  85.         public void Compile()
  86.         {
  87.             results = provider.CompileAssemblyFromSource(compilerParams, sourceWithAssembliesAndClassesAndMethod);
  88.  
  89.             if (results.Errors.Count != 0)
  90.             {
  91.                 string errorMessage = "Could not compile assembly!\nErrors:\n\n";
  92.  
  93.                 foreach (CompilerError error in results.Errors)
  94.                 {
  95.                     errorMessage += String.Format("Line {0}: Error code {1}, {2}\n", error.Line, error.ErrorNumber, error.ErrorText);
  96.                 }
  97.  
  98.                 throw new Exception(errorMessage);
  99.             }
  100.         }
  101.  
  102.         public object ExecuteMethod(object input)
  103.         {
  104.             object compiledAssembly = results.CompiledAssembly.CreateInstance("ScriptInterpreter.Interpreter");
  105.  
  106.             MethodInfo methodInfo = compiledAssembly.GetType().GetMethod("GetOutput");
  107.  
  108.             object output = methodInfo.Invoke(compiledAssembly, new object[] { input });
  109.  
  110.             return null;
  111.         }
  112.  
  113.         string source;
  114.         string sourceWithAssemblies;
  115.         string sourceWithAssembliesAndClasses;
  116.         string sourceWithAssembliesAndClassesAndMethod;
  117.  
  118.         List<string> usedAssemblies;
  119.  
  120.         CSharpCodeProvider provider;
  121.         CompilerParameters compilerParams;
  122.         CompilerResults results;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment