Cogs.Core
ResourceStore.h
1#pragma once
2
3#include "Base.h"
4#include "ResourceArchive.h"
5
6#include "Foundation/Memory/MemoryBuffer.h"
7#include "Foundation/Platform/Threads.h"
8#include "Foundation/StringView.h"
9
10#include <string_view>
11#include <span>
12
13namespace Cogs
14{
15 struct IIOHandler;
16
17 namespace Core
18 {
19 class Context;
20
22 {
24 [[nodiscard]]
25 uint8_t * data() { return static_cast<uint8_t *>(buffer->data()); }
26
28 [[nodiscard]]
29 const uint8_t * data() const { return static_cast<uint8_t *>(buffer->data()); }
30
31 [[nodiscard]]
32 size_t size() const { return buffer ? buffer->size() : 0; }
33
34 [[nodiscard]]
35 bool empty() const { return !buffer || size() == 0; }
36
38 [[nodiscard]]
39 std::string_view toStringView() const {
40 return empty() ? std::string_view() : std::string_view(static_cast<const char*>(buffer->data()), size());
41 }
42
48 template<class T>
49 [[nodiscard]]
50 std::span<const T> toSpan(size_t offset = 0) const {
51 if (offset >= size()) {
52 return std::span<const T>();
53 }
54 else {
55 const void* dataStart = static_cast<const uint8_t*>(buffer->data()) + offset;
56 return std::span<const T>(static_cast<const T*>(dataStart), (size() - offset) / sizeof(T));
57 }
58 }
59
61 std::shared_ptr<Memory::MemoryBuffer> buffer;
62 };
63
65 {
66 None = 0,
67 NoCachedContent = 1 << 0,
68 QueryOnly = 1 << 1,
69 PreferUncachedContent = 1 << 2
70 };
71
72 enum class ResourceProtocol
73 {
74 None = 0,
75 File = 1,
76 Default = File,
77 Cache = 2,
78 Archive = 3,
79 Http = 4,
80 Other = 5,
81 };
82
84 {
85 std::string path;
86 ResourceProtocol protocol = ResourceProtocol::None;
87 };
88
89 ENABLE_ENUM_FLAGS(ResourceStoreFlags);
90
106 class COGSCORE_DLL_API ResourceStore
107 {
108 friend void resourceStoreInspector(Context * context, bool * show);
109 public:
110 explicit ResourceStore(class Context * context);
112
116 void preloadResources(const std::vector<std::string> & resourceNames);
117
121 [[nodiscard]]
122 bool hasResources(const std::vector<std::string> & resourceNames);
123
127 [[nodiscard]]
128 bool hasResource(const std::string & resourceName);
129
133 void addResource(std::string_view resourceName, const std::string_view content);
134
138 void addResource(std::string_view resourceName, const void* content_data, size_t content_size);
139
143 void addResourceArchive(const std::string & archiveName, bool prepend = false);
144
145 /*
146 Get the actual path of a resource.
147 */
148 [[nodiscard]]
149 std::string getResourcePath(std::string_view resourceName, ResourceStoreFlags flags = ResourceStoreFlags::None) const;
150
154 [[nodiscard]]
155 ResourceQueryResult getResourceLocation(std::string_view resourceName, ResourceStoreFlags flags = ResourceStoreFlags::None) const;
156
160 [[nodiscard]]
161 ResourceBuffer getResourceContents(std::string_view resourceName, ResourceStoreFlags flags = ResourceStoreFlags::None) const;
162
167 [[nodiscard]]
168 std::string getResourceContentString(std::string_view resourceName, ResourceStoreFlags flags = ResourceStoreFlags::None) const;
169
174 void addSearchPath(const std::string & path, bool prepend = false);
175
179 [[nodiscard]]
180 const std::string& getDataDir() const;
181
185 void purge(const std::string & resourceName);
186
190 void clear();
191
193 [[nodiscard]]
194 IIOHandler * getIOHandler();
195
196 private:
197 std::unique_ptr<struct ResourceStoreStorage> data;
198 };
199 }
200}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Provides handling of reading and caching of external resources.
@ PreferUncachedContent
Try fetching data before resorting to cached data.
@ NoCachedContent
Never use cached data.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
std::shared_ptr< Memory::MemoryBuffer > buffer
Actual resource contents. Prefer using access methods.
Definition: ResourceStore.h:61
std::string_view toStringView() const
Return contents as a std::string_view.
Definition: ResourceStore.h:39
std::span< const T > toSpan(size_t offset=0) const
Definition: ResourceStore.h:50
const uint8_t * data() const
Returns pointer to start of buffer data. Requires non-empty Contents.
Definition: ResourceStore.h:29
uint8_t * data()
Returns pointer to start of buffer data. Requires non-empty Contents.
Definition: ResourceStore.h:25
I/O handler.
Definition: IEffects.h:106