MetaUtil.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using dnlib.DotNet;
  2. using HybridCLR.Editor.Meta;
  3. using HybridCLR.Editor.Settings;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using UnityEditor;
  11. namespace HybridCLR.Editor.Meta
  12. {
  13. public static class MetaUtil
  14. {
  15. public static bool EqualsTypeSig(TypeSig a, TypeSig b)
  16. {
  17. if (a == b)
  18. {
  19. return true;
  20. }
  21. if (a != null && b != null)
  22. {
  23. return TypeEqualityComparer.Instance.Equals(a, b);
  24. }
  25. return false;
  26. }
  27. public static bool EqualsTypeSigArray(List<TypeSig> a, List<TypeSig> b)
  28. {
  29. if (a == b)
  30. {
  31. return true;
  32. }
  33. if (a != null && b != null)
  34. {
  35. if (a.Count != b.Count)
  36. {
  37. return false;
  38. }
  39. for (int i = 0; i < a.Count; i++)
  40. {
  41. if (!TypeEqualityComparer.Instance.Equals(a[i], b[i]))
  42. {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. public static TypeSig Inflate(TypeSig sig, GenericArgumentContext ctx)
  51. {
  52. if (!sig.ContainsGenericParameter)
  53. {
  54. return sig;
  55. }
  56. return ctx.Resolve(sig);
  57. }
  58. public static TypeSig ToShareTypeSig(ICorLibTypes corTypes, TypeSig typeSig)
  59. {
  60. var a = typeSig.RemovePinnedAndModifiers();
  61. switch (a.ElementType)
  62. {
  63. case ElementType.Void: return corTypes.Void;
  64. case ElementType.Boolean: return corTypes.Byte;
  65. case ElementType.Char: return corTypes.UInt16;
  66. case ElementType.I1: return corTypes.SByte;
  67. case ElementType.U1:return corTypes.Byte;
  68. case ElementType.I2: return corTypes.Int16;
  69. case ElementType.U2: return corTypes.UInt16;
  70. case ElementType.I4: return corTypes.Int32;
  71. case ElementType.U4: return corTypes.UInt32;
  72. case ElementType.I8: return corTypes.Int64;
  73. case ElementType.U8: return corTypes.UInt64;
  74. case ElementType.R4: return corTypes.Single;
  75. case ElementType.R8: return corTypes.Double;
  76. case ElementType.String: return corTypes.Object;
  77. case ElementType.TypedByRef: return corTypes.TypedReference;
  78. case ElementType.I: return corTypes.IntPtr;
  79. case ElementType.U: return corTypes.UIntPtr;
  80. case ElementType.Object: return corTypes.Object;
  81. case ElementType.Sentinel: return typeSig;
  82. case ElementType.Ptr: return corTypes.UIntPtr;
  83. case ElementType.ByRef: return corTypes.UIntPtr;
  84. case ElementType.SZArray: return corTypes.Object;
  85. case ElementType.Array: return corTypes.Object;
  86. case ElementType.ValueType:
  87. {
  88. TypeDef typeDef = a.ToTypeDefOrRef().ResolveTypeDef();
  89. if (typeDef == null)
  90. {
  91. throw new Exception($"type:{a} definition could not be found");
  92. }
  93. if (typeDef.IsEnum)
  94. {
  95. return ToShareTypeSig(corTypes, typeDef.GetEnumUnderlyingType());
  96. }
  97. return typeSig;
  98. }
  99. case ElementType.Var:
  100. case ElementType.MVar:
  101. case ElementType.Class: return corTypes.Object;
  102. case ElementType.GenericInst:
  103. {
  104. var gia = (GenericInstSig)a;
  105. TypeDef typeDef = gia.GenericType.ToTypeDefOrRef().ResolveTypeDef();
  106. if (typeDef == null)
  107. {
  108. throw new Exception($"type:{a} definition could not be found");
  109. }
  110. if (typeDef.IsEnum)
  111. {
  112. return ToShareTypeSig(corTypes, typeDef.GetEnumUnderlyingType());
  113. }
  114. if (!typeDef.IsValueType)
  115. {
  116. return corTypes.Object;
  117. }
  118. return new GenericInstSig(gia.GenericType, gia.GenericArguments.Select(ga => ToShareTypeSig(corTypes, ga)).ToList());
  119. }
  120. case ElementType.FnPtr: return corTypes.IntPtr;
  121. case ElementType.ValueArray: return typeSig;
  122. case ElementType.Module: return typeSig;
  123. default:
  124. throw new NotSupportedException(typeSig.ToString());
  125. }
  126. }
  127. public static List<TypeSig> ToShareTypeSigs(ICorLibTypes corTypes, IList<TypeSig> typeSigs)
  128. {
  129. if (typeSigs == null)
  130. {
  131. return null;
  132. }
  133. return typeSigs.Select(s => ToShareTypeSig(corTypes, s)).ToList();
  134. }
  135. public static IAssemblyResolver CreateHotUpdateAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
  136. {
  137. var externalDirs = HybridCLRSettings.Instance.externalHotUpdateAssembliyDirs;
  138. var defaultHotUpdateOutputDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  139. IAssemblyResolver defaultHotUpdateResolver = new FixedSetAssemblyResolver(defaultHotUpdateOutputDir, hotUpdateDlls);
  140. if (externalDirs == null || externalDirs.Length == 0)
  141. {
  142. return defaultHotUpdateResolver;
  143. }
  144. else
  145. {
  146. var resolvers = new List<IAssemblyResolver>();
  147. foreach (var dir in externalDirs)
  148. {
  149. resolvers.Add(new FixedSetAssemblyResolver($"{dir}/{target}", hotUpdateDlls));
  150. resolvers.Add(new FixedSetAssemblyResolver(dir, hotUpdateDlls));
  151. }
  152. resolvers.Add(defaultHotUpdateResolver);
  153. return new CombinedAssemblyResolver(resolvers.ToArray());
  154. }
  155. }
  156. public static IAssemblyResolver CreateAOTAssemblyResolver(BuildTarget target)
  157. {
  158. return new PathAssemblyResolver(SettingsUtil.GetAssembliesPostIl2CppStripDir(target));
  159. }
  160. public static IAssemblyResolver CreateHotUpdateAndAOTAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
  161. {
  162. return new CombinedAssemblyResolver(
  163. CreateHotUpdateAssemblyResolver(target, hotUpdateDlls),
  164. CreateAOTAssemblyResolver(target)
  165. );
  166. }
  167. public static string ResolveNetStandardAssemblyPath(string assemblyName)
  168. {
  169. return $"{SettingsUtil.HybridCLRDataPathInPackage}/NetStandard/{assemblyName}.dll";
  170. }
  171. public static List<TypeSig> CreateDefaultGenericParams(ModuleDef module, int genericParamCount)
  172. {
  173. var methodGenericParams = new List<TypeSig>();
  174. for (int i = 0; i < genericParamCount; i++)
  175. {
  176. methodGenericParams.Add(module.CorLibTypes.Object);
  177. }
  178. return methodGenericParams;
  179. }
  180. }
  181. }