From 4a2b1fd743f87930798e5b81114c64a4b38e4142 Mon Sep 17 00:00:00 2001
From: Gary Talent <gary@drinkingtea.net>
Date: Wed, 22 May 2024 23:17:28 -0500
Subject: [PATCH] [studio,keel] Make fileChanged emit UUID as well as path, add
 uuidUrlToUuid

---
 src/olympic/keel/include/keel/media.hpp                  | 4 ++++
 src/olympic/studio/modlib/include/studio/project.hpp     | 5 +++--
 src/olympic/studio/modlib/include/studio/undocommand.hpp | 5 ++++-
 src/olympic/studio/modlib/src/project.cpp                | 8 ++++----
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/olympic/keel/include/keel/media.hpp b/src/olympic/keel/include/keel/media.hpp
index 84912bbd..e7253750 100644
--- a/src/olympic/keel/include/keel/media.hpp
+++ b/src/olympic/keel/include/keel/media.hpp
@@ -95,6 +95,10 @@ ox::Error buildUuidMap(Context &ctx) noexcept;
 
 ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
 
+constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
+	return ox::UUID::fromString(substr(uuidUrl, 7));
+}
+
 ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
 
 ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
diff --git a/src/olympic/studio/modlib/include/studio/project.hpp b/src/olympic/studio/modlib/include/studio/project.hpp
index 2b2d9f34..33643e9f 100644
--- a/src/olympic/studio/modlib/include/studio/project.hpp
+++ b/src/olympic/studio/modlib/include/studio/project.hpp
@@ -115,7 +115,7 @@ class Project {
 		// file.
 		ox::Signal<ox::Error(ox::CRStringView)> fileRecognized;
 		ox::Signal<ox::Error(ox::CRStringView)> fileDeleted;
-		ox::Signal<ox::Error(ox::CRStringView)> fileUpdated;
+		ox::Signal<ox::Error(ox::StringView, ox::UUID)> fileUpdated;
 
 };
 
@@ -135,7 +135,8 @@ ox::Error Project::writeObj(ox::CRStringView path, T const&obj, ox::ClawFormat f
 		oxReturnError(writeTypeStore());
 	}
 	oxReturnError(keel::setAsset(m_ctx, path, obj));
-	fileUpdated.emit(path);
+	oxRequire(uuid, pathToUuid(m_ctx, path));
+	fileUpdated.emit(path, uuid);
 	return {};
 }
 
diff --git a/src/olympic/studio/modlib/include/studio/undocommand.hpp b/src/olympic/studio/modlib/include/studio/undocommand.hpp
index c42af98f..5d57cef1 100644
--- a/src/olympic/studio/modlib/include/studio/undocommand.hpp
+++ b/src/olympic/studio/modlib/include/studio/undocommand.hpp
@@ -4,13 +4,16 @@
 
 #pragma once
 
+#include <source_location>
+
 #include <ox/std/error.hpp>
 
 namespace studio {
 
 class NoChangesException: public ox::Exception {
 	public:
-		inline NoChangesException(): ox::Exception(OxError(1, "Command makes no changes.")) {}
+		inline NoChangesException(std::source_location sloc = std::source_location::current()):
+				ox::Exception(sloc.file_name(), sloc.line(), 1, "Command makes no changes.") {}
 };
 
 class UndoCommand {
diff --git a/src/olympic/studio/modlib/src/project.cpp b/src/olympic/studio/modlib/src/project.cpp
index 02b19acc..edbb7323 100644
--- a/src/olympic/studio/modlib/src/project.cpp
+++ b/src/olympic/studio/modlib/src/project.cpp
@@ -57,7 +57,7 @@ ox::FileSystem &Project::romFs() noexcept {
 
 ox::Error Project::mkdir(ox::CRStringView path) const noexcept {
 	oxReturnError(m_fs.mkdir(path, true));
-	fileUpdated.emit(path);
+	fileUpdated.emit(path, {});
 	return {};
 }
 
@@ -115,9 +115,9 @@ ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexc
 	ox::Buffer outBuff;
 	outBuff.reserve(buff.size() + HdrSz);
 	ox::BufferWriter writer(&outBuff);
-	auto const [uuid, err] = m_ctx.pathToUuid.at(path);
+	auto const [uuid, err] = pathToUuid(m_ctx, path);
 	if (!err) {
-		oxReturnError(keel::writeUuidHeader(writer, *uuid));
+		oxReturnError(keel::writeUuidHeader(writer, uuid));
 	}
 	oxReturnError(writer.write(buff.data(), buff.size()));
 	auto const newFile = m_fs.stat(path).error != 0;
@@ -126,7 +126,7 @@ ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexc
 		fileAdded.emit(path);
 		indexFile(path);
 	} else {
-		fileUpdated.emit(path);
+		fileUpdated.emit(path, uuid);
 	}
 	return {};
 }