public static string GetTypeMarshalPrologueCppCli(ManagedParameter parameter)
 {
     switch (parameter.Native.Type.Name)
     {
         case "btMatrix3x3":
             return "MATRIX3X3_CONV(" + parameter.Name + ");";
         case "btQuaternion":
             return "QUATERNION_CONV(" + parameter.Name + ");";
         case "btTransform":
             return "TRANSFORM_CONV(" + parameter.Name + ");";
         case "btVector4":
             return "VECTOR4_CONV(" + parameter.Name + ");";
         default:
             return null;
     }
 }
        public ManagedMethod(MethodDefinition nativeMethod, ManagedClass parent, string name)
        {
            Native = nativeMethod;
            Parent = parent;
            Name = name;

            Parameters = nativeMethod.Parameters.Select(p => new ManagedParameter(p)).ToArray();
            if (nativeMethod.OutValueParameter != null)
            {
                OutValueParameter = new ManagedParameter(nativeMethod.OutValueParameter);
            }

            if (parent != null)
            {
                parent.Methods.Add(this);
            }
        }
Example #3
0
        public static string GetTypeMarshalFieldSetCppCli(FieldDefinition field, ManagedParameter parameter, string nativePointer)
        {
            switch (field.Type.Name)
            {
            case "btQuaternion":
                return("Math::QuaternionToBtQuat(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')');

            case "btTransform":
                return("Math::MatrixToBtTransform(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')');

            case "btVector4":
                return("Math::Vector4ToBtVector4(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')');

            default:
                return(null);
            }
        }
        public ManagedMethod(MethodDefinition nativeMethod, ManagedClass parent, string name)
        {
            Native = nativeMethod;
            Parent = parent;
            Name   = name;

            Parameters = nativeMethod.Parameters.Select(p => new ManagedParameter(p)).ToArray();
            if (nativeMethod.OutValueParameter != null)
            {
                OutValueParameter = new ManagedParameter(nativeMethod.OutValueParameter);
            }

            if (parent != null)
            {
                parent.Methods.Add(this);
            }
        }
Example #5
0
        private static string GetParameterMarshal(ManagedParameter param)
        {
            var type = param.Native.Type.Canonical;

            if ((type.Target != null && type.Target.MarshalAsStruct) ||
                (type.Kind == TypeKind.LValueReference && type.Referenced.Target != null && type.Referenced.Target.MarshalAsStruct))
            {
                if (param.Native.MarshalDirection == MarshalDirection.Out)
                {
                    return($"out {param.Name}");
                }
                return($"ref {param.Name}");
            }

            if (type.Kind == TypeKind.LValueReference && type.Referenced.Canonical.IsBasic)
            {
                switch (param.Native.MarshalDirection)
                {
                case MarshalDirection.Out:
                    return($"out {param.Name}");

                case MarshalDirection.InOut:
                    return($"ret {param.Name}");

                default:
                    return(param.Name);
                }
            }

            if (type.Referenced != null)
            {
                switch (type.Name)
                {
                case "btIDebugDraw":
                    return("DebugDraw.GetUnmanaged(" + param.Name + ')');
                }

                if (!(type.Kind == TypeKind.Pointer && type.Referenced.Kind == TypeKind.Void))
                {
                    return(param.Name + "._native");
                }
            }

            return(param.Name);
        }
Example #6
0
        public static string GetTypeMarshalPrologueCppCli(ManagedParameter parameter)
        {
            switch (parameter.Native.Type.Name)
            {
            case "btMatrix3x3":
                return("MATRIX3X3_CONV(" + parameter.Name + ");");

            case "btQuaternion":
                return("QUATERNION_CONV(" + parameter.Name + ");");

            case "btTransform":
                return("TRANSFORM_CONV(" + parameter.Name + ");");

            case "btVector4":
                return("VECTOR4_CONV(" + parameter.Name + ");");

            default:
                return(null);
            }
        }
Example #7
0
        public static string GetTypeMarshalCppCli(ManagedParameter parameter)
        {
            switch (parameter.Native.Type.Name)
            {
            case "btIDebugDraw":
                return("DebugDraw::GetUnmanaged(" + parameter.Name + ")");

            case "btQuaternion":
                return("QUATERNION_USE(" + parameter.Name + ")");

            case "btTransform":
                return("TRANSFORM_USE(" + parameter.Name + ")");

            case "btMatrix3x3":
                return("MATRIX3X3_USE(" + parameter.Name + ")");

            case "btVector4":
                return("VECTOR4_USE(" + parameter.Name + ")");

            default:
                return(null);
            }
        }
        private static string GetParameterMarshal(ManagedParameter param)
        {
            var type = param.Native.Type.Canonical;

            if ((type.Target != null && type.Target.MarshalAsStruct) ||
                (type.Kind == TypeKind.LValueReference && type.Referenced.Target != null && type.Referenced.Target.MarshalAsStruct))
            {
                if (param.Native.MarshalDirection == MarshalDirection.Out)
                {
                    return $"out {param.Name}";
                }
                return $"ref {param.Name}";
            }

            if (type.Kind == TypeKind.LValueReference && type.Referenced.Canonical.IsBasic)
            {
                switch (param.Native.MarshalDirection)
                {
                    case MarshalDirection.Out:
                        return $"out {param.Name}";
                    case MarshalDirection.InOut:
                        return $"ret {param.Name}";
                    default:
                        return param.Name;
                }
            }

            if (type.Referenced != null)
            {
                switch (type.Name)
                {
                    case "btIDebugDraw":
                        return "DebugDraw.GetUnmanaged(" + param.Name + ')';
                }

                if (!(type.Kind == TypeKind.Pointer && type.Referenced.Kind == TypeKind.Void))
                {
                    return param.Name + "._native";
                }
            }

            return param.Name;
        }
Example #9
0
        private void WriteMethodDeclaration(ManagedMethod method, int numParameters, int level, int overloadIndex,
                                            ManagedParameter outValueParameter = null)
        {
            var nativeMethod = method.Native;

            EnsureWhiteSpace(WriteTo.CS);

            WriteTo cs;
            WriteTo dllImport = WriteTo.Buffer;

            if (method.Property != null)
            {
                // Do not write accessor methods of C# properties here
                cs = WriteTo.None;

                // Cached properties that are initialized only once
                // do not need a DllImport for the get method
                if (method.Parent.CachedProperties.ContainsKey(method.Property.Name) &&
                    method.Equals(method.Property.Getter))
                {
                    dllImport = WriteTo.None;
                }
            }
            else if (method.Name.Equals("delete"))
            {
                WriteDeleteMethodCS(method, level);
                cs = WriteTo.None;
            }
            else
            {
                Write(level + 1, "public ", WriteTo.CS);
                cs = WriteTo.CS;
            }

            // DllImport clause
            WriteLine(level + 1,
                      "[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]",
                      dllImport);
            if (nativeMethod.ReturnType != null && nativeMethod.ReturnType.Kind == TypeKind.Bool)
            {
                WriteLine(level + 1, "[return: MarshalAs(UnmanagedType.I1)]", dllImport);
            }
            Write(level + 1, "static extern ", dllImport);

            // Return type
            if (nativeMethod.IsConstructor)
            {
                Write("IntPtr ", dllImport);
            }
            else
            {
                var returnType = nativeMethod.ReturnType.Canonical;

                if (nativeMethod.IsStatic)
                {
                    Write("static ", cs);
                }
                Write($"{GetTypeNameCS(returnType)} ", cs);

                if (outValueParameter != null)
                {
                    Write("void ", dllImport);
                }
                else
                {
                    if (returnType.IsBasic)
                    {
                        Write(GetTypeNameCS(returnType), dllImport);
                    }
                    else if (returnType.Referenced != null)
                    {
                        if (returnType.Kind == TypeKind.LValueReference &&
                            returnType.Referenced.IsConst && returnType.Referenced.Canonical.IsBasic)
                        {
                            Write(GetTypeNameCS(returnType.Referenced.Canonical), dllImport);
                        }
                        else
                        {
                            Write("IntPtr", dllImport);
                        }
                    }
                    else
                    {
                        // Return structures to an additional out parameter, not immediately
                        Write("void", dllImport);
                    }
                    Write(' ', dllImport);
                }
            }


            // Name
            string methodName = nativeMethod.IsConstructor ? "new" : method.Native.Name;

            if (overloadIndex != 0)
            {
                methodName += (overloadIndex + 1).ToString();
            }
            Write($"{GetFullNameC(method.Parent.Native)}_{methodName}(", dllImport);
            Write($"{method.Name}(", cs);


            // Parameters
            var parameters   = method.Parameters.Take(numParameters);
            var parametersCs = parameters.Select(p => $"{GetParameterTypeNameCS(p.Native)} {p.Name}");

            WriteLine($"{ListToLines(parametersCs, WriteTo.CS, level + 1)})", cs);

            if (outValueParameter != null)
            {
                parameters = parameters.Concat(new[] { outValueParameter });
            }

            var parametersDllImport = parameters.Select(p => $"{GetParameterTypeNameDllImport(p.Native)} {p.Native.Name}");

            // The first parameter is the instance pointer (if not constructor or static method)
            if (!nativeMethod.IsConstructor && !nativeMethod.IsStatic)
            {
                parametersDllImport =
                    new[] { "IntPtr obj" }.Concat(parametersDllImport);
            }

            WriteLine($"{string.Join(", ", parametersDllImport)});", dllImport);
        }
Example #10
0
        void WriteMethodDefinition(ManagedMethod method, int numParameters, int overloadIndex, int level,
                                   ManagedParameter outValueParameter)
        {
            var nativeMethod = method.Native;

            if (nativeMethod.IsConstructor)
            {
                if (method.Parent.BaseClass == null)
                {
                    Write("_native = ");
                }
                Write($"{GetFullNameC(method.Parent.Native)}_new");
            }
            else
            {
                if (!nativeMethod.IsVoid)
                {
                    if (outValueParameter != null)
                    {
                        // Temporary variable
                        WriteLine(string.Format("{0} {1};",
                                                DotNetParser.GetManaged(outValueParameter.Native.Type.Referenced.Target).Name,
                                                outValueParameter.Name));
                        WriteTabs(level + 2);
                    }
                    else
                    {
                        Write($"return {BulletParser.GetTypeMarshalConstructorStartCS(nativeMethod)}");
                    }
                }

                Write($"{GetFullNameC(method.Parent.Native)}_{method.Native.Name}");
            }

            if (overloadIndex != 0)
            {
                Write((overloadIndex + 1).ToString());
            }

            Write('(');

            var parameters = method.Parameters.Take(numParameters)
                             .Select(p => GetParameterMarshal(p));

            if (outValueParameter != null)
            {
                parameters = parameters.Concat(new[] { $"out {outValueParameter.Name }" });
            }

            // The first parameter is the instance pointer (if not constructor or static method)
            if (!nativeMethod.IsConstructor && !nativeMethod.IsStatic)
            {
                parameters = new[] { "_native" }.Concat(parameters);
            }

            Write(ListToLines(parameters, WriteTo.CS, level + 2));

            if (nativeMethod.IsConstructor && method.Parent.BaseClass != null)
            {
                Write(")");
                if (method.Parent.BaseClass.Native.HasPreventDelete)
                {
                    Write(", false");
                }
                WriteLine(")");
                WriteLine(level + 1, "{");
            }
            else
            {
                if (!nativeMethod.IsConstructor && !nativeMethod.IsVoid)
                {
                    Write(BulletParser.GetTypeMarshalConstructorEndCS(nativeMethod));
                }
                WriteLine(");");
            }

            // Cache property values
            if (nativeMethod.IsConstructor)
            {
                var methodParent = method.Parent;
                while (methodParent != null)
                {
                    foreach (var cachedProperty in methodParent.CachedProperties.OrderBy(p => p.Key))
                    {
                        foreach (var param in method.Parameters)
                        {
                            if (param.Name.ToLower().Equals(cachedProperty.Key.ToLower()) &&
                                GetName(param.Native.Type).Equals(GetName(cachedProperty.Value.Property.Type)))
                            {
                                WriteLine(level + 2,
                                          $"{cachedProperty.Value.CacheFieldName} = {param.Name};");
                            }
                        }
                    }
                    methodParent = methodParent.BaseClass;
                }
            }

            // Return temporary variable
            if (outValueParameter != null)
            {
                WriteLine(level + 2, $"return {outValueParameter.Name};");
            }
        }
 public static string GetTypeMarshalFieldSetCppCli(FieldDefinition field, ManagedParameter parameter, string nativePointer)
 {
     switch (field.Type.Name)
     {
         case "btQuaternion":
             return "Math::QuaternionToBtQuat(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')';
         case "btTransform":
             return "Math::MatrixToBtTransform(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')';
         case "btVector4":
             return "Math::Vector4ToBtVector4(" + parameter.Name + ", &" + nativePointer + "->" + field.Name + ')';
         default:
             return null;
     }
 }
 public static string GetTypeMarshalCppCli(ManagedParameter parameter)
 {
     switch (parameter.Native.Type.Name)
     {
         case "btIDebugDraw":
             return "DebugDraw::GetUnmanaged(" + parameter.Name + ")";
         case "btQuaternion":
             return "QUATERNION_USE(" + parameter.Name + ")";
         case "btTransform":
             return "TRANSFORM_USE(" + parameter.Name + ")";
         case "btMatrix3x3":
             return "MATRIX3X3_USE(" + parameter.Name + ")";
         case "btVector4":
             return "VECTOR4_USE(" + parameter.Name + ")";
         default:
             return null;
     }
 }