diff options
author | rtk0c <[email protected]> | 2022-04-18 20:59:31 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-18 20:59:31 -0700 |
commit | 7f871b04470766f0f5266cf949b65a54b7a6f79e (patch) | |
tree | 541b316fb564ffe12885586131a6ccd17662f662 /source/Ires.cpp | |
parent | 7af9992ca81c699bc1cf05eb691e284bf424f050 (diff) |
Changeset: 10 Add Uid for IresObject
Diffstat (limited to 'source/Ires.cpp')
-rw-r--r-- | source/Ires.cpp | 131 |
1 files changed, 83 insertions, 48 deletions
diff --git a/source/Ires.cpp b/source/Ires.cpp index 255c221..dde4ace 100644 --- a/source/Ires.cpp +++ b/source/Ires.cpp @@ -63,11 +63,19 @@ void IresObject::SetName(std::string name) { } void IresObject::ShowEditor(EditorInstance& editor) { + ImGui::Text("%s", ToString(mKind).data()); + bool isAnnoymous = mName.empty(); if (isAnnoymous) { - ImGui::Text("<Annoymous Ires at %p>", (void*)this); + ImGui::Text("Name: <annoymous: %p>", (void*)this); + } else { + ImGui::Text("Name: %s", mName.c_str()); + } + + if (mUid.IsNull()) { + ImGui::TextUnformatted("Uid: <none>"); } else { - ImGui::Text("%s", mName.c_str()); + ImGui::Text("Uid: %lx-%lx", mUid.upper, mUid.lower); } } @@ -76,6 +84,7 @@ void IresObject::WriteFull(IresObject* ires, rapidjson::Value& value, rapidjson: ires->Write(rvIres, root); value.AddMember("Type", rapidjson::StringRef(ToString(ires->GetKind())), root.GetAllocator()); + value.AddMember("Uid", ires->mUid.Write(root), root.GetAllocator()); value.AddMember("Value", rvIres, root.GetAllocator()); } @@ -86,6 +95,10 @@ std::unique_ptr<IresObject> IresObject::ReadFull(const rapidjson::Value& value) auto ires = Create(kind); if (!ires) return nullptr; + auto rvUid = rapidjson::GetProperty(value, rapidjson::kArrayType, "Uid"sv); + if (!rvUid) return nullptr; + ires->mUid.Read(*rvUid); + if (!ReadPartial(ires.get(), value)) { return nullptr; } @@ -100,6 +113,12 @@ bool IresObject::ReadPartial(IresObject* ires, const rapidjson::Value& value) { return true; } +void IresObject::Write(rapidjson::Value& value, rapidjson::Document& root) const { +} + +void IresObject::Read(const rapidjson::Value& value) { +} + void IresManager::DiscoverFilesDesignatedLocation() { auto path = AppConfig::assetDirPath / "Ires"; DiscoverFiles(path); @@ -116,39 +135,7 @@ void IresManager::DiscoverFiles(const fs::path& dir) { continue; } - auto file = Utils::OpenCstdioFile(item.path(), Utils::Read); - if (!file) continue; - DEFER { fclose(file); }; - - char readerBuffer[65536]; - rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer)); - - rapidjson::Document root; - root.ParseStream(stream); - - auto ires = IresObject::ReadFull(root); - if (!ires) { - continue; - } - - auto iden = fs::path(item.path()).replace_extension().lexically_relative(dir).string(); - std::replace(iden.begin(), iden.end(), '\\', '/'); - -#if 0 - std::string_view idenView(iden); - - // Trim heading slashes - while (idenView.front() == '/') { - idenView = std::string_view(idenView.data() + 1, idenView.size()); - } - // Trim trailing slashes - while (idenView.back() == '/') { - idenView = std::string_view(idenView.data(), idenView.size() - 1); - } -#endif - ires->mName = std::move(iden); - std::string_view key(ires->mName); - mObjects.try_emplace(key, ires.release()); + Load(item.path()); } } @@ -162,21 +149,73 @@ std::pair<IresObject*, bool> IresManager::Add(IresObject* ires) { snprintf(name.data(), size, "Unnamed %s #%d", IresObject::ToString(ires->GetKind()).data(), n); } - auto [iter, inserted] = mObjects.try_emplace(name, ires); + auto& uid = ires->mUid; + if (uid.IsNull()) { + uid = Uid::Create(); + } + + auto [iter, inserted] = mObjByUid.try_emplace(uid, ires); if (inserted) { ires->mMan = this; + // TODO handle full path return { ires, true }; } else { return { iter->second.Get(), false }; } } +IresObject* IresManager::Load(const fs::path& filePath) { + auto file = Utils::OpenCstdioFile(filePath, Utils::Read); + if (!file) return nullptr; + DEFER { fclose(file); }; + + char readerBuffer[65536]; + rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer)); + + rapidjson::Document root; + root.ParseStream(stream); + + auto ires = IresObject::ReadFull(root); + if (!ires) { + return nullptr; + } + + // Load uid should be handled by IresObject::ReadFull + assert(!ires->mUid.IsNull()); + // Load name from filename + ires->mName = filePath.filename().replace_extension().string(); + Add(ires.get()); + + // TODO subdirectory support +#if 0 + auto iden = fs::path(filePath).replace_extension().lexically_relative(dir).string(); + std::replace(iden.begin(), iden.end(), '\\', '/'); + + std::string_view idenView(iden); + + // Trim heading slashes + while (idenView.front() == '/') { + idenView = std::string_view(idenView.data() + 1, idenView.size()); + } + // Trim trailing slashes + while (idenView.back() == '/') { + idenView = std::string_view(idenView.data(), idenView.size() - 1); + } +#endif + + return ires.release(); +} + void IresManager::Delete(IresObject* ires) { // TODO } bool IresManager::Rename(IresObject* ires, std::string newName) { - if (mObjects.contains(newName)) { + ires->mName = std::move(newName); + + // TODO validate no name duplication +#if 0 + if (mObjByPath.contains(newName)) { return false; } @@ -184,20 +223,16 @@ bool IresManager::Rename(IresObject* ires, std::string newName) { RcPtr rc(ires); // Remove old entry (must do before replacing Material::mName, because the std::string_view in the map is a reference to it) - mObjects.erase(ires->GetName()); + mObjByPath.erase(ires->GetName()); // Add new entry ires->mName = std::move(newName); - mObjects.try_emplace(ires->GetName(), ires); - + // TODO handle full path + mObjByPath.try_emplace(ires->GetName(), ires); +#endif return true; } -IresObject* IresManager::Load(const fs::path& path) { - // TODO - return nullptr; -} - static fs::path GetDesignatedPath(IresObject* ires) { return AppConfig::assetDirPath / "Ires" / fs::path(ires->GetName()).replace_extension(".json"); } @@ -235,9 +270,9 @@ void IresManager::Save(IresObject* ires, const fs::path& filePath) { root.Accept(writer); } -IresObject* IresManager::FindIres(std::string_view path) { - auto iter = mObjects.find(path); - if (iter != mObjects.end()) { +IresObject* IresManager::FindIres(const Uid& uid) { + auto iter = mObjByUid.find(uid); + if (iter != mObjByUid.end()) { return iter->second.Get(); } else { return nullptr; |