[nostalgia] Fix the initialization of UUIDs for new assets
This commit is contained in:
		| @@ -19,7 +19,7 @@ ox::Result<ox::UUID> readUuidHeader(const ox::Buffer &buff) noexcept; | |||||||
| ox::Result<ox::UUID> readUuidHeader(const char *buff, std::size_t buffLen) noexcept; | ox::Result<ox::UUID> readUuidHeader(const char *buff, std::size_t buffLen) noexcept; | ||||||
|  |  | ||||||
| ox::Error writeUuidHeader(ox::Writer_c auto *writer, const ox::UUID &uuid) noexcept { | ox::Error writeUuidHeader(ox::Writer_c auto *writer, const ox::UUID &uuid) noexcept { | ||||||
| 	const auto hdr = ox::sfmt<ox::BString<40>>("N1{};", uuid.toString()); | 	const auto hdr = ox::sfmt<ox::BString<40>>("N1;{};", uuid.toString()); | ||||||
| 	return write(writer, hdr); | 	return write(writer, hdr); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -45,6 +45,11 @@ static void clearUuidMap(Context *ctx) noexcept { | |||||||
| 	ctx->pathToUuid.clear(); | 	ctx->pathToUuid.clear(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept { | ||||||
|  | 	ctx->pathToUuid[filePath] = uuid; | ||||||
|  | 	ctx->uuidToPath[uuid.toString()] = filePath; | ||||||
|  | } | ||||||
|  |  | ||||||
| static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { | static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { | ||||||
| 	oxRequire(files, ctx->rom->ls(path)); | 	oxRequire(files, ctx->rom->ls(path)); | ||||||
| 	for (const auto &f : files) { | 	for (const auto &f : files) { | ||||||
| @@ -52,9 +57,10 @@ static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { | |||||||
| 		oxRequire(stat, ctx->rom->stat(filePath)); | 		oxRequire(stat, ctx->rom->stat(filePath)); | ||||||
| 		if (stat.fileType == ox::FileType::NormalFile) { | 		if (stat.fileType == ox::FileType::NormalFile) { | ||||||
| 			oxRequire(data, ctx->rom->read(filePath)); | 			oxRequire(data, ctx->rom->read(filePath)); | ||||||
| 			oxRequire(hdr, readAssetHeader(data)); | 			const auto [hdr, err] = readAssetHeader(data); | ||||||
| 			ctx->pathToUuid[filePath] = hdr.uuid; | 			if (!err) { | ||||||
| 			ctx->uuidToPath[hdr.uuid.toString()] = filePath; | 				createUuidMapping(ctx, filePath, hdr.uuid); | ||||||
|  | 			} | ||||||
| 		} else if (stat.fileType == ox::FileType::Directory) { | 		} else if (stat.fileType == ox::FileType::Directory) { | ||||||
| 			if (!beginsWith(f, ".")) { | 			if (!beginsWith(f, ".")) { | ||||||
| 				oxReturnError(buildUuidMap(ctx, filePath)); | 				oxReturnError(buildUuidMap(ctx, filePath)); | ||||||
|   | |||||||
| @@ -91,6 +91,8 @@ ox::Result<foundation::AssetRef<T>> readObjNoCache( | |||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept; | ||||||
|  |  | ||||||
| ox::Error buildUuidMap(Context *ctx) noexcept; | ox::Error buildUuidMap(Context *ctx) noexcept; | ||||||
|  |  | ||||||
| template<typename T> | template<typename T> | ||||||
| @@ -135,7 +137,7 @@ ox::Error writeObj( | |||||||
|  |  | ||||||
| ox::Error setRomFs(Context *ctx, ox::UPtr<ox::FileSystem> fs) noexcept; | ox::Error setRomFs(Context *ctx, ox::UPtr<ox::FileSystem> fs) noexcept; | ||||||
|  |  | ||||||
| ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView assetId) noexcept; | ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView path) noexcept; | ||||||
|  |  | ||||||
| ox::Result<char*> loadRom(ox::CRStringView assetId = "") noexcept; | ox::Result<char*> loadRom(ox::CRStringView assetId = "") noexcept; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,13 +1,16 @@ | |||||||
| /* | /* | ||||||
|  * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. |  * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include <ox/claw/claw.hpp> | #include <ox/claw/claw.hpp> | ||||||
|  |  | ||||||
|  | #include <nostalgia/foundation/media.hpp> | ||||||
| #include <nostalgia/core/context.hpp> | #include <nostalgia/core/context.hpp> | ||||||
|  |  | ||||||
|  | #include "context.hpp" | ||||||
|  |  | ||||||
| namespace nostalgia::studio { | namespace nostalgia::studio { | ||||||
|  |  | ||||||
| class ItemMaker { | class ItemMaker { | ||||||
| @@ -45,8 +48,9 @@ class ItemMakerT: public ItemMaker { | |||||||
| 			fmt(pFmt) { | 			fmt(pFmt) { | ||||||
| 		} | 		} | ||||||
| 		ox::Error write(core::Context *ctx, ox::CRStringView pName) const noexcept override { | 		ox::Error write(core::Context *ctx, ox::CRStringView pName) const noexcept override { | ||||||
| 			const auto path = ox::sfmt("{}/{}.{}", parentDir, pName, fileExt); | 			const auto path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt); | ||||||
| 			auto sctx = core::applicationData<studio::StudioContext>(ctx); | 			auto sctx = core::applicationData<studio::StudioContext>(ctx); | ||||||
|  | 			foundation::createUuidMapping(ctx, path, ox::UUID::generate().unwrap()); | ||||||
| 			return sctx->project->writeObj(path, &item, fmt); | 			return sctx->project->writeObj(path, &item, fmt); | ||||||
| 		} | 		} | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -87,8 +87,10 @@ ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) | |||||||
| 	ox::Buffer outBuff; | 	ox::Buffer outBuff; | ||||||
| 	outBuff.reserve(buff.size() + HdrSz); | 	outBuff.reserve(buff.size() + HdrSz); | ||||||
| 	ox::BufferWriter writer(&outBuff); | 	ox::BufferWriter writer(&outBuff); | ||||||
| 	oxRequire(uuid, m_ctx->pathToUuid.at(path)); | 	const auto [uuid, err] =  m_ctx->pathToUuid.at(path); | ||||||
| 	oxReturnError(foundation::writeUuidHeader(&writer, *uuid)); | 	if (!err) { | ||||||
|  | 		oxReturnError(foundation::writeUuidHeader(&writer, *uuid)); | ||||||
|  | 	} | ||||||
| 	oxReturnError(writer.write(buff.data(), buff.size())); | 	oxReturnError(writer.write(buff.data(), buff.size())); | ||||||
| 	const auto newFile = m_fs->stat(path).error != 0; | 	const auto newFile = m_fs->stat(path).error != 0; | ||||||
| 	oxReturnError(m_fs->write(path, outBuff.data(), outBuff.size(), ox::FileType::NormalFile)); | 	oxReturnError(m_fs->write(path, outBuff.data(), outBuff.size(), ox::FileType::NormalFile)); | ||||||
|   | |||||||
| @@ -120,7 +120,7 @@ ox::Error Project::writeObj(const ox::String &path, const T *obj, ox::ClawFormat | |||||||
| 		// replace garbage last character with new line | 		// replace garbage last character with new line | ||||||
| 		typeOut.back().value = '\n'; | 		typeOut.back().value = '\n'; | ||||||
| 		// write to FS | 		// write to FS | ||||||
| 		const auto typePath = ox::sfmt("{}/{}", descPath, buildTypeId(*t)); | 		const auto typePath = ox::sfmt("/{}/{}", descPath, buildTypeId(*t)); | ||||||
| 		oxReturnError(writeBuff(typePath, typeOut)); | 		oxReturnError(writeBuff(typePath, typeOut)); | ||||||
| 	} | 	} | ||||||
| 	fileUpdated.emit(path); | 	fileUpdated.emit(path); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| /* | /* | ||||||
|  * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. |  * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <imgui.h> | #include <imgui.h> | ||||||
| @@ -108,7 +108,8 @@ void NewMenu::drawLastPageButtons(core::Context *ctx) noexcept { | |||||||
| } | } | ||||||
|  |  | ||||||
| void NewMenu::finish(core::Context *ctx) noexcept { | void NewMenu::finish(core::Context *ctx) noexcept { | ||||||
| 	const auto err = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, m_itemName); | 	const auto itemName = ox::String(m_itemName); | ||||||
|  | 	const auto err = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, itemName); | ||||||
| 	if (err) { | 	if (err) { | ||||||
| 		oxLogError(err); | 		oxLogError(err); | ||||||
| 		return; | 		return; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user