ScriptEventParameter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "ScriptEvents/ScriptEventParameter.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/std/string/regex.h>
  13. namespace ScriptEvents
  14. {
  15. void Parameter::FromScript(AZ::ScriptDataContext& dc)
  16. {
  17. if (dc.GetNumArguments() > 0)
  18. {
  19. AZStd::string name;
  20. if (dc.ReadArg(0, name))
  21. {
  22. m_name.Set(name.c_str());
  23. }
  24. if (dc.GetNumArguments() > 1)
  25. {
  26. AZ::Uuid parameterType;
  27. if (dc.ReadArg(1, parameterType))
  28. {
  29. m_type.Set(parameterType);
  30. }
  31. }
  32. }
  33. // AZ_TracePrintf("Script Events", "Added Parameter: %s (type: %s)\n", GetName().c_str(), GetType().ToString<AZStd::string>()
  34. // .c_str());
  35. }
  36. void Parameter::Reflect(AZ::ReflectContext* context)
  37. {
  38. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  39. {
  40. serializeContext->Class<Parameter>()
  41. ->Field("m_name", &Parameter::m_name)
  42. ->Field("m_tooltip", &Parameter::m_tooltip)
  43. ->Field("m_type", &Parameter::m_type);
  44. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  45. {
  46. editContext->Class<Parameter>("A Script Event's method parameter", "A parameter to a Script Event's event definition")
  47. ->DataElement(
  48. AZ::Edit::UIHandlers::Default, &Parameter::m_name, "Name",
  49. "Name of the parameter, ex. void foo(int thisIsTheParameterName)")
  50. ->DataElement(AZ::Edit::UIHandlers::Default, &Parameter::m_tooltip, "Tooltip", "A description of this parameter")
  51. ->DataElement(
  52. AZ::Edit::UIHandlers::ComboBox, &Parameter::m_type, "Type",
  53. "The typeid of the parameter, ex. void foo(AZ::type_info<int>::Uuid())")
  54. ->Attribute(AZ::Edit::Attributes::GenericValueList, &Types::GetValidParameterTypes);
  55. }
  56. }
  57. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  58. {
  59. behaviorContext->Class<Parameter>("Parameter")
  60. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  61. ->Property("Name", BehaviorValueProperty(&Parameter::m_name))
  62. ->Property("Type", BehaviorValueProperty(&Parameter::m_type));
  63. }
  64. }
  65. AZ::Outcome<bool, AZStd::string> Parameter::Validate() const
  66. {
  67. const AZStd::string& name = GetName();
  68. const AZ::Uuid* parameterType = m_type.Get<const AZ::Uuid>();
  69. AZ_Assert(parameterType && !parameterType->IsNull(), "The Parameter type should not be null");
  70. // Validate address type
  71. if (!Types::IsValidParameterType(*parameterType))
  72. {
  73. return AZ::Failure(AZStd::string::format(
  74. "The specified type %s is not valid as parameter type for Script Event: %s",
  75. (*parameterType).ToString<AZStd::string>().c_str(), name.c_str()));
  76. }
  77. // Definition name cannot be empty
  78. if (name.empty())
  79. {
  80. return AZ::Failure(AZStd::string("Definition name cannot be empty"));
  81. }
  82. // Name cannot start with a number
  83. if (isdigit(name.at(0)))
  84. {
  85. return AZ::Failure(AZStd::string::format("%s, names cannot start with a number", name.c_str()));
  86. }
  87. // Conform to valid function names
  88. AZStd::smatch match;
  89. // Ascii-only
  90. AZStd::regex asciionly_regex("[^\x0A\x0D\x20-\x7E]");
  91. AZStd::regex_match(name, match, asciionly_regex);
  92. if (match.size() > 0)
  93. {
  94. return AZ::Failure(AZStd::string::format("%s, invalid name, names may only contain ASCII characters", name.c_str()));
  95. }
  96. // Function name syntax
  97. AZStd::regex validate_regex("[_[:alpha:]][_[:alnum:]]*");
  98. AZStd::regex_match(name, match, validate_regex);
  99. if (match.size() == 0)
  100. {
  101. return AZ::Failure(AZStd::string::format("%s, invalid name specified", name.c_str()));
  102. }
  103. return AZ::Success(true);
  104. }
  105. } // namespace ScriptEvents