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.

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