123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <GraphCanvas.h>
- namespace GraphCanvas
- {
- TranslationDatabase::TranslationDatabase()
- {
- TranslationRequestBus::Handler::BusConnect();
- }
- TranslationDatabase::~TranslationDatabase()
- {
- TranslationRequestBus::Handler::BusDisconnect();
- AZ::Data::AssetBus::MultiHandler::BusDisconnect();
- }
- void TranslationDatabase::Init()
- {
- AzFramework::AssetCatalogEventBus::Handler::BusConnect();
- }
- void TranslationDatabase::Restore()
- {
- AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
- m_database.clear();
- AZStd::function<void()> reloadFn = []()
- {
- // Collects all script assets for reloading
- AZ::Data::AssetCatalogRequests::AssetEnumerationCB collectAssetsCb = [](const AZ::Data::AssetId, const AZ::Data::AssetInfo& info)
- {
- // Check asset type
- if (info.m_assetType == azrtti_typeid<TranslationAsset>())
- {
- auto asset = AZ::Data::AssetManager::Instance().GetAsset<TranslationAsset>(info.m_assetId, AZ::Data::AssetLoadBehavior::Default);
- if (asset && asset.IsReady())
- {
- // Reload the asset from it's current data
- asset.Reload();
- }
- }
- };
- AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, nullptr, collectAssetsCb, nullptr);
- };
- AZ::TickBus::QueueFunction(reloadFn);
- }
- void TranslationDatabase::DumpDatabase(const AZStd::string& filename)
- {
- rapidjson::Document document;
- document.SetObject();
- rapidjson::Value entries(rapidjson::kArrayType);
- for (const auto& entry : m_database)
- {
- rapidjson::Value key(rapidjson::kStringType);
- key.SetString(entry.first.c_str(), document.GetAllocator());
- rapidjson::Value value(rapidjson::kStringType);
- value.SetString(entry.second.c_str(), document.GetAllocator());
- rapidjson::Value item(rapidjson::kObjectType);
- item.AddMember(key, value, document.GetAllocator());
- entries.PushBack(item, document.GetAllocator());
- }
- document.AddMember("entries", entries, document.GetAllocator());
- AZ::IO::SystemFile outputFile;
- if (!outputFile.Open(filename.c_str(),
- AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE |
- AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE_PATH |
- AZ::IO::SystemFile::OpenMode::SF_OPEN_WRITE_ONLY))
- {
- AZ_Error("Translation", false, "Failed to create output file: %s", filename.c_str());
- return;
- }
- rapidjson::StringBuffer scratchBuffer;
- rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(scratchBuffer);
- document.Accept(writer);
- outputFile.Write(scratchBuffer.GetString(), scratchBuffer.GetSize());
- outputFile.Close();
- scratchBuffer.Clear();
- }
- bool TranslationDatabase::HasKey(const AZStd::string& key)
- {
- return m_database.find(key) != m_database.end();
- }
- GraphCanvas::TranslationRequests::Details TranslationDatabase::GetDetails(const AZStd::string& key, const Details& fallbackDetails)
- {
- Details details;
- if (!Get(key + ".name", details.m_name))
- {
- details.m_name = fallbackDetails.m_name;
- }
- if (!Get(key + ".tooltip", details.m_tooltip))
- {
- details.m_tooltip = fallbackDetails.m_tooltip;
- }
- if (!Get(key + ".subtitle", details.m_subtitle))
- {
- details.m_subtitle = fallbackDetails.m_subtitle;
- }
- if (!Get(key + ".category", details.m_category))
- {
- details.m_category = fallbackDetails.m_category;
- }
- return details;
- }
- bool TranslationDatabase::Get(const AZStd::string& key, AZStd::string& value)
- {
- AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
- if (m_database.find(key) != m_database.end())
- {
- value = m_database[key];
- return true;
- }
- static bool s_traceMissingItems = false;
- if (s_traceMissingItems)
- {
- AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value not found for key: %s", key.c_str()).c_str());
- }
- return false;
- }
- bool TranslationDatabase::Add(const TranslationFormat& format)
- {
- AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
- bool warnings = false;
- for (auto& entry : format.m_database)
- {
- if (m_database.find(entry.first) == m_database.end())
- {
- m_database[entry.first] = entry.second;
- }
- else
- {
- const bool valueMatches = entry.second == m_database[entry.first];
- AZ_Warning("TranslationDatabase", valueMatches,
- R"(Unable to store key: "%s" with value: "%s" because that key already exists with value: "%s")",
- entry.first.c_str(), entry.second.c_str(), m_database[entry.first].c_str());
- warnings = !valueMatches;
- }
- }
- return warnings;
- }
- bool TranslationDatabase::IsDuplicate(const AZStd::string& key)
- {
- return m_database.find(key) != m_database.end();
- }
- }
|