载入中...
搜索中...
未找到
ImageLoader.cpp
浏览该文件的文档.
1// Loads ImageData through the unified resource cache (common/Resource.h).
2//
3// Registered as an eve::caps::IAssetReloader provider: ResourceManager asks it
4// to load() on a cache miss and to produce a fresh ImageData on hot reload,
5// which is adopted into the cached instance in place (identity stays stable).
6// reload() is intentionally not implemented here -- the GPU-side refresh is
7// owned by graphics (TextureReloader) and runs after the cache refresh
8// (kCache < kTexture).
9
11#include "common/Capability.h"
12#include "common/Exception.h"
13#include "common/Module.h"
14#include "common/Resource.h"
15#include "filesystem/FileData.h"
17#include "image/Image.h"
18#include "image/ImageData.h"
19
20#include <cctype>
21#include <memory>
22#include <mutex>
23#include <string>
24
25namespace eve::image {
26namespace {
27
28std::string extensionOf(const std::string &path) {
29 const auto pos = path.find_last_of('.');
30 if (pos == std::string::npos) return {};
31 std::string ext = path.substr(pos);
32 for (char &c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
33 return ext;
34}
35
36bool isImagePath(const std::string &path) {
37 const std::string ext = extensionOf(path);
38 return ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".tga" ||
39 ext == ".gif" || ext == ".webp" || ext == ".exr" || ext == ".hdr";
40}
41
42class ImageLoader : public eve::caps::IAssetReloader {
43public:
44 const char *reloadKind() const override { return "image"; }
45
46 bool handlesPath(const std::string &normPath) const override {
47 return isImagePath(eve::ResourceManager::pathOfKey(normPath));
48 }
49
56 eve::Result<bool> reload(const std::string &) override { return eve::Result<bool>::success(false); }
57
58 eve::Resource *load(const std::string &key) override {
59 const std::string path = eve::ResourceManager::pathOfKey(key);
60 if (!isImagePath(path)) return nullptr;
61
62 auto *fs = filesystem::Filesystem::create();
63 if (!fs) return nullptr;
64 std::unique_ptr<filesystem::FileData> fd;
65 {
66 static std::mutex vfsReadMu;
67 std::lock_guard<std::mutex> vfsLock(vfsReadMu);
68 fd.reset(fs->read(path));
69 }
70 if (!fd || fd->getData() == nullptr || fd->getSize() == 0)
71 throw eve::Exception("Could not read image file: %s", path.c_str());
72
73 auto *mod = Image::create();
74 if (!mod) throw eve::Exception("eve::image module is not loaded");
75 return mod->newImageData(fd.get());
76 }
77};
78
79struct Register {
80 Register() {
81 static ImageLoader loader;
82 eve::cap::addListener<eve::caps::IAssetReloader>(&loader, eve::caps::IAssetReloader::kCache);
83 }
84} g_register;
85
86} // namespace
87} // namespace eve::image
std::uint32_t key
std::int32_t c
std::string path
Definition PlayHost.cpp:110
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
static std::string pathOfKey(const std::string &key)
The path part of a cache key (everything before the first '?').
Definition Resource.cpp:46
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:51
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
EVENGINE_API_FOUNDATION public API.