ProxyAssemblyGenerator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using AssemblyUnhollower;
  5. using BepInEx.Logging;
  6. using Il2CppDumper;
  7. namespace BepInEx.IL2CPP
  8. {
  9. internal static class ProxyAssemblyGenerator
  10. {
  11. public static string GameAssemblyPath => Path.Combine(Paths.GameRootPath, "GameAssembly.dll");
  12. private static string HashPath => Path.Combine(Preloader.IL2CPPUnhollowedPath, "assembly-hash.txt");
  13. private static string tempDumperDirectory => Path.Combine(Preloader.IL2CPPUnhollowedPath, "temp");
  14. private static ManualLogSource Il2cppDumperLogger = Logger.CreateLogSource("Il2CppDumper");
  15. private static string ComputeGameAssemblyHash()
  16. {
  17. using var md5 = MD5.Create();
  18. using var assemblyStream = File.OpenRead(GameAssemblyPath);
  19. var hash = md5.ComputeHash(assemblyStream);
  20. return Utility.ByteArrayToString(hash);
  21. }
  22. public static bool CheckIfGenerationRequired()
  23. {
  24. if (!Directory.Exists(Preloader.IL2CPPUnhollowedPath))
  25. return true;
  26. if (!File.Exists(HashPath))
  27. return true;
  28. if (ComputeGameAssemblyHash() != File.ReadAllText(HashPath))
  29. {
  30. Preloader.Log.LogInfo("Detected a game update, will regenerate proxy assemblies");
  31. return true;
  32. }
  33. return false;
  34. }
  35. public static void GenerateAssemblies()
  36. {
  37. var domain = AppDomain.CreateDomain("GeneratorDomain", null, new AppDomainSetup
  38. {
  39. ApplicationBase = Paths.BepInExAssemblyDirectory
  40. });
  41. var runner = (AppDomainRunner)domain.CreateInstanceAndUnwrap(typeof(AppDomainRunner).Assembly.FullName, typeof(AppDomainRunner).FullName);
  42. runner.Setup(Paths.ExecutablePath, Preloader.IL2CPPUnhollowedPath);
  43. runner.GenerateAssembliesInternal(new AppDomainListener());
  44. AppDomain.Unload(domain);
  45. Directory.Delete(tempDumperDirectory, true);
  46. File.WriteAllText(HashPath, ComputeGameAssemblyHash());
  47. }
  48. [Serializable]
  49. private class AppDomainListener : MarshalByRefObject
  50. {
  51. public void DoPreloaderLog(object data, LogLevel level)
  52. {
  53. Preloader.Log.Log(level, data);
  54. }
  55. public void DoDumperLog(object data, LogLevel level)
  56. {
  57. Il2cppDumperLogger.Log(level, data);
  58. }
  59. public void DoUnhollowerLog(object data, LogLevel level)
  60. {
  61. Preloader.UnhollowerLog.Log(level, data);
  62. }
  63. }
  64. [Serializable]
  65. private class AppDomainRunner : MarshalByRefObject
  66. {
  67. public void Setup(string executablePath, string unhollowedPath)
  68. {
  69. Paths.SetExecutablePath(executablePath);
  70. Preloader.IL2CPPUnhollowedPath = unhollowedPath;
  71. }
  72. public void GenerateAssembliesInternal(AppDomainListener listener)
  73. {
  74. listener.DoPreloaderLog("Generating Il2CppUnhollower assemblies", LogLevel.Message);
  75. if (Directory.Exists(Preloader.IL2CPPUnhollowedPath))
  76. Directory.Delete(Preloader.IL2CPPUnhollowedPath, true);
  77. Directory.CreateDirectory(Preloader.IL2CPPUnhollowedPath);
  78. string tempDumperDirectory = Path.Combine(Preloader.IL2CPPUnhollowedPath, "temp");
  79. Directory.CreateDirectory(tempDumperDirectory);
  80. var dumperConfig = new Config
  81. {
  82. GenerateScript = false,
  83. GenerateDummyDll = true
  84. };
  85. listener.DoPreloaderLog("Generating Il2CppDumper intermediate assemblies", LogLevel.Info);
  86. Il2CppDumper.Il2CppDumper.PerformDump(GameAssemblyPath,
  87. Path.Combine(Paths.GameRootPath, $"{Paths.ProcessName}_Data", "il2cpp_data", "Metadata", "global-metadata.dat"),
  88. tempDumperDirectory,
  89. dumperConfig,
  90. s => listener.DoDumperLog(s, LogLevel.Debug));
  91. listener.DoPreloaderLog("Executing Il2CppUnhollower generator", LogLevel.Info);
  92. UnhollowerBaseLib.LogSupport.InfoHandler += s => listener.DoUnhollowerLog(s, LogLevel.Info);
  93. UnhollowerBaseLib.LogSupport.WarningHandler += s => listener.DoUnhollowerLog(s, LogLevel.Warning);
  94. UnhollowerBaseLib.LogSupport.TraceHandler += s => listener.DoUnhollowerLog(s, LogLevel.Debug);
  95. UnhollowerBaseLib.LogSupport.ErrorHandler += s => listener.DoUnhollowerLog(s, LogLevel.Error);
  96. var unhollowerOptions = new UnhollowerOptions
  97. {
  98. GameAssemblyPath = GameAssemblyPath,
  99. MscorlibPath = Path.Combine(Paths.GameRootPath, "mono", "Managed", "mscorlib.dll"),
  100. SourceDir = Path.Combine(tempDumperDirectory, "DummyDll"),
  101. OutputDir = Preloader.IL2CPPUnhollowedPath
  102. };
  103. AssemblyUnhollower.Program.Main(unhollowerOptions);
  104. }
  105. }
  106. }
  107. }