Пример #1
0
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public Methods

        public static string WriteEmptyClass(McgResource resource)
        {
            string publicAnimatableProperties = String.Empty;
            string protectedAnimatableMethods = String.Empty;
            string createInstanceCore         = String.Empty;

            createInstanceCore =
        public static string UpdateResource(McgResource resource)
        {
            if (!resource.HasUnmanagedResource)
            {
                return(String.Empty);
            }

            return
                ([[inline]]
            /// <summary>
            /// Builds the real field names and decides their type.
            /// </summary>
            internal void BuildMarshaledFieldNames()
            {
                int padSuffix = 0;

                foreach (AlignmentEntry entry in AlignmentEntries)
                {
                    McgField field = entry.Field;

                    //
                    // This primary if else else block switches between the tree types of entries in the
                    // paddedFields alignment list: padding, animation and normal field entries.
                    //
                    if (field == null)
                    {
                        entry.Name  = "padQuadAlignment" + padSuffix++;
                        entry.IsPad = true;
                    }
                    else if (entry.IsAnimation)
                    {
                        entry.Name     = "h" + field.PropertyName + "Animations";
                        entry.IsHandle = true;
                    }
                    else
                    {
                        // This case is a boring, non animate field.

                        McgType     type         = field.Type;
                        McgResource resourceType = type as McgResource;

                        // If it's an McgResource, we have to handle reference types and collections
                        if (resourceType != null && !resourceType.IsValueType)
                        {
                            entry.Name     = "h" + field.Name;
                            entry.IsHandle = true;
                        }
                        else
                        {
                            entry.Name = field.Name;

                            if (type.Name == "ResourceHandle")
                            {
                                entry.IsHandle = true;
                            }
                        }
                    }
                }
            }
Пример #4
0
        private void WriteGetHashCode(McgResource resource,
                                      StringCodeSink cs)
        {
            // At this time, we only know how to emit GetHashCode() for value types
            if (!resource.IsValueType) return;

            string getHashCodeBody = String.Empty;

            if (resource.LocalFields.Length < 1)
            {
                throw new System.NotSupportedException("Do not attempt to generate local fields for resources without local fields.");
            }
            else
            {
                StringCodeSink returnStmt = new StringCodeSink();

                // The inline block intentionally lacks a newline so that the first field
                // will be on the same line as the return.
                returnStmt.Write(
Пример #5
0
 /// <summary>
 /// Write a native struct definition
 /// </summary>
 public static string FormatNativeStructure(McgResource resource)
 {
     string decl =
Пример #6
0
        public PaddedCommand(ResourceModel resourceModel, McgResource resource)
        {
            m_resourceModel        = resourceModel;
            m_origin               = PaddedCommandOrigin.Resource;
            m_domain               = resource.Domain;
            m_target               = resource.Target;
            m_name                 = "Update";
            m_commandName          = resource.CommandName;
            m_targetResourceType   = "TYPE_" + resource.Name.ToUpper();
            m_targetResourceName   = resource.DuceClass;
            m_resourceIsCollection = resource.IsCollection;
            m_typeName             = resource.CommandTypeName;

            //
            // Do not generate managed structures for unmanaged only resources.
            //

            m_commandIsUnmanagedOnly =
                !resourceModel.ShouldGenerate(CodeSections.ManagedClass, resource);

            //
            // Fixup for resources having payloads.
            //
            //
            //

            if (resource.Name == "LinearGradientBrush" ||
                resource.Name == "RadialGradientBrush" ||
                resource.Name == "TransformGroup" ||
                resource.Name == "BitmapEffectGroup" ||
                resource.Name == "PixelShader" ||
                resource.Name == "ShaderEffect" ||
                resource.Name == "MultipassShaderEffect" ||
                resource.Name == "DrawingGroup" ||
                resource.Name == "PathGeometry" ||
                resource.Name == "GeometryGroup" ||
                resource.Name == "MaterialGroup" ||
                resource.Name == "Model3DGroup" ||
                resource.Name == "Transform3DGroup" ||
                resource.Name == "ScreenSpaceLines3D" ||
                resource.Name == "MeshGeometry3D" ||
                resource.Name == "DashStyle" ||
                resource.Name == "GuidelineSet" ||
                resource.Name == "MeshGeometry2D" ||
                resource.Name == "Geometry2DGroup"
                )
            {
                m_hasPayload = true;
            }

            m_paddedStructData =
                Helpers.CodeGenHelpers.SortStructForAlignment(
                    resource.AllUceFields,
                    true /* do animations */,
                    false /* don't pad entire structure */
                    );

            //
            // Prepend "Type" and "Handle" fields (in that order!)
            //

            PrependHandleField();
            PrependTypeField();

            if (m_resourceIsCollection)
            {
                //
                // Append the "Size" field for resource collections.
                //

                AppendCollectionSizeField();
            }

            m_paddedStructData.BuildMarshaledFieldNames();
        }
        /// <summary>
        /// AppendParameters - Helper routine to append parameters to a param list.
        /// </summary>
        internal static void AppendParameter(
            DelimitedList parameterList,
            McgType type,
            string name,
            string fieldName,
            string propertyName,
            string internalReadName,
            bool isAnimated,
            ParameterType parameterType)
        {
            isAnimated = isAnimated && ((parameterType & ParameterType.SkipAnimations) == 0);

            McgResource resourceType = type as McgResource;

            // We accumulate the parameter(s) in these strings.
            string paramString        = String.Empty;
            string animateParamString = String.Empty;

            if ((parameterType & ParameterType.ManagedParamList) != 0)
            {
                paramString = type.Name + " " + name;

                // Field is animated -- also pass resource by handle.
                if (((resourceType == null) || resourceType.IsValueType) && isAnimated)
                {
                    // Field is animated -- also pass resource by handle.
                    animateParamString = "AnimationClock " + name + "Animations";
                }
            }
            else if ((parameterType & ParameterType.ManagedCallParamList) != 0)
            {
                paramString = name;

                // Field is animated -- also pass resource by handle.
                if (((resourceType == null) || resourceType.IsValueType) && isAnimated)
                {
                    // Field is animated -- also pass resource by handle.
                    animateParamString = name + "Animations";
                }
            }
            else if ((parameterType & ParameterType.RenderDataCallParamList) != 0)
            {
                // Field is a resource that can not be passed by value.
                if (resourceType != null && !resourceType.IsValueType)
                {
                    paramString = "_renderData.AddDependentResource(" + fieldName + ")";
                }
                else
                {
                    paramString = ConvertToValueType(type, name);

                    if (isAnimated)
                    {
                        // Field is animated -- also pass resource by handle.
                        animateParamString = "h" + propertyName + "Animations";
                    }
                }
            }
            else if ((parameterType & ParameterType.ManagedImportsParamList) != 0)
            {
                // If it's not a value type, then we can pass by handle
                if ((resourceType != null) && !resourceType.IsValueType)
                {
                    paramString = DuceHandle.ManagedTypeName + " h" + propertyName;
                }
                else
                {
                    // If it is a value type, we need to know whether we convert it or not
                    if (type.NeedsConvert)
                    {
                        // If it does need a convert, then the import is typed to the
                        // unmanaged value type, because that's what the Convert* method
                        // returns.
                        paramString = type.UnmanagedDataType + " " + name;
                    }
                    else
                    {
                        // Otherwise, we know that this value type does not need a conversion,
                        // and thus should be passed directly as its managed type.
                        paramString = type.ManagedName + " " + name;
                    }

                    // Field is animated -- also pass resource by handle.
                    if (isAnimated)
                    {
                        animateParamString = DuceHandle.ManagedTypeName + " h" + GeneratorMethods.FirstCap(name) + "Animations";
                    }
                }
            }
            else if ((parameterType & ParameterType.UnmanagedParamList) != 0)
            {
                if ((resourceType != null) && !resourceType.IsValueType)
                {
                    paramString = DuceHandle.UnmanagedTypeName + " h" + GeneratorMethods.FirstCap(name);
                }
                else
                {
                    paramString = type.UnmanagedDataType + " " + name;

                    // Field is animated -- also pass resource by handle.
                    if (isAnimated)
                    {
                        animateParamString = DuceHandle.UnmanagedTypeName + " h" + GeneratorMethods.FirstCap(name) + "Animations";
                    }
                }
            }
            else
            {
                Debug.Assert((parameterType & ParameterType.UnmanagedCallParamList) != 0);

                if ((resourceType != null) && !resourceType.IsValueType)
                {
                    paramString = "h" + GeneratorMethods.FirstCap(name);
                }
                else
                {
                    paramString = name;

                    // Field is animated -- also pass resource by handle.
                    if (isAnimated)
                    {
                        animateParamString = "h" + GeneratorMethods.FirstCap(name) + "Animations";
                    }
                }
            }

            // animateParamString should either be Empty or have content - it shouldn't be null
            Debug.Assert(null != animateParamString);


            // Now that we have the parameter(s), we have to decide what to do with them
            if ((parameterType & ParameterType.AssignToMemberVariables) != 0)
            {
                parameterList.Append("this." + paramString + " = " + paramString);

                if (animateParamString.Length != 0)
                {
                    parameterList.Append("this." + animateParamString + " = " + animateParamString);
                }
            }
            else
            {
                parameterList.Append(paramString);

                if (animateParamString.Length != 0)
                {
                    parameterList.Append(animateParamString);
                }
            }
        }
        internal static int AppendStructParameters(
            DelimitedList parameterList,
            PaddedStructData paddedFields,
            int initialPosition,
            bool unmanagedStruct,
            bool kernelAccessibleStruct
            )
        {
            int padSuffix = 0;

            Helpers.CodeGenHelpers.AlignedFieldOffsetHelper alignedFieldHelper = new Helpers.CodeGenHelpers.AlignedFieldOffsetHelper(4, 4);

            foreach (AlignmentEntry alignmentEntry in paddedFields.AlignmentEntries)
            {
                McgField field    = alignmentEntry.Field;
                int      position = initialPosition + alignmentEntry.Offset;

                alignedFieldHelper.MoveToNextEntry(position, alignmentEntry.Size);
                int alignedFieldOffset = alignedFieldHelper.AlignedFieldOffset;

                if (unmanagedStruct)
                {
                    // Insert enough padding to ensure the field is properly aligned
                    foreach (string alignmentField in alignedFieldHelper.AlignmentFields)
                    {
                        parameterList.Append(alignmentField);
                    }
                }

                // This primary if else else block switches between the tree types of entries in the
                // paddedFields alignment list: padding, animation and normal field entries.
                // The offset of each field is stored in "position", which is the padding offset of the
                // entry + the initial position.
                if (field == null)
                {
                    Debug.Assert(alignmentEntry.IsPad);

                    string padIdentifier = "padQuadAlignment" + padSuffix;

                    if (unmanagedStruct)
                    {
                        parameterList.Append("UINT32 " + padIdentifier + ";");
                    }
                    else
                    {
                        parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] private UInt32 " + padIdentifier);
                    }
                    ++padSuffix;
                }
                else if (alignmentEntry.IsAnimation)
                {
                    if (unmanagedStruct)
                    {
                        parameterList.Append("HMIL_RESOURCE " + "h" + field.PropertyName + "Animations;");
                    }
                    else
                    {
                        parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] internal DUCE.ResourceHandle h" + field.PropertyName + "Animations");
                    }
                }
                else
                {
                    // This case is a boring, non animate field.

                    bool        constructedParam = false;
                    McgType     type             = field.Type;
                    McgResource resourceType     = type as McgResource;

                    // If it's an McgResource, we have to handle reference types and collections
                    if (resourceType != null)
                    {
                        // Currently, collections are accounted for by storing just their size inline
                        if (resourceType.IsCollection)
                        {
                            if (unmanagedStruct)
                            {
                                parameterList.Append("UINT32 " + field.Name + "Size;");
                            }
                            else
                            {
                                parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] internal UInt32 " + field.Name + "Size");
                            }
                            constructedParam = true;
                        }
                        // If it's not a collection, is it a reference type?  If so, it's passed by handle.
                        else if (!resourceType.IsValueType)
                        {
                            if (unmanagedStruct)
                            {
                                parameterList.Append("HMIL_RESOURCE " + "h" + field.Name + ";");
                            }
                            else
                            {
                                parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] internal DUCE.ResourceHandle h" + field.Name);
                            }

                            constructedParam = true;
                        }
                    }

                    // If we haven't yet handled this parameter, we're left with a primitive type,
                    // like a struct or an enum.  At this point, the value will be stored inline.
                    // The only real question left is whether the unmanaged struct matches the managed struct.
                    // If so, the struct is simply stored.  If not, then .NeedsConvert is true, and
                    // the record is of the unmanaged type, even on the managed side of things.
                    if (!constructedParam)
                    {
                        if (type.NeedsConvert)
                        {
                            if (unmanagedStruct)
                            {
                                parameterList.Append(type.BaseUnmanagedType + " " + field.Name + ";");
                            }
                            else
                            {
                                parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] internal " + type.MarshalUnmanagedType + " " + field.Name);
                            }
                        }
                        else
                        {
                            if (unmanagedStruct)
                            {
                                if (kernelAccessibleStruct)
                                {
                                    McgEnum enumType = type as McgEnum;

                                    if (enumType != null)
                                    {
                                        parameterList.Append(enumType.KernelAccessibleType + " " + field.Name + ";");
                                        constructedParam = true;
                                    }
                                }

                                if (!constructedParam)
                                {
                                    parameterList.Append(type.BaseUnmanagedType + " " + field.Name + ";");
                                }
                            }
                            else
                            {
                                parameterList.Append("[FieldOffset(" + alignedFieldOffset + ")] internal " + type.ManagedName + " " + field.Name);
                            }
                        }
                    }
                }
            }

            if (unmanagedStruct)
            {
                // Insert enough padding to ensure the struct's size falls on a packing boundary
                foreach (string packingField in alignedFieldHelper.NativePackingFields)
                {
                    parameterList.Append(packingField);
                }
            }
            else
            {
                // Insert enough padding to ensure the struct's size falls on a packing boundary
                foreach (string packingField in alignedFieldHelper.ManagedPackingFields)
                {
                    parameterList.Append(packingField);
                }
            }

            return(initialPosition + paddedFields.PaddedSize);
        }