Exemplo n.º 1
0
        public static List <GFModel> ParseHTML(string filePath)
        {
            List <GFModel> models = new List <GFModel>();

            StreamReader gameFile = new StreamReader(filePath);

            string content = gameFile.ReadToEnd();

            gameFile.Close();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(content);

            var myDiv = doc.DocumentNode.SelectNodes("//div[@class='name']");

            for (int i = 0; i < myDiv.Count; i++)
            {
                GFModel tempModel = new GFModel();
                tempModel.Name = myDiv[i].InnerText.Replace(',', ' ').Replace(';', ' ');
                models.Add(tempModel);
            }

            myDiv = doc.DocumentNode.SelectNodes("//div[@class='extra']");

            for (int i = 0; i < myDiv.Count; i++)
            {
                models[i].Year = myDiv[i].InnerText;
            }

            return(models);
        }
Exemplo n.º 2
0
        public GFModelPack(BinaryReader Reader) : this()
        {
            long Position = Reader.BaseStream.Position;

            uint MagicNumber = Reader.ReadUInt32();

            uint[] Counts = new uint[5];

            for (int Index = 0; Index < Counts.Length; Index++)
            {
                Counts[Index] = Reader.ReadUInt32();
            }

            long PointersAddr = Reader.BaseStream.Position;

            for (int Sect = 0; Sect < Counts.Length; Sect++)
            {
                for (int Entry = 0; Entry < Counts[Sect]; Entry++)
                {
                    Reader.BaseStream.Seek(PointersAddr + Entry * 4, SeekOrigin.Begin);
                    Reader.BaseStream.Seek(Position + Reader.ReadUInt32(), SeekOrigin.Begin);

                    string Name    = Reader.ReadByteLengthString();
                    uint   Address = Reader.ReadUInt32();

                    Reader.BaseStream.Seek(Position + Address, SeekOrigin.Begin);

                    switch ((Section)Sect)
                    {
                    case Section.Model:
                        GFModel Model = new GFModel(Reader, Name);
                        Model.Name = Name;
                        Models.Add(Model);
                        break;

                    case Section.Texture:
                        Textures.Add(new GFTexture(Reader));
                        break;

                    case Section.VertShader:
                        VertexShaders.Add(new GFShader(Reader));
                        break;

                    case Section.MaterialShader:
                        MaterialShaders.Add(new GFShader(Reader));
                        break;
                    }
                }

                PointersAddr += Counts[Sect] * 4;
            }
        }
Exemplo n.º 3
0
        public H3D ToH3D()
        {
            H3D Output = new H3D();

            H3DLUT L = new H3DLUT();

            L.Name = GFModel.DefaultLUTName;

            for (int MdlIndex = 0; MdlIndex < Models.Count; MdlIndex++)
            {
                GFModel  Model = Models[MdlIndex];
                H3DModel Mdl   = Model.ToH3DModel();

                for (int MatIndex = 0; MatIndex < Model.Materials.Count; MatIndex++)
                {
                    H3DMaterialParams Params = Mdl.Materials[MatIndex].MaterialParams;

                    string FragShaderName = Model.Materials[MatIndex].FragShaderName;
                    string VtxShaderName  = Model.Materials[MatIndex].VtxShaderName;

                    GFShader FragShader = Shaders.FirstOrDefault(x => x.Name == FragShaderName);
                    GFShader VtxShader  = Shaders.FirstOrDefault(x => x.Name == VtxShaderName);

                    if (FragShader != null)
                    {
                        Params.TexEnvBufferColor = FragShader.TexEnvBufferColor;

                        Array.Copy(FragShader.TexEnvStages, Params.TexEnvStages, 6);
                    }

                    if (VtxShader != null)
                    {
                        foreach (KeyValuePair <uint, Vector4> KV in VtxShader.VtxShaderUniforms)
                        {
                            Params.VtxShaderUniforms.Add(KV.Key, KV.Value);
                        }

                        foreach (KeyValuePair <uint, Vector4> KV in VtxShader.GeoShaderUniforms)
                        {
                            Params.GeoShaderUniforms.Add(KV.Key, KV.Value);
                        }
                    }
                }

                foreach (GFLUT LUT in Model.LUTs)
                {
                    L.Samplers.Add(new H3DLUTSampler()
                    {
                        Name  = LUT.Name,
                        Table = LUT.Table
                    });
                }

                Output.Models.Add(Mdl);
            }

            Output.LUTs.Add(L);

            Output.CopyMaterials();

            foreach (GFTexture Texture in Textures)
            {
                Output.Textures.Add(Texture.ToH3DTexture());
            }

            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 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.º 6
0
        public H3D ToH3D()
        {
            H3D Output = new H3D();

            Output.SourceData.Add(this);

            H3DLUT L = new H3DLUT();

            L.Name = GFModel.DefaultLUTName;

            for (int MdlIndex = 0; MdlIndex < Models.Count; MdlIndex++)
            {
                GFModel  Model = Models[MdlIndex];
                H3DModel Mdl   = Model.ToH3DModel();

                for (int MatIndex = 0; MatIndex < Model.Materials.Count; MatIndex++)
                {
                    H3DMaterialParams Params = Mdl.Materials[MatIndex].MaterialParams;

                    string FragShaderName = Model.Materials[MatIndex].FragShaderName;
                    string VtxShaderName  = Model.Materials[MatIndex].VtxShaderName;

                    GFShader FragShader = MaterialShaders.FirstOrDefault(x => x.Name == FragShaderName);
                    GFShader VtxShader  = MaterialShaders.FirstOrDefault(x => x.Name == VtxShaderName);

                    if (FragShader != null)
                    {
                        Params.TexEnvBufferColor = FragShader.TexEnvBufferColor;

                        Array.Copy(FragShader.TexEnvStages, Params.TexEnvStages, 6);
                    }

                    Params.MetaData.Add(new H3DMetaDataValue("OriginMaterialHash", (int)GetTexEnvConfigHash(Params)));

                    if (VtxShader != null)
                    {
                        foreach (KeyValuePair <uint, Vector4> KV in VtxShader.VtxShaderUniforms)
                        {
                            Params.VtxShaderUniforms.Add(KV.Key, KV.Value);
                        }

                        foreach (KeyValuePair <uint, Vector4> KV in VtxShader.GeoShaderUniforms)
                        {
                            Params.GeoShaderUniforms.Add(KV.Key, KV.Value);
                        }
                    }
                }

                int Index = 1;

                while (Output.Models.Contains(Mdl.Name))
                {
                    Mdl.Name = $"{Mdl.Name}_{Index}";
                }

                Model.AddToH3DLUT(L);

                Output.Models.Add(Mdl);
            }

            Output.LUTs.Add(L);

            Output.CopyMaterials();

            foreach (GFTexture Texture in Textures)
            {
                Output.Textures.Add(Texture.ToH3DTexture());
            }

            /*Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(Output, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings()
             *  {
             *      ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
             *  }));*/

            return(Output);
        }
Exemplo n.º 7
0
        public void MergeH3D(H3D Scene)
        {
            foreach (object SourceData in Scene.SourceData)
            {
                if (SourceData is GFModelPack)
                {
                    MergeGFModelPack((GFModelPack)SourceData);
                }
                else if (SourceData is GFShader)
                {
                    GFShader Sha = (GFShader)SourceData;
                    if (Sha.HasVertexShader)
                    {
                        Console.WriteLine("Merging vertex shader: " + Sha.Name);
                        VertexShaders.Add(Sha);
                    }
                    else
                    {
                        Console.WriteLine("Merging material shader: " + Sha.Name);
                        MaterialShaders.Add(Sha);
                    }
                }
            }
            Models.Clear();
            Textures.Clear();

            Dictionary <uint, GFShader> ShaderHashesOriginal = new Dictionary <uint, GFShader>();
            Dictionary <uint, GFShader> ShaderHashes         = new Dictionary <uint, GFShader>();

            List <string> UsedVertexShaders   = new List <string>();
            List <string> UsedMaterialShaders = new List <string>();

            foreach (GFShader Shader in MaterialShaders)
            {
                uint Hash = GetTexEnvConfigHash(Shader);
                if (!ShaderHashes.ContainsKey(Hash))
                {
                    Console.WriteLine("Adding existing shader " + Shader.Name);
                    ShaderHashesOriginal.Add(Hash, Shader);
                    ShaderHashes.Add(Hash, Shader);
                }
            }

            foreach (H3DModel Model in Scene.Models)
            {
                GFModel GFM = new GFModel(Model, Scene.LUTs);

                foreach (H3DMaterial Material in Model.Materials)
                {
                    bool             IsAllowOriginalShader = false;
                    uint             Hash       = GetTexEnvConfigHash(Material.MaterialParams);
                    H3DMetaDataValue OriginHash = Material.MaterialParams.MetaData.Get("OriginMaterialHash");
                    if (OriginHash != null)
                    {
                        IsAllowOriginalShader = (int)OriginHash.Values[0] == Hash && ShaderHashesOriginal.ContainsKey(Hash);
                    }

                    if (!IsAllowOriginalShader)
                    {
                        GFShader Shader;
                        if (ShaderHashes.ContainsKey(Hash))
                        {
                            Shader = ShaderHashes[Hash];
                        }
                        else
                        {
                            Shader = new GFShader(Material, Material.Name + "_SHA");
                            Console.WriteLine("Generating shader " + Shader.Name);
                            ShaderHashes.Add(GetTexEnvConfigHash(Shader), Shader);
                            AddUnique(MaterialShaders, Shader);
                        }

                        foreach (GFMaterial GFMat in GFM.Materials)
                        {
                            if (GFMat.Name == Material.Name)
                            {
                                GFMat.ShaderName     = Shader.Name;
                                GFMat.FragShaderName = Shader.Name;
                                break;
                            }
                        }
                    }
                }

                foreach (GFMaterial Mat in GFM.Materials)
                {
                    if (!UsedVertexShaders.Contains(Mat.VtxShaderName))
                    {
                        UsedVertexShaders.Add(Mat.VtxShaderName);
                    }
                    if (!UsedMaterialShaders.Contains(Mat.FragShaderName))
                    {
                        UsedMaterialShaders.Add(Mat.FragShaderName);
                    }
                }

                AddUnique(Models, GFM);
            }

            for (int i = 0; i < MaterialShaders.Count; i++)
            {
                if (!UsedMaterialShaders.Contains(MaterialShaders[i].Name))
                {
                    Console.WriteLine("Removing unused shader " + MaterialShaders[i].Name);
                    MaterialShaders.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < VertexShaders.Count; i++)
            {
                if (!UsedVertexShaders.Contains(VertexShaders[i].Name))
                {
                    Console.WriteLine("Removing unused shader " + VertexShaders[i].Name);
                    VertexShaders.RemoveAt(i);
                    i--;
                }
            }

            foreach (H3DTexture Texture in Scene.Textures)
            {
                AddUnique(Textures, new GFTexture(Texture));
            }
        }
Exemplo n.º 8
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;
                    }
                }
            }
        }