123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- 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()
- {
-
- 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);
-
- private static void RealMain(string[] args)
- {
- var pname = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
- try
- {
- pname = Application.ProductName;
- }
- catch
- {
-
- }
- 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(),
-
- 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);
-
- if (Directory.Exists(fillFileDir) == false)
- {
- Directory.CreateDirectory(fillFileDir);
- }
-
- File.WriteAllBytes(fullFilePath, bufPerFile);
- }
- }
- }
-
-
-
-
- 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));
- }
-
- private static void Execute(string[] args)
- {
- var ep = _targetAssembly.EntryPoint;
- ep.Invoke(null, ep.GetParameters().Length == 0 ? null : new object[] { args });
- }
- }
- }
|