Пример #1
0
        internal InstancedModelPart( InstancedModel owner, ContentReader input,
            GraphicsDevice graphicsDevice)
        {
            this.owner = owner;

              indexCount = input.ReadInt32();
              vertexCount = input.ReadInt32();
              vertexStride = input.ReadInt32();

              vertexDeclaration = input.ReadObject<VertexDeclaration>();
              vertexBuffer = input.ReadObject<VertexBuffer>();
              indexBuffer = input.ReadObject<IndexBuffer>();

              input.ReadSharedResource<Effect>( delegate( Effect value )
              {
            Effect = value;
            effectVertexCountParam = Effect.Parameters["VertexCount"];
            effectViewParam = Effect.Parameters["View"];
            effectProjectionParam = Effect.Parameters["Projection"];
            effectEyeParam = Effect.Parameters["EyePosition"];
              } );

              originalVertexDeclaration = vertexDeclaration.GetVertexElements();
        }
        internal static SkinnedModelBone Read(ContentReader input)
        {
            // Read bone data
            ushort index = input.ReadUInt16();
            string name = input.ReadString();

            // Read bind pose
            Pose bindPose;
            bindPose.Translation = input.ReadVector3();
            bindPose.Orientation = input.ReadQuaternion();
            bindPose.Scale = input.ReadVector3();

            Matrix inverseBindPoseTransform = input.ReadMatrix();
            SkinnedModelBone skinnedBone =
                new SkinnedModelBone(index, name, bindPose, inverseBindPoseTransform);

            // Read bone parent
            input.ReadSharedResource<SkinnedModelBone>(
                delegate(SkinnedModelBone parentBone) { skinnedBone.parent = parentBone; });

            // Read bone children
            int numChildren = input.ReadInt32();
            List<SkinnedModelBone> childrenList = new List<SkinnedModelBone>(numChildren);
            for (int i = 0; i < numChildren; i++)
            {
                input.ReadSharedResource<SkinnedModelBone>(
                    delegate(SkinnedModelBone childBone) { childrenList.Add(childBone); });
            }
            skinnedBone.children = new SkinnedModelBoneCollection(childrenList);

            return skinnedBone;
        }
        /// <summary>
        /// Constructor reads instanced model data from our custom XNB format.
        /// </summary>
        internal InstancedModelPart( ContentReader input, GraphicsDevice graphicsDevice )
        {
            this.graphicsDevice = graphicsDevice;

              // Load the model data.
              indexCount = input.ReadInt32();
              vertexCount = input.ReadInt32();
              vertexStride = input.ReadInt32();

              vertexDeclaration = input.ReadObject<VertexDeclaration>();
              vertexBuffer = input.ReadObject<VertexBuffer>();
              indexBuffer = input.ReadObject<IndexBuffer>();

              input.ReadSharedResource<Effect>( delegate( Effect value )
              {
            effect = value;

            // accessors
            EffectParameterColor = effect.Parameters["Color"];
            EffectParameterDiffuseMap = effect.Parameters["DiffuseMap"];
            EffectParameterSpecularPower = effect.Parameters["SpecularPower"];

            effectParameterView = effect.Parameters["View"];
            effectParameterProjection = effect.Parameters["Projection"];
            effectParameterEye = effect.Parameters["Eye"];
            effectParameterVertexCount = effect.Parameters["VertexCount"];
            effectParameterInstanceTransforms = effect.Parameters["InstanceTransforms"];
              } );

              // Work out how many shader instances we can fit into a single batch.
              int indexOverflowLimit = ushort.MaxValue / vertexCount;

              maxInstances = Math.Min( indexOverflowLimit, MaxShaderMatrices );

              // On Xbox, we must replicate several copies of our index buffer data for
              // the VFetch instancing technique. We could alternatively precompute this
              // in the content processor, but that would bloat the size of the XNB file.
              // It is more efficient to generate the repeated values at load time.
              //
              // We also require replicated index data for the Windows ShaderInstancing
              // technique, but this is computed lazily on Windows, so as to avoid
              // bloating the index buffer if it turns out that we only ever use the
              // HardwareInstancingTechnique (which does not require any repeated data).

              ReplicateIndexData();
        }
Пример #4
0
        private void Read(
            object parent,
            ContentReader input,
            MemberInfo member
            )
        {
            PropertyInfo property = member as PropertyInfo;
            FieldInfo    field    = member as FieldInfo;

            // Properties must have public get and set
            if (property != null &&
                (property.CanWrite == false ||
                 property.CanRead == false))
            {
                return;
            }

            if (property != null && property.Name == "Item")
            {
                MethodInfo getMethod = property.GetGetMethod();
                MethodInfo setMethod = property.GetSetMethod();

                if ((getMethod != null && getMethod.GetParameters().Length > 0) ||
                    (setMethod != null && setMethod.GetParameters().Length > 0))
                {
                    /* This is presumably a property like this[indexer] and this
                     * should not get involved in the object deserialization
                     */
                    return;
                }
            }
            Attribute attr = Attribute.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute));

            if (attr != null)
            {
                return;
            }
            Attribute attr2            = Attribute.GetCustomAttribute(member, typeof(ContentSerializerAttribute));
            bool      isSharedResource = false;

            if (attr2 != null)
            {
                ContentSerializerAttribute cs = attr2 as ContentSerializerAttribute;
                isSharedResource = cs.SharedResource;
            }
            else
            {
                if (property != null)
                {
                    foreach (MethodInfo info in property.GetAccessors(true))
                    {
                        if (info.IsPublic == false)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (!field.IsPublic)
                    {
                        return;
                    }

                    // Evolutional: Added check to skip initialise only fields
                    if (field.IsInitOnly)
                    {
                        return;
                    }
                }
            }
            ContentTypeReader reader = null;
            Type elementType         = null;

            if (property != null)
            {
                reader = manager.GetTypeReader(elementType = property.PropertyType);
            }
            else
            {
                reader = manager.GetTypeReader(elementType = field.FieldType);
            }
            if (!isSharedResource)
            {
                object existingChildObject = CreateChildObject(property, field);
                object obj2;
                if (reader == null && elementType == typeof(object))
                {
                    // Reading elements serialized as "object"
                    obj2 = input.ReadObject <object>();
                }
                else
                {
                    // Default

                    /* Evolutional: Fix. We can get here and still be NULL,
                     * exit gracefully
                     */
                    if (reader == null)
                    {
                        return;
                    }
                    obj2 = input.ReadObject(reader, existingChildObject);
                }
                if (property != null)
                {
                    property.SetValue(parent, obj2, null);
                }
                else
                {
                    /* Private fields can be serialized if they have
                     * ContentSerializerAttribute added to them
                     */
                    if (field.IsPrivate == false || attr2 != null)
                    {
                        field.SetValue(parent, obj2);
                    }
                }
            }
            else
            {
                Action <object> action = delegate(object value)
                {
                    if (property != null)
                    {
                        property.SetValue(parent, value, null);
                    }
                    else
                    {
                        field.SetValue(parent, value);
                    }
                };
                input.ReadSharedResource(action);
            }
        }
Пример #5
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            // Read the bone names and transforms.
            uint boneCount = reader.ReadUInt32();
            //Debug.WriteLine("Bone count: {0}", boneCount);

            List <ModelBone> bones = new List <ModelBone>((int)boneCount);

            for (uint i = 0; i < boneCount; i++)
            {
                string name   = reader.ReadObject <string>();
                var    matrix = reader.ReadMatrix();
                var    bone   = new ModelBone {
                    Transform = matrix, Index = (int)i, Name = name
                };
                bones.Add(bone);
            }

            // Read the bone hierarchy.
            for (int i = 0; i < boneCount; i++)
            {
                var bone = bones[i];

                //Debug.WriteLine("Bone {0} hierarchy:", i);

                // Read the parent bone reference.
                //Debug.WriteLine("Parent: ");
                var parentIndex = ReadBoneReference(reader, boneCount);

                if (parentIndex != -1)
                {
                    bone.Parent = bones[parentIndex];
                }

                // Read the child bone references.
                uint childCount = reader.ReadUInt32();

                if (childCount != 0)
                {
                    //Debug.WriteLine("Children:");

                    for (uint j = 0; j < childCount; j++)
                    {
                        var childIndex = ReadBoneReference(reader, boneCount);
                        if (childIndex != -1)
                        {
                            bone.AddChild(bones[childIndex]);
                        }
                    }
                }
            }

            List <ModelMesh> meshes = new List <ModelMesh>();

            //// Read the mesh data.
            int meshCount = reader.ReadInt32();

            //Debug.WriteLine("Mesh count: {0}", meshCount);

            for (int i = 0; i < meshCount; i++)
            {
                //Debug.WriteLine("Mesh {0}", i);
                string name            = reader.ReadObject <string>();
                var    parentBoneIndex = ReadBoneReference(reader, boneCount);
                var    boundingSphere  = reader.ReadBoundingSphere();

                // Tag
                reader.ReadObject <object>();

                // Read the mesh part data.
                int partCount = reader.ReadInt32();
                //Debug.WriteLine("Mesh part count: {0}", partCount);

                List <ModelMeshPart> parts = new List <ModelMeshPart>(partCount);

                for (uint j = 0; j < partCount; j++)
                {
                    ModelMeshPart part;
                    if (existingInstance != null)
                    {
                        part = existingInstance.Meshes[i].MeshParts[(int)j];
                    }
                    else
                    {
                        part = new ModelMeshPart();
                    }

                    part.VertexOffset   = reader.ReadInt32();
                    part.NumVertices    = reader.ReadInt32();
                    part.StartIndex     = reader.ReadInt32();
                    part.PrimitiveCount = reader.ReadInt32();

                    // tag
                    part.Tag = reader.ReadObject <object>();

                    parts.Add(part);

                    int jj = (int)j;
                    reader.ReadSharedResource <VertexBuffer>(delegate(VertexBuffer v)
                    {
                        parts[jj].VertexBuffer = v;
                    });
                    reader.ReadSharedResource <IndexBuffer>(delegate(IndexBuffer v)
                    {
                        parts[jj].IndexBuffer = v;
                    });
                    reader.ReadSharedResource <Effect>(delegate(Effect v)
                    {
                        parts[jj].Effect = v;
                    });
                }

                if (existingInstance != null)
                {
                    continue;
                }

                ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                mesh.Name       = name;
                mesh.ParentBone = bones[parentBoneIndex];
                mesh.ParentBone.AddMesh(mesh);
                mesh.BoundingSphere = boundingSphere;
                meshes.Add(mesh);
            }

            if (existingInstance != null)
            {
                // Read past remaining data and return existing instance
                ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }

            // Read the final pieces of model data.
            var rootBoneIndex = ReadBoneReference(reader, boneCount);

            Model model = new Model(reader.GraphicsDevice, bones, meshes);

            model.Root = bones[rootBoneIndex];

            model.BuildHierarchy();

            // Tag?
            model.Tag = reader.ReadObject <object>();

            return(model);
        }
        private void ReadBones(ContentReader input)
        {
            int numSkeletonBones = input.ReadInt32();
            List<SkinnedModelBone> skinnedModelBoneList = new List<SkinnedModelBone>(numSkeletonBones);

            // Read all bones
            for (int i = 0; i < numSkeletonBones; i++)
            {
                input.ReadSharedResource<SkinnedModelBone>(
                    delegate(SkinnedModelBone skinnedBone) { skinnedModelBoneList.Add(skinnedBone); });
            }

            // Create the skeleton
            skeleton = new SkinnedModelBoneCollection(skinnedModelBoneList);

            //foreach (SkinnedModelBoneContent bone in skeleton)
            //output.WriteSharedResource(bone);

            /*
            ///
            /// TODO I can optimize this using write shared resource for the skeleton
            /// 
            string rootBoneName = input.ReadString();
            int numBones = input.ReadInt32();

            SkinnedModelBone[] tempBonesArray = new SkinnedModelBone[numBones];
            Dictionary<string, SkinnedModelBone> boneDictionary =
                new Dictionary<string, SkinnedModelBone>();

            // Read all boneDictionary
            for (int i = 0; i < numBones; i++)
            {
                string boneName = input.ReadString();
                Matrix boneTransform = input.ReadMatrix();
                Matrix boneAbsoluteTransform = input.ReadMatrix();
                Matrix boneBindPoseTransform = input.ReadMatrix();

                SkinnedModelBone bone =
                    new SkinnedModelBone(boneName, boneTransform, boneAbsoluteTransform,
                        boneBindPoseTransform);

                tempBonesArray[i] = bone;
                boneDictionary.Add(boneName, bone);
            }

            // Read the boneDictionary parent and children
            for (int i = 0; i < numBones; i++)
            {
                // Find parent bone
                SkinnedModelBone parentBone = null;
                string parentBoneName = input.ReadObject<string>();
                if (parentBoneName != null)
                    parentBone = boneDictionary[parentBoneName];

                tempBonesArray[i].Parent = parentBone;

                // Find children
                int numChildren = input.ReadInt32();
                List<SkinnedModelBone> childrenList = new List<SkinnedModelBone>(numChildren);
                for (int j = 0; j < numChildren; j++)
                {
                    string childName = input.ReadString();
                    childrenList.Add(boneDictionary[childName]);
                }

                tempBonesArray[i].Children = new SkinnedModelBoneCollection(childrenList);
            }

            // Store everything
            tempBonesArray = null;

            rootBone = boneDictionary[rootBoneName];
            this.boneDictionary = new SkinnedModelBoneDictionary(boneDictionary);
            skinnedBones = new Matrix[numBones];

            inverseBindPose = new Matrix[numBones];
            rootBone.GetHierarchyInverseBindPoseTransfom(ref inverseBindPose);
             */
        }
Пример #7
0
        /// <summary>
        /// The constructor reads model data from our custom XNB format.
        /// This is called by the CustomModelReader class, which is invoked
        /// whenever you ask the ContentManager to read a CustomModel object.
        /// </summary>
        internal CustomModel( ContentReader input )
        {
            int partCount = input.ReadInt32();

              for ( int i = 0; i < partCount; i++ )
              {
            ModelPart modelPart = new ModelPart();

            // Simple data types like integers can be read directly.
            modelPart.TriangleCount = input.ReadInt32();
            modelPart.VertexCount = input.ReadInt32();
            modelPart.VertexStride = input.ReadInt32();

            // These XNA Framework types can be read using the ReadObject method,
            // which calls into the appropriate ContentTypeReader for each type.
            // The framework provides built-in ContentTypeReader implementations
            // for important types such as vertex declarations, vertex buffers,
            // index buffers, effects, and textures.
            modelPart.VertexDeclaration = input.ReadObject<VertexDeclaration>();
            modelPart.VertexBuffer = input.ReadObject<VertexBuffer>();
            modelPart.IndexBuffer = input.ReadObject<IndexBuffer>();

            // Shared resources have to be read in a special way. Because the same
            // object can be referenced from many different parts of the file, the
            // actual object data is stored at the end of the XNB binary. When we
            // call ReadSharedResource we are just reading an ID that will later be
            // used to locate the actual data, so ReadSharedResource is unable to
            // directly return the shared instance. Instead, it takes in a delegate
            // parameter, and will call us back as soon as the shared value becomes
            // available. We use C# anonymous delegate syntax to store the value
            // into its final location.
            input.ReadSharedResource<Effect>( delegate( Effect effect )
            {
              //Effect effect = value.Clone( value.GraphicsDevice );
              if ( effect.Parameters["DiffuseMap"].GetValueTexture2D() != null )
              {
            if ( effect.Parameters["NormalMap"].GetValueTexture2D() != null )
            {
              effect.CurrentTechnique = effect.Techniques["NormalDiffuseColor"];
              modelPart.VertexDeclaration = new VertexDeclaration( effect.GraphicsDevice,
                                                                   VertexPositionNormalTextureTangentBinormal.VertexElements );
            }
            else
            {
              effect.CurrentTechnique = effect.Techniques["DiffuseColor"];
              modelPart.VertexDeclaration = new VertexDeclaration( effect.GraphicsDevice,
                                                                   VertexPositionNormalTexture.VertexElements );
            }
              }
              else
              {
            effect.CurrentTechnique = effect.Techniques["ColorDefault"];
            modelPart.VertexDeclaration = new VertexDeclaration( effect.GraphicsDevice,
                                                                 VertexPositionNormalColor.VertexElements );
              }
              modelPart.Effect = effect;
              modelPart.EffectParamWorld = modelPart.Effect.Parameters["World"];
              modelPart.EffectParamView = modelPart.Effect.Parameters["View"];
              modelPart.EffectParamProjection = modelPart.Effect.Parameters["Projection"];
              modelPart.EffectParamEye = modelPart.Effect.Parameters["Eye"];
              modelPart.EffectParamColor = modelPart.Effect.Parameters["Color"];
            } );

            modelParts.Add( modelPart );
              }
        }
        private void ReadAnimations(ContentReader input)
        {
            int numAnimationClips = input.ReadInt32();
            Dictionary<string, AnimationClip> animationClipDictionary =
                new Dictionary<string, AnimationClip>();

            // Read all animation clips
            for (int i = 0; i < numAnimationClips; i++)
            {
                input.ReadSharedResource<AnimationClip>(
                    delegate(AnimationClip animationClip) { animationClipDictionary.Add(animationClip.Name, animationClip); });
            }

            animationClips = new AnimationClipDictionary(animationClipDictionary);

            /*
            Dictionary<string, AnimationClip> trackDictionary =
                new Dictionary<string, AnimationClip>();

            int numAnimations = input.ReadInt32();
            for (int i = 0; i < numAnimations; i++)
            {
                Dictionary<string, AnimationChannel> channelDictionary =
                    new Dictionary<string, AnimationChannel>();

                string trackName = input.ReadString();
                TimeSpan trackDuration = input.ReadObject<TimeSpan>();

                int numChannels = input.ReadInt32();
                for (int j = 0; j < numChannels; j++)
                {
                    string channelName = input.ReadString();

                    int numKeyframes = input.ReadInt32();
                    List<AnimationChannelKeyframe> keyframeList = new List<AnimationChannelKeyframe>(numKeyframes);
                    for (int k = 0; k < numKeyframes; k++)
                    {
                        TimeSpan keyframeTime = input.ReadObject<TimeSpan>();
                        Matrix keyframeTransform = input.ReadMatrix();

                        keyframeList.Add(new AnimationChannelKeyframe(keyframeTime, keyframeTransform));
                    }

                    channelDictionary.Add(channelName, new AnimationChannel(keyframeList));
                }

                AnimationClip animationClip =
                    new AnimationClip(trackName, new AnimationChannelDictionary(channelDictionary),
                        trackDuration);
                trackDictionary.Add(trackName, animationClip);
            }

            animationClips = new AnimationClipDictionary(trackDictionary);
             */
        }
Пример #9
0
        private void Read(object parent, ContentReader input, MemberInfo member)
        {
            PropertyInfo property = member as PropertyInfo;
            FieldInfo    field    = member as FieldInfo;

            if (property != (PropertyInfo)null && (!property.CanWrite || !property.CanRead))
            {
                return;
            }
            if (property != (PropertyInfo)null && property.Name == "Item")
            {
                MethodInfo getMethod = property.GetGetMethod();
                MethodInfo setMethod = property.GetSetMethod();
                if (getMethod != (MethodInfo)null && getMethod.GetParameters().Length > 0 || setMethod != (MethodInfo)null && setMethod.GetParameters().Length > 0)
                {
                    return;
                }
            }
            if (Attribute.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute)) != null)
            {
                return;
            }
            Attribute customAttribute = Attribute.GetCustomAttribute(member, typeof(ContentSerializerAttribute));
            bool      flag            = false;

            if (customAttribute != null)
            {
                flag = (customAttribute as ContentSerializerAttribute).SharedResource;
            }
            else if (property != (PropertyInfo)null)
            {
                foreach (MethodBase methodBase in property.GetAccessors(true))
                {
                    if (!methodBase.IsPublic)
                    {
                        return;
                    }
                }
            }
            else if (!field.IsPublic)
            {
                return;
            }
            Type type;
            ContentTypeReader typeReader = !(property != (PropertyInfo)null) ? this.manager.GetTypeReader(type = field.FieldType) : this.manager.GetTypeReader(type = property.PropertyType);

            if (!flag)
            {
                object childObject = ReflectiveReader <T> .CreateChildObject(property, field);

                object obj = typeReader != null || !(type == typeof(object)) ? input.ReadObject <object>(typeReader, childObject) : input.ReadObject <object>();
                if (property != (PropertyInfo)null)
                {
                    property.SetValue(parent, obj, (object[])null);
                }
                else if (!field.IsPrivate || customAttribute != null)
                {
                    field.SetValue(parent, obj);
                }
            }
            else
            {
                Action <object> fixup = (Action <object>)(value =>
                {
                    if (property != (PropertyInfo)null)
                    {
                        property.SetValue(parent, value, (object[])null);
                    }
                    else
                    {
                        field.SetValue(parent, value);
                    }
                });
                input.ReadSharedResource <object>(fixup);
            }
        }
        private void Read(object parent, ContentReader input, MemberInfo member)
        {
            PropertyInfo property = member as PropertyInfo;
            FieldInfo    field    = member as FieldInfo;

            if (property != null && property.CanWrite == false)
            {
                return;
            }
            Attribute attr = Attribute.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute));

            if (attr != null)
            {
                return;
            }
            Attribute attr2            = Attribute.GetCustomAttribute(member, typeof(ContentSerializerAttribute));
            bool      isSharedResource = false;

            if (attr2 != null)
            {
                var cs = attr2 as ContentSerializerAttribute;
                isSharedResource = cs.SharedResource;
            }
            else
            {
                if (property != null)
                {
                    foreach (MethodInfo info in property.GetAccessors(true))
                    {
                        if (info.IsPublic == false)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (!field.IsPublic)
                    {
                        return;
                    }
                }
            }
            ContentTypeReader reader = null;

            if (property != null)
            {
                reader = manager.GetTypeReader(property.PropertyType);
            }
            else
            {
                reader = manager.GetTypeReader(field.FieldType);
            }
            if (!isSharedResource)
            {
                object existingChildObject = CreateChildObject(property, field);
                object obj2 = null;

                obj2 = input.ReadObject <object>(reader, existingChildObject);

                if (property != null)
                {
                    property.SetValue(parent, obj2, null);
                }
                else
                {
                    if (field.IsPrivate == false)
                    {
                        field.SetValue(parent, obj2);
                    }
                }
            }
            else
            {
                Action <object> action = delegate(object value)
                {
                    if (property != null)
                    {
                        property.SetValue(parent, value, null);
                    }
                    else
                    {
                        field.SetValue(parent, value);
                    }
                };
                input.ReadSharedResource <object>(action);
            }
        }
Пример #11
0
 private static ModelMeshPart[] ReadMeshParts(ContentReader input, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, VertexDeclaration[] vertexDeclarations)
 {
     int length = input.ReadInt32();
     ModelMeshPart[] meshParts = new ModelMeshPart[length];
     for (int i = 0; i < length; i++)
     {
         int streamOffset = input.ReadInt32();
         int baseVertex = input.ReadInt32();
         int numVertices = input.ReadInt32();
         int startIndex = input.ReadInt32();
         int primitiveCount = input.ReadInt32();
         int index = input.ReadInt32();
         VertexDeclaration vertexDeclaration = vertexDeclarations[index];
         object tag = input.ReadObject<object>();
         meshParts[i] = new ModelMeshPart(streamOffset, baseVertex, numVertices, startIndex, primitiveCount, vertexBuffer, indexBuffer, vertexDeclaration, tag);
         int uniqueI = i;
         input.ReadSharedResource<Effect>(delegate(Effect effect)
         {
             meshParts[uniqueI].Effect = effect;
         });
     }   
     return meshParts;
  }
Пример #12
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            List <ModelBone> bones     = new List <ModelBone>();
            uint             boneCount = reader.ReadUInt32();

            for (uint index = 0U; index < boneCount; ++index)
            {
                string    str       = reader.ReadObject <string>();
                Matrix    matrix    = reader.ReadMatrix();
                ModelBone modelBone = new ModelBone()
                {
                    Transform = matrix,
                    Index     = (int)index,
                    Name      = str
                };
                bones.Add(modelBone);
            }
            for (int index1 = 0; (long)index1 < (long)boneCount; ++index1)
            {
                ModelBone modelBone = bones[index1];
                int       index2    = ModelReader.ReadBoneReference(reader, boneCount);
                if (index2 != -1)
                {
                    modelBone.Parent = bones[index2];
                }
                uint num = reader.ReadUInt32();
                if ((int)num != 0)
                {
                    for (uint index3 = 0U; index3 < num; ++index3)
                    {
                        int index4 = ModelReader.ReadBoneReference(reader, boneCount);
                        if (index4 != -1)
                        {
                            modelBone.AddChild(bones[index4]);
                        }
                    }
                }
            }
            List <ModelMesh> meshes = new List <ModelMesh>();
            int num1 = reader.ReadInt32();

            for (int index1 = 0; index1 < num1; ++index1)
            {
                string         str            = reader.ReadObject <string>();
                int            index2         = ModelReader.ReadBoneReference(reader, boneCount);
                BoundingSphere boundingSphere = reader.ReadBoundingSphere();
                reader.ReadObject <object>();
                int num2 = reader.ReadInt32();
                List <ModelMeshPart> parts = new List <ModelMeshPart>();
                for (uint index3 = 0U; (long)index3 < (long)num2; ++index3)
                {
                    ModelMeshPart modelMeshPart = existingInstance == null ? new ModelMeshPart() : ((ReadOnlyCollection <ModelMesh>)existingInstance.Meshes)[index1].MeshParts[(int)index3];
                    modelMeshPart.VertexOffset   = reader.ReadInt32();
                    modelMeshPart.NumVertices    = reader.ReadInt32();
                    modelMeshPart.StartIndex     = reader.ReadInt32();
                    modelMeshPart.PrimitiveCount = reader.ReadInt32();
                    modelMeshPart.Tag            = reader.ReadObject <object>();
                    parts.Add(modelMeshPart);
                    int jj = (int)index3;
                    reader.ReadSharedResource <VertexBuffer>((Action <VertexBuffer>)(v => parts[jj].VertexBuffer = v));
                    reader.ReadSharedResource <IndexBuffer>((Action <IndexBuffer>)(v => parts[jj].IndexBuffer    = v));
                    reader.ReadSharedResource <Effect>((Action <Effect>)(v => parts[jj].Effect = v));
                }
                if (existingInstance == null)
                {
                    ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                    mesh.Name       = str;
                    mesh.ParentBone = bones[index2];
                    mesh.ParentBone.AddMesh(mesh);
                    mesh.BoundingSphere = boundingSphere;
                    meshes.Add(mesh);
                }
            }
            if (existingInstance != null)
            {
                ModelReader.ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }
            else
            {
                int   index = ModelReader.ReadBoneReference(reader, boneCount);
                Model model = new Model(reader.GraphicsDevice, bones, meshes);
                model.Root = bones[index];
                model.BuildHierarchy();
                model.Tag = reader.ReadObject <object>();
                return(model);
            }
        }
Пример #13
0
        private void ReadBones(ContentReader input)
        {
            int numSkeletonBones = input.ReadInt32();
            List<SkinnedModelBone> skinnedModelBoneList = new List<SkinnedModelBone>(numSkeletonBones);

            // Read all bones
            for (int i = 0; i < numSkeletonBones; i++)
            {
                input.ReadSharedResource<SkinnedModelBone>(
                    delegate(SkinnedModelBone skinnedBone) { skinnedModelBoneList.Add(skinnedBone); });
            }

            // Create the skeleton
            skeleton = new SkinnedModelBoneCollection(skinnedModelBoneList);
        }
Пример #14
0
        private void ReadAnimations(ContentReader input)
        {
            int numAnimationClips = input.ReadInt32();
            Dictionary<string, AnimationClip> animationClipDictionary =
                new Dictionary<string, AnimationClip>();

            // Read all animation clips
            for (int i = 0; i < numAnimationClips; i++)
            {
                input.ReadSharedResource<AnimationClip>(
                    delegate(AnimationClip animationClip) { animationClipDictionary.Add(animationClip.Name, animationClip); });
            }

            animationClips = new AnimationClipDictionary(animationClipDictionary);
        }
Пример #15
0
        private void Read(object parent, ContentReader input, MemberInfo member)
        {
            PropertyInfo property = member as PropertyInfo;
            FieldInfo    field    = member as FieldInfo;

            // properties must have public get and set
            if (property != null && (property.CanWrite == false || property.CanRead == false))
            {
                return;
            }

            if (property != null && property.Name == "Item")
            {
#if WINRT
                var getMethod = property.GetMethod;
                var setMethod = property.SetMethod;
#else
                var getMethod = property.GetGetMethod();
                var setMethod = property.GetSetMethod();
#endif

                if ((getMethod != null && getMethod.GetParameters().Length > 0) ||
                    (setMethod != null && setMethod.GetParameters().Length > 0))
                {
                    /*
                     * This is presumably a property like this[indexer] and this
                     * should not get involved in the object deserialization
                     * */
                    return;
                }
            }
#if WINRT
            Attribute attr = member.GetCustomAttribute(typeof(ContentSerializerIgnoreAttribute));
#else
            Attribute attr = Attribute.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute));
#endif
            if (attr != null)
            {
                return;
            }
#if WINRT
            Attribute attr2 = member.GetCustomAttribute(typeof(ContentSerializerAttribute));
#else
            Attribute attr2 = Attribute.GetCustomAttribute(member, typeof(ContentSerializerAttribute));
#endif
            bool isSharedResource = false;
            if (attr2 != null)
            {
                var cs = attr2 as ContentSerializerAttribute;
                isSharedResource = cs.SharedResource;
            }
            else
            {
                if (property != null)
                {
#if WINRT
                    if (property.GetMethod != null && !property.GetMethod.IsPublic)
                    {
                        return;
                    }
                    if (property.SetMethod != null && !property.SetMethod.IsPublic)
                    {
                        return;
                    }
#else
                    foreach (MethodInfo info in property.GetAccessors(true))
                    {
                        if (info.IsPublic == false)
                        {
                            return;
                        }
                    }
#endif
                }
                else
                {
                    if (!field.IsPublic)
                    {
                        return;
                    }
                }
            }
            ContentTypeReader reader = null;

            Type elementType = null;

            if (property != null)
            {
                reader = manager.GetTypeReader(elementType = property.PropertyType);
            }
            else
            {
                reader = manager.GetTypeReader(elementType = field.FieldType);
            }
            if (!isSharedResource)
            {
                object existingChildObject = CreateChildObject(property, field);
                object obj2;

                if (reader == null && elementType == typeof(object))
                {
                    /* Reading elements serialized as "object" */
                    obj2 = input.ReadObject <object>();
                }
                else
                {
                    /* Default */
                    obj2 = input.ReadObject(reader, existingChildObject);
                }

                if (property != null)
                {
                    property.SetValue(parent, obj2, null);
                }
                else
                {
                    // Private fields can be serialized if they have ContentSerializerAttribute added to them
                    if (field.IsPrivate == false || attr2 != null)
                    {
                        field.SetValue(parent, obj2);
                    }
                }
            }
            else
            {
                Action <object> action = delegate(object value)
                {
                    if (property != null)
                    {
                        property.SetValue(parent, value, null);
                    }
                    else
                    {
                        field.SetValue(parent, value);
                    }
                };
                input.ReadSharedResource(action);
            }
        }