CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ResourceManager.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Resource.hpp"
13#include "ResourceHandle.hpp"
16
23
25
26namespace CeresEngine {
27
28 class URLSession;
29
36
39
40 };
41
48 friend class PackageManager;
49
50 private:
53
56
59
63
64 private:
67
70
73 struct LoadedResource;
74
77
80
83 struct LoadingResource;
84
87
90
93 struct SavingResource;
94
97
100
103 struct ImportingResource;
104
107
110
111 public:
114
115 public:
124
127
128 public: // Resource Getting (without loading)
130 template<typename T> [[nodiscard]] ResourceHandle<T> getResource(const StringView name) { return static_resource_cast<T>(getResource(name)); }
131
134
140
146
147 public: // Resource Locating
149 template<typename T> [[nodiscard]] Async<ResourceHandle<T>> locate(const StringView name) {
150 return locate(name) | [](HResource&& resource) {
151 return static_resource_cast<T>(resource);
152 };
153 }
154
166
167 public: // Resource Loading & Saving
169 template<typename T> [[nodiscard]] Async<ResourceHandle<T>> load(const ResourceURL url, const Optional<ResourceID> resourceID = {}) {
170 return load(url, resourceID) | [](HResource&& resource) {
171 return static_resource_cast<T>(resource);
172 };
173 }
174
176 template<typename T> [[nodiscard]] Async<ResourceHandle<T>> load(const ResourceID id) {
177 return load(id) | [](HResource&& resource) {
178 return static_resource_cast<T>(resource);
179 };
180 }
181
183 template<typename T> [[nodiscard]] Async<ResourceHandle<T>> load(InputStream inputStream, const Optional<ResourceID> resourceID = {}) {
184 return load(std::move(inputStream), resourceID) | [](HResource&& resource) {
185 return static_resource_cast<T>(resource);
186 };
187 }
188
197
204
213
222
230
231 public: // Resource Creation & Replacement
244 template<typename T, typename... Args> [[nodiscard]] ResourceHandle<T> create(Args&&... args) {
245 const ResourceDataPtr resourceData = createResourceData();
246 return static_resource_cast<T>(createInternal(*resourceData, constructResource<T>(*resourceData, std::forward<Args>(args)...)));
247 }
248
252 [[nodiscard]] HResource create(const ResourcePtr& resource) { return create(ResourcePtr(resource)); }
253
256
274 template<typename T, typename... Args> [[nodiscard]] ResourceHandle<T> createWithID(const UUID& uuid, Args&&... args) {
275 if constexpr(std::is_constructible_v<T, ResourceData&, Args...>) {
276 const ResourceDataPtr resourceData = createResourceData();
277 return static_resource_cast<T>(createWithID(uuid, ce_shared_new<T>(*resourceData, std::forward<Args>(args)...)));
278 } else {
279 return static_resource_cast<T>(createWithID(uuid, ce_shared_new<T>(std::forward<Args>(args)...)));
280 }
281 }
282
288 [[nodiscard]] HResource createWithID(const UUID& uuid, const ResourcePtr& resource) { return createWithID(uuid, ResourcePtr(resource)); }
289
292
293 public: // Resource Importing
295 template<typename T> [[nodiscard]] Async<ResourceHandle<T>> import(const ResourceURL url, UPtr<ResourceImportOptions>&& importOptions = nullptr) {
296 return import(url, std::move(importOptions)) | [](HResource&& resource) {
297 return static_resource_cast<T>(resource);
298 };
299 }
300
309 [[nodiscard]] Async<HResource> import(ResourceURL url, UPtr<ResourceImportOptions>&& importOptions = nullptr);
310
311 public:
314
317
320
323
326
327 private:
329
330 private: // ResourceData interface
331 friend class ResourceData;
332
335
336 private:
337 friend class ResourceHandleData;
338
343
347 };
348
349} // namespace CeresEngine
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
A context for function object execution.
Definition ExecutionContext.hpp:90
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
A helper class that wraps and protects a value from non-synchronized access.
Definition Threading.hpp:516
Definition Optional.hpp:17
A stream that provides write-only stream functionality.
Definition Stream.hpp:307
A manager that handles packages, their loading, saving and other runtime related tasks.
Definition PackageManager.hpp:18
An object, provided by the resource manager, to view and alter data from the resource itself.
Definition Resource.hpp:89
Definition ResourceHandle.hpp:23
Definition ResourceHandle.hpp:166
A registry that keeps track of all resource importers for a ResourceManager.
Definition ResourceImporterRegistry.hpp:19
The ResourceManager is the main class responsible for managing and handling all resources in the engi...
Definition ResourceManager.hpp:47
Async< ResourceHandle< T > > load(const ResourceURL url, const Optional< ResourceID > resourceID={})
Definition ResourceManager.hpp:169
PackageManager * getPackageManager() const noexcept
The package manager associated with this resource manager.
Definition ResourceManager.hpp:325
LockedObject< SavingResourcesList > mSavingResources
A list of saving resources.
Definition ResourceManager.hpp:99
ResourceHandle< T > create(Args &&... args)
Create a new resource by constructing it with Args and registers it with the manager.
Definition ResourceManager.hpp:244
void setBaseURL(const ResourceURL &baseURL)
The base URL used to load resources from.
HResource createWithID(const UUID &uuid, ResourcePtr &&resourcePtr)
Registers a new resource with the manager.
Async save(ResourceURL url, HResource resource)
Saves a resource to a URL.
void notifyStrongHandleDestroy(ResourceHandleData *resourceHandleData)
Notifies the manager that the resource pointed by the resource handle is no longer referenced by any ...
Async< HResource > load(ResourceID id)
Loads a resource using a UUID.
ResourceHandle< T > getResource(const StringView name)
Definition ResourceManager.hpp:130
LockedObject< LoadingResourcesList > mLoadingResources
A list of loading resources.
Definition ResourceManager.hpp:89
Async< ResourceHandle< T > > load(InputStream inputStream, const Optional< ResourceID > resourceID={})
Definition ResourceManager.hpp:183
URLSession & mURLSession
The URL session to be used to load resources.
Definition ResourceManager.hpp:52
PackageManager * mPackageManager
The package manager associated with this resource manager.
Definition ResourceManager.hpp:62
LockedObject< LoadedResourceMap > mResources
A map that maps every loaded resource by it's UUID.
Definition ResourceManager.hpp:79
Async< ResourceHandle< T > > locate(const StringView name)
Definition ResourceManager.hpp:149
HResource create(const ResourcePtr &resource)
Registers a new resource with the manager.
Definition ResourceManager.hpp:252
URLSession & getURLSession() const noexcept
The URL session to be used to load resources.
Definition ResourceManager.hpp:319
List< ImportingResource > ImportingResourcesList
A list of importing resources.
Definition ResourceManager.hpp:106
HashMap< UUID, LoadedResource > LoadedResourceMap
A map that maps every loaded resource by it's UUID.
Definition ResourceManager.hpp:76
ResourceURL mBaseURL
The base URL used to load resources from.
Definition ResourceManager.hpp:69
const ResourceURL & getBaseURL() const noexcept
The base URL used to load resources from.
Definition ResourceManager.hpp:313
ResourceDataPtr createResourceData(const UUID &uuid=UUID())
Creates a new ResourceData object for a resource.
HResource createInternal(ResourceData &resourceData, ResourcePtr &&resource)
HResource getResource(const UUID &uuid)
Locates an existing resource by it's UUID.
LockedObject< ImportingResourcesList > mImportingResources
A list of importing resources.
Definition ResourceManager.hpp:109
HResource createWithID(const UUID &uuid, const ResourcePtr &resource)
Registers a new resource with the manager using a known UUID.
Definition ResourceManager.hpp:288
Async< HResource > load(ResourceURL url, Optional< ResourceID > resourceID={})
Loads a resource using a URL.
ResourceHandle< T > getResource(const UUID &uuid)
Definition ResourceManager.hpp:133
Async save(OutputStream outputStream, HResource resource)
Saves a resource to an output stream.
ResourceHandle< T > createWithID(const UUID &uuid, Args &&... args)
Create a new resource by constructing it with Args and registers it with the manager.
Definition ResourceManager.hpp:274
Async< ResourceHandle< T > > load(const ResourceID id)
Definition ResourceManager.hpp:176
HResource getResource(StringView name)
Locates an existing resource by it's name.
List< LoadingResource > LoadingResourcesList
A list of loading resources.
Definition ResourceManager.hpp:86
Async< HResource > load(InputStream inputStream, Optional< ResourceID > resourceID={})
Loads a resource using an existing input stream.
ResourceImporterRegistry importers
The registry that keeps track of registered importers.
Definition ResourceManager.hpp:113
void notifyHandleDestroy(const ResourceHandleData *resourceHandleData)
Notifies the manager that the last resource handle was destroyed.
ExecutionContext & getExecutionContext() const noexcept
The execution context to perform asynchronous resource operations on.
Definition ResourceManager.hpp:322
const SerializationContext & mSerializationContext
A context for the serializer and deserializer to operate on.
Definition ResourceManager.hpp:58
List< SavingResource > SavingResourcesList
A list of saving resources.
Definition ResourceManager.hpp:96
Async< HResource > locate(StringView name)
Loads a resource by it's name.
ResourceManager(URLSession &urlSession, ExecutionContext &executionContext, const SerializationContext &serializationContext=SerializationContext::getDefault())
Creates a new resource manager instance.
~ResourceManager()
Destroys the resource manager and all it's managed resources.
LockedObject< HashMap< UUID, ResourceHandleData * > > mResourceHandles
A map of active resource handles, mapped by their UUID.
Definition ResourceManager.hpp:66
HResource create(ResourcePtr &&resourcePtr)
Registers a new resource with the manager.
ExecutionContext & mExecutionContext
The execution context to perform asynchronous resource operations on.
Definition ResourceManager.hpp:55
A context that is shared between multiple serializer and deserializer instances.
Definition Serialization.hpp:196
static const SerializationContext & getDefault()
Gets the default serialization context to be used in case the user doesn't provide one himself.
A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or p...
Definition URI.hpp:54
An object that coordinates a group of related, network data transfer tasks.
Definition URLSession.hpp:399
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
std::list< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > List
List is a container that supports constant time insertion and removal of elements from anywhere in th...
Definition List.hpp:21
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
SPtr< Resource > ResourcePtr
Definition Forward.hpp:47
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Type that uniquely represents a resource in the resource system.
Definition Resource.hpp:37
A structure that describes options for the loading of a resource.
Definition ResourceManager.hpp:31
Optional< ResourceID > resourceID
A ID to be used as a hint for the loader.
Definition ResourceManager.hpp:34
A structure that describes options for the saving of a resource.
Definition ResourceManager.hpp:38
Represents a universally unique identifier (UUID).
Definition UUID.hpp:27