123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using Aio1Ef.Packer.Common;
- using Aio1Ef.Packer.Common.Coders;
- using Aio1Ef.Packer.Properties;
- using Loader;
- using System;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- namespace Aio1Ef.Packer
- {
- internal static class Program
- {
- private static void Main(string[] args)
- {
- string sourceDir;
- string mainAssemblyFilename;
- string outputAssemblyFilePath;
- switch (args.Length)
- {
- case 3:
- sourceDir = args[0];
- mainAssemblyFilename = args[1];
- outputAssemblyFilePath = args[2];
- break;
- case 2:
- sourceDir = null;
- mainAssemblyFilename = args[0];
- outputAssemblyFilePath = args[1];
- break;
- case 1:
- sourceDir = null;
- mainAssemblyFilename = args[0];
- // ReSharper disable once AssignNullToNotNullAttribute
- outputAssemblyFilePath = Path.Combine(Path.GetDirectoryName(mainAssemblyFilename), Path.GetFileNameWithoutExtension(mainAssemblyFilename) + ".Aio1Ef" + Path.GetExtension(mainAssemblyFilename));
- break;
- default:
- Console.WriteLine("Useage: {0} <sourceDir> <mainAsm> <outputAsm> - compress all file of source dir", Assembly.GetExecutingAssembly().GetName().Name);
- Console.WriteLine("Useage: {0} <mainAsm> <outputAsm> - compress only 1 file", Assembly.GetExecutingAssembly().GetName().Name);
- Console.WriteLine("Useage: {0} <mainAsm> - compress that to .Aio1Ef", Assembly.GetExecutingAssembly().GetName().Name);
- return;
- }
- //---------
- var mainAssemblyFullpath = sourceDir != null
- ? Path.Combine(sourceDir, mainAssemblyFilename)
- : mainAssemblyFilename;
- var isConsole = CoffUtility.AssemblyUsesConsoleSubsystem(mainAssemblyFullpath);
- var mainAssembly = Assembly.LoadFrom(mainAssemblyFullpath);
- var targetPlatform = CoffUtility.GetTargetPlatform(mainAssembly);
- var filesToPack = new List<PackedFileEntry>();
- byte[] solidBytes;
- using (var msAllBytes = new MemoryStream())
- {
- if (sourceDir != null)
- {
- var fromFullpath = Path.GetFullPath(sourceDir);
- foreach (var file in Directory.GetFiles(fromFullpath, "*", SearchOption.AllDirectories))
- {
- var bufPerFile = File.ReadAllBytes(file);
- msAllBytes.Write(bufPerFile, 0, bufPerFile.Length);
- var that = file.Substring(fromFullpath.Length, file.Length - fromFullpath.Length).Trim(Path.DirectorySeparatorChar);
- filesToPack.Add(new PackedFileEntry(that, bufPerFile.Length));
- Console.WriteLine("Loaded: Size: {1:000,000,000} - {0}", that, bufPerFile.Length);
- }
- }
- else
- {
- var bufPerFile = File.ReadAllBytes(mainAssemblyFilename);
- msAllBytes.Write(bufPerFile, 0, bufPerFile.Length);
- var that = Path.GetFileName(mainAssemblyFilename);
- filesToPack.Add(new PackedFileEntry(that, bufPerFile.Length));
- Console.WriteLine("Loaded: Size: {1:000,000,000} - {0}", that, bufPerFile.Length);
- }
- solidBytes = msAllBytes.ToArray();
- }
- Console.WriteLine("Total uncompressed data: {0:N0}", solidBytes.Length);
- Console.WriteLine("Solid compressing...");
- var bufCompressed = Loader.Program.CompressSevenZipSharpLzma(solidBytes);
- Console.WriteLine("Before: {0:N0} -- After: {1:N0} -- Ratio:{2:P}", solidBytes.Length, bufCompressed.Length, bufCompressed.Length / (float)solidBytes.Length);
- Console.WriteLine("Building output start.");
- var icon = CoffUtility.GetIconFromFile(mainAssemblyFullpath);
- if (icon == null)
- {
- Console.WriteLine("WARNING: Fail to get icon from target");
- }
- using (
- TempFile tfCompressLibrary = new TempFile(Resources.SevenZipSharp, "SevenZipSharp")
- , tfCompressLibraryGz = new TempFile(Resources.SevenZipSharp.GzipCompress(), "SevenZipSharp.gz")
- , tfIcon = new TempFile(icon, "Icon.ico")
- )
- {
- Console.WriteLine("Configing...");
- var config = LoaderConfigCoder.GenerateResult(
- //target assembly
- mainAssembly,
- //files (include target assembly)
- filesToPack.ToArray(),
- //packed(compressed) length
- bufCompressed.Length,
- //compress libarary & method
- Path.GetFileName(tfCompressLibraryGz.FilePath), Loader.Program.DecompressSevenZipSharpLzma,
- //compress library decompress method from resource
- Loader.Program.GzipDecompress,
- !isConsole
- );
- var lstCodes = new List<string>
- {
- config,
- Resources.Loader_Main,
- Resources.Loader_Utility,
- Resources.Loader_Decompresser_SevenZipSharp_Lzma,
- AssemblyInfoCoder.GenerateResult(mainAssembly)
- };
- var cp = new CompilerParameters();
- cp.ReferencedAssemblies.Add("System.dll");
- cp.ReferencedAssemblies.Add("System.Core.dll");
- cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
- cp.ReferencedAssemblies.Add(tfCompressLibrary.FilePath);
- cp.ReferencedAssemblies.Add(mainAssembly.Location);
- cp.EmbeddedResources.Add(tfCompressLibraryGz.FilePath);
- cp.GenerateInMemory = false;
- cp.GenerateExecutable = true;
- cp.OutputAssembly = outputAssemblyFilePath;
- cp.CompilerOptions = $"/optimize /platform:{targetPlatform} ";
-
- if (isConsole == false)
- {
- cp.CompilerOptions += "/t:winexe ";
- }
- if (tfIcon.IsValid)
- cp.CompilerOptions += "/win32icon:" + tfIcon.FilePath;
- Console.WriteLine("Compiling...");
- var providerOptions = new Dictionary<string, string>();
- providerOptions.Add("CompilerVersion", "v4.0");
- var cs = CodeDomProvider.CreateProvider("CSharp", providerOptions);
- var cr = cs.CompileAssemblyFromSource(cp, lstCodes.ToArray());
- if (cr.Errors.HasErrors)
- {
- Console.WriteLine("ERROR");
- foreach (CompilerError error in cr.Errors)
- {
- Console.WriteLine(error);
- }
- return;
- }
- Console.WriteLine("Append comperssed filepackage...");
- using (var fs = File.OpenWrite(cp.OutputAssembly))
- {
- fs.Seek(0, SeekOrigin.End);
- fs.Write(bufCompressed, 0, bufCompressed.Length);
- }
- Console.WriteLine("File closed: {0}", outputAssemblyFilePath);
- }
- Console.WriteLine("Done.");
- var finLen = new FileInfo(outputAssemblyFilePath).Length;
- Console.WriteLine("Finally File Length: {0:N0} -- Ratio with files:{1:P}", finLen, finLen / (float)solidBytes.Length);
- }
- }
- }
|