internal static string ConvertToValueType(McgType type, string fieldName) { switch (type.Name) { case "Boolean": return("CompositionResourceManager.BooleanToUInt32(" + fieldName + ")"); case "Color": return("CompositionResourceManager.ColorToMilColorF(" + fieldName + ")"); case "Matrix3D": return("CompositionResourceManager.Matrix3DToD3DMATRIX(" + fieldName + ")"); case "Matrix": return("CompositionResourceManager.MatrixToMilMatrix3x2D(" + fieldName + ")"); case "Vector3D": return("CompositionResourceManager.Vector3DToMilPoint3F(" + fieldName + ")"); case "Point3D": return("CompositionResourceManager.Point3DToMilPoint3F(" + fieldName + ")"); case "Quaternion": return("CompositionResourceManager.QuaternionToMilQuaternionF(" + fieldName + ")"); default: return(fieldName); } }
/// <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; } } } } }
/// <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); }