示例#1
0
 private byte[] serializeForDynamicType(params object[] vals)
 {
     var elt = new BEBinaryWriter();
     foreach (object p in vals)
     {
         if (p is int)
         {
             elt.WriteUInt16(0x8000 | 'i');
             elt.WriteUInt16(4);
             elt.WriteInt32((int) p);
             elt.WriteByte(0);
         }
         else if (p is String)
         {
             elt.WriteUInt16(0x8000 | 's');
             elt.WriteString(p as string);
             elt.WriteByte(0);
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
     var ret = new byte[elt.Length];
     Buffer.BlockCopy(elt.GetBuffer(), 0, ret, 0, (int) elt.Length);
     return ret;
 }
 public RequestFrame GetFrame()
 {
     var wb = new BEBinaryWriter();
     wb.WriteFrameHeader(0x01, 0x00, (byte)_streamId, OpCode);
     wb.WriteLongString(_cqlQuery);
     return wb.GetFrame();
 }
示例#3
0
 public RequestFrame GetFrame(short streamId)
 {
     var wb = new BEBinaryWriter();
     wb.WriteFrameHeader((byte)ProtocolVersion, 0x00, streamId, OpCode);
     wb.WriteLongString(Query);
     return wb.GetFrame();
 }
示例#4
0
文件: c1PIX.cs 项目: q4a/ToxicRagers
        public void Save(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
                using (BEBinaryWriter bw = new BEBinaryWriter(fs))
                {
                    bw.WriteInt32(0x12);
                    bw.WriteInt32(0x8);
                    bw.WriteInt32(0x2);
                    bw.WriteInt32(0x2);

                    foreach (PIXIE pixie in Pixies)
                    {
                        bw.WriteInt32(0x3);
                        bw.WriteInt32(12 + pixie.Name.Length);
                        bw.WriteByte((byte)pixie.Format);
                        bw.WriteInt16(pixie.RowSize);
                        bw.WriteInt16(pixie.Width);
                        bw.WriteInt16(pixie.Height);
                        bw.WriteInt16(pixie.HalfWidth);
                        bw.WriteInt16(pixie.HalfHeight);
                        bw.WriteString(pixie.Name.ToUpper());
                        bw.WriteByte(0);

                        bw.WriteInt32(0x21);
                        bw.WriteInt32(8 + (pixie.RowSize * pixie.Height));
                        bw.WriteInt32(pixie.PixelCount);
                        bw.WriteInt32(pixie.PixelSize);
                        bw.Write(pixie.Data);

                        bw.WriteInt32(0x0);
                        bw.WriteInt32(0);
                    }
                }
        }
        /*
         * Test a wide row of size 1,000,000 consisting of a ByteBuffer
         */
        private void testByteRows()
        {
            // Build small ByteBuffer sample
            BEBinaryWriter bw = new BEBinaryWriter();

            for (int i = 0; i < 56; i++)
            {
                bw.WriteByte(0);
            }
            bw.WriteUInt16((ushort)0xCAFE);
            byte[] bb = new byte[58];
            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 500; ++i)//1000000
            {
                session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, Cassandra.CqlQueryTools.ToHex(bb)), ConsistencyLevel.Quorum);
            }

            // Read data
            using (var rs = session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
            {
                // Verify data
                foreach (Row row in rs.GetRows())
                {
                    Assert.ArrEqual((byte[])row["i"], bb);
                }
            }
        }
        public RequestFrame GetFrame()
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(RequestFrame.ProtocolRequestVersionByte, 0x00, (byte)_streamId, OpCode);
            return(wb.GetFrame());
        }
示例#7
0
        /*
         * Test a wide row consisting of a ByteBuffer
         */

        private void testByteRows()
        {
            // Build small ByteBuffer sample
            var bw = new BEBinaryWriter();

            for (int i = 0; i < 56; i++)
            {
                bw.WriteByte(0);
            }
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];

            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
            {
                Session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);
            }

            // Read data
            var rs = Session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key, ConsistencyLevel.Quorum);

            // Verify data
            foreach (var row in rs)
            {
                Assert.AreEqual((byte[])row["i"], bb);
            }
        }
示例#8
0
        public RequestFrame GetFrame()
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(0x01, 0x00, (byte)_streamId, OpCode);
            return(wb.GetFrame());
        }
