Ejemplo n.º 1
0
        public Texture(string path, string xnb)
        {
            string fn = GetName(path, xnb);

            if (fn == null)
                throw new Exception(String.Format("Couldn't locate {0}", xnb));
            using (BinaryReader b = new BinaryReader(File.Open(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                UInt32 header = b.ReadUInt32();
                // xnb header is XNBw, XNBx, XNBm for win, unix, mac, respectively.
                if (header != 0x77424e58 && header != 0x78424e58 && header != 0x6d424e58)
                    throw new Exception(String.Format("{0} is not a valid XNB", xnb));
                UInt16 version = b.ReadUInt16();
                bool compressed = (version & 0x8000) == 0x8000;
                version &= 0xff; //ignore graphics profile
                if (version != 4 && version != 5)
                    throw new Exception(String.Format("{0}: Invalid XNB Version", xnb));
                int length = b.ReadInt32(); //length of entire file
                if (compressed)
                {
                    int decompSize = b.ReadInt32();
                    MemoryStream ms = new MemoryStream(decompSize);
                    LzxDecoder lzx = new LzxDecoder(16);
                    for (int pos = 14; pos < length; )
                    {
                        b.BaseStream.Seek(pos, SeekOrigin.Begin);
                        byte hi = b.ReadByte();
                        byte lo = b.ReadByte();
                        pos += 2;
                        int compLen = (hi << 8) | lo;
                        int decompLen = 0x8000;
                        if (hi == 0xff)
                        {
                            hi = lo;
                            lo = b.ReadByte();
                            decompLen = (hi << 8) | lo;
                            hi = b.ReadByte();
                            lo = b.ReadByte();
                            compLen = (hi << 8) | lo;
                            pos += 3;
                        }
                        if (compLen == 0 || decompLen == 0) //done
                            break;
                        if (lzx.Decompress(b.BaseStream, compLen, ms, decompLen) < 0)
                            throw new Exception("Failed to decompress");
                        pos += compLen;
                    }
                    ms.Seek(0, SeekOrigin.Begin);
                    ReadTexture(ms);
                }
                else
                    ReadTexture(b.BaseStream);
            }
        }
Ejemplo n.º 2
0
        public Texture(string path, string xnb)
        {
            string fn = GetName(path, xnb);

            if (fn == null)
            {
                throw new Exception(String.Format("Couldn't locate {0}", xnb));
            }
            using (BinaryReader b = new BinaryReader(File.Open(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                UInt32 header = b.ReadUInt32();
                // xnb header is XNBw, XNBx, XNBm for win, unix, mac, respectively.
                if (header != 0x77424e58 && header != 0x78424e58 && header != 0x6d424e58)
                {
                    throw new Exception(String.Format("{0} is not a valid XNB", xnb));
                }
                UInt16 version    = b.ReadUInt16();
                bool   compressed = (version & 0x8000) == 0x8000;
                version &= 0xff; //ignore graphics profile
                if (version != 4 && version != 5)
                {
                    throw new Exception(String.Format("{0}: Invalid XNB Version", xnb));
                }
                int length = b.ReadInt32(); //length of entire file
                if (compressed)
                {
                    int          decompSize = b.ReadInt32();
                    MemoryStream ms         = new MemoryStream(decompSize);
                    LzxDecoder   lzx        = new LzxDecoder(16);
                    for (int pos = 14; pos < length;)
                    {
                        b.BaseStream.Seek(pos, SeekOrigin.Begin);
                        byte hi = b.ReadByte();
                        byte lo = b.ReadByte();
                        pos += 2;
                        int compLen   = (hi << 8) | lo;
                        int decompLen = 0x8000;
                        if (hi == 0xff)
                        {
                            hi        = lo;
                            lo        = b.ReadByte();
                            decompLen = (hi << 8) | lo;
                            hi        = b.ReadByte();
                            lo        = b.ReadByte();
                            compLen   = (hi << 8) | lo;
                            pos      += 3;
                        }
                        if (compLen == 0 || decompLen == 0) //done
                        {
                            break;
                        }
                        if (lzx.Decompress(b.BaseStream, compLen, ms, decompLen) < 0)
                        {
                            throw new Exception("Failed to decompress");
                        }
                        pos += compLen;
                    }
                    ms.Seek(0, SeekOrigin.Begin);
                    ReadTexture(ms);
                }
                else
                {
                    ReadTexture(b.BaseStream);
                }
            }
        }