Loader.Main.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. // ReSharper disable once CheckNamespace
  8. namespace Loader
  9. {
  10. public class PackedFileEntry
  11. {
  12. public PackedFileEntry(string path, int length)
  13. {
  14. Path = path;
  15. Length = length;
  16. }
  17. public string Path { get; private set; }
  18. public int Length { get; private set; }
  19. public override string ToString()
  20. {
  21. // ReSharper disable once UseStringInterpolation
  22. return string.Format("new {0}(@\"{1}\",{2})", GetType().Name, Path, Length);
  23. }
  24. }
  25. partial class Program
  26. {
  27. private static string _targetAssemblyName;
  28. private static string _targetFileName;
  29. private static Assembly _targetAssembly;
  30. private static int _packageLength;
  31. private static byte[] _compressLibrary;
  32. private static string _tempPath;
  33. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  34. [return: MarshalAs(UnmanagedType.Bool)]
  35. private static extern bool SetDllDirectory(string lpPathName);
  36. // ReSharper disable once UnusedMember.Local
  37. private static void RealMain(string[] args)
  38. {
  39. var pname = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
  40. try
  41. {
  42. pname = Application.ProductName;
  43. }
  44. catch
  45. {
  46. //dont care! XP BULL SHIT
  47. }
  48. var frmLoading = new Form();
  49. frmLoading.StartPosition = FormStartPosition.CenterScreen;
  50. frmLoading.ControlBox = false;
  51. frmLoading.Text = "Loading... - " + pname;
  52. frmLoading.Width = 480;
  53. frmLoading.Height = 128;
  54. frmLoading.FormBorderStyle = FormBorderStyle.FixedSingle;
  55. frmLoading.Show();
  56. Application.DoEvents();
  57. Init();
  58. Unpack();
  59. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  60. Load();
  61. frmLoading.Close();
  62. Application.DoEvents();
  63. Execute(args);
  64. }
  65. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  66. {
  67. if (args.Name == _targetAssemblyName)
  68. {
  69. return _targetAssembly;
  70. }
  71. return null;
  72. }
  73. private static void Unpack()
  74. {
  75. var bufPackageCompressed = new byte[_packageLength];
  76. using (
  77. var fs = new FileStream(Assembly.GetExecutingAssembly().Location, FileMode.Open, FileAccess.Read, FileShare.Read)
  78. )
  79. {
  80. fs.Seek(-_packageLength, SeekOrigin.End);
  81. fs.Read(bufPackageCompressed, 0, bufPackageCompressed.Length);
  82. }
  83. _tempPath = Path.Combine(
  84. Path.GetTempPath(),
  85. // ReSharper disable once UseStringInterpolation
  86. string.Format("Aio1ef-{0}-{1}-{2}", new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime.Ticks.ToString("X16"), _targetFileName, HashData(bufPackageCompressed))
  87. );
  88. if (!Directory.Exists(_tempPath) || !PackedFiles.All(p => File.Exists(Path.Combine(_tempPath + p.Path))))
  89. {
  90. var bufPackage = DecompressData(bufPackageCompressed);
  91. Directory.CreateDirectory(_tempPath);
  92. using (var ms = new MemoryStream(bufPackage))
  93. {
  94. foreach (var entry in PackedFiles)
  95. {
  96. var bufPerFile = new byte[entry.Length];
  97. ms.Read(bufPerFile, 0, bufPerFile.Length);
  98. var fullFilePath = Path.Combine(_tempPath, entry.Path);
  99. var fillFileDir = Path.GetDirectoryName(fullFilePath);
  100. // ReSharper disable AssignNullToNotNullAttribute
  101. if (Directory.Exists(fillFileDir) == false)
  102. {
  103. Directory.CreateDirectory(fillFileDir);
  104. }
  105. // ReSharper restore AssignNullToNotNullAttribute
  106. File.WriteAllBytes(fullFilePath, bufPerFile);
  107. }
  108. }
  109. }
  110. //else
  111. //{
  112. // //cached
  113. //}
  114. SetDllDirectory(_tempPath);
  115. Environment.SetEnvironmentVariable("path", _tempPath + ";" + Environment.GetEnvironmentVariable("path"));
  116. Environment.CurrentDirectory = _tempPath;
  117. AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = _tempPath;
  118. }
  119. private static void Load()
  120. {
  121. _targetAssembly = Assembly.LoadFrom(Path.Combine(_tempPath, _targetFileName));
  122. }
  123. // ReSharper disable once SuggestBaseTypeForParameter
  124. private static void Execute(string[] args)
  125. {
  126. var ep = _targetAssembly.EntryPoint;
  127. ep.Invoke(null, ep.GetParameters().Length == 0 ? null : new object[] { args });
  128. }
  129. }
  130. }