示例#9
0
        // Test a wide row consisting of a ByteBuffer
        private static void TestByteRows(ISession session, string tableName)
        {
            session.Execute(String.Format("CREATE TABLE {0} (k INT, i {1}, PRIMARY KEY(k,i))", tableName, "BLOB"));

            // Build small ByteBuffer sample
            var bw = new BEBinaryWriter();

            for (int i = 0; i < 56; i++)
            {
                bw.WriteByte(0);
            }
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];

            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
            {
                session.Execute(string.Format("INSERT INTO {0}(k,i) values({1},0x{2})", tableName, Key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);
            }

            // Read data
            var rs = session.Execute("SELECT i FROM " + tableName + " WHERE k = " + Key, ConsistencyLevel.Quorum);

            // Verify data
            foreach (var row in rs)
            {
                Assert.AreEqual((byte[])row["i"], bb);
            }
        }
示例#10
0
        public RequestFrame GetFrame(short streamId)
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader((byte)ProtocolVersion, 0x00, streamId, OpCode);
            return(wb.GetFrame());
        }
示例#11
0
        private byte[] serializeForDynamicType(params object[] vals)
        {
            BEBinaryWriter elt = new BEBinaryWriter();

            foreach (var p in vals)
            {
                if (p is int)
                {
                    elt.WriteUInt16((ushort)(0x8000 | 'i'));
                    elt.WriteUInt16((ushort)4);
                    elt.WriteInt32((int)p);
                    elt.WriteByte((byte)0);
                }
                else if (p is String)
                {
                    elt.WriteUInt16((ushort)(0x8000 | 's'));
                    elt.WriteString(p as string);
                    elt.WriteByte((byte)0);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            var ret = new byte[elt.Length];

            Buffer.BlockCopy(elt.GetBuffer(), 0, ret, 0, (int)elt.Length);
            return(ret);
        }
        public override void Write(IGeometry geometry, Stream stream)
        {
            BinaryWriter writer;

            if (this.encodingType == ByteOrder.LittleEndian)
            {
                writer = new BinaryWriter(stream);
            }
            else
            {
                writer = new BEBinaryWriter(stream);
            }
            using (writer)
            {
                writer.Write(geometry.SRID < 0 ? 0 : geometry.SRID);
                if (geometry.IsEmpty)
                {
                    WriteGeometryCollectionEmpty(geometry, writer);
                }
                else
                {
                    Write(geometry, writer);
                }
            }
        }
        public RequestFrame GetFrame(byte protocolVersionByte)
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(protocolVersionByte, 0x00, (byte)_streamId, OpCode);
            wb.WriteBytes(_token);
            return(wb.GetFrame());
        }
示例#14
0
        public RequestFrame GetFrame(byte protocolVersionByte)
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(protocolVersionByte, 0x00, (byte)_streamId, OpCode);
            wb.WriteLongString(_cqlQuery);
            return(wb.GetFrame());
        }
示例#15
0
 public RequestFrame GetFrame()
 {
     var wb = new BEBinaryWriter();
     wb.WriteFrameHeader(0x01, _flags, (byte) _streamId, OpCode);
     wb.WriteLongString(_cqlQuery);
     wb.WriteInt16((short) _consistency);
     return wb.GetFrame();
 }
        public RequestFrame GetFrame()
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(0x01, _flags, (byte)_streamId, OpCode);
            wb.WriteLongString(_cqlQuery);
            wb.WriteInt16((short)_consistency);
            return(wb.GetFrame());
        }
示例#17
0
        public void save(string filename)
        {
            FileStream     stream = new FileStream(filename, FileMode.Create);
            BEBinaryWriter writer = new BEBinaryWriter(stream, Encoding.UTF8);

            // Retrieve information
            uint file_size    = HEADER_SIZE;
            uint table_offset = 0;

            foreach (KeyValuePair <string, T> p in m_entries)
            {
                table_offset += p.Value.get_size();

                file_size += p.Value.get_size();
                file_size += ENTRY_SIZE;
                file_size += (uint)p.Key.Length + 1;
            }

            // Write header data
            writer.Write(file_size);
            writer.Write(table_offset);
            writer.Write((uint)0);
            writer.Write((uint)m_entries.Count);
            writer.Write((uint)0);
            writer.Write((uint)0);
            writer.Write((uint)0);
            writer.Write((uint)0);

            // Write event data, record offsets
            SortedDictionary <string, uint> entry_offsets = new SortedDictionary <string, uint>();

            foreach (KeyValuePair <string, T> p in m_entries)
            {
                entry_offsets.Add(p.Key, (uint)stream.Position - HEADER_SIZE);
                p.Value.save(writer);
            }

            // Write table data
            uint current_string_offset = 0;

            foreach (KeyValuePair <string, uint> p in entry_offsets) // expected to iterate the same as m_entries
            {
                writer.Write(p.Value);
                writer.Write(current_string_offset);
                current_string_offset += (uint)p.Key.Length + 1;
            }

            // Write string data
            foreach (KeyValuePair <string, uint> p in entry_offsets) // expected to iterate the same as m_entries
            {
                writer.Write(p.Key.ToCharArray());
                writer.Write('\0');
            }

            writer.Close();
            stream.Close();
        }
示例#18
0
        public RequestFrame GetFrame(byte protocolVersionByte)
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(protocolVersionByte, _headerFlags, (byte)_streamId, OpCode);
            wb.WriteLongString(_cqlQuery);

            _queryProtocolOptions.Write(wb, _consistency, protocolVersionByte);

            return(wb.GetFrame());
        }
示例#19
0
        public void Save(string Path)
        {
            if (Materials.Count == 0)
            {
                return;
            }

            using (BEBinaryWriter bw = new BEBinaryWriter(new FileStream(Path, FileMode.Create)))
            {
                bw.WriteInt32(18);
                bw.WriteInt32(8);
                bw.WriteInt32(5);
                bw.WriteInt32(2);

                foreach (MATMaterial M in Materials)
                {
                    bw.Write(new byte[] { 0, 0, 0, 60 });
                    bw.WriteInt32(68 + M.Name.Length);

                    bw.Write(M.DiffuseColour);
                    bw.WriteSingle(M.AmbientLighting);
                    bw.WriteSingle(M.DirectionalLighting);
                    bw.WriteSingle(M.SpecularLighting);
                    bw.WriteSingle(M.SpecularPower);

                    bw.WriteInt32(M.Flags);

                    bw.WriteSingle(M.UVMatrix.M11);
                    bw.WriteSingle(M.UVMatrix.M12);
                    bw.WriteSingle(M.UVMatrix.M21);
                    bw.WriteSingle(M.UVMatrix.M22);
                    bw.WriteSingle(M.UVMatrix.M31);
                    bw.WriteSingle(M.UVMatrix.M32);

                    bw.Write(new byte[] { 10, 31, 0, 0 });                          //Unknown, seems to be a constant
                    bw.Write(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); //13 bytes of nothing please!

                    bw.Write(M.Name.ToCharArray());
                    bw.WriteByte(0);

                    if (M.HasTexture)
                    {
                        bw.Write(new byte[] { 0, 0, 0, 28 });
                        bw.WriteInt32(M.Texture.Length + 1);
                        bw.Write(M.Texture.ToCharArray());
                        bw.WriteByte(0);
                    }

                    bw.Write(0);
                    bw.Write(0);
                }
            }
        }
示例#20
0
 public RequestFrame GetFrame()
 {
     var wb = new BEBinaryWriter();
     wb.WriteFrameHeader(RequestFrame.ProtocolRequestVersionByte, _flags, (byte)_streamId, OpCode);
     wb.WriteShortBytes(_id);
     wb.WriteUInt16((ushort) _values.Length);
     for (int i = 0; i < _metadata.Columns.Length; i++)
     {
         var bytes = _metadata.ConvertFromObject(i, _values[i]);
         wb.WriteBytes(bytes);
     }
     wb.WriteInt16((short)_consistency);
     return wb.GetFrame();
 }
示例#21
0
        public RequestFrame GetFrame()
        {
            var wb = new BEBinaryWriter();

            wb.WriteFrameHeader(0x01, _flags, (byte)_streamId, OpCode);
            wb.WriteShortBytes(_id);
            wb.WriteUInt16((ushort)_values.Length);
            for (int i = 0; i < _metadata.Columns.Length; i++)
            {
                var bytes = _metadata.ConvertFromObject(i, _values[i]);
                wb.WriteBytes(bytes);
            }
            wb.WriteInt16((short)_consistency);
            return(wb.GetFrame());
        }
