diff --git a/src/olympic/studio/applib/src/newmenu.cpp b/src/olympic/studio/applib/src/newmenu.cpp index 8755ce40..21897b22 100644 --- a/src/olympic/studio/applib/src/newmenu.cpp +++ b/src/olympic/studio/applib/src/newmenu.cpp @@ -57,7 +57,7 @@ void NewMenu::addItemMaker(ox::UniquePtr &&im) noexcept { std::sort( m_types.begin(), m_types.end(), [](ox::UPtr const&im1, ox::UPtr const&im2) { - return im1->name < im2->name; + return im1->typeName < im2->typeName; }); } @@ -65,7 +65,7 @@ void NewMenu::drawNewItemType(turbine::Context &ctx) noexcept { drawWindow(ctx, &m_open, [this] { auto items = ox_malloca(m_types.size() * sizeof(char const*), char const*, nullptr); for (auto i = 0u; auto const&im : m_types) { - items.get()[i] = im->name.c_str(); + items.get()[i] = im->typeName.c_str(); ++i; } ImGui::ListBox("Item Type", &m_selectedType, items.get(), static_cast(m_types.size())); @@ -115,7 +115,16 @@ void NewMenu::drawLastPageButtons(turbine::Context &ctx) noexcept { } void NewMenu::finish(turbine::Context &ctx) noexcept { - auto const [path, err] = m_types[static_cast(m_selectedType)]->write(ctx, m_itemName); + if (m_itemName.len() == 0) { + return; + } + auto const sctx = turbine::applicationData(ctx); + auto const&typeMaker = *m_types[static_cast(m_selectedType)]; + if (sctx->project->exists(typeMaker.itemPath(m_itemName))) { + oxLogError(OxError(1, "New file error: File already exists")); + return; + } + auto const [path, err] = typeMaker.write(ctx, m_itemName); if (err) { oxLogError(err); return; diff --git a/src/olympic/studio/modlib/include/studio/itemmaker.hpp b/src/olympic/studio/modlib/include/studio/itemmaker.hpp index e51b738a..2102f3d0 100644 --- a/src/olympic/studio/modlib/include/studio/itemmaker.hpp +++ b/src/olympic/studio/modlib/include/studio/itemmaker.hpp @@ -15,16 +15,21 @@ namespace studio { class ItemMaker { public: - ox::String const name; + ox::String const typeName; ox::String const parentDir; ox::String const fileExt; constexpr explicit ItemMaker(ox::StringView pName, ox::StringView pParentDir, ox::CRStringView pFileExt) noexcept: - name(pName), + typeName(pName), parentDir(pParentDir), fileExt(pFileExt) { } virtual ~ItemMaker() noexcept = default; + [[nodiscard]] + inline virtual ox::String itemPath(ox::StringView pName) const noexcept { + return ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt); + } + /** * Returns path of the file created. * @param ctx @@ -69,8 +74,8 @@ class ItemMakerT: public ItemMaker { m_fmt(pFmt) { } ox::Result write(turbine::Context &ctx, ox::CRStringView pName) const noexcept override { - auto const path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt); - auto sctx = turbine::applicationData(ctx); + auto const path = itemPath(pName); + auto const sctx = turbine::applicationData(ctx); keel::createUuidMapping(keelCtx(ctx), path, ox::UUID::generate().unwrap()); oxReturnError(sctx->project->writeObj(path, m_item, m_fmt)); return path;