载入中...
搜索中...
未找到
SoundLoader.cpp
浏览该文件的文档.
1// Loads SoundData through the unified resource cache (common/Resource.h).
2//
3// Decoded PCM is the largest CPU asset in the engine, so repeated loads of one
4// file share a single buffer; a file change refreshes it in place.
5
7#include "common/Capability.h"
8#include "common/Exception.h"
9#include "common/Module.h"
10#include "common/Resource.h"
11#include "filesystem/FileData.h"
13#include "sound/Sound.h"
14#include "sound/SoundData.h"
15
16#include <cctype>
17#include <memory>
18#include <string>
19
20namespace eve::sound {
21namespace {
22
23std::string extensionOf(const std::string &path) {
24 const auto pos = path.find_last_of('.');
25 if (pos == std::string::npos) return {};
26 std::string ext = path.substr(pos);
27 for (char &c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
28 return ext;
29}
30
31bool isSoundPath(const std::string &path) {
32 const std::string ext = extensionOf(path);
33 return ext == ".wav" || ext == ".ogg" || ext == ".mp3" || ext == ".flac" || ext == ".mod" ||
34 ext == ".xm" || ext == ".s3m" || ext == ".it";
35}
36
37class SoundLoader : public eve::caps::IAssetReloader {
38public:
39 const char *reloadKind() const override { return "sound"; }
40
41 bool handlesPath(const std::string &normPath) const override {
42 return isSoundPath(eve::ResourceManager::pathOfKey(normPath));
43 }
44
50 eve::Result<bool> reload(const std::string &) override { return eve::Result<bool>::success(false); }
51
52 eve::Resource *load(const std::string &key) override {
53 const std::string path = eve::ResourceManager::pathOfKey(key);
54 if (!isSoundPath(path)) return nullptr;
55
56 auto *fs = filesystem::Filesystem::create();
57 if (!fs) return nullptr;
58 std::unique_ptr<filesystem::FileData> fd(fs->read(path));
59 if (!fd || fd->getData() == nullptr || fd->getSize() == 0)
60 throw eve::Exception("Could not read sound file: %s", path.c_str());
61
62 auto *mod = Sound::create();
63 if (!mod) throw eve::Exception("eve::sound module is not loaded");
64 return mod->newSoundData(fd.get());
65 }
66};
67
68struct Register {
69 Register() {
70 static SoundLoader loader;
71 eve::cap::addListener<eve::caps::IAssetReloader>(&loader, eve::caps::IAssetReloader::kCache);
72 }
73} g_register;
74
75} // namespace
76} // namespace eve::sound
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.