private MethodDefinition AddGetUnityTypeMethod(TypeDefinition type, AssemblyWrapper assemblyWrapper)
        {
            MethodDefinition item = new MethodDefinition("GetUnityType", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, base.ModuleContext.GetCorLibType("System.IntPtr"));

            type.Methods.Add(item);
            ParameterDefinition definition2 = new ParameterDefinition("id", ParameterAttributes.None, base.ModuleContext.GetCorLibType("System.Int32"));

            item.Parameters.Add(definition2);
            MethodBody  body        = item.Body;
            ILProcessor iLProcessor = body.GetILProcessor();
            Collection <Instruction> instructions = body.Instructions;

            iLProcessor.EmitLdarg(definition2);
            iLProcessor.EmitLdc_I4(assemblyWrapper.FirstTypeId);
            iLProcessor.Emit(OpCodes.Sub);
            Instruction[] targets = new Instruction[assemblyWrapper.Types.Length];
            iLProcessor.Emit(OpCodes.Switch, targets);
            iLProcessor.EmitLdc_I4(0);
            iLProcessor.Emit(OpCodes.Conv_I);
            iLProcessor.Emit(OpCodes.Ret);
            for (int i = 0; i < assemblyWrapper.Types.Length; i++)
            {
                iLProcessor.Emit(OpCodes.Newobj, assemblyWrapper.Types[i].UnityType.Methods.Single <MethodDefinition>(< > c.< > 9__3_0 ?? (< > c.< > 9__3_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddGetUnityTypeMethod > b__3_0))));
                targets[i] = instructions.Last <Instruction>();
                iLProcessor.Emit(OpCodes.Call, this.allocMethod);
                iLProcessor.Emit(OpCodes.Call, this.toIntPtrMethod);
                iLProcessor.Emit(OpCodes.Ret);
            }
            return(item);
        }
        private MethodDefinition AddCreateInstanceMethod(TypeWrapper typeWrapper, TypeDefinition type)
        {
            MethodDefinition item        = new MethodDefinition("CreateInstance", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, this.objectType);
            MethodBody       body        = item.Body;
            ILProcessor      iLProcessor = body.GetILProcessor();

            if (typeWrapper.Type.IsValueType)
            {
                body.InitLocals = true;
                VariableDefinition definition2 = new VariableDefinition("instance", typeWrapper.Type);
                body.Variables.Add(definition2);
                iLProcessor.EmitLdloca(definition2);
                iLProcessor.Emit(OpCodes.Initobj, typeWrapper.Type);
                iLProcessor.EmitLdloc(definition2);
                iLProcessor.Emit(OpCodes.Box, typeWrapper.Type);
            }
            else
            {
                if (typeWrapper.SpecialConstructor == null)
                {
                    throw new Exception(string.Format("CreateInstance method can't be added for type \"{0}\".", typeWrapper.Type.FullName));
                }
                iLProcessor.EmitLdc_I4(0);
                iLProcessor.Emit(OpCodes.Conv_U);
                iLProcessor.Emit(OpCodes.Newobj, typeWrapper.SpecialConstructor);
            }
            iLProcessor.Emit(OpCodes.Ret);
            type.Methods.Add(item);
            return(item);
        }
 private void FillMapsForType(ILProcessor ilProcessor, TypeWrapper type)
 {
     ilProcessor.Emit(OpCodes.Ldtoken, this.Import(type.Type));
     ilProcessor.Emit(OpCodes.Call, this.typeOfMethod);
     ilProcessor.Emit(OpCodes.Stloc_0);
     ilProcessor.Emit(OpCodes.Ldarg_0);
     ilProcessor.Emit(OpCodes.Ldloc_0);
     ilProcessor.EmitLdc_I4(type.Id);
     ilProcessor.Emit(OpCodes.Callvirt, this.dictionaryAddMethod);
     ilProcessor.Emit(OpCodes.Ldarg_1);
     ilProcessor.Emit(OpCodes.Ldloc_0);
     ilProcessor.Emit(OpCodes.Callvirt, this.listAddMethod);
 }
        private void ConstructList(ILProcessor ilProcessor, int typeCount)
        {
            ilProcessor.Body.Variables.Add(new VariableDefinition(this.listInstanceType));
            MethodDefinition method = this.listType.Methods.Single <MethodDefinition>(< > c.< > 9__16_0 ?? (< > c.< > 9__16_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < ConstructList > b__16_0)));

            TypeReference[] arguments = new TypeReference[] { this.systemTypeType };
            MethodReference reference = this.Import(method).MakeGenericMethod(arguments);

            ilProcessor.Emit(OpCodes.Ldarg_1);
            ilProcessor.EmitLdc_I4(typeCount);
            ilProcessor.Emit(OpCodes.Newobj, reference);
            ilProcessor.Emit(OpCodes.Dup);
            ilProcessor.Emit(OpCodes.Stloc_1);
            ilProcessor.Emit(OpCodes.Stind_Ref);
        }
        private static MethodDefinition Process(ModuleContext moduleContext, MethodWrapper methodWrapper, int index)
        {
            TypeReference    corLibType   = moduleContext.GetCorLibType("System.Int64");
            MethodReference  method       = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsGCHandleToObjectMethod);
            MethodReference  reference3   = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsGCHandleToPinnedArrayObjectMethod);
            MethodReference  reference4   = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsObjectToGCHandleMethod);
            MethodReference  corLibMethod = moduleContext.GetCorLibMethod("System.Runtime.InteropServices.Marshal", <> c.< > 9__7_0 ?? (< > c.< > 9__7_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < Process > b__7_0)));
            MethodDefinition targetMethod = methodWrapper.TargetMethod;
            MethodDefinition definition2  = new MethodDefinition(string.Format("$Invoke{0}", index), MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, corLibType);

            methodWrapper.InvokeMethod = definition2;
            ParameterDefinition item        = new ParameterDefinition("instance", ParameterAttributes.None, corLibType);
            ParameterDefinition definition4 = new ParameterDefinition("args", ParameterAttributes.None, new PointerType(corLibType));

            definition2.Parameters.Add(item);
            definition2.Parameters.Add(definition4);
            ILProcessor iLProcessor = definition2.Body.GetILProcessor();

            if (targetMethod.HasThis)
            {
                iLProcessor.EmitLdarg(item);
                iLProcessor.Emit(OpCodes.Call, method);
                if (targetMethod.DeclaringType.MetadataType != MetadataType.Object)
                {
                    iLProcessor.Emit(OpCodes.Castclass, moduleContext.Import(methodWrapper.DeclaringType.Type));
                }
            }
            for (int i = 0; i < targetMethod.Parameters.Count; i++)
            {
                TypeDefinition      definition6;
                ParameterDefinition definition5 = targetMethod.Parameters[i];
                iLProcessor.EmitLdarg(definition4);
                if (i != 0)
                {
                    iLProcessor.EmitLdc_I4(i * 8);
                    iLProcessor.Emit(OpCodes.Add);
                }
                switch (definition5.ParameterType.MetadataType)
                {
                case MetadataType.Boolean:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I1);
                    continue;
                }

                case MetadataType.Byte:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I1);
                    continue;
                }

                case MetadataType.Int32:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I4);
                    continue;
                }

                case MetadataType.Int64:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    continue;
                }

                case MetadataType.Single:
                {
                    iLProcessor.Emit(OpCodes.Ldind_R4);
                    continue;
                }

                case MetadataType.Double:
                {
                    iLProcessor.Emit(OpCodes.Ldind_R8);
                    continue;
                }

                case MetadataType.String:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    iLProcessor.Emit(OpCodes.Call, corLibMethod);
                    continue;
                }

                case MetadataType.ValueType:
                {
                    definition6 = definition5.ParameterType.Resolve();
                    if (!definition6.IsEnum)
                    {
                        break;
                    }
                    iLProcessor.Emit(OpCodes.Ldind_I4);
                    continue;
                }

                case MetadataType.Class:
                case MetadataType.GenericInstance:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    iLProcessor.Emit(definition5.ParameterType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, moduleContext.Import(definition5.ParameterType));
                    continue;
                }

                case MetadataType.Array:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, reference3);
                    iLProcessor.Emit(OpCodes.Castclass, moduleContext.Import(definition5.ParameterType));
                    continue;
                }

                case MetadataType.IntPtr:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    continue;
                }

                case MetadataType.Object:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    continue;
                }

                default:
                    throw new NotSupportedException(string.Format("Parameter type {0} is not supported.", definition5.ParameterType.FullName));
                }
                if (!IsPodStruct(definition6))
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    iLProcessor.Emit(OpCodes.Unbox_Any, moduleContext.Import(definition5.ParameterType));
                }
                else
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    iLProcessor.Emit(OpCodes.Ldobj, moduleContext.Import(definition5.ParameterType));
                }
            }
            iLProcessor.Emit(targetMethod.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, moduleContext.Import(methodWrapper.InvokeTargetMethod));
            switch (targetMethod.ReturnType.MetadataType)
            {
            case MetadataType.Void:
                iLProcessor.EmitLdc_I4(-1);
                iLProcessor.Emit(OpCodes.Conv_I8);
                break;

            case MetadataType.Boolean:
            case MetadataType.Byte:
            case MetadataType.Int32:
            case MetadataType.Int64:
            case MetadataType.Single:
            case MetadataType.ValueType:
                iLProcessor.Emit(OpCodes.Box, moduleContext.Import(targetMethod.ReturnType));
                iLProcessor.Emit(OpCodes.Call, reference4);
                break;

            case MetadataType.String:
            case MetadataType.Class:
            case MetadataType.Array:
            case MetadataType.GenericInstance:
            case MetadataType.Object:
                if (targetMethod.ReturnType.IsValueType)
                {
                    iLProcessor.Emit(OpCodes.Box, moduleContext.Import(targetMethod.ReturnType));
                }
                iLProcessor.Emit(OpCodes.Call, reference4);
                break;

            default:
                throw new NotSupportedException(string.Format("Return type {0} is not supported.", targetMethod.ReturnType.FullName));
            }
            iLProcessor.Emit(OpCodes.Ret);
            return(definition2);
        }
