You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using Harmony;
  2. using IPA.Loader;
  3. using IPA.Logging;
  4. using Mono.Cecil;
  5. using Mono.Cecil.Cil;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Runtime.InteropServices;
  11. using UnityEngine;
  12. using static IPA.Logging.Logger;
  13. using Logger = IPA.Logging.Logger;
  14. using MethodAttributes = Mono.Cecil.MethodAttributes;
  15. namespace IPA.Injector
  16. {
  17. public static class Injector
  18. {
  19. public static void Main(string[] args)
  20. { // entry point for doorstop
  21. // At this point, literally nothing but mscorlib is loaded,
  22. // and since this class doesn't have any static fields that
  23. // aren't defined in mscorlib, we can control exactly what
  24. // gets loaded.
  25. try
  26. {
  27. // This loads System.Runtime.InteropServices, and Microsoft.Win32.SafeHandles.
  28. Windows.WinConsole.Initialize();
  29. // This loads AppDomain, System.IO, System.Collections.Generic, and System.Reflection.
  30. // If kernel32.dll is not already loaded, this will also load it.
  31. // This call also loads IPA.Loader and initializes the logging system. In the process
  32. // it loads Ionic.Zip.
  33. SetupLibraryLoading();
  34. loader.Debug("Prepping bootstrapper");
  35. // // This will load Mono.Cecil
  36. InstallBootstrapPatch();
  37. }
  38. catch (Exception e)
  39. {
  40. Console.WriteLine(e);
  41. }
  42. }
  43. private static void InstallBootstrapPatch()
  44. {
  45. var cAsmName = Assembly.GetExecutingAssembly().GetName();
  46. loader.Debug("Ensuring patch on UnityEngine.CoreModule exists");
  47. #region Insert patch into UnityEngine.CoreModule.dll
  48. var unityPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "UnityEngine.CoreModule.dll");
  49. var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath);
  50. var unityModDef = unityAsmDef.MainModule;
  51. bool modified = false;
  52. foreach (var asmref in unityModDef.AssemblyReferences)
  53. {
  54. if (asmref.Name == cAsmName.Name)
  55. {
  56. if (asmref.Version != cAsmName.Version)
  57. {
  58. asmref.Version = cAsmName.Version;
  59. modified = true;
  60. }
  61. }
  62. }
  63. var application = unityModDef.GetType("UnityEngine", "Application");
  64. MethodDefinition cctor = null;
  65. foreach (var m in application.Methods)
  66. if (m.IsRuntimeSpecialName && m.Name == ".cctor")
  67. cctor = m;
  68. var cbs = unityModDef.Import(((Action)CreateBootstrapper).Method);
  69. if (cctor == null)
  70. {
  71. cctor = new MethodDefinition(".cctor", MethodAttributes.RTSpecialName | MethodAttributes.Static | MethodAttributes.SpecialName, unityModDef.TypeSystem.Void);
  72. application.Methods.Add(cctor);
  73. modified = true;
  74. var ilp = cctor.Body.GetILProcessor();
  75. ilp.Emit(OpCodes.Call, cbs);
  76. ilp.Emit(OpCodes.Ret);
  77. }
  78. else
  79. {
  80. var ilp = cctor.Body.GetILProcessor();
  81. for (int i = 0; i < Math.Min(2, cctor.Body.Instructions.Count); i++)
  82. {
  83. var ins = cctor.Body.Instructions[i];
  84. if (i == 0 && (ins.OpCode != OpCodes.Call || ins.Operand != cbs))
  85. {
  86. ilp.Replace(ins, ilp.Create(OpCodes.Call, cbs));
  87. modified = true;
  88. }
  89. if (i == 1 && ins.OpCode != OpCodes.Ret)
  90. {
  91. ilp.Replace(ins, ilp.Create(OpCodes.Ret));
  92. modified = true;
  93. }
  94. }
  95. }
  96. if (modified)
  97. unityAsmDef.Write(unityPath);
  98. #endregion
  99. loader.Debug("Ensuring Assembly-CSharp is virtualized");
  100. #region Virtualize Assembly-CSharp.dll
  101. var ascPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "Assembly-CSharp.dll");
  102. var ascModule = VirtualizedModule.Load(ascPath);
  103. ascModule.Virtualize(cAsmName);
  104. #endregion
  105. }
  106. private static bool bootstrapped = false;
  107. private static void CreateBootstrapper()
  108. {
  109. if (bootstrapped) return;
  110. bootstrapped = true;
  111. // need to reinit streams singe Unity seems to redirect stdout
  112. Windows.WinConsole.InitializeStreams();
  113. var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
  114. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  115. }
  116. private static bool injected = false;
  117. public static void Inject()
  118. {
  119. if (!injected)
  120. {
  121. injected = true;
  122. Windows.WinConsole.Initialize();
  123. SetupLibraryLoading();
  124. var bootstrapper = new GameObject("Bootstrapper").AddComponent<Bootstrapper>();
  125. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  126. }
  127. }
  128. private static bool loadingDone = false;
  129. public static void SetupLibraryLoading()
  130. {
  131. if (loadingDone) return;
  132. loadingDone = true;
  133. #region Add Library load locations
  134. AppDomain.CurrentDomain.AssemblyResolve += LibLoader.AssemblyLibLoader;
  135. try
  136. {
  137. if (!SetDllDirectory(LibLoader.NativeDir))
  138. {
  139. libLoader.Warn("Unable to add native library path to load path");
  140. }
  141. }
  142. catch (Exception) { }
  143. #endregion
  144. }
  145. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  146. [return: MarshalAs(UnmanagedType.Bool)]
  147. static extern bool SetDllDirectory(string lpPathName);
  148. private static void Bootstrapper_Destroyed()
  149. {
  150. PluginComponent.Create();
  151. }
  152. }
  153. }