AssetManager.cpp
Go to the documentation of this file.
2 
3 #include "spdlog/fmt/fmt.h"
4 #include "utility/spdlog-fmt.hpp"
5 
6 // std
7 #include <fstream>
8 
9 namespace dai {
10 
11 std::string Asset::getRelativeUri() {
12  return fmt::format("{}:{}", "asset", key);
13 }
14 
15 std::shared_ptr<dai::Asset> AssetManager::set(Asset asset) {
16  std::string key = asset.key;
17  assetMap[key] = std::make_shared<Asset>(std::move(asset));
18  return assetMap[key];
19 }
20 
21 std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, Asset asset) {
22  // Rename the asset with supplied key and store
23  Asset a(key);
24  a.data = std::move(asset.data);
25  a.alignment = asset.alignment;
26  return set(std::move(a));
27 }
28 
29 std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, const dai::Path& path, int alignment) {
30  // Load binary file at path
31  std::ifstream stream(path, std::ios::in | std::ios::binary);
32  if(!stream.is_open()) {
33  // Throw an error
34  // TODO(themarpe) - Unify exceptions into meaningful groups
35  throw std::runtime_error(fmt::format("Cannot load asset, file at path {} doesn't exist.", path));
36  }
37 
38  // Create an asset
39  Asset binaryAsset(key);
40  binaryAsset.alignment = alignment;
41  binaryAsset.data = std::vector<std::uint8_t>(std::istreambuf_iterator<char>(stream), {});
42  // Store asset
43  return set(std::move(binaryAsset));
44 }
45 
46 std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, const std::vector<std::uint8_t>& data, int alignment) {
47  // Create an asset
48  Asset binaryAsset(key);
49  binaryAsset.alignment = alignment;
50  binaryAsset.data = std::move(data);
51  // Store asset
52  return set(std::move(binaryAsset));
53 }
54 
55 std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, std::vector<std::uint8_t>&& data, int alignment) {
56  // Create an asset
57  Asset binaryAsset(key);
58  binaryAsset.alignment = alignment;
59  binaryAsset.data = std::move(data);
60  // Store asset
61  return set(std::move(binaryAsset));
62 }
63 
64 std::shared_ptr<const Asset> AssetManager::get(const std::string& key) const {
65  if(assetMap.count(key) == 0) {
66  return nullptr;
67  }
68  return assetMap.at(key);
69 }
70 
71 std::shared_ptr<Asset> AssetManager::get(const std::string& key) {
72  if(assetMap.count(key) == 0) {
73  return nullptr;
74  }
75  return assetMap.at(key);
76 }
77 
78 void AssetManager::addExisting(std::vector<std::shared_ptr<Asset>> assets) {
79  // make sure that key doesn't exist already
80  for(const auto& asset : assets) {
81  if(assetMap.count(asset->key) > 0) throw std::logic_error("An Asset with the key: " + asset->key + " already exists.");
82  std::string key = asset->key;
83  assetMap[key] = asset;
84  }
85 }
86 
87 std::vector<std::shared_ptr<const Asset>> AssetManager::getAll() const {
88  std::vector<std::shared_ptr<const Asset>> a;
89  for(const auto& kv : assetMap) {
90  a.push_back(kv.second);
91  }
92  return a;
93 }
94 
95 std::vector<std::shared_ptr<Asset>> AssetManager::getAll() {
96  std::vector<std::shared_ptr<Asset>> a;
97  for(const auto& kv : assetMap) {
98  a.push_back(kv.second);
99  }
100  return a;
101 }
102 
103 std::size_t AssetManager::size() const {
104  return assetMap.size();
105 }
106 
107 void AssetManager::remove(const std::string& key) {
108  assetMap.erase(key);
109 }
110 
111 void AssetManager::serialize(AssetsMutable& mutableAssets, std::vector<std::uint8_t>& storage, std::string prefix) const {
112  using namespace std;
113 
114  for(auto& kv : assetMap) {
115  auto& a = *kv.second;
116 
117  // calculate additional bytes needed to offset to alignment
118  int toAdd = 0;
119  if(a.alignment > 1 && storage.size() % a.alignment != 0) {
120  toAdd = a.alignment - (storage.size() % a.alignment);
121  }
122 
123  // calculate offset
124  std::uint32_t offset = static_cast<uint32_t>(storage.size()) + toAdd;
125 
126  // Add alignment bytes
127  storage.resize(storage.size() + toAdd);
128 
129  // copy data
130  storage.insert(storage.end(), a.data.begin(), a.data.end());
131 
132  // Add to map the currently added asset
133  mutableAssets.set(prefix + a.key, offset, static_cast<uint32_t>(a.data.size()), a.alignment);
134  }
135 }
136 
137 void AssetsMutable::set(std::string key, std::uint32_t offset, std::uint32_t size, std::uint32_t alignment) {
138  AssetInternal internal = {};
139  internal.offset = offset;
140  internal.size = size;
141  internal.alignment = alignment;
142  map[key] = internal;
143 }
144 
145 } // namespace dai
dai::AssetsMutable
Definition: AssetManager.hpp:24
dai::AssetManager::addExisting
void addExisting(std::vector< std::shared_ptr< Asset >> assets)
Definition: AssetManager.cpp:78
dai::AssetManager::remove
void remove(const std::string &key)
Definition: AssetManager.cpp:107
dai::Asset::alignment
std::uint32_t alignment
Definition: AssetManager.hpp:20
dai::AssetManager::serialize
void serialize(AssetsMutable &assets, std::vector< std::uint8_t > &assetStorage, std::string prefix="") const
Serializes.
Definition: AssetManager.cpp:111
dai::utility::map
static std::unordered_map< std::string, std::string > map
Definition: Environment.cpp:16
dai::AssetManager::size
std::size_t size() const
Definition: AssetManager.cpp:103
dai::Asset::data
std::vector< std::uint8_t > data
Definition: AssetManager.hpp:19
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
dai::AssetsMutable::set
void set(std::string, std::uint32_t offset, std::uint32_t size, std::uint32_t alignment)
Definition: AssetManager.cpp:137
dai::Asset
Asset is identified with string key and can store arbitrary binary data.
Definition: AssetManager.hpp:15
dai::Asset::key
const std::string key
Definition: AssetManager.hpp:18
dai::Asset::getRelativeUri
std::string getRelativeUri()
Definition: AssetManager.cpp:11
dai::AssetManager::set
std::shared_ptr< dai::Asset > set(Asset asset)
Definition: AssetManager.cpp:15
AssetManager.hpp
dai::Assets::AssetInternal::offset
std::uint32_t offset
Definition: Assets.hpp:27
dai::AssetManager::get
std::shared_ptr< const Asset > get(const std::string &key) const
Definition: AssetManager.cpp:64
std
Definition: Node.hpp:366
spdlog-fmt.hpp
dai::Path
Represents paths on a filesystem; accepts utf-8, Windows utf-16 wchar_t, or std::filesystem::path.
Definition: Path.hpp:27
dai::AssetManager::assetMap
std::map< std::string, std::shared_ptr< Asset > > assetMap
Definition: AssetManager.hpp:34
dai
Definition: CameraExposureOffset.hpp:6
dai::Assets::AssetInternal
Definition: Assets.hpp:26
dai::AssetManager::getAll
std::vector< std::shared_ptr< const Asset > > getAll() const
Definition: AssetManager.cpp:87


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:18