Preloader.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Mono.Cecil;
  7. using Mono.Cecil.Cil;
  8. using MethodAttributes = Mono.Cecil.MethodAttributes;
  9. namespace BepInEx.Bootstrap
  10. {
  11. internal static class Preloader
  12. {
  13. public static string ExecutablePath { get; private set; }
  14. internal static string CurrentExecutingAssemblyPath => Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace('/', '\\');
  15. internal static string CurrentExecutingAssemblyDirectoryPath => Path.GetDirectoryName(CurrentExecutingAssemblyPath);
  16. internal static string GameName => Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName);
  17. internal static string GameRootPath => Path.GetDirectoryName(ExecutablePath);
  18. internal static string ManagedPath => Path.Combine(GameRootPath, Path.Combine($"{GameName}_Data", "Managed"));
  19. public static void Main(string[] args)
  20. {
  21. ExecutablePath = args[0];
  22. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  23. try
  24. {
  25. AssemblyPatcher.AssemblyLoad += PatchEntrypoint;
  26. AssemblyPatcher.PatchAll(ManagedPath);
  27. }
  28. catch (Exception ex)
  29. {
  30. //File.WriteAllText("B:\\test.txt", ex.ToString());
  31. }
  32. }
  33. static void PatchEntrypoint(AssemblyDefinition assembly)
  34. {
  35. if (assembly.Name.Name == "UnityEngine")
  36. {
  37. using (AssemblyDefinition injected = AssemblyDefinition.ReadAssembly(CurrentExecutingAssemblyPath))
  38. {
  39. var originalInjectMethod = injected.MainModule.Types.First(x => x.Name == "Chainloader")
  40. .Methods.First(x => x.Name == "Initialize");
  41. var injectMethod = assembly.MainModule.ImportReference(originalInjectMethod);
  42. var sceneManager = assembly.MainModule.Types.First(x => x.Name == "Application");
  43. var voidType = assembly.MainModule.ImportReference(typeof(void));
  44. var cctor = new MethodDefinition(".cctor",
  45. MethodAttributes.Static
  46. | MethodAttributes.Private
  47. | MethodAttributes.HideBySig
  48. | MethodAttributes.SpecialName
  49. | MethodAttributes.RTSpecialName,
  50. voidType);
  51. var ilp = cctor.Body.GetILProcessor();
  52. ilp.Append(ilp.Create(OpCodes.Call, injectMethod));
  53. ilp.Append(ilp.Create(OpCodes.Ret));
  54. sceneManager.Methods.Add(cctor);
  55. }
  56. }
  57. }
  58. static Assembly LocalResolve(object sender, ResolveEventArgs args)
  59. {
  60. if (args.Name == "0Harmony, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null")
  61. return Assembly.LoadFile(Path.Combine(CurrentExecutingAssemblyDirectoryPath, "0Harmony.dll"));
  62. return null;
  63. }
  64. }
  65. }