接入obfuz->2.0

This commit is contained in:
Alex-Rachel
2025-07-26 08:10:41 +08:00
parent f2c7ff4336
commit cb86d8868e
713 changed files with 57092 additions and 10 deletions

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.ABI
{
public static class ABIUtil
{
public static string GetHybridCLRPlatformMacro(PlatformABI abi)
{
switch(abi)
{
case PlatformABI.Arm64: return "HYBRIDCLR_ABI_ARM_64";
case PlatformABI.Universal64: return "HYBRIDCLR_ABI_UNIVERSAL_64";
case PlatformABI.Universal32: return "HYBRIDCLR_ABI_UNIVERSAL_32";
case PlatformABI.WebGL32: return "HYBRIDCLR_ABI_WEBGL32";
default: throw new NotSupportedException();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: edeb86f4b5b13ca4cb0fe9d87ce509bb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,77 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace HybridCLR.Editor.ABI
{
public class MethodDesc : IEquatable<MethodDesc>
{
public string Sig { get; private set; }
public MethodDef MethodDef { get; set; }
public ReturnInfo ReturnInfo { get; set; }
public List<ParamInfo> ParamInfos { get; set; }
public void Init()
{
for(int i = 0; i < ParamInfos.Count; i++)
{
ParamInfos[i].Index = i;
}
Sig = CreateCallSigName();
}
public void TransfromSigTypes(Func<TypeInfo, bool, TypeInfo> transformer)
{
ReturnInfo.Type = transformer(ReturnInfo.Type, true);
foreach(var paramType in ParamInfos)
{
paramType.Type = transformer(paramType.Type, false);
}
}
public string CreateCallSigName()
{
var n = new StringBuilder();
n.Append(ReturnInfo.Type.CreateSigName());
foreach(var param in ParamInfos)
{
n.Append(param.Type.CreateSigName());
}
return n.ToString();
}
public string CreateInvokeSigName()
{
var n = new StringBuilder();
n.Append(ReturnInfo.Type.CreateSigName());
foreach (var param in ParamInfos)
{
n.Append(param.Type.CreateSigName());
}
return n.ToString();
}
public override bool Equals(object obj)
{
return Equals((MethodDesc)obj);
}
public bool Equals(MethodDesc other)
{
return Sig == other.Sig;
}
public override int GetHashCode()
{
return Sig.GetHashCode();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 28e06667d06f37b4990b16f54f903a35
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.ABI
{
public class ParamInfo
{
public TypeInfo Type { get; set; }
public int Index { get; set; }
}
public class ReturnInfo
{
public TypeInfo Type { get; set; }
public bool IsVoid => Type.PorType == ParamOrReturnType.VOID;
public override string ToString()
{
return Type.GetTypeName();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f2ba16cf4bf82374c814789b6ced3abd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.ABI
{
public enum ParamOrReturnType
{
VOID,
I1,
U1,
I2,
U2,
I4,
U4,
I8,
U8,
R4,
R8,
I,
U,
TYPEDBYREF,
STRUCT,
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 80682e47c38a2f04f8af94d356688cf0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
namespace HybridCLR.Editor.ABI
{
public enum PlatformABI
{
Universal32,
Universal64,
Arm64,
WebGL32,
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b9f06ff0612105b4ea20e0309e759e24
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,102 @@
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.ABI
{
public class TypeCreator
{
private readonly Dictionary<TypeSig, TypeInfo> _typeInfoCache = new Dictionary<TypeSig, TypeInfo>(TypeEqualityComparer.Instance);
private int _nextStructId = 0;
public TypeInfo CreateTypeInfo(TypeSig type)
{
type = type.RemovePinnedAndModifiers();
if (!_typeInfoCache.TryGetValue(type, out var typeInfo))
{
typeInfo = CreateTypeInfo0(type);
_typeInfoCache.Add(type, typeInfo);
}
return typeInfo;
}
TypeInfo CreateTypeInfo0(TypeSig type)
{
type = type.RemovePinnedAndModifiers();
if (type.IsByRef)
{
return TypeInfo.s_u;
}
switch (type.ElementType)
{
case ElementType.Void: return TypeInfo.s_void;
case ElementType.Boolean: return TypeInfo.s_u1;
case ElementType.I1: return TypeInfo.s_i1;
case ElementType.U1: return TypeInfo.s_u1;
case ElementType.I2: return TypeInfo.s_i2;
case ElementType.Char:
case ElementType.U2: return TypeInfo.s_u2;
case ElementType.I4: return TypeInfo.s_i4;
case ElementType.U4: return TypeInfo.s_u4;
case ElementType.I8: return TypeInfo.s_i8;
case ElementType.U8: return TypeInfo.s_u8;
case ElementType.R4: return TypeInfo.s_r4;
case ElementType.R8: return TypeInfo.s_r8;
case ElementType.I: return TypeInfo.s_i;
case ElementType.U:
case ElementType.String:
case ElementType.Ptr:
case ElementType.ByRef:
case ElementType.Class:
case ElementType.Array:
case ElementType.SZArray:
case ElementType.FnPtr:
case ElementType.Object:
case ElementType.Module:
case ElementType.Var:
case ElementType.MVar:
return TypeInfo.s_u;
case ElementType.TypedByRef: return TypeInfo.s_typedByRef;
case ElementType.ValueType:
{
TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDef();
if (typeDef == null)
{
throw new Exception($"type:{type} definition could not be found. Please try `HybridCLR/Genergate/LinkXml`, then Build once to generate the AOT dll, and then regenerate the bridge function");
}
if (typeDef.IsEnum)
{
return CreateTypeInfo(typeDef.GetEnumUnderlyingType());
}
return CreateValueType(type);
}
case ElementType.GenericInst:
{
GenericInstSig gis = (GenericInstSig)type;
if (!gis.GenericType.IsValueType)
{
return TypeInfo.s_u;
}
TypeDef typeDef = gis.GenericType.ToTypeDefOrRef().ResolveTypeDef();
if (typeDef.IsEnum)
{
return CreateTypeInfo(typeDef.GetEnumUnderlyingType());
}
return CreateValueType(type);
}
default: throw new NotSupportedException($"{type.ElementType}");
}
}
protected TypeInfo CreateValueType(TypeSig type)
{
return new TypeInfo(ParamOrReturnType.STRUCT, type, _nextStructId++);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0b1df5760b488fa43a68843c46fda63a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,116 @@
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace HybridCLR.Editor.ABI
{
public class TypeInfo : IEquatable<TypeInfo>
{
public static readonly TypeInfo s_void = new TypeInfo(ParamOrReturnType.VOID);
public static readonly TypeInfo s_i1 = new TypeInfo(ParamOrReturnType.I1);
public static readonly TypeInfo s_u1 = new TypeInfo(ParamOrReturnType.U1);
public static readonly TypeInfo s_i2 = new TypeInfo(ParamOrReturnType.I2);
public static readonly TypeInfo s_u2 = new TypeInfo(ParamOrReturnType.U2);
public static readonly TypeInfo s_i4 = new TypeInfo(ParamOrReturnType.I4);
public static readonly TypeInfo s_u4 = new TypeInfo(ParamOrReturnType.U4);
public static readonly TypeInfo s_i8 = new TypeInfo(ParamOrReturnType.I8);
public static readonly TypeInfo s_u8 = new TypeInfo(ParamOrReturnType.U8);
public static readonly TypeInfo s_r4 = new TypeInfo(ParamOrReturnType.R4);
public static readonly TypeInfo s_r8 = new TypeInfo(ParamOrReturnType.R8);
public static readonly TypeInfo s_i = new TypeInfo(ParamOrReturnType.I);
public static readonly TypeInfo s_u = new TypeInfo(ParamOrReturnType.U);
public static readonly TypeInfo s_typedByRef = new TypeInfo(ParamOrReturnType.TYPEDBYREF);
public const string strTypedByRef = "typedbyref";
public TypeInfo(ParamOrReturnType portype, TypeSig klass = null, int typeId = 0)
{
PorType = portype;
Klass = klass;
_typeId = typeId;
}
public ParamOrReturnType PorType { get; }
public TypeSig Klass { get; }
public bool IsStruct => PorType == ParamOrReturnType.STRUCT;
public bool IsPrimitiveType => PorType <= ParamOrReturnType.U;
private readonly int _typeId;
public int TypeId => _typeId;
public bool Equals(TypeInfo other)
{
return PorType == other.PorType && TypeEqualityComparer.Instance.Equals(Klass, other.Klass);
}
public override bool Equals(object obj)
{
return Equals((TypeInfo)obj);
}
public override int GetHashCode()
{
return (int)PorType * 23 + (Klass != null ? TypeEqualityComparer.Instance.GetHashCode(Klass) : 0);
}
public bool NeedExpandValue()
{
return PorType >= ParamOrReturnType.I1 && PorType <= ParamOrReturnType.U2;
}
public string CreateSigName()
{
switch (PorType)
{
case ParamOrReturnType.VOID: return "v";
case ParamOrReturnType.I1: return "i1";
case ParamOrReturnType.U1: return "u1";
case ParamOrReturnType.I2: return "i2";
case ParamOrReturnType.U2: return "u2";
case ParamOrReturnType.I4: return "i4";
case ParamOrReturnType.U4: return "u4";
case ParamOrReturnType.I8: return "i8";
case ParamOrReturnType.U8: return "u8";
case ParamOrReturnType.R4: return "r4";
case ParamOrReturnType.R8: return "r8";
case ParamOrReturnType.I: return "i";
case ParamOrReturnType.U: return "u";
case ParamOrReturnType.TYPEDBYREF: return strTypedByRef;
case ParamOrReturnType.STRUCT: return $"s{_typeId}";
default: throw new NotSupportedException(PorType.ToString());
};
}
public string GetTypeName()
{
switch (PorType)
{
case ParamOrReturnType.VOID: return "void";
case ParamOrReturnType.I1: return "int8_t";
case ParamOrReturnType.U1: return "uint8_t";
case ParamOrReturnType.I2: return "int16_t";
case ParamOrReturnType.U2: return "uint16_t";
case ParamOrReturnType.I4: return "int32_t";
case ParamOrReturnType.U4: return "uint32_t";
case ParamOrReturnType.I8: return "int64_t";
case ParamOrReturnType.U8: return "uint64_t";
case ParamOrReturnType.R4: return "float";
case ParamOrReturnType.R8: return "double";
case ParamOrReturnType.I: return "intptr_t";
case ParamOrReturnType.U: return "uintptr_t";
case ParamOrReturnType.TYPEDBYREF: return "Il2CppTypedRef";
case ParamOrReturnType.STRUCT: return $"__struct_{_typeId}__";
default: throw new NotImplementedException(PorType.ToString());
};
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ffafce7f1f0bf614d95b48ca39385377
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,181 @@
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.ABI
{
public class ValueTypeSizeAligmentCalculator
{
public static ValueTypeSizeAligmentCalculator Caculator64 { get; } = new ValueTypeSizeAligmentCalculator(false);
public static ValueTypeSizeAligmentCalculator Caculator32 { get; } = new ValueTypeSizeAligmentCalculator(true);
public ValueTypeSizeAligmentCalculator(bool arch32)
{
_referenceSize = arch32 ? 4 : 8;
}
private readonly int _referenceSize;
private static bool IsIgnoreField(FieldDef field)
{
var ignoreAttr = field.CustomAttributes.Where(a => a.AttributeType.FullName == "UnityEngine.Bindings.IgnoreAttribute").FirstOrDefault();
if (ignoreAttr == null)
{
return false;
}
CANamedArgument arg = ignoreAttr.GetProperty("DoesNotContributeToSize");
if(arg != null && (bool)arg.Value)
{
//Debug.Log($"IgnoreField.DoesNotContributeToSize = true:{field}");
return true;
}
return false;
}
private (int Size, int Aligment) SizeAndAligmentOfStruct(TypeSig type)
{
int totalSize = 0;
int packAligment = 8;
int maxAligment = 1;
TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDefThrow();
List<TypeSig> klassInst = type.ToGenericInstSig()?.GenericArguments?.ToList();
GenericArgumentContext ctx = klassInst != null ? new GenericArgumentContext(klassInst, null) : null;
ClassLayout sa = typeDef.ClassLayout;
if (sa != null && sa.PackingSize > 0)
{
packAligment = sa.PackingSize;
}
bool useSLSize = true;
foreach (FieldDef field in typeDef.Fields)
{
if (field.IsStatic)
{
continue;
}
TypeSig fieldType = ctx != null ? MetaUtil.Inflate(field.FieldType, ctx) : field.FieldType;
var (fs, fa) = SizeAndAligmentOf(fieldType);
fa = Math.Min(fa, packAligment);
if (fa > maxAligment)
{
maxAligment = fa;
}
if (IsIgnoreField(field))
{
continue;
}
if (typeDef.Layout.HasFlag(dnlib.DotNet.TypeAttributes.ExplicitLayout))
{
int offset = (int)field.FieldOffset.Value;
totalSize = Math.Max(totalSize, offset + fs);
if (sa != null && offset > sa.ClassSize)
{
useSLSize = false;
}
}
else
{
if (totalSize % fa != 0)
{
totalSize = (totalSize + fa - 1) / fa * fa;
}
totalSize += fs;
if (sa != null && totalSize > sa.ClassSize)
{
useSLSize = false;
}
}
}
if (totalSize == 0)
{
totalSize = maxAligment;
}
if (totalSize % maxAligment != 0)
{
totalSize = (totalSize + maxAligment - 1) / maxAligment * maxAligment;
}
if (sa != null && sa.ClassSize > 0)
{
if (/*sa.Value == LayoutKind.Explicit &&*/ useSLSize)
{
totalSize = (int)sa.ClassSize;
while(totalSize % maxAligment != 0)
{
maxAligment /= 2;
}
}
}
return (totalSize, maxAligment);
}
public (int Size, int Aligment) SizeAndAligmentOf(TypeSig type)
{
type = type.RemovePinnedAndModifiers();
if (type.IsByRef || !type.IsValueType || type.IsArray)
return (_referenceSize, _referenceSize);
switch (type.ElementType)
{
case ElementType.Void: throw new NotSupportedException(type.ToString());
case ElementType.Boolean:
case ElementType.I1:
case ElementType.U1: return (1, 1);
case ElementType.Char:
case ElementType.I2:
case ElementType.U2: return (2, 2);
case ElementType.I4:
case ElementType.U4: return (4, 4);
case ElementType.I8:
case ElementType.U8: return (8, 8);
case ElementType.R4: return (4, 4);
case ElementType.R8: return (8, 8);
case ElementType.I:
case ElementType.U:
case ElementType.String:
case ElementType.Ptr:
case ElementType.ByRef:
case ElementType.Class:
case ElementType.Array:
case ElementType.SZArray:
case ElementType.FnPtr:
case ElementType.Object:
case ElementType.Module: return (_referenceSize, _referenceSize);
case ElementType.TypedByRef: return SizeAndAligmentOfStruct(type);
case ElementType.ValueType:
{
TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDef();
if (typeDef.IsEnum)
{
return SizeAndAligmentOf(typeDef.GetEnumUnderlyingType());
}
return SizeAndAligmentOfStruct(type);
}
case ElementType.GenericInst:
{
GenericInstSig gis = (GenericInstSig)type;
if (!gis.GenericType.IsValueType)
{
return (_referenceSize, _referenceSize);
}
TypeDef typeDef = gis.GenericType.ToTypeDefOrRef().ResolveTypeDef();
if (typeDef.IsEnum)
{
return SizeAndAligmentOf(typeDef.GetEnumUnderlyingType());
}
return SizeAndAligmentOfStruct(type);
}
default: throw new NotSupportedException(type.ToString());
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b7af32bdf1cf55c42bfc449820d401cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: