Exemplo n.º 1
0
        public static H3D OpenAsH3D(Stream Input, GFPackage.Header Header, H3DDict <H3DBone> Skeleton = null)
        {
            BinaryReader Reader = new BinaryReader(Input);

            GFModelPack  MdlPack = new GFModelPack();
            GFMotionPack MotPack = new GFMotionPack();

            ReadModelsBG(Header.Entries[0], Reader, MdlPack); //Textures
            ReadModelsBG(Header.Entries[1], Reader, MdlPack); //Shaders
            ReadModelsBG(Header.Entries[2], Reader, MdlPack); //Models
            ReadModelsBG(Header.Entries[3], Reader, MdlPack); //Models?
            ReadModelsBG(Header.Entries[4], Reader, MdlPack); //More models
            ReadAnimsBG(Header.Entries[5], Reader, MotPack);  //Animations
            ReadAnimsBG(Header.Entries[6], Reader, MotPack);  //More animations

            H3D Output = MdlPack.ToH3D();

            foreach (GFMotion Mot in MotPack)
            {
                H3DMaterialAnim MatAnim = Mot.ToH3DMaterialAnimation();
                H3DAnimation    VisAnim = Mot.ToH3DVisibilityAnimation();

                if (MatAnim != null)
                {
                    MatAnim.Name = $"Motion_{Mot.Index}";

                    Output.MaterialAnimations.Add(MatAnim);
                }

                if (VisAnim != null)
                {
                    VisAnim.Name = $"Motion_{Mot.Index}";

                    Output.VisibilityAnimations.Add(VisAnim);
                }
            }

            return(Output);
        }
Exemplo n.º 2
0
        public static H3D OpenAsH3D(Stream Input, GFPackage.Header Header)
        {
            H3D Output;

            //Model
            Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

            GFModelPack MdlPack = new GFModelPack(Input);

            Output = MdlPack.ToH3D();

            //Animations
            Input.Seek(Header.Entries[1].Address, SeekOrigin.Begin);

            GFMotionPack MotPack = new GFMotionPack(Input);

            Output.Merge(MotPack.ToH3D(MdlPack.Models[0].Skeleton));

            //Texture
            if (Header.Entries.Length > 3 && Header.Entries[3].Length >= 4)
            {
                Input.Seek(Header.Entries[3].Address, SeekOrigin.Begin);

                BinaryReader Reader = new BinaryReader(Input);

                uint MagicNum = Reader.ReadUInt32();

                if (MagicNum == 0x15041213)
                {
                    Input.Seek(-4, SeekOrigin.Current);

                    GFTexture Tex = new GFTexture(Reader);

                    Output.Textures.Add(Tex.ToH3DTexture());
                }
            }

            return(Output);
        }
Exemplo n.º 3
0
        public static H3D OpenAsH3D(Stream input, GFPackage.Header header, H3DDict <H3DBone> skeleton = null)
        {
            var reader = new BinaryReader(input);

            var mdlPack = new GFModelPack();
            var motPack = new GFMotionPack();

            ReadModelsBG(header.Entries[0], reader, mdlPack); //Textures
            ReadModelsBG(header.Entries[1], reader, mdlPack); //Shaders
            ReadModelsBG(header.Entries[2], reader, mdlPack); //Models
            ReadModelsBG(header.Entries[3], reader, mdlPack); //Models?
            ReadModelsBG(header.Entries[4], reader, mdlPack); //More models
            ReadAnimsBG(header.Entries[5], reader, motPack);  //Animations
            ReadAnimsBG(header.Entries[6], reader, motPack);  //More animations

            var output = mdlPack.ToH3D();

            foreach (var mot in motPack)
            {
                var matAnim = mot.ToH3DMaterialAnimation();
                var visAnim = mot.ToH3DVisibilityAnimation();

                if (matAnim != null)
                {
                    matAnim.Name = $"Motion_{mot.Index}";

                    output.MaterialAnimations.Add(matAnim);
                }

                if (visAnim != null)
                {
                    visAnim.Name = $"Motion_{mot.Index}";

                    output.VisibilityAnimations.Add(visAnim);
                }
            }

            return(output);
        }
Exemplo n.º 4
0
        public static H3D IdentifyAndOpen(string FileName, H3DDict <H3DBone> Skeleton = null)
        {
            //Formats that can by identified by extensions
            string FilePath = Path.GetDirectoryName(FileName);

            switch (Path.GetExtension(FileName).ToLower())
            {
            case ".txt":
                H3D AllFiles = new H3D();

                string[] files = File.ReadAllLines(FileName);

                string Parent = FilePath;

                foreach (string File in files)
                {
                    AllFiles.Merge(IdentifyAndOpen(Path.Combine(Parent, File)));
                }

                return(AllFiles);

            case ".gmp":
                H3D           OutputH3D = new H3D();
                GF1MotionPack MotPack   = new GF1MotionPack(new BinaryReader(new FileStream(FileName, FileMode.Open)));
                foreach (GF1Motion Mot in MotPack)
                {
                    H3DAnimation SklAnim = Mot.ToH3DSkeletalAnimation(Skeleton);

                    SklAnim.Name = $"Motion_{Mot.Index}";

                    OutputH3D.SkeletalAnimations.Add(SklAnim);
                }
                return(OutputH3D);

            case ".smd": return(new SMD(FileName).ToH3D(FilePath));

            case ".obj": return(new OBJ(FileName).ToH3D(FilePath));

            case ".mtl": return(new OBJ(FileName).ToH3D(FilePath));

            case ".cmif": return(new CMIFFile(new FileStream(FileName, FileMode.Open)).ToH3D());

            case ".png":
                H3D Out = new H3D();
                Out.Textures.Add(new H3DTexture(FileName, true));
                return(Out);

            case ".gfbmdl":
                H3DModel model = new GFModel(new BinaryReader(new FileStream(FileName, FileMode.Open)), Path.GetFileNameWithoutExtension(FileName)).ToH3DModel();
                H3D      Scene = new H3D();
                Scene.Models.Add(model);
                return(Scene);

            case ".mbn":
                using (FileStream Input = new FileStream(FileName, FileMode.Open))
                {
                    H3D BaseScene = H3D.Open(File.ReadAllBytes(FileName.Replace(".mbn", ".bch")));

                    MBn ModelBinary = new MBn(new BinaryReader(Input), BaseScene);

                    return(ModelBinary.ToH3D());
                }
            }

            //Formats that can only be indetified by "magic numbers"
            H3D Output = null;

            using (FileStream FS = new FileStream(FileName, FileMode.Open))
            {
                if (FS.Length > 4)
                {
                    BinaryReader Reader = new BinaryReader(FS);

                    uint MagicNum = Reader.ReadUInt32();

                    FS.Seek(-4, SeekOrigin.Current);

                    string Magic = Encoding.ASCII.GetString(Reader.ReadBytes(4));

                    FS.Seek(0, SeekOrigin.Begin);

                    if (Magic.StartsWith("BCH"))
                    {
                        return(H3D.Open(Reader.ReadBytes((int)FS.Length)));
                    }
                    else if (Magic.StartsWith("MOD"))
                    {
                        return(LoadMTModel(Reader, FileName, Path.GetDirectoryName(FileName)));
                    }
                    else if (Magic.StartsWith("TEX"))
                    {
                        return(new MTTexture(Reader, Path.GetFileNameWithoutExtension(FileName)).ToH3D());
                    }
                    else if (Magic.StartsWith("MFX"))
                    {
                        MTShader = new MTShaderEffects(Reader);
                    }
                    else if (Magic.StartsWith("CGFX"))
                    {
                        return(Gfx.Open(FS));
                    }
                    else if (Magic.StartsWith("CMIF"))
                    {
                        return(new CMIFFile(new FileStream(FileName, FileMode.Open)).ToH3D());
                    }
                    else
                    {
                        if (GFPackage.IsValidPackage(FS))
                        {
                            GFPackage.Header PackHeader = GFPackage.GetPackageHeader(FS);

                            switch (PackHeader.Magic)
                            {
                            case "AL": Output = GFAreaLOD.OpenAsH3D(FS, PackHeader, 1); break;

                            case "AD": Output = GFPackedTexture.OpenAsH3D(FS, PackHeader, 1); break;

                            //case "BG": Output = GFL2OverWorld.OpenAsH3D(FS, PackHeader, Skeleton); break;
                            case "BS": Output = GFBtlSklAnim.OpenAsH3D(FS, PackHeader, Skeleton); break;

                            case "CM": Output = GFCharaModel.OpenAsH3D(FS, PackHeader); break;

                            case "GR": Output = GFOWMapModel.OpenAsH3D(FS, PackHeader); break;

                            case "MM": Output = GFOWCharaModel.OpenAsH3D(FS, PackHeader); break;

                            case "PC": Output = GFPkmnModel.OpenAsH3D(FS, PackHeader, Skeleton); break;

                            case "LL":
                            default:
                            case "PT": Output = GFPackedTexture.OpenAsH3D(FS, PackHeader, 0); break;

                            case "PK":
                            case "PB":
                                Output = GFPkmnSklAnim.OpenAsH3D(FS, PackHeader, Skeleton); break;
                            }
                        }
                        else
                        {
                            switch (MagicNum)
                            {
                            case 0x15122117:
                                Output = new H3D();

                                Output.Models.Add(new GFModel(Reader, "Model").ToH3DModel());

                                break;

                            case 0x15041213:
                                Output = new H3D();

                                Output.Textures.Add(new GFTexture(Reader).ToH3DTexture());

                                break;

                            case 0x00010000: Output = new GFModelPack(Reader).ToH3D(); break;

                            case 0x00060000:
                                if (Skeleton != null)
                                {
                                    Output = new H3D();

                                    GFMotion Motion = new GFMotion(Reader, 0);

                                    H3DAnimation    SklAnim = Motion.ToH3DSkeletalAnimation(Skeleton);
                                    H3DMaterialAnim MatAnim = Motion.ToH3DMaterialAnimation();
                                    H3DAnimation    VisAnim = Motion.ToH3DVisibilityAnimation();

                                    if (SklAnim != null)
                                    {
                                        Output.SkeletalAnimations.Add(SklAnim);
                                    }
                                    if (MatAnim != null)
                                    {
                                        Output.MaterialAnimations.Add(MatAnim);
                                    }
                                    if (VisAnim != null)
                                    {
                                        Output.VisibilityAnimations.Add(VisAnim);
                                    }
                                }

                                break;
                            }
                        }
                    }
                }
            }

            return(Output);
        }
Exemplo n.º 5
0
        public static H3D OpenAsH3D(Stream Input, GFPackage.Header Header)
        {
            H3D Output;

            //Model
            Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

            GFModelPack MdlPack = new GFModelPack(Input);

            Output = MdlPack.ToH3D();

            //Animations
            Input.Seek(Header.Entries[1].Address, SeekOrigin.Begin);

            GFMotionPack MotPack = new GFMotionPack(Input);

            foreach (GFMotion Mot in MotPack)
            {
                H3DAnimation    SklAnim = Mot.ToH3DSkeletalAnimation(MdlPack.Models[0].Skeleton);
                H3DMaterialAnim MatAnim = Mot.ToH3DMaterialAnimation();
                H3DAnimation    VisAnim = Mot.ToH3DVisibilityAnimation();

                if (SklAnim != null)
                {
                    SklAnim.Name = $"Motion_{Mot.Index}";

                    Output.SkeletalAnimations.Add(SklAnim);
                }

                if (MatAnim != null)
                {
                    MatAnim.Name = $"Motion_{Mot.Index}";

                    Output.MaterialAnimations.Add(MatAnim);
                }

                if (VisAnim != null)
                {
                    VisAnim.Name = $"Motion_{Mot.Index}";

                    Output.VisibilityAnimations.Add(VisAnim);
                }
            }

            //Texture
            if (Header.Entries.Length > 3 && Header.Entries[3].Length >= 4)
            {
                Input.Seek(Header.Entries[3].Address, SeekOrigin.Begin);

                BinaryReader Reader = new BinaryReader(Input);

                uint MagicNum = Reader.ReadUInt32();

                if (MagicNum == 0x15041213)
                {
                    Input.Seek(-4, SeekOrigin.Current);

                    GFTexture Tex = new GFTexture(Reader);

                    Output.Textures.Add(Tex.ToH3DTexture());
                }
            }

            return(Output);
        }
