Ejemplo n.º 1
0
        public static Tex LoadTex(byte[] bytes, int maxMipmapsToLoad = -1)
        {
            var stream = new MemoryStream(bytes);
            var reader = new BinaryReader(stream, Encoding.ASCII);
            Tex tex;

            try
            {
                tex       = new Tex();
                tex.Magic = reader.ReadNString();

                if (tex.Magic != "TEXV0005")
                {
                    throw new InvalidTexHeaderMagic("TEXV0005", tex.Magic);
                }

                tex.Magic2 = reader.ReadNString();

                if (tex.Magic2 != "TEXI0001")
                {
                    throw new InvalidTexHeaderMagic("TEXI0001", tex.Magic2);
                }

                tex.Format        = (TexFormat)reader.ReadInt32();
                tex._unkInt_1     = reader.ReadInt32();
                tex.TextureWidth  = reader.ReadInt32();
                tex.TextureHeight = reader.ReadInt32();
                tex.ImageWidth    = reader.ReadInt32();
                tex.ImageHeight   = reader.ReadInt32();
                tex._unkInt_2     = reader.ReadUInt32(); // some checksum or something

                // mipmap header
                tex.TextureContainerMagic = reader.ReadNString();

                if (tex.TextureContainerMagic == "TEXB0003")
                {
                    tex.TextureContainerVersion = TexMipmapVersion.Version3;
                    tex._unkIntCont_0           = reader.ReadInt32();
                    tex.ImageFormat             = (FreeImageFormat)reader.ReadInt32();
                }
                else if (tex.TextureContainerMagic == "TEXB0002")
                {
                    tex.TextureContainerVersion = TexMipmapVersion.Version2;
                    tex._unkIntCont_0           = reader.ReadInt32();
                }
                else
                {
                    throw new InvalidTexHeaderMagic("TEXB0003/TEXB0002", tex.TextureContainerMagic);
                }

                tex.MipmapCount = reader.ReadInt32();

                if (maxMipmapsToLoad == 0)
                {
                    return(tex);
                }

                var mipmapCount = tex.MipmapCount;
                if (maxMipmapsToLoad > 0)
                {
                    mipmapCount = Math.Min(maxMipmapsToLoad, mipmapCount);
                }

                for (var i = 0; i < mipmapCount; i++)
                {
                    tex.Mipmaps.Add(ReadMipmap(reader));
                }
            }
            finally
            {
                reader.Close();
            }

            return(tex);
        }
Ejemplo n.º 2
0
        public static Tex LoadTex(byte[] bytes, int maxMipmapsToLoad = -1)
        {
            var stream = new MemoryStream(bytes);
            var reader = new BinaryReader(stream, Encoding.ASCII);
            Tex tex;

            try
            {
                tex = new Tex {
                    Magic = reader.ReadNString()
                };

                if (tex.Magic != "TEXV0005")
                {
                    throw new InvalidTexHeaderMagic("TEXV0005", tex.Magic);
                }

                tex.Magic2 = reader.ReadNString();

                if (tex.Magic2 != "TEXI0001")
                {
                    throw new InvalidTexHeaderMagic("TEXI0001", tex.Magic2);
                }

                tex.FormatId = reader.ReadInt32();
                switch (tex.FormatId)
                {
                case 0:
                    tex.Format = TexFormat.ARGB8888;
                    break;

                case 4:
                    tex.Format = TexFormat.DXT5;
                    break;

                case 6:
                    tex.Format = TexFormat.DXT3;
                    break;

                case 7:
                    tex.Format = TexFormat.DXT1;
                    break;

                default:
                    throw new Exception(
                              $"Unknown tex format id: {tex.FormatId} for {tex.TextureContainerMagic}");
                }

                tex.Flags         = (TexFlags)reader.ReadInt32();
                tex.TextureWidth  = reader.ReadInt32();
                tex.TextureHeight = reader.ReadInt32();
                tex.ImageWidth    = reader.ReadInt32();
                tex.ImageHeight   = reader.ReadInt32();
                tex.UnkInt0       = reader.ReadUInt32();

                // mipmap header
                tex.TextureContainerMagic = reader.ReadNString();

                if (tex.TextureContainerMagic == "TEXB0003")
                {
                    tex.TextureContainerVersion = TexMipmapVersion.Version3;
                    tex.UnkIntCont0             = reader.ReadInt32();
                    tex.ImageFormat             = (FreeImageFormat)reader.ReadInt32();
                }
                else if (tex.TextureContainerMagic == "TEXB0002")
                {
                    tex.TextureContainerVersion = TexMipmapVersion.Version2;
                    tex.UnkIntCont0             = reader.ReadInt32();
                }
                else if (tex.TextureContainerMagic == "TEXB0001")
                {
                    tex.TextureContainerVersion = TexMipmapVersion.Version1;
                    tex.UnkIntCont0             = reader.ReadInt32();
                }
                else
                {
                    throw new InvalidTexHeaderMagic("TEXB0001/TEXB0002/TEXB0003", tex.TextureContainerMagic);
                }

                tex.MipmapCount = reader.ReadInt32();

                if (maxMipmapsToLoad == 0)
                {
                    return(tex);
                }

                var mipmapCount = tex.MipmapCount;
                if (maxMipmapsToLoad > 0)
                {
                    mipmapCount = Math.Min(maxMipmapsToLoad, mipmapCount);
                }

                for (var i = 0; i < mipmapCount; i++)
                {
                    tex.Mipmaps.Add(ReadMipmap(reader, tex.TextureContainerVersion));
                }
            }
            finally
            {
                reader.Close();
            }

            return(tex);
        }