Cleanup SerializeContext.h pt.1 (#4264)
* Remove AssetSerializer inclusion from SerializeContext header Moved a few Reflect methods to new cpp files. In addition, some preparations for further header dependency reductions. Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Fix smoke test lua failures. Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Windows build fixes. Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Missing license headers Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Fix white-space issues. Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Code review fix for AzToolsFramework/AssetEditor/AssetEditorBus.h Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> * Fix inheritance list wrapping broken by older clang-format Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com> Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <Builder/ScriptEventsBuilderComponent.h>
|
||||
#include <ScriptEvents/ScriptEventsBus.h>
|
||||
|
||||
#include <AzCore/Asset/AssetSerializer.h>
|
||||
#if defined(SCRIPTEVENTS_EDITOR)
|
||||
|
||||
namespace ScriptEvents
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* 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 "ScriptEvents/ScriptEventMethod.h"
|
||||
|
||||
#include <AzCore/RTTI/BehaviorContext.h>
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
#include <AzCore/std/string/regex.h>
|
||||
|
||||
namespace ScriptEvents
|
||||
{
|
||||
void Method::FromScript(AZ::ScriptDataContext& dc)
|
||||
{
|
||||
if (dc.GetNumArguments() > 0)
|
||||
{
|
||||
AZStd::string name;
|
||||
if (dc.IsString(0) && dc.ReadArg(0, name))
|
||||
{
|
||||
m_name.Set(name.c_str());
|
||||
}
|
||||
|
||||
if (dc.GetNumArguments() > 1)
|
||||
{
|
||||
AZ::Uuid returnType;
|
||||
if (dc.ReadArg(1, returnType))
|
||||
{
|
||||
m_returnType.Set(returnType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AZ_TracePrintf("Script Events", "Added Script Method: %s (return type: %s)\n", GetName().c_str(), m_returnType.IsEmpty() ? "none"
|
||||
// : GetReturnType().ToString<AZStd::string>().c_str());
|
||||
}
|
||||
void Method::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
|
||||
{
|
||||
serializeContext->Class<Method>()
|
||||
->Field("m_name", &Method::m_name)
|
||||
->Field("m_tooltip", &Method::m_tooltip)
|
||||
->Field("m_returnType", &Method::m_returnType)
|
||||
->Field("m_parameters", &Method::m_parameters);
|
||||
|
||||
if (AZ::EditContext* editContext = serializeContext->GetEditContext())
|
||||
{
|
||||
editContext->Class<Method>("Script Event", "A script event's definition")
|
||||
->DataElement(
|
||||
AZ::Edit::UIHandlers::Default, &Method::m_name, "Name",
|
||||
"The specified name for this event, represents a callable function (i.e. MyScriptEvent())")
|
||||
->DataElement(AZ::Edit::UIHandlers::Default, &Method::m_tooltip, "Tooltip", "A description of this event")
|
||||
->DataElement(
|
||||
AZ::Edit::UIHandlers::ComboBox, &Method::m_returnType, "Return value type",
|
||||
"the typeid of the return value, ex. AZ::type_info<int>::Uuid foo()")
|
||||
->Attribute(AZ::Edit::Attributes::GenericValueList, &Types::GetValidReturnTypes)
|
||||
->DataElement(
|
||||
AZ::Edit::UIHandlers::Default, &Method::m_parameters, "Parameters",
|
||||
"A list of parameters for the EBus event, ex. void foo(Parameter1, Parameter2)");
|
||||
}
|
||||
}
|
||||
|
||||
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
|
||||
{
|
||||
behaviorContext->Class<Method>("Method")
|
||||
->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
|
||||
->Method("AddParameter", &Method::AddParameter)
|
||||
->Property("Name", BehaviorValueProperty(&Method::m_name))
|
||||
->Property("ReturnType", BehaviorValueProperty(&Method::m_returnType))
|
||||
->Property("Parameters", BehaviorValueProperty(&Method::m_parameters));
|
||||
}
|
||||
}
|
||||
|
||||
AZ::Outcome<bool, AZStd::string> Method::Validate() const
|
||||
{
|
||||
const AZStd::string name = GetName();
|
||||
const AZ::Uuid returnType = GetReturnType();
|
||||
|
||||
// Validate address type
|
||||
if (!Types::IsValidReturnType(returnType))
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format(
|
||||
"The specified type %s is not valid as return type for Script Event: %s", returnType.ToString<AZStd::string>().c_str(),
|
||||
name.c_str()));
|
||||
}
|
||||
|
||||
// Definition name cannot be empty
|
||||
if (name.empty())
|
||||
{
|
||||
return AZ::Failure(AZStd::string("Definition name cannot be empty"));
|
||||
}
|
||||
|
||||
// Name cannot start with a number
|
||||
if (isdigit(name.at(0)))
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, names cannot start with a number", name.c_str()));
|
||||
}
|
||||
|
||||
// Conform to valid function names
|
||||
AZStd::smatch match;
|
||||
|
||||
// Ascii-only
|
||||
AZStd::regex asciionly_regex("[^\x0A\x0D\x20-\x7E]");
|
||||
AZStd::regex_match(name, match, asciionly_regex);
|
||||
if (!match.empty())
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, invalid name, names may only contain ASCII characters", name.c_str()));
|
||||
}
|
||||
|
||||
AZStd::regex validate_regex("[_[:alpha:]][_[:alnum:]]*");
|
||||
AZStd::regex_match(name, match, validate_regex);
|
||||
if (match.empty())
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, invalid name specified", name.c_str()));
|
||||
}
|
||||
|
||||
AZStd::string parameterName;
|
||||
int parameterIndex = 0;
|
||||
for (const Parameter& parameter : m_parameters)
|
||||
{
|
||||
auto outcome = parameter.Validate();
|
||||
if (!outcome.IsSuccess())
|
||||
{
|
||||
return outcome;
|
||||
}
|
||||
|
||||
if (parameter.GetName().compare(parameterName) == 0)
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format(
|
||||
"Cannot have duplicate parameter names (%d: %s) make sure each parameter name is unique", parameterIndex,
|
||||
parameterName.c_str()));
|
||||
}
|
||||
|
||||
parameterName = parameter.GetName();
|
||||
++parameterIndex;
|
||||
}
|
||||
|
||||
return AZ::Success(true);
|
||||
}
|
||||
|
||||
void Method::PreSave()
|
||||
{
|
||||
m_name.PreSave();
|
||||
m_tooltip.PreSave();
|
||||
m_returnType.PreSave();
|
||||
|
||||
for (Parameter parameter : m_parameters)
|
||||
{
|
||||
parameter.PreSave();
|
||||
}
|
||||
}
|
||||
|
||||
void Method::Flatten()
|
||||
{
|
||||
m_name.Flatten();
|
||||
m_tooltip.Flatten();
|
||||
m_returnType.Flatten();
|
||||
for (Parameter& parameter : m_parameters)
|
||||
{
|
||||
parameter.Flatten();
|
||||
}
|
||||
}
|
||||
} // namespace ScriptEvents
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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 "ScriptEvents/ScriptEventParameter.h"
|
||||
|
||||
#include <AzCore/RTTI/BehaviorContext.h>
|
||||
#include <AzCore/Serialization/EditContext.h>
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
#include <AzCore/std/string/regex.h>
|
||||
|
||||
namespace ScriptEvents
|
||||
{
|
||||
void Parameter::FromScript(AZ::ScriptDataContext& dc)
|
||||
{
|
||||
if (dc.GetNumArguments() > 0)
|
||||
{
|
||||
AZStd::string name;
|
||||
if (dc.ReadArg(0, name))
|
||||
{
|
||||
m_name.Set(name.c_str());
|
||||
}
|
||||
|
||||
if (dc.GetNumArguments() > 1)
|
||||
{
|
||||
AZ::Uuid parameterType;
|
||||
if (dc.ReadArg(1, parameterType))
|
||||
{
|
||||
m_type.Set(parameterType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AZ_TracePrintf("Script Events", "Added Parameter: %s (type: %s)\n", GetName().c_str(), GetType().ToString<AZStd::string>()
|
||||
// .c_str());
|
||||
}
|
||||
|
||||
void Parameter::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
|
||||
{
|
||||
serializeContext->Class<Parameter>()
|
||||
->Field("m_name", &Parameter::m_name)
|
||||
->Field("m_tooltip", &Parameter::m_tooltip)
|
||||
->Field("m_type", &Parameter::m_type);
|
||||
|
||||
if (AZ::EditContext* editContext = serializeContext->GetEditContext())
|
||||
{
|
||||
editContext->Class<Parameter>("A Script Event's method parameter", "A parameter to a Script Event's event definition")
|
||||
->DataElement(
|
||||
AZ::Edit::UIHandlers::Default, &Parameter::m_name, "Name",
|
||||
"Name of the parameter, ex. void foo(int thisIsTheParameterName)")
|
||||
->DataElement(AZ::Edit::UIHandlers::Default, &Parameter::m_tooltip, "Tooltip", "A description of this parameter")
|
||||
->DataElement(
|
||||
AZ::Edit::UIHandlers::ComboBox, &Parameter::m_type, "Type",
|
||||
"The typeid of the parameter, ex. void foo(AZ::type_info<int>::Uuid())")
|
||||
->Attribute(AZ::Edit::Attributes::GenericValueList, &Types::GetValidParameterTypes);
|
||||
}
|
||||
}
|
||||
|
||||
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
|
||||
{
|
||||
behaviorContext->Class<Parameter>("Parameter")
|
||||
->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
|
||||
->Property("Name", BehaviorValueProperty(&Parameter::m_name))
|
||||
->Property("Type", BehaviorValueProperty(&Parameter::m_type));
|
||||
}
|
||||
}
|
||||
|
||||
AZ::Outcome<bool, AZStd::string> Parameter::Validate() const
|
||||
{
|
||||
const AZStd::string& name = GetName();
|
||||
const AZ::Uuid* parameterType = m_type.Get<const AZ::Uuid>();
|
||||
|
||||
AZ_Assert(parameterType && !parameterType->IsNull(), "The Parameter type should not be null");
|
||||
|
||||
// Validate address type
|
||||
if (!Types::IsValidParameterType(*parameterType))
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format(
|
||||
"The specified type %s is not valid as parameter type for Script Event: %s",
|
||||
(*parameterType).ToString<AZStd::string>().c_str(), name.c_str()));
|
||||
}
|
||||
|
||||
// Definition name cannot be empty
|
||||
if (name.empty())
|
||||
{
|
||||
return AZ::Failure(AZStd::string("Definition name cannot be empty"));
|
||||
}
|
||||
|
||||
// Name cannot start with a number
|
||||
if (isdigit(name.at(0)))
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, names cannot start with a number", name.c_str()));
|
||||
}
|
||||
|
||||
// Conform to valid function names
|
||||
AZStd::smatch match;
|
||||
|
||||
// Ascii-only
|
||||
AZStd::regex asciionly_regex("[^\x0A\x0D\x20-\x7E]");
|
||||
AZStd::regex_match(name, match, asciionly_regex);
|
||||
if (match.size() > 0)
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, invalid name, names may only contain ASCII characters", name.c_str()));
|
||||
}
|
||||
|
||||
// Function name syntax
|
||||
AZStd::regex validate_regex("[_[:alpha:]][_[:alnum:]]*");
|
||||
AZStd::regex_match(name, match, validate_regex);
|
||||
if (match.size() == 0)
|
||||
{
|
||||
return AZ::Failure(AZStd::string::format("%s, invalid name specified", name.c_str()));
|
||||
}
|
||||
|
||||
return AZ::Success(true);
|
||||
}
|
||||
} // namespace ScriptEvents
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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 "ScriptEvents/ScriptEventsAssetRef.h"
|
||||
|
||||
#include <AzCore/Asset/AssetSerializer.h>
|
||||
#include <AzCore/RTTI/BehaviorContext.h>
|
||||
#include <AzCore/Serialization/SerializeContext.h>
|
||||
|
||||
namespace ScriptEvents
|
||||
{
|
||||
void ScriptEventsAssetRef::Reflect(AZ::ReflectContext* context)
|
||||
{
|
||||
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
|
||||
{
|
||||
serializeContext->Class<ScriptEventsAssetRef>()->Version(0)->Field("Asset", &ScriptEventsAssetRef::m_asset);
|
||||
|
||||
if (AZ::EditContext* editContext = serializeContext->GetEditContext())
|
||||
{
|
||||
editContext->Class<ScriptEventsAssetRef>("Script Event Asset", "")
|
||||
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
|
||||
->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
|
||||
->DataElement(AZ::Edit::UIHandlers::Default, &ScriptEventsAssetRef::m_asset, "Script Event Asset", "")
|
||||
->Attribute(AZ::Edit::Attributes::ChangeNotify, &ScriptEventsAssetRef::OnAssetChanged)
|
||||
// TODO #lsempe: hook up to open Asset Editor when ready
|
||||
//->Attribute("EditButton", "")
|
||||
//->Attribute("EditDescription", "Open in Script Canvas Editor")
|
||||
//->Attribute("EditCallback", &ScriptEventsAssetRef::LaunchScriptCanvasEditor)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
|
||||
{
|
||||
behaviorContext->Class<ScriptEventsAssetRef>()
|
||||
->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
|
||||
->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
|
||||
->Attribute(AZ::Script::Attributes::ConstructibleFromNil, false)
|
||||
->Method("Get", &ScriptEventsAssetRef::GetDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEventsAssetRef::SetAsset(const AZ::Data::Asset<ScriptEventsAsset>& asset)
|
||||
{
|
||||
m_asset = asset;
|
||||
|
||||
if (m_asset.IsReady())
|
||||
{
|
||||
if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
|
||||
{
|
||||
scriptEventAsset->m_definition.RegisterInternal();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (AZ::Data::AssetBus::Handler::BusIsConnectedId(m_asset.GetId()))
|
||||
{
|
||||
AZ::Data::AssetBus::Handler::BusDisconnect(m_asset.GetId());
|
||||
}
|
||||
|
||||
AZ::Data::AssetBus::Handler::BusConnect(m_asset.GetId());
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEventsAssetRef::Load(bool loadBlocking /*= false*/)
|
||||
{
|
||||
if (!m_asset.IsReady())
|
||||
{
|
||||
AZ::Data::AssetInfo assetInfo;
|
||||
AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, m_asset.GetId());
|
||||
if (assetInfo.m_assetId.IsValid())
|
||||
{
|
||||
auto& assetManager = AZ::Data::AssetManager::Instance();
|
||||
m_asset = assetManager.GetAsset(m_asset.GetId(), azrtti_typeid<ScriptEventsAsset>(), m_asset.GetAutoLoadBehavior());
|
||||
|
||||
if(loadBlocking)
|
||||
{
|
||||
m_asset.BlockUntilLoadComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AZ::u32 ScriptEventsAssetRef::OnAssetChanged()
|
||||
{
|
||||
SetAsset(m_asset);
|
||||
Load(false);
|
||||
|
||||
if (m_assetNotifyCallback)
|
||||
{
|
||||
m_assetNotifyCallback(m_asset, m_userData);
|
||||
}
|
||||
|
||||
return AZ::Edit::PropertyRefreshLevels::None;
|
||||
}
|
||||
|
||||
void ScriptEventsAssetRef::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
|
||||
{
|
||||
if (ScriptEventsAsset* scriptEventAsset = m_asset.GetAs<ScriptEventsAsset>())
|
||||
{
|
||||
scriptEventAsset->m_definition.RegisterInternal();
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEventsAssetRef::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
|
||||
{
|
||||
SetAsset(asset);
|
||||
|
||||
if (m_assetNotifyCallback)
|
||||
{
|
||||
m_assetNotifyCallback(m_asset, m_userData);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEventsAssetRef::OnAssetUnloaded(
|
||||
[[maybe_unused]] const AZ::Data::AssetId assetId, [[maybe_unused]] const AZ::Data::AssetType assetType)
|
||||
{
|
||||
if (ScriptEventsAsset* ebusAsset = m_asset.GetAs<ScriptEventsAsset>())
|
||||
{
|
||||
bool isRegistered = false;
|
||||
// ScriptEventsLegacy::RegistrationRequestBus::BroadcastResult(isRegistered,
|
||||
// &ScriptEventsLegacy::RegistrationRequestBus::Events::IsBusRegistered, ebusAsset->m_scriptEventsDefinition.m_name);
|
||||
if (isRegistered)
|
||||
{
|
||||
// ScriptEventsLegacy::RegistrationRequestBus::Broadcast(&ScriptEventsLegacy::RegistrationRequestBus::Events::Unregister,
|
||||
// ebusAsset->m_scriptEventsDefinition.m_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace ScriptEvents
|
||||
Reference in New Issue
Block a user