2022-09-22 08:56:07 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using HybridCLR.Editor.Meta;
|
2023-10-09 15:39:32 +08:00
|
|
|
|
using HybridCLR.Editor.Settings;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-09-27 14:57:00 +08:00
|
|
|
|
using System.IO;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-12-02 10:33:13 +08:00
|
|
|
|
using UnityEditor;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2022-10-17 12:16:18 +08:00
|
|
|
|
namespace HybridCLR.Editor.Meta
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
public static class MetaUtil
|
|
|
|
|
{
|
2022-10-17 12:16:18 +08:00
|
|
|
|
|
|
|
|
|
public static bool EqualsTypeSig(TypeSig a, TypeSig b)
|
|
|
|
|
{
|
|
|
|
|
if (a == b)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (a != null && b != null)
|
|
|
|
|
{
|
|
|
|
|
return TypeEqualityComparer.Instance.Equals(a, b);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool EqualsTypeSigArray(List<TypeSig> a, List<TypeSig> b)
|
|
|
|
|
{
|
|
|
|
|
if (a == b)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (a != null && b != null)
|
|
|
|
|
{
|
|
|
|
|
if (a.Count != b.Count)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < a.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!TypeEqualityComparer.Instance.Equals(a[i], b[i]))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
public static TypeSig Inflate(TypeSig sig, GenericArgumentContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (!sig.ContainsGenericParameter)
|
|
|
|
|
{
|
|
|
|
|
return sig;
|
|
|
|
|
}
|
|
|
|
|
return ctx.Resolve(sig);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:16:31 +08:00
|
|
|
|
public static TypeSig ToShareTypeSig(ICorLibTypes corTypes, TypeSig typeSig)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
var a = typeSig.RemovePinnedAndModifiers();
|
2022-10-14 14:47:27 +08:00
|
|
|
|
switch (a.ElementType)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
case ElementType.Void: return corTypes.Void;
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.Boolean: return corTypes.Byte;
|
|
|
|
|
case ElementType.Char: return corTypes.UInt16;
|
|
|
|
|
case ElementType.I1: return corTypes.SByte;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.U1:return corTypes.Byte;
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.I2: return corTypes.Int16;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.U2: return corTypes.UInt16;
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.I4: return corTypes.Int32;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.U4: return corTypes.UInt32;
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.I8: return corTypes.Int64;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.U8: return corTypes.UInt64;
|
|
|
|
|
case ElementType.R4: return corTypes.Single;
|
|
|
|
|
case ElementType.R8: return corTypes.Double;
|
|
|
|
|
case ElementType.String: return corTypes.Object;
|
|
|
|
|
case ElementType.TypedByRef: return corTypes.TypedReference;
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.I: return corTypes.IntPtr;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.U: return corTypes.UIntPtr;
|
|
|
|
|
case ElementType.Object: return corTypes.Object;
|
|
|
|
|
case ElementType.Sentinel: return typeSig;
|
2023-09-23 21:19:45 +08:00
|
|
|
|
case ElementType.Ptr: return corTypes.UIntPtr;
|
|
|
|
|
case ElementType.ByRef: return corTypes.UIntPtr;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.SZArray: return corTypes.Object;
|
|
|
|
|
case ElementType.Array: return corTypes.Object;
|
2023-08-22 11:16:31 +08:00
|
|
|
|
case ElementType.ValueType:
|
|
|
|
|
{
|
|
|
|
|
TypeDef typeDef = a.ToTypeDefOrRef().ResolveTypeDef();
|
|
|
|
|
if (typeDef == null)
|
|
|
|
|
{
|
2023-08-30 11:07:12 +08:00
|
|
|
|
throw new Exception($"type:{a} definition could not be found");
|
2023-08-22 11:16:31 +08:00
|
|
|
|
}
|
|
|
|
|
if (typeDef.IsEnum)
|
|
|
|
|
{
|
|
|
|
|
return ToShareTypeSig(corTypes, typeDef.GetEnumUnderlyingType());
|
|
|
|
|
}
|
|
|
|
|
return typeSig;
|
|
|
|
|
}
|
2023-07-12 23:09:58 +08:00
|
|
|
|
case ElementType.Var:
|
|
|
|
|
case ElementType.MVar:
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.Class: return corTypes.Object;
|
|
|
|
|
case ElementType.GenericInst:
|
|
|
|
|
{
|
|
|
|
|
var gia = (GenericInstSig)a;
|
2023-08-22 11:16:31 +08:00
|
|
|
|
TypeDef typeDef = gia.GenericType.ToTypeDefOrRef().ResolveTypeDef();
|
|
|
|
|
if (typeDef == null)
|
|
|
|
|
{
|
2023-08-30 11:07:12 +08:00
|
|
|
|
throw new Exception($"type:{a} definition could not be found");
|
2023-08-22 11:16:31 +08:00
|
|
|
|
}
|
|
|
|
|
if (typeDef.IsEnum)
|
|
|
|
|
{
|
|
|
|
|
return ToShareTypeSig(corTypes, typeDef.GetEnumUnderlyingType());
|
|
|
|
|
}
|
|
|
|
|
if (!typeDef.IsValueType)
|
|
|
|
|
{
|
|
|
|
|
return corTypes.Object;
|
|
|
|
|
}
|
|
|
|
|
return new GenericInstSig(gia.GenericType, gia.GenericArguments.Select(ga => ToShareTypeSig(corTypes, ga)).ToList());
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
2022-09-22 16:57:01 +08:00
|
|
|
|
case ElementType.FnPtr: return corTypes.IntPtr;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
case ElementType.ValueArray: return typeSig;
|
|
|
|
|
case ElementType.Module: return typeSig;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotSupportedException(typeSig.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:16:31 +08:00
|
|
|
|
public static List<TypeSig> ToShareTypeSigs(ICorLibTypes corTypes, IList<TypeSig> typeSigs)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
if (typeSigs == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-08-22 11:16:31 +08:00
|
|
|
|
return typeSigs.Select(s => ToShareTypeSig(corTypes, s)).ToList();
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 14:11:32 +08:00
|
|
|
|
public static IAssemblyResolver CreateHotUpdateAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-12-02 10:33:13 +08:00
|
|
|
|
var externalDirs = HybridCLRSettings.Instance.externalHotUpdateAssembliyDirs;
|
|
|
|
|
var defaultHotUpdateOutputDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
|
2022-12-14 14:11:32 +08:00
|
|
|
|
IAssemblyResolver defaultHotUpdateResolver = new FixedSetAssemblyResolver(defaultHotUpdateOutputDir, hotUpdateDlls);
|
2022-12-02 10:33:13 +08:00
|
|
|
|
if (externalDirs == null || externalDirs.Length == 0)
|
|
|
|
|
{
|
2022-12-14 14:11:32 +08:00
|
|
|
|
return defaultHotUpdateResolver;
|
2022-12-02 10:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-14 14:11:32 +08:00
|
|
|
|
var resolvers = new List<IAssemblyResolver>();
|
2022-12-02 10:33:13 +08:00
|
|
|
|
foreach (var dir in externalDirs)
|
|
|
|
|
{
|
2022-12-14 14:11:32 +08:00
|
|
|
|
resolvers.Add(new FixedSetAssemblyResolver($"{dir}/{target}", hotUpdateDlls));
|
|
|
|
|
resolvers.Add(new FixedSetAssemblyResolver(dir, hotUpdateDlls));
|
2022-12-02 10:33:13 +08:00
|
|
|
|
}
|
2022-12-14 14:11:32 +08:00
|
|
|
|
resolvers.Add(defaultHotUpdateResolver);
|
|
|
|
|
return new CombinedAssemblyResolver(resolvers.ToArray());
|
2022-12-02 10:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 14:11:32 +08:00
|
|
|
|
public static IAssemblyResolver CreateAOTAssemblyResolver(BuildTarget target)
|
|
|
|
|
{
|
|
|
|
|
return new PathAssemblyResolver(SettingsUtil.GetAssembliesPostIl2CppStripDir(target));
|
|
|
|
|
}
|
2022-12-13 11:40:18 +08:00
|
|
|
|
|
2022-12-14 14:11:32 +08:00
|
|
|
|
public static IAssemblyResolver CreateHotUpdateAndAOTAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
|
2022-12-02 10:33:13 +08:00
|
|
|
|
{
|
2022-12-14 14:11:32 +08:00
|
|
|
|
return new CombinedAssemblyResolver(
|
|
|
|
|
CreateHotUpdateAssemblyResolver(target, hotUpdateDlls),
|
2023-11-01 20:33:42 +08:00
|
|
|
|
CreateAOTAssemblyResolver(target),
|
|
|
|
|
new PathAssemblyResolver($"{SettingsUtil.HybridCLRDataPathInPackage}/NetStandard")
|
2022-12-14 14:11:32 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2023-08-22 11:16:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<TypeSig> CreateDefaultGenericParams(ModuleDef module, int genericParamCount)
|
|
|
|
|
{
|
|
|
|
|
var methodGenericParams = new List<TypeSig>();
|
|
|
|
|
for (int i = 0; i < genericParamCount; i++)
|
|
|
|
|
{
|
|
|
|
|
methodGenericParams.Add(module.CorLibTypes.Object);
|
|
|
|
|
}
|
|
|
|
|
return methodGenericParams;
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|