[olympic/studio] Fix NewMenu not to overwrite existing files or create a file without name
All checks were successful
Build / build (push) Successful in 2m30s

This commit is contained in:
Gary Talent 2024-01-18 00:24:11 -06:00
parent 2bc2003caa
commit e367575974
2 changed files with 21 additions and 7 deletions

View File

@ -57,7 +57,7 @@ void NewMenu::addItemMaker(ox::UniquePtr<studio::ItemMaker> &&im) noexcept {
std::sort(
m_types.begin(), m_types.end(),
[](ox::UPtr<ItemMaker> const&im1, ox::UPtr<ItemMaker> 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<int>(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<std::size_t>(m_selectedType)]->write(ctx, m_itemName);
if (m_itemName.len() == 0) {
return;
}
auto const sctx = turbine::applicationData<studio::StudioContext>(ctx);
auto const&typeMaker = *m_types[static_cast<std::size_t>(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;

View File

@ -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<ox::String> write(turbine::Context &ctx, ox::CRStringView pName) const noexcept override {
auto const path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt);
auto sctx = turbine::applicationData<studio::StudioContext>(ctx);
auto const path = itemPath(pName);
auto const sctx = turbine::applicationData<studio::StudioContext>(ctx);
keel::createUuidMapping(keelCtx(ctx), path, ox::UUID::generate().unwrap());
oxReturnError(sctx->project->writeObj(path, m_item, m_fmt));
return path;