Ejemplo n.º 1
0
        private void CreateVerFrameDir(GrnNode mainNode)
        {
            GrnSectionNode verFrameDir = new GrnSectionNode(null, GrnNodeType.VersionFrameDirectory);

            verFrameDir.Offset             = 156;
            verFrameDir.NumTotalChildNodes = 6;
            mainNode.AppendChild(verFrameDir);

            GrnStringTableNode strNode = new GrnStringTableNode(verFrameDir);

            strNode.Offset = 88;
            strNode.Strings.Add(string.Empty);
            strNode.Strings.Add("RAD 3D Studio MAX 4.x");
            strNode.Strings.Add("1.2b");
            strNode.Strings.Add("10-4-2000");
            strNode.Strings.Add("win32");
            strNode.Strings.Add("(C) Copyright 1999-2000 RAD Game Tools, Inc.  All Rights Reserved.");
            verFrameDir.AppendChild(strNode);

            GrnNode verSect = new GrnNode(verFrameDir, GrnNodeType.VersionSection);

            verSect.Offset             = 208;
            verSect.NumTotalChildNodes = 3;
            verFrameDir.AppendChild(verSect);

            GrnNode expVer = new GrnNode(verSect, GrnNodeType.ExporterVersion);

            expVer.Offset             = 208;
            expVer.NumTotalChildNodes = 1;
            expVer.Data = new byte[] { 0x01, 0x00, 0x00, 0x00,
                                       0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 };
            verSect.AppendChild(expVer);

            GrnNode maxSys = new GrnNode(expVer, GrnNodeType.ModelerAxisSystem);

            maxSys.Offset = 220;
            maxSys.Data   = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xBF,
                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00 };
            expVer.AppendChild(maxSys);

            GrnNode runVer = new GrnNode(verSect, GrnNodeType.RuntimeVersion);

            runVer.Offset = 268;
            runVer.Data   = new byte[] { 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
                                         0x03, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00 };
            verSect.AppendChild(runVer);

            GrnNode nullTer = new GrnNode(verFrameDir, GrnNodeType.NullTerminator);

            nullTer.Offset = 284;
            verFrameDir.AppendChild(nullTer);
        }
