summaryrefslogtreecommitdiffstats
path: root/source/core/slang-archive-file-system.cpp
blob: e219fcbb55efe2bdcaf0f0df11503fe0c2a0704c (plain)
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
#include "slang-archive-file-system.h"

#include "slang-com-helper.h"
#include "slang-com-ptr.h"

#include "../core/slang-castable.h"

#include "slang-io.h"
#include "slang-string-util.h"
#include "slang-blob.h"

#include "slang-riff-file-system.h"

// Compression systems
#include "slang-deflate-compression-system.h"
#include "slang-lz4-compression-system.h"

// Zip file system
#include "slang-zip-file-system.h"

#include "slang-riff.h"

namespace Slang
{

SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, ComPtr<ISlangFileSystemExt>& outFileSystem)
{
    ComPtr<ISlangMutableFileSystem> fileSystem;
    if (ZipFileSystem::isArchive(data, dataSizeInBytes))
    {
        // It's a zip
        SLANG_RETURN_ON_FAIL(ZipFileSystem::create(fileSystem));
    }
    else if (RiffFileSystem::isArchive(data, dataSizeInBytes))
    {
        // It's riff contained (Slang specific)
       fileSystem = new RiffFileSystem(nullptr);
    }
    else
    {
        return SLANG_FAIL;
    }

    auto archiveFileSystem = as<IArchiveFileSystem>(fileSystem);
    if (!archiveFileSystem)
    {
        return SLANG_FAIL;
    }

    SLANG_RETURN_ON_FAIL(archiveFileSystem->loadArchive(data, dataSizeInBytes));

    outFileSystem = fileSystem;
    return SLANG_OK;
}
    
SlangResult createArchiveFileSystem(SlangArchiveType type, ComPtr<ISlangMutableFileSystem>& outFileSystem)
{
    switch (type)
    {
        case SLANG_ARCHIVE_TYPE_ZIP:
        {
            return ZipFileSystem::create(outFileSystem);
        }
        case SLANG_ARCHIVE_TYPE_RIFF:
        {
            outFileSystem = new RiffFileSystem(nullptr);
            return SLANG_OK;
        }
        case SLANG_ARCHIVE_TYPE_RIFF_DEFLATE:
        {
            outFileSystem = new RiffFileSystem(DeflateCompressionSystem::getSingleton());
            return SLANG_OK;
        }
        case SLANG_ARCHIVE_TYPE_RIFF_LZ4:
        {
            outFileSystem = new RiffFileSystem(LZ4CompressionSystem::getSingleton());
            return SLANG_OK;
        }
    }

    return SLANG_FAIL;
}

} // namespace Slang