载入中...
搜索中...
未找到
Resource.h
浏览该文件的文档.
1#pragma once
2#include "Object.h"
4
5#include <cstddef>
6#include <map>
7#include <mutex>
8#include <set>
9#include <string>
10#include <vector>
11
12namespace eve {
13
14class Resource;
15
35class Resource : public Object {
36public:
37 virtual ~Resource() {}
38
39 std::string getUri() const { return uri; }
40 std::vector<eve::ref<Resource>> getDependencies() const { return dependencies; }
41
42 virtual void addDependency(eve::ref<Resource> resource) { dependencies.push_back(resource); }
43
52 virtual void adopt(eve::Resource& replacement) = 0;
53
54protected:
55 Resource(std::string uri) : uri(uri) {}
56
58 void setUri(std::string value) { uri = std::move(value); }
59
60 friend class ResourceManager;
61
62 std::string uri;
63 std::vector<eve::ref<Resource>> dependencies;
64};
65
66
88public:
90
92 static std::string normalizePath(std::string path);
93
95 static std::string makeKey(const std::string& path, const std::string& query = "");
96
98 static std::string pathOfKey(const std::string& key);
99
110 Resource *get(std::string key);
111
116 void unload(std::string key);
117
119 void unloadPath(const std::string& path);
120
122 size_t count() const;
123
125 void clear();
126
127 // eve::caps::IAssetReloader -- the cache participates in hot reload as the
128 // first listener, refreshing CPU resources before GPU/consumers re-bind.
129 const char* reloadKind() const override { return "cache"; }
130 bool handlesPath(const std::string& normPath) const override;
131 bool reload(const std::string& normPath) override;
132 Resource* load(const std::string& key) override { return nullptr; }
133
134protected:
135 ResourceManager() = default;
136
137 void ensureRegistered();
138 bool refreshEntry(const std::string& key, std::set<std::string>& visited);
139 void refreshDependents(Resource* updated, std::set<std::string>& visited);
140
141 std::map<std::string, ref<Resource>> resources;
142 mutable std::mutex mu_;
143 bool registered_ = false;
144};
145
146} // namespace eve
std::string value
Object is the base class for all game objects. It provides reference counting and dirty flag for upda...
Definition Object.h:18
ResourceManager is a singleton that manages all resources in the game. It provides a way to load,...
Definition Resource.h:87
ResourceManager()=default
void unloadPath(const std::string &path)
Drop every cache entry whose path matches, whatever its parameters.
Definition Resource.cpp:94
bool handlesPath(const std::string &normPath) const override
Definition Resource.cpp:116
Resource * load(const std::string &key) override
Definition Resource.h:132
void unload(std::string key)
Drop the exact cache entry key (parameters included). The resource stays alive while other holders st...
Definition Resource.cpp:89
Resource * get(std::string key)
Get the cached resource for key; on a miss, load it through the registered IAssetReloader providers a...
Definition Resource.cpp:56
std::map< std::string, ref< Resource > > resources
Definition Resource.h:141
bool reload(const std::string &normPath) override
Definition Resource.cpp:126
static std::string pathOfKey(const std::string &key)
The path part of a cache key (everything before the first '?').
Definition Resource.cpp:39
bool refreshEntry(const std::string &key, std::set< std::string > &visited)
Definition Resource.cpp:146
static std::string normalizePath(std::string path)
Normalize a VFS path: backslashes to '/', strip leading "./" and trailing '/'.
Definition Resource.cpp:21
const char * reloadKind() const override
Definition Resource.h:129
void clear()
Drop every entry and re-arm lazy registration (tests / teardown).
Definition Resource.cpp:105
static std::string makeKey(const std::string &path, const std::string &query="")
Build a cache key from a path and optional ?query parameters.
Definition Resource.cpp:30
size_t count() const
Number of cached entries.
Definition Resource.cpp:51
void refreshDependents(Resource *updated, std::set< std::string > &visited)
Definition Resource.cpp:185
static ResourceManager & getInstance()
Definition Resource.cpp:8
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:35
virtual void adopt(eve::Resource &replacement)=0
Replace this instance's contents with replacement's. The ResourceManager keeps instance identity stab...
std::string uri
Definition Resource.h:62
void setUri(std::string value)
Set the resource URI (the cache key). Used by ResourceManager.
Definition Resource.h:58
std::string getUri() const
Definition Resource.h:39
std::vector< eve::ref< Resource > > dependencies
Definition Resource.h:63
virtual ~Resource()
Definition Resource.h:37
std::vector< eve::ref< Resource > > getDependencies() const
Definition Resource.h:40
Resource(std::string uri)
Definition Resource.h:55
virtual void addDependency(eve::ref< Resource > resource)
Definition Resource.h:42
ref is a smart pointer that automatically calls ref() and unref() on the object. Type T must be a sub...
Definition Object.h:54
Definition Build.cpp:11