public void Save(Stream stream, StoryDebugInfo debugInfo)
        {
            var msg = ToProtobuf(debugInfo);

            using (var ms = new MemoryStream())
                using (var codedStream = new CodedOutputStream(ms))
                {
                    msg.WriteTo(codedStream);
                    codedStream.Flush();

                    byte[] proto      = ms.ToArray();
                    byte   flags      = BinUtils.MakeCompressionFlags(LSLib.LS.Enums.CompressionMethod.LZ4, LSLib.LS.Enums.CompressionLevel.FastCompression);
                    byte[] compressed = BinUtils.Compress(proto, flags);
                    stream.Write(compressed, 0, compressed.Length);

                    using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
                    {
                        writer.Write((UInt32)proto.Length);
                    }
                }
        }
Exemple #2
0
        public StoryDebugInfo Load(byte[] msgPayload)
        {
            UInt32 decompressedSize;

            byte[] lengthBuf = new byte[4];
            Array.Copy(msgPayload, msgPayload.Length - 4, lengthBuf, 0, 4);
            using (var ms = new MemoryStream(lengthBuf))
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true))
                {
                    decompressedSize = reader.ReadUInt32();
                }

            var compressed = new byte[msgPayload.Length - 4];

            Array.Copy(msgPayload, 0, compressed, 0, msgPayload.Length - 4);

            byte flags = BinUtils.MakeCompressionFlags(LSLib.LS.Enums.CompressionMethod.LZ4, LSLib.LS.Enums.CompressionLevel.FastCompression);

            byte[] decompressed = BinUtils.Decompress(compressed, (int)decompressedSize, flags);
            var    msg          = StoryDebugInfoMsg.Parser.ParseFrom(decompressed);
            var    debugInfo    = FromProtobuf(msg);

            return(debugInfo);
        }
Exemple #3
0
        public void Write(Resource resource)
        {
            Compression      = CompressionMethod.LZ4;
            CompressionLevel = CompressionLevel.MaxCompression;

            using (this.Writer = new BinaryWriter(Stream))
                using (this.NodeStream = new MemoryStream())
                    using (this.NodeWriter = new BinaryWriter(NodeStream))
                        using (this.AttributeStream = new MemoryStream())
                            using (this.AttributeWriter = new BinaryWriter(AttributeStream))
                                using (this.ValueStream = new MemoryStream())
                                    using (this.ValueWriter = new BinaryWriter(ValueStream))
                                    {
                                        NextNodeIndex      = 0;
                                        NextAttributeIndex = 0;
                                        NodeIndices        = new Dictionary <Node, int>();
                                        StringHashMap      = new List <List <string> >(StringHashMapSize);
                                        while (StringHashMap.Count < StringHashMapSize)
                                        {
                                            StringHashMap.Add(new List <string>());
                                        }

                                        WriteRegions(resource);

                                        byte[] stringBuffer = null;
                                        using (var stringStream = new MemoryStream())
                                            using (var stringWriter = new BinaryWriter(stringStream))
                                            {
                                                WriteStaticStrings(stringWriter);
                                                stringBuffer = stringStream.ToArray();
                                            }

                                        var nodeBuffer      = NodeStream.ToArray();
                                        var attributeBuffer = AttributeStream.ToArray();
                                        var valueBuffer     = ValueStream.ToArray();

                                        var header = new Header();
                                        header.Magic         = BitConverter.ToUInt32(Header.Signature, 0);
                                        header.Version       = Version;
                                        header.EngineVersion = (resource.Metadata.majorVersion << 24) |
                                                               (resource.Metadata.minorVersion << 16) |
                                                               (resource.Metadata.revision << 8) |
                                                               resource.Metadata.buildNumber;

                                        bool   chunked              = (header.Version >= FileVersion.VerChunkedCompress);
                                        byte[] stringsCompressed    = BinUtils.Compress(stringBuffer, Compression, CompressionLevel);
                                        byte[] nodesCompressed      = BinUtils.Compress(nodeBuffer, Compression, CompressionLevel, chunked);
                                        byte[] attributesCompressed = BinUtils.Compress(attributeBuffer, Compression, CompressionLevel, chunked);
                                        byte[] valuesCompressed     = BinUtils.Compress(valueBuffer, Compression, CompressionLevel, chunked);

                                        header.StringsUncompressedSize    = (UInt32)stringBuffer.Length;
                                        header.StringsSizeOnDisk          = (UInt32)stringsCompressed.Length;
                                        header.NodesUncompressedSize      = (UInt32)nodeBuffer.Length;
                                        header.NodesSizeOnDisk            = (UInt32)nodesCompressed.Length;
                                        header.AttributesUncompressedSize = (UInt32)attributeBuffer.Length;
                                        header.AttributesSizeOnDisk       = (UInt32)attributesCompressed.Length;
                                        header.ValuesUncompressedSize     = (UInt32)valueBuffer.Length;
                                        header.ValuesSizeOnDisk           = (UInt32)valuesCompressed.Length;
                                        header.CompressionFlags           = BinUtils.MakeCompressionFlags(Compression, CompressionLevel);
                                        header.Unknown2 = 0;
                                        header.Unknown3 = 0;
                                        header.Extended = ExtendedNodes ? 1u : 0u;
                                        BinUtils.WriteStruct <Header>(Writer, ref header);

                                        Writer.Write(stringsCompressed, 0, stringsCompressed.Length);
                                        Writer.Write(nodesCompressed, 0, nodesCompressed.Length);
                                        Writer.Write(attributesCompressed, 0, attributesCompressed.Length);
                                        Writer.Write(valuesCompressed, 0, valuesCompressed.Length);
                                    }
        }