Integrating up through commit 90f050496
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
|
||||
#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 = [this]()
|
||||
{
|
||||
// Collects all script assets for reloading
|
||||
AZ::Data::AssetCatalogRequests::AssetEnumerationCB collectAssetsCb = [this](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 char* name = Get(key + ".name");
|
||||
const char* tooltip = Get(key + ".tooltip");
|
||||
const char* subtitle = Get(key + ".subtitle");
|
||||
const char* category = Get(key + ".category");
|
||||
|
||||
static bool s_traceMissingItems = true;
|
||||
if (s_traceMissingItems)
|
||||
{
|
||||
AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value (name) not found for key: %s", key.c_str()).c_str());
|
||||
AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value (tooltip) not found for key: %s", key.c_str()).c_str());
|
||||
AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value (subtitle) not found for key: %s", key.c_str()).c_str());
|
||||
AZ_TracePrintf("GraphCanvas", AZStd::string::format("Value (category) not found for key: %s", key.c_str()).c_str());
|
||||
}
|
||||
|
||||
return Details(name ? name : "", tooltip ? tooltip : "", subtitle ? subtitle : "", category ? category : "");
|
||||
}
|
||||
|
||||
const char* TranslationDatabase::Get(const AZStd::string& key)
|
||||
{
|
||||
AZStd::lock_guard<AZStd::recursive_mutex> lock(m_mutex);
|
||||
|
||||
if (m_database.find(key) != m_database.end())
|
||||
{
|
||||
return m_database[key].c_str();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
AZStd::string warning = AZStd::string::format("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());
|
||||
AZ_Warning("TranslationSerializer", false, warning.c_str());
|
||||
warnings = true;
|
||||
}
|
||||
}
|
||||
|
||||
return warnings;
|
||||
}
|
||||
|
||||
bool TranslationDatabase::IsDuplicate(const AZStd::string& key)
|
||||
{
|
||||
return m_database.find(key) != m_database.end();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user