public static void RunAttachedExe(string[] args) { if (!IsExeAttached) { throw new Exception("exe is not attached."); } NZ fsz = GetPack(); RegisterAssemblyResolver(fsz); var exeBytes = fsz.GetFileBytes(ExeName); var exeAssembly = Assembly.Load(exeBytes); var entryPoint = exeAssembly.EntryPoint; var flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; var isPackedField = entryPoint.DeclaringType.GetField("__magic_is_packed", flags); var pkgField = entryPoint.DeclaringType.GetField("__magic_files", flags); if (isPackedField != null && isPackedField.FieldType == typeof(bool)) { isPackedField.SetValue(null, true); } if (pkgField != null && pkgField.FieldType == typeof(IDictionary <string, Func <byte[]> >)) { var fszDict = new Dictionary <string, Func <byte[]> >(); foreach (var item in fsz.GetFiles()) { fszDict.Add(item.name, () => fsz.GetFileBytes(item)); } pkgField.SetValue(null, fszDict); } entryPoint.Invoke(null, new object[] { args }); }
private static void RegisterAssemblyResolver(NZ fsz) { var asms = new Dictionary <string, NZFileinfo>(); foreach (var item in fsz.GetFiles()) { if (item.name != ExeName && item.name.StartsWith(NamePrefix)) { asms.Add(item.name.Substring(NamePrefix.Length), item); } } AppDomain.CurrentDomain.AssemblyResolve += (s, arg) => { if (!asms.TryGetValue(arg.Name, out var nfi)) { //Console.Error.WriteLine($"NZip: assembly '{arg.Name}' not found"); return(null); } return(Assembly.Load(fsz.GetFileBytes(nfi))); }; }