Exemplo n.º 6
0
        public static H3D OpenAsH3D(Stream Input, GFPackage.Header Header, H3DDict <H3DBone> Skeleton = null)
        {
            H3D Output = default(H3D);

            BinaryReader Reader = new BinaryReader(Input);

            if (Input.Length <= Header.Entries[0].Address)
            {
                return(null);
            }

            Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

            uint MagicNum = Reader.ReadUInt32();

            switch (MagicNum)
            {
            case GFModelConstant:
                GFModelPack MdlPack = new GFModelPack();

                //High Poly Pokémon model
                Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

                MdlPack.Models.Add(new GFModel(Reader, "PM_HighPoly"));

                //Low Poly Pokémon model
                Input.Seek(Header.Entries[1].Address, SeekOrigin.Begin);

                MdlPack.Models.Add(new GFModel(Reader, "PM_LowPoly"));

                //Pokémon Shader package
                Input.Seek(Header.Entries[2].Address, SeekOrigin.Begin);

                GFPackage.Header PSHeader = GFPackage.GetPackageHeader(Input);

                foreach (GFPackage.Entry Entry in PSHeader.Entries)
                {
                    Input.Seek(Entry.Address, SeekOrigin.Begin);

                    MdlPack.Shaders.Add(new GFShader(Reader));
                }

                //More shaders
                Input.Seek(Header.Entries[3].Address, SeekOrigin.Begin);

                if (GFPackage.IsValidPackage(Input))
                {
                    GFPackage.Header PCHeader = GFPackage.GetPackageHeader(Input);

                    foreach (GFPackage.Entry Entry in PCHeader.Entries)
                    {
                        Input.Seek(Entry.Address, SeekOrigin.Begin);

                        MdlPack.Shaders.Add(new GFShader(Reader));
                    }
                }

                Output = MdlPack.ToH3D();

                break;

            case GFTextureConstant:
                Output = new H3D();

                foreach (GFPackage.Entry Entry in Header.Entries)
                {
                    Input.Seek(Entry.Address, SeekOrigin.Begin);

                    Output.Textures.Add(new GFTexture(Reader).ToH3DTexture());
                }

                break;

            case GFMotionConstant:
                Output = new H3D();

                if (Skeleton == null)
                {
                    break;
                }

                for (int Index = 0; Index < Header.Entries.Length; Index++)
                {
                    Input.Seek(Header.Entries[Index].Address, SeekOrigin.Begin);

                    if (Input.Position + 4 > Input.Length)
                    {
                        break;
                    }
                    if (Reader.ReadUInt32() != GFMotionConstant)
                    {
                        continue;
                    }

                    Input.Seek(-4, SeekOrigin.Current);

                    GFMotion Mot = new GFMotion(Reader, Index);

                    H3DAnimation    SklAnim = Mot.ToH3DSkeletalAnimation(Skeleton);
                    H3DMaterialAnim MatAnim = Mot.ToH3DMaterialAnimation();
                    H3DAnimation    VisAnim = Mot.ToH3DVisibilityAnimation();

                    if (SklAnim != null)
                    {
                        SklAnim.Name = $"Motion_{Mot.Index}";

                        Output.SkeletalAnimations.Add(SklAnim);
                    }

                    if (MatAnim != null)
                    {
                        MatAnim.Name = $"Motion_{Mot.Index}";

                        Output.MaterialAnimations.Add(MatAnim);
                    }

                    if (VisAnim != null)
                    {
                        VisAnim.Name = $"Motion_{Mot.Index}";

                        Output.VisibilityAnimations.Add(VisAnim);
                    }
                }

                break;

            case BCHConstant:
                Output = new H3D();

                foreach (GFPackage.Entry Entry in Header.Entries)
                {
                    Input.Seek(Entry.Address, SeekOrigin.Begin);

                    MagicNum = Reader.ReadUInt32();

                    if (MagicNum != BCHConstant)
                    {
                        continue;
                    }

                    Input.Seek(-4, SeekOrigin.Current);

                    byte[] Buffer = Reader.ReadBytes(Entry.Length);

                    Output.Merge(H3D.Open(Buffer));
                }

                break;
            }

            return(Output);
        }