Ejemplo n.º 2
0
        public void Read(System.IO.Stream stream)
        {
            using (GrnBinaryReader reader = new GrnBinaryReader(stream))
            {
                Int64 magic = reader.ReadInt64();
                if (magic != 7380350958317416490)
                {
                    throw new Exception("This is not a GRN file!");
                }
                reader.ReadBytes(56);
                GrnNodeType nodeType = (GrnNodeType)reader.ReadInt32(); // should be FileDirectory
                GrnNode     mainNode = GrnNode.ReadByNodeType(reader, null, nodeType);
                //mainNode.CreateFolder(@"C:\Users\Petar\Desktop\Nieuwe map (3)\Output", 0);

                GrnSectionNode dirNode         = mainNode.FindNode <GrnSectionNode>(GrnNodeType.StandardFrameDirectory);
                uint           directoryOffset = dirNode.Offset;

                // 0StringTable
                List <string> strings = dirNode.FindNode <GrnStringTableNode>(GrnNodeType.StringTable).Strings;

                // 1DataExtension
                this.ReadDataExtension(strings, dirNode.FindNodes <GrnNode>(GrnNodeType.DataExtension));

                // 2VectorChannel

                // 3TransformChannel
                List <int> transformChannels = this.ReadTransformChannel(dirNode.ChildNodes[3].FindNodes <GrnNode>(GrnNodeType.TransformChannel));

                // 4Mesh
                List <GrnNode> meshes = dirNode.ChildNodes[4].FindNodes <GrnNode>(GrnNodeType.Mesh);
                for (int i = 0; i < meshes.Count; ++i)
                {
                    this.Meshes.Add(new GrnMesh(this));
                    this.Meshes[i].Read(meshes[i]);
                }

                // 5Skeleton
                List <GrnBoneNode> bones = dirNode.ChildNodes[5].FindNodes <GrnBoneNode>(GrnNodeType.Bone);
                for (int i = 0; i < bones.Count; ++i)
                {
                    this.Bones.Add(new GrnBone(this));
                    this.Bones[i].Read(bones[i]);
                }

                // 6Texture
                List <GrnNode> textureMaps = dirNode.ChildNodes[6].FindNodes <GrnNode>(GrnNodeType.TextureMap);
                for (int i = 0; i < textureMaps.Count; ++i)
                {
                    this.Textures.Add(new GrnTexture(this));
                    this.Textures[i].Read(textureMaps[i]);
                }

                // 7Material
                List <GrnNode> materials = dirNode.ChildNodes[7].FindNodes <GrnNode>(GrnNodeType.Material);
                for (int i = 0; i < materials.Count; ++i)
                {
                    this.Materials.Add(new GrnMaterial(this));
                    this.Materials[i].Read(materials[i]);
                }

                // 8Form
                this.ReadFormBoneChannel(transformChannels,
                                         dirNode.ChildNodes[8].FindNode <GrnFormBoneChannelsNode>(GrnNodeType.FormBoneChannels));
                List <GrnFormMeshNode> formMeshes = dirNode.ChildNodes[8].
                                                    FindNodes <GrnFormMeshNode>(GrnNodeType.FormMesh);
                List <int> meshLinks = new List <int>(formMeshes.Count);
                for (int i = 0; i < formMeshes.Count; ++i)
                {
                    meshLinks.Add(formMeshes[i].MeshIndex - 1);
                    this.Meshes[formMeshes[i].MeshIndex - 1].ReadFormMeshBones(formMeshes[i].
                                                                               FindNodes <GrnFormMeshBoneNode>(GrnNodeType.FormMeshBone));
                }

                // 9Model
                List <GrnRenderPassNode> renderPass =
                    dirNode.ChildNodes[9].FindNodes <GrnRenderPassNode>(GrnNodeType.RenderPass);
                for (int i = 0; i < renderPass.Count; ++i)
                {
                    int meshIndex = meshLinks[renderPass[i].FormMeshIndex];
                    int matIndex  = renderPass[i].MaterialIndex - 1;
                    this.Meshes[meshIndex].ReadRenderPassTriangles(matIndex,
                                                                   renderPass[i].FindNode <GrnRenderPassTrianglesNode>(GrnNodeType.RenderPassTriangles));
                }

                // 10Animation
                List <GrnAnimationTransformTrackKeysNode> animTransTrackKeys =
                    dirNode.ChildNodes[10].FindNodes <GrnAnimationTransformTrackKeysNode>(
                        GrnNodeType.AnimationTransformTrackKeys);
                this.Animation.BoneTracks = new List <GrnBoneTrack>(animTransTrackKeys.Count);
                for (int i = 0; i < animTransTrackKeys.Count; i++)
                {
                    if (animTransTrackKeys[i].PreviousSibling != null &&
                        ((GrnAnimationTransformTrackKeysNode)animTransTrackKeys[i].PreviousSibling).TransformChannelIndex == animTransTrackKeys[i].TransformChannelIndex)
                    {
                        animTransTrackKeys.RemoveAt(i);
                        --i;
                        continue;
                    }
                    this.Animation.BoneTracks.Add(new GrnBoneTrack());
                    this.Animation.BoneTracks[i].Read(transformChannels, animTransTrackKeys[i]);
                }
                this.CalculateAnimationDuration();
            }
        }