Пример #6
0
        private void AddLoadMethod(TypeDefinition type, FieldDefinition dataField)
        {
            MethodDefinition item = new MethodDefinition("Load", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, base.ModuleContext.IntPtrType);

            type.Methods.Add(item);
            MethodBody         body         = item.Body;
            ILProcessor        iLProcessor  = body.GetILProcessor();
            TypeReference      corLibType   = base.ModuleContext.GetCorLibType("System.Byte");
            TypeReference      variableType = base.ModuleContext.GetCorLibType("System.Int32");
            TypeReference      reference3   = base.ModuleContext.GetCorLibType("System.IntPtr");
            TypeReference      reference4   = base.ModuleContext.GetCorLibType("System.Runtime.InteropServices.Marshal");
            TypeReference      reference5   = base.ModuleContext.GetCorLibType("System.IO.Stream");
            TypeReference      reference6   = base.ModuleContext.GetCorLibType("System.Type");
            VariableDefinition definition2  = new VariableDefinition("stream", reference5);
            VariableDefinition definition3  = new VariableDefinition("length", variableType);
            VariableDefinition definition4  = new VariableDefinition("buffer", new ArrayType(corLibType));
            VariableDefinition definition5  = new VariableDefinition("result", reference3);

            body.Variables.Add(definition2);
            body.Variables.Add(definition3);
            body.Variables.Add(definition4);
            body.Variables.Add(definition5);
            body.InitLocals = true;
            iLProcessor.Emit(OpCodes.Ldtoken, type);
            iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference6, "GetTypeFromHandle"));
            if (base.OperationContext.IsWSA)
            {
                iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod("System.Reflection.IntrospectionExtensions", "GetTypeInfo"));
                iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.TypeInfo", "get_Assembly"));
            }
            else
            {
                iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference6, "get_Assembly"));
            }
            iLProcessor.Emit(OpCodes.Ldstr, "UnityMetadata");
            iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.Assembly", <> c.< > 9__2_0 ?? (< > c.< > 9__2_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_0))));
            iLProcessor.EmitStloc(definition2);
            iLProcessor.EmitLdloc(definition2);
            iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "get_Length"));
            iLProcessor.Emit(OpCodes.Conv_I4);
            iLProcessor.EmitStloc(definition3);
            iLProcessor.EmitLdloc(definition3);
            iLProcessor.Emit(OpCodes.Newarr, corLibType);
            iLProcessor.EmitStloc(definition4);
            iLProcessor.EmitLdloc(definition2);
            iLProcessor.EmitLdloc(definition4);
            iLProcessor.EmitLdc_I4(0);
            iLProcessor.EmitLdloc(definition3);
            iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "Read"));
            iLProcessor.Emit(OpCodes.Pop);
            iLProcessor.EmitLdloc(definition2);
            iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, <> c.< > 9__2_1 ?? (< > c.< > 9__2_1 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_1))));
            iLProcessor.EmitLdloc(definition3);
            iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, "AllocCoTaskMem"));
            iLProcessor.EmitStloc(definition5);
            iLProcessor.EmitLdloc(definition4);
            iLProcessor.EmitLdc_I4(0);
            iLProcessor.EmitLdloc(definition5);
            iLProcessor.EmitLdloc(definition3);
            iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, <> c.< > 9__2_2 ?? (< > c.< > 9__2_2 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_2))));
            foreach (AssemblyWrapper wrapper in base.MetadataContainer.Assemblies)
            {
                if (wrapper.GetUnityTypeMethod != null)
                {
                    iLProcessor.EmitLdloc(definition5);
                    iLProcessor.EmitLdc_I4(wrapper.Offset);
                    iLProcessor.Emit(OpCodes.Add);
                    iLProcessor.Emit(OpCodes.Ldftn, base.ModuleContext.Import(wrapper.GetUnityTypeMethod));
                    iLProcessor.Emit(OpCodes.Stind_I);
                }
            }
            iLProcessor.EmitLdloc(definition5);
            iLProcessor.Emit(OpCodes.Stsfld, dataField);
            iLProcessor.EmitLdloc(definition5);
            iLProcessor.Emit(OpCodes.Ret);
        }