Exemplo n.º 7
0
        public static H3D IdentifyByMagic(Stream Stream, H3DDict <H3DBone> Skeleton, string FileName)
        {
            H3D Output = null;

            if (Stream.Length > 4)
            {
                BinaryReader Reader = new BinaryReader(Stream);

                uint MagicNum = Reader.ReadUInt32();

                Stream.Seek(-4, SeekOrigin.Current);

                string Magic = Encoding.ASCII.GetString(Reader.ReadBytes(4));

                Stream.Seek(0, SeekOrigin.Begin);

                if (Magic.StartsWith("BCH"))
                {
                    return(H3D.Open(Reader.ReadBytes((int)Stream.Length)));
                }
                else if (Magic.StartsWith("MOD") && FileName != null)
                {
                    return(LoadMTModel(Reader, FileName, Path.GetDirectoryName(FileName)));
                }
                else if (Magic.StartsWith("TEX") && FileName != null)
                {
                    return(new MTTexture(Reader, Path.GetFileNameWithoutExtension(FileName)).ToH3D());
                }
                else if (Magic.StartsWith("MFX"))
                {
                    MTShader = new MTShaderEffects(Reader);
                }
                else if (Magic.StartsWith("CGFX"))
                {
                    return(Gfx.Open(Stream));
                }
                else if (Magic.StartsWith("CMIF"))
                {
                    return(new CMIFFile(Stream).ToH3D());
                }
                else
                {
                    switch (MagicNum)
                    {
                    case GFModel.MagicNum:
                        GFModel Model = new GFModel(Reader, "Model");
                        Output = Model.ToH3D();
                        Output.SourceData.Add(Model);

                        break;

                    case GFTexture.MagicNum:
                        //Can be GFShader or GFTexture
                        Reader.BaseStream.Seek(0x8, SeekOrigin.Current);

                        string GFMagicStr = StringUtils.ReadPaddedString(Reader, 8);

                        if (GFMagicStr == GFTexture.MagicStr)
                        {
                            Reader.BaseStream.Seek(-0x10, SeekOrigin.Current);
                            Output = new H3D();
                            Output.Textures.Add(new GFTexture(Reader).ToH3DTexture());
                        }
                        else
                        {
                            Reader.BaseStream.Seek(0x8, SeekOrigin.Current);
                            GFMagicStr = StringUtils.ReadPaddedString(Reader, 8);

                            if (GFMagicStr == GFShader.MagicStr)
                            {
                                Reader.BaseStream.Seek(-0x18, SeekOrigin.Current);
                                Output = new H3D();
                                Output.SourceData.Add(new GFShader(Reader));
                            }
                        }

                        break;

                    case GFModelPack.MagicNum:
                        if (GFModelPack.IsModelPack(Reader))
                        {
                            Output = new GFModelPack(Reader).ToH3D();
                        }
                        break;

                    case GFMotion.MagicNum:
                        if (Skeleton != null)
                        {
                            Output = new H3D();

                            GFMotion Motion = new GFMotion(Reader, 0);

                            H3DAnimation    SklAnim = Motion.ToH3DSkeletalAnimation(Skeleton);
                            H3DMaterialAnim MatAnim = Motion.ToH3DMaterialAnimation();
                            H3DAnimation    VisAnim = Motion.ToH3DVisibilityAnimation();

                            if (SklAnim != null)
                            {
                                Output.SkeletalAnimations.Add(SklAnim);
                            }
                            if (MatAnim != null)
                            {
                                Output.MaterialAnimations.Add(MatAnim);
                            }
                            if (VisAnim != null)
                            {
                                Output.VisibilityAnimations.Add(VisAnim);
                            }
                        }

                        break;
                    }

                    if (GFMotionPack.IsGFL2MotionPack(Reader))
                    {
                        GFMotionPack Pack = new GFMotionPack(Reader);
                        Output = Pack.ToH3D(Skeleton);
                    }
                    if (GF1MotionPack.IsGFL1MotionPack(Reader))
                    {
                        Output = new GF1MotionPack(Reader).ToH3D(Skeleton);
                    }
                }
            }

            return(Output);
        }
