// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Types/DataContainer.h"
#include "Engine/Core/Types/DateTime.h"
#include "../Config.h"
///
/// Custom flags for the storage chunk data.
///
enum class FlaxChunkFlags
{
///
/// The none.
///
None = 0,
///
/// Compress chunk data using LZ4 algorithm.
///
CompressedLZ4 = 1,
};
DECLARE_ENUM_OPERATORS(FlaxChunkFlags);
///
/// Represents chunks of data used by the content storage layer
///
class FLAXENGINE_API FlaxChunk
{
public:
///
/// Chunk of data location info
///
struct Location
{
///
/// Address of the chunk beginning in file
///
uint32 Address;
///
/// Chunk size (in bytes)
/// Note: chunk which size equals 0 is considered as not existing
///
uint32 Size;
///
/// Init
///
Location()
: Address(0)
, Size(0)
{
}
///
/// Init
///
/// The location.
/// The size.
Location(uint32 location, uint32 size)
: Address(location)
, Size(size)
{
}
};
public:
///
/// The chunk location in file.
///
Location LocationInFile;
///
/// The chunk flags.
///
FlaxChunkFlags Flags;
///
/// The last usage time (UTC).
///
DateTime LastAccessTime;
///
/// The chunk data.
///
BytesContainer Data;
public:
///
/// Initializes a new instance of the class.
///
FlaxChunk()
: Flags(FlaxChunkFlags::None)
, LastAccessTime(0)
{
}
///
/// Initializes a new instance of the class.
///
/// The chunk location in the file.
FlaxChunk(const Location& location)
: LocationInFile(location)
, Flags(FlaxChunkFlags::None)
, LastAccessTime(0)
{
}
///
/// Initializes a new instance of the class.
///
/// The chunk location in the file.
/// The flags.
FlaxChunk(const Location& location, const FlaxChunkFlags flags)
: LocationInFile(location)
, Flags(flags)
, LastAccessTime(0)
{
}
///
/// Initializes a new instance of the class.
///
/// The address in the file.
/// The size in bytes.
FlaxChunk(uint32 address, uint32 size)
: LocationInFile(address, size)
, Flags(FlaxChunkFlags::None)
, LastAccessTime(0)
{
}
public:
///
/// Finalizes an instance of the class.
///
~FlaxChunk()
{
}
public:
///
/// Gets this chunk data pointer.
///
/// Data
FORCE_INLINE byte* Get()
{
return Data.Get();
}
///
/// Gets this chunk data pointer.
///
/// Data
FORCE_INLINE const byte* Get() const
{
return Data.Get();
}
///
/// Gets this chunk data pointer.
///
/// Data
template
FORCE_INLINE T* Get() const
{
return (T*)Data.Get();
}
///
/// Gets this chunk data size (in bytes).
///
/// Data size
FORCE_INLINE int32 Size() const
{
return Data.Length();
}
public:
///
/// Determines whether this chunk is loaded.
///
/// True if this instance is loaded, otherwise false.
FORCE_INLINE bool IsLoaded() const
{
return Data.IsValid();
}
///
/// Determines whether this chunk is missing (no data loaded or assigned).
///
/// True if this instance is missing, otherwise false.
FORCE_INLINE bool IsMissing() const
{
return Data.IsInvalid();
}
///
/// Determines whether this chunk exists in a file.
///
/// True if this instance is in a file, otherwise false.
FORCE_INLINE bool ExistsInFile() const
{
return LocationInFile.Size > 0;
}
///
/// Registers the usage operation of chunk data.
///
void RegisterUsage()
{
LastAccessTime = DateTime::NowUTC();
}
///
/// Unloads this chunk data.
///
void Unload()
{
Data.Release();
}
///
/// Clones this chunk data (doesn't copy location in file).
///
/// The cloned chunk.
FlaxChunk* Clone() const
{
auto chunk = New();
chunk->Data.Copy(Data);
return chunk;
}
};