Guest User

help me ahahahah

a guest
Sep 17th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.83 KB | None | 0 0
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using Microsoft.CSharp;
  7. using Microsoft.VisualBasic;
  8. using System.Collections.Generic;
  9.  
  10. namespace CrypterTest.Utils
  11. {
  12.     public static class Codedom
  13.     {
  14.         public static bool Compile(string outPath, string[] sources, string iconPath, string conditionalCompilation, string[] imports, string[] resourcePaths, string target, int netVersion)
  15.         {
  16.  
  17.             if (!string.IsNullOrEmpty(outPath))
  18.             {
  19.                 string netPath;
  20.                 string mscorlib;
  21.                 string system;
  22.  
  23.                 switch (netVersion)
  24.                 {
  25.                     case 20:
  26.                         netPath = Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"),
  27.                   "Microsoft.NET\\Framework\\v2.0.50727\\");
  28.  
  29.                         mscorlib = Path.Combine(netPath, "mscorlib.dll");
  30.                         system = Path.Combine(netPath, "System.dll");
  31.                         break;
  32.                     case 30:
  33.                         netPath = Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"),
  34.                 "Microsoft.NET\\Framework\\v3.0\\");
  35.  
  36.                         mscorlib = Path.Combine(netPath, "mscorlib.dll");
  37.                         system = Path.Combine(netPath, "System.dll");
  38.                         break;
  39.                     case 35:
  40.                         netPath = Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"),
  41.                 "Microsoft.NET\\Framework\\v3.5\\");
  42.  
  43.                         mscorlib = Path.Combine(netPath, "mscorlib.dll");
  44.                         system = Path.Combine(netPath, "System.dll");
  45.                         break;
  46.                     case 40:
  47.                         netPath = Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"),
  48.                 "Microsoft.NET\\Framework\\v4.0.30319\\");
  49.  
  50.                         mscorlib = Path.Combine(netPath, "mscorlib.dll");
  51.                         system = Path.Combine(netPath, "System.dll");
  52.                         break;
  53.                     default:
  54.                         throw new Exception();
  55.  
  56.                 }
  57.  
  58.  
  59.  
  60.                 //var provider = new CSharpCodeProvider();
  61.  
  62.                 var p = new CompilerParameters();
  63.                 var sb = new StringBuilder();
  64.  
  65.                 p.OutputAssembly = outPath;
  66.  
  67.                 p.ReferencedAssemblies.AddRange(new[] { mscorlib, system });
  68.  
  69.                 //p.ReferencedAssemblies.Add("System.dll");
  70.                 p.ReferencedAssemblies.Add("System.Windows.Forms.dll");
  71.                 p.ReferencedAssemblies.Add("System.Drawing.dll");
  72.                 p.ReferencedAssemblies.Add("System.Data.dll");
  73.                 p.ReferencedAssemblies.Add("System.Security.dll");
  74.  
  75.                 /*if (imports != null && imports.Length > 0)
  76.                 {
  77.                     foreach (string import in imports)
  78.                     {
  79.                         p.ReferencedAssemblies.Add(Path.Combine(netPath, import));
  80.  
  81.                     }
  82.                     // p.ReferencedAssemblies.AddRange(imports);
  83.                 }*/
  84.                 p.GenerateExecutable = true;
  85.  
  86.                 p.IncludeDebugInformation = false;
  87.  
  88.                 string tempPath = Path.Combine(Environment.CurrentDirectory, "temp");
  89.  
  90.                 Directory.CreateDirectory(tempPath);
  91.  
  92.                 p.TempFiles = new TempFileCollection(tempPath, false);
  93.  
  94.                 if (resourcePaths != null && resourcePaths.Length > 0)
  95.                 {
  96.                     p.EmbeddedResources.AddRange(resourcePaths);
  97.                 }
  98.  
  99.                 if (!string.IsNullOrEmpty(conditionalCompilation))
  100.                 {
  101.                     sb.AppendFormat(" /define:{0} ", conditionalCompilation);
  102.                 }
  103.  
  104.                 sb.AppendFormat(" /target:{0} ", target);
  105.  
  106.  
  107.                 sb.Append(" /filealign:512 /platform:x86 /optimize+ /nostdlib /unsafe");
  108.  
  109.                 if ((!string.IsNullOrEmpty(iconPath)) && (File.Exists(iconPath)))
  110.                 {
  111.                     sb.AppendFormat(" /win32icon:{0}", Convert.ToChar(34) + iconPath + Convert.ToChar(34));
  112.                 }
  113.  
  114.                 p.CompilerOptions = sb.ToString();
  115.  
  116.  
  117.  
  118.                 var providerOptions = new Dictionary<string, string>();
  119.                 providerOptions.Add("CompilerVersion", "v2.0");
  120.  
  121.                 CompilerResults results = null;
  122.  
  123.                 CodeDomProvider compilerProvider;
  124.  
  125.                
  126.                compilerProvider = new CSharpCodeProvider();
  127.                
  128.  
  129.                 // = new CSharpCodeProvider();
  130.  
  131.                 results = compilerProvider.CompileAssemblyFromSource(p, sources);
  132.  
  133.                 // results.TempFiles.Delete();
  134.  
  135.                 if (resourcePaths != null && resourcePaths.Length > 0)
  136.                 {
  137.                     foreach (string res in resourcePaths)
  138.                     {
  139.                         try
  140.                         {
  141.                             File.Delete(res);
  142.                         }
  143.                         catch (Exception)
  144.                         {
  145.                         }
  146.                     }
  147.                 }
  148.  
  149.                 // if (File.Exists("res.resources")) File.Delete("res.resources");
  150.                 //if (Directory.Exists("temp")) Directory.Delete("temp", true);
  151.  
  152.  
  153.                 StringBuilder errors = new StringBuilder();
  154.  
  155.                 if (results.Errors.HasErrors)
  156.                 {
  157.  
  158.                     //  logger.LogText("Knight Logger Reloaded Edition encountered " + results.Errors.Count + " errors.");
  159.                     foreach (CompilerError err in results.Errors)
  160.                     {
  161.                         if (!err.IsWarning)
  162.                             errors.AppendLine(err.ToString());
  163.  
  164.                         //if (UseLogger)
  165.                         //{
  166.                         //    Logger.Log(err.ErrorText + " At Line : " + err.Line);
  167.                         //}
  168.                         //log.Log(err.ErrorText + " At Line : " + err.Line);
  169.  
  170.  
  171.                     }
  172.  
  173. #if DEBUG
  174.                     File.WriteAllText("errors.log", errors.ToString());
  175.                     Process.Start("errors.log");
  176.  
  177.                     File.WriteAllText("source.cs", sources[0]);
  178. #else
  179.  
  180. #endif          
  181.  
  182.  
  183.                     return false;
  184.                 }
  185.                 else
  186.                 {
  187.                     return File.Exists(outPath);
  188.                 }
  189.  
  190.                 //    else
  191.                 //  {
  192.  
  193.                 // }
  194.             }
  195.             //  else return false;//throw new ArgumentException("Please provide the output path to write to compiled executable.", "EthernalCompiler.Output = \"<path to output file here>\"");
  196.  
  197.             return false;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment