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.

168 lines
5.8 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. #region Insert patch into UnityEngine.CoreModule.dll
  47. var unityPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "UnityEngine.CoreModule.dll");
  48. var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath);
  49. var unityModDef = unityAsmDef.MainModule;
  50. bool modified = false;
  51. foreach (var asmref in unityModDef.AssemblyReferences)
  52. {
  53. if (asmref.Name == cAsmName.Name)
  54. {
  55. if (asmref.Version != cAsmName.Version)
  56. {
  57. asmref.Version = cAsmName.Version;
  58. modified = true;
  59. }
  60. }
  61. }
  62. var application = unityModDef.GetType("UnityEngine", "Application");
  63. MethodDefinition cctor = null;
  64. foreach (var m in application.Methods)
  65. if (m.IsRuntimeSpecialName && m.Name == ".cctor")
  66. cctor = m;
  67. var cbs = unityModDef.Import(((Action)CreateBootstrapper).Method);
  68. if (cctor == null)
  69. {
  70. cctor = new MethodDefinition(".cctor", MethodAttributes.RTSpecialName | MethodAttributes.Static | MethodAttributes.SpecialName, unityModDef.TypeSystem.Void);
  71. application.Methods.Add(cctor);
  72. modified = true;
  73. var ilp = cctor.Body.GetILProcessor();
  74. ilp.Emit(OpCodes.Call, cbs);
  75. ilp.Emit(OpCodes.Ret);
  76. }
  77. else
  78. {
  79. var ilp = cctor.Body.GetILProcessor();
  80. for (int i = 0; i < Math.Min(2, cctor.Body.Instructions.Count); i++)
  81. {
  82. var ins = cctor.Body.Instructions[i];
  83. if (i == 0 && (ins.OpCode != OpCodes.Call || ins.Operand != cbs))
  84. {
  85. ilp.Replace(ins, ilp.Create(OpCodes.Call, cbs));
  86. modified = true;
  87. }
  88. if (i == 1 && ins.OpCode != OpCodes.Ret)
  89. {
  90. ilp.Replace(ins, ilp.Create(OpCodes.Ret));
  91. modified = true;
  92. }
  93. }
  94. }
  95. if (modified)
  96. unityAsmDef.Write(unityPath);
  97. #endregion
  98. }
  99. private static bool bootstrapped = false;
  100. private static void CreateBootstrapper()
  101. {
  102. if (bootstrapped) return;
  103. bootstrapped = true;
  104. // need to reinit streams singe Unity seems to redirect stdout
  105. Windows.WinConsole.InitializeStreams();
  106. var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
  107. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  108. }
  109. private static bool injected = false;
  110. public static void Inject()
  111. {
  112. if (!injected)
  113. {
  114. injected = true;
  115. Windows.WinConsole.Initialize();
  116. SetupLibraryLoading();
  117. var bootstrapper = new GameObject("Bootstrapper").AddComponent<Bootstrapper>();
  118. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  119. }
  120. }
  121. private static bool loadingDone = false;
  122. public static void SetupLibraryLoading()
  123. {
  124. if (loadingDone) return;
  125. loadingDone = true;
  126. #region Add Library load locations
  127. AppDomain.CurrentDomain.AssemblyResolve += LibLoader.AssemblyLibLoader;
  128. try
  129. {
  130. if (!SetDllDirectory(LibLoader.NativeDir))
  131. {
  132. libLoader.Warn("Unable to add native library path to load path");
  133. }
  134. }
  135. catch (Exception) { }
  136. #endregion
  137. }
  138. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  139. [return: MarshalAs(UnmanagedType.Bool)]
  140. static extern bool SetDllDirectory(string lpPathName);
  141. private static void Bootstrapper_Destroyed()
  142. {
  143. PluginComponent.Create();
  144. }
  145. }
  146. }