TranslationAsset.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #pragma once
  9. #include <AzCore/Asset/AssetCommon.h>
  10. #include <AzCore/Asset/AssetManager.h>
  11. #include <AzCore/Asset/AssetJsonSerializer.h>
  12. #include <AzCore/Asset/AssetTypeInfoBus.h>
  13. #include "TranslationSerializer.h"
  14. namespace GraphCanvas
  15. {
  16. // Stores a key/value database of strings that users can query translated
  17. // names from
  18. class TranslationFormat
  19. {
  20. public:
  21. AZ_RTTI(TranslationFormat, "{F51F816E-AEFB-40D4-B3DC-8478364AEB82}");
  22. virtual ~TranslationFormat() = default;
  23. AZStd::unordered_map<AZStd::string, AZStd::string> m_database;
  24. };
  25. // These are individual assets that store portions of the TranslationDatabase
  26. // during load time, the TranslationDatabase will enumerate through all of these
  27. // and build itself up
  28. class TranslationAsset
  29. : public AZ::Data::AssetData
  30. {
  31. public:
  32. AZ_RTTI(TranslationAsset, "{6A1A3B00-3DF2-4297-96BB-3BA067A978E6}", AZ::Data::AssetData);
  33. AZ_CLASS_ALLOCATOR(TranslationAsset, AZ::SystemAllocator);
  34. TranslationAsset(const AZ::Data::AssetId& assetId = AZ::Data::AssetId(), AZ::Data::AssetData::AssetStatus status = AZ::Data::AssetData::AssetStatus::NotLoaded);
  35. ~TranslationAsset() = default;
  36. static const char* GetDisplayName() { return "Graph Canvas Translation"; }
  37. static const char* GetGroup() { return "GraphCanvas"; }
  38. static const char* GetFileFilter() { return ".names"; }
  39. static void Reflect(AZ::ReflectContext* context);
  40. TranslationFormat m_translationData;
  41. };
  42. //! TranslationAssetHandler will process JSON based files that provide a mapping from string, string
  43. //! the key will be generated using the JSON file structure, but does have some requirements.
  44. //!
  45. //! Requirements:
  46. //! - Must have a top level array called "entries"
  47. //! - Must provide a "base" element for any entry added
  48. //!
  49. //! Example:
  50. //!
  51. //! {
  52. //! "entries": [
  53. //! {
  54. //! "base": "Globals",
  55. //! "details": {
  56. //! "name": "My Name",
  57. //! "tooltip": "My Tooltip"
  58. //! }
  59. //! ]
  60. //! }
  61. //!
  62. //! The example JSON will produce the following database:
  63. //!
  64. //! [Globals.details.name, "My Name"]
  65. //! [Globals.details.tooltip", "My Tooltip"]
  66. //!
  67. //! Arrays
  68. //! Arrays are supported and will contain an index value encoded into the key, for an array called "somearray" these may look like this:
  69. //! "somearray": [ {
  70. //! "name": "First one"
  71. //! }, {
  72. //! "name": "Second one"
  73. //! } ]
  74. //!
  75. //! Globals.details.somearray.0.name
  76. //! Globals.details.somearray.1.name
  77. //!
  78. //! There is one important aspect however, if an element in an array has a "base" value, the value of this key
  79. //! will replace the index. This is useful when the index and/or ordering of an entry is not relevant or may
  80. //! change.
  81. //!
  82. //! "somearray": [ {
  83. //! "name": "First one"
  84. //! "base": "a_key"
  85. //! }, {
  86. //! "name": "Second one",
  87. //! "base": "b_key"
  88. //! } ]
  89. //!
  90. //! Globals.details.somearray.0.base == "a_key"
  91. //! Globals.details.somearray.0.name == "First one"
  92. //! Globals.details.somearray.1.base == "b_key"
  93. //! Globals.details.somearray.1.name == "Second one"
  94. //!
  95. class TranslationAssetHandler
  96. : public AZ::Data::AssetHandler
  97. , protected AZ::AssetTypeInfoBus::MultiHandler
  98. {
  99. public:
  100. AZ_CLASS_ALLOCATOR(TranslationAssetHandler, AZ::SystemAllocator);
  101. AZ_RTTI(TranslationAssetHandler, "{C161AB3B-86F6-4CB1-9DAE-83F2DE084CF4}", AZ::Data::AssetHandler);
  102. TranslationAssetHandler();
  103. ~TranslationAssetHandler() override;
  104. //////////////////////////////////////////////////////////////////////////////////////////////
  105. // AZ::Data::AssetHandler
  106. AZ::Data::AssetPtr CreateAsset(const AZ::Data::AssetId& id, const AZ::Data::AssetType& type) override;
  107. AZ::Data::AssetHandler::LoadResult LoadAssetData(const AZ::Data::Asset<AZ::Data::AssetData>& asset, AZStd::shared_ptr<AZ::Data::AssetDataStream> stream, const AZ::Data::AssetFilterCB& assetLoadFilterCB) override;
  108. void DestroyAsset(AZ::Data::AssetPtr ptr) override;
  109. void GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes) override;
  110. bool CanHandleAsset(const AZ::Data::AssetId& id) const override;
  111. //////////////////////////////////////////////////////////////////////////////////////////////
  112. //////////////////////////////////////////////////////////////////////////////////////////////
  113. // AZ::AssetTypeInfoBus::Handler
  114. AZ::Data::AssetType GetAssetType() const override;
  115. const char* GetAssetTypeDisplayName() const override;
  116. const char* GetGroup() const override;
  117. const char* GetBrowserIcon() const override;
  118. AZ::Uuid GetComponentTypeId() const override;
  119. void GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions) override;
  120. //////////////////////////////////////////////////////////////////////////////////////////////
  121. void Register();
  122. void Unregister();
  123. private:
  124. AZStd::unique_ptr<TranslationFormatSerializer> m_serializer;
  125. AZStd::unique_ptr<AZ::JsonSerializerSettings> m_serializationSettings;
  126. AZStd::unique_ptr<AZ::JsonDeserializerSettings> m_deserializationSettings;
  127. AZStd::unique_ptr<AZ::JsonSerializerContext> m_jsonSerializationContext;
  128. AZStd::unique_ptr<AZ::JsonDeserializerContext> m_jsonDeserializationContext;
  129. rapidjson::Document::AllocatorType m_jsonAllocator;
  130. };
  131. }