Program.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Aio1Ef.Packer.Common;
  2. using Aio1Ef.Packer.Common.Coders;
  3. using Aio1Ef.Packer.Properties;
  4. using Loader;
  5. using System;
  6. using System.CodeDom.Compiler;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Reflection;
  10. namespace Aio1Ef.Packer
  11. {
  12. internal static class Program
  13. {
  14. private static void Main(string[] args)
  15. {
  16. string sourceDir;
  17. string mainAssemblyFilename;
  18. string outputAssemblyFilePath;
  19. switch (args.Length)
  20. {
  21. case 3:
  22. sourceDir = args[0];
  23. mainAssemblyFilename = args[1];
  24. outputAssemblyFilePath = args[2];
  25. break;
  26. case 2:
  27. sourceDir = null;
  28. mainAssemblyFilename = args[0];
  29. outputAssemblyFilePath = args[1];
  30. break;
  31. case 1:
  32. sourceDir = null;
  33. mainAssemblyFilename = args[0];
  34. // ReSharper disable once AssignNullToNotNullAttribute
  35. outputAssemblyFilePath = Path.Combine(Path.GetDirectoryName(mainAssemblyFilename), Path.GetFileNameWithoutExtension(mainAssemblyFilename) + ".Aio1Ef" + Path.GetExtension(mainAssemblyFilename));
  36. break;
  37. default:
  38. Console.WriteLine("Useage: {0} <sourceDir> <mainAsm> <outputAsm> - compress all file of source dir", Assembly.GetExecutingAssembly().GetName().Name);
  39. Console.WriteLine("Useage: {0} <mainAsm> <outputAsm> - compress only 1 file", Assembly.GetExecutingAssembly().GetName().Name);
  40. Console.WriteLine("Useage: {0} <mainAsm> - compress that to .Aio1Ef", Assembly.GetExecutingAssembly().GetName().Name);
  41. return;
  42. }
  43. //---------
  44. var mainAssemblyFullpath = sourceDir != null
  45. ? Path.Combine(sourceDir, mainAssemblyFilename)
  46. : mainAssemblyFilename;
  47. var isConsole = CoffUtility.AssemblyUsesConsoleSubsystem(mainAssemblyFullpath);
  48. var mainAssembly = Assembly.LoadFrom(mainAssemblyFullpath);
  49. var targetPlatform = CoffUtility.GetTargetPlatform(mainAssembly);
  50. var filesToPack = new List<PackedFileEntry>();
  51. byte[] solidBytes;
  52. using (var msAllBytes = new MemoryStream())
  53. {
  54. if (sourceDir != null)
  55. {
  56. var fromFullpath = Path.GetFullPath(sourceDir);
  57. foreach (var file in Directory.GetFiles(fromFullpath, "*", SearchOption.AllDirectories))
  58. {
  59. var bufPerFile = File.ReadAllBytes(file);
  60. msAllBytes.Write(bufPerFile, 0, bufPerFile.Length);
  61. var that = file.Substring(fromFullpath.Length, file.Length - fromFullpath.Length).Trim(Path.DirectorySeparatorChar);
  62. filesToPack.Add(new PackedFileEntry(that, bufPerFile.Length));
  63. Console.WriteLine("Loaded: Size: {1:000,000,000} - {0}", that, bufPerFile.Length);
  64. }
  65. }
  66. else
  67. {
  68. var bufPerFile = File.ReadAllBytes(mainAssemblyFilename);
  69. msAllBytes.Write(bufPerFile, 0, bufPerFile.Length);
  70. var that = Path.GetFileName(mainAssemblyFilename);
  71. filesToPack.Add(new PackedFileEntry(that, bufPerFile.Length));
  72. Console.WriteLine("Loaded: Size: {1:000,000,000} - {0}", that, bufPerFile.Length);
  73. }
  74. solidBytes = msAllBytes.ToArray();
  75. }
  76. Console.WriteLine("Total uncompressed data: {0:N0}", solidBytes.Length);
  77. Console.WriteLine("Solid compressing...");
  78. var bufCompressed = Loader.Program.CompressSevenZipSharpLzma(solidBytes);
  79. Console.WriteLine("Before: {0:N0} -- After: {1:N0} -- Ratio:{2:P}", solidBytes.Length, bufCompressed.Length, bufCompressed.Length / (float)solidBytes.Length);
  80. Console.WriteLine("Building output start.");
  81. var icon = CoffUtility.GetIconFromFile(mainAssemblyFullpath);
  82. if (icon == null)
  83. {
  84. Console.WriteLine("WARNING: Fail to get icon from target");
  85. }
  86. using (
  87. TempFile tfCompressLibrary = new TempFile(Resources.SevenZipSharp, "SevenZipSharp")
  88. , tfCompressLibraryGz = new TempFile(Resources.SevenZipSharp.GzipCompress(), "SevenZipSharp.gz")
  89. , tfIcon = new TempFile(icon, "Icon.ico")
  90. )
  91. {
  92. Console.WriteLine("Configing...");
  93. var config = LoaderConfigCoder.GenerateResult(
  94. //target assembly
  95. mainAssembly,
  96. //files (include target assembly)
  97. filesToPack.ToArray(),
  98. //packed(compressed) length
  99. bufCompressed.Length,
  100. //compress libarary & method
  101. Path.GetFileName(tfCompressLibraryGz.FilePath), Loader.Program.DecompressSevenZipSharpLzma,
  102. //compress library decompress method from resource
  103. Loader.Program.GzipDecompress,
  104. !isConsole
  105. );
  106. var lstCodes = new List<string>
  107. {
  108. config,
  109. Resources.Loader_Main,
  110. Resources.Loader_Utility,
  111. Resources.Loader_Decompresser_SevenZipSharp_Lzma,
  112. AssemblyInfoCoder.GenerateResult(mainAssembly)
  113. };
  114. var cp = new CompilerParameters();
  115. cp.ReferencedAssemblies.Add("System.dll");
  116. cp.ReferencedAssemblies.Add("System.Core.dll");
  117. cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
  118. cp.ReferencedAssemblies.Add(tfCompressLibrary.FilePath);
  119. cp.ReferencedAssemblies.Add(mainAssembly.Location);
  120. cp.EmbeddedResources.Add(tfCompressLibraryGz.FilePath);
  121. cp.GenerateInMemory = false;
  122. cp.GenerateExecutable = true;
  123. cp.OutputAssembly = outputAssemblyFilePath;
  124. cp.CompilerOptions = $"/optimize /platform:{targetPlatform} ";
  125. if (isConsole == false)
  126. {
  127. cp.CompilerOptions += "/t:winexe ";
  128. }
  129. if (tfIcon.IsValid)
  130. cp.CompilerOptions += "/win32icon:" + tfIcon.FilePath;
  131. Console.WriteLine("Compiling...");
  132. var providerOptions = new Dictionary<string, string>();
  133. providerOptions.Add("CompilerVersion", "v4.0");
  134. var cs = CodeDomProvider.CreateProvider("CSharp", providerOptions);
  135. var cr = cs.CompileAssemblyFromSource(cp, lstCodes.ToArray());
  136. if (cr.Errors.HasErrors)
  137. {
  138. Console.WriteLine("ERROR");
  139. foreach (CompilerError error in cr.Errors)
  140. {
  141. Console.WriteLine(error);
  142. }
  143. return;
  144. }
  145. Console.WriteLine("Append comperssed filepackage...");
  146. using (var fs = File.OpenWrite(cp.OutputAssembly))
  147. {
  148. fs.Seek(0, SeekOrigin.End);
  149. fs.Write(bufCompressed, 0, bufCompressed.Length);
  150. }
  151. Console.WriteLine("File closed: {0}", outputAssemblyFilePath);
  152. }
  153. Console.WriteLine("Done.");
  154. var finLen = new FileInfo(outputAssemblyFilePath).Length;
  155. Console.WriteLine("Finally File Length: {0:N0} -- Ratio with files:{1:P}", finLen, finLen / (float)solidBytes.Length);
  156. }
  157. }
  158. }