载入中...
搜索中...
未找到
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 <condition_variable>
13#include <cstddef>
14#include <map>
15#include <memory>
16#include <mutex>
17#include <span>
18#include <string>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
23namespace eve {
24
25class Resource;
26
28struct ResourceCacheTag;
29
32
52class Resource {
53public:
55 virtual ~Resource() {}
56
70 Resource(const Resource&) = delete;
71 Resource& operator=(const Resource&) = delete;
72
74 std::string getUri() const { return uri; }
75
86 [[nodiscard]] EVENGINE_API_FOUNDATION std::vector<Resource*> getDependencies() const;
87
99
111 virtual void adopt(eve::Resource& replacement) = 0;
112
113protected:
115 Resource(std::string uri) : uri(std::move(uri)) {}
116
118 void setUri(std::string value) { uri = std::move(value); }
119
120 friend class ResourceManager;
121
122 std::string uri;
124 std::vector<ResourcePin> dependencies;
125};
126
127
154public:
156 static ResourceManager& getInstance();
157
159 static std::string normalizePath(std::string path);
160
162 static std::string makeKey(const std::string& path, const std::string& query = "");
163
165 static std::string pathOfKey(const std::string& key);
166
185 [[nodiscard("resource lookup ownership must be retained or explicitly handled")]] Resource* get(std::string key);
186
199 [[nodiscard("async resource request outcome must be checked")]] eve::Result<void> request(
200 std::string key);
201
206 [[nodiscard("async resource request outcome must be checked")]] eve::Result<void> requestAll(
207 std::span<const std::string> keys);
208
217 [[nodiscard]] OptionalRef<Resource> peek(const std::string& key);
218
239 [[nodiscard]] eve::Result<ResourcePin> pin(Resource* resource);
240
251 [[nodiscard("resource wait outcome must be checked")]] ResultRef<Resource> waitFor(std::string key);
252
254 size_t pendingCount() const;
255
260 void unload(std::string key);
261
263 void unloadPath(const std::string& path);
264
266 size_t count() const;
267
269 void clear();
270
271 // eve::caps::IAssetReloader -- the cache participates in hot reload as the
272 // first listener, refreshing CPU resources before GPU/consumers re-bind.
282 const char* reloadKind() const override { return "cache"; }
284 bool handlesPath(const std::string& normPath) const override;
286 [[nodiscard("resource reload outcome must be checked")]] eve::Result<bool> reload(
287 const std::string& normPath) override;
297 [[nodiscard("loaded resource ownership must be retained or explicitly handled")]] Resource* load(
298 const std::string& key) override {
299 return nullptr;
300 }
301
302protected:
304 ResourceManager() = default;
305
307 void ensureRegistered();
309 Resource* loadReplacement(const std::string& key);
311 Resource* loadUncached(const std::string& norm);
313 void runLoadJob(std::string norm, uint64_t epoch);
314
316 struct AsyncLoad {
317 bool done = false;
318 bool failed = false;
319 std::string error;
320 uint64_t epoch = 0;
321 };
322
324
332 [[nodiscard]] Resource* borrowLocked(const ResourceRef& reference) { return registry_.resolve(reference).get(); }
333
336 std::map<std::string, ResourceRef> resources;
337 std::unordered_map<std::string, std::shared_ptr<AsyncLoad>> pending_;
338 mutable std::mutex mu_;
339 std::condition_variable cv_;
340 uint64_t epoch_ = 1;
341 bool registered_ = false;
342};
343
344} // namespace eve
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
Vocabulary aliases for nullable and checked borrowed references.
SQObjectPtr key
Definition ECS.cpp:155
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
const GltfImportRequest & request
bool done
uint64_t epoch
Definition OnnxGpgpu.cpp:28
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
Move-only, checked operation results for the common layer.
Tag-free slot registry: handles, borrows, unique ownership and keep-alive pins.
std::string resource
int count
ResourceManager is a singleton that manages all resources in the game. It provides a way to load,...
Definition Resource.h:153
ResourceManager()=default
Resource manager.
Resource * load(const std::string &key) override
Cache provider entry point; this implementation does not claim keys.
Definition Resource.h:297
const char * reloadKind() const override
Stable capability name for the cache reloader.
Definition Resource.h:282
std::unordered_map< std::string, std::shared_ptr< AsyncLoad > > pending_
Definition Resource.h:337
Resource * borrowLocked(const ResourceRef &reference)
Borrows the live resource behind a cache entry.
Definition Resource.h:332
script::RuntimeObjectRegistry< Resource, ResourceCacheTag > registry_
Registry that exclusively owns every cached resource.
Definition Resource.h:335
std::condition_variable cv_
Definition Resource.h:339
std::map< std::string, ResourceRef > resources
Definition Resource.h:336
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:52
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:122
void setUri(std::string value)
Set the resource URI (the cache key). Used by ResourceManager.
Definition Resource.h:118
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:124
std::string getUri() const
Returns the uri.
Definition Resource.h:74
virtual ~Resource()
Releases Resource resources.
Definition Resource.h:55
Resource & operator=(const Resource &)=delete
Resource(std::string uri)
Constructs a Resource.
Definition Resource.h:115
Move-only operation result carrying either a value or Status.
Definition Result.h:176
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:316
Handle plus owner-lifetime epoch for a non-ECS registry object.