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.

211 lines
6.5 KiB

  1. using Mono.Cecil;
  2. using Mono.Cecil.Rocks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Reflection;
  7. namespace IPA.Injector
  8. {
  9. internal class VirtualizedModule : IDisposable
  10. {
  11. private readonly FileInfo file;
  12. private ModuleDefinition module;
  13. public static VirtualizedModule Load(string engineFile)
  14. {
  15. return new VirtualizedModule(engineFile);
  16. }
  17. private VirtualizedModule(string assemblyFile)
  18. {
  19. file = new FileInfo(assemblyFile);
  20. LoadModules();
  21. }
  22. private void LoadModules()
  23. {
  24. module = ModuleDefinition.ReadModule(file.FullName, new ReaderParameters
  25. {
  26. ReadWrite = false,
  27. InMemory = true,
  28. ReadingMode = ReadingMode.Immediate
  29. });
  30. }
  31. public void Virtualize(AssemblyName selfName, Action beforeChangeCallback = null)
  32. {
  33. var changed = false;
  34. var virtualize = true;
  35. foreach (var r in module.AssemblyReferences)
  36. {
  37. if (r.Name == selfName.Name)
  38. {
  39. virtualize = false;
  40. if (r.Version != selfName.Version)
  41. {
  42. r.Version = selfName.Version;
  43. changed = true;
  44. }
  45. }
  46. }
  47. if (virtualize)
  48. {
  49. changed = true;
  50. module.AssemblyReferences.Add(new AssemblyNameReference(selfName.Name, selfName.Version));
  51. foreach (var type in module.Types)
  52. {
  53. VirtualizeType(type);
  54. }
  55. }
  56. if (changed)
  57. {
  58. beforeChangeCallback?.Invoke();
  59. module.Write(file.FullName);
  60. }
  61. }
  62. private TypeReference inModreqRef;
  63. private TypeReference outModreqRef;
  64. private void VirtualizeType(TypeDefinition type)
  65. {
  66. if(type.IsSealed)
  67. {
  68. // Unseal
  69. type.IsSealed = false;
  70. }
  71. if (type.IsInterface) return;
  72. if (type.IsAbstract) return;
  73. // These two don't seem to work.
  74. if (type.Name == "SceneControl" || type.Name == "ConfigUI") return;
  75. // Take care of sub types
  76. foreach (var subType in type.NestedTypes)
  77. {
  78. VirtualizeType(subType);
  79. }
  80. foreach (var method in type.Methods)
  81. {
  82. if (method.IsManaged
  83. && method.IsIL
  84. && !method.IsStatic
  85. && !method.IsVirtual
  86. && !method.IsAbstract
  87. && !method.IsAddOn
  88. && !method.IsConstructor
  89. && !method.IsSpecialName
  90. && !method.IsGenericInstance
  91. && !method.HasOverrides)
  92. {
  93. // fix In and Out parameters to have the modreqs required by the compiler
  94. foreach (var param in method.Parameters)
  95. {
  96. if (param.IsIn)
  97. {
  98. inModreqRef ??= module.ImportReference(typeof(System.Runtime.InteropServices.InAttribute));
  99. param.ParameterType = AddModreqIfNotExist(param.ParameterType, inModreqRef);
  100. }
  101. if (param.IsOut)
  102. {
  103. outModreqRef ??= module.ImportReference(typeof(System.Runtime.InteropServices.OutAttribute));
  104. param.ParameterType = AddModreqIfNotExist(param.ParameterType, outModreqRef);
  105. }
  106. }
  107. method.IsVirtual = true;
  108. method.IsPublic = true;
  109. method.IsPrivate = false;
  110. method.IsNewSlot = true;
  111. method.IsHideBySig = true;
  112. }
  113. }
  114. foreach (var field in type.Fields)
  115. {
  116. if (field.IsPrivate) field.IsFamily = true;
  117. }
  118. }
  119. private TypeReference AddModreqIfNotExist(TypeReference type, TypeReference mod)
  120. {
  121. var (element, opt, req) = GetDecomposedModifiers(type);
  122. if (!req.Contains(mod))
  123. {
  124. req.Add(mod);
  125. }
  126. return BuildModifiedType(element, opt, req);
  127. }
  128. private (TypeReference Element, List<TypeReference> ModOpt, List<TypeReference> ModReq) GetDecomposedModifiers(TypeReference type)
  129. {
  130. var opt = new List<TypeReference>();
  131. var req = new List<TypeReference>();
  132. while (type is IModifierType modif)
  133. {
  134. if (type.IsOptionalModifier)
  135. opt.Add(modif.ModifierType);
  136. if (type.IsRequiredModifier)
  137. req.Add(modif.ModifierType);
  138. type = modif.ElementType;
  139. }
  140. return (type, opt, req);
  141. }
  142. private TypeReference BuildModifiedType(TypeReference type, IEnumerable<TypeReference> opt, IEnumerable<TypeReference> req)
  143. {
  144. foreach (var mod in req)
  145. {
  146. type = type.MakeRequiredModifierType(mod);
  147. }
  148. foreach (var mod in opt)
  149. {
  150. type = type.MakeOptionalModifierType(mod);
  151. }
  152. return type;
  153. }
  154. #region IDisposable Support
  155. private bool disposedValue = false; // To detect redundant calls
  156. protected virtual void Dispose(bool disposing)
  157. {
  158. if (!disposedValue)
  159. {
  160. if (disposing)
  161. {
  162. module.Dispose();
  163. }
  164. disposedValue = true;
  165. }
  166. }
  167. ~VirtualizedModule()
  168. {
  169. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  170. Dispose(false);
  171. }
  172. // This code added to correctly implement the disposable pattern.
  173. public void Dispose()
  174. {
  175. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  176. Dispose(true);
  177. GC.SuppressFinalize(this);
  178. }
  179. #endregion
  180. }
  181. }