1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#include "slang-riff-file-system.h"
#include "slang-com-helper.h"
#include "slang-com-ptr.h"
#include "slang-blob.h"
// Compression systems
#include "slang-deflate-compression-system.h"
#include "slang-lz4-compression-system.h"
namespace Slang
{
RiffFileSystem::RiffFileSystem(ICompressionSystem* compressionSystem):
m_compressionSystem(compressionSystem)
{
}
void* RiffFileSystem::getInterface(const Guid& guid)
{
if (auto ptr = Super::getInterface(guid))
{
return ptr;
}
else if (guid == IArchiveFileSystem::getTypeGuid())
{
return static_cast<IArchiveFileSystem*>(this);
}
return nullptr;
}
void* RiffFileSystem::getObject(const Guid& guid)
{
SLANG_UNUSED(guid);
return nullptr;
}
void* RiffFileSystem::castAs(const Guid& guid)
{
if (auto ptr = getInterface(guid))
{
return ptr;
}
return getObject(guid);
}
SlangResult RiffFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
{
Entry* entry;
SLANG_RETURN_ON_FAIL(_loadFile(path, &entry));
ISlangBlob* contents = entry->m_contents;
if (m_compressionSystem)
{
// Okay lets decompress into a blob
ScopedAllocation alloc;
void* dst = alloc.allocateTerminated(entry->m_uncompressedSizeInBytes);
SLANG_RETURN_ON_FAIL(m_compressionSystem->decompress(contents->getBufferPointer(), contents->getBufferSize(), entry->m_uncompressedSizeInBytes, dst));
auto blob = RawBlob::moveCreate(alloc);
*outBlob = blob.detach();
return SLANG_OK;
}
else
{
// Just return as is
contents->addRef();
*outBlob = contents;
return SLANG_OK;
}
}
SlangResult RiffFileSystem::saveFile(const char* path, const void* data, size_t size)
{
Entry* entry;
SLANG_RETURN_ON_FAIL(_requireFile(path, &entry));
ComPtr<ISlangBlob> contents;
if (m_compressionSystem)
{
// Lets try compressing the input
SLANG_RETURN_ON_FAIL(m_compressionSystem->compress(&m_compressionStyle, data, size, contents.writeRef()));
}
else
{
// Just store the data directly.
contents = RawBlob::create(data, size);
}
entry->setContents(size, contents);
return SLANG_OK;
}
SlangResult RiffFileSystem::saveFileBlob(const char* path, ISlangBlob* dataBlob)
{
if (!dataBlob)
{
return SLANG_E_INVALID_ARG;
}
if (m_compressionSystem)
{
return saveFile(path, dataBlob->getBufferPointer(), dataBlob->getBufferSize());
}
else
{
return Super::saveFileBlob(path, dataBlob);
}
}
SlangResult RiffFileSystem::loadArchive(const void* archive, size_t archiveSizeInBytes)
{
// Load the riff
RiffContainer container;
MemoryStreamBase stream(FileAccess::Read, archive, archiveSizeInBytes);
SLANG_RETURN_ON_FAIL(RiffUtil::read(&stream, container));
RiffContainer::ListChunk* rootList = container.getRoot();
// Make sure it's the right type
if (rootList == nullptr || rootList->m_fourCC != RiffFileSystemBinary::kContainerFourCC)
{
return SLANG_FAIL;
}
// Clear the contents
_clear();
// Find the header
const auto header = rootList->findContainedData<RiffFileSystemBinary::Header>(RiffFileSystemBinary::kHeaderFourCC);
CompressionSystemType compressionType = CompressionSystemType(header->compressionSystemType);
switch (compressionType)
{
case CompressionSystemType::None:
{
// Null m_compressionSystem means no compression
m_compressionSystem.setNull();
break;
}
case CompressionSystemType::Deflate:
{
m_compressionSystem = DeflateCompressionSystem::getSingleton();
break;
}
case CompressionSystemType::LZ4:
{
m_compressionSystem = LZ4CompressionSystem::getSingleton();
break;
}
default: return SLANG_FAIL;
}
// Read all of the contained data
{
List<RiffContainer::DataChunk*> srcEntries;
rootList->findContained(RiffFileSystemBinary::kEntryFourCC, srcEntries);
for (auto chunk : srcEntries)
{
auto data = chunk->getSingleData();
const uint8_t* srcData = (const uint8_t*)data->getPayload();
const size_t dataSize = data->getSize();
if (dataSize < sizeof(RiffFileSystemBinary::Entry))
{
return SLANG_FAIL;
}
auto srcEntry = (const RiffFileSystemBinary::Entry*)srcData;
srcData += sizeof(*srcEntry);
// Check if seems plausible
if (sizeof(RiffFileSystemBinary::Entry) + srcEntry->compressedSize + srcEntry->pathSize != dataSize)
{
return SLANG_FAIL;
}
Entry dstEntry;
const char* path = (const char*)srcData;
srcData += srcEntry->pathSize;
dstEntry.m_canonicalPath = UnownedStringSlice(path, srcEntry->pathSize - 1);
dstEntry.m_type = (SlangPathType)srcEntry->pathType;
dstEntry.m_uncompressedSizeInBytes = srcEntry->uncompressedSize;
switch (dstEntry.m_type)
{
case SLANG_PATH_TYPE_FILE:
{
if (srcData + srcEntry->compressedSize != data->getPayloadEnd())
{
return SLANG_FAIL;
}
// Get the compressed data
dstEntry.m_contents = RawBlob::create(srcData, srcEntry->compressedSize);
break;
}
case SLANG_PATH_TYPE_DIRECTORY: break;
default: return SLANG_FAIL;
}
// If it's the root entry we can ignore (as already added)
if (dstEntry.m_canonicalPath == ".")
{
continue;
}
// Add to the list of entries
m_entries.add(dstEntry.m_canonicalPath, dstEntry);
}
}
return SLANG_OK;
}
SlangResult RiffFileSystem::storeArchive(bool blobOwnsContent, ISlangBlob** outBlob)
{
// All blobs are owned in this style
SLANG_UNUSED(blobOwnsContent)
RiffContainer container;
RiffContainer::ScopeChunk scopeContainer(&container, RiffContainer::Chunk::Kind::List, RiffFileSystemBinary::kContainerFourCC);
{
RiffFileSystemBinary::Header header;
CompressionSystemType compressionSystemType = m_compressionSystem ? m_compressionSystem->getSystemType() : CompressionSystemType::None;
header.compressionSystemType = uint32_t(compressionSystemType);
container.addDataChunk(RiffFileSystemBinary::kHeaderFourCC, &header, sizeof(header));
}
for (const auto& [_, srcEntry] : m_entries)
{
// Ignore the root entry
if (srcEntry.m_canonicalPath == toSlice("."))
{
continue;
}
RiffContainer::ScopeChunk scopeData(&container, RiffContainer::Chunk::Kind::Data, RiffFileSystemBinary::kEntryFourCC);
RiffFileSystemBinary::Entry dstEntry;
dstEntry.uncompressedSize = 0;
dstEntry.compressedSize = 0;
dstEntry.pathSize = uint32_t(srcEntry.m_canonicalPath.getLength() + 1);
dstEntry.pathType = srcEntry.m_type;
ISlangBlob* blob = srcEntry.m_contents;
if (srcEntry.m_type == SLANG_PATH_TYPE_FILE)
{
dstEntry.compressedSize = uint32_t(blob->getBufferSize());
dstEntry.uncompressedSize = uint32_t(srcEntry.m_uncompressedSizeInBytes);
}
// Entry header
container.write(&dstEntry, sizeof(dstEntry));
// Path
container.write(srcEntry.m_canonicalPath.getBuffer(), srcEntry.m_canonicalPath.getLength() + 1);
// Add the contained data without copying
if (blob)
{
RiffContainer::Data* data = container.addData();
container.setUnowned(data, const_cast<void*>(blob->getBufferPointer()), blob->getBufferSize());
}
}
OwnedMemoryStream stream(FileAccess::Write);
// We now write the RiffContainer to the stream
SLANG_RETURN_ON_FAIL(RiffUtil::write(container.getRoot(), true, &stream));
List<uint8_t> data;
stream.swapContents(data);
*outBlob = ListBlob::moveCreate(data).detach();
return SLANG_OK;
}
/* static */bool RiffFileSystem::isArchive(const void* data, size_t sizeInBytes)
{
MemoryStreamBase stream(FileAccess::Read, data, sizeInBytes);
RiffListHeader header;
return SLANG_SUCCEEDED(RiffUtil::readHeader(&stream, header)) && header.subType == RiffFileSystemBinary::kContainerFourCC;
}
} // namespace Slang
|