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} - compress all file of source dir", Assembly.GetExecutingAssembly().GetName().Name); Console.WriteLine("Useage: {0} - compress only 1 file", Assembly.GetExecutingAssembly().GetName().Name); Console.WriteLine("Useage: {0} - 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(); 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 { 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(); 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); } } }