Cardlife mod patcher
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.

Virtualizer.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Mono.Cecil;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace IPA.Patcher
  8. {
  9. class VirtualizedModule
  10. {
  11. private const string ENTRY_TYPE = "Display";
  12. private FileInfo _File;
  13. private ModuleDefinition _Module;
  14. public static VirtualizedModule Load(string engineFile)
  15. {
  16. return new VirtualizedModule(engineFile);
  17. }
  18. private VirtualizedModule(string assemblyFile)
  19. {
  20. _File = new FileInfo(assemblyFile);
  21. LoadModules();
  22. }
  23. private void LoadModules()
  24. {
  25. var resolver = new DefaultAssemblyResolver();
  26. resolver.AddSearchDirectory(_File.DirectoryName);
  27. var parameters = new ReaderParameters
  28. {
  29. AssemblyResolver = resolver,
  30. };
  31. _Module = ModuleDefinition.ReadModule(_File.FullName, parameters);
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="module"></param>
  37. public void Virtualize()
  38. {
  39. foreach (var type in _Module.Types)
  40. {
  41. VirtualizeType(type);
  42. }
  43. }
  44. private void VirtualizeType(TypeDefinition type)
  45. {
  46. if (type.IsSealed) return;
  47. if (type.IsInterface) return;
  48. if (type.IsAbstract) return;
  49. // These two don't seem to work.
  50. if (type.Name == "SceneControl" || type.Name == "ConfigUI") return;
  51. // Take care of sub types
  52. foreach (var subType in type.NestedTypes)
  53. {
  54. VirtualizeType(subType);
  55. }
  56. foreach (var method in type.Methods)
  57. {
  58. if (method.IsManaged
  59. && method.IsIL
  60. && !method.IsStatic
  61. && !method.IsVirtual
  62. && !method.IsAbstract
  63. && !method.IsAddOn
  64. && !method.IsConstructor
  65. && !method.IsSpecialName
  66. && !method.IsGenericInstance
  67. && !method.HasOverrides)
  68. {
  69. method.IsVirtual = true;
  70. method.IsPublic = true;
  71. method.IsPrivate = false;
  72. method.IsNewSlot = true;
  73. method.IsHideBySig = true;
  74. }
  75. }
  76. foreach (var field in type.Fields)
  77. {
  78. if (field.IsPrivate) field.IsFamily = true;
  79. }
  80. }
  81. public bool IsVirtualized
  82. {
  83. get
  84. {
  85. return _Module.GetTypes().SelectMany(t => t.Methods.Where(m => m.Name == "Awake")).All(m => m.IsVirtual);
  86. }
  87. }
  88. }
  89. }