Ejemplo n.º 3
0
        public void Write(Stream stream)
        {
            using (GrnBinaryWriter writer = new GrnBinaryWriter(stream))
            {
                this.WriteHeader(writer);

                GrnMainNode mainNode = new GrnMainNode();
                mainNode.NumTotalChildNodes = 3;
                this.CreateVerFrameDir(mainNode);

                GrnSectionNode staFrameDir = new GrnSectionNode(mainNode, GrnNodeType.StandardFrameDirectory);
                staFrameDir.Offset = 440;
                mainNode.AppendChild(staFrameDir);

                // 0StringTable
                OrderedDictionary  stringMap    = this.CreateStringMap();
                GrnStringTableNode strTableNode = new GrnStringTableNode(staFrameDir);
                strTableNode.Strings = stringMap.Keys.Cast <string>().ToList();
                staFrameDir.AppendChild(strTableNode);

                // 1DataExtension
                GrnNode dataExtSecNode = new GrnNode(staFrameDir, GrnNodeType.DataExtensionSection);
                staFrameDir.AppendChild(dataExtSecNode);
                this.WriteDataExtensions(dataExtSecNode, stringMap);

                // 2VectorChannel
                GrnNode vecChanSecNode = new GrnNode(staFrameDir, GrnNodeType.VectorChannelSection);
                staFrameDir.AppendChild(vecChanSecNode);

                // 3TransformChannel
                GrnNode traChanSecNode = new GrnNode(staFrameDir, GrnNodeType.TransformChannelSection);
                staFrameDir.AppendChild(traChanSecNode);
                foreach (GrnBone bone in this.Bones)
                {
                    GrnNode traChanNode = new GrnNode(traChanSecNode, GrnNodeType.TransformChannel);
                    traChanSecNode.AppendChild(traChanNode);

                    GrnDataExtensionReferenceNode refNode = new GrnDataExtensionReferenceNode(traChanNode);
                    refNode.DataExtensionIndex = bone.DataExtensionIndex + 1;
                    traChanNode.AppendChild(refNode);
                }

                // 4Mesh
                GrnNode meshSecNode = new GrnNode(staFrameDir, GrnNodeType.MeshSection);
                staFrameDir.AppendChild(meshSecNode);
                foreach (GrnMesh mesh in this.Meshes)
                {
                    mesh.Write(meshSecNode);
                }

                // 5Skeleton
                GrnNode skelSecNode = new GrnNode(staFrameDir, GrnNodeType.SkeletonSection);
                staFrameDir.AppendChild(skelSecNode);
                GrnNode skelNode = new GrnNode(skelSecNode, GrnNodeType.Skeleton);
                skelSecNode.AppendChild(skelNode);
                GrnNode boneSecNode = new GrnNode(skelNode, GrnNodeType.BoneSection);
                skelNode.AppendChild(boneSecNode);
                foreach (GrnBone bone in this.Bones)
                {
                    bone.Write(boneSecNode);
                }

                // 6Texture
                GrnNode texSecNode = new GrnNode(staFrameDir, GrnNodeType.TextureSection);
                staFrameDir.AppendChild(texSecNode);
                foreach (GrnTexture tex in this.Textures)
                {
                    tex.Write(texSecNode);
                }

                // 7Material
                GrnNode matSecNode = new GrnNode(staFrameDir, GrnNodeType.MaterialSection);
                staFrameDir.AppendChild(matSecNode);
                foreach (GrnMaterial mat in this.Materials)
                {
                    mat.Write(matSecNode);
                }

                // 8Form
                GrnNode formSecNode = new GrnNode(staFrameDir, GrnNodeType.FormSection);
                staFrameDir.AppendChild(formSecNode);
                this.WriteForm(formSecNode);

                // 9Model
                GrnNode modelSecNode = new GrnNode(staFrameDir, GrnNodeType.ModelSection);
                staFrameDir.AppendChild(modelSecNode);
                GrnNode modelNode = new GrnNode(modelSecNode, GrnNodeType.Model);
                modelNode.Data = new byte[] { 0x01, 0x00, 0x00, 0x00 };
                modelSecNode.AppendChild(modelNode);
                GrnNode rendPassSecNode = new GrnNode(modelNode, GrnNodeType.RenderPassSection);
                modelNode.AppendChild(rendPassSecNode);
                for (int i = 0; i < this.Meshes.Count; ++i)
                {
                    this.Meshes[i].WriteRenderPass(rendPassSecNode, i);
                }

                // 10Animation
                GrnNode animSecNode = new GrnNode(staFrameDir, GrnNodeType.AnimationSection);
                staFrameDir.AppendChild(animSecNode);
                GrnNode animNode = new GrnNode(animSecNode, GrnNodeType.Animation);
                animSecNode.AppendChild(animNode);
                GrnNode animVecTraSecNode = new GrnNode(animNode, GrnNodeType.AnimationVectorTrackSection);
                animNode.AppendChild(animVecTraSecNode);
                GrnNode animTraTraSecNode = new GrnNode(animNode, GrnNodeType.AnimationTransformTrackSection);
                animNode.AppendChild(animTraTraSecNode);
                for (int i = 0; i < this.Animation.BoneTracks.Count; ++i)
                {
                    this.Animation.BoneTracks[i].Write(animTraTraSecNode, i);
                }

                // 11NullTerminator
                GrnNode nullTermNode = new GrnNode(staFrameDir, GrnNodeType.NullTerminator);
                staFrameDir.AppendChild(nullTermNode);

                GrnSectionNode nullFrameDir = new GrnSectionNode(mainNode, GrnNodeType.NullFrameDirectory);
                mainNode.AppendChild(nullFrameDir);
                nullTermNode = new GrnNode(nullFrameDir, GrnNodeType.NullTerminator);
                nullFrameDir.AppendChild(nullTermNode);

                mainNode.Write(writer);
            }
        }