From 9f43a210c56c50d4054b28e75669818820c7300f Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 15:53:34 -0400 Subject: [PATCH] Stage 5: content-hash AssetStore; undo records asset keys, not pixels - resource::AssetStore (core) keeps immutable images and sounds keyed by content hash for the life of the document; nothing is purged until it closes. - Undo state holds spritesheet/sound id -> asset key maps (always tracked), so every edit can undo texture changes; the textures-only snapshot flag (edit_begin(label, true) / push(message, isTextures)) and the pixel/audio equality helpers are gone. - Document::texture_get/sound_get read through the store; texture_set/sound_set add assets; texture_edit gives the pencil a private draft that becomes a new asset on the next change(). - Spritesheet dirty state compares path + asset key instead of hashing pixels. - Preview audio reads sounds through the store. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 +- src/document.cpp | 103 ++++++++++++++---------- src/document.hpp | 13 ++- src/imgui/window/animation_preview.cpp | 22 ++--- src/imgui/window/regions.cpp | 4 +- src/imgui/window/spritesheet_editor.cpp | 13 ++- src/imgui/window/spritesheets.cpp | 2 +- src/imgui/window/window.hpp | 2 +- src/resource/asset_store.cpp | 52 ++++++++++++ src/resource/asset_store.hpp | 24 ++++++ src/snapshots.cpp | 49 +---------- src/snapshots.hpp | 20 ++--- tests/assets.cpp | 19 +++++ 13 files changed, 195 insertions(+), 130 deletions(-) create mode 100644 src/resource/asset_store.cpp create mode 100644 src/resource/asset_store.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a1452e8..e878507 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,7 +109,7 @@ file(GLOB PROJECT_SRC CONFIGURE_DEPENDS set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp) -foreach(CORE_FILE log resource/strings resource/image resource/audio_data util/file util/path util/math util/string util/time util/working_directory +foreach(CORE_FILE log resource/strings resource/image resource/audio_data resource/asset_store util/file util/path util/math util/string util/time util/working_directory util/xml util/pack util/imgui/multiselect snapshots storage playback selection) list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp) endforeach() diff --git a/src/document.cpp b/src/document.cpp index 3269078..3bd238b 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -129,17 +129,10 @@ namespace anm2ed::document std::string element_name_get(const Element& element) { return element.name; } - template - void resource_load(int id, const std::filesystem::path& path, std::map& resources, - std::unordered_map& paths) - { - resources[id] = Resource(path::case_insensitive_find(path)); - paths[id] = path; - } - - template - void resources_sync(Document& document, ElementType type, std::map& resources, - std::unordered_map& paths) + // Loads assets for elements that are new or whose path changed, and forgets those of removed elements. + template + void resources_sync(Document& document, ElementType type, std::map& keys, + std::unordered_map& paths, Load&& load) { std::set validIds{}; util::WorkingDirectory workingDirectory(document.directory_get()); @@ -149,28 +142,22 @@ namespace anm2ed::document if (element.type != type) continue; validIds.insert(element.id); auto path = paths.find(element.id); - if (!resources.contains(element.id) || path == paths.end() || path->second != element.path) - resource_load(element.id, element.path, resources, paths); + if (!keys.contains(element.id) || path == paths.end() || path->second != element.path) + { + load(element.id, path::case_insensitive_find(element.path)); + paths[element.id] = element.path; + } } - std::erase_if(resources, [&](const auto& pair) { return !validIds.contains(pair.first); }); + std::erase_if(keys, [&](const auto& pair) { return !validIds.contains(pair.first); }); std::erase_if(paths, [&](const auto& pair) { return !validIds.contains(pair.first); }); } - uint64_t spritesheet_hash_get(const Element& spritesheet, const resource::Image* texture) + // A spritesheet is dirty when its path or image asset differs from what was last saved. + uint64_t spritesheet_hash_get(const Element& spritesheet, std::uint64_t imageKey) { - std::size_t seed{}; - auto hash_combine = [&](std::size_t value) { seed ^= value + HASH_COMBINE_CONSTANT + (seed << 6) + (seed >> 2); }; - - hash_combine(std::hash{}(texture ? texture->size.x : 0)); - hash_combine(std::hash{}(texture ? texture->size.y : 0)); - hash_combine(std::hash{}(texture && texture->isLinear)); - hash_combine(std::hash{}(path::to_utf8(spritesheet.path))); - auto isPixels = texture && !texture->pixels.empty(); - hash_combine(isPixels ? std::hash{}(std::string_view( - reinterpret_cast(texture->pixels.data()), texture->pixels.size())) - : 0); - return static_cast(seed); + auto seed = std::hash{}(path::to_utf8(spritesheet.path)); + return seed ^ (imageKey + HASH_COMBINE_CONSTANT + (seed << 6) + (seed >> 2)); } Reference item_reference_get(Reference reference) @@ -272,7 +259,8 @@ namespace anm2ed auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id); if (!spritesheet) return false; util::WorkingDirectory workingDirectory(directory_get()); - document::resource_load(id, spritesheet->path, textures, texturePaths); + texture_set(id, resource::Image(path::case_insensitive_find(spritesheet->path))); + texturePaths[id] = spritesheet->path; return true; } @@ -281,7 +269,8 @@ namespace anm2ed auto sound = anm2.element_get(ElementType::SOUND_ELEMENT, id); if (!sound) return false; util::WorkingDirectory workingDirectory(directory_get()); - document::resource_load(id, sound->path, sounds, soundPaths); + sound_set(id, resource::AudioData(path::case_insensitive_find(sound->path))); + soundPaths[id] = sound->path; return true; } @@ -335,8 +324,13 @@ namespace anm2ed void Document::assets_sync(ChangeType type) { if (type == ALL || type == SPRITESHEETS || type == TEXTURES) - document::resources_sync(*this, ElementType::SPRITESHEET, textures, texturePaths); - if (type == ALL || type == SOUNDS) document::resources_sync(*this, ElementType::SOUND_ELEMENT, sounds, soundPaths); + document::resources_sync(*this, ElementType::SPRITESHEET, textures, texturePaths, + [&](int id, const std::filesystem::path& path) + { texture_set(id, resource::Image(path)); }); + if (type == ALL || type == SOUNDS) + document::resources_sync(*this, ElementType::SOUND_ELEMENT, sounds, soundPaths, + [&](int id, const std::filesystem::path& path) + { sound_set(id, resource::AudioData(path)); }); if (type != ALL && type != SHADERS) return; std::set validShaderIds{}; @@ -368,18 +362,35 @@ namespace anm2ed std::erase_if(shaderFragmentPaths, is_invalid); } - resource::Image* Document::texture_get(int id) + const resource::Image* Document::texture_get(int id) const { - auto it = textures.find(id); - return it == textures.end() ? nullptr : &it->second; + if (auto draft = textureDrafts.find(id); draft != textureDrafts.end()) return &draft->second; + auto key = textures.find(id); + return key == textures.end() ? nullptr : assets.image_get(key->second); } - resource::AudioData* Document::sound_get(int id) + // A private copy of the spritesheet's image to draw on; it becomes a new asset on the next change(). + resource::Image* Document::texture_edit(int id) { - auto it = sounds.find(id); - return it == sounds.end() ? nullptr : &it->second; + auto texture = texture_get(id); + if (!texture) return nullptr; + return &textureDrafts.try_emplace(id, *texture).first->second; } + void Document::texture_set(int id, resource::Image image) + { + textureDrafts.erase(id); + textures[id] = assets.image_add(std::move(image)); + } + + const resource::AudioData* Document::sound_get(int id) const + { + auto key = sounds.find(id); + return key == sounds.end() ? nullptr : assets.audio_get(key->second); + } + + void Document::sound_set(int id, resource::AudioData data) { sounds[id] = assets.audio_add(std::move(data)); } + resource::Shader* Document::shader_get(int shaderId) { auto it = shaders.find(shaderId); @@ -498,7 +509,7 @@ namespace anm2ed } } - textures[id] = resource::Image(packedPixels.data(), packedSize); + texture_set(id, resource::Image(packedPixels.data(), packedSize)); for (auto& region : spritesheet->children) if (region.type == ElementType::REGION && crops.contains(region.id)) region.crop = crops.at(region.id); @@ -554,7 +565,7 @@ namespace anm2ed regionIdMap[id][sourceRegionId] = sourceRegion.id; } } - textures[baseId] = std::move(mergedTexture); + texture_set(baseId, std::move(mergedTexture)); std::unordered_map layerSpritesheetBefore{}; if (auto layers = anm2.element_get(ElementType::LAYERS)) @@ -614,7 +625,8 @@ namespace anm2ed if (spritesheet.type == ElementType::SPRITESHEET) { validIds.insert(spritesheet.id); - auto currentHash = document::spritesheet_hash_get(spritesheet, texture_get(spritesheet.id)); + auto currentHash = document::spritesheet_hash_get( + spritesheet, textures.contains(spritesheet.id) ? textures.at(spritesheet.id) : 0); spritesheetHashes[spritesheet.id] = currentHash; spritesheetSaveHashes.try_emplace(spritesheet.id, currentHash); } @@ -626,6 +638,8 @@ namespace anm2ed void Document::change(ChangeType type) { + for (auto& [id, draft] : std::exchange(textureDrafts, {})) + textures[id] = assets.image_add(std::move(draft)); hash_set(); assets_sync(type); @@ -712,7 +726,7 @@ namespace anm2ed index = UidIndex(anm2); } - void Document::edit_begin(StringType label, bool isTextures) { snapshots.push(localize.get(label), isTextures); } + void Document::edit_begin(StringType label) { snapshots.push(localize.get(label)); } edit::Uids Document::edit_run(StringType label, ChangeType type, const std::function& operation) { @@ -739,7 +753,7 @@ namespace anm2ed auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, id); if (!spritesheet) return; assets_sync(TEXTURES); - spritesheetHashes[id] = document::spritesheet_hash_get(*spritesheet, texture_get(id)); + spritesheetHashes[id] = document::spritesheet_hash_get(*spritesheet, textures.contains(id) ? textures.at(id) : 0); } void Document::spritesheet_hash_set_saved(int id) @@ -914,7 +928,7 @@ namespace anm2ed auto& element = items->children.emplace_back(element_make(ElementType::SPRITESHEET)); element.id = element_child_next_id_get(*items, ElementType::SPRITESHEET); element.path = relativePath; - textures[element.id] = std::move(texture); + texture_set(element.id, std::move(texture)); texturePaths[element.id] = element.path; added.insert(element.id); spritesheet.reference = element.id; @@ -940,7 +954,8 @@ namespace anm2ed element.id = element_child_next_id_get(*items, ElementType::SOUND_ELEMENT); element.path = path::backslash_replace(path::make_relative(path::backslash_handle(path), directory_get())); WorkingDirectory workingDirectory(directory_get()); - document::resource_load(element.id, element.path, sounds, soundPaths); + sound_set(element.id, resource::AudioData(path::case_insensitive_find(element.path))); + soundPaths[element.id] = element.path; added.insert(element.id); sound.reference = element.id; toast_log(Level::INFO, TOAST_SOUND_INITIALIZED, element.id, path::to_utf8(element.path)); diff --git a/src/document.hpp b/src/document.hpp index 0e08271..26bb6aa 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -9,6 +9,7 @@ #include #include +#include "asset_store.hpp" #include "edit/edit.hpp" #include "selection.hpp" #include "snapshots.hpp" @@ -32,6 +33,8 @@ namespace anm2ed Snapshots snapshots{}; std::map regionBySpritesheet{}; Storage animation{}; + resource::AssetStore assets{}; + std::map textureDrafts{}; int changeAllFramePropertiesRegionId{-1}; int changeAllFramePropertiesShaderId{-1}; @@ -97,7 +100,6 @@ namespace anm2ed Snapshot& current = snapshots.current; #define X(type, name) type& name = current.name; SNAPSHOT_STEP_STATE_FIELDS - SNAPSHOT_STEP_RESOURCE_FIELDS #undef X Anm2& anm2 = current.anm2; std::string& message = current.message; @@ -115,8 +117,11 @@ namespace anm2ed bool texture_reload(int); bool sound_reload(int); bool shader_reload(int, std::string* = nullptr); - resource::Image* texture_get(int); - resource::AudioData* sound_get(int); + const resource::Image* texture_get(int) const; + resource::Image* texture_edit(int); + void texture_set(int, resource::Image); + const resource::AudioData* sound_get(int) const; + void sound_set(int, resource::AudioData); resource::Shader* shader_get(int); bool regions_trim(int, const std::set&); bool spritesheet_pack(int, int); @@ -124,7 +129,7 @@ namespace anm2ed void hash_set(); void clean(); void change(ChangeType); - void edit_begin(StringType, bool = false); + void edit_begin(StringType); edit::Uids edit_run(StringType, ChangeType, const std::function&); // Snapshots for undo, runs the operation on the model, then commits; void operations select nothing. diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index 5714251..454b8c5 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -210,7 +210,13 @@ namespace anm2ed::imgui return document.sound_get(soundID) ? soundID : -1; } - bool render_audio_stream_generate(AudioStream& audioStream, std::map& sounds, + void sounds_detach(const Document& document, MIX_Mixer* mixer) + { + for (auto& [id, _] : document.sounds) + if (auto sound = document.sound_get(id)) audio::track_detach(*sound, mixer); + } + + bool render_audio_stream_generate(AudioStream& audioStream, const Document& document, const std::vector& frameSoundIDs, int fps) { audioStream.stream.clear(); @@ -229,7 +235,7 @@ namespace anm2ed::imgui for (auto soundID : frameSoundIDs) { - if (soundID != -1 && sounds.contains(soundID)) audio::play(sounds.at(soundID), false, mixer); + if (auto sound = document.sound_get(soundID)) audio::play(*sound, false, mixer); sampleFrameAccumulator += framesPerStep; auto sampleFramesToGenerate = (int)std::floor(sampleFrameAccumulator); @@ -239,8 +245,7 @@ namespace anm2ed::imgui frameBuffer.resize((std::size_t)sampleFramesToGenerate * (std::size_t)channels); if (!MIX_Generate(mixer, frameBuffer.data(), (int)(frameBuffer.size() * sizeof(float)))) { - for (auto& [_, sound] : sounds) - audio::track_detach(sound, mixer); + sounds_detach(document, mixer); MIX_DestroyMixer(mixer); audioStream.stream.clear(); return false; @@ -249,8 +254,7 @@ namespace anm2ed::imgui audioStream.stream.insert(audioStream.stream.end(), frameBuffer.begin(), frameBuffer.end()); } - for (auto& [_, sound] : sounds) - audio::track_detach(sound, mixer); + sounds_detach(document, mixer); MIX_DestroyMixer(mixer); return true; } @@ -268,8 +272,8 @@ namespace anm2ed::imgui auto stop_all_sounds = [&]() { - for (auto& [_, sound] : document.sounds) - audio::stop(sound, mixer); + for (auto& [id, _] : document.sounds) + if (auto sound = document.sound_get(id)) audio::stop(*sound, mixer); }; if (manager.isRecording) @@ -367,7 +371,7 @@ namespace anm2ed::imgui { if (settings.timelineIsSound && type != render::GIF) { - if (!render_audio_stream_generate(audioStream, document.sounds, renderFrameSoundIDs, renderFrameRate)) + if (!render_audio_stream_generate(audioStream, document, renderFrameSoundIDs, renderFrameRate)) { toasts.push(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED)); logger.error("Failed to generate deterministic render audio stream; exporting without audio."); diff --git a/src/imgui/window/regions.cpp b/src/imgui/window/regions.cpp index af91b0e..c2d2187 100644 --- a/src/imgui/window/regions.cpp +++ b/src/imgui/window/regions.cpp @@ -133,7 +133,7 @@ namespace anm2ed::imgui if (options.isMakeSpritesheet || options.isRemoveCurrent) { - document.edit_begin(EDIT_EXPORT_REGION, true); + document.edit_begin(EDIT_EXPORT_REGION); if (options.isMakeSpritesheet) { @@ -150,7 +150,7 @@ namespace anm2ed::imgui exported.children.push_back(exportedRegion); spritesheets->children.push_back(exported); - document.textures[exported.id] = Image(pixels.data(), exportSize); + document.texture_set(exported.id, Image(pixels.data(), exportSize)); document.texturePaths[exported.id] = exported.path; document.spritesheet.reference = exported.id; document.spritesheet.selection = {exported.id}; diff --git a/src/imgui/window/spritesheet_editor.cpp b/src/imgui/window/spritesheet_editor.cpp index 87b3074..d43aebb 100644 --- a/src/imgui/window/spritesheet_editor.cpp +++ b/src/imgui/window/spritesheet_editor.cpp @@ -359,10 +359,10 @@ namespace anm2ed::imgui auto fraction = current - glm::floor(current); return glm::floor(value) + fraction; }; - auto edit_begin_push = [&](StringType label, bool isTextures = false) + auto edit_begin_push = [&](StringType label) { - manager.command_push({manager.selected, [label, isTextures](Manager&, Document& document) - { document.edit_begin(label, isTextures); }}); + manager.command_push( + {manager.selected, [label](Manager&, Document& document) { document.edit_begin(label); }}); }; auto document_change_push = [&](Document::ChangeType changeType) { @@ -542,9 +542,8 @@ namespace anm2ed::imgui auto queuedSpritesheet = referenceSpritesheet; manager.command_push({manager.selected, [=](Manager&, Document& document) { - auto texture = document.texture_get(queuedSpritesheet); - if (!texture) return; - texture->pixel_line(start, end, color); + if (auto texture = document.texture_edit(queuedSpritesheet)) + texture->pixel_line(start, end, color); }}); }; auto texture_change_push = [&]() @@ -819,7 +818,7 @@ namespace anm2ed::imgui { if (!texture) break; auto color = useTool == tool::DRAW ? toolColor : vec4(); - if (isMouseClicked) edit_begin_push(useTool == tool::DRAW ? EDIT_DRAW : EDIT_ERASE, true); + if (isMouseClicked) edit_begin_push(useTool == tool::DRAW ? EDIT_DRAW : EDIT_ERASE); if (isMouseDown) texture_line_apply(ivec2(previousTextureMousePos), ivec2(textureMousePos), color); if (isMouseReleased) texture_change_push(); break; diff --git a/src/imgui/window/spritesheets.cpp b/src/imgui/window/spritesheets.cpp index 1ad60f2..97d2f42 100644 --- a/src/imgui/window/spritesheets.cpp +++ b/src/imgui/window/spritesheets.cpp @@ -80,7 +80,7 @@ namespace anm2ed::imgui void spritesheet_textures_edit(Document& document, StringType edit, Document::ChangeType changeType, const std::function& behavior, StringType successToast, StringType failToast) { - document.edit_begin(edit, true); + document.edit_begin(edit); auto isSuccess = behavior(); toast_log(isSuccess ? Level::INFO : Level::ERROR, isSuccess ? successToast : failToast); document.change(changeType); diff --git a/src/imgui/window/window.hpp b/src/imgui/window/window.hpp index 388bd87..7c626d5 100644 --- a/src/imgui/window/window.hpp +++ b/src/imgui/window/window.hpp @@ -66,7 +66,7 @@ namespace anm2ed::imgui struct WindowCardImage { - resource::Image* texture{}; + const resource::Image* texture{}; glm::vec2 size{}; glm::vec2 uvMin{}; glm::vec2 uvMax{1.0f}; diff --git a/src/resource/asset_store.cpp b/src/resource/asset_store.cpp new file mode 100644 index 0000000..57874ca --- /dev/null +++ b/src/resource/asset_store.cpp @@ -0,0 +1,52 @@ +#include "asset_store.hpp" + +#include + +namespace anm2ed::resource +{ + constexpr std::uint64_t FNV_OFFSET = 14695981039346656037ull; + constexpr std::uint64_t FNV_PRIME = 1099511628211ull; + + std::uint64_t bytes_hash(std::span bytes, std::uint64_t hash = FNV_OFFSET) + { + for (auto byte : bytes) + hash = (hash ^ byte) * FNV_PRIME; + return hash; + } + + template std::span value_bytes_get(const Value& value) + { + return {reinterpret_cast(&value), sizeof(value)}; + } + + // Content hash; 0 stays free to mean "no asset". + std::uint64_t hash_finish(std::uint64_t hash) { return hash ? hash : 1; } + + std::uint64_t AssetStore::image_add(Image image) + { + auto hash = bytes_hash(value_bytes_get(image.size)); + hash = bytes_hash(value_bytes_get(image.isLinear), hash); + hash = hash_finish(bytes_hash(image.pixels, hash)); + images.try_emplace(hash, std::move(image)); + return hash; + } + + std::uint64_t AssetStore::audio_add(AudioData data) + { + auto hash = hash_finish(bytes_hash(data.bytes)); + audio.try_emplace(hash, std::move(data)); + return hash; + } + + const Image* AssetStore::image_get(std::uint64_t hash) const + { + auto it = images.find(hash); + return it == images.end() ? nullptr : &it->second; + } + + const AudioData* AssetStore::audio_get(std::uint64_t hash) const + { + auto it = audio.find(hash); + return it == audio.end() ? nullptr : &it->second; + } +} diff --git a/src/resource/asset_store.hpp b/src/resource/asset_store.hpp new file mode 100644 index 0000000..a3e000c --- /dev/null +++ b/src/resource/asset_store.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +#include "audio_data.hpp" +#include "image.hpp" + +namespace anm2ed::resource +{ + // Immutable images and sounds keyed by content hash. Undo history records keys, never pixels or bytes, and + // entries live until the document closes so any step can be restored. + class AssetStore + { + std::unordered_map images{}; + std::unordered_map audio{}; + + public: + std::uint64_t image_add(Image); + std::uint64_t audio_add(AudioData); + const Image* image_get(std::uint64_t) const; + const AudioData* audio_get(std::uint64_t) const; + }; +} diff --git a/src/snapshots.cpp b/src/snapshots.cpp index 54523d9..985776e 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -26,51 +26,12 @@ namespace anm2ed::snapshots left.references == right.references; } - bool is_texture_equal(const resource::Image& left, const resource::Image& right) - { - return left.uid == right.uid || - (left.size == right.size && left.isLinear == right.isLinear && left.pixels == right.pixels); - } - - bool is_audio_equal(const resource::AudioData& left, const resource::AudioData& right) - { - return left.is_valid() == right.is_valid(); - } - - template - bool is_map_equal(const std::map& left, const std::map& right, Compare compare) - { - if (left.size() != right.size()) return false; - - auto leftIt = left.begin(); - auto rightIt = right.begin(); - while (leftIt != left.end() && rightIt != right.end()) - { - if (leftIt->first != rightIt->first) return false; - if (!compare(leftIt->second, rightIt->second)) return false; - ++leftIt; - ++rightIt; - } - - return leftIt == left.end() && rightIt == right.end(); - } - template bool is_value_equal(const T& left, const T& right) { return left == right; } bool is_value_equal(const Playback& left, const Playback& right) { return is_playback_equal(left, right); } bool is_value_equal(const Storage& left, const Storage& right) { return is_storage_equal(left, right); } - bool is_value_equal(const std::map& left, const std::map& right) - { - return is_map_equal(left, right, is_texture_equal); - } - - bool is_value_equal(const std::map& left, const std::map& right) - { - return is_map_equal(left, right, is_audio_equal); - } - bool is_element_shallow_equal(const Element& left, const Element& right) { return left.type == right.type && left.tag == right.tag && left.name == right.name && @@ -202,7 +163,6 @@ namespace anm2ed #define X(type, name) \ if (this->name) return false; SNAPSHOT_STEP_STATE_FIELDS - SNAPSHOT_STEP_RESOURCE_FIELDS #undef X return true; @@ -216,7 +176,6 @@ namespace anm2ed #define X(type, name) \ if (this->name) snapshot.name = direction == SnapshotStepDirection::UNDO ? this->name->undo : this->name->redo; SNAPSHOT_STEP_STATE_FIELDS - SNAPSHOT_STEP_RESOURCE_FIELDS #undef X } @@ -268,12 +227,7 @@ namespace anm2ed pendingStep = step.is_empty() ? std::nullopt : std::optional(std::move(step)); } - void Snapshots::push(const std::string& message, bool isTextures) - { - auto step = snapshots::step_anm2_make(current); - if (isTextures) step.textures = SnapshotStepValue{current.textures, {}}; - step_push(message, std::move(step)); - } + void Snapshots::push(const std::string& message) { step_push(message, snapshots::step_anm2_make(current)); } void Snapshots::commit() { @@ -321,7 +275,6 @@ namespace anm2ed if (snapshots::is_value_equal(step.name->undo, step.name->redo)) step.name.reset(); \ } SNAPSHOT_STEP_STATE_FIELDS - SNAPSHOT_STEP_RESOURCE_FIELDS #undef X if (step.is_empty()) return; diff --git a/src/snapshots.hpp b/src/snapshots.hpp index bb2107f..728748c 100644 --- a/src/snapshots.hpp +++ b/src/snapshots.hpp @@ -7,8 +7,6 @@ #include #include "anm2/anm2.hpp" -#include "audio_data.hpp" -#include "image.hpp" #include "playback.hpp" #include "selection.hpp" #include "storage.hpp" @@ -61,16 +59,15 @@ namespace anm2ed Storage shader{}; Storage sound{}; Storage spritesheet{}; - std::map textures{}; - std::map sounds{}; + std::map textures{}; + std::map sounds{}; Anm2 anm2{}; Selection selection{}; float frameTime{}; std::string message = snapshots::ACTION; }; - using SnapshotTextureMap = std::map; - using SnapshotSoundMap = std::map; + using AssetKeys = std::map; #define SNAPSHOT_STEP_STATE_FIELDS \ X(Playback, playback) \ @@ -83,11 +80,9 @@ namespace anm2ed X(Storage, sound) \ X(Storage, spritesheet) \ X(Selection, selection) \ - X(float, frameTime) - -#define SNAPSHOT_STEP_RESOURCE_FIELDS \ - X(SnapshotTextureMap, textures) \ - X(SnapshotSoundMap, sounds) + X(float, frameTime) \ + X(AssetKeys, textures) \ + X(AssetKeys, sounds) struct SnapshotStep { @@ -96,7 +91,6 @@ namespace anm2ed #define X(type, name) std::optional> name{}; SNAPSHOT_STEP_STATE_FIELDS - SNAPSHOT_STEP_RESOURCE_FIELDS #undef X bool is_empty() const; @@ -131,7 +125,7 @@ namespace anm2ed std::optional pendingStep{}; std::optional pendingFocus{}; - void push(const std::string&, bool = false); + void push(const std::string&); void step_push(const std::string&, SnapshotStep); void commit(); bool undo(); diff --git a/tests/assets.cpp b/tests/assets.cpp index eb7dbf8..9aec2a6 100644 --- a/tests/assets.cpp +++ b/tests/assets.cpp @@ -1,5 +1,6 @@ #include "common.hpp" +#include "asset_store.hpp" #include "audio_data.hpp" #include "image.hpp" @@ -39,3 +40,21 @@ TEST_CASE("AudioData holds file bytes") CHECK(audio.uid != 0); CHECK_FALSE(AudioData(std::filesystem::path("missing.wav")).is_valid()); } + +TEST_CASE("AssetStore keys assets by content") +{ + AssetStore store{}; + const std::vector pixels{1, 2, 3, 4}; + auto key = store.image_add(Image(pixels.data(), {1, 1})); + CHECK(key != 0); + CHECK(store.image_add(Image(pixels.data(), {1, 1})) == key); + CHECK(store.image_add(Image(pixels.data(), {1, 1}, true)) != key); + REQUIRE(store.image_get(key)); + CHECK(store.image_get(key)->pixels == pixels); + CHECK_FALSE(store.image_get(key + 1)); + + const unsigned char bytes[] = {'O', 'g', 'g', 'S'}; + auto audioKey = store.audio_add(AudioData(bytes, sizeof(bytes))); + CHECK(store.audio_add(AudioData(bytes, sizeof(bytes))) == audioKey); + CHECK(store.audio_get(audioKey)->bytes.size() == sizeof(bytes)); +}