载入中...
搜索中...
未找到
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
45 eve::Resource *load(const std::string &key) override {
46 const std::string path = eve::ResourceManager::pathOfKey(key);
47 if (!isSoundPath(path)) return nullptr;
48
49 auto *fs = filesystem::Filesystem::create();
50 if (!fs) return nullptr;
51 std::unique_ptr<filesystem::FileData> fd(fs->read(path));
52 if (!fd || fd->getData() == nullptr || fd->getSize() == 0)
53 throw eve::Exception("Could not read sound file: %s", path.c_str());
54
55 auto *mod = Sound::create();
56 if (!mod) throw eve::Exception("eve::sound module is not loaded");
57 return mod->newSoundData(fd.get());
58 }
59};
60
61struct Register {
62 Register() {
63 static SoundLoader loader;
65 }
66} g_register;
67
68} // namespace
69} // namespace eve::sound
uint32_t c
static std::string pathOfKey(const std::string &key)
The path part of a cache key (everything before the first '?').
Definition Resource.cpp:39
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:35
I * query()
Definition Capability.h:77