Integrating latest 47acbe8
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 <AzCore/RTTI/ReflectContext.h>
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
#include <AzFramework/Spawnable/Spawnable.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
Spawnable::Spawnable(const AZ::Data::AssetId& id)
|
||||
: AZ::Data::AssetData(id)
|
||||
{
|
||||
}
|
||||
|
||||
Spawnable::Spawnable(Spawnable&& other)
|
||||
: m_entities(AZStd::move(other.m_entities))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Spawnable& Spawnable::operator=(Spawnable&& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
m_entities = AZStd::move(other.m_entities);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Spawnable::EntityList& Spawnable::GetEntities() const
|
||||
{
|
||||
return m_entities;
|
||||
}
|
||||
|
||||
Spawnable::EntityList& Spawnable::GetEntities()
|
||||
{
|
||||
return m_entities;
|
||||
}
|
||||
|
||||
bool Spawnable::IsEmpty() const
|
||||
{
|
||||
return m_entities.empty();
|
||||
}
|
||||
|
||||
SpawnableMetaData& Spawnable::GetMetaData()
|
||||
{
|
||||
return m_metaData;
|
||||
}
|
||||
|
||||
const SpawnableMetaData& Spawnable::GetMetaData() const
|
||||
{
|
||||
return m_metaData;
|
||||
}
|
||||
|
||||
void Spawnable::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context); serializeContext != nullptr)
|
||||
{
|
||||
serializeContext->Class<Spawnable, AZ::Data::AssetData>()->Version(1)
|
||||
->Field("Meta data", &Spawnable::m_metaData)
|
||||
->Field("Entities", &Spawnable::m_entities);
|
||||
}
|
||||
}
|
||||
} // namespace AzFramework
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/Asset/AssetCommon.h>
|
||||
#include <AzCore/Component/Entity.h>
|
||||
#include <AzCore/Memory/SystemAllocator.h>
|
||||
#include <AzCore/std/containers/vector.h>
|
||||
#include <AzCore/std/smart_ptr/unique_ptr.h>
|
||||
#include <AzFramework/Spawnable/SpawnableMetaData.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
class ReflectContext;
|
||||
|
||||
class Spawnable final
|
||||
: public AZ::Data::AssetData
|
||||
{
|
||||
public:
|
||||
AZ_CLASS_ALLOCATOR(Spawnable, AZ::SystemAllocator, 0);
|
||||
AZ_RTTI(AzFramework::Spawnable, "{855E3021-D305-4845-B284-20C3F7FDF16B}", AZ::Data::AssetData);
|
||||
|
||||
using EntityList = AZStd::vector<AZStd::unique_ptr<AZ::Entity>>;
|
||||
|
||||
inline static constexpr const char* FileExtension = "spawnable";
|
||||
|
||||
Spawnable() = default;
|
||||
explicit Spawnable(const AZ::Data::AssetId& id);
|
||||
Spawnable(const Spawnable& rhs) = delete;
|
||||
Spawnable(Spawnable&& other);
|
||||
~Spawnable() override = default;
|
||||
|
||||
Spawnable& operator=(const Spawnable& rhs) = delete;
|
||||
Spawnable& operator=(Spawnable&& other);
|
||||
|
||||
const EntityList& GetEntities() const;
|
||||
EntityList& GetEntities();
|
||||
bool IsEmpty() const;
|
||||
|
||||
SpawnableMetaData& GetMetaData();
|
||||
const SpawnableMetaData& GetMetaData() const;
|
||||
|
||||
static void Reflect(AZ::ReflectContext* context);
|
||||
|
||||
private:
|
||||
SpawnableMetaData m_metaData;
|
||||
|
||||
// Container for keeping all entities of the prefab the Spawnable was created from.
|
||||
// Includes both direct and nested entities of the prefab.
|
||||
EntityList m_entities;
|
||||
};
|
||||
|
||||
using SpawnableList = AZStd::vector<Spawnable>;
|
||||
|
||||
} // namespace AzFramework
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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 <AzCore/Serialization/Utils.h>
|
||||
#include <AzCore/std/string/string.h>
|
||||
#include <AzFramework/Spawnable/SpawnableAssetHandler.h>
|
||||
#include <AzFramework/Spawnable/Spawnable.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
SpawnableAssetHandler::SpawnableAssetHandler()
|
||||
{
|
||||
AZ::AssetTypeInfoBus::MultiHandler::BusConnect(AZ::AzTypeInfo<Spawnable>::Uuid());
|
||||
}
|
||||
|
||||
SpawnableAssetHandler::~SpawnableAssetHandler()
|
||||
{
|
||||
AZ::AssetTypeInfoBus::MultiHandler::BusDisconnect();
|
||||
}
|
||||
|
||||
AZ::Data::AssetPtr SpawnableAssetHandler::CreateAsset(const AZ::Data::AssetId& id, [[maybe_unused]] const AZ::Data::AssetType& type)
|
||||
{
|
||||
AZ_Assert(type == AZ::AzTypeInfo<Spawnable>::Uuid(),
|
||||
"Asset handler for Spawnable was given a type that's not a Spawnable: %s", type.ToString<AZStd::string>().c_str());
|
||||
return aznew Spawnable(id);
|
||||
}
|
||||
|
||||
void SpawnableAssetHandler::DestroyAsset(AZ::Data::AssetPtr ptr)
|
||||
{
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
void SpawnableAssetHandler::GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes)
|
||||
{
|
||||
assetTypes.push_back(AZ::AzTypeInfo<Spawnable>::Uuid());
|
||||
}
|
||||
|
||||
auto SpawnableAssetHandler::LoadAssetData(
|
||||
const AZ::Data::Asset<AZ::Data::AssetData>& asset,
|
||||
AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
|
||||
const AZ::Data::AssetFilterCB& assetLoadFilterCB) -> LoadResult
|
||||
{
|
||||
Spawnable* spawnable = asset.GetAs<Spawnable>();
|
||||
AZ_Assert(spawnable, "Loaded asset data handed to the SpawnableAssetHandler didn't contain a Spawanble.");
|
||||
|
||||
AZ::ObjectStream::FilterDescriptor filter(assetLoadFilterCB);
|
||||
if (AZ::Utils::LoadObjectFromStreamInPlace(*stream, *spawnable, nullptr /*SerializeContext*/, filter))
|
||||
{
|
||||
return AZ::Data::AssetHandler::LoadResult::LoadComplete;
|
||||
}
|
||||
else
|
||||
{
|
||||
AZ_Error("Spawnable", false, "Failed to deserialize asset %s.", asset->GetId().ToString<AZStd::string>().c_str());
|
||||
return AZ::Data::AssetHandler::LoadResult::Error;
|
||||
}
|
||||
}
|
||||
|
||||
AZ::Data::AssetType SpawnableAssetHandler::GetAssetType() const
|
||||
{
|
||||
return AZ::AzTypeInfo<Spawnable>::Uuid();
|
||||
}
|
||||
|
||||
const char* SpawnableAssetHandler::GetAssetTypeDisplayName() const
|
||||
{
|
||||
return "Spawnable";
|
||||
}
|
||||
|
||||
const char* SpawnableAssetHandler::GetGroup() const
|
||||
{
|
||||
return "Prefab";
|
||||
}
|
||||
|
||||
const char* SpawnableAssetHandler::GetBrowserIcon() const
|
||||
{
|
||||
return "Editor/Icons/Components/Viewport/EntityInSlice.png";
|
||||
}
|
||||
|
||||
void SpawnableAssetHandler::GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions)
|
||||
{
|
||||
extensions.push_back(Spawnable::FileExtension);
|
||||
}
|
||||
} // namespace AzFramework
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/Asset/AssetManager.h>
|
||||
#include <AzCore/Asset/AssetTypeInfoBus.h>
|
||||
#include <AzCore/Memory/SystemAllocator.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
class SpawnableAssetHandler final
|
||||
: public AZ::Data::AssetHandler
|
||||
, public AZ::AssetTypeInfoBus::MultiHandler
|
||||
{
|
||||
public:
|
||||
AZ_CLASS_ALLOCATOR(SpawnableAssetHandler, AZ::SystemAllocator, 0);
|
||||
AZ_RTTI(AZ::SpawnableAssetHandler, "{BF6E2D17-87C9-4BB1-A205-3656CF6D551D}", AZ::Data::AssetHandler);
|
||||
|
||||
SpawnableAssetHandler();
|
||||
~SpawnableAssetHandler() override;
|
||||
|
||||
//
|
||||
// AssetHandler
|
||||
//
|
||||
|
||||
AZ::Data::AssetPtr CreateAsset(const AZ::Data::AssetId& id, const AZ::Data::AssetType& type) override;
|
||||
void DestroyAsset(AZ::Data::AssetPtr ptr) override;
|
||||
void GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes) override;
|
||||
|
||||
|
||||
//
|
||||
// AssetTypeInfoBus
|
||||
//
|
||||
|
||||
AZ::Data::AssetType GetAssetType() const override;
|
||||
const char* GetAssetTypeDisplayName() const override;
|
||||
const char* GetGroup() const override;
|
||||
const char* GetBrowserIcon() const override;
|
||||
void GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions) override;
|
||||
|
||||
protected:
|
||||
LoadResult LoadAssetData(
|
||||
const AZ::Data::Asset<AZ::Data::AssetData>& asset,
|
||||
AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
|
||||
const AZ::Data::AssetFilterCB& assetLoadFilterCB) override;
|
||||
};
|
||||
} // namespace AzFramework
|
||||
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* 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 <AzCore/RTTI/ReflectContext.h>
|
||||
#include <AzCore/Serialization/ObjectStream.h> // Needed for ObjectStreamWriteOverrideCB and its AZ_TYPE_INFO_SPECIALIZE
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
#include <AzCore/Casting/numeric_cast.h>
|
||||
#include <AzCore/std/algorithm.h>
|
||||
#include <AzCore/std/typetraits/typetraits.h>
|
||||
#include <AzFramework/Spawnable/SpawnableMetaData.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
SpawnableMetaData::SpawnableMetaData(Table table)
|
||||
{
|
||||
AZ_Assert(AZStd::is_sorted(table.begin(), table.end(),
|
||||
[](const auto& lhs, const auto& rhs)
|
||||
{
|
||||
return lhs.first < rhs.first;
|
||||
}), "The key/value table provided to SpawnableMetaData needs to be sorted by key.");
|
||||
m_keys.reserve(table.size());
|
||||
m_values.reserve(table.size());
|
||||
|
||||
for (auto&& entry : table)
|
||||
{
|
||||
m_keys.push_back(entry.first);
|
||||
m_values.push_back(AZStd::move(entry.second));
|
||||
}
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, bool& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, uint64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, int64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, double& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, AZStd::string_view& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view key, SpawnableMetaDataArraySize& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(key), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, uint64_t index, bool& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, uint64_t index, uint64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, uint64_t index, int64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, uint64_t index, double& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, uint64_t index, AZStd::string_view& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, bool& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, uint64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, int64_t& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, double& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
bool SpawnableMetaData::Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, AZStd::string_view& value) const
|
||||
{
|
||||
return GetGeneric(GetKeyHash(arrayKey, index), value);
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetType(AZStd::string_view key) const -> ValueType
|
||||
{
|
||||
return GetTypeGeneric(GetKeyHash(key));
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetType(AZStd::string_view arrayKey, uint64_t index) const -> ValueType
|
||||
{
|
||||
return GetTypeGeneric(GetKeyHash(arrayKey, index));
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetType(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index) const -> ValueType
|
||||
{
|
||||
return GetTypeGeneric(GetKeyHash(arrayKey, index));
|
||||
}
|
||||
|
||||
void SpawnableMetaData::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context); serializeContext != nullptr)
|
||||
{
|
||||
serializeContext->Class<SpawnableMetaData>()->Version(1)
|
||||
->Field("Keys", &SpawnableMetaData::m_keys)
|
||||
->Field("Values", &SpawnableMetaData::m_values);
|
||||
}
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetKeyHash(AZStd::string_view key) const -> TableKey
|
||||
{
|
||||
return AZ::TypeHash64(reinterpret_cast<const uint8_t*>(key.data()), aznumeric_cast<uint64_t>(key.length()));
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetKeyHash(AZStd::string_view arrayKey, uint64_t index) const -> TableKey
|
||||
{
|
||||
return AZ::TypeHash64(
|
||||
reinterpret_cast<const uint8_t*>(arrayKey.data()), aznumeric_cast<uint64_t>(arrayKey.length()),
|
||||
aznumeric_caster(ArrayKeyRoot + index));
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetKeyHash(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index) const -> TableKey
|
||||
{
|
||||
return GetKeyHash(arrayKey, aznumeric_cast<uint64_t>(index));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SpawnableMetaData::GetGeneric(AZ::HashValue64 key, T& value) const
|
||||
{
|
||||
auto it = AZStd::lower_bound(m_keys.begin(), m_keys.end(), key);
|
||||
if (it != m_keys.end() && *it == key)
|
||||
{
|
||||
size_t index = AZStd::distance(m_keys.begin(), it);
|
||||
if constexpr (AZStd::is_same_v<T, AZStd::string_view>)
|
||||
{
|
||||
if (const AZStd::string* storedValue = AZStd::get_if<AZStd::string>(&m_values[index]); storedValue != nullptr)
|
||||
{
|
||||
value = *storedValue;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (const T* storedValue = AZStd::get_if<T>(&m_values[index]); storedValue != nullptr)
|
||||
{
|
||||
value = *storedValue;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto SpawnableMetaData::GetTypeGeneric(AZ::HashValue64 key) const -> ValueType
|
||||
{
|
||||
auto it = AZStd::lower_bound(m_keys.begin(), m_keys.end(), key);
|
||||
if (it != m_keys.end() && *it == key)
|
||||
{
|
||||
size_t index = AZStd::distance(m_keys.begin(), it);
|
||||
return AZStd::visit([](auto&& args) -> ValueType
|
||||
{
|
||||
using Key = AZStd::decay_t<decltype(args)>;
|
||||
if constexpr (AZStd::is_same_v<Key, bool>) { return ValueType::Boolean; }
|
||||
else if constexpr (AZStd::is_same_v<Key, uint64_t>) { return ValueType::UnsignedInteger; }
|
||||
else if constexpr (AZStd::is_same_v<Key, int64_t>) { return ValueType::SignedInteger; }
|
||||
else if constexpr (AZStd::is_same_v<Key, double>) { return ValueType::FloatingPoint; }
|
||||
else if constexpr (AZStd::is_same_v<Key, AZStd::string>){ return ValueType::String; }
|
||||
else if constexpr (AZStd::is_same_v<Key, SpawnableMetaDataArraySize>)
|
||||
{
|
||||
return ValueType::ArraySize;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ValueType::Unavailable;
|
||||
}
|
||||
|
||||
}, m_values[index]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ValueType::Unavailable;
|
||||
}
|
||||
}
|
||||
}; // namespace AzFramework
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/RTTI/TypeSafeIntegral.h>
|
||||
#include <AzCore/Utils/TypeHash.h>
|
||||
#include <AzCore/std/containers/variant.h>
|
||||
#include <AzCore/std/containers/vector.h>
|
||||
#include <AzCore/std/string/string.h>
|
||||
#include <AzCore/std/utils.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
class ReflectContext;
|
||||
}
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
AZ_TYPE_SAFE_INTEGRAL(SpawnableMetaDataArraySize, uint64_t);
|
||||
using SpawnableMetaDataArrayIndex = SpawnableMetaDataArraySize;
|
||||
|
||||
//! Simple meta data that can be stored along with a spawnable.
|
||||
//! This class is designed to be read-only and is expected to be serialized. AzToolsFramework provides a class for
|
||||
//! constructing meta data.
|
||||
class SpawnableMetaData final
|
||||
{
|
||||
public:
|
||||
static inline constexpr uint64_t ArrayKeyRoot = 146223222818353; // Random prime number
|
||||
|
||||
using TableKey = AZ::HashValue64;
|
||||
using TableValue = AZStd::variant<bool, uint64_t, int64_t, double, AZStd::string, SpawnableMetaDataArraySize>;
|
||||
using TableEntry = AZStd::pair<TableKey, TableValue>;
|
||||
using Table = AZStd::vector<TableEntry>;
|
||||
|
||||
enum class ValueType
|
||||
{
|
||||
Unavailable,
|
||||
Boolean,
|
||||
UnsignedInteger,
|
||||
SignedInteger,
|
||||
FloatingPoint,
|
||||
String,
|
||||
ArraySize
|
||||
};
|
||||
|
||||
AZ_TYPE_INFO(AZ::SpawnableMetaData, "{3832FA08-B10B-49AF-A81E-8E2FC1FF98B1}");
|
||||
|
||||
SpawnableMetaData() = default;
|
||||
// This constructor exists only so externally constructed tables can be provided by tools.
|
||||
explicit SpawnableMetaData(Table table);
|
||||
|
||||
bool Get(AZStd::string_view key, bool& value) const;
|
||||
bool Get(AZStd::string_view key, uint64_t& value) const;
|
||||
bool Get(AZStd::string_view key, int64_t& value) const;
|
||||
bool Get(AZStd::string_view key, double& value) const;
|
||||
bool Get(AZStd::string_view key, AZStd::string_view& value) const;
|
||||
bool Get(AZStd::string_view key, SpawnableMetaDataArraySize& value) const;
|
||||
|
||||
bool Get(AZStd::string_view arrayKey, uint64_t index, bool& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, uint64_t index, uint64_t& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, uint64_t index, int64_t& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, uint64_t index, double& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, uint64_t index, AZStd::string_view& value) const;
|
||||
|
||||
bool Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, bool& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, uint64_t& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, int64_t& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, double& value) const;
|
||||
bool Get(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index, AZStd::string_view& value) const;
|
||||
|
||||
ValueType GetType(AZStd::string_view key) const;
|
||||
ValueType GetType(AZStd::string_view arrayKey, uint64_t index) const;
|
||||
ValueType GetType(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index) const;
|
||||
|
||||
static void Reflect(AZ::ReflectContext* context);
|
||||
|
||||
private:
|
||||
TableKey GetKeyHash(AZStd::string_view key) const;
|
||||
TableKey GetKeyHash(AZStd::string_view arrayKey, uint64_t index) const;
|
||||
TableKey GetKeyHash(AZStd::string_view arrayKey, SpawnableMetaDataArrayIndex index) const;
|
||||
|
||||
template<typename T>
|
||||
bool GetGeneric(AZ::HashValue64 key, T& value) const;
|
||||
|
||||
ValueType GetTypeGeneric(AZ::HashValue64 key) const;
|
||||
|
||||
AZStd::vector<TableKey> m_keys;
|
||||
AZStd::vector<TableValue> m_values;
|
||||
};
|
||||
} // namespace AzFramework
|
||||
|
||||
AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(AzFramework::SpawnableMetaDataArraySize);
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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 <AzCore/Asset/AssetManager.h>
|
||||
#include <AzCore/Settings/SettingsRegistry.h>
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
#include <AzFramework/Spawnable/SpawnableMetaData.h>
|
||||
#include <AzFramework/Spawnable/SpawnableSystemComponent.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
void SpawnableSystemComponent::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
Spawnable::Reflect(context);
|
||||
SpawnableMetaData::Reflect(context);
|
||||
|
||||
if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context); serializeContext != nullptr)
|
||||
{
|
||||
serializeContext->RegisterGenericType<AZ::Data::Asset<Spawnable>>();
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
|
||||
{
|
||||
services.push_back(AZ_CRC_CE("SpawnableSystemService"));
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
|
||||
{
|
||||
services.push_back(AZ_CRC_CE("SpawnableSystemService"));
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& services)
|
||||
{
|
||||
services.push_back(AZ_CRC_CE("AssetDatabaseService"));
|
||||
services.push_back(AZ_CRC_CE("AssetCatalogService"));
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::OnCatalogLoaded([[maybe_unused]] const char* catalogFile)
|
||||
{
|
||||
if (!m_rootSpawnableInitialized)
|
||||
{
|
||||
auto registry = AZ::SettingsRegistry::Get();
|
||||
AZ_Assert(registry, "Unable to check for root spawnable because the Settings Registry is not available.");
|
||||
if (registry->GetObject(m_rootSpawnable, RootSpawnableRegistryKey) && m_rootSpawnable.GetId().IsValid())
|
||||
{
|
||||
AZ_TracePrintf("Spawnables", "Root spawnable '%s' used.\n", m_rootSpawnable.GetHint().c_str());
|
||||
if (!m_rootSpawnable.QueueLoad())
|
||||
{
|
||||
AZ_Error("Spawnables", false, "Unable to queue root spawnable for loading.\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AZ_Warning("Spawnables", false, "No root spawnable assigned or root spawanble couldnt' be loaded.\n"
|
||||
"The root spawnable can be assigned in the Settings Registry under the key '$s'.\n", RootSpawnableRegistryKey);
|
||||
}
|
||||
|
||||
m_rootSpawnableInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::Activate()
|
||||
{
|
||||
// Register with AssetDatabase
|
||||
AZ_Assert(AZ::Data::AssetManager::IsReady(), "Spawnables can't be registered because the Asset Manager is not ready yet.");
|
||||
AZ::Data::AssetManager::Instance().RegisterHandler(&m_assetHandler, AZ::AzTypeInfo<Spawnable>::Uuid());
|
||||
|
||||
// Register with AssetCatalog
|
||||
AZ::Data::AssetCatalogRequestBus::Broadcast(
|
||||
&AZ::Data::AssetCatalogRequestBus::Events::EnableCatalogForAsset, AZ::AzTypeInfo<Spawnable>::Uuid());
|
||||
AZ::Data::AssetCatalogRequestBus::Broadcast(
|
||||
&AZ::Data::AssetCatalogRequestBus::Events::AddExtension, Spawnable::FileExtension);
|
||||
|
||||
AssetCatalogEventBus::Handler::BusConnect();
|
||||
}
|
||||
|
||||
void SpawnableSystemComponent::Deactivate()
|
||||
{
|
||||
AssetCatalogEventBus::Handler::BusDisconnect();
|
||||
|
||||
AZ_Assert(AZ::Data::AssetManager::IsReady(),
|
||||
"Spawnables can't be unregistered because the Asset Manager has been destroyed already or never started.");
|
||||
AZ::Data::AssetManager::Instance().UnregisterHandler(&m_assetHandler);
|
||||
}
|
||||
} // namespace AzFramework
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/Component/Component.h>
|
||||
#include <AzFramework/Asset/AssetCatalogBus.h>
|
||||
#include <AzFramework/Spawnable/Spawnable.h>
|
||||
#include <AzFramework/Spawnable/SpawnableAssetHandler.h>
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
class SpawnableSystemComponent
|
||||
: public AZ::Component
|
||||
, public AssetCatalogEventBus::Handler
|
||||
{
|
||||
public:
|
||||
AZ_COMPONENT(SpawnableSystemComponent, "{12D0DA52-BB86-4AC3-8862-9493E0D0E207}");
|
||||
|
||||
inline static constexpr const char* RootSpawnableRegistryKey = "/Amazon/AzCore/Bootstrap/RootSpawnable";
|
||||
|
||||
SpawnableSystemComponent() = default;
|
||||
SpawnableSystemComponent(const SpawnableSystemComponent&) = delete;
|
||||
SpawnableSystemComponent(SpawnableSystemComponent&&) = delete;
|
||||
|
||||
SpawnableSystemComponent& operator=(const SpawnableSystemComponent&) = delete;
|
||||
SpawnableSystemComponent& operator=(SpawnableSystemComponent&&) = delete;
|
||||
|
||||
static void Reflect(AZ::ReflectContext* context);
|
||||
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
|
||||
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
|
||||
static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& services);
|
||||
|
||||
//
|
||||
// AssetCatalogEventBus
|
||||
//
|
||||
|
||||
void OnCatalogLoaded(const char* catalogFile) override;
|
||||
|
||||
protected:
|
||||
void Activate() override;
|
||||
void Deactivate() override;
|
||||
|
||||
SpawnableAssetHandler m_assetHandler;
|
||||
AZ::Data::Asset<Spawnable> m_rootSpawnable;
|
||||
bool m_rootSpawnableInitialized{ false };
|
||||
};
|
||||
} // namespace AzFramework
|
||||
Reference in New Issue
Block a user