public static byte[] ReadAllBytes(this Stream stream)
 {
     using (var reader = new FileReader(stream, true)) {
         reader.Position = 0;
         return(reader.ReadBytes((int)stream.Length));
     }
 }
Example #2
0
            public static byte[] Decompress(byte[] b, bool hasMagic = true)
            {
                using (var br = new FileReader(new MemoryStream(b), true))
                {
                    if (br.ReadString(4) == "ZCMP")
                    {
                        return(DecompressZCMP(b));
                    }
                    else
                    {
                        var ms = new System.IO.MemoryStream();
                        if (hasMagic)
                        {
                            br.Position = 2;
                            using (var ds = new DeflateStream(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length - 6)), CompressionMode.Decompress))
                                ds.CopyTo(ms);
                        }
                        else
                        {
                            using (var ds = new DeflateStream(new MemoryStream(b), CompressionMode.Decompress))
                                ds.CopyTo(ms);
                        }

                        return(ms.ToArray());
                    }
                }
            }
 static byte[] GetConstantsFromCode(Stream shaderCode, uint offset, uint length)
 {
     using (var reader = new Toolbox.Core.IO.FileReader(shaderCode, true))
     {
         reader.SeekBegin(offset);
         return(reader.ReadBytes((int)length));
     }
 }
Example #4
0
 public static Byte[] DecompressZCMP(byte[] b)
 {
     using (var br = new FileReader(new MemoryStream(b), true))
     {
         var ms = new System.IO.MemoryStream();
         br.BaseStream.Position = 130;
         using (var ds = new DeflateStream(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length - 80)), CompressionMode.Decompress))
             ds.CopyTo(ms);
         return(ms.ToArray());
     }
 }
Example #5
0
            public static Stream Compress(Stream stream, bool isBigEndian = true)
            {
                uint decompSize = (uint)stream.Length;

                uint[] section_sizes;
                uint   sectionCount = 0;

                var mem = new MemoryStream();

                using (var reader = new FileReader(stream, true))
                    using (var writer = new FileWriter(mem, true))
                    {
                        writer.SetByteOrder(isBigEndian);

                        if (!(decompSize % 0x10000 != 0))
                        {
                            sectionCount = decompSize / 0x10000;
                        }
                        else
                        {
                            sectionCount = (decompSize / 0x10000) + 1;
                        }

                        writer.Write(0x10000);
                        writer.Write(sectionCount);
                        writer.Write(decompSize);
                        writer.Write(new uint[sectionCount]);
                        writer.Align(128);

                        reader.SeekBegin(0);
                        section_sizes = new uint[sectionCount];
                        for (int i = 0; i < sectionCount; i++)
                        {
                            byte[] chunk = STLibraryCompression.ZLIB.Compress(reader.ReadBytes(0x10000));

                            section_sizes[i] = (uint)chunk.Length;

                            writer.Write(chunk.Length);
                            writer.Write(chunk);
                            writer.Align(128);
                        }

                        writer.SeekBegin(12);
                        for (int i = 0; i < sectionCount; i++)
                        {
                            writer.Write(section_sizes[i] + 4);
                        }
                    }
                return(mem);
            }