summaryrefslogtreecommitdiff
path: root/src/assets.h
blob: 015564e4fbfcc66e7b8b71a7106e7de067b0d226 (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
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
#ifndef _ASSET_H
#define _ASSET_H

#include <string>
#include <vector>

#include "util.h"

namespace Mauri
{

enum AssetType
{
    ASSET_VOID = 0,
    ASSET_SLICE,
    ASSET_FILE
};

enum AssetTypeHint
{
    NONE = 0,
    TEXTURE,
    SHADER,
    FRAGMENT_SHADER,
    VERTEX_SHADER
};

class Asset
{
  public:
    Asset();
    Asset(const std::string &path, byte *begin, byte *end);
    Asset(const std::string &path);
    ~Asset();

    AssetType type;

    std::string path;

    AssetTypeHint ltype = NONE;
    void *lasset = nullptr;

    u32 size;
    u64 hash;

    auto ptr() const -> byte *
    {
        return &this->data[this->pos];
    }

    auto seek(s32 n) -> void
    {
        this->pos += n;
    }

    auto reset() -> void
    {
        this->pos = 0;
    }

    template <typename T>
    auto read() -> T
    {
        T val = *((T *)this->ptr());
        this->seek(sizeof(T));
        return val;
    }

    auto reads(u32 n) -> std::string_view
    {
        std::string_view s((char *)this->ptr(), n);
        this->seek(n);
        return s;
    }

    auto as_string() -> const std::string_view &
    {
        if (str.empty())
        {
            str = std::string_view((char *)this->ptr(), this->size);
        }
        return str;
    }

  private:
    u32 pos = 0;
    byte *data;
    std::string_view str;
};

struct PackageFile
{
    std::string_view path;
    u32 offset;
    u32 length;
};

class AssetManager
{
  public:
    AssetManager() = default;
    ~AssetManager() = default;

    bool prepare_output;

    auto arm_package(const std::string &path, bool can_output) -> bool;
    auto add_search_directory(const std::string &directory) -> bool;
    auto get(const std::string &part, AssetTypeHint hint = NONE) -> Asset *;
    auto dump(const std::string &output_path) -> void;
    auto unload_package() -> void;

  private:
    std::vector<std::string> directories;

    Asset *package;
    std::vector<Asset *> assets;
};

extern auto asset_manager() -> AssetManager *;

} // namespace Mauri

#endif // _ASSET_H