Advertisement
AyrA

C# Module Compiler

Mar 30th, 2016
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.CodeDom.Compiler;
  3. using Microsoft.CSharp;
  4. using System;
  5. using System.Reflection;
  6. using System.Diagnostics;
  7.  
  8. namespace PingerService
  9. {
  10.     public static class ScriptManager
  11.     {
  12.         private const string INCLUDE = "//#include";
  13.  
  14.         private static string Self
  15.         {
  16.             get
  17.             {
  18.                 return Process.GetCurrentProcess().MainModule.FileName;
  19.             }
  20.         }
  21.  
  22.         /// <summary>
  23.         /// Contains a List of Compiler Errors that may occur on the "Load" Call, if it failed.
  24.         /// </summary>
  25.         public static CompilerErrorCollection LastErrors = null;
  26.  
  27.         /// <summary>
  28.         /// Loads Source into Memory
  29.         /// </summary>
  30.         /// <param name="Content">Source Code to load</param>
  31.         /// <returns>Loaded modules</returns>
  32.         public static Module[] Load(string Content)
  33.         {
  34.             //Initialize Compiler
  35.             string retValue = string.Empty;
  36.             CodeDomProvider codeProvider = new CSharpCodeProvider();
  37.             CompilerParameters compilerParams = new CompilerParameters();
  38.             compilerParams.GenerateExecutable = false;
  39.             compilerParams.GenerateInMemory = true;
  40.  
  41.             string[] Lines = Content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
  42.             foreach (string Line in Lines)
  43.             {
  44.                 //Check if Include Statement or Code
  45.                 if (Line.Trim().ToLower().StartsWith(INCLUDE))
  46.                 {
  47.                     compilerParams.ReferencedAssemblies.Add(Line.Substring(INCLUDE.Length + 1));
  48.                 }
  49.             }
  50.  
  51.             //Compile
  52.             CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerParams, new string[] { Content });
  53.  
  54.             //Check if Errors
  55.             if (results.Errors.Count > 0)
  56.             {
  57.                 LastErrors = results.Errors;
  58.                 return null;
  59.             }
  60.             return LoadAssembly(results.CompiledAssembly);
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Loads an Assembly ang gets its Name
  65.         /// </summary>
  66.         /// <param name="a">Assembly to load</param>
  67.         /// <returns>Loaded modules</returns>
  68.         private static Module[] LoadAssembly(Assembly a)
  69.         {
  70.             List<Module> Modules = new List<Module>();
  71.  
  72.             foreach (Type type in a.GetTypes())
  73.             {
  74.                 if (type.IsPublic && !type.IsAbstract)
  75.                 {
  76.                     if (Module.IsModule(type))
  77.                     {
  78.                         Modules.Add(new Module(type));
  79.                     }
  80.                     else
  81.                     {
  82.                         Logger.Info("Not a module: {0}", type.Name);
  83.                     }
  84.                 }
  85.             }
  86.             return Modules.ToArray();
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Loads a compiled Script into Memory
  91.         /// </summary>
  92.         /// <param name="Path">DLL File Name</param>
  93.         /// <returns>Loaded modules</returns>
  94.         public static Module[] LoadLib(string Path)
  95.         {
  96.             return LoadAssembly(Assembly.LoadFile(System.IO.Path.GetFullPath(Path)));
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Compiles source code into an assembly.
  101.         /// Useful for larger files that take long to load.
  102.         /// </summary>
  103.         /// <param name="Content">Source code</param>
  104.         /// <param name="Destination">Destination dll name</param>
  105.         public static void Compile(string Content, string Destination)
  106.         {
  107.             //Initialize Compiler
  108.             string retValue = string.Empty;
  109.             CodeDomProvider codeProvider = new CSharpCodeProvider();
  110.             CompilerParameters compilerParams = new CompilerParameters();
  111.             compilerParams.GenerateExecutable = false;
  112.             compilerParams.GenerateInMemory = false;
  113.             compilerParams.OutputAssembly = Destination;
  114.  
  115.             string[] Lines = Content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
  116.             foreach (string Line in Lines)
  117.             {
  118.                 //Check if Include Statement or Code
  119.                 if (Line.Trim().ToLower().StartsWith(INCLUDE))
  120.                 {
  121.                     compilerParams.ReferencedAssemblies.Add(Line.Substring(INCLUDE.Length + 1));
  122.                 }
  123.             }
  124.  
  125.             //Compile
  126.             CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerParams, new string[] { Content });
  127.  
  128.             //Check if Errors
  129.             if (results.Errors.Count > 0)
  130.             {
  131.                 LastErrors = results.Errors;
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement