载入中...
搜索中...
未找到
Resource.h
浏览该文件的文档.
1#pragma once
4#include "common/Export.h"
5#include "common/Result.h"
6// Cached resources are owned by a RuntimeObjectRegistry and handed out as borrows and
7// keep-alive pins, so this header needs those definitions. RuntimeRegistry.h is
8// deliberately Squirrel-free: the cache's consumers must not be forced to include
9// <squirrel.h> just to name a resource handle.
11
12#include <cstddef>
13#include <condition_variable>
14#include <map>
15#include <memory>
16#include <mutex>
17#include <span>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22namespace eve {
23
24class Resource;
25
27struct ResourceCacheTag;
28
31
51class Resource {
52public:
54 virtual ~Resource() {}
55
69 Resource(const Resource&) = delete;
70 Resource& operator=(const Resource&) = delete;
71
73 std::string getUri() const { return uri; }
74
85 [[nodiscard]] EVENGINE_API_FOUNDATION std::vector<Resource*> getDependencies() const;
86
98
110 virtual void adopt(eve::Resource& replacement) = 0;
111
112protected:
114 Resource(std::string uri) : uri(uri) {}
115
117 void setUri(std::string value) { uri = std::move(value); }
118
119 friend class ResourceManager;
120
121 std::string uri;
123 std::vector<ResourcePin> dependencies;
124};
125
126
153public:
155 static ResourceManager& getInstance();
156
158 static std::string normalizePath(std::string path);
159
161 static std::string makeKey(const std::string& path, const std::string& query = "");
162
164 static std::string pathOfKey(const std::string& key);
165
184 [[nodiscard("resource lookup ownership must be retained or explicitly handled")]] Resource* get(std::string key);
185
198 [[nodiscard("async resource request outcome must be checked")]] eve::Result<void> request(
199 std::string key);
200
205 [[nodiscard("async resource request outcome must be checked")]] eve::Result<void> requestAll(
206 std::span<const std::string> keys);
207
216 [[nodiscard]] OptionalRef<Resource> peek(const std::string& key);
217
238 [[nodiscard]] eve::Result<ResourcePin> pin(Resource* resource);
239
250 [[nodiscard("resource wait outcome must be checked")]] ResultRef<Resource> waitFor(std::string key);
251
253 size_t pendingCount() const;
254
259 void unload(std::string key);
260
262 void unloadPath(const std::string& path);
263
265 size_t count() const;
266
268 void clear();
269
270 // eve::caps::IAssetReloader -- the cache participates in hot reload as the
271 // first listener, refreshing CPU resources before GPU/consumers re-bind.
281 const char* reloadKind() const override { return "cache"; }
283 bool handlesPath(const std::string& normPath) const override;
285 [[nodiscard("resource reload outcome must be checked")]] eve::Result<bool> reload(
286 const std::string& normPath) override;
296 [[nodiscard("loaded resource ownership must be retained or explicitly handled")]] Resource* load(
297 const std::string& key) override {
298 return nullptr;
299 }
300
301protected:
303 ResourceManager() = default;
304
306 void ensureRegistered();
308 Resource* loadReplacement(const std::string& key);
310 Resource* loadUncached(const std::string& norm);
312 void runLoadJob(std::string norm, uint64_t epoch);
313
315 struct AsyncLoad {
316 bool done = false;
317 bool failed = false;
318 std::string error;
319 uint64_t epoch = 0;
320 };
321
323
331 [[nodiscard]] Resource* borrowLocked(const ResourceRef& reference) { return registry_.resolve(reference).get(); }
332
335 std::map<std::string, ResourceRef> resources;
336 std::unordered_map<std::string, std::shared_ptr<AsyncLoad>> pending_;
337 mutable std::mutex mu_;
338 std::condition_variable cv_;
339 uint64_t epoch_ = 1;
340 bool registered_ = false;
341};
342
343} // namespace eve
double value
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
Vocabulary aliases for nullable and checked borrowed references.
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
const GltfImportRequest & request
std::uint32_t key
uint64_t epoch
Definition OnnxGpgpu.cpp:28
std::string path
Definition PlayHost.cpp:110
Move-only, checked operation results for the common layer.
Tag-free slot registry: handles, borrows, unique ownership and keep-alive pins.
std::string resource
std::uint32_t count
ResourceManager is a singleton that manages all resources in the game. It provides a way to load,...
Definition Resource.h:152
ResourceManager()=default
Resource manager.
Resource * load(const std::string &key) override
Cache provider entry point; this implementation does not claim keys.
Definition Resource.h:296
const char * reloadKind() const override
Stable capability name for the cache reloader.
Definition Resource.h:281
std::unordered_map< std::string, std::shared_ptr< AsyncLoad > > pending_
Definition Resource.h:336
Resource * borrowLocked(const ResourceRef &reference)
Borrows the live resource behind a cache entry.
Definition Resource.h:331
script::RuntimeObjectRegistry< Resource, ResourceCacheTag > registry_
Registry that exclusively owns every cached resource.
Definition Resource.h:334
std::condition_variable cv_
Definition Resource.h:338
std::map< std::string, ResourceRef > resources
Definition Resource.h:335
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:51
virtual void adopt(eve::Resource &replacement)=0
Replace this instance's contents with replacement's. The ResourceManager keeps instance identity stab...
Resource(const Resource &)=delete
Resources own keep-alive pins, so they are not copyable.
std::string uri
Definition Resource.h:121
void setUri(std::string value)
Set the resource URI (the cache key). Used by ResourceManager.
Definition Resource.h:117
EVENGINE_API_FOUNDATION std::vector< Resource * > getDependencies() const
Borrowed views of the resources this instance keeps alive.
Definition Resource.cpp:504
EVENGINE_API_FOUNDATION eve::Result< void > addDependency(Resource &dependency)
Keeps dependency alive for as long as this instance lives.
Definition Resource.cpp:511
std::vector< ResourcePin > dependencies
Keep-alive pins; see getDependencies()/addDependency().
Definition Resource.h:123
std::string getUri() const
Returns the uri.
Definition Resource.h:73
virtual ~Resource()
Releases Resource resources.
Definition Resource.h:54
Resource & operator=(const Resource &)=delete
Resource(std::string uri)
Constructs a Resource.
Definition Resource.h:114
Move-only operation result carrying either a value or Status.
Definition Result.h:155
EVENGINE_API_FOUNDATION public API.
Slot/generation registry whose objects are exclusively unique-owned.
RAII keep-alive for one RuntimeObjectRegistry entry.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
AsyncLoad public API.
Definition Resource.h:315
Handle plus owner-lifetime epoch for a non-ECS registry object.