TranslationDatabase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <GraphCanvas.h>
  9. namespace GraphCanvas
  10. {
  11. TranslationDatabase::TranslationDatabase()
  12. {
  13. TranslationRequestBus::Handler::BusConnect();
  14. }
  15. TranslationDatabase::~TranslationDatabase()
  16. {
  17. TranslationRequestBus::Handler::BusDisconnect();
  18. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  19. }
  20. void TranslationDatabase::Init()
  21. {
  22. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  23. }
  24. void TranslationDatabase::Restore()
  25. {
  26. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
  27. m_database.clear();
  28. AZStd::function<void()> reloadFn = []()
  29. {
  30. // Collects all script assets for reloading
  31. AZ::Data::AssetCatalogRequests::AssetEnumerationCB collectAssetsCb = [](const AZ::Data::AssetId, const AZ::Data::AssetInfo& info)
  32. {
  33. // Check asset type
  34. if (info.m_assetType == azrtti_typeid<TranslationAsset>())
  35. {
  36. auto asset = AZ::Data::AssetManager::Instance().GetAsset<TranslationAsset>(info.m_assetId, AZ::Data::AssetLoadBehavior::Default);
  37. if (asset && asset.IsReady())
  38. {
  39. // Reload the asset from it's current data
  40. asset.Reload();
  41. }
  42. }
  43. };
  44. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, nullptr, collectAssetsCb, nullptr);
  45. };
  46. AZ::TickBus::QueueFunction(reloadFn);
  47. }
  48. void TranslationDatabase::DumpDatabase(const AZStd::string& filename)
  49. {
  50. rapidjson::Document document;
  51. document.SetObject();
  52. rapidjson::Value entries(rapidjson::kArrayType);
  53. for (const auto& entry : m_database)
  54. {
  55. rapidjson::Value key(rapidjson::kStringType);
  56. key.SetString(entry.first.c_str(), document.GetAllocator());
  57. rapidjson::Value value(rapidjson::kStringType);
  58. value.SetString(entry.second.c_str(), document.GetAllocator());
  59. rapidjson::Value item(rapidjson::kObjectType);
  60. item.AddMember(key, value, document.GetAllocator());
  61. entries.PushBack(item, document.GetAllocator());
  62. }
  63. document.AddMember("entries", entries, document.GetAllocator());
  64. AZ::IO::SystemFile outputFile;
  65. if (!outputFile.Open(filename.c_str(),
  66. AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE |
  67. AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE_PATH |
  68. AZ::IO::SystemFile::OpenMode::SF_OPEN_WRITE_ONLY))
  69. {
  70. AZ_Error("Translation", false, "Failed to create output file: %s", filename.c_str());
  71. return;
  72. }
  73. rapidjson::StringBuffer scratchBuffer;
  74. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(scratchBuffer);
  75. document.Accept(writer);
  76. outputFile.Write(scratchBuffer.GetString(), scratchBuffer.GetSize());
  77. outputFile.Close();
  78. scratchBuffer.Clear();
  79. }
  80. bool TranslationDatabase::HasKey(const AZStd::string& key)
  81. {
  82. return m_database.find(key) != m_database.end();
  83. }
  84. GraphCanvas::TranslationRequests::Details TranslationDatabase::GetDetails(const AZStd::string& key, const Details& fallbackDetails)
  85. {
  86. Details details;
  87. if (!Get(key + ".name", details.m_name))
  88. {
  89. details.m_name = fallbackDetails.m_name;
  90. }
  91. if (!Get(key + ".tooltip", details.m_tooltip))
  92. {
  93. details.m_tooltip = fallbackDetails.m_tooltip;
  94. }
  95. if (!Get(key + ".subtitle", details.m_subtitle))
  96. {
  97. details.m_subtitle = fallbackDetails.m_subtitle;
  98. }
  99. if (!Get(key + ".category", details.m_category))
  100. {
  101. details.m_category = fallbackDetails.m_category;
  102. }
  103. return details;
  104. }
  105. bool TranslationDatabase::Get(const AZStd::string& key, AZStd::string& value)
  106. {
  107. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
  108. if (m_database.find(key) != m_database.end())
  109. {
  110. value = m_database[key];
  111. return true;
  112. }
  113. static bool s_traceMissingItems = false;
  114. if (s_traceMissingItems)
  115. {
  116. AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value not found for key: %s", key.c_str()).c_str());
  117. }
  118. return false;
  119. }
  120. bool TranslationDatabase::Add(const TranslationFormat& format)
  121. {
  122. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
  123. bool warnings = false;
  124. for (auto& entry : format.m_database)
  125. {
  126. if (m_database.find(entry.first) == m_database.end())
  127. {
  128. m_database[entry.first] = entry.second;
  129. }
  130. else
  131. {
  132. const bool valueMatches = entry.second == m_database[entry.first];
  133. AZ_Warning("TranslationDatabase", valueMatches,
  134. R"(Unable to store key: "%s" with value: "%s" because that key already exists with value: "%s")",
  135. entry.first.c_str(), entry.second.c_str(), m_database[entry.first].c_str());
  136. warnings = !valueMatches;
  137. }
  138. }
  139. return warnings;
  140. }
  141. bool TranslationDatabase::IsDuplicate(const AZStd::string& key)
  142. {
  143. return m_database.find(key) != m_database.end();
  144. }
  145. }