ProxyAssemblyGenerator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. Directory.CreateDirectory(Preloader.IL2CPPUnhollowedPath);
  76. foreach (var dllFile in Directory.EnumerateFiles(Preloader.IL2CPPUnhollowedPath, "*.dll", SearchOption.TopDirectoryOnly))
  77. File.Delete(dllFile);
  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. string unityBaseLibDir = Path.Combine(Preloader.IL2CPPUnhollowedPath, "base");
  97. if (Directory.Exists(unityBaseLibDir))
  98. {
  99. listener.DoPreloaderLog("Found base unity libraries", LogLevel.Debug);
  100. }
  101. else
  102. {
  103. unityBaseLibDir = null;
  104. }
  105. var unhollowerOptions = new UnhollowerOptions
  106. {
  107. GameAssemblyPath = GameAssemblyPath,
  108. MscorlibPath = Path.Combine(Paths.GameRootPath, "mono", "Managed", "mscorlib.dll"),
  109. SourceDir = Path.Combine(tempDumperDirectory, "DummyDll"),
  110. OutputDir = Preloader.IL2CPPUnhollowedPath,
  111. UnityBaseLibsDir = unityBaseLibDir,
  112. NoCopyUnhollowerLibs = true
  113. };
  114. AssemblyUnhollower.Program.Main(unhollowerOptions);
  115. }
  116. }
  117. }
  118. }