Exemplo n.º 8
0
        public static H3D OpenAsH3D(Stream Input, GFPackage.Header Header, int FileIndex, int AnimCount, H3DDict <H3DBone> Skeleton = null)
        {
            int fileIndex = FileIndex;
            H3D Output    = default(H3D);

            BinaryReader Reader = new BinaryReader(Input);

            try
            {
                Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

                uint MagicNum = Reader.ReadUInt32();

                switch (MagicNum)
                {
                case GFModelConstant:
                    GFModelPack MdlPack = new GFModelPack();

                    //High Poly Pokémon model
                    Input.Seek(Header.Entries[0].Address, SeekOrigin.Begin);

                    MdlPack.Models.Add(new GFModel(Reader, "PM_HighPoly"));

                    //Low Poly Pokémon model
                    Input.Seek(Header.Entries[1].Address, SeekOrigin.Begin);

                    MdlPack.Models.Add(new GFModel(Reader, "PM_LowPoly"));

                    //Pokémon Shader package
                    Input.Seek(Header.Entries[2].Address, SeekOrigin.Begin);

                    GFPackage.Header PSHeader = GFPackage.GetPackageHeader(Input);

                    foreach (GFPackage.Entry Entry in PSHeader.Entries)
                    {
                        Input.Seek(Entry.Address, SeekOrigin.Begin);

                        MdlPack.Shaders.Add(new GFShader(Reader));
                    }

                    //More shaders
                    Input.Seek(Header.Entries[3].Address, SeekOrigin.Begin);

                    if (GFPackage.IsValidPackage(Input))
                    {
                        GFPackage.Header PCHeader = GFPackage.GetPackageHeader(Input);

                        foreach (GFPackage.Entry Entry in PCHeader.Entries)
                        {
                            Input.Seek(Entry.Address, SeekOrigin.Begin);

                            MdlPack.Shaders.Add(new GFShader(Reader));
                        }
                    }

                    Output = MdlPack.ToH3D();

                    break;

                case GFTextureConstant:
                    Output = new H3D();

                    foreach (GFPackage.Entry Entry in Header.Entries)
                    {
                        Input.Seek(Entry.Address, SeekOrigin.Begin);

                        Output.Textures.Add(new GFTexture(Reader).ToH3DTexture());
                    }

                    break;

                case GFMotionConstant:
                    Output = new H3D();

                    if (Skeleton == null)
                    {
                        break;
                    }
                    HashSet <uint> sklAdresses        = new HashSet <uint>();
                    HashSet <uint> materialAdresses   = new HashSet <uint>();
                    HashSet <uint> visibilityAdresses = new HashSet <uint>();
                    for (int Index = 0; Index < Header.Entries.Length; Index++)
                    {
                        Input.Seek(Header.Entries[Index].Address, SeekOrigin.Begin);
                        if (Input.Position + 4 > Input.Length)
                        {
                            break;
                        }
                        if (Reader.ReadUInt32() != GFMotionConstant)
                        {
                            continue;
                        }

                        Input.Seek(-4, SeekOrigin.Current);

                        GFMotion Mot = new GFMotion(Reader, Index);

                        H3DAnimation    SklAnim = Mot.ToH3DSkeletalAnimation(Skeleton);
                        H3DMaterialAnim MatAnim = Mot.ToH3DMaterialAnimation();
                        H3DAnimation    VisAnim = Mot.ToH3DVisibilityAnimation();

                        if (SklAnim != null)
                        {
                            // SklAnim.Name = $"Motion_{Mot.Index}";

                            // Output.SkeletalAnimations.Add(SklAnim);

                            if (!sklAdresses.Contains(Header.Entries[Index].Address))
                            {
                                Output.SkeletalAnimations.Add(SklAnim);
                                sklAdresses.Add(Header.Entries[Index].Address);
                                Console.WriteLine("skeletal " + Header.Entries[Index].Address);
                            }
                        }

                        if (MatAnim != null)
                        {
                            //MatAnim.Name = $"Motion_{Mot.Index}";

                            if (!materialAdresses.Contains(Header.Entries[Index].Address))
                            {
                                Output.MaterialAnimations.Add(MatAnim);
                                materialAdresses.Add(Header.Entries[Index].Address);
                                Console.WriteLine("material " + Header.Entries[Index].Address);
                            }
                        }

                        if (VisAnim != null)
                        {
                            //VisAnim.Name = $"Motion_{Mot.Index}";

                            Output.VisibilityAnimations.Add(VisAnim);
                            if (!visibilityAdresses.Contains(Header.Entries[Index].Address))
                            {
                                Output.VisibilityAnimations.Add(VisAnim);
                                visibilityAdresses.Add(Header.Entries[Index].Address);
                                Console.WriteLine("visibility " + Header.Entries[Index].Address);
                            }
                        }
                    }
                    // Console.WriteLine(Output.SkeletalAnimations.Count);
                    // Console.WriteLine(AnimCount);
                    while (Output.SkeletalAnimations.Count > AnimCount)
                    {
                        Output.SkeletalAnimations.Remove(Output.SkeletalAnimations.Count - 1);
                    }
                    // if (fileIndex == 4)
                    // {
                    //
                    // }
                    //todo здесь проверку
                    // if (Output.SkeletalAnimations.Any())
                    // {
                    //     Output.SkeletalAnimations.Remove(Output.SkeletalAnimations.Count-1);
                    // }

                    break;

                case BCHConstant:
                    Output = new H3D();

                    foreach (GFPackage.Entry Entry in Header.Entries)
                    {
                        Input.Seek(Entry.Address, SeekOrigin.Begin);

                        MagicNum = Reader.ReadUInt32();

                        if (MagicNum != BCHConstant)
                        {
                            continue;
                        }

                        Input.Seek(-4, SeekOrigin.Current);

                        byte[] Buffer = Reader.ReadBytes(Entry.Length);

                        Output.Merge(H3D.Open(Buffer));
                    }

                    break;
                }

                return(Output);
            }
            catch (EndOfStreamException e)
            {
                return(new H3D());

                throw;
            }
        }
Exemplo n.º 9
0
        private static void ReadModelsBG(GFPackage.Entry File, BinaryReader Reader, GFModelPack MdlPack)
        {
            if (File.Length < 0x80)
            {
                return;
            }

            Reader.BaseStream.Seek(File.Address, SeekOrigin.Begin);

            GFPackage.Header Header = GFPackage.GetPackageHeader(Reader.BaseStream);

            foreach (GFPackage.Entry Entry in Header.Entries)
            {
                if (Entry.Length < 4)
                {
                    continue;
                }

                Reader.BaseStream.Seek(Entry.Address, SeekOrigin.Begin);

                uint MagicNum = Reader.ReadUInt32();

                switch (MagicNum)
                {
                case GFModelConstant:
                    Reader.BaseStream.Seek(-4, SeekOrigin.Current);

                    MdlPack.Models.Add(new GFModel(Reader, $"Model_{MdlPack.Models.Count}"));

                    break;

                case GFTextureConstant:
                    uint Count = Reader.ReadUInt32();

                    string Signature = string.Empty;

                    for (int i = 0; i < 8; i++)
                    {
                        byte Value = Reader.ReadByte();

                        if (Value < 0x20 || Value > 0x7e)
                        {
                            break;
                        }

                        Signature += (char)Value;
                    }

                    Reader.BaseStream.Seek(Entry.Address, SeekOrigin.Begin);

                    if (Signature == "texture")
                    {
                        MdlPack.Textures.Add(new GFTexture(Reader));
                    }
                    else
                    {
                        MdlPack.MaterialShaders.Add(new GFShader(Reader));
                    }

                    break;
                }
            }
        }
Exemplo n.º 10
0
        private static void ReadModelsBG(GFPackage.Entry file, BinaryReader reader, GFModelPack mdlPack)
        {
            if (file.Length < 0x80)
            {
                return;
            }

            reader.BaseStream.Seek(file.Address, SeekOrigin.Begin);

            var header = GFPackage.GetPackageHeader(reader.BaseStream);

            foreach (var entry in header.Entries)
            {
                if (entry.Length < 4)
                {
                    continue;
                }

                reader.BaseStream.Seek(entry.Address, SeekOrigin.Begin);

                var magicNum = reader.ReadUInt32();

                switch (magicNum)
                {
                case GFModelConstant:
                    reader.BaseStream.Seek(-4, SeekOrigin.Current);

                    mdlPack.Models.Add(new GFModel(reader, $"Model_{mdlPack.Models.Count}"));

                    break;

                case GFTextureConstant:
                    var count = reader.ReadUInt32();

                    var signature = string.Empty;

                    for (var i = 0; i < 8; i++)
                    {
                        var value = reader.ReadByte();

                        if (value < 0x20 || value > 0x7e)
                        {
                            break;
                        }

                        signature += (char)value;
                    }

                    reader.BaseStream.Seek(entry.Address, SeekOrigin.Begin);

                    if (signature == "texture")
                    {
                        mdlPack.Textures.Add(new GFTexture(reader));
                    }
                    else
                    {
                        mdlPack.Shaders.Add(new GFShader(reader));
                    }

                    break;
                }
            }
        }
