Plasma Engine  2.0
Loading...
Searching...
No Matches
Archive.h
1#pragma once
2
3#include <Foundation/Containers/DynamicArray.h>
4#include <Foundation/Containers/HashTable.h>
5#include <Foundation/Strings/HashedString.h>
6
8
10enum class plArchiveCompressionMode : plUInt8
11{
12 Uncompressed,
13 Compressed_zstd,
14 Compressed_zip,
15};
16
18class PL_FOUNDATION_DLL plArchiveEntry
19{
20public:
21 plUInt64 m_uiDataStartOffset = 0;
22 plUInt64 m_uiUncompressedDataSize = 0;
23 plUInt64 m_uiStoredDataSize = 0;
24 plUInt32 m_uiPathStringOffset = 0;
25 plArchiveCompressionMode m_CompressionMode = plArchiveCompressionMode::Uncompressed;
26
27 plResult Serialize(plStreamWriter& inout_stream) const;
28 plResult Deserialize(plStreamReader& inout_stream);
29};
30
37class PL_FOUNDATION_DLL plArchiveStoredString
38{
39public:
40 PL_DECLARE_POD_TYPE();
41
42 plArchiveStoredString() = default;
43
44 plArchiveStoredString(plUInt64 uiLowerCaseHash, plUInt32 uiSrcStringOffset)
45 : m_uiLowerCaseHash(plHashingUtils::StringHashTo32(uiLowerCaseHash))
46 , m_uiSrcStringOffset(uiSrcStringOffset)
47 {
48 }
49
50 plUInt32 m_uiLowerCaseHash;
51 plUInt32 m_uiSrcStringOffset;
52};
53
54void operator<<(plStreamWriter& inout_stream, const plArchiveStoredString& value);
55void operator>>(plStreamReader& inout_stream, plArchiveStoredString& value);
56
61{
62 PL_DISALLOW_COPY_AND_ASSIGN(plArchiveLookupString);
63
64public:
65 PL_DECLARE_POD_TYPE();
66
67 plArchiveLookupString(plUInt64 uiLowerCaseHash, plStringView sString, const plDynamicArray<plUInt8>& archiveAllPathStrings)
68 : m_uiLowerCaseHash(plHashingUtils::StringHashTo32(uiLowerCaseHash))
69 , m_sString(sString)
70 , m_ArchiveAllPathStrings(archiveAllPathStrings)
71 {
72 }
73
74 plUInt32 m_uiLowerCaseHash;
75 plStringView m_sString;
76 const plDynamicArray<plUInt8>& m_ArchiveAllPathStrings;
77};
78
80template <>
82{
83 PL_ALWAYS_INLINE static plUInt32 Hash(const plArchiveStoredString& hs) { return hs.m_uiLowerCaseHash; }
84 PL_ALWAYS_INLINE static plUInt32 Hash(const plArchiveLookupString& hs) { return hs.m_uiLowerCaseHash; }
85
86 PL_ALWAYS_INLINE static bool Equal(const plArchiveStoredString& a, const plArchiveStoredString& b) { return a.m_uiSrcStringOffset == b.m_uiSrcStringOffset; }
87
88 PL_ALWAYS_INLINE static bool Equal(const plArchiveStoredString& a, const plArchiveLookupString& b)
89 {
90 // in case that we want to lookup a string using a plArchiveLookupString, we validate
91 // that the stored string is actually equal to the lookup string, to enable handling of hash collisions
92 return b.m_sString.IsEqual_NoCase(reinterpret_cast<const char*>(&b.m_ArchiveAllPathStrings[a.m_uiSrcStringOffset]));
93 }
94};
95
97class PL_FOUNDATION_DLL plArchiveTOC
98{
99public:
106
108 plUInt32 FindEntry(plStringView sFile) const;
109
110 plUInt32 AddPathString(plStringView sPathString);
111
112 void RebuildPathToEntryHashes();
113
114 plStringView GetEntryPathString(plUInt32 uiEntryIdx) const;
115
116 plResult Serialize(plStreamWriter& inout_stream) const;
117 plResult Deserialize(plStreamReader& inout_stream, plUInt8 uiArchiveVersion);
118};
Data for a single file entry in an plArchive file.
Definition Archive.h:19
Helper class for looking up path strings in plArchiveTOC::FindEntry()
Definition Archive.h:61
Helper class to store a hashed string for quick lookup in the archive TOC.
Definition Archive.h:38
Table-of-contents for an plArchive file.
Definition Archive.h:98
plHashTable< plArchiveStoredString, plUInt32 > m_PathToEntryIndex
allows to map a hashed string to the index of the file entry for the file path
Definition Archive.h:103
plDynamicArray< plArchiveEntry > m_Entries
all files stored in the plArchive
Definition Archive.h:101
plDynamicArray< plUInt8 > m_AllPathStrings
one large array holding all path strings for the file entries, to reduce allocations
Definition Archive.h:105
Definition DynamicArray.h:81
Definition HashTable.h:333
static constexpr plUInt32 StringHashTo32(plUInt64 uiHash)
Truncates a 64 bit string hash to 32 bit.
Definition HashingUtils_inl.h:138
Maps a raw chunk of memory to the plStreamReader interface.
Definition MemoryStream.h:358
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
bool IsEqual_NoCase(plStringView sOther) const
Compares this string view with the other string view for equality.
Definition StringView_inl.h:95
Helper struct to calculate the Hash of different types.
Definition HashingUtils.h:75
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54