// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using EpicGames.Core;
#pragma warning disable CA1716 // Do not use 'Imports' as identifier
namespace EpicGames.Horde.Storage
{
///
/// Interface for reading nodes from storage
///
public interface IBlobReader : IMemoryReader
{
///
/// Type to deserialize
///
BlobType Type { get; }
///
/// Version of the current node, as specified via
///
int Version { get; }
///
/// Locations of all referenced nodes. These handles do not have valid hashes.
///
IReadOnlyList Imports { get; }
///
/// Gets the next serialized blob handle
///
IHashedBlobRef ReadBlobRef();
///
/// Gets the next serialized blob handle
///
IHashedBlobRef ReadBlobRef();
}
///
/// Reader for blob objects
///
public sealed class BlobReader : MemoryReader, IBlobReader
{
///
/// Type to deserialize
///
public BlobType Type => _blobData.Type;
///
/// Version of the current node, as specified via
///
public int Version => Type.Version;
///
/// Total length of the data in this node
///
public int Length => _blobData.Data.Length;
///
/// Amount of data remaining to be read
///
public int RemainingLength => RemainingMemory.Length;
///
/// Raw data for this blob
///
public ReadOnlyMemory Data => _blobData.Data;
///
/// Locations of all referenced nodes.
///
public IReadOnlyList Imports => _blobData.Imports;
readonly BlobData _blobData;
readonly BlobSerializerOptions _options;
int _importIdx;
///
/// Constructor
///
public BlobReader(BlobData blobData, BlobSerializerOptions? options)
: base(blobData.Data)
{
_blobData = blobData;
_options = options ?? BlobSerializerOptions.Default;
}
///
/// Gets the next serialized blob reference
///
public IHashedBlobRef ReadBlobRef()
{
IBlobRef import = Imports[_importIdx++];
IoHash hash = this.ReadIoHash();
return HashedBlobRef.Create(hash, import);
}
///
/// Gets the next serialized blob reference
///
public IHashedBlobRef ReadBlobRef()
{
IBlobRef import = Imports[_importIdx++];
IoHash hash = this.ReadIoHash();
return HashedBlobRef.Create(hash, import, _options);
}
}
}