Exemplo n.º 11
0
        public static H3D IdentifyAndOpen(string fileName, H3DDict <H3DBone> skeleton = null)
        {
            //Formats that can by identified by extensions
            var filePath = Path.GetDirectoryName(fileName);

            switch (Path.GetExtension(fileName).ToLower())
            {
            case ".smd": return(new SMD(fileName).ToH3D(filePath));

            case ".obj": return(new OBJ(fileName).ToH3D(filePath));

            case ".mbn":
                using (var input = new FileStream(fileName, FileMode.Open))
                {
                    var baseScene = H3D.Open(File.ReadAllBytes(fileName.Replace(".mbn", ".bch")));

                    var modelBinary = new MBn(new BinaryReader(input), baseScene);

                    return(modelBinary.ToH3D());
                }
            }

            //Formats that can only be indetified by "magic numbers"
            var output = default(H3D);

            using (var fs = new FileStream(fileName, FileMode.Open))
            {
                if (fs.Length > 4)
                {
                    var reader = new BinaryReader(fs);

                    var magicNum = reader.ReadUInt32();

                    fs.Seek(-4, SeekOrigin.Current);

                    var magic = Encoding.ASCII.GetString(reader.ReadBytes(4));

                    fs.Seek(0, SeekOrigin.Begin);

                    if (magic.StartsWith("BCH"))
                    {
                        return(H3D.Open(reader.ReadBytes((int)fs.Length)));
                    }
                    else if (magic.StartsWith("MOD"))
                    {
                        return(LoadMTModel(reader, fileName, Path.GetDirectoryName(fileName)));
                    }
                    else if (magic.StartsWith("TEX"))
                    {
                        return(new MTTexture(reader, Path.GetFileNameWithoutExtension(fileName)).ToH3D());
                    }
                    else if (magic.StartsWith("MFX"))
                    {
                        _mtShader = new MTShaderEffects(reader);
                    }
                    else if (magic.StartsWith("CGFX"))
                    {
                        return(Gfx.Open(fs));
                    }
                    else
                    {
                        if (GFPackage.IsValidPackage(fs))
                        {
                            var packHeader = GFPackage.GetPackageHeader(fs);

                            switch (packHeader.Magic)
                            {
                            case "AD": output = GFPackedTexture.OpenAsH3D(fs, packHeader, 1); break;

                            case "BG": output = GFL2OverWorld.OpenAsH3D(fs, packHeader, skeleton); break;

                            case "BS": output = GFBtlSklAnim.OpenAsH3D(fs, packHeader, skeleton); break;

                            case "CM": output = GFCharaModel.OpenAsH3D(fs, packHeader); break;

                            case "GR": output = GFOWMapModel.OpenAsH3D(fs, packHeader); break;

                            case "MM": output = GFOWCharaModel.OpenAsH3D(fs, packHeader); break;

                            case "PC": output = GFPkmnModel.OpenAsH3D(fs, packHeader, skeleton); break;

                            case "PT": output = GFPackedTexture.OpenAsH3D(fs, packHeader, 0); break;

                            case "PK":
                            case "PB": output = GFPkmnSklAnim.OpenAsH3D(fs, packHeader, skeleton); break;
                            }
                        }
                        else
                        {
                            switch (magicNum)
                            {
                            case 0x15122117:
                                output = new H3D();

                                output.Models.Add(new GFModel(reader, "Model").ToH3DModel());

                                break;

                            case 0x15041213:
                                output = new H3D();

                                output.Textures.Add(new GFTexture(reader).ToH3DTexture());

                                break;

                            case 0x00010000: output = new GFModelPack(reader).ToH3D(); break;

                            case 0x00060000:
                                if (skeleton != null)
                                {
                                    output = new H3D();

                                    var motion = new GFMotion(reader, 0);

                                    var sklAnim = motion.ToH3DSkeletalAnimation(skeleton);
                                    var matAnim = motion.ToH3DMaterialAnimation();
                                    var visAnim = motion.ToH3DVisibilityAnimation();

                                    if (sklAnim != null)
                                    {
                                        output.SkeletalAnimations.Add(sklAnim);
                                    }
                                    if (matAnim != null)
                                    {
                                        output.MaterialAnimations.Add(matAnim);
                                    }
                                    if (visAnim != null)
                                    {
                                        output.VisibilityAnimations.Add(visAnim);
                                    }
                                }

                                break;
                            }
                        }
                    }
                }
            }

            return(output);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            ArgumentBuilder ab = new ArgumentBuilder(
                new ArgumentPattern("input", "One or more converter input files", ArgumentType.STRING, null, true, "-i"),
                new ArgumentPattern("output", "An optional specified output file.", ArgumentType.STRING, null, "-o"),
                new ArgumentPattern("outputType", "The output format type (h3d/gfmbdlp/gfx)", ArgumentType.STRING, "h3d", "-t", "--type"),
                new ArgumentPattern("outputVersion", "The output format version", ArgumentType.INT, (int)0x21, "-v", "--version"),
                new ArgumentPattern("filter", "Output format filters (model/texture/animation/all)", ArgumentType.STRING, "all", true, "-f", "--filter")
                );

            Console.WriteLine("SPICA Embedded Command Line Interface\n");
            ab.parse(args);

            ArgumentContent inputs = ab.getContent("input");

            if (inputs.contents.Count == 0)
            {
                Console.WriteLine("No inputs given. Stopping.\n");

                printHelp(ab);
            }
            else
            {
                string formatName = ab.getContent("outputType").stringValue();
                string formatExtension;
                switch (formatName)
                {
                case "h3d":
                    formatExtension = "bch";
                    break;

                case "gfbmdlp":
                    formatExtension = formatName;
                    break;

                case "gfx":
                    formatExtension = "bcres";
                    break;

                default:
                    Console.WriteLine("Unknown output type: " + formatName);
                    return;
                }

                H3D Scene = new H3D();

                Scene.BackwardCompatibility = (byte)ab.getContent("outputVersion").intValue();
                Scene.ForwardCompatibility  = Scene.BackwardCompatibility;

                for (int i = 0; i < inputs.contents.Count; i++)
                {
                    string inPath = inputs.stringValue(i);
                    if (File.Exists(inPath))
                    {
                        Scene.Merge(FormatIdentifier.IdentifyAndOpen(inPath));
                    }
                }

                ArgumentContent flt = ab.getContent("filter");

                bool deleteModel = true;
                bool deleteTex   = true;
                bool deleteAnime = true;

                for (int i = 0; i < flt.contents.Count; i++)
                {
                    string filter = flt.stringValue(i);
                    switch (filter)
                    {
                    case "model":
                        deleteModel = false;
                        break;

                    case "texture":
                        deleteModel = false;
                        break;

                    case "animation":
                        deleteAnime = false;
                        break;

                    case "all":
                        deleteAnime = false;
                        deleteModel = false;
                        deleteTex   = false;
                        break;

                    default:
                        Console.WriteLine("Warning: unknown filter - " + filter);
                        break;
                    }
                }

                if (deleteModel)
                {
                    Scene.Models.Clear();
                }
                if (deleteTex)
                {
                    Scene.Textures.Clear();
                }
                if (deleteAnime)
                {
                    Scene.MaterialAnimations.Clear();
                    Scene.SkeletalAnimations.Clear();
                    Scene.VisibilityAnimations.Clear();
                    Scene.FogAnimations.Clear();
                    Scene.CameraAnimations.Clear();
                    Scene.LightAnimations.Clear();
                }

                string          outputFilePath = Path.Combine(Directory.GetParent(inputs.stringValue(0)).FullName, Path.GetFileNameWithoutExtension(inputs.stringValue(0)) + "." + formatExtension);
                ArgumentContent outCnt         = ab.getContent("output", true);
                if (outCnt != null)
                {
                    outputFilePath = outCnt.stringValue();
                }

                switch (formatName)
                {
                case "h3d":
                    H3D.Save(outputFilePath, Scene);
                    break;

                case "gfbmdlp":
                    using (BinaryWriter Writer = new BinaryWriter(new FileStream(outputFilePath, FileMode.Create, FileAccess.Write)))
                    {
                        GFModelPack ModelPack = new GFModelPack(Scene);
                        ModelPack.Write(Writer);
                        Writer.Close();
                    }
                    break;

                case "gfx":
                    Gfx.Save(outputFilePath, new Gfx(Scene));
                    break;
                }
            }
        }
