ProxyAssemblyGenerator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. System.Threading.Thread.Sleep(5000);
  76. if (Directory.Exists(Preloader.IL2CPPUnhollowedPath))
  77. Directory.Delete(Preloader.IL2CPPUnhollowedPath, true);
  78. Directory.CreateDirectory(Preloader.IL2CPPUnhollowedPath);
  79. string tempDumperDirectory = Path.Combine(Preloader.IL2CPPUnhollowedPath, "temp");
  80. Directory.CreateDirectory(tempDumperDirectory);
  81. var dumperConfig = new Config
  82. {
  83. GenerateScript = false,
  84. GenerateDummyDll = true
  85. };
  86. listener.DoPreloaderLog("Generating Il2CppDumper intermediate assemblies", LogLevel.Info);
  87. Il2CppDumper.Il2CppDumper.PerformDump(GameAssemblyPath,
  88. Path.Combine(Paths.GameRootPath, $"{Paths.ProcessName}_Data", "il2cpp_data", "Metadata", "global-metadata.dat"),
  89. tempDumperDirectory,
  90. dumperConfig,
  91. s => listener.DoDumperLog(s, LogLevel.Debug));
  92. listener.DoPreloaderLog("Executing Il2CppUnhollower generator", LogLevel.Info);
  93. System.Threading.Thread.Sleep(5000);
  94. UnhollowerBaseLib.LogSupport.InfoHandler += s => listener.DoUnhollowerLog(s, LogLevel.Info);
  95. UnhollowerBaseLib.LogSupport.WarningHandler += s => listener.DoUnhollowerLog(s, LogLevel.Warning);
  96. UnhollowerBaseLib.LogSupport.TraceHandler += s => listener.DoUnhollowerLog(s, LogLevel.Debug);
  97. UnhollowerBaseLib.LogSupport.ErrorHandler += s => listener.DoUnhollowerLog(s, LogLevel.Error);
  98. var unhollowerOptions = new UnhollowerOptions
  99. {
  100. GameAssemblyPath = GameAssemblyPath,
  101. MscorlibPath = Path.Combine(Paths.GameRootPath, "mono", "Managed", "mscorlib.dll"),
  102. SourceDir = Path.Combine(tempDumperDirectory, "DummyDll"),
  103. OutputDir = Preloader.IL2CPPUnhollowedPath
  104. };
  105. AssemblyUnhollower.Program.Main(unhollowerOptions);
  106. }
  107. }
  108. }
  109. }