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.

219 lines
7.0 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.IsNestedPrivate)
  72. {
  73. type.IsNestedPrivate = false;
  74. type.IsNestedPublic = true;
  75. }
  76. if (type.IsInterface) return;
  77. if (type.IsAbstract) return;
  78. // These two don't seem to work.
  79. if (type.Name == "SceneControl" || type.Name == "ConfigUI") return;
  80. // Take care of sub types
  81. foreach (var subType in type.NestedTypes)
  82. {
  83. VirtualizeType(subType);
  84. }
  85. foreach (var method in type.Methods)
  86. {
  87. if (method.IsManaged
  88. && method.IsIL
  89. && !method.IsStatic
  90. && (!method.IsVirtual || method.IsFinal)
  91. && !method.IsAbstract
  92. && !method.IsAddOn
  93. && !method.IsConstructor
  94. && !method.IsSpecialName
  95. && !method.IsGenericInstance
  96. && !method.HasOverrides)
  97. {
  98. // fix In and Out parameters to have the modreqs required by the compiler
  99. foreach (var param in method.Parameters)
  100. {
  101. if (param.IsIn)
  102. {
  103. inModreqRef ??= module.ImportReference(typeof(System.Runtime.InteropServices.InAttribute));
  104. param.ParameterType = AddModreqIfNotExist(param.ParameterType, inModreqRef);
  105. }
  106. // Breaks override methods if modreq is applied to `out` parameters
  107. //if (param.IsOut)
  108. //{
  109. // outModreqRef ??= module.ImportReference(typeof(System.Runtime.InteropServices.OutAttribute));
  110. // param.ParameterType = AddModreqIfNotExist(param.ParameterType, outModreqRef);
  111. //}
  112. }
  113. method.IsVirtual = true;
  114. method.IsFinal = false;
  115. method.IsPublic = true;
  116. method.IsPrivate = false;
  117. method.IsNewSlot = true;
  118. method.IsHideBySig = true;
  119. }
  120. }
  121. foreach (var field in type.Fields)
  122. {
  123. if (field.IsPrivate) field.IsFamily = true;
  124. }
  125. }
  126. private TypeReference AddModreqIfNotExist(TypeReference type, TypeReference mod)
  127. {
  128. var (element, opt, req) = GetDecomposedModifiers(type);
  129. if (!req.Contains(mod))
  130. {
  131. req.Add(mod);
  132. }
  133. return BuildModifiedType(element, opt, req);
  134. }
  135. private (TypeReference Element, List<TypeReference> ModOpt, List<TypeReference> ModReq) GetDecomposedModifiers(TypeReference type)
  136. {
  137. var opt = new List<TypeReference>();
  138. var req = new List<TypeReference>();
  139. while (type is IModifierType modif)
  140. {
  141. if (type.IsOptionalModifier)
  142. opt.Add(modif.ModifierType);
  143. if (type.IsRequiredModifier)
  144. req.Add(modif.ModifierType);
  145. type = modif.ElementType;
  146. }
  147. return (type, opt, req);
  148. }
  149. private TypeReference BuildModifiedType(TypeReference type, IEnumerable<TypeReference> opt, IEnumerable<TypeReference> req)
  150. {
  151. foreach (var mod in req)
  152. {
  153. type = type.MakeRequiredModifierType(mod);
  154. }
  155. foreach (var mod in opt)
  156. {
  157. type = type.MakeOptionalModifierType(mod);
  158. }
  159. return type;
  160. }
  161. #region IDisposable Support
  162. private bool disposedValue = false; // To detect redundant calls
  163. protected virtual void Dispose(bool disposing)
  164. {
  165. if (!disposedValue)
  166. {
  167. if (disposing)
  168. {
  169. module.Dispose();
  170. }
  171. disposedValue = true;
  172. }
  173. }
  174. ~VirtualizedModule()
  175. {
  176. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  177. Dispose(false);
  178. }
  179. // This code added to correctly implement the disposable pattern.
  180. public void Dispose()
  181. {
  182. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  183. Dispose(true);
  184. GC.SuppressFinalize(this);
  185. }
  186. #endregion
  187. }
  188. }