diff --git a/Templates/DefaultGem/Template/Code/Include/${Name}/${Name}Bus.h b/Templates/DefaultGem/Template/Code/Include/${Name}/${Name}Bus.h index 0e8ab29cc4..1b3b67c241 100644 --- a/Templates/DefaultGem/Template/Code/Include/${Name}/${Name}Bus.h +++ b/Templates/DefaultGem/Template/Code/Include/${Name}/${Name}Bus.h @@ -16,9 +16,9 @@ #include -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}Requests + class ${SanitizedCppName}Requests : public AZ::EBusTraits { public: @@ -31,6 +31,6 @@ namespace ${Name} // Put your public methods here }; - using ${Name}RequestBus = AZ::EBus<${Name}Requests>; + using ${SanitizedCppName}RequestBus = AZ::EBus<${SanitizedCppName}Requests>; -} // namespace ${Name} +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.cpp b/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.cpp index 5884489904..4fca5e8d3c 100644 --- a/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.cpp +++ b/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.cpp @@ -15,24 +15,24 @@ #include #include <${Name}EditorSystemComponent.h> -namespace ${Name} +namespace ${SanitizedCppName} { - void ${Name}EditorSystemComponent::Reflect(AZ::ReflectContext* context) + void ${SanitizedCppName}EditorSystemComponent::Reflect(AZ::ReflectContext* context) { if (auto serializeContext = azrtti_cast(context)) { - serializeContext->Class<${Name}EditorSystemComponent, AZ::Component>()->Version(1); + serializeContext->Class<${SanitizedCppName}EditorSystemComponent, AZ::Component>()->Version(1); } } - void ${Name}EditorSystemComponent::Activate() + void ${SanitizedCppName}EditorSystemComponent::Activate() { AzToolsFramework::EditorEvents::Bus::Handler::BusConnect(); } - void ${Name}EditorSystemComponent::Deactivate() + void ${SanitizedCppName}EditorSystemComponent::Deactivate() { AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect(); } -} // namespace ${Name} +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.h b/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.h index 1f973651ee..296d2c3bcd 100644 --- a/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.h +++ b/Templates/DefaultGem/Template/Code/Source/${Name}EditorSystemComponent.h @@ -18,32 +18,32 @@ #include -namespace ${Name} +namespace ${SanitizedCppName} { - /// System component for ${Name} editor - class ${Name}EditorSystemComponent + /// System component for ${SanitizedCppName} editor + class ${SanitizedCppName}EditorSystemComponent : public AZ::Component , private AzToolsFramework::EditorEvents::Bus::Handler { public: - AZ_COMPONENT(${Name}EditorSystemComponent, "${EditorSysCompClassId}"); + AZ_COMPONENT(${SanitizedCppName}EditorSystemComponent, "${EditorSysCompClassId}"); static void Reflect(AZ::ReflectContext* context); - ${Name}EditorSystemComponent() = default; + ${SanitizedCppName}EditorSystemComponent() = default; private: static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { - provided.push_back(AZ_CRC("${Name}EditorService")); + provided.push_back(AZ_CRC("${SanitizedCppName}EditorService")); } static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) { - required.push_back(AZ_CRC("${Name}Service")); + required.push_back(AZ_CRC("${SanitizedCppName}Service")); } // AZ::Component void Activate() override; void Deactivate() override; }; -} // namespace ${Name} +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultGem/Template/Code/Source/${Name}Module.cpp b/Templates/DefaultGem/Template/Code/Source/${Name}Module.cpp index 3c6be88158..ca4a724f49 100644 --- a/Templates/DefaultGem/Template/Code/Source/${Name}Module.cpp +++ b/Templates/DefaultGem/Template/Code/Source/${Name}Module.cpp @@ -17,21 +17,21 @@ #include <${Name}SystemComponent.h> -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}Module + class ${SanitizedCppName}Module : public AZ::Module { public: - AZ_RTTI(${Name}Module, "${ModuleClassId}", AZ::Module); - AZ_CLASS_ALLOCATOR(${Name}Module, AZ::SystemAllocator, 0); + AZ_RTTI(${SanitizedCppName}Module, "${ModuleClassId}", AZ::Module); + AZ_CLASS_ALLOCATOR(${SanitizedCppName}Module, AZ::SystemAllocator, 0); - ${Name}Module() + ${SanitizedCppName}Module() : AZ::Module() { // Push results of [MyComponent]::CreateDescriptor() into m_descriptors here. m_descriptors.insert(m_descriptors.end(), { - ${Name}SystemComponent::CreateDescriptor(), + ${SanitizedCppName}SystemComponent::CreateDescriptor(), }); } @@ -41,10 +41,10 @@ namespace ${Name} AZ::ComponentTypeList GetRequiredSystemComponents() const override { return AZ::ComponentTypeList { - azrtti_typeid<${Name}SystemComponent>(), + azrtti_typeid<${SanitizedCppName}SystemComponent>(), }; } }; -}// namespace ${Name} +}// namespace ${SanitizedCppName} -AZ_DECLARE_MODULE_CLASS(Gem_${Name}, ${Name}::${Name}Module) +AZ_DECLARE_MODULE_CLASS(Gem_${SanitizedCppName}, ${SanitizedCppName}::${SanitizedCppName}Module) diff --git a/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.cpp b/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.cpp index 7f91197e4a..214d2cfe39 100644 --- a/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.cpp +++ b/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.cpp @@ -18,19 +18,19 @@ #include #include -namespace ${Name} +namespace ${SanitizedCppName} { - void ${Name}SystemComponent::Reflect(AZ::ReflectContext* context) + void ${SanitizedCppName}SystemComponent::Reflect(AZ::ReflectContext* context) { if (AZ::SerializeContext* serialize = azrtti_cast(context)) { - serialize->Class<${Name}SystemComponent, AZ::Component>() + serialize->Class<${SanitizedCppName}SystemComponent, AZ::Component>() ->Version(0) ; if (AZ::EditContext* ec = serialize->GetEditContext()) { - ec->Class<${Name}SystemComponent>("${Name}", "[Description of functionality provided by this System Component]") + ec->Class<${SanitizedCppName}SystemComponent>("${SanitizedCppName}", "[Description of functionality provided by this System Component]") ->ClassElement(AZ::Edit::ClassElements::EditorData, "") ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System")) ->Attribute(AZ::Edit::Attributes::AutoExpand, true) @@ -39,45 +39,45 @@ namespace ${Name} } } - void ${Name}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + void ${SanitizedCppName}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { - provided.push_back(AZ_CRC("${Name}Service")); + provided.push_back(AZ_CRC("${SanitizedCppName}Service")); } - void ${Name}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) + void ${SanitizedCppName}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) { - incompatible.push_back(AZ_CRC("${Name}Service")); + incompatible.push_back(AZ_CRC("${SanitizedCppName}Service")); } - void ${Name}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) + void ${SanitizedCppName}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) { AZ_UNUSED(required); } - void ${Name}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent) + void ${SanitizedCppName}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent) { AZ_UNUSED(dependent); } - void ${Name}SystemComponent::Init() + void ${SanitizedCppName}SystemComponent::Init() { } - void ${Name}SystemComponent::Activate() + void ${SanitizedCppName}SystemComponent::Activate() { - ${Name}RequestBus::Handler::BusConnect(); + ${SanitizedCppName}RequestBus::Handler::BusConnect(); AZ::TickBus::Handler::BusConnect(); } - void ${Name}SystemComponent::Deactivate() + void ${SanitizedCppName}SystemComponent::Deactivate() { AZ::TickBus::Handler::BusDisconnect(); - ${Name}RequestBus::Handler::BusDisconnect(); + ${SanitizedCppName}RequestBus::Handler::BusDisconnect(); } - void ${Name}SystemComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/) + void ${SanitizedCppName}SystemComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/) { } -} // namespace ${Name} +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.h b/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.h index 5071e7b9c7..5eff2e6430 100644 --- a/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.h +++ b/Templates/DefaultGem/Template/Code/Source/${Name}SystemComponent.h @@ -18,15 +18,15 @@ #include #include <${Name}/${Name}Bus.h> -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}SystemComponent + class ${SanitizedCppName}SystemComponent : public AZ::Component - , protected ${Name}RequestBus::Handler + , protected ${SanitizedCppName}RequestBus::Handler , public AZ::TickBus::Handler { public: - AZ_COMPONENT(${Name}SystemComponent, "${SysCompClassId}"); + AZ_COMPONENT(${SanitizedCppName}SystemComponent, "${SysCompClassId}"); static void Reflect(AZ::ReflectContext* context); @@ -37,7 +37,7 @@ namespace ${Name} protected: //////////////////////////////////////////////////////////////////////// - // ${Name}RequestBus interface implementation + // ${SanitizedCppName}RequestBus interface implementation //////////////////////////////////////////////////////////////////////// @@ -54,4 +54,4 @@ namespace ${Name} //////////////////////////////////////////////////////////////////////// }; -} // namespace ${Name} +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultGem/Template/Code/Tests/${Name}EditorTest.cpp b/Templates/DefaultGem/Template/Code/Tests/${Name}EditorTest.cpp index 7fd3fa7cfa..9b36a3aadb 100644 --- a/Templates/DefaultGem/Template/Code/Tests/${Name}EditorTest.cpp +++ b/Templates/DefaultGem/Template/Code/Tests/${Name}EditorTest.cpp @@ -14,7 +14,7 @@ #include -class ${Name}EditorTest +class ${SanitizedCppName}EditorTest : public ::testing::Test { protected: @@ -29,7 +29,7 @@ protected: } }; -TEST_F(${Name}EditorTest, SanityTest) +TEST_F(${SanitizedCppName}EditorTest, SanityTest) { ASSERT_TRUE(true); } diff --git a/Templates/DefaultGem/Template/Code/Tests/${Name}Test.cpp b/Templates/DefaultGem/Template/Code/Tests/${Name}Test.cpp index 8021ca920e..bee38fa9d7 100644 --- a/Templates/DefaultGem/Template/Code/Tests/${Name}Test.cpp +++ b/Templates/DefaultGem/Template/Code/Tests/${Name}Test.cpp @@ -14,7 +14,7 @@ #include -class ${Name}Test +class ${SanitizedCppName}Test : public ::testing::Test { protected: @@ -29,7 +29,7 @@ protected: } }; -TEST_F(${Name}Test, SanityTest) +TEST_F(${SanitizedCppName}Test, SanityTest) { ASSERT_TRUE(true); } diff --git a/Templates/DefaultProject/Template/Code/Include/${Name}/${Name}Bus.h b/Templates/DefaultProject/Template/Code/Include/${Name}/${Name}Bus.h index faa0e8acf6..e6f9dd57bd 100644 --- a/Templates/DefaultProject/Template/Code/Include/${Name}/${Name}Bus.h +++ b/Templates/DefaultProject/Template/Code/Include/${Name}/${Name}Bus.h @@ -16,9 +16,9 @@ #include -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}Requests + class ${SanitizedCppName}Requests : public AZ::EBusTraits { public: @@ -30,5 +30,5 @@ namespace ${Name} // Put your public methods here }; - using ${Name}RequestBus = AZ::EBus<${Name}Requests>; -} // namespace ${Name} + using ${SanitizedCppName}RequestBus = AZ::EBus<${SanitizedCppName}Requests>; +} // namespace ${SanitizedCppName} diff --git a/Templates/DefaultProject/Template/Code/Source/${Name}Module.cpp b/Templates/DefaultProject/Template/Code/Source/${Name}Module.cpp index 4f11828366..57b473b317 100644 --- a/Templates/DefaultProject/Template/Code/Source/${Name}Module.cpp +++ b/Templates/DefaultProject/Template/Code/Source/${Name}Module.cpp @@ -17,21 +17,21 @@ #include "${Name}SystemComponent.h" -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}Module + class ${SanitizedCppName}Module : public AZ::Module { public: - AZ_RTTI(${Name}Module, "${ModuleClassId}", AZ::Module); - AZ_CLASS_ALLOCATOR(${Name}Module, AZ::SystemAllocator, 0); + AZ_RTTI(${SanitizedCppName}Module, "${ModuleClassId}", AZ::Module); + AZ_CLASS_ALLOCATOR(${SanitizedCppName}Module, AZ::SystemAllocator, 0); - ${Name}Module() + ${SanitizedCppName}Module() : AZ::Module() { // Push results of [MyComponent]::CreateDescriptor() into m_descriptors here. m_descriptors.insert(m_descriptors.end(), { - ${Name}SystemComponent::CreateDescriptor(), + ${SanitizedCppName}SystemComponent::CreateDescriptor(), }); } @@ -41,10 +41,10 @@ namespace ${Name} AZ::ComponentTypeList GetRequiredSystemComponents() const override { return AZ::ComponentTypeList{ - azrtti_typeid<${Name}SystemComponent>(), + azrtti_typeid<${SanitizedCppName}SystemComponent>(), }; } }; -}// namespace ${Name} +}// namespace ${SanitizedCppName} -AZ_DECLARE_MODULE_CLASS(Gem_${Name}, ${Name}::${Name}Module) +AZ_DECLARE_MODULE_CLASS(Gem_${SanitizedCppName}, ${SanitizedCppName}::${SanitizedCppName}Module) diff --git a/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.cpp b/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.cpp index 1e505f743c..82f7d7d17b 100644 --- a/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.cpp +++ b/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.cpp @@ -18,19 +18,19 @@ #include "${Name}SystemComponent.h" -namespace ${Name} +namespace ${SanitizedCppName} { - void ${Name}SystemComponent::Reflect(AZ::ReflectContext* context) + void ${SanitizedCppName}SystemComponent::Reflect(AZ::ReflectContext* context) { if (AZ::SerializeContext* serialize = azrtti_cast(context)) { - serialize->Class<${Name}SystemComponent, AZ::Component>() + serialize->Class<${SanitizedCppName}SystemComponent, AZ::Component>() ->Version(0) ; if (AZ::EditContext* ec = serialize->GetEditContext()) { - ec->Class<${Name}SystemComponent>("${Name}", "[Description of functionality provided by this System Component]") + ec->Class<${SanitizedCppName}SystemComponent>("${SanitizedCppName}", "[Description of functionality provided by this System Component]") ->ClassElement(AZ::Edit::ClassElements::EditorData, "") ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System")) ->Attribute(AZ::Edit::Attributes::AutoExpand, true) @@ -39,37 +39,37 @@ namespace ${Name} } } - void ${Name}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + void ${SanitizedCppName}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { - provided.push_back(AZ_CRC("${Name}Service")); + provided.push_back(AZ_CRC("${SanitizedCppName}Service")); } - void ${Name}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) + void ${SanitizedCppName}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) { - incompatible.push_back(AZ_CRC("${Name}Service")); + incompatible.push_back(AZ_CRC("${SanitizedCppName}Service")); } - void ${Name}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) + void ${SanitizedCppName}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) { AZ_UNUSED(required); } - void ${Name}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent) + void ${SanitizedCppName}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent) { AZ_UNUSED(dependent); } - void ${Name}SystemComponent::Init() + void ${SanitizedCppName}SystemComponent::Init() { } - void ${Name}SystemComponent::Activate() + void ${SanitizedCppName}SystemComponent::Activate() { - ${Name}RequestBus::Handler::BusConnect(); + ${SanitizedCppName}RequestBus::Handler::BusConnect(); } - void ${Name}SystemComponent::Deactivate() + void ${SanitizedCppName}SystemComponent::Deactivate() { - ${Name}RequestBus::Handler::BusDisconnect(); + ${SanitizedCppName}RequestBus::Handler::BusDisconnect(); } } diff --git a/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.h b/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.h index 39ccbda549..4df74027be 100644 --- a/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.h +++ b/Templates/DefaultProject/Template/Code/Source/${Name}SystemComponent.h @@ -18,14 +18,14 @@ #include <${Name}/${Name}Bus.h> -namespace ${Name} +namespace ${SanitizedCppName} { - class ${Name}SystemComponent + class ${SanitizedCppName}SystemComponent : public AZ::Component - , protected ${Name}RequestBus::Handler + , protected ${SanitizedCppName}RequestBus::Handler { public: - AZ_COMPONENT(${Name}SystemComponent, "${SysCompClassId}"); + AZ_COMPONENT(${SanitizedCppName}SystemComponent, "${SysCompClassId}"); static void Reflect(AZ::ReflectContext* context); @@ -36,7 +36,7 @@ namespace ${Name} protected: //////////////////////////////////////////////////////////////////////// - // ${Name}RequestBus interface implementation + // ${SanitizedCppName}RequestBus interface implementation //////////////////////////////////////////////////////////////////////// diff --git a/scripts/o3de/o3de/engine_template.py b/scripts/o3de/o3de/engine_template.py index 9bb62eff35..05d6c5f8b9 100755 --- a/scripts/o3de/o3de/engine_template.py +++ b/scripts/o3de/o3de/engine_template.py @@ -1232,10 +1232,12 @@ def create_from_template(destination_path: str, with_this = replace.pop(0) replacements.append((replace_this, with_this)) + sanitized_cpp_name = utils.sanitize_identifier_for_cpp(destination_name) # dst name is Name replacements.append(("${Name}", destination_name)) replacements.append(("${NameUpper}", destination_name.upper())) replacements.append(("${NameLower}", destination_name.lower())) + replacements.append(("${SanitizedCppName}", sanitized_cpp_name)) if _instantiate_template(template_json_data, destination_name, @@ -1536,10 +1538,12 @@ def create_project(project_path: str, with_this = replace.pop(0) replacements.append((replace_this, with_this)) + sanitized_cpp_name = utils.sanitize_identifier_for_cpp(project_name) # project name replacements.append(("${Name}", project_name)) replacements.append(("${NameUpper}", project_name.upper())) replacements.append(("${NameLower}", project_name.lower())) + replacements.append(("${SanitizedCppName}", sanitized_cpp_name)) # module id is a uuid with { and - if module_id: @@ -1927,10 +1931,13 @@ def create_gem(gem_path: str, with_this = replace.pop(0) replacements.append((replace_this, with_this)) + sanitized_cpp_name = utils.sanitize_identifier_for_cpp(gem_name) # gem name replacements.append(("${Name}", gem_name)) replacements.append(("${NameUpper}", gem_name.upper())) replacements.append(("${NameLower}", gem_name.lower())) + replacements.append(("${SanitizedCppName}", sanitized_cpp_name)) + # module id is a uuid with { and - if module_id: diff --git a/scripts/o3de/o3de/utils.py b/scripts/o3de/o3de/utils.py index 4330de25b8..6bb536f0fd 100755 --- a/scripts/o3de/o3de/utils.py +++ b/scripts/o3de/o3de/utils.py @@ -36,6 +36,26 @@ def validate_identifier(identifier: str) -> bool: return True +def sanitize_identifier_for_cpp(identifier: str) -> str: + """ + Convert the provided identifier to a valid C++ identifier + :param identifier: the name which needs to to sanitized + :return: str: sanitized identifier + """ + if not identifier: + return '' + + sanitized_identifier = list(identifier) + if not (sanitized_identifier[0].isalpha() or sanitized_identifier[0] == '_'): + sanitized_identifier.insert(0, '_') + + for index, character in enumerate(sanitized_identifier): + if not (character.isalnum() or character == '_'): + sanitized_identifier[index] = '_' + + return "".join(sanitized_identifier) + + def validate_uuid4(uuid_string: str) -> bool: """ Determine if the uuid supplied is valid.