8#include "nlohmann/json.hpp"
16std::unordered_map<ArtifactKey, std::string>
19 std::unordered_map<ArtifactKey, std::string> checksums{};
21 for (
const auto &all_keys = key_provider_->
get_all_artifact_keys(tileset_name);
const auto &key : all_keys) {
24 panic(fmt::format(
"expected artifact '{}' does not exist", key.key()));
26 std::ifstream stream{key.key()};
27 const auto key_digest = digest.digest(stream);
28 checksums.emplace(key, key_digest);
34std::unordered_map<ArtifactKey, std::string>
38 const auto artifact_checksum_file = key_provider_->
tileset_root(tileset_name) /
"artifact_checksums.json";
41 if (!exists(artifact_checksum_file)) {
44 panic(fmt::format(
"failed to initialize checksum cache for '{}'", tileset_name));
48 std::ifstream file{artifact_checksum_file};
49 nlohmann::json json_data;
52 std::unordered_map<ArtifactKey, std::string> checksums;
53 for (
const auto &[key, value] : json_data.items()) {
54 const auto full_path = key_provider_->
tileset_root(tileset_name) / std::filesystem::path{key};
55 checksums.emplace(
ArtifactKey{full_path}, value.get<std::string>());
62 const std::string &tileset_name,
const std::unordered_map<ArtifactKey, std::string> &checksums)
const
65 const auto artifact_checksum_file = key_provider_->
tileset_root(tileset_name) /
"artifact_checksums.json";
66 std::ofstream file{artifact_checksum_file};
69 std::map<std::string, std::string> sorted_checksums;
70 for (
const auto &[artifact_key, checksum] : checksums) {
77 const auto relative_key =
78 std::filesystem::relative(artifact_key.key(), key_provider_->
tileset_root(tileset_name));
79 sorted_checksums[relative_key] = checksum;
91 nlohmann::json json_data;
92 for (
const auto &[path, checksum] : sorted_checksums) {
93 json_data[path] = checksum;
97 file << json_data.dump(2);
A type-safe wrapper for artifact keys.
Result< void > cache_tileset_checksums(const std::string &tileset_name, const std::unordered_map< ArtifactKey, std::string > &checksums) const override
Caches checksums for the given Tileset to persistent storage.
std::unordered_map< ArtifactKey, std::string > compute_tileset_artifact_checksums(const std::string &tileset_name) const override
Computes checksums for the artifacts that belong to the given Tileset.
std::unordered_map< ArtifactKey, std::string > load_cached_tileset_checksums(const std::string &tileset_name) const override
Loads the cached checksums for the given Tileset.
std::filesystem::path tileset_root(const std::string &tileset_name) const
Returns the filesystem path to the root directory of a tileset.
bool artifact_exists(const ArtifactKey &key) const override
Checks whether an artifact exists in the backing store for the given key.
Computes the MD5 digest of an input stream.
virtual std::vector< ArtifactKey > get_all_artifact_keys(const std::string &tileset_name) const
Gets the keys for all tileset artifacts (both Porytiles and Porymap) present in the given Tileset.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
std::expected< T, E > Result
A result with some type T on success, otherwise an error of type E.