Exemplo n.º 1
0
        internal void Serialize(StreamAccessor accessor)
        {
            var bufferBlob     = new Blob();
            var bufferAccessor = new BlobAccessor(bufferBlob);

            accessor.Write7BitEncodedInt(_count);

            var entries = new Entry[_count];

            for (int i = 0; i < _count; i++)
            {
                int offset = (int)bufferAccessor.Position;

                var item = _items[i];
                if (item != null && item.IsChanged)
                {
                    item.Write(bufferAccessor);
                }
                else if (_entries != null && _entries.Length > i)
                {
                    int existingSize = _entries[i].Size;
                    if (existingSize > 0)
                    {
                        bufferAccessor.Write(_buffer, _entries[i].Offset, existingSize);
                    }
                }

                int size = (int)bufferAccessor.Position - offset;

                accessor.Write7BitEncodedInt(offset);
                accessor.Write7BitEncodedInt(size);

                entries[i] =
                    new Entry()
                {
                    Offset = offset,
                    Size   = size,
                };
            }

            _entries = entries;
            _buffer  = bufferBlob.ToArray();
            Array.Clear(_items, 0, _count);

            accessor.Write7BitEncodedInt(_buffer.Length);

            if (_buffer.Length > 0)
            {
                accessor.Write(_buffer, 0, _buffer.Length);
            }
        }
        internal void Save()
        {
            if (!_isChanged && !_isNew)
            {
                return;
            }

            if (_isNew)
            {
                // Set paths
                string fileName = _module.NameChanged ? _module.NewName : _module.Name;

                var    assembly   = (BuildAssembly)_module.Assembly;
                string outputPath = assembly.OutputPath;
                if (outputPath == null)
                {
                    outputPath = Path.GetDirectoryName(assembly.Location);
                }

                _outputFilePath = Path.Combine(outputPath, fileName);
                _stateFilePath  = _outputFilePath + ".adstate";
                _buildFilePath  = _outputFilePath + ".adbuild";
            }

            using (var accessor = new StreamAccessor(new FileStream(_stateFilePath, FileMode.Create, FileAccess.Write, FileShare.None)))
            {
                accessor.Write7BitEncodedInt(_bufferLength);
                accessor.Write(_buffer, 0, _bufferLength);

                _objects.Serialize(accessor);

                _blobs.Serialize(accessor);

                var signatureBlob = new Blob();
                _signatures.Serialize(new BlobAccessor(signatureBlob));

                var stringBlob = new Blob();
                _strings.Serialize(new BlobAccessor(stringBlob));
                StrongCryptoUtils.Encrypt(stringBlob.GetBuffer(), 0, stringBlob.Length);
                accessor.Write7BitEncodedInt(stringBlob.Length);
                accessor.Write(stringBlob.GetBuffer(), 0, stringBlob.Length);

                accessor.Write(signatureBlob.GetBuffer(), 0, signatureBlob.Length);
            }

            _isNew     = false;
            _isChanged = false;
        }