Exemplo n.º 13
0
        public static H3D OpenAsH3D(Stream input, GFPackage.Header header)
        {
            var output = default(H3D);

            //Model
            input.Seek(header.Entries[0].Address, SeekOrigin.Begin);

            var mdlPack = new GFModelPack(input);

            output = mdlPack.ToH3D();

            //Animations
            input.Seek(header.Entries[1].Address, SeekOrigin.Begin);

            var motPack = new GFMotionPack(input);

            foreach (var mot in motPack)
            {
                var sklAnim = mot.ToH3DSkeletalAnimation(mdlPack.Models[0].Skeleton);
                var matAnim = mot.ToH3DMaterialAnimation();
                var visAnim = mot.ToH3DVisibilityAnimation();

                if (sklAnim != null)
                {
                    sklAnim.Name = $"Motion_{mot.Index}";

                    output.SkeletalAnimations.Add(sklAnim);
                }

                if (matAnim != null)
                {
                    matAnim.Name = $"Motion_{mot.Index}";

                    output.MaterialAnimations.Add(matAnim);
                }

                if (visAnim != null)
                {
                    visAnim.Name = $"Motion_{mot.Index}";

                    output.VisibilityAnimations.Add(visAnim);
                }
            }

            //Texture
            if (header.Entries.Length > 3 && header.Entries[3].Length >= 4)
            {
                input.Seek(header.Entries[3].Address, SeekOrigin.Begin);

                var reader = new BinaryReader(input);

                var magicNum = reader.ReadUInt32();

                if (magicNum == 0x15041213)
                {
                    input.Seek(-4, SeekOrigin.Current);

                    var tex = new GFTexture(reader);

                    output.Textures.Add(tex.ToH3DTexture());
                }
            }

            return(output);
        }
Exemplo n.º 14
0
        public static H3D OpenAsH3D(Stream input, GFPackage.Header header, H3DDict <H3DBone> skeleton = null)
        {
            var output = default(H3D);

            var reader = new BinaryReader(input);

            input.Seek(header.Entries[0].Address, SeekOrigin.Begin);

            var magicNum = reader.ReadUInt32();

            switch (magicNum)
            {
            case GFModelConstant:
                var mdlPack = new GFModelPack();

                //High Poly Pokémon model
                input.Seek(header.Entries[0].Address, SeekOrigin.Begin);

                mdlPack.Models.Add(new GFModel(reader, "PM_HighPoly"));

                //Low Poly Pokémon model
                input.Seek(header.Entries[1].Address, SeekOrigin.Begin);

                mdlPack.Models.Add(new GFModel(reader, "PM_LowPoly"));

                //Pokémon Shader package
                input.Seek(header.Entries[2].Address, SeekOrigin.Begin);

                var psHeader = GFPackage.GetPackageHeader(input);

                foreach (var entry in psHeader.Entries)
                {
                    input.Seek(entry.Address, SeekOrigin.Begin);

                    mdlPack.Shaders.Add(new GFShader(reader));
                }

                //More shaders
                input.Seek(header.Entries[3].Address, SeekOrigin.Begin);

                if (GFPackage.IsValidPackage(input))
                {
                    var pcHeader = GFPackage.GetPackageHeader(input);

                    foreach (var entry in pcHeader.Entries)
                    {
                        input.Seek(entry.Address, SeekOrigin.Begin);

                        mdlPack.Shaders.Add(new GFShader(reader));
                    }
                }

                output = mdlPack.ToH3D();

                break;

            case GFTextureConstant:
                output = new H3D();

                foreach (var entry in header.Entries)
                {
                    input.Seek(entry.Address, SeekOrigin.Begin);

                    output.Textures.Add(new GFTexture(reader).ToH3DTexture());
                }

                break;

            case GFMotionConstant:
                output = new H3D();

                if (skeleton == null)
                {
                    break;
                }

                for (var index = 0; index < header.Entries.Length; index++)
                {
                    input.Seek(header.Entries[index].Address, SeekOrigin.Begin);

                    if (input.Position + 4 > input.Length)
                    {
                        break;
                    }
                    if (reader.ReadUInt32() != GFMotionConstant)
                    {
                        continue;
                    }

                    input.Seek(-4, SeekOrigin.Current);

                    var mot = new GFMotion(reader, index);

                    var sklAnim = mot.ToH3DSkeletalAnimation(skeleton);
                    var matAnim = mot.ToH3DMaterialAnimation();
                    var visAnim = mot.ToH3DVisibilityAnimation();

                    if (sklAnim != null)
                    {
                        sklAnim.Name = $"Motion_{mot.Index}";

                        output.SkeletalAnimations.Add(sklAnim);
                    }

                    if (matAnim != null)
                    {
                        matAnim.Name = $"Motion_{mot.Index}";

                        output.MaterialAnimations.Add(matAnim);
                    }

                    if (visAnim != null)
                    {
                        visAnim.Name = $"Motion_{mot.Index}";

                        output.VisibilityAnimations.Add(visAnim);
                    }
                }

                break;

            case BCHConstant:
                output = new H3D();

                foreach (var entry in header.Entries)
                {
                    input.Seek(entry.Address, SeekOrigin.Begin);

                    magicNum = reader.ReadUInt32();

                    if (magicNum != BCHConstant)
                    {
                        continue;
                    }

                    input.Seek(-4, SeekOrigin.Current);

                    var buffer = reader.ReadBytes(entry.Length);

                    output.Merge(H3D.Open(buffer));
                }

                break;
            }

            return(output);
        }
Exemplo n.º 15
0
        public static void Save(H3D Scene, SceneState State)
        {
            if (Scene == null)
            {
                MessageBox.Show(
                    "Please load a file first!",
                    "No data",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                return;
            }

            using (SaveFileDialog SaveDlg = new SaveFileDialog())
            {
                SaveDlg.Filter =
                    "COLLADA 1.4.1|*.dae" +
                    "|Valve StudioMdl|*.smd" +
                    "|Binary Ctr H3D v33|*.bch" +
                    "|Binary Ctr H3D v7|*.bch" +
                    /* "|Game Freak Binary Model Pack|*.gfbmdlp" +*/
                    "|Binary CTR Resource|*.bcres"// +
                    /*"|Game Freak Binary Model|*.gfbmdl"*/
                ;

                SaveDlg.FileName = "Model";
                if (Scene.Models.Count > 0)
                {
                    SaveDlg.FileName = Scene.Models[0].Name;
                }

                if (SaveDlg.ShowDialog() == DialogResult.OK)
                {
                    int MdlIndex  = State.ModelIndex;
                    int AnimIndex = State.SklAnimIndex;

                    switch (SaveDlg.FilterIndex)
                    {
                    case 1: new DAE(Scene, MdlIndex, AnimIndex).Save(SaveDlg.FileName); break;

                    case 2: new SMD(Scene, MdlIndex, AnimIndex).Save(SaveDlg.FileName); break;

                    case 3:
                        Scene.BackwardCompatibility = 0x21;
                        Scene.ForwardCompatibility  = 0x21;
                        H3D.Save(SaveDlg.FileName, Scene);
                        break;

                    case 4:
                        Scene.BackwardCompatibility = 0x7;
                        Scene.ForwardCompatibility  = 0x7;
                        H3D.Save(SaveDlg.FileName, Scene);
                        break;

                    case 5:
                        Gfx GFX = new Gfx(Scene);
                        Gfx.Save(SaveDlg.FileName, GFX);
                        break;

                    case 6:
                        MessageBox.Show(
                            "GFBMDLP writing comes with no warranty whatsoever. In fact, character and Pokémon models will most certainly not work at all.\n\n(Before you ask, this dialog can not be disabled. Intentionally.)",
                            "Disclaimer",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
                        using (BinaryWriter Writer = new BinaryWriter(new FileStream(SaveDlg.FileName, FileMode.Create, FileAccess.Write)))
                        {
                            GFModelPack ModelPack = new GFModelPack(Scene);
                            ModelPack.Write(Writer);
                            Writer.Close();
                        }
                        break;

                    case 7:
                        if (State.ModelIndex != -1)
                        {
                            H3DModel Model = Scene.Models[State.ModelIndex];

                            using (BinaryWriter Writer = new BinaryWriter(new FileStream(SaveDlg.FileName, FileMode.Create, FileAccess.Write)))
                            {
                                GFModel Mdl = new GFModel(Model, Scene.LUTs);
                                Mdl.Write(Writer);
                            }
                        }
                        else
                        {
                            MessageBox.Show(
                                "Please select a model to export.",
                                "No model selected",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                        }
                        break;
                    }
                }
            }
        }