35 "DATA override payload is null"));
39 static_cast<std::size_t
>(
40 std::numeric_limits<std::uint32_t>::max()))
45 "DATA override payload exceeds the 32-bit runtime size"));
48 return static_cast<std::uint32_t
>(
bytes->size());
53 LayeredDataSource::LayeredDataSource(
54 std::shared_ptr<const DataSource>
fallback)
61 std::shared_ptr<const LayeredDataSource>,
63 LayeredDataSource::create(
64 std::shared_ptr<const DataSource>
fallback,
65 std::span<const DataSourceOverride> overrides)
67 auto result = std::shared_ptr<LayeredDataSource>(
70 for (
const auto& replacement : overrides)
75 DataErrorCode::EmptyItem,
77 "DATA override cannot replace EmptyDataId"));
80 if (replacement.type == LegacyDataType::Unknown)
83 DataErrorCode::InvalidItemType,
85 "DATA override requires an explicit runtime type"));
93 return std::unexpected(size.error());
96 const auto [
_,
inserted] = result->overrides_.emplace(
98 Item{replacement.type, replacement.bytes});
102 DataErrorCode::DuplicateDataId,
104 "DATA override contains the same logical DataId twice"));
108 return std::shared_ptr<const LayeredDataSource>(
115 return overrides_.size();
119 bool LayeredDataSource::overrides(
DataId id)
const noexcept
121 return overrides_.contains(
id);
125 const LayeredDataSource::Item* LayeredDataSource::find(
128 const auto item = overrides_.find(
id);
129 return item == overrides_.end() ?
nullptr : &item->second;
133 DataError LayeredDataSource::missing(DataId
id)
const
136 DataErrorCode::ResourceNotFound,
138 "logical DATA item is absent from the layered source");
142 std::expected<ArchiveItemMetadata, DataError>
143 LayeredDataSource::metadata(
DataId id)
const
145 if (
const auto* item = find(
id))
148 if (!size)
return std::unexpected(size.error());
158 if (fallback_)
return fallback_->metadata(
id);
159 return std::unexpected(missing(
id));
163 std::expected<LegacyDataType, DataError>
164 LayeredDataSource::initialDataType(
DataId id)
const
166 if (
const auto* item = find(
id))
return item->type;
167 if (fallback_)
return fallback_->initialDataType(
id);
168 return std::unexpected(missing(
id));
172 std::expected<std::uint32_t, DataError>
173 LayeredDataSource::initialSize(
DataId id)
const
175 if (
const auto* item = find(
id))
177 if (fallback_)
return fallback_->initialSize(
id);
178 return std::unexpected(missing(
id));
182 std::expected<std::uint32_t, DataError>
183 LayeredDataSource::loadedRawSize(
DataId id)
const
185 if (
const auto* item = find(
id))
187 if (fallback_)
return fallback_->loadedRawSize(
id);
188 return std::unexpected(missing(
id));
192 std::expected<SharedDataBytes, DataError>
195 if (
const auto* item = find(
id))
return item->bytes;
196 if (fallback_)
return fallback_->load(
id);
197 return std::unexpected(missing(
id));
201 std::expected<std::size_t, DataError>
202 LayeredDataSource::readRaw(
207 if (
const auto* item = find(
id))
209 const auto size = item->bytes->size();
210 const auto start =
static_cast<std::size_t
>(
startOffset);
212 return std::size_t{0};
214 const auto amount = std::min(
219 item->bytes->data() + start,
225 return fallback_->readRaw(
229 return std::unexpected(missing(
id));
233 std::expected<bool, DataError>
234 LayeredDataSource::isLoaded(
DataId id)
const
236 if (find(
id))
return true;
237 if (fallback_)
return fallback_->isLoaded(
id);
238 return std::unexpected(missing(
id));
std::vector< std::byte > bytes
constexpr DataTag dataTag(DataId id) noexcept
std::shared_ptr< const DataBytes > SharedDataBytes
constexpr bool isEmptyDataId(DataId id) noexcept