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.

179 lines
6.3 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. {
  98. unityAsmDef.Write(unityPath);
  99. }
  100. #endregion
  101. loader.Debug("Ensuring Assembly-CSharp is virtualized");
  102. #region Virtualize Assembly-CSharp.dll
  103. var ascPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "Assembly-CSharp.dll");
  104. var ascModule = VirtualizedModule.Load(ascPath);
  105. ascModule.Virtualize(cAsmName);
  106. #endregion
  107. }
  108. private static bool bootstrapped = false;
  109. private static void CreateBootstrapper()
  110. {
  111. if (bootstrapped) return;
  112. bootstrapped = true;
  113. // need to reinit streams singe Unity seems to redirect stdout
  114. Windows.WinConsole.InitializeStreams();
  115. var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
  116. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  117. }
  118. private static bool injected = false;
  119. public static void Inject()
  120. {
  121. if (!injected)
  122. {
  123. injected = true;
  124. Windows.WinConsole.Initialize();
  125. SetupLibraryLoading();
  126. var bootstrapper = new GameObject("Bootstrapper").AddComponent<Bootstrapper>();
  127. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  128. }
  129. }
  130. private static bool loadingDone = false;
  131. public static void SetupLibraryLoading()
  132. {
  133. if (loadingDone) return;
  134. loadingDone = true;
  135. #region Add Library load locations
  136. AppDomain.CurrentDomain.AssemblyResolve += LibLoader.AssemblyLibLoader;
  137. try
  138. {
  139. if (!SetDllDirectory(LibLoader.NativeDir))
  140. {
  141. libLoader.Warn("Unable to add native library path to load path");
  142. }
  143. }
  144. catch (Exception) { }
  145. #endregion
  146. }
  147. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  148. [return: MarshalAs(UnmanagedType.Bool)]
  149. static extern bool SetDllDirectory(string lpPathName);
  150. private static void Bootstrapper_Destroyed()
  151. {
  152. PluginComponent.Create();
  153. }
  154. }
  155. }