Exemple #1
0
        public static PmxDocument Parse(Stream stream)
        {
            // leave open
            var br     = new BinaryReader(stream);
            var header = Encoding.ASCII.GetString(br.ReadBytes(4));
            var rt     = new PmxDocument();

            if (header != "PMX ")
            {
                throw new InvalidOperationException("invalid format");
            }

            rt.Version = br.ReadSingle();

            if (rt.Version != 2 &&
                rt.Version != 2.1f)
            {
                throw new NotSupportedException("specified format version not supported");
            }

            rt.Header           = PmxHeader.Parse(br);
            rt.ModelInformation = PmxModelInformation.Parse(br, rt);
            rt.Vertices         = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxVertex.Parse(br, rt)).ToList();
            rt.Indices          = Enumerable.Range(0, br.ReadInt32()).Select(_ => rt.ReadIndex(br, PmxIndexKind.Vertex)).ToList();
            rt.Textures         = Enumerable.Range(0, br.ReadInt32()).Select(_ => rt.ReadString(br)).ToList();
            rt.Materials        = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxMaterial.Parse(br, rt)).ToList();
            rt.Bones            = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxBone.Parse(br, rt)).ToList();
            rt.Morphs           = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxMorph.Parse(br, rt)).ToList();
            rt.DisplayList      = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxDisplayList.Parse(br, rt)).ToList();
            rt.Rigids           = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxRigidBody.Parse(br, rt)).ToList();
            rt.Constraints      = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxConstraint.Parse(br, rt)).ToList();

            if (rt.Version > 2)
            {
                rt.SoftBodies = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxSoftBody.Parse(br, rt)).ToList();
            }

            return(rt);
        }