提交 ac6abf5d 编写于 作者: J johnche

规避il2cpp在字段个数大于32767生成代码报错的bug

上级 fa7c9a25
......@@ -215,13 +215,19 @@ namespace IFix.Core
return redirectField;
}
static int getMapId(Type idMapType, MethodBase method)
static int getMapId(List<Type> idMapArray, MethodBase method)
{
IDTagAttribute id = Attribute.GetCustomAttribute(method, typeof(IDTagAttribute), false) as IDTagAttribute;
int overrideId = id == null ? 0 : id.ID;
var fieldName = string.Format("{0}-{1}{2}", method.DeclaringType.FullName.Replace('.', '-')
.Replace('+', '-'), method.Name, overrideId);
var field = idMapType.GetField(fieldName);
FieldInfo field = null;
for (int i = 0; i < idMapArray.Count; i++)
{
field = idMapArray[i].GetField(fieldName);
if (field != null) break;
}
if (field == null)
{
throw new Exception(string.Format("cat not find id field: {0}, for {1}", fieldName, method));
......@@ -450,8 +456,15 @@ namespace IFix.Core
}
virtualMachine.WrappersManager = wrapperManager;
var idMapTypeName = reader.ReadString();
var idMapType = Type.GetType(idMapTypeName, true);
var assemblyStr = reader.ReadString();
var idMapList = new List<Type>();
for(int i = 0; i < 100; i++)
{
var idMapType = Type.GetType("IFix.IDMAP" + i + assemblyStr, false);
if (idMapType == null) break;
idMapList.Add(idMapType);
}
lock (removers)
{
......@@ -496,7 +509,7 @@ namespace IFix.Core
{
var fixMethod = readMethod(reader, externTypes);
var fixMethodId = reader.ReadInt32();
var pos = getMapId(idMapType, fixMethod);
var pos = getMapId(idMapList, fixMethod);
methodIdArray[i] = fixMethodId;
posArray[i] = pos;
if (pos > maxPos)
......
......@@ -1067,22 +1067,22 @@ namespace IFix
code.Add(new Core.Instruction { Code = Core.Code.StackSpace, Operand = (body.Variables.Count << 16)
| body.MaxStackSize }); // local | maxstack
foreach(var variable in body.Variables)
{
if (variable.VariableType.IsValueType && !variable.VariableType.IsPrimitive)
{
code.Add(new Core.Instruction
{
Code = Core.Code.Ldloca,
Operand = variable.Index,
});
code.Add(new Core.Instruction
{
Code = Core.Code.Initobj,
Operand = addExternType(variable.VariableType)
});
}
foreach(var variable in body.Variables)
{
if (variable.VariableType.IsValueType && !variable.VariableType.IsPrimitive)
{
code.Add(new Core.Instruction
{
Code = Core.Code.Ldloca,
Operand = variable.Index,
});
code.Add(new Core.Instruction
{
Code = Core.Code.Initobj,
Operand = addExternType(variable.VariableType)
});
}
}
Core.ExceptionHandler[] exceptionHandlers = new Core.ExceptionHandler[body.ExceptionHandlers.Count];
......@@ -1748,6 +1748,8 @@ namespace IFix
private TypeReference voidType;
private TypeDefinition wrapperType;
private TypeDefinition idMapType;
private TypeReference enumType;
private List<TypeDefinition> idMapList;
private TypeDefinition itfBridgeType;
private int bridgeMethodId;
private TypeReference anonymousStoreyTypeRef;
......@@ -2516,12 +2518,9 @@ namespace IFix
//end init itfBridgeType
//begin init idMapper
var enumType = assembly.MainModule.ImportReference(typeof(System.Enum));
idMapType = new TypeDefinition("IFix", "IDMAP", TypeAttributes.Public | TypeAttributes.Sealed,
enumType);
assembly.MainModule.Types.Add(idMapType);
idMapType.Fields.Add(new FieldDefinition("value__", FieldAttributes.Public | FieldAttributes.SpecialName
| FieldAttributes.RTSpecialName, assembly.MainModule.TypeSystem.Int32));
enumType = assembly.MainModule.ImportReference(typeof(System.Enum));
idMapList = new List<TypeDefinition>();
idMapType = null;
//end init idMapper
wrapperMethods = new List<MethodDefinition>();
......@@ -2594,6 +2593,24 @@ namespace IFix
initStackOp(Call, assembly.MainModule.TypeSystem.UIntPtr);
}
const int MAX_ID_MAP_FIELD_COUNT = 32760;
void idMapTypeCheck()
{
if (idMapType == null || idMapType.Fields.Count >= MAX_ID_MAP_FIELD_COUNT)
{
if (idMapType != null)
{
idMapList.Add(idMapType);
}
idMapType = new TypeDefinition("IFix", "IDMAP" + idMapList.Count, TypeAttributes.Public | TypeAttributes.Sealed,
enumType);
assembly.MainModule.Types.Add(idMapType);
idMapType.Fields.Add(new FieldDefinition("value__", FieldAttributes.Public | FieldAttributes.SpecialName
| FieldAttributes.RTSpecialName, assembly.MainModule.TypeSystem.Int32));
}
}
void initStackOp(TypeDefinition call, TypeReference type)
{
pushMap[type] = importMethodReference(call, "Push" + type.Name);
......@@ -2842,6 +2859,7 @@ namespace IFix
{
throw new Exception("try inject method twice: " + method);
}
idMapTypeCheck();
var redirectIdField = new FieldDefinition("tmp_r_field_" + redirectIdMap.Count, FieldAttributes.Public
| FieldAttributes.Static | FieldAttributes.Literal | FieldAttributes.HasDefault, idMapType);
idMapType.Fields.Add(redirectIdField);
......@@ -2932,6 +2950,8 @@ namespace IFix
}
}
}
idMapList.Add(idMapType);
idMapType = null;
}
//1、构造函数及析构函数不转,不支持的指令不转,转的函数留下函数定义,所以支持反射
......@@ -3269,9 +3289,9 @@ namespace IFix
{
paramType = (paramType as GenericParameter).ResolveGenericArgument(method.DeclaringType);
}
if (paramType.IsRequiredModifier)
{
paramType = (paramType as RequiredModifierType).ElementType;
if (paramType.IsRequiredModifier)
{
paramType = (paramType as RequiredModifierType).ElementType;
}
if (!externTypeToId.ContainsKey(paramType))
{
......@@ -3431,8 +3451,24 @@ namespace IFix
writeSlotInfo(writer, anonymousType);
}
writer.Write(wrapperMgrImpl.GetAssemblyQualifiedName());
writer.Write(idMapType.GetAssemblyQualifiedName());
writer.Write(wrapperMgrImpl.GetAssemblyQualifiedName());
TypeDefinition idMap0 = null;
if (idMapList.Count == 0)
{
if (idMapType == null)
{
idMapTypeCheck();
}
idMap0 = idMapType;
idMapType = null;
}
else
{
idMap0 = idMapList[0];
}
var idMap0Name = idMap0.GetAssemblyQualifiedName();
writer.Write(idMap0Name.Substring("IFix.IDMAP0".Length));
writer.Write(interpretMethods.Count);
//Console.WriteLine("interpretMethods.Count:" + interpretMethods.Count);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册