FastLED 3.9.15
Loading...
Searching...
No Matches
asset.cpp.hpp
Go to the documentation of this file.
1#pragma once
2
3// IWYU pragma: private
4
12
13#include "fl/asset/asset.h"
14#include "fl/stl/fstream.h"
15#include "fl/stl/noexcept.h"
16#include "fl/stl/string.h"
17#include "fl/stl/string_view.h"
18#include "fl/stl/url.h"
19#include "fl/stl/vector.h"
20#include "platforms/is_platform.h"
21
22namespace fl {
23
24namespace asset_detail {
25
41
43 // Construct-on-first-use avoids static-init ordering issues with the
44 // generated asset table (which also uses static init).
45 static fl::vector<AssetEntry> s_registry;
46 return s_registry;
47}
48
51 auto& r = registry();
52 for (fl::size i = 0; i < r.size(); ++i) {
53 // Compare as string_view on both sides to avoid any ambiguity in the
54 // fl::string / fl::string_view overload resolution.
55 fl::string_view candidate(r[i].path.c_str(), r[i].path.size());
56 if (candidate == path) {
57 return r[i].url;
58 }
59 }
60 return fl::url();
61}
62
63#if defined(FASTLED_TESTING) || defined(FL_IS_STUB)
74inline fl::url resolve_host(fl::string_view path) FL_NOEXCEPT {
75 // Try raw file on disk.
76 fl::string pathStr(path.data(), path.size());
77 {
78 fl::ifstream probe(pathStr.c_str(), fl::ios::in | fl::ios::binary);
79 if (probe.is_open()) {
80 probe.close();
81 // Return the path as-is (a relative/absolute filesystem path).
82 // fl::url accepts any string; callers on host treat .string() as
83 // a local path. This matches the existing AudioUrl test pattern
84 // that stores a raw URL string in the url field.
85 return fl::url(pathStr);
86 }
87 }
88
89 // Fallback: try <path>.lnk alongside.
90 fl::string lnkPath = pathStr;
91 lnkPath += ".lnk";
92 fl::ifstream lnk(lnkPath.c_str(), fl::ios::in | fl::ios::binary);
93 if (!lnk.is_open()) {
94 return fl::url();
95 }
96 // Read entire file. .lnk files are expected to be tiny (URL + a few
97 // metadata lines); cap the read at 8 KiB to avoid pathological inputs.
98 constexpr fl::size kMaxLnkBytes = 8 * 1024;
99 fl::size sz = lnk.size();
100 if (sz > kMaxLnkBytes) {
101 sz = kMaxLnkBytes;
102 }
103 fl::string content;
104 content.resize(sz);
105 if (sz > 0) {
106 lnk.read(&content[0], sz);
107 }
108 lnk.close();
109 return parse_lnk(fl::string_view(content.data(), content.size()));
110}
111#endif
112
113} // namespace asset_detail
114
119 asset_detail::registry().push_back(
120 {fl::string(path.data(), path.size()), u});
121}
122
124 if (!a) {
125 return fl::url();
126 }
127 const fl::string_view p = a.path();
128
129 // 1. Registry always wins. On WASM this is populated by generated code
130 // from the asset scanner; on host/stub tests may populate it manually.
131 fl::url fromRegistry = asset_detail::lookup_registry(p);
132 if (fromRegistry.isValid()) {
133 return fromRegistry;
134 }
135
136 // 2. Host/stub: fall back to filesystem probe + sibling .lnk.
137#if defined(FASTLED_TESTING) || defined(FL_IS_STUB)
138 return asset_detail::resolve_host(p);
139#else
140 // 3. Other platforms (WASM without manifest, ESP32 v1): no fallback.
141 // TODO(#2284): ESP32 LittleFS / SD-card resolution.
142 return fl::url();
143#endif
144}
145
146} // namespace fl
First-class asset handles for sketches that live under <sketch>/data/.
Opaque handle to a sketch-local asset.
Definition asset.h:84
void resize(fl::size count) FL_NOEXCEPT
const char * data() const FL_NOEXCEPT
const char * c_str() const FL_NOEXCEPT
fl::size size() const FL_NOEXCEPT
bool isValid() const FL_NOEXCEPT
Definition url.h:37
Definition url.h:15
fl::url lookup_registry(fl::string_view path) FL_NOEXCEPT
Look up an asset path in the registry. Returns invalid url() on miss.
Definition asset.cpp.hpp:50
fl::vector< AssetEntry > & registry() FL_NOEXCEPT
Definition asset.cpp.hpp:42
fl::url url
Resolved URL (or file:// for host).
Definition asset.cpp.hpp:39
fl::string path
Relative asset path (e.g. "data/track.mp3").
Definition asset.cpp.hpp:38
Process-local registry of (asset_path → resolved URL) pairs.
Definition asset.cpp.hpp:37
static constexpr openmode in
Definition fstream.h:31
static constexpr openmode binary
Definition fstream.h:29
fl::url resolve_asset(const asset_ref &a) FL_NOEXCEPT
Resolve an asset handle to a URL (or local file path) at runtime.
url parse_lnk(fl::string_view content) FL_NOEXCEPT
Parse the contents of a .lnk file into a fl::url.
Definition url.h:274
void register_asset(fl::string_view path, const fl::url &u) FL_NOEXCEPT
Public helper: plug an asset mapping at runtime.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Lightweight URL parser for embedded environments.