AyrA icon

C# Module Compiler

AyrA | PRO | 03/30/16 11:58:58 AM UTC | 0 ⭐ | 550 👁️ | Never ⏰ | []
C# |

4.72 KB

|

None

|

0 👍

/

0 👎

using System.Collections.Generic;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System;
using System.Reflection;
using System.Diagnostics;
 
namespace PingerService
{
    public static class ScriptManager
    {
        private const string INCLUDE = "//#include";
 
        private static string Self
        {
            get
            {
                return Process.GetCurrentProcess().MainModule.FileName;
            }
        }
 
        /// <summary>
        /// Contains a List of Compiler Errors that may occur on the "Load" Call, if it failed.
        /// </summary>
        public static CompilerErrorCollection LastErrors = null;
 
        /// <summary>
        /// Loads Source into Memory
        /// </summary>
        /// <param name="Content">Source Code to load</param>
        /// <returns>Loaded modules</returns>
        public static Module[] Load(string Content)
        {
            //Initialize Compiler
            string retValue = string.Empty;
            CodeDomProvider codeProvider = new CSharpCodeProvider();
            CompilerParameters compilerParams = new CompilerParameters();
            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory = true;
 
            string[] Lines = Content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
            foreach (string Line in Lines)
            {
                //Check if Include Statement or Code
                if (Line.Trim().ToLower().StartsWith(INCLUDE))
                {
                    compilerParams.ReferencedAssemblies.Add(Line.Substring(INCLUDE.Length + 1));
                }
            }
 
            //Compile
            CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerParams, new string[] { Content });
 
            //Check if Errors
            if (results.Errors.Count > 0)
            {
                LastErrors = results.Errors;
                return null;
            }
            return LoadAssembly(results.CompiledAssembly);
        }
 
        /// <summary>
        /// Loads an Assembly ang gets its Name
        /// </summary>
        /// <param name="a">Assembly to load</param>
        /// <returns>Loaded modules</returns>
        private static Module[] LoadAssembly(Assembly a)
        {
            List<Module> Modules = new List<Module>();
 
            foreach (Type type in a.GetTypes())
            {
                if (type.IsPublic && !type.IsAbstract)
                {
                    if (Module.IsModule(type))
                    {
                        Modules.Add(new Module(type));
                    }
                    else
                    {
                        Logger.Info("Not a module: {0}", type.Name);
                    }
                }
            }
            return Modules.ToArray();
        }
 
        /// <summary>
        /// Loads a compiled Script into Memory
        /// </summary>
        /// <param name="Path">DLL File Name</param>
        /// <returns>Loaded modules</returns>
        public static Module[] LoadLib(string Path)
        {
            return LoadAssembly(Assembly.LoadFile(System.IO.Path.GetFullPath(Path)));
        }
 
        /// <summary>
        /// Compiles source code into an assembly.
        /// Useful for larger files that take long to load.
        /// </summary>
        /// <param name="Content">Source code</param>
        /// <param name="Destination">Destination dll name</param>
        public static void Compile(string Content, string Destination)
        {
            //Initialize Compiler
            string retValue = string.Empty;
            CodeDomProvider codeProvider = new CSharpCodeProvider();
            CompilerParameters compilerParams = new CompilerParameters();
            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory = false;
            compilerParams.OutputAssembly = Destination;
 
            string[] Lines = Content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
            foreach (string Line in Lines)
            {
                //Check if Include Statement or Code
                if (Line.Trim().ToLower().StartsWith(INCLUDE))
                {
                    compilerParams.ReferencedAssemblies.Add(Line.Substring(INCLUDE.Length + 1));
                }
            }
 
            //Compile
            CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerParams, new string[] { Content });
 
            //Check if Errors
            if (results.Errors.Count > 0)
            {
                LastErrors = results.Errors;
            }
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