using System; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; // ReSharper disable once CheckNamespace namespace Loader { public class PackedFileEntry { public PackedFileEntry(string path, int length) { Path = path; Length = length; } public string Path { get; private set; } public int Length { get; private set; } public override string ToString() { // ReSharper disable once UseStringInterpolation return string.Format("new {0}(@\"{1}\",{2})", GetType().Name, Path, Length); } } partial class Program { private static string _targetAssemblyName; private static string _targetFileName; private static Assembly _targetAssembly; private static int _packageLength; private static byte[] _compressLibrary; private static string _tempPath; [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetDllDirectory(string lpPathName); // ReSharper disable once UnusedMember.Local private static void RealMain(string[] args) { var pname = Path.GetFileNameWithoutExtension(Application.ExecutablePath); try { pname = Application.ProductName; } catch { //dont care! XP BULL SHIT } var frmLoading = new Form(); frmLoading.StartPosition = FormStartPosition.CenterScreen; frmLoading.ControlBox = false; frmLoading.Text = "Loading... - " + pname; frmLoading.Width = 480; frmLoading.Height = 128; frmLoading.FormBorderStyle = FormBorderStyle.FixedSingle; frmLoading.Show(); Application.DoEvents(); Init(); Unpack(); AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Load(); frmLoading.Close(); Application.DoEvents(); Execute(args); } private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name == _targetAssemblyName) { return _targetAssembly; } return null; } private static void Unpack() { var bufPackageCompressed = new byte[_packageLength]; using ( var fs = new FileStream(Assembly.GetExecutingAssembly().Location, FileMode.Open, FileAccess.Read, FileShare.Read) ) { fs.Seek(-_packageLength, SeekOrigin.End); fs.Read(bufPackageCompressed, 0, bufPackageCompressed.Length); } _tempPath = Path.Combine( Path.GetTempPath(), // ReSharper disable once UseStringInterpolation string.Format("Aio1ef-{0}-{1}-{2}", new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime.Ticks.ToString("X16"), _targetFileName, HashData(bufPackageCompressed)) ); if (!Directory.Exists(_tempPath) || !PackedFiles.All(p => File.Exists(Path.Combine(_tempPath + p.Path)))) { var bufPackage = DecompressData(bufPackageCompressed); Directory.CreateDirectory(_tempPath); using (var ms = new MemoryStream(bufPackage)) { foreach (var entry in PackedFiles) { var bufPerFile = new byte[entry.Length]; ms.Read(bufPerFile, 0, bufPerFile.Length); var fullFilePath = Path.Combine(_tempPath, entry.Path); var fillFileDir = Path.GetDirectoryName(fullFilePath); // ReSharper disable AssignNullToNotNullAttribute if (Directory.Exists(fillFileDir) == false) { Directory.CreateDirectory(fillFileDir); } // ReSharper restore AssignNullToNotNullAttribute File.WriteAllBytes(fullFilePath, bufPerFile); } } } //else //{ // //cached //} SetDllDirectory(_tempPath); Environment.SetEnvironmentVariable("path", _tempPath + ";" + Environment.GetEnvironmentVariable("path")); Environment.CurrentDirectory = _tempPath; AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = _tempPath; } private static void Load() { _targetAssembly = Assembly.LoadFrom(Path.Combine(_tempPath, _targetFileName)); } // ReSharper disable once SuggestBaseTypeForParameter private static void Execute(string[] args) { var ep = _targetAssembly.EntryPoint; ep.Invoke(null, ep.GetParameters().Length == 0 ? null : new object[] { args }); } } }