示例#22
0
 public void WriteToBatch(BEBinaryWriter wb)
 {
     wb.WriteByte(0); //not a prepared query
     wb.WriteLongString(_cqlQuery);
     if (_queryProtocolOptions.Values == null || _queryProtocolOptions.Values.Length == 0)
     {
         wb.WriteInt16(0); //not values
     }
     else
     {
         wb.WriteUInt16((ushort)_queryProtocolOptions.Values.Length);
         for (int i = 0; i < _queryProtocolOptions.Values.Length; i++)
         {
             byte[] bytes = TypeInterpreter.InvCqlConvert(_queryProtocolOptions.Values[i]);
             wb.WriteBytes(bytes);
         }
     }
 }
示例#23
0
        public void Save(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
                using (BEBinaryWriter bw = new BEBinaryWriter(fs, Encoding.Default))
                {
                    int length;

                    bw.WriteInt32(0x12); // Magic Number
                    bw.WriteInt32(0x8);  //
                    bw.WriteInt32(0x1);  //
                    bw.WriteInt32(0x2);  //

                    foreach (ACTNode A in Sections)
                    {
                        bw.WriteInt32((int)A.Section);

                        switch (A.Section)
                        {
                        case Section.Name:
                            length = A.Identifier.Length + 3;

                            bw.WriteInt32(length);
                            bw.WriteByte((byte)A.ActorType);
                            bw.WriteByte((byte)A.RenderStyle);
                            bw.Write(A.Identifier.ToCharArray());
                            bw.WriteByte(0);
                            break;

                        case Section.Matrix:
                            bw.WriteInt32(48);
                            bw.WriteSingle(A.Transform.M11);
                            bw.WriteSingle(A.Transform.M12);
                            bw.WriteSingle(A.Transform.M13);
                            bw.WriteSingle(A.Transform.M21);
                            bw.WriteSingle(A.Transform.M22);
                            bw.WriteSingle(A.Transform.M23);
                            bw.WriteSingle(A.Transform.M31);
                            bw.WriteSingle(A.Transform.M32);
                            bw.WriteSingle(A.Transform.M33);
                            bw.WriteSingle(A.Transform.M41);
                            bw.WriteSingle(A.Transform.M42);
                            bw.WriteSingle(A.Transform.M43);
                            break;

                        case Section.Section37:
                        case Section.SubLevelBegin:
                        case Section.SubLevelEnd:
                            bw.WriteInt32(0);
                            break;

                        case Section.Model:
                            bw.WriteInt32(A.Model.Length + 1);
                            bw.Write(A.Model.ToCharArray());
                            bw.WriteByte(0);
                            break;
                        }
                    }

                    bw.WriteInt32(0);
                    bw.WriteInt32(0);
                }
        }
示例#24
0
        /*
         * Test a wide row consisting of a ByteBuffer
         */
        private void testByteRows()
        {
            // Build small ByteBuffer sample
            var bw = new BEBinaryWriter();
            for (int i = 0; i < 56; i++)
                bw.WriteByte(0);
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];
            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
                Session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);

            // Read data
            var rs = Session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key, ConsistencyLevel.Quorum);
            // Verify data
            foreach (var row in rs)
            {
                Assert.AreEqual((byte[])row["i"], bb);
            }
        }
示例#25
0
        /*
         * Test a wide row of size 1,000,000 consisting of a ByteBuffer
         */
        private void testByteRows()
        {
            // Build small ByteBuffer sample
            BEBinaryWriter bw = new BEBinaryWriter();
            for (int i = 0; i < 56; i++)
                bw.WriteByte(0);
            bw.WriteUInt16((ushort)0xCAFE);
            byte[] bb = new byte[58];
            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 500; ++i)//1000000
                session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, Cassandra.CqlQueryTools.ToHex(bb)), ConsistencyLevel.Quorum);

            // Read data
            using (var rs = session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
            {
                // Verify data
                foreach (Row row in rs.GetRows())
                    Assert.ArrEqual((byte[])row["i"], bb);
            }
        }
示例#26
0
 public RequestFrame GetFrame()
 {
     var wb = new BEBinaryWriter();
     wb.WriteFrameHeader(RequestFrame.ProtocolRequestVersionByte, 0x00, (byte)_streamId, OpCode);
     return wb.GetFrame();
 }
示例#27
0
        static byte[] TryFixNameTable(ref byte[] data)
        {
            /*
             * For a font to render properly in the XAML engine it requires
             * a FamilyName to be properly set in the font's 'name' table.
             * It is valid to not have this value set and web fonts may
             * intentionally leave this blank as most web-font renderers will
             * work fine without it. However as we need it to get XAML to render
             * the font we need to manually pull the font name out from somewhere
             * else and stick it in the expected place.
             */

            using var ms = new MemoryStream(data);
            using var t  = new BEBinaryReader(ms);

            var version = t.ReadUInt16();

            if (version == 0)
            {
                var count  = t.ReadUInt16();
                var offset = t.ReadUInt16();
                var recs   = Enumerable.Range(0, count).Select(_ =>
                {
                    return(new NameRecord
                    {
                        PlatformId = t.ReadUInt16(),
                        EncodingId = t.ReadUInt16(),
                        LangId = t.ReadUInt16(),
                        NameId = t.ReadUInt16(),
                        Length = t.ReadUInt16(),
                        Offset = t.ReadUInt16()
                    });
                }).ToList();



                // Debug code to view table data.
                //var options = recs.Where(r => r.PlatformId == 3).ToList();
                //var ns = recs.Where(r => r.NameId == 1).ToList();
                //foreach (var rec in recs)
                //{
                //    ms.Seek(rec.Offset + offset, SeekOrigin.Begin);
                //    byte[] buf = t.ReadBytes(rec.Length);
                //    Encoding enc2;
                //    if (rec.EncodingId == 3 || rec.EncodingId == 1)
                //    {
                //        enc2 = Encoding.BigEndianUnicode;
                //    }
                //    else
                //    {
                //        enc2 = Encoding.UTF8;
                //    }
                //    string strRet = enc2.GetString(buf, 0, buf.Length);
                //    System.Diagnostics.Debug.WriteLine(strRet);
                //}

                var fam  = recs.FirstOrDefault(r => r.PlatformId == 3 && r.NameId == 1);
                var post = recs.FirstOrDefault(r => r.NameId == 6);
                if (fam is not null && post is not null &&
                    fam.Length == 0 && post.Length != 0)
                {
                    fam.Length = post.Length;
                    fam.Offset = post.Offset;
                }
                else
                {
                    return(null);
                }


                using var mso = new MemoryStream();
                using var w   = new BEBinaryWriter(mso);
                w.Write(version);
                w.Write(count);
                w.Write(offset);
                foreach (var r in recs)
                {
                    w.Write(r.PlatformId);
                    w.Write(r.EncodingId);
                    w.Write(r.LangId);
                    w.Write(r.NameId);
                    w.Write(r.Length);
                    w.Write(r.Offset);
                }
                w.Flush();

                ms.Seek(offset, SeekOrigin.Begin);
                ms.CopyTo(mso);
                mso.Flush();
                return(mso.ToArray());
            }
示例#28
0
        public static ConversionStatus Convert(
            Stream input,
            Stream output)
        {
            using var reader = new BEBinaryReader(input);

            var header = new WoffHeader
            {
                Signature         = reader.ReadUInt32(),
                Flavor            = reader.ReadUInt32(),
                Length            = reader.ReadUInt32(),
                TableCount        = reader.ReadUInt16(),
                Reserved          = reader.ReadUInt16(),
                TotalSfntSize     = reader.ReadUInt32(),
                MajorVersion      = reader.ReadUInt16(),
                MinorVersion      = reader.ReadUInt16(),
                MetaOffset        = reader.ReadUInt32(),
                MetaLength        = reader.ReadUInt32(),
                MetaOrignalLength = reader.ReadUInt32(),
                PrivOffset        = reader.ReadUInt32(),
                PrivLength        = reader.ReadUInt32()
            };

            if (header.Signature is not 0x774F4646)
            {
                if (header.Signature is 0x774F4632)
                {
                    return(ConversionStatus.UnsupportedWOFF2);
                }
                else
                {
                    return(ConversionStatus.UnrecognisedFile);
                }
            }


            UInt32 offset = 12;

            // Read Table Headers
            List <TableDirectory> entries = new List <TableDirectory>();

            for (var i = 0; i < header.TableCount; i++)
            {
                var entry = new TableDirectory
                {
                    Tag              = reader.ReadUInt32(),
                    Offset           = reader.ReadUInt32(),
                    CompressedLength = reader.ReadUInt32(),
                    OriginalLength   = reader.ReadUInt32(),
                    OriginalChecksum = reader.ReadUInt32()
                };

                if (Name(entry.Tag) == "DSIG") // Conversion invalidates
                {
                    continue;
                }

                entries.Add(entry);
                offset += (4 * 4);
            }

            // Amend table count after removing DSIG
            header.TableCount = (ushort)entries.Count;

            // Calculate header values
            UInt16 entrySelector = 0;

            while (Math.Pow(2, entrySelector) <= header.TableCount)
            {
                entrySelector++;
            }
            entrySelector--;

            UInt16 searchRange = (UInt16)(Math.Pow(2, entrySelector) * 16);
            UInt16 rangeShift  = (UInt16)(header.TableCount * 16 - searchRange);

            // Create writer
            using var writer = new BEBinaryWriter(output);

            // Write Font Header
            writer.Write(header.Flavor);
            writer.Write(header.TableCount);
            writer.Write(searchRange);
            writer.Write(entrySelector);
            writer.Write(rangeShift);

            // Write Table Headers
            foreach (var entry in entries)
            {
                writer.Write(entry.Tag);
                writer.Write(entry.OriginalChecksum);
                writer.Write(offset);
                writer.Write(entry.OriginalLength);
                entry.OutputOffset = offset;

                offset += entry.OriginalLength;
                if ((offset % 4) != 0)
                {
                    offset += 4 - (offset % 4);
                }
            }

            // Write Table contents
            foreach (var entry in entries)
            {
                input.Seek(entry.Offset, SeekOrigin.Begin);
                byte[] compressed = reader.ReadBytes((int)entry.CompressedLength);
                byte[] uncompressed;
                if (entry.CompressedLength != entry.OriginalLength)
                {
                    // Decompress table
                    using var comp = new MemoryStream(compressed.AsSpan().Slice(2).ToArray()); // Ignore the ZLib header (2 bytes long)
                    using var outs = new MemoryStream();
                    using var def  = new DeflateStream(comp, CompressionMode.Decompress);
                    def.CopyTo(outs);
                    uncompressed = outs.ToArray();
                }
                else
                {
                    uncompressed = compressed;
                }

                if (uncompressed.Length != entry.OriginalLength)
                {
                    return(ConversionStatus.TableLengthMismatch);
                }

                if (entry.ToString() == "name")
                {
                    if (TryFixNameTable(ref uncompressed) is byte[] ammended)
                    {
                        uncompressed = ammended;
                    }
                }

                output.Seek(entry.OutputOffset, SeekOrigin.Begin);
                writer.Write(uncompressed);
                offset = (uint)output.Position;
                if (offset % 4 != 0)
                {
                    uint padding = 4 - (offset % 4);
                    writer.Write(new byte[padding]);
                }
            }

            writer.Flush();
            output.Flush();

            return(ConversionStatus.OK);
        }
示例#29
0
        public void Save(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
                using (BEBinaryWriter bw = new BEBinaryWriter(fs))
                {
                    int    matListLength;
                    string name;

                    //output header
                    bw.WriteInt32(0x12);
                    bw.WriteInt32(0x8);
                    bw.WriteInt32(0xface);
                    bw.WriteInt32(0x2);

                    for (int i = 0; i < DatMeshes.Count; i++)
                    {
                        DatMesh dm = DatMeshes[i];
                        matListLength = 0;

                        for (int j = 0; j < dm.Mesh.Materials.Count; j++)
                        {
                            matListLength += dm.Mesh.Materials[j].Length + 1;
                        }

                        name = dm.Name;
                        //Console.WriteLine(name + " : " + dm.Mesh.Verts.Count);

                        //begin name section
                        bw.WriteInt32(54);
                        bw.WriteInt32(name.Length + 3);
                        bw.WriteByte(0);
                        bw.WriteByte(0);
                        bw.Write(name.ToCharArray());
                        bw.WriteByte(0);
                        //end name section

                        //begin vertex data
                        bw.WriteInt32(23);
                        bw.WriteInt32((dm.Mesh.Verts.Count * 12) + 4);
                        bw.WriteInt32(dm.Mesh.Verts.Count);

                        for (int j = 0; j < dm.Mesh.Verts.Count; j++)
                        {
                            bw.WriteSingle(dm.Mesh.Verts[j].X);
                            bw.WriteSingle(dm.Mesh.Verts[j].Y);
                            bw.WriteSingle(dm.Mesh.Verts[j].Z);
                        }
                        //end vertex data

                        //begin uv data (00 00 00 18)
                        bw.WriteInt32(24);
                        bw.WriteInt32((dm.Mesh.UVs.Count * 8) + 4);
                        bw.WriteInt32(dm.Mesh.UVs.Count);

                        for (int j = 0; j < dm.Mesh.UVs.Count; j++)
                        {
                            bw.WriteSingle(dm.Mesh.UVs[j].X);
                            bw.WriteSingle(dm.Mesh.UVs[j].Y);
                        }
                        //end uv data

                        //begin face data (00 00 00 35)
                        bw.WriteInt32(53);
                        bw.WriteInt32((dm.Mesh.Faces.Count * 9) + 4);
                        bw.WriteInt32(dm.Mesh.Faces.Count);

                        for (int j = 0; j < dm.Mesh.Faces.Count; j++)
                        {
                            bw.WriteInt16(dm.Mesh.Faces[j].V1);
                            bw.WriteInt16(dm.Mesh.Faces[j].V2);
                            bw.WriteInt16(dm.Mesh.Faces[j].V3);
                            bw.WriteByte(0); // smoothing groups 9 - 16
                            bw.WriteByte(1); // smoothing groups 1 - 8
                            bw.WriteByte(0); // number of edges, 0 and 3 = tri.  4 = quad.
                        }
                        //end face data

                        //begin material list
                        bw.WriteInt32(22);
                        bw.WriteInt32(matListLength + 4);
                        bw.WriteInt32(dm.Mesh.Materials.Count);

                        for (int j = 0; j < dm.Mesh.Materials.Count; j++)
                        {
                            bw.Write(dm.Mesh.Materials[j].ToCharArray());
                            bw.WriteByte(0);
                        }
                        //end material list

                        //begin face textures
                        bw.WriteInt32(26);
                        bw.WriteInt32((dm.Mesh.Faces.Count * 2) + 4);
                        bw.WriteInt32(dm.Mesh.Faces.Count);
                        bw.WriteInt32(2);

                        for (int j = 0; j < dm.Mesh.Faces.Count; j++)
                        {
                            bw.WriteInt16(dm.Mesh.Faces[j].MaterialID + 1);
                        }
                        //end face textures

                        bw.WriteInt32(0);
                        bw.WriteInt32(0);
                    }
                }
        }
示例#30
0
        // Test a wide row consisting of a ByteBuffer
        private static void TestByteRows(ISession session, string tableName)
        {
            session.Execute(String.Format("CREATE TABLE {0} (k INT, i {1}, PRIMARY KEY(k,i))", tableName, "BLOB"));

            // Build small ByteBuffer sample
            var bw = new BEBinaryWriter();
            for (int i = 0; i < 56; i++)
                bw.WriteByte(0);
            bw.WriteUInt16(0xCAFE);
            var bb = new byte[58];
            Array.Copy(bw.GetBuffer(), bb, 58);

            // Write data
            for (int i = 0; i < 1024; ++i)
                session.Execute(string.Format("INSERT INTO {0}(k,i) values({1},0x{2})", tableName, Key, CqlQueryTools.ToHex(bb)),
                                ConsistencyLevel.Quorum);

            // Read data
            var rs = session.Execute("SELECT i FROM " + tableName + " WHERE k = " + Key, ConsistencyLevel.Quorum);
            // Verify data            
            foreach (var row in rs)
                Assert.AreEqual((byte[])row["i"], bb);
        }