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.

218 lines
6.9 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
  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.IsPublic = true;
  115. method.IsPrivate = false;
  116. method.IsNewSlot = true;
  117. method.IsHideBySig = true;
  118. }
  119. }
  120. foreach (var field in type.Fields)
  121. {
  122. if (field.IsPrivate) field.IsFamily = true;
  123. }
  124. }
  125. private TypeReference AddModreqIfNotExist(TypeReference type, TypeReference mod)
  126. {
  127. var (element, opt, req) = GetDecomposedModifiers(type);
  128. if (!req.Contains(mod))
  129. {
  130. req.Add(mod);
  131. }
  132. return BuildModifiedType(element, opt, req);
  133. }
  134. private (TypeReference Element, List<TypeReference> ModOpt, List<TypeReference> ModReq) GetDecomposedModifiers(TypeReference type)
  135. {
  136. var opt = new List<TypeReference>();
  137. var req = new List<TypeReference>();
  138. while (type is IModifierType modif)
  139. {
  140. if (type.IsOptionalModifier)
  141. opt.Add(modif.ModifierType);
  142. if (type.IsRequiredModifier)
  143. req.Add(modif.ModifierType);
  144. type = modif.ElementType;
  145. }
  146. return (type, opt, req);
  147. }
  148. private TypeReference BuildModifiedType(TypeReference type, IEnumerable<TypeReference> opt, IEnumerable<TypeReference> req)
  149. {
  150. foreach (var mod in req)
  151. {
  152. type = type.MakeRequiredModifierType(mod);
  153. }
  154. foreach (var mod in opt)
  155. {
  156. type = type.MakeOptionalModifierType(mod);
  157. }
  158. return type;
  159. }
  160. #region IDisposable Support
  161. private bool disposedValue = false; // To detect redundant calls
  162. protected virtual void Dispose(bool disposing)
  163. {
  164. if (!disposedValue)
  165. {
  166. if (disposing)
  167. {
  168. module.Dispose();
  169. }
  170. disposedValue = true;
  171. }
  172. }
  173. ~VirtualizedModule()
  174. {
  175. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  176. Dispose(false);
  177. }
  178. // This code added to correctly implement the disposable pattern.
  179. public void Dispose()
  180. {
  181. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  182. Dispose(true);
  183. GC.SuppressFinalize(this);
  184. }
  185. #endregion
  186. }
  187. }