Exemplo n.º 1
0
        private static void AppendBinaryOperation(ModuleDefinition moduleDefinition, TypeDefinition definition, InstructionData instructionData, string baseName, Instruction operation)
        {
            var method = FieldOperationMethod.Create(moduleDefinition, definition, $"{baseName}{instructionData.type.Name}", instructionData.GetTypeReference(moduleDefinition), instructionData.GetBinaryConfigTypeDefinition(moduleDefinition), $"core_field_operation_{baseName.ToLower()}", baseName, instructionData.type.Name.ToLower());

            method.definition.Body.InitLocals = true;
            method.definition.Body.SimplifyMacros();
            var data = new VariableDefinition(instructionData.GetBinaryConfigTypeDefinition(moduleDefinition));

            method.definition.Body.Variables.Add(data);
            var processor = method.definition.Body.GetILProcessor();

            processor.Emit(OpCodes.Nop);
            processor.Emit(OpCodes.Ldarga_S, method.configParameter);
            processor.Emit(OpCodes.Call, moduleDefinition.ImportReference(typeof(IntPtr).GetMethod(nameof(IntPtr.ToPointer))));
            var call = new GenericInstanceMethod(moduleDefinition.ImportReference(typeof(UnsafeUtility).GetMethod(nameof(UnsafeUtility.AsRef))));

            call.GenericArguments.Add(instructionData.GetBinaryConfigTypeDefinition(moduleDefinition));
            processor.Emit(OpCodes.Call, call);
            processor.Emit(OpCodes.Ldobj, instructionData.GetBinaryConfigTypeDefinition(moduleDefinition));
            processor.Emit(OpCodes.Stloc, data);
            processor.Emit(OpCodes.Ldarg_1);
            processor.Emit(OpCodes.Call, moduleDefinition.ImportReference(typeof(IntPtr).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance).First(m => m.Name == "op_Explicit" && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(IntPtr) && m.ReturnType == typeof(void *))));
            processor.Emit(OpCodes.Ldloc_0);
            processor.Emit(OpCodes.Ldfld, instructionData.GetBinaryConfigTypeDefinition(moduleDefinition).Fields[0]);
            processor.Emit(OpCodes.Ldloc_0);
            processor.Emit(OpCodes.Ldfld, instructionData.GetBinaryConfigTypeDefinition(moduleDefinition).Fields[1]);
            processor.Append(operation);
            processor.Append(instructionData.GetStoreInstruction(moduleDefinition));
            processor.Emit(OpCodes.Ret);
            method.definition.Body.OptimizeMacros();
        }
Exemplo n.º 2
0
            public static FieldOperationMethod Create(ModuleDefinition moduleDefinition, TypeDefinition container, string name, TypeReference returnType, TypeDefinition config, string identifier, string displayName, string subIdentifier)
            {
                var method = new FieldOperationMethod
                {
                    definition      = new MethodDefinition(name, Mono.Cecil.MethodAttributes.Static | Mono.Cecil.MethodAttributes.Public, moduleDefinition.TypeSystem.Void),
                    configParameter = new ParameterDefinition(moduleDefinition.TypeSystem.IntPtr),
                    outputParameter = new ParameterDefinition(moduleDefinition.TypeSystem.IntPtr)
                };

                method.definition.Parameters.Add(method.configParameter);
                method.definition.Parameters.Add(method.outputParameter);
                method.definition.CustomAttributes.Add(new CustomAttribute(moduleDefinition.ImportReference(typeof(BurstCompileAttribute).GetConstructor(Type.EmptyTypes))));
                var attr = new CustomAttribute(moduleDefinition.ImportReference(typeof(FieldOperationAttribute).GetConstructors()[0]));

                attr.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.TypeSystem.String, identifier));
                attr.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.ImportReference(Type.GetType("System.Type")), config));
                attr.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.TypeSystem.String, displayName));
                attr.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.TypeSystem.String, subIdentifier));
                attr.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.ImportReference(Type.GetType("System.Type")), returnType));

                method.definition.CustomAttributes.Add(attr);
                var pInvoke = new CustomAttribute(moduleDefinition.ImportReference(typeof(MonoPInvokeCallbackAttribute).GetConstructors()[0]));

                pInvoke.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.ImportReference(Type.GetType("System.Type")), moduleDefinition.ImportReference(typeof(FieldOperation))));
                method.definition.CustomAttributes.Add(pInvoke);
                container.Methods.Add(method.definition);
                return(method);
            }
Exemplo n.º 3
0
        private static void AppendMathOperation(ModuleDefinition moduleDefinition, TypeDefinition container, TypeDefinition config, MethodInfo methodInfo)
        {
            var method = FieldOperationMethod.Create(moduleDefinition, container, $"MathOperation_{methodInfo.Name}_{config.Name}", moduleDefinition.ImportReference(methodInfo.ReturnType), config, $"core_field_operation_math_{methodInfo.Name}", CultureInfo.InvariantCulture.TextInfo.ToTitleCase(methodInfo.Name), GetSubIdentifier(methodInfo));

            method.definition.Body.InitLocals = true;
            method.definition.Body.SimplifyMacros();
            var data = new VariableDefinition(config);

            method.definition.Body.Variables.Add(data);
            var processor = method.definition.Body.GetILProcessor();

            processor.Emit(OpCodes.Nop);
            processor.Emit(OpCodes.Ldarga_S, method.configParameter);
            processor.Emit(OpCodes.Call, moduleDefinition.ImportReference(typeof(IntPtr).GetMethod(nameof(IntPtr.ToPointer))));
            var call = new GenericInstanceMethod(moduleDefinition.ImportReference(typeof(UnsafeUtility).GetMethod(nameof(UnsafeUtility.AsRef))));

            call.GenericArguments.Add(config);
            processor.Emit(OpCodes.Call, call);
            processor.Emit(OpCodes.Ldobj, config);
            processor.Emit(OpCodes.Stloc, data);
            processor.Emit(OpCodes.Ldarg_1);
            processor.Emit(OpCodes.Call, moduleDefinition.ImportReference(typeof(IntPtr).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance).First(m => m.Name == "op_Explicit" && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(IntPtr) && m.ReturnType == typeof(void *))));
            foreach (var field in config.Fields)
            {
                processor.Emit(OpCodes.Ldloc_0);
                processor.Emit(OpCodes.Ldfld, field);
            }
            processor.Emit(OpCodes.Call, moduleDefinition.ImportReference(methodInfo));
            processor.Emit(OpCodes.Stobj, moduleDefinition.ImportReference(methodInfo.ReturnType));
            processor.Emit(OpCodes.Ret);
            method.definition.Body.OptimizeMacros();
        }