Autosave edited spritesheets beside their files

With document autosave, spritesheets with unsaved pixel edits are written
to a hidden .<name>.png.autosave beside their PNG (only when changed since
their last autosave; a spritesheet edit alone also triggers the document
autosave). Restoring a document's autosave brings them back as unsaved
edits without touching the PNGs. The copies are removed when the
spritesheet is saved or reloaded, or the document's autosave is discarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 21:28:02 -04:00
1 parent ded95f436a
commit ce3b544392
4 files changed
+66 -3

No files matched your search

+56 -1
View File
@@ -219,6 +219,13 @@ namespace anm2ed
auto autosavePathUtf8 = path::to_utf8(autosavePath);
if (model::model_save(model, autosavePath, errorString, options))
{
for (auto id : spritesheets_autosave_dirty_get())
{
auto spritesheet = model::item_get(model.content.spritesheets, id);
auto texture = texture_get(id);
if (texture && texture->write_png(spritesheet_autosave_path_get(*spritesheet)))
spritesheetAutosaveHashes[id] = spritesheetHashes.at(id);
}
autosaveHash = hash;
lastAutosaveTime = 0.0f;
toast_log(Level::INFO, TOAST_AUTOSAVING);
@@ -463,7 +470,49 @@ namespace anm2ed
}
bool Document::is_dirty() const { return hash != saveHash; }
bool Document::is_autosave_dirty() const { return hash != autosaveHash; }
bool Document::is_autosave_dirty() { return hash != autosaveHash || !spritesheets_autosave_dirty_get().empty(); }
// Edited spritesheets are autosaved as a hidden copy beside their file: .<name>.png.autosave.
std::filesystem::path Document::spritesheet_autosave_path_get(const model::Spritesheet& spritesheet)
{
auto path = directory_get() / spritesheet.path;
return path.parent_path() /
path::from_utf8("." + path::to_utf8(path.filename()) + std::string(document::AUTOSAVE_EXTENSION));
}
// Spritesheets with unsaved pixel edits that their autosave copy doesn't have yet.
std::set<int> Document::spritesheets_autosave_dirty_get()
{
std::set<int> ids{};
for (const auto& spritesheet : model.content.spritesheets)
{
auto id = spritesheet.id;
auto autosaveHash = spritesheetAutosaveHashes.find(id);
if (spritesheet_is_dirty(id) &&
(autosaveHash == spritesheetAutosaveHashes.end() || autosaveHash->second != spritesheetHashes.at(id)))
ids.insert(id);
}
return ids;
}
// After a crash, autosaved spritesheets come back as unsaved edits; their files are left as they are.
void Document::spritesheets_autosave_restore()
{
for (const auto& spritesheet : model.content.spritesheets)
{
auto path = spritesheet_autosave_path_get(spritesheet);
if (!path::is_exist(path)) continue;
if (auto image = resource::Image(path); image.is_valid()) texture_set(spritesheet.id, std::move(image));
}
}
void Document::spritesheets_autosave_clear()
{
std::error_code ec{};
for (const auto& spritesheet : model.content.spritesheets)
std::filesystem::remove(spritesheet_autosave_path_get(spritesheet), ec);
spritesheetAutosaveHashes.clear();
}
void Document::spritesheet_hash_update(int id)
{
@@ -480,6 +529,12 @@ namespace anm2ed
if (spritesheetHashes.contains(id)) spritesheetSaveHashes[id] = spritesheetHashes[id];
textureWriteTimes.erase(id);
spritesheets_changed_get();
if (auto spritesheet = model::item_get(model.content.spritesheets, id))
{
std::error_code ec{};
std::filesystem::remove(spritesheet_autosave_path_get(*spritesheet), ec);
}
spritesheetAutosaveHashes.erase(id);
}
// Spritesheets whose file changed on disk since it was last loaded or saved; ones with unsaved edits are left alone.
+6 -1
View File
@@ -57,6 +57,7 @@ namespace anm2ed
bool isForceDirty{false};
std::unordered_map<int, uint64_t> spritesheetHashes{};
std::unordered_map<int, uint64_t> spritesheetSaveHashes{};
std::unordered_map<int, uint64_t> spritesheetAutosaveHashes{};
std::unordered_map<int, std::filesystem::path> texturePaths{};
std::unordered_map<int, std::filesystem::file_time_type> textureWriteTimes{};
std::unordered_map<int, std::filesystem::path> soundPaths{};
@@ -135,7 +136,7 @@ namespace anm2ed
}
std::vector<Reference> references_get(const edit::Uids&) const;
bool is_dirty() const;
bool is_autosave_dirty() const;
bool is_autosave_dirty();
std::filesystem::path directory_get() const;
std::filesystem::path filename_get() const;
bool is_valid() const;
@@ -147,6 +148,10 @@ namespace anm2ed
void spritesheet_hashes_sync();
std::set<int> spritesheets_changed_get();
void spritesheets_reload(const std::set<int>&);
std::filesystem::path spritesheet_autosave_path_get(const model::Spritesheet&);
std::set<int> spritesheets_autosave_dirty_get();
void spritesheets_autosave_restore();
void spritesheets_autosave_clear();
Reference reference_get() const;
void reference_set(Reference);
std::set<Reference> selected_get(SelectionKind) const;
+2 -1
View File
@@ -147,7 +147,8 @@ namespace anm2ed::imgui
for (auto i = 0; i < (int)manager.documents.size(); ++i)
{
auto& document = manager.documents[i];
auto isDirty = (document.is_dirty() || document.isForceDirty) && document.is_autosave_dirty();
auto isDirty = (document.is_dirty() || document.isForceDirty || document.spritesheet_any_dirty()) &&
document.is_autosave_dirty();
if (isDirty)
{
document.lastAutosaveTime += ImGui::GetIO().DeltaTime;
+2
View File
@@ -206,6 +206,7 @@ namespace anm2ed
auto autosavePath = document->autosave_path_get();
autosaveFiles.erase(std::remove(autosaveFiles.begin(), autosaveFiles.end(), autosavePath), autosaveFiles.end());
autosave_file_remove(autosavePath);
document->spritesheets_autosave_clear();
autosave_files_write();
}
@@ -343,6 +344,7 @@ namespace anm2ed
{
document->isForceDirty = true;
document->path = document->path_from_autosave_get(path);
document->spritesheets_autosave_restore();
document->change();
}
}