diff --git a/.github/ISSUE_TEMPLATE/ar_bug_report.md b/.github/ISSUE_TEMPLATE/ar_bug_report.md index 0d4aee9397..93b6e70b03 100644 --- a/.github/ISSUE_TEMPLATE/ar_bug_report.md +++ b/.github/ISSUE_TEMPLATE/ar_bug_report.md @@ -1,5 +1,5 @@ --- -name: ar_bug_report.md +name: Automated Review bug report about: Create a bug for a an issue found in the Automated Review title: 'AR Bug Report' labels: 'needs-triage,kind/bug,kind/automation' diff --git a/.gitignore b/.gitignore index 5f6172cd76..9012ec7576 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ .vscode/ __pycache__ AssetProcessorTemp/** -[Bb]uild/** +[Bb]uild/ [Oo]ut/** CMakeUserPresets.json [Cc]ache/ diff --git a/Assets/Editor/Translation/scriptcanvas_en_us.ts b/Assets/Editor/Translation/scriptcanvas_en_us.ts index 7b8361c879..6d436b7caf 100644 --- a/Assets/Editor/Translation/scriptcanvas_en_us.ts +++ b/Assets/Editor/Translation/scriptcanvas_en_us.ts @@ -62164,7 +62164,7 @@ An Entity can be selected by using the pick button, or by dragging an Entity fro HANDLER_TAGGLOBALNOTIFICATIONBUS_ONENTITYTAGADDED_OUTPUT0_NAME Simple Type: EntityID C++ Type: const EntityId& - Entity + EntityID HANDLER_TAGGLOBALNOTIFICATIONBUS_ONENTITYTAGADDED_OUTPUT0_TOOLTIP @@ -62202,7 +62202,7 @@ An Entity can be selected by using the pick button, or by dragging an Entity fro HANDLER_TAGGLOBALNOTIFICATIONBUS_ONENTITYTAGREMOVED_OUTPUT0_NAME Simple Type: EntityID C++ Type: const EntityId& - Entity + EntityId HANDLER_TAGGLOBALNOTIFICATIONBUS_ONENTITYTAGREMOVED_OUTPUT0_TOOLTIP @@ -81852,7 +81852,7 @@ The element is removed from its current parent and added as a child of the new p HANDLER_SPAWNERCOMPONENTNOTIFICATIONBUS_ONENTITYSPAWNED_OUTPUT1_NAME Simple Type: EntityID C++ Type: const EntityId& - Entity + EntityID HANDLER_SPAWNERCOMPONENTNOTIFICATIONBUS_ONENTITYSPAWNED_OUTPUT1_TOOLTIP @@ -89198,7 +89198,7 @@ The element is removed from its current parent and added as a child of the new p HANDLER_ENTITYBUS_ONENTITYACTIVATED_OUTPUT0_NAME Simple Type: EntityID C++ Type: const EntityId& - Entity + EntityID HANDLER_ENTITYBUS_ONENTITYACTIVATED_OUTPUT0_TOOLTIP @@ -89236,7 +89236,7 @@ The element is removed from its current parent and added as a child of the new p HANDLER_ENTITYBUS_ONENTITYDEACTIVATED_OUTPUT0_NAME Simple Type: EntityID C++ Type: const EntityId& - Entity + EntityID HANDLER_ENTITYBUS_ONENTITYDEACTIVATED_OUTPUT0_TOOLTIP diff --git a/Assets/Engine/EngineAssets/Slices/DefaultLevelSetup.slice b/Assets/Engine/EngineAssets/Slices/DefaultLevelSetup.slice index 1b7dfdf40d..b82c482c4f 100644 --- a/Assets/Engine/EngineAssets/Slices/DefaultLevelSetup.slice +++ b/Assets/Engine/EngineAssets/Slices/DefaultLevelSetup.slice @@ -145,7 +145,7 @@ - + diff --git a/Assets/Engine/Entities/GeomCache.ent b/Assets/Engine/Entities/GeomCache.ent deleted file mode 100644 index e7a63190c3..0000000000 --- a/Assets/Engine/Entities/GeomCache.ent +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cf441215a769562f88aa20711aee68dadcbf02597d1e2270547055e8e6aec6a3 -size 77 diff --git a/Assets/Engine/Scripts/Entities/Render/GeomCache.lua b/Assets/Engine/Scripts/Entities/Render/GeomCache.lua deleted file mode 100644 index b496aecd8d..0000000000 --- a/Assets/Engine/Scripts/Entities/Render/GeomCache.lua +++ /dev/null @@ -1,178 +0,0 @@ ----------------------------------------------------------------------------------------------------- --- --- 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 --- --- --- ----------------------------------------------------------------------------------------------------- -Script.ReloadScript("scripts/Utils/EntityUtils.lua") - -GeomCache = -{ - Properties = { - geomcacheFile = "EngineAssets/GeomCaches/defaultGeomCache.cax", - bPlaying = 0, - fStartTime = 0, - bLooping = 0, - objectStandIn = "", - materialStandInMaterial = "", - objectFirstFrameStandIn = "", - materialFirstFrameStandInMaterial = "", - objectLastFrameStandIn = "", - materialLastFrameStandInMaterial = "", - fStandInDistance = 0, - fStreamInDistance = 0, - Physics = { - bPhysicalize = 0, - } - }, - - Editor={ - Icon = "animobject.bmp", - IconOnTop = 1, - }, - - bPlaying = 0, - currentTime = 0, - precacheTime = 0, - bPrecachedOutputTriggered = false, -} - -function GeomCache:OnLoad(table) - self.currentTime = table.currentTime; -end - -function GeomCache:OnSave(table) - table.currentTime = self.currentTime; -end - -function GeomCache:OnSpawn() - self.currentTime = self.Properties.fStartTime; - self:SetFromProperties(); -end - -function GeomCache:OnReset() - self.currentTime = self.Properties.fStartTime; - self.bPrecachedOutputTriggered = true; - self:SetFromProperties(); -end - -function GeomCache:SetFromProperties() - local Properties = self.Properties; - - if (Properties.geomcacheFile == "") then - do return end; - end - - self:LoadGeomCache(0, Properties.geomcacheFile); - - self.bPlaying = Properties.bPlaying; - if (self.bPlaying == 0) then - self.currentTime = Properties.fStartTime; - end - - self:SetGeomCachePlaybackTime(self.currentTime); - self:SetGeomCacheParams(Properties.bLooping, Properties.objectStandIn, Properties.materialStandInMaterial, Properties.objectFirstFrameStandIn, - Properties.materialFirstFrameStandInMaterial, Properties.objectLastFrameStandIn, Properties.materialLastFrameStandInMaterial, - Properties.fStandInDistance, Properties.fStreamInDistance); - self:SetGeomCacheStreaming(false, 0); - - if (Properties.Physics.bPhysicalize == 1) then - local tempPhysParams = EntityCommon.TempPhysParams; - self:Physicalize(0, PE_ARTICULATED, tempPhysParams); - end - - self:Activate(1); -end - -function GeomCache:PhysicalizeThis() - local Physics = self.Properties.Physics; - EntityCommon.PhysicalizeRigid(self, 0, Physics, false); -end - -function GeomCache:OnUpdate(dt) - if (self.bPlaying == 1) then - self:SetGeomCachePlaybackTime(self.currentTime); - end - - if (self:IsGeomCacheStreaming() and not self.bPrecachedOutputTriggered) then - local precachedTime = self:GetGeomCachePrecachedTime(); - if (precachedTime >= self.precacheTime) then - self:ActivateOutput("Precached", true); - self.bPrecachedOutputTriggered = true; - end - end - - if (self.bPlaying == 1) then - self.currentTime = self.currentTime + dt; - end -end - -function GeomCache:OnPropertyChange() - self:SetFromProperties(); -end - -function GeomCache:Event_Start(sender, val) - self.bPlaying = 1; -end - -function GeomCache:Event_Stop(sender, value) - self.bPlaying = 0; -end - -function GeomCache:Event_SetTime(sender, value) - self.currentTime = value; -end - -function GeomCache:Event_StartStreaming(sender, value) - self.bPrecachedOutputTriggered = false; - self:SetGeomCacheStreaming(true, self.currentTime); -end - -function GeomCache:Event_StopStreaming(sender, value) - self:SetGeomCacheStreaming(false, 0); -end - -function GeomCache:Event_PrecacheTime(sender, value) - self.precacheTime = value; -end - -function GeomCache:Event_Hide(sender, value) - self:Hide(1); -end - -function GeomCache:Event_Unhide(sender, value) - self:Hide(0); -end - -function GeomCache:Event_StopDrawing(sender, value) - self:SetGeomCacheDrawing(false); -end - -function GeomCache:Event_StartDrawing(sender, value) - self:SetGeomCacheDrawing(true); -end - -GeomCache.FlowEvents = -{ - Inputs = - { - Start = { GeomCache.Event_Start, "any" }, - Stop = { GeomCache.Event_Stop, "any" }, - SetTime = { GeomCache.Event_SetTime, "float" }, - StartStreaming = { GeomCache.Event_StartStreaming, "any" }, - StopStreaming = { GeomCache.Event_StopStreaming, "any" }, - PrecacheTime = { GeomCache.Event_PrecacheTime, "float" }, - Hide = { GeomCache.Event_Hide, "any" }, - Unhide = { GeomCache.Event_Unhide, "any" }, - StopDrawing = { GeomCache.Event_StopDrawing, "any" }, - StartDrawing = { GeomCache.Event_StartDrawing, "any" }, - }, - Outputs = - { - Precached = "bool", - }, -} diff --git a/AutomatedTesting/Assets/Physics/Collider_PxMeshAutoAssigned/SphereBot/r0-b_body.fbx.assetinfo b/AutomatedTesting/Assets/Physics/Collider_PxMeshAutoAssigned/SphereBot/R0-B_Body.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Assets/Physics/Collider_PxMeshAutoAssigned/SphereBot/r0-b_body.fbx.assetinfo rename to AutomatedTesting/Assets/Physics/Collider_PxMeshAutoAssigned/SphereBot/R0-B_Body.fbx.assetinfo diff --git a/AutomatedTesting/Assets/Physics/Collider_PxMeshConvexMeshCollides/SphereBot/r0-b_body.fbx.assetinfo b/AutomatedTesting/Assets/Physics/Collider_PxMeshConvexMeshCollides/SphereBot/R0-B_Body.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Assets/Physics/Collider_PxMeshConvexMeshCollides/SphereBot/r0-b_body.fbx.assetinfo rename to AutomatedTesting/Assets/Physics/Collider_PxMeshConvexMeshCollides/SphereBot/R0-B_Body.fbx.assetinfo diff --git a/AutomatedTesting/CMakeLists.txt b/AutomatedTesting/CMakeLists.txt index dee9d73aea..1c5382ba4b 100644 --- a/AutomatedTesting/CMakeLists.txt +++ b/AutomatedTesting/CMakeLists.txt @@ -8,11 +8,12 @@ if(NOT PROJECT_NAME) cmake_minimum_required(VERSION 3.20) + include(cmake/CompilerSettings.cmake) project(AutomatedTesting LANGUAGES C CXX VERSION 1.0.0.0 ) - include(EngineFinder.cmake OPTIONAL) + include(cmake/EngineFinder.cmake OPTIONAL) find_package(o3de REQUIRED) o3de_initialize() else() diff --git a/AutomatedTesting/Gem/Code/Source/AutoGen/NetworkTestPlayerComponent.AutoComponent.xml b/AutomatedTesting/Gem/Code/Source/AutoGen/NetworkTestPlayerComponent.AutoComponent.xml index 12c222add6..46d6191835 100644 --- a/AutomatedTesting/Gem/Code/Source/AutoGen/NetworkTestPlayerComponent.AutoComponent.xml +++ b/AutomatedTesting/Gem/Code/Source/AutoGen/NetworkTestPlayerComponent.AutoComponent.xml @@ -13,4 +13,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AutomatedTesting/Gem/Code/enabled_gems.cmake b/AutomatedTesting/Gem/Code/enabled_gems.cmake index e6a4d6ca37..3915fd36da 100644 --- a/AutomatedTesting/Gem/Code/enabled_gems.cmake +++ b/AutomatedTesting/Gem/Code/enabled_gems.cmake @@ -54,6 +54,7 @@ set(ENABLED_GEMS AWSMetrics PrefabBuilder AudioSystem + Terrain Profiler Multiplayer ) diff --git a/AutomatedTesting/Gem/PythonTests/AWS/Windows/aws_metrics/aws_metrics_automation_test.py b/AutomatedTesting/Gem/PythonTests/AWS/Windows/aws_metrics/aws_metrics_automation_test.py index 34b2217916..6f3113d771 100644 --- a/AutomatedTesting/Gem/PythonTests/AWS/Windows/aws_metrics/aws_metrics_automation_test.py +++ b/AutomatedTesting/Gem/PythonTests/AWS/Windows/aws_metrics/aws_metrics_automation_test.py @@ -14,6 +14,7 @@ from datetime import datetime import ly_test_tools.log.log_monitor from AWS.common import constants +from AWS.common.resource_mappings import AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY from .aws_metrics_custom_thread import AWSMetricsThread # fixture imports @@ -200,6 +201,59 @@ class TestAWSMetricsWindows(object): for thread in operational_threads: thread.join() + @pytest.mark.parametrize('level', ['AWS/Metrics']) + def test_realtime_and_batch_analytics_no_global_accountid(self, + level: str, + launcher: pytest.fixture, + asset_processor: pytest.fixture, + workspace: pytest.fixture, + aws_utils: pytest.fixture, + resource_mappings: pytest.fixture, + aws_metrics_utils: pytest.fixture): + """ + Verify that the metrics events are sent to CloudWatch and S3 for analytics. + """ + # Remove top-level account ID from resource mappings + resource_mappings.clear_select_keys([AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY]) + # Start Kinesis analytics application on a separate thread to avoid blocking the test. + kinesis_analytics_application_thread = AWSMetricsThread(target=update_kinesis_analytics_application_status, + args=(aws_metrics_utils, resource_mappings, True)) + kinesis_analytics_application_thread.start() + + log_monitor = setup(launcher, asset_processor) + + # Kinesis analytics application needs to be in the running state before we start the game launcher. + kinesis_analytics_application_thread.join() + launcher.args = ['+LoadLevel', level] + launcher.args.extend(['-rhi=null']) + start_time = datetime.utcnow() + with launcher.start(launch_ap=False): + monitor_metrics_submission(log_monitor) + + # Verify that real-time analytics metrics are delivered to CloudWatch. + aws_metrics_utils.verify_cloud_watch_delivery( + AWS_METRICS_FEATURE_NAME, + 'TotalLogins', + [], + start_time) + logger.info('Real-time metrics are sent to CloudWatch.') + + # Run time-consuming operations on separate threads to avoid blocking the test. + operational_threads = list() + operational_threads.append( + AWSMetricsThread(target=query_metrics_from_s3, + args=(aws_metrics_utils, resource_mappings))) + operational_threads.append( + AWSMetricsThread(target=verify_operational_metrics, + args=(aws_metrics_utils, resource_mappings, start_time))) + operational_threads.append( + AWSMetricsThread(target=update_kinesis_analytics_application_status, + args=(aws_metrics_utils, resource_mappings, False))) + for thread in operational_threads: + thread.start() + for thread in operational_threads: + thread.join() + @pytest.mark.parametrize('level', ['AWS/Metrics']) def test_unauthorized_user_request_rejected(self, level: str, diff --git a/AutomatedTesting/Gem/PythonTests/AWS/Windows/client_auth/aws_client_auth_automation_test.py b/AutomatedTesting/Gem/PythonTests/AWS/Windows/client_auth/aws_client_auth_automation_test.py index 198fb934d9..077a068a18 100644 --- a/AutomatedTesting/Gem/PythonTests/AWS/Windows/client_auth/aws_client_auth_automation_test.py +++ b/AutomatedTesting/Gem/PythonTests/AWS/Windows/client_auth/aws_client_auth_automation_test.py @@ -12,6 +12,7 @@ import pytest import ly_test_tools.log.log_monitor from AWS.common import constants +from AWS.common.resource_mappings import AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY # fixture imports from assetpipeline.ap_fixtures.asset_processor_fixture import asset_processor @@ -70,6 +71,41 @@ class TestAWSClientAuthWindows(object): halt_on_unexpected=True, ) assert result, 'Anonymous credentials fetched successfully.' + + @pytest.mark.parametrize('level', ['AWS/ClientAuth']) + def test_anonymous_credentials_no_global_accountid(self, + level: str, + launcher: pytest.fixture, + resource_mappings: pytest.fixture, + workspace: pytest.fixture, + asset_processor: pytest.fixture + ): + """ + Test to verify AWS Cognito Identity pool anonymous authorization. + + Setup: Updates resource mapping file using existing CloudFormation stacks. + Tests: Getting credentials when no credentials are configured + Verification: Log monitor looks for success credentials log. + """ + # Remove top-level account ID from resource mappings + resource_mappings.clear_select_keys([AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY]) + + asset_processor.start() + asset_processor.wait_for_idle() + + file_to_monitor = os.path.join(launcher.workspace.paths.project_log(), constants.GAME_LOG_NAME) + log_monitor = ly_test_tools.log.log_monitor.LogMonitor(launcher=launcher, log_file_path=file_to_monitor) + + launcher.args = ['+LoadLevel', level] + launcher.args.extend(['-rhi=null']) + + with launcher.start(launch_ap=False): + result = log_monitor.monitor_log_for_lines( + expected_lines=['(Script) - Success anonymous credentials'], + unexpected_lines=['(Script) - Fail anonymous credentials'], + halt_on_unexpected=True, + ) + assert result, 'Anonymous credentials fetched successfully.' def test_password_signin_credentials(self, launcher: pytest.fixture, diff --git a/AutomatedTesting/Gem/PythonTests/AWS/Windows/core/test_aws_resource_interaction.py b/AutomatedTesting/Gem/PythonTests/AWS/Windows/core/test_aws_resource_interaction.py index 949186ad50..59c517fd1c 100644 --- a/AutomatedTesting/Gem/PythonTests/AWS/Windows/core/test_aws_resource_interaction.py +++ b/AutomatedTesting/Gem/PythonTests/AWS/Windows/core/test_aws_resource_interaction.py @@ -18,6 +18,7 @@ import ly_test_tools.environment.process_utils as process_utils import ly_test_tools.o3de.asset_processor_utils as asset_processor_utils from AWS.common import constants +from AWS.common.resource_mappings import AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY # fixture imports from assetpipeline.ap_fixtures.asset_processor_fixture import asset_processor @@ -141,3 +142,51 @@ class TestAWSCoreAWSResourceInteraction(object): 'The expected file wasn\'t successfully downloaded.' # clean up the file directories. shutil.rmtree(s3_download_dir) + + @pytest.mark.parametrize('expected_lines', [ + ['(Script) - [S3] Head object request is done', + '(Script) - [S3] Head object success: Object example.txt is found.', + '(Script) - [S3] Get object success: Object example.txt is downloaded.', + '(Script) - [Lambda] Completed Invoke', + '(Script) - [Lambda] Invoke success: {"statusCode": 200, "body": {}}', + '(Script) - [DynamoDB] Results finished']]) + @pytest.mark.parametrize('unexpected_lines', [ + ['(Script) - [S3] Head object error: No response body.', + '(Script) - [S3] Get object error: Request validation failed, output file directory doesn\'t exist.', + '(Script) - Request validation failed, output file miss full path.', + '(Script) - ']]) + def test_scripting_behavior_no_global_accountid(self, + level: str, + launcher: pytest.fixture, + workspace: pytest.fixture, + asset_processor: pytest.fixture, + resource_mappings: pytest.fixture, + aws_utils: pytest.fixture, + expected_lines: typing.List[str], + unexpected_lines: typing.List[str]): + """ + Setup: Updates resource mapping file using existing CloudFormation stacks. + Tests: Interact with AWS S3, DynamoDB and Lambda services. + Verification: Script canvas nodes can communicate with AWS services successfully. + """ + + resource_mappings.clear_select_keys([AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY]) + log_monitor, s3_download_dir = setup(launcher, asset_processor) + write_test_data_to_dynamodb_table(resource_mappings, aws_utils) + + launcher.args = ['+LoadLevel', level] + launcher.args.extend(['-rhi=null']) + + with launcher.start(launch_ap=False): + result = log_monitor.monitor_log_for_lines( + expected_lines=expected_lines, + unexpected_lines=unexpected_lines, + halt_on_unexpected=True + ) + + assert result, "Expected lines weren't found." + + assert os.path.exists(os.path.join(s3_download_dir, 'output.txt')), \ + 'The expected file wasn\'t successfully downloaded.' + # clean up the file directories. + shutil.rmtree(s3_download_dir) diff --git a/AutomatedTesting/Gem/PythonTests/AWS/common/resource_mappings.py b/AutomatedTesting/Gem/PythonTests/AWS/common/resource_mappings.py index 5f01ecdbf8..988d5bf1fc 100644 --- a/AutomatedTesting/Gem/PythonTests/AWS/common/resource_mappings.py +++ b/AutomatedTesting/Gem/PythonTests/AWS/common/resource_mappings.py @@ -102,3 +102,17 @@ class ResourceMappings: def get_resource_name_id(self, resource_key: str): return self._resource_mappings[AWS_RESOURCE_MAPPINGS_KEY][resource_key]['Name/ID'] + + def clear_select_keys(self, resource_keys=None) -> None: + """ + Clears values from select resource mapping keys. + :param resource_keys: list of keys to clear out + """ + with open(self._resource_mapping_file_path) as file_content: + resource_mappings = json.load(file_content) + + for key in resource_keys: + resource_mappings[key] = '' + + with open(self._resource_mapping_file_path, 'w') as file_content: + json.dump(resource_mappings, file_content, indent=4) \ No newline at end of file diff --git a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main.py index d183ca12be..d79e144ae0 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main.py @@ -13,6 +13,8 @@ from ly_test_tools.o3de.editor_test import EditorSharedTest, EditorTestSuite @pytest.mark.parametrize("launcher_platform", ['windows_editor']) class TestAutomation(EditorTestSuite): + enable_prefab_system = False + @pytest.mark.test_case_id("C36525657") class AtomEditorComponents_BloomAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_BloomAdded as test_module @@ -41,6 +43,10 @@ class TestAutomation(EditorTestSuite): class AtomEditorComponents_DisplayMapperAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_DisplayMapperAdded as test_module + @pytest.mark.test_case_id("C36525661") + class AtomEditorComponents_EntityReferenceAdded(EditorSharedTest): + from Atom.tests import hydra_AtomEditorComponents_EntityReferenceAdded as test_module + @pytest.mark.test_case_id("C32078121") class AtomEditorComponents_ExposureControlAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_ExposureControlAdded as test_module @@ -57,10 +63,18 @@ class TestAutomation(EditorTestSuite): class AtomEditorComponents_HDRColorGradingAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_HDRColorGradingAdded as test_module + @pytest.mark.test_case_id("C32078116") + class AtomEditorComponents_HDRiSkyboxAdded(EditorSharedTest): + from Atom.tests import hydra_AtomEditorComponents_HDRiSkyboxAdded as test_module + @pytest.mark.test_case_id("C32078117") class AtomEditorComponents_LightAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_LightAdded as test_module + @pytest.mark.test_case_id("C36525662") + class AtomEditorComponents_LookModificationAdded(EditorSharedTest): + from Atom.tests import hydra_AtomEditorComponents_LookModificationAdded as test_module + @pytest.mark.test_case_id("C32078123") class AtomEditorComponents_MaterialAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_MaterialAdded as test_module @@ -98,5 +112,9 @@ class TestAutomation(EditorTestSuite): class AtomEditorComponents_ReflectionProbeAdded(EditorSharedTest): from Atom.tests import hydra_AtomEditorComponents_ReflectionProbeAdded as test_module + @pytest.mark.test_case_id("C36525666") + class AtomEditorComponents_SSAOAdded(EditorSharedTest): + from Atom.tests import hydra_AtomEditorComponents_SSAOAdded as test_module + class ShaderAssetBuilder_RecompilesShaderAsChainOfDependenciesChanges(EditorSharedTest): from Atom.tests import hydra_ShaderAssetBuilder_RecompilesShaderAsChainOfDependenciesChanges as test_module diff --git a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU.py b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU.py index f0e92c7e3e..6a220d1bd8 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU.py @@ -104,6 +104,7 @@ class TestAllComponentsIndepthTests(object): halt_on_unexpected=True, cfg_args=[level], null_renderer=False, + enable_prefab_system=False, ) similarity_threshold = 0.99 @@ -158,6 +159,7 @@ class TestAllComponentsIndepthTests(object): halt_on_unexpected=True, cfg_args=[level], null_renderer=False, + enable_prefab_system=False, ) similarity_threshold = 0.99 @@ -205,6 +207,7 @@ class TestPerformanceBenchmarkSuite(object): halt_on_unexpected=True, cfg_args=[level], null_renderer=False, + enable_prefab_system=False, ) aggregator = BenchmarkDataAggregator(workspace, logger, 'periodic') @@ -242,5 +245,6 @@ class TestMaterialEditor(object): halt_on_unexpected=False, null_renderer=False, cfg_args=[cfg_args], - log_file_name="MaterialEditor.log" + log_file_name="MaterialEditor.log", + enable_prefab_system=False, ) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU_Optimized.py b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU_Optimized.py index 568768e12e..7f97c132bb 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Main_GPU_Optimized.py @@ -23,6 +23,8 @@ class TestAutomation(EditorTestSuite): # Remove -autotest_mode from global_extra_cmdline_args since we need rendering for these tests. global_extra_cmdline_args = ["-BatchMode"] # Default is ["-BatchMode", "-autotest_mode"] + enable_prefab_system = False + @pytest.mark.test_case_id("C34603773") class AtomGPU_BasicLevelSetup_SetsUpLevel(EditorSharedTest): use_null_renderer = False # Default is True diff --git a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Sandbox.py b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Sandbox.py index 9ceb3c951f..292bb9c19c 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Sandbox.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/TestSuite_Sandbox.py @@ -85,6 +85,7 @@ class TestAtomEditorComponentsMain(object): halt_on_unexpected=True, null_renderer=True, cfg_args=cfg_args, + enable_prefab_system=False, ) @@ -155,5 +156,6 @@ class TestMaterialEditorBasicTests(object): halt_on_unexpected=True, null_renderer=True, log_file_name="MaterialEditor.log", + enable_prefab_system=False, ) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_component_helper.py b/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_component_helper.py index 50cc15f7e8..9f37873557 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_component_helper.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_component_helper.py @@ -172,8 +172,7 @@ def create_basic_atom_level(level_name): entity_position=default_position, components=["HDRi Skybox", "Global Skylight (IBL)"], parent_id=default_level.id) - global_skylight_asset_path = os.path.join( - "LightingPresets", "greenwich_park_02_4k_iblskyboxcm_iblspecular.exr.streamingimage") + global_skylight_asset_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") global_skylight_asset_value = asset.AssetCatalogRequestBus( bus.Broadcast, "GetAssetIdByPath", global_skylight_asset_path, math.Uuid(), False) global_skylight.get_set_test(0, "Controller|Configuration|Cubemap Texture", global_skylight_asset_value) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_constants.py b/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_constants.py index 817ff1fad0..ef9e90802b 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_constants.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/atom_utils/atom_constants.py @@ -57,11 +57,13 @@ class AtomComponentProperties: def camera(property: str = 'name') -> str: """ Camera component properties. + - 'Field of view': Sets the value for the camera's FOV (Field of View) in degrees, i.e. 60.0 :param property: From the last element of the property tree path. Default 'name' for component name string. :return: Full property path OR component name if no property specified. """ properties = { 'name': 'Camera', + 'Field of view': 'Controller|Configuration|Field of view' } return properties[property] @@ -202,11 +204,13 @@ class AtomComponentProperties: def grid(property: str = 'name') -> str: """ Grid component properties. + - 'Secondary Grid Spacing': The spacing value for the secondary grid, i.e. 1.0 :param property: From the last element of the property tree path. Default 'name' for component name string. :return: Full property path OR component name if no property specified. """ properties = { 'name': 'Grid', + 'Secondary Grid Spacing': 'Controller|Configuration|Secondary Grid Spacing', } return properties[property] @@ -231,11 +235,13 @@ class AtomComponentProperties: def hdri_skybox(property: str = 'name') -> str: """ HDRi Skybox component properties. + - 'Cubemap Texture': Asset.id for the cubemap texture to set. :param property: From the last element of the property tree path. Default 'name' for component name string. :return: Full property path OR component name if no property specified. """ properties = { 'name': 'HDRi Skybox', + 'Cubemap Texture': 'Controller|Configuration|Cubemap Texture', } return properties[property] @@ -259,12 +265,16 @@ class AtomComponentProperties: Look Modification component properties. Requires PostFX Layer component. - 'requires' a list of component names as strings required by this component. Use editor_entity_utils EditorEntity.add_components(list) to add this list of requirements.\n + - 'Enable look modification' Toggle active state of the component True/False + - 'Color Grading LUT' Asset.id for the LUT used for affecting level look. :param property: From the last element of the property tree path. Default 'name' for component name string. :return: Full property path OR component name if no property specified. """ properties = { 'name': 'Look Modification', 'requires': [AtomComponentProperties.postfx_layer()], + 'Enable look modification': 'Controller|Configuration|Enable look modification', + 'Color Grading LUT': 'Controller|Configuration|Color Grading LUT', } return properties[property] @@ -274,12 +284,14 @@ class AtomComponentProperties: Material component properties. Requires one of Actor OR Mesh component. - 'requires' a list of component names as strings required by this component. Only one of these is required at a time for this component.\n + - 'Material Asset': the material Asset.id of the material. :param property: From the last element of the property tree path. Default 'name' for component name string. :return: Full property path OR component name if no property specified. """ properties = { 'name': 'Material', 'requires': [AtomComponentProperties.actor(), AtomComponentProperties.mesh()], + 'Material Asset': 'Default Material|Material Asset', } return properties[property] diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_1.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_1.ppm index 0725999dcf..8695f6bb93 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_1.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_1.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:954d7d0df47c840a24e313893800eb3126d0c0d47c3380926776b51833778db7 +oid sha256:aee1fd4d5264e5ef1676b507409ce70af6358cf1ff368d9aeb17f7b2597dfbca size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_2.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_2.ppm index 3a45bd31e3..d45d3f1581 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_2.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_2.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e81c19128f42ba362a2d5f3ccf159dfbc942d67ceeb1ac8c21f295a6fd9d2ce5 +oid sha256:d4787cdafbcc2fe71c1cb3f1da53a249db839a9df539a9e88be43ccd6d8e4d6a size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_3.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_3.ppm index 15d679b784..0661ead69f 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_3.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_3.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5e20801213e065b6ea8c95ede81c23faa9b6dc70a2002dc5bced293e1bed989f +oid sha256:5fac5bf41c9b16b6fbd762868e5cf514376af92d6ef7ebb9e819f024f1a3e1a7 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_4.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_4.ppm index 85c083a386..a7fc77f0ec 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_4.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_4.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e250f812e594e5152bf2d6f23caa8b53b78276bfdf344d7a8d355dd96cb995c0 +oid sha256:7a23969670499524725535e8be7428b55b6f3e887cc24e2e903f7ea821a6d1a5 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_5.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_5.ppm index d575de761e..7404ab3ccc 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_5.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AreaLight_5.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:95be359041f8291c74b335297a4dfe9902a180510f24a181b15e1a5ba4d3b024 +oid sha256:2f1f4d8865c56ed7f96f339c39e5feb4e0dbc6c6a8b4a7843b4166381b06b00d size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AtomBasicLevelSetup.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AtomBasicLevelSetup.ppm index ef41b6cf77..acfbe57900 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AtomBasicLevelSetup.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/AtomBasicLevelSetup.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:07e09d3eb5bf0cee3d9b3752aaad40f3ead1dcc5ddd837a6226fadde55d57274 +oid sha256:5d4ee5641e19eef08dd6b93d2f4054a1aae2165325416ed2cbf0b8243f2c0b06 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_1.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_1.ppm index bbbd127929..3104088689 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_1.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_1.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:118e43e4b915e262726183467cc4b82f244565213fea5b6bfe02be07f0851ab1 +oid sha256:55c8f0d1790bb12660b7557630efca297b2a1b59e6c93167a2563da79e0a8255 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_2.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_2.ppm index 8e716fabcc..287ec406e1 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_2.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_2.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dc2ce3256a6552975962c9e113c52c1a22bf3817d417151f6f60640dd568e0fa +oid sha256:082ff368b621e12b083d96562a0889b11a1d683767a74296cbe6d8732830e9e8 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_3.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_3.ppm index 6b6a5a5d6e..9de8785f65 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_3.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_3.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:287d98890b35427688999760f9d066bcbff1a3bc9001534241dc212b32edabd8 +oid sha256:78cc62d89782899747875b41abee57c2efdfacf4c8af6511c88f82d76eaae4ca size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_4.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_4.ppm index eb05228cc2..93acc14dbc 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_4.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_4.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:66e91c92c868167c850078cd91714db47e10a96e23cc30191994486bd79c353f +oid sha256:3d6719326f4dacae278d1723090ce1182193b793f250963af8be4b2c298e8841 size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_5.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_5.ppm index 5e12edc46d..9cd58caae5 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_5.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_5.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d950d173f5101820c5e18205401ca08ce5feeff2302ac2920b292750d86a8fa4 +oid sha256:9e492bb394fb18fb117f8a5b61cd2789922f9d6e88fc83189b5b6d59ffb1c3ef size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_6.ppm b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_6.ppm index d442d90287..136eecbaaf 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_6.ppm +++ b/AutomatedTesting/Gem/PythonTests/Atom/golden_images/SpotLight_6.ppm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:72eddb7126eae0c839b933886e0fb69d78229f72d49ef13199de28df2b7879db +oid sha256:caca85f7728f660daae36afc81d681ba2de2377c516eb3c637599de5c94012aa size 6220817 diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_EntityReferenceAdded.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_EntityReferenceAdded.py new file mode 100644 index 0000000000..dddcca64fa --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_EntityReferenceAdded.py @@ -0,0 +1,158 @@ +""" +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 +""" + + +class Tests: + creation_undo = ( + "UNDO Entity creation success", + "UNDO Entity creation failed") + creation_redo = ( + "REDO Entity creation success", + "REDO Entity creation failed") + entity_reference_creation = ( + "Entity Reference Entity successfully created", + "Entity Reference Entity failed to be created") + entity_reference_component = ( + "Entity has an Entity Reference component", + "Entity failed to find Entity Reference component") + enter_game_mode = ( + "Entered game mode", + "Failed to enter game mode") + exit_game_mode = ( + "Exited game mode", + "Couldn't exit game mode") + is_visible = ( + "Entity is visible", + "Entity was not visible") + is_hidden = ( + "Entity is hidden", + "Entity was not hidden") + entity_deleted = ( + "Entity deleted", + "Entity was not deleted") + deletion_undo = ( + "UNDO deletion success", + "UNDO deletion failed") + deletion_redo = ( + "REDO deletion success", + "REDO deletion failed") + + +def AtomEditorComponents_EntityReference_AddedToEntity(): + """ + Summary: + Tests the Entity Reference component can be added to an entity and has the expected functionality. + + Test setup: + - Wait for Editor idle loop. + - Open the "Base" level. + + Expected Behavior: + The component can be added, used in game mode, hidden/shown, deleted, and has accurate required components. + Creation and deletion undo/redo should also work. + + Test Steps: + 1) Create an Entity Reference entity with no components. + 2) Add Entity Reference component to Entity Reference entity. + 3) UNDO the entity creation and component addition. + 4) REDO the entity creation and component addition. + 5) Enter/Exit game mode. + 6) Test IsHidden. + 7) Test IsVisible. + 8) Delete Entity Reference entity. + 9) UNDO deletion. + 10) REDO deletion. + 11) Look for errors. + + :return: None + """ + + import azlmbr.legacy.general as general + + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import Report, Tracer, TestHelper + from Atom.atom_utils.atom_constants import AtomComponentProperties + + with Tracer() as error_tracer: + # Test setup begins. + # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level. + TestHelper.init_idle() + TestHelper.open_level("", "Base") + + # Test steps begin. + # 1. Create an Entity Reference entity with no components. + entity_reference_entity = EditorEntity.create_editor_entity(AtomComponentProperties.entity_reference()) + Report.critical_result(Tests.entity_reference_creation, entity_reference_entity.exists()) + + # 2. Add Entity Reference component to Entity Reference entity. + entity_reference_component = entity_reference_entity.add_component( + AtomComponentProperties.entity_reference()) + Report.critical_result( + Tests.entity_reference_component, + entity_reference_entity.has_component(AtomComponentProperties.entity_reference())) + + # 3. UNDO the entity creation and component addition. + # -> UNDO component addition. + general.undo() + # -> UNDO naming entity. + general.undo() + # -> UNDO selecting entity. + general.undo() + # -> UNDO entity creation. + general.undo() + general.idle_wait_frames(1) + Report.result(Tests.creation_undo, not entity_reference_entity.exists()) + + # 4. REDO the entity creation and component addition. + # -> REDO entity creation. + general.redo() + # -> REDO selecting entity. + general.redo() + # -> REDO naming entity. + general.redo() + # -> REDO component addition. + general.redo() + general.idle_wait_frames(1) + Report.result(Tests.creation_redo, entity_reference_entity.exists()) + + # 5. Enter/Exit game mode. + TestHelper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + TestHelper.exit_game_mode(Tests.exit_game_mode) + + # 6. Test IsHidden. + entity_reference_entity.set_visibility_state(False) + Report.result(Tests.is_hidden, entity_reference_entity.is_hidden() is True) + + # 7. Test IsVisible. + entity_reference_entity.set_visibility_state(True) + general.idle_wait_frames(1) + Report.result(Tests.is_visible, entity_reference_entity.is_visible() is True) + + # 8. Delete Entity Reference entity. + entity_reference_entity.delete() + Report.result(Tests.entity_deleted, not entity_reference_entity.exists()) + + # 9. UNDO deletion. + general.undo() + Report.result(Tests.deletion_undo, entity_reference_entity.exists()) + + # 10. REDO deletion. + general.redo() + Report.result(Tests.deletion_redo, not entity_reference_entity.exists()) + + # 11. Look for errors and asserts. + TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(AtomEditorComponents_EntityReference_AddedToEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_GlobalSkylightIBLAdded.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_GlobalSkylightIBLAdded.py index db8f2daaee..7f0c38e289 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_GlobalSkylightIBLAdded.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_GlobalSkylightIBLAdded.py @@ -151,7 +151,7 @@ def AtomEditorComponents_GlobalSkylightIBL_AddedToEntity(): Report.result(Tests.is_visible, global_skylight_entity.is_visible() is True) # 8. Set the Diffuse Image asset on the Global Skylight (IBL) entity. - diffuse_image_path = os.path.join("LightingPresets", "greenwich_park_02_4k_iblskyboxcm.exr.streamingimage") + diffuse_image_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") diffuse_image_asset = Asset.find_asset_by_path(diffuse_image_path, False) global_skylight_component.set_component_property_value( AtomComponentProperties.global_skylight('Diffuse Image'), diffuse_image_asset.id) @@ -161,7 +161,7 @@ def AtomEditorComponents_GlobalSkylightIBL_AddedToEntity(): AtomComponentProperties.global_skylight('Diffuse Image'))) # 9. Set the Specular Image asset on the Global Light (IBL) entity. - specular_image_path = os.path.join("LightingPresets", "greenwich_park_02_4k_iblskyboxcm.exr.streamingimage") + specular_image_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") specular_image_asset = Asset.find_asset_by_path(specular_image_path, False) global_skylight_component.set_component_property_value( AtomComponentProperties.global_skylight('Specular Image'), specular_image_asset.id) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_HDRiSkyboxAdded.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_HDRiSkyboxAdded.py new file mode 100644 index 0000000000..0f96bc5424 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_HDRiSkyboxAdded.py @@ -0,0 +1,177 @@ +""" +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 +""" + + +class Tests: + creation_undo = ( + "UNDO Entity creation success", + "UNDO Entity creation failed") + creation_redo = ( + "REDO Entity creation success", + "REDO Entity creation failed") + hdri_skybox_entity_creation = ( + "HDRi Skybox successfully created", + "HDRi Skybox failed to be created") + hdri_skybox_component = ( + "Entity has an HDRi Skybox component", + "Entity failed to find HDRi Skybox component") + cubemap_property_set = ( + "Cubemap property set on HDRi Skybox component", + "Couldn't set Cubemap property on HDRi Skybox component") + enter_game_mode = ( + "Entered game mode", + "Failed to enter game mode") + exit_game_mode = ( + "Exited game mode", + "Couldn't exit game mode") + is_visible = ( + "Entity is visible", + "Entity was not visible") + is_hidden = ( + "Entity is hidden", + "Entity was not hidden") + entity_deleted = ( + "Entity deleted", + "Entity was not deleted") + deletion_undo = ( + "UNDO deletion success", + "UNDO deletion failed") + deletion_redo = ( + "REDO deletion success", + "REDO deletion failed") + + +def AtomEditorComponents_HDRiSkybox_AddedToEntity(): + """ + Summary: + Tests the HDRi Skybox component can be added to an entity and has the expected functionality. + + Test setup: + - Wait for Editor idle loop. + - Open the "Base" level. + + Expected Behavior: + The component can be added, used in game mode, hidden/shown, deleted, and has accurate required components. + Creation and deletion undo/redo should also work. + + Test Steps: + 1) Create an HDRi Skybox with no components. + 2) Add an HDRi Skybox component to HDRi Skybox. + 3) UNDO the entity creation and component addition. + 4) REDO the entity creation and component addition. + 5) Enter/Exit game mode. + 6) Test IsHidden. + 7) Test IsVisible. + 8) Delete HDRi Skybox. + 9) UNDO deletion. + 10) REDO deletion. + 11) Look for errors. + + :return: None + """ + + import os + + import azlmbr.legacy.general as general + + from editor_python_test_tools.asset_utils import Asset + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import Report, Tracer, TestHelper + from Atom.atom_utils.atom_constants import AtomComponentProperties + + with Tracer() as error_tracer: + # Test setup begins. + # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level. + TestHelper.init_idle() + TestHelper.open_level("", "Base") + + # Test steps begin. + # 1. Create an HDRi Skybox with no components. + hdri_skybox_entity = EditorEntity.create_editor_entity( + AtomComponentProperties.hdri_skybox()) + Report.critical_result(Tests.hdri_skybox_entity_creation, + hdri_skybox_entity.exists()) + + # 2. Add an HDRi Skybox component to HDRi Skybox. + hdri_skybox_component = hdri_skybox_entity.add_component( + AtomComponentProperties.hdri_skybox()) + Report.critical_result( + Tests.hdri_skybox_component, + hdri_skybox_entity.has_component(AtomComponentProperties.hdri_skybox())) + + # 3. UNDO the entity creation and component addition. + # -> UNDO component addition. + general.undo() + # -> UNDO naming entity. + general.undo() + # -> UNDO selecting entity. + general.undo() + # -> UNDO entity creation. + general.undo() + general.idle_wait_frames(1) + Report.result(Tests.creation_undo, not hdri_skybox_entity.exists()) + + # 4. REDO the entity creation and component addition. + # -> REDO entity creation. + general.redo() + # -> REDO selecting entity. + general.redo() + # -> REDO naming entity. + general.redo() + # -> REDO component addition. + general.redo() + general.idle_wait_frames(1) + Report.result(Tests.creation_redo, hdri_skybox_entity.exists()) + + + # 5. Set Cubemap Texture on HDRi Skybox component. + skybox_cubemap_asset_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") + skybox_cubemap_material_asset = Asset.find_asset_by_path(skybox_cubemap_asset_path, False) + hdri_skybox_component.set_component_property_value( + AtomComponentProperties.hdri_skybox('Cubemap Texture'), skybox_cubemap_material_asset.id) + get_cubemap_property = hdri_skybox_component.get_component_property_value( + AtomComponentProperties.hdri_skybox('Cubemap Texture')) + Report.result(Tests.cubemap_property_set, get_cubemap_property == skybox_cubemap_material_asset.id) + + + # 6. Enter/Exit game mode. + TestHelper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + TestHelper.exit_game_mode(Tests.exit_game_mode) + + # 7. Test IsHidden. + hdri_skybox_entity.set_visibility_state(False) + Report.result(Tests.is_hidden, hdri_skybox_entity.is_hidden() is True) + + # 8. Test IsVisible. + hdri_skybox_entity.set_visibility_state(True) + general.idle_wait_frames(1) + Report.result(Tests.is_visible, hdri_skybox_entity.is_visible() is True) + + # 9. Delete hdri_skybox entity. + hdri_skybox_entity.delete() + Report.result(Tests.entity_deleted, not hdri_skybox_entity.exists()) + + # 10. UNDO deletion. + general.undo() + Report.result(Tests.deletion_undo, hdri_skybox_entity.exists()) + + # 11. REDO deletion. + general.redo() + Report.result(Tests.deletion_redo, not hdri_skybox_entity.exists()) + + # 12. Look for errors or asserts. + TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(AtomEditorComponents_HDRiSkybox_AddedToEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_LookModificationAdded.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_LookModificationAdded.py new file mode 100644 index 0000000000..afb8033426 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_LookModificationAdded.py @@ -0,0 +1,211 @@ +""" +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 +""" + + +class Tests: + creation_undo = ( + "UNDO Entity creation success", + "UNDO Entity creation failed") + creation_redo = ( + "REDO Entity creation success", + "REDO Entity creation failed") + look_modification_creation = ( + "Look Modification Entity successfully created", + "Look Modification Entity failed to be created") + look_modification_component = ( + "Entity has a Look Modification component", + "Entity failed to find Look Modification component") + look_modification_disabled = ( + "Look Modification component disabled", + "Look Modification component was not disabled") + postfx_layer_component = ( + "Entity has a PostFX Layer component", + "Entity did not have an PostFX Layer component") + look_modification_enabled = ( + "Look Modification component enabled", + "Look Modification component was not enabled") + enable_look_modification_parameter_enabled = ( + "Enable look modification parameter enabled", + "Enable look modification parameter was not enabled") + color_grading_lut_set = ( + "Entity has the Color Grading LUT set", + "Entity did not the Color Grading LUT set") + enter_game_mode = ( + "Entered game mode", + "Failed to enter game mode") + exit_game_mode = ( + "Exited game mode", + "Couldn't exit game mode") + is_visible = ( + "Entity is visible", + "Entity was not visible") + is_hidden = ( + "Entity is hidden", + "Entity was not hidden") + entity_deleted = ( + "Entity deleted", + "Entity was not deleted") + deletion_undo = ( + "UNDO deletion success", + "UNDO deletion failed") + deletion_redo = ( + "REDO deletion success", + "REDO deletion failed") + + +def AtomEditorComponents_LookModification_AddedToEntity(): + """ + Summary: + Tests the Look Modification component can be added to an entity and has the expected functionality. + + Test setup: + - Wait for Editor idle loop. + - Open the "Base" level. + + Expected Behavior: + The component can be added, used in game mode, hidden/shown, deleted, and has accurate required components. + Creation and deletion undo/redo should also work. + + Test Steps: + 1) Create an Look Modification entity with no components. + 2) Add Look Modification component to Look Modification entity. + 3) UNDO the entity creation and component addition. + 4) REDO the entity creation and component addition. + 5) Verify Look Modification component not enabled. + 6) Add PostFX Layer component since it is required by the Look Modification component. + 7) Verify Look Modification component is enabled. + 8) Enable the "Enable Look Modification" parameter. + 9) Add LUT asset to the Color Grading LUT parameter. + 9) Enter/Exit game mode. + 10) Test IsHidden. + 11) Test IsVisible. + 12) Delete Look Modification entity. + 13) UNDO deletion. + 14) REDO deletion. + 15) Look for errors. + + :return: None + """ + + import os + + import azlmbr.legacy.general as general + + from editor_python_test_tools.asset_utils import Asset + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import Report, Tracer, TestHelper + from Atom.atom_utils.atom_constants import AtomComponentProperties + + with Tracer() as error_tracer: + # Test setup begins. + # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level. + TestHelper.init_idle() + TestHelper.open_level("", "Base") + + # Test steps begin. + # 1. Create an Look Modification entity with no components. + look_modification_entity = EditorEntity.create_editor_entity(AtomComponentProperties.look_modification()) + Report.critical_result(Tests.look_modification_creation, look_modification_entity.exists()) + + # 2. Add Look Modification component to Look Modification entity. + look_modification_component = look_modification_entity.add_component( + AtomComponentProperties.look_modification()) + Report.critical_result( + Tests.look_modification_component, + look_modification_entity.has_component(AtomComponentProperties.look_modification())) + + # 3. UNDO the entity creation and component addition. + # -> UNDO component addition. + general.undo() + # -> UNDO naming entity. + general.undo() + # -> UNDO selecting entity. + general.undo() + # -> UNDO entity creation. + general.undo() + general.idle_wait_frames(1) + Report.result(Tests.creation_undo, not look_modification_entity.exists()) + + # 4. REDO the entity creation and component addition. + # -> REDO entity creation. + general.redo() + # -> REDO selecting entity. + general.redo() + # -> REDO naming entity. + general.redo() + # -> REDO component addition. + general.redo() + general.idle_wait_frames(1) + Report.result(Tests.creation_redo, look_modification_entity.exists()) + + # 5. Verify Look Modification component not enabled. + Report.result(Tests.look_modification_disabled, not look_modification_component.is_enabled()) + + # 6. Add PostFX Layer component since it is required by the Look Modification component. + look_modification_entity.add_component(AtomComponentProperties.postfx_layer()) + Report.result( + Tests.postfx_layer_component, + look_modification_entity.has_component(AtomComponentProperties.postfx_layer())) + + # 7. Verify Look Modification component is enabled. + Report.result(Tests.look_modification_enabled, look_modification_component.is_enabled()) + + # 8. Enable the "Enable look modification" parameter. + look_modification_component.set_component_property_value( + AtomComponentProperties.look_modification('Enable look modification'), True) + Report.result(Tests.enable_look_modification_parameter_enabled, + look_modification_component.get_component_property_value( + AtomComponentProperties.look_modification('Enable look modification')) is True) + + # 9. Set the Color Grading LUT asset on the Look Modification entity. + color_grading_lut_path = os.path.join("ColorGrading", "TestData", "Photoshop", "inv-Log2-48nits", + "test_3dl_32_lut.azasset") + color_grading_lut_asset = Asset.find_asset_by_path(color_grading_lut_path, False) + look_modification_component.set_component_property_value( + AtomComponentProperties.look_modification('Color Grading LUT'), color_grading_lut_asset.id) + Report.result( + Tests.color_grading_lut_set, + color_grading_lut_asset.id == look_modification_component.get_component_property_value( + AtomComponentProperties.look_modification('Color Grading LUT'))) + + # 10. Enter/Exit game mode. + TestHelper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + TestHelper.exit_game_mode(Tests.exit_game_mode) + + # 11. Test IsHidden. + look_modification_entity.set_visibility_state(False) + Report.result(Tests.is_hidden, look_modification_entity.is_hidden() is True) + + # 12. Test IsVisible. + look_modification_entity.set_visibility_state(True) + general.idle_wait_frames(1) + Report.result(Tests.is_visible, look_modification_entity.is_visible() is True) + + # 13. Delete Look Modification entity. + look_modification_entity.delete() + Report.result(Tests.entity_deleted, not look_modification_entity.exists()) + + # 14. UNDO deletion. + general.undo() + Report.result(Tests.deletion_undo, look_modification_entity.exists()) + + # 15. REDO deletion. + general.redo() + Report.result(Tests.deletion_redo, not look_modification_entity.exists()) + + # 16. Look for errors and asserts. + TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(AtomEditorComponents_LookModification_AddedToEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_SSAOAdded.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_SSAOAdded.py new file mode 100644 index 0000000000..15f40f8b70 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomEditorComponents_SSAOAdded.py @@ -0,0 +1,182 @@ +""" +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 +""" + + +class Tests: + creation_undo = ( + "UNDO Entity creation success", + "UNDO Entity creation failed") + creation_redo = ( + "REDO Entity creation success", + "REDO Entity creation failed") + ssao_creation = ( + "SSAO Entity successfully created", + "SSAO Entity failed to be created") + ssao_component = ( + "Entity has a SSAO component", + "Entity failed to find SSAO component") + ssao_disabled = ( + "SSAO component disabled", + "SSAO component was not disabled.") + postfx_layer_component = ( + "Entity has a PostFX Layer component", + "Entity did not have an PostFX Layer component") + ssao_enabled = ( + "SSAO component enabled", + "SSAO component was not enabled.") + enter_game_mode = ( + "Entered game mode", + "Failed to enter game mode") + exit_game_mode = ( + "Exited game mode", + "Couldn't exit game mode") + is_visible = ( + "Entity is visible", + "Entity was not visible") + is_hidden = ( + "Entity is hidden", + "Entity was not hidden") + entity_deleted = ( + "Entity deleted", + "Entity was not deleted") + deletion_undo = ( + "UNDO deletion success", + "UNDO deletion failed") + deletion_redo = ( + "REDO deletion success", + "REDO deletion failed") + + +def AtomEditorComponents_SSAO_AddedToEntity(): + """ + Summary: + Tests the SSAO component can be added to an entity and has the expected functionality. + Screen Space Ambient Occlusion (SSAO) is a PostFX shadow lighting effect. + + Test setup: + - Wait for Editor idle loop. + - Open the "Base" level. + + Expected Behavior: + The component can be added, used in game mode, hidden/shown, deleted, and has accurate required components. + Creation and deletion undo/redo should also work. + + Test Steps: + 1) Create a SSAO entity with no components. + 2) Add SSAO component to SSAO entity. + 3) UNDO the entity creation and component addition. + 4) REDO the entity creation and component addition. + 5) Verify SSAO component not enabled. + 6) Add PostFX Layer component since it is required by the SSAO component. + 7) Verify SSAO component is enabled. + 8) Enter/Exit game mode. + 9) Test IsHidden. + 10) Test IsVisible. + 11) Delete SSAO entity. + 12) UNDO deletion. + 13) REDO deletion. + 14) Look for errors. + + :return: None + """ + + import azlmbr.legacy.general as general + + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import Report, Tracer, TestHelper + from Atom.atom_utils.atom_constants import AtomComponentProperties + + with Tracer() as error_tracer: + # Test setup begins. + # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level. + TestHelper.init_idle() + TestHelper.open_level("", "Base") + + # Test steps begin. + # 1. Create a SSAO entity with no components. + ssao_entity = EditorEntity.create_editor_entity(AtomComponentProperties.ssao()) + Report.critical_result(Tests.ssao_creation, ssao_entity.exists()) + + # 2. Add SSAO component to SSAO entity. + ssao_component = ssao_entity.add_component(AtomComponentProperties.ssao()) + Report.critical_result( + Tests.ssao_component, + ssao_entity.has_component(AtomComponentProperties.ssao())) + ssao_component.get_property_tree() + # 3. UNDO the entity creation and component addition. + # -> UNDO component addition. + general.undo() + # -> UNDO naming entity. + general.undo() + # -> UNDO selecting entity. + general.undo() + # -> UNDO entity creation. + general.undo() + general.idle_wait_frames(1) + Report.result(Tests.creation_undo, not ssao_entity.exists()) + + # 4. REDO the entity creation and component addition. + # -> REDO entity creation. + general.redo() + # -> REDO selecting entity. + general.redo() + # -> REDO naming entity. + general.redo() + # -> REDO component addition. + general.redo() + general.idle_wait_frames(1) + Report.result(Tests.creation_redo, ssao_entity.exists()) + + # 5. Verify SSAO component not enabled. + Report.result(Tests.ssao_disabled, not ssao_component.is_enabled()) + + # 6. Add PostFX Layer component since it is required by the SSAO component. + ssao_entity.add_component(AtomComponentProperties.postfx_layer()) + Report.result( + Tests.postfx_layer_component, + ssao_entity.has_component(AtomComponentProperties.postfx_layer())) + + # 7. Verify SSAO component is enabled. + Report.result(Tests.ssao_enabled, ssao_component.is_enabled()) + + # 8. Enter/Exit game mode. + TestHelper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + TestHelper.exit_game_mode(Tests.exit_game_mode) + + # 9. Test IsHidden. + ssao_entity.set_visibility_state(False) + Report.result(Tests.is_hidden, ssao_entity.is_hidden() is True) + + # 10. Test IsVisible. + ssao_entity.set_visibility_state(True) + general.idle_wait_frames(1) + Report.result(Tests.is_visible, ssao_entity.is_visible() is True) + + # 11. Delete SSAO entity. + ssao_entity.delete() + Report.result(Tests.entity_deleted, not ssao_entity.exists()) + + # 12. UNDO deletion. + general.undo() + Report.result(Tests.deletion_undo, ssao_entity.exists()) + + # 13. REDO deletion. + general.redo() + Report.result(Tests.deletion_redo, not ssao_entity.exists()) + + # 14. Look for errors and asserts. + TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(AtomEditorComponents_SSAO_AddedToEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomGPU_BasicLevelSetup.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomGPU_BasicLevelSetup.py index 92c555127a..127b3e5b5f 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomGPU_BasicLevelSetup.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_AtomGPU_BasicLevelSetup.py @@ -6,30 +6,70 @@ SPDX-License-Identifier: Apache-2.0 OR MIT """ -# fmt: off -class Tests : - camera_component_added = ("Camera component was added", "Camera component wasn't added") - camera_fov_set = ("Camera component FOV property set", "Camera component FOV property wasn't set") - directional_light_component_added = ("Directional Light component added", "Directional Light component wasn't added") - enter_game_mode = ("Entered game mode", "Failed to enter game mode") - exit_game_mode = ("Exited game mode", "Couldn't exit game mode") - global_skylight_component_added = ("Global Skylight (IBL) component added", "Global Skylight (IBL) component wasn't added") - global_skylight_diffuse_image_set = ("Global Skylight Diffuse Image property set", "Global Skylight Diffuse Image property wasn't set") - global_skylight_specular_image_set = ("Global Skylight Specular Image property set", "Global Skylight Specular Image property wasn't set") - ground_plane_material_asset_set = ("Ground Plane Material Asset was set", "Ground Plane Material Asset wasn't set") - ground_plane_material_component_added = ("Ground Plane Material component added", "Ground Plane Material component wasn't added") - ground_plane_mesh_asset_set = ("Ground Plane Mesh Asset property was set", "Ground Plane Mesh Asset property wasn't set") - hdri_skybox_component_added = ("HDRi Skybox component added", "HDRi Skybox component wasn't added") - hdri_skybox_cubemap_texture_set = ("HDRi Skybox Cubemap Texture property set", "HDRi Skybox Cubemap Texture property wasn't set") - mesh_component_added = ("Mesh component added", "Mesh component wasn't added") - no_assert_occurred = ("No asserts detected", "Asserts were detected") - no_error_occurred = ("No errors detected", "Errors were detected") - secondary_grid_spacing = ("Secondary Grid Spacing set", "Secondary Grid Spacing not set") - sphere_material_component_added = ("Sphere Material component added", "Sphere Material component wasn't added") - sphere_material_set = ("Sphere Material Asset was set", "Sphere Material Asset wasn't set") - sphere_mesh_asset_set = ("Sphere Mesh Asset was set", "Sphere Mesh Asset wasn't set") - viewport_set = ("Viewport set to correct size", "Viewport not set to correct size") -# fmt: on +class Tests: + camera_component_added = ( + "Camera component was added", + "Camera component wasn't added") + camera_fov_set = ( + "Camera component FOV property set", + "Camera component FOV property wasn't set") + directional_light_component_added = ( + "Directional Light component added", + "Directional Light component wasn't added") + enter_game_mode = ( + "Entered game mode", + "Failed to enter game mode") + exit_game_mode = ( + "Exited game mode", + "Couldn't exit game mode") + global_skylight_component_added = ( + "Global Skylight (IBL) component added", + "Global Skylight (IBL) component wasn't added") + global_skylight_diffuse_image_set = ( + "Global Skylight Diffuse Image property set", + "Global Skylight Diffuse Image property wasn't set") + global_skylight_specular_image_set = ( + "Global Skylight Specular Image property set", + "Global Skylight Specular Image property wasn't set") + ground_plane_material_asset_set = ( + "Ground Plane Material Asset was set", + "Ground Plane Material Asset wasn't set") + ground_plane_material_component_added = ( + "Ground Plane Material component added", + "Ground Plane Material component wasn't added") + ground_plane_mesh_asset_set = ( + "Ground Plane Mesh Asset property was set", + "Ground Plane Mesh Asset property wasn't set") + hdri_skybox_component_added = ( + "HDRi Skybox component added", + "HDRi Skybox component wasn't added") + hdri_skybox_cubemap_texture_set = ( + "HDRi Skybox Cubemap Texture property set", + "HDRi Skybox Cubemap Texture property wasn't set") + mesh_component_added = ( + "Mesh component added", + "Mesh component wasn't added") + no_assert_occurred = ( + "No asserts detected", + "Asserts were detected") + no_error_occurred = ( + "No errors detected", + "Errors were detected") + secondary_grid_spacing = ( + "Secondary Grid Spacing set", + "Secondary Grid Spacing not set") + sphere_material_component_added = ( + "Sphere Material component added", + "Sphere Material component wasn't added") + sphere_material_set = ( + "Sphere Material Asset was set", + "Sphere Material Asset wasn't set") + sphere_mesh_asset_set = ( + "Sphere Mesh Asset was set", + "Sphere Mesh Asset wasn't set") + viewport_set = ( + "Viewport set to correct size", + "Viewport not set to correct size") def AtomGPU_BasicLevelSetup_SetsUpLevel(): @@ -77,19 +117,17 @@ def AtomGPU_BasicLevelSetup_SetsUpLevel(): import os from math import isclose - import azlmbr.asset as asset - import azlmbr.bus as bus import azlmbr.legacy.general as general import azlmbr.math as math import azlmbr.paths + from editor_python_test_tools.asset_utils import Asset from editor_python_test_tools.editor_entity_utils import EditorEntity - from editor_python_test_tools.utils import Report, Tracer, TestHelper as helper + from editor_python_test_tools.utils import Report, Tracer, TestHelper + from Atom.atom_utils.atom_constants import AtomComponentProperties from Atom.atom_utils.screenshot_utils import ScreenshotHelper - MATERIAL_COMPONENT_NAME = "Material" - MESH_COMPONENT_NAME = "Mesh" SCREENSHOT_NAME = "AtomBasicLevelSetup" SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720 @@ -98,24 +136,24 @@ def AtomGPU_BasicLevelSetup_SetsUpLevel(): def initial_viewport_setup(screen_width, screen_height): general.set_viewport_size(screen_width, screen_height) general.update_viewport() - result = isclose( - a=general.get_viewport_size().x, b=SCREEN_WIDTH, rel_tol=0.1) and isclose( - a=general.get_viewport_size().y, b=SCREEN_HEIGHT, rel_tol=0.1) - - return result + TestHelper.wait_for_condition( + function=lambda: isclose(a=general.get_viewport_size().x, b=SCREEN_WIDTH, rel_tol=0.1) + and isclose(a=general.get_viewport_size().y, b=SCREEN_HEIGHT, rel_tol=0.1), + timeout_in_seconds=4.0 + ) with Tracer() as error_tracer: # Test setup begins. # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level. - helper.init_idle() - helper.open_level("", "Base") + TestHelper.init_idle() + TestHelper.open_level("", "Base") # Test steps begin. # 1. Close error windows and display helpers then update the viewport size. - helper.close_error_windows() - helper.close_display_helpers() + TestHelper.close_error_windows() + TestHelper.close_display_helpers() + initial_viewport_setup(SCREEN_WIDTH, SCREEN_HEIGHT) general.update_viewport() - Report.critical_result(Tests.viewport_set, initial_viewport_setup(SCREEN_WIDTH, SCREEN_HEIGHT)) # 2. Create Default Level Entity. default_level_entity_name = "Default Level" @@ -123,168 +161,167 @@ def AtomGPU_BasicLevelSetup_SetsUpLevel(): math.Vector3(0.0, 0.0, 0.0), default_level_entity_name) # 3. Create Grid Entity as a child entity of the Default Level Entity. - grid_name = "Grid" - grid_entity = EditorEntity.create_editor_entity(grid_name, default_level_entity.id) + grid_entity = EditorEntity.create_editor_entity(AtomComponentProperties.grid(), default_level_entity.id) # 4. Add Grid component to Grid Entity and set Secondary Grid Spacing. - grid_component = grid_entity.add_component(grid_name) - secondary_grid_spacing_property = "Controller|Configuration|Secondary Grid Spacing" + grid_component = grid_entity.add_component(AtomComponentProperties.grid()) secondary_grid_spacing_value = 1.0 - grid_component.set_component_property_value(secondary_grid_spacing_property, secondary_grid_spacing_value) + grid_component.set_component_property_value( + AtomComponentProperties.grid('Secondary Grid Spacing'), secondary_grid_spacing_value) secondary_grid_spacing_set = grid_component.get_component_property_value( - secondary_grid_spacing_property) == secondary_grid_spacing_value + AtomComponentProperties.grid('Secondary Grid Spacing')) == secondary_grid_spacing_value Report.result(Tests.secondary_grid_spacing, secondary_grid_spacing_set) # 5. Create Global Skylight (IBL) Entity as a child entity of the Default Level Entity. - global_skylight_name = "Global Skylight (IBL)" - global_skylight_entity = EditorEntity.create_editor_entity(global_skylight_name, default_level_entity.id) + global_skylight_entity = EditorEntity.create_editor_entity( + AtomComponentProperties.global_skylight(), default_level_entity.id) # 6. Add HDRi Skybox component to the Global Skylight (IBL) Entity. - hdri_skybox_name = "HDRi Skybox" - hdri_skybox_component = global_skylight_entity.add_component(hdri_skybox_name) - Report.result(Tests.hdri_skybox_component_added, global_skylight_entity.has_component(hdri_skybox_name)) + hdri_skybox_component = global_skylight_entity.add_component(AtomComponentProperties.hdri_skybox()) + Report.result(Tests.hdri_skybox_component_added, global_skylight_entity.has_component( + AtomComponentProperties.hdri_skybox())) # 7. Add Global Skylight (IBL) component to the Global Skylight (IBL) Entity. - global_skylight_component = global_skylight_entity.add_component(global_skylight_name) - Report.result(Tests.global_skylight_component_added, global_skylight_entity.has_component(global_skylight_name)) + global_skylight_component = global_skylight_entity.add_component(AtomComponentProperties.global_skylight()) + Report.result(Tests.global_skylight_component_added, global_skylight_entity.has_component( + AtomComponentProperties.global_skylight())) # 8. Set the Cubemap Texture property of the HDRi Skybox component. - global_skylight_image_asset_path = os.path.join( - "LightingPresets", "greenwich_park_02_4k_iblskyboxcm_iblspecular.exr.streamingimage") - global_skylight_image_asset = asset.AssetCatalogRequestBus( - bus.Broadcast, "GetAssetIdByPath", global_skylight_image_asset_path, math.Uuid(), False) - hdri_skybox_cubemap_texture_property = "Controller|Configuration|Cubemap Texture" + global_skylight_image_asset_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") + global_skylight_image_asset = Asset.find_asset_by_path(global_skylight_image_asset_path, False) hdri_skybox_component.set_component_property_value( - hdri_skybox_cubemap_texture_property, global_skylight_image_asset) + AtomComponentProperties.hdri_skybox('Cubemap Texture'), global_skylight_image_asset.id) Report.result( Tests.hdri_skybox_cubemap_texture_set, hdri_skybox_component.get_component_property_value( - hdri_skybox_cubemap_texture_property) == global_skylight_image_asset) + AtomComponentProperties.hdri_skybox('Cubemap Texture')) == global_skylight_image_asset.id) # 9. Set the Diffuse Image property of the Global Skylight (IBL) component. # Re-use the same image that was used in the previous test step. - global_skylight_diffuse_image_property = "Controller|Configuration|Diffuse Image" + global_skylight_diffuse_image_asset_path = os.path.join( + "LightingPresets", "default_iblskyboxcm_ibldiffuse.exr.streamingimage") + global_skylight_diffuse_image_asset = Asset.find_asset_by_path(global_skylight_diffuse_image_asset_path, False) global_skylight_component.set_component_property_value( - global_skylight_diffuse_image_property, global_skylight_image_asset) + AtomComponentProperties.global_skylight('Diffuse Image'), global_skylight_diffuse_image_asset.id) Report.result( Tests.global_skylight_diffuse_image_set, global_skylight_component.get_component_property_value( - global_skylight_diffuse_image_property) == global_skylight_image_asset) + AtomComponentProperties.global_skylight('Diffuse Image')) == global_skylight_diffuse_image_asset.id) # 10. Set the Specular Image property of the Global Skylight (IBL) component. # Re-use the same image that was used in the previous test step. - global_skylight_specular_image_property = "Controller|Configuration|Specular Image" + global_skylight_specular_image_asset_path = os.path.join( + "LightingPresets", "default_iblskyboxcm_iblspecular.exr.streamingimage") + global_skylight_specular_image_asset = Asset.find_asset_by_path( + global_skylight_specular_image_asset_path, False) global_skylight_component.set_component_property_value( - global_skylight_specular_image_property, global_skylight_image_asset) + AtomComponentProperties.global_skylight('Specular Image'), global_skylight_specular_image_asset.id) global_skylight_specular_image_set = global_skylight_component.get_component_property_value( - global_skylight_specular_image_property) + AtomComponentProperties.global_skylight('Specular Image')) Report.result( - Tests.global_skylight_specular_image_set, global_skylight_specular_image_set == global_skylight_image_asset) + Tests.global_skylight_specular_image_set, + global_skylight_specular_image_set == global_skylight_specular_image_asset.id) # 11. Create a Ground Plane Entity with a Material component that is a child entity of the Default Level Entity. ground_plane_name = "Ground Plane" ground_plane_entity = EditorEntity.create_editor_entity(ground_plane_name, default_level_entity.id) - ground_plane_material_component = ground_plane_entity.add_component(MATERIAL_COMPONENT_NAME) + ground_plane_material_component = ground_plane_entity.add_component(AtomComponentProperties.material()) Report.result( - Tests.ground_plane_material_component_added, ground_plane_entity.has_component(MATERIAL_COMPONENT_NAME)) + Tests.ground_plane_material_component_added, + ground_plane_entity.has_component(AtomComponentProperties.material())) # 12. Set the Material Asset property of the Material component for the Ground Plane Entity. ground_plane_entity.set_local_uniform_scale(32.0) ground_plane_material_asset_path = os.path.join("Materials", "Presets", "PBR", "metal_chrome.azmaterial") - ground_plane_material_asset = asset.AssetCatalogRequestBus( - bus.Broadcast, "GetAssetIdByPath", ground_plane_material_asset_path, math.Uuid(), False) - ground_plane_material_asset_property = "Default Material|Material Asset" + ground_plane_material_asset = Asset.find_asset_by_path(ground_plane_material_asset_path, False) ground_plane_material_component.set_component_property_value( - ground_plane_material_asset_property, ground_plane_material_asset) + AtomComponentProperties.material('Material Asset'), ground_plane_material_asset.id) Report.result( Tests.ground_plane_material_asset_set, ground_plane_material_component.get_component_property_value( - ground_plane_material_asset_property) == ground_plane_material_asset) + AtomComponentProperties.material('Material Asset')) == ground_plane_material_asset.id) # 13. Add the Mesh component to the Ground Plane Entity and set the Mesh component Mesh Asset property. - ground_plane_mesh_component = ground_plane_entity.add_component(MESH_COMPONENT_NAME) - Report.result(Tests.mesh_component_added, ground_plane_entity.has_component(MESH_COMPONENT_NAME)) - ground_plane_mesh_asset_path = os.path.join("Objects", "plane.azmodel") - ground_plane_mesh_asset = asset.AssetCatalogRequestBus( - bus.Broadcast, "GetAssetIdByPath", ground_plane_mesh_asset_path, math.Uuid(), False) - ground_plane_mesh_asset_property = "Controller|Configuration|Mesh Asset" + ground_plane_mesh_component = ground_plane_entity.add_component(AtomComponentProperties.mesh()) + Report.result(Tests.mesh_component_added, ground_plane_entity.has_component(AtomComponentProperties.mesh())) + ground_plane_mesh_asset_path = os.path.join("TestData", "Objects", "plane.azmodel") + ground_plane_mesh_asset = Asset.find_asset_by_path(ground_plane_mesh_asset_path, False) ground_plane_mesh_component.set_component_property_value( - ground_plane_mesh_asset_property, ground_plane_mesh_asset) + AtomComponentProperties.mesh('Mesh Asset'), ground_plane_mesh_asset.id) Report.result( Tests.ground_plane_mesh_asset_set, ground_plane_mesh_component.get_component_property_value( - ground_plane_mesh_asset_property) == ground_plane_mesh_asset) + AtomComponentProperties.mesh('Mesh Asset')) == ground_plane_mesh_asset.id) # 14. Create a Directional Light Entity as a child entity of the Default Level Entity. - directional_light_name = "Directional Light" directional_light_entity = EditorEntity.create_editor_entity_at( - math.Vector3(0.0, 0.0, 10.0), directional_light_name, default_level_entity.id) + math.Vector3(0.0, 0.0, 10.0), AtomComponentProperties.directional_light(), default_level_entity.id) # 15. Add Directional Light component to Directional Light Entity and set entity rotation. - directional_light_entity.add_component(directional_light_name) + directional_light_entity.add_component(AtomComponentProperties.directional_light()) directional_light_entity_rotation = math.Vector3(DEGREE_RADIAN_FACTOR * -90.0, 0.0, 0.0) directional_light_entity.set_local_rotation(directional_light_entity_rotation) Report.result( - Tests.directional_light_component_added, directional_light_entity.has_component(directional_light_name)) + Tests.directional_light_component_added, directional_light_entity.has_component( + AtomComponentProperties.directional_light())) # 16. Create a Sphere Entity as a child entity of the Default Level Entity then add a Material component. sphere_entity = EditorEntity.create_editor_entity_at( math.Vector3(0.0, 0.0, 1.0), "Sphere", default_level_entity.id) - sphere_material_component = sphere_entity.add_component(MATERIAL_COMPONENT_NAME) - Report.result(Tests.sphere_material_component_added, sphere_entity.has_component(MATERIAL_COMPONENT_NAME)) + sphere_material_component = sphere_entity.add_component(AtomComponentProperties.material()) + Report.result(Tests.sphere_material_component_added, sphere_entity.has_component( + AtomComponentProperties.material())) # 17. Set the Material Asset property of the Material component for the Sphere Entity. sphere_material_asset_path = os.path.join("Materials", "Presets", "PBR", "metal_brass_polished.azmaterial") - sphere_material_asset = asset.AssetCatalogRequestBus( - bus.Broadcast, "GetAssetIdByPath", sphere_material_asset_path, math.Uuid(), False) - sphere_material_asset_property = "Default Material|Material Asset" - sphere_material_component.set_component_property_value(sphere_material_asset_property, sphere_material_asset) + sphere_material_asset = Asset.find_asset_by_path(sphere_material_asset_path, False) + sphere_material_component.set_component_property_value( + AtomComponentProperties.material('Material Asset'), sphere_material_asset.id) Report.result(Tests.sphere_material_set, sphere_material_component.get_component_property_value( - sphere_material_asset_property) == sphere_material_asset) + AtomComponentProperties.material('Material Asset')) == sphere_material_asset.id) # 18. Add Mesh component to Sphere Entity and set the Mesh Asset property for the Mesh component. - sphere_mesh_component = sphere_entity.add_component(MESH_COMPONENT_NAME) + sphere_mesh_component = sphere_entity.add_component(AtomComponentProperties.mesh()) sphere_mesh_asset_path = os.path.join("Models", "sphere.azmodel") - sphere_mesh_asset = asset.AssetCatalogRequestBus( - bus.Broadcast, "GetAssetIdByPath", sphere_mesh_asset_path, math.Uuid(), False) - sphere_mesh_asset_property = "Controller|Configuration|Mesh Asset" - sphere_mesh_component.set_component_property_value(sphere_mesh_asset_property, sphere_mesh_asset) + sphere_mesh_asset = Asset.find_asset_by_path(sphere_mesh_asset_path, False) + sphere_mesh_component.set_component_property_value( + AtomComponentProperties.mesh('Mesh Asset'), sphere_mesh_asset.id) Report.result(Tests.sphere_mesh_asset_set, sphere_mesh_component.get_component_property_value( - sphere_mesh_asset_property) == sphere_mesh_asset) + AtomComponentProperties.mesh('Mesh Asset')) == sphere_mesh_asset.id) # 19. Create a Camera Entity as a child entity of the Default Level Entity then add a Camera component. - camera_name = "Camera" camera_entity = EditorEntity.create_editor_entity_at( - math.Vector3(5.5, -12.0, 9.0), camera_name, default_level_entity.id) - camera_component = camera_entity.add_component(camera_name) - Report.result(Tests.camera_component_added, camera_entity.has_component(camera_name)) + math.Vector3(5.5, -12.0, 9.0), AtomComponentProperties.camera(), default_level_entity.id) + camera_component = camera_entity.add_component(AtomComponentProperties.camera()) + Report.result(Tests.camera_component_added, camera_entity.has_component(AtomComponentProperties.camera())) # 20. Set the Camera Entity rotation value and set the Camera component Field of View value. camera_entity_rotation = math.Vector3( DEGREE_RADIAN_FACTOR * -27.0, DEGREE_RADIAN_FACTOR * -12.0, DEGREE_RADIAN_FACTOR * 25.0) camera_entity.set_local_rotation(camera_entity_rotation) - camera_fov_property = "Controller|Configuration|Field of view" camera_fov_value = 60.0 - camera_component.set_component_property_value(camera_fov_property, camera_fov_value) + camera_component.set_component_property_value(AtomComponentProperties.camera('Field of view'), camera_fov_value) azlmbr.camera.EditorCameraViewRequestBus(azlmbr.bus.Event, "ToggleCameraAsActiveView", camera_entity.id) Report.result(Tests.camera_fov_set, camera_component.get_component_property_value( - camera_fov_property) == camera_fov_value) + AtomComponentProperties.camera('Field of view')) == camera_fov_value) # 21. Enter game mode. - helper.enter_game_mode(Tests.enter_game_mode) - helper.wait_for_condition(function=lambda: general.is_in_game_mode(), timeout_in_seconds=4.0) + TestHelper.enter_game_mode(Tests.enter_game_mode) + TestHelper.wait_for_condition(function=lambda: general.is_in_game_mode(), timeout_in_seconds=4.0) # 22. Take screenshot. ScreenshotHelper(general.idle_wait_frames).capture_screenshot_blocking(f"{SCREENSHOT_NAME}.ppm") # 23. Exit game mode. - helper.exit_game_mode(Tests.exit_game_mode) - helper.wait_for_condition(function=lambda: not general.is_in_game_mode(), timeout_in_seconds=4.0) + TestHelper.exit_game_mode(Tests.exit_game_mode) + TestHelper.wait_for_condition(function=lambda: not general.is_in_game_mode(), timeout_in_seconds=4.0) # 24. Look for errors. - helper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) - Report.result(Tests.no_assert_occurred, not error_tracer.has_asserts) - Report.result(Tests.no_error_occurred, not error_tracer.has_errors) + TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") if __name__ == "__main__": diff --git a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_GPUTest_BasicLevelSetup.py b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_GPUTest_BasicLevelSetup.py index 645447e6de..1641b529ae 100644 --- a/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_GPUTest_BasicLevelSetup.py +++ b/AutomatedTesting/Gem/PythonTests/Atom/tests/hydra_GPUTest_BasicLevelSetup.py @@ -106,8 +106,7 @@ def run(): components=["HDRi Skybox", "Global Skylight (IBL)"], parent_id=default_level.id ) - global_skylight_image_asset_path = os.path.join( - "LightingPresets", "greenwich_park_02_4k_iblskyboxcm_iblspecular.exr.streamingimage") + global_skylight_image_asset_path = os.path.join("LightingPresets", "default_iblskyboxcm.exr.streamingimage") global_skylight_image_asset = asset.AssetCatalogRequestBus( bus.Broadcast, "GetAssetIdByPath", global_skylight_image_asset_path, math.Uuid(), False) global_skylight.get_set_test(0, "Controller|Configuration|Cubemap Texture", global_skylight_image_asset) diff --git a/AutomatedTesting/Gem/PythonTests/Blast/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Blast/TestSuite_Main.py index dbc4a1a456..a047d09d29 100644 --- a/AutomatedTesting/Gem/PythonTests/Blast/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/Blast/TestSuite_Main.py @@ -23,28 +23,28 @@ from base import TestAutomationBase class TestAutomation(TestAutomationBase): def test_ActorSplitsAfterCollision(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterCollision as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterRadialDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterRadialDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterCapsuleDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterCapsuleDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterImpactSpreadDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterImpactSpreadDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterShearDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterShearDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterTriangleDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterTriangleDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ActorSplitsAfterStressDamage(self, request, workspace, editor, launcher_platform): from .tests import Blast_ActorSplitsAfterStressDamage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/CMakeLists.txt b/AutomatedTesting/Gem/PythonTests/CMakeLists.txt index bec49185bd..800f347359 100644 --- a/AutomatedTesting/Gem/PythonTests/CMakeLists.txt +++ b/AutomatedTesting/Gem/PythonTests/CMakeLists.txt @@ -56,6 +56,9 @@ add_subdirectory(streaming) ## Smoke ## add_subdirectory(smoke) +## Terrain ## +add_subdirectory(Terrain) + ## AWS ## add_subdirectory(AWS) diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonBindings/ComponentUpdateListProperty_test.py b/AutomatedTesting/Gem/PythonTests/EditorPythonBindings/ComponentUpdateListProperty_test.py index ed677ea185..a8ca721cca 100755 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonBindings/ComponentUpdateListProperty_test.py +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonBindings/ComponentUpdateListProperty_test.py @@ -51,5 +51,6 @@ class TestComponentAssetListAutomation(object): editor, "ComponentUpdateListProperty_test_case.py", expected_lines=expected_lines, - cfg_args=[level] + cfg_args=[level], + enable_prefab_system=False, ) diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/README.txt b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/README.txt index 90afee39bc..642d7279a4 100644 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/README.txt +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/README.txt @@ -28,7 +28,7 @@ Assuming CMake is already setup on your operating system, below are some sample cd /path/to/od3e/ mkdir windows_vs2019 cd windows_vs2019 - cmake .. -G "Visual Studio 16 2019" -A x64 -T host=x64 -DLY_3RDPARTY_PATH="%3RDPARTYPATH%" -DLY_PROJECTS=AutomatedTesting + cmake .. -G "Visual Studio 16 2019" -DLY_PROJECTS=AutomatedTesting To manually install the project in development mode using your own installed Python interpreter: cd /path/to/od3e/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/editor_entity_utils.py b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/editor_entity_utils.py index 9e857ed8bc..59e454479c 100644 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/editor_entity_utils.py +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/editor_entity_utils.py @@ -132,6 +132,7 @@ class EditorEntity: def __init__(self, id: azlmbr.entity.EntityId): self.id: azlmbr.entity.EntityId = id + self.components: List[EditorComponent] = [] # Creation functions @classmethod @@ -279,7 +280,7 @@ class EditorEntity: ), f"Failure: Could not add component: '{new_comp.get_component_name()}' to entity: '{self.get_name()}'" new_comp.id = add_component_outcome.GetValue()[0] components.append(new_comp) - + self.components.append(new_comp) return components def get_components_of_type(self, component_names: list) -> List[EditorComponent]: diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py index 985e32ede5..19aa6e9d3c 100644 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py @@ -57,6 +57,10 @@ def add_level_component(component_name): level_component_list, entity.EntityType().Level) level_component_outcome = editor.EditorLevelComponentAPIBus(bus.Broadcast, 'AddComponentsOfType', [level_component_type_ids_list[0]]) + if not level_component_outcome.IsSuccess(): + print('Failed to add {} level component'.format(component_name)) + return None + level_component = level_component_outcome.GetValue()[0] return level_component diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_test_utils.py b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_test_utils.py index 510d1b1149..ee36a640f0 100644 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_test_utils.py +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_test_utils.py @@ -29,7 +29,7 @@ def teardown_editor(editor): def launch_and_validate_results(request, test_directory, editor, editor_script, expected_lines, unexpected_lines=[], halt_on_unexpected=False, run_python="--runpythontest", auto_test_mode=True, null_renderer=True, cfg_args=[], - timeout=300, log_file_name="Editor.log"): + timeout=300, log_file_name="Editor.log", enable_prefab_system=True): """ Runs the Editor with the specified script, and monitors for expected log lines. :param request: Special fixture providing information of the requesting test function. @@ -45,17 +45,22 @@ def launch_and_validate_results(request, test_directory, editor, editor_script, :param cfg_args: Additional arguments for CFG, such as LevelName. :param timeout: Length of time for test to run. Default is 60. :param log_file_name: Name of the log file created by the editor. Defaults to 'Editor.log' + :param enable_prefab_system: Flag to determine whether to use new prefab system or use deprecated slice system. Defaults to True. """ test_case = os.path.join(test_directory, editor_script) request.addfinalizer(lambda: teardown_editor(editor)) logger.debug("Running automated test: {}".format(editor_script)) editor.args.extend(["--skipWelcomeScreenDialog", "--regset=/Amazon/Settings/EnableSourceControl=false", - "--regset=/Amazon/Preferences/EnablePrefabSystem=false", run_python, test_case, + run_python, test_case, f"--pythontestcase={request.node.name}", "--runpythonargs", " ".join(cfg_args)]) if auto_test_mode: editor.args.extend(["--autotest_mode"]) if null_renderer: editor.args.extend(["-rhi=Null"]) + if enable_prefab_system: + editor.args.extend(["--regset=/Amazon/Preferences/EnablePrefabSystem=true"]) + else: + editor.args.extend(["--regset=/Amazon/Preferences/EnablePrefabSystem=false"]) with editor.start(): diff --git a/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Main.py index df1eb62943..450c760786 100644 --- a/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Main.py @@ -23,7 +23,6 @@ from base import TestAutomationBase class TestAutomation(TestAutomationBase): def _run_prefab_test(self, request, workspace, editor, test_module, batch_mode=True, autotest_mode=True): self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"], batch_mode=batch_mode, autotest_mode=autotest_mode) diff --git a/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Sandbox.py b/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Sandbox.py index 52ac19b26e..8f50d4d36d 100644 --- a/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Sandbox.py +++ b/AutomatedTesting/Gem/PythonTests/Multiplayer/TestSuite_Sandbox.py @@ -24,7 +24,6 @@ from base import TestAutomationBase class TestAutomation(TestAutomationBase): def _run_prefab_test(self, request, workspace, editor, test_module, batch_mode=True, autotest_mode=True): self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"], batch_mode=batch_mode, autotest_mode=autotest_mode) diff --git a/AutomatedTesting/Gem/PythonTests/NvCloth/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/NvCloth/TestSuite_Main.py index 49a776a48c..f35f098b0f 100644 --- a/AutomatedTesting/Gem/PythonTests/NvCloth/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/NvCloth/TestSuite_Main.py @@ -25,8 +25,8 @@ class TestAutomation(TestAutomationBase): def test_NvCloth_AddClothSimulationToMesh(self, request, workspace, editor, launcher_platform): from .tests import NvCloth_AddClothSimulationToMesh as test_module - self._run_test(request, workspace, editor, test_module, use_null_renderer = self.use_null_renderer) + self._run_test(request, workspace, editor, test_module, use_null_renderer = self.use_null_renderer, enable_prefab_system=False) def test_NvCloth_AddClothSimulationToActor(self, request, workspace, editor, launcher_platform): from .tests import NvCloth_AddClothSimulationToActor as test_module - self._run_test(request, workspace, editor, test_module, use_null_renderer = self.use_null_renderer) + self._run_test(request, workspace, editor, test_module, use_null_renderer = self.use_null_renderer, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_InDevelopment.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_InDevelopment.py index 9e3a9cb4ff..09d8f5635f 100755 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_InDevelopment.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_InDevelopment.py @@ -26,158 +26,158 @@ class TestAutomation(TestAutomationBase): r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnRagdollBones") def test_Material_LibraryCrudOperationsReflectOnRagdollBones(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnRagdollBones as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Material_RagdollBones(self, request, workspace, editor, launcher_platform): from .tests.material import Material_RagdollBones as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c15308221_material_componentsinsyncwithlibrary.physmaterial", r"AutomatedTesting\Levels\Physics\Material_ComponentsInSyncWithLibrary") def test_Material_ComponentsInSyncWithLibrary(self, request, workspace, editor, launcher_platform): from .tests.material import Material_ComponentsInSyncWithLibrary as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # BUG: LY-107723") def test_ScriptCanvas_SetKinematicTargetTransform(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_SetKinematicTargetTransform as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Failing, PhysXTerrain @fm.file_revert("c4925579_material_addmodifydeleteonterrain.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnTerrain") def test_Material_LibraryCrudOperationsReflectOnTerrain(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnTerrain as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Failing, PhysXTerrain def test_Terrain_TerrainTexturePainterWorks(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_TerrainTexturePainterWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Failing, PhysXTerrain def test_Material_CanBeAssignedToTerrain(self, request, workspace, editor, launcher_platform): from .tests.material import Material_CanBeAssignedToTerrain as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Failing, PhysXTerrain def test_Material_DefaultLibraryConsistentOnAllFeatures(self, request, workspace, editor, launcher_platform): from .tests.material import Material_DefaultLibraryConsistentOnAllFeatures as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Failing, PhysXTerrain @fm.file_revert("all_ones_1.physmaterial", r"AutomatedTesting\Levels\Physics\Material_DefaultMaterialLibraryChangesWork") @fm.file_override("default.physxconfiguration", "Material_DefaultMaterialLibraryChangesWork.physxconfiguration", "AutomatedTesting") def test_Material_DefaultMaterialLibraryChangesWork(self, request, workspace, editor, launcher_platform): from .tests.material import Material_DefaultMaterialLibraryChangesWork as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_SameCollisionGroupSameLayerCollide(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_SameCollisionGroupSameLayerCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Ragdoll_OldRagdollSerializationNoErrors(self, request, workspace, editor, launcher_platform): from .tests.ragdoll import Ragdoll_OldRagdollSerializationNoErrors as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_override("default.physxconfiguration", "ScriptCanvas_OverlapNode.physxconfiguration") def test_ScriptCanvas_OverlapNode(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_OverlapNode as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Material_StaticFriction(self, request, workspace, editor, launcher_platform): from .tests.material import Material_StaticFriction as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c4888315_material_addmodifydeleteoncollider.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnCollider") def test_Material_LibraryCrudOperationsReflectOnCollider(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnCollider as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c15563573_material_addmodifydeleteoncharactercontroller.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnCharacterController") def test_Material_LibraryCrudOperationsReflectOnCharacterController(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnCharacterController as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c4888315_material_addmodifydeleteoncollider.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnCollider") def test_Material_LibraryCrudOperationsReflectOnCollider(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnCollider as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c15563573_material_addmodifydeleteoncharactercontroller.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryCrudOperationsReflectOnCharacterController") def test_Material_LibraryCrudOperationsReflectOnCharacterController(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryCrudOperationsReflectOnCharacterController as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("c4044455_material_librarychangesinstantly.physmaterial", r"AutomatedTesting\Levels\Physics\C4044455_Material_LibraryChangesInstantly") def test_Material_LibraryChangesReflectInstantly(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryChangesReflectInstantly as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @fm.file_revert("Material_LibraryUpdatedAcrossLevels.physmaterial", r"AutomatedTesting\Levels\Physics\Material_LibraryUpdatedAcrossLevels") def test_Material_LibraryUpdatedAcrossLevels(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryUpdatedAcrossLevels as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_RigidBody_LinearDampingAffectsMotion(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_LinearDampingAffectsMotion as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_CollisionAgainstRigidBody(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_CollisionAgainstRigidBody as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ShapeCollider_CylinderShapeCollides(self, request, workspace, editor, launcher_platform): from .tests.collider import ShapeCollider_CylinderShapeCollides as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Physics_WorldBodyBusWorksOnEditorComponents(self, request, workspace, editor, launcher_platform): from .tests import Physics_WorldBodyBusWorksOnEditorComponents as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_PxMeshErrorIfNoMesh(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_PxMeshErrorIfNoMesh as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_ImpulsesBoxShapedRigidBody(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ImpulsesBoxShapedRigidBody as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_SpawnSecondTerrainComponentWarning(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_SpawnSecondTerrainComponentWarning as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_AddPhysTerrainComponent(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_AddPhysTerrainComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_CanAddMultipleTerrainComponents(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_CanAddMultipleTerrainComponents as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_MultipleTerrainComponentsWarning(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_MultipleTerrainComponentsWarning as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_MultipleTerrainComponentsWarning(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_MultipleTerrainComponentsWarning as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_HighValuesDirectionAxesWorkWithNoError(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_HighValuesDirectionAxesWorkWithNoError as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Terrain_MultipleResolutionsValid(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_MultipleResolutionsValid as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_SmallMagnitudeDeviationOnLargeForces(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SmallMagnitudeDeviationOnLargeForces as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main.py index b64fbb5656..07ae5f6ca0 100644 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main.py @@ -30,33 +30,33 @@ class TestAutomation(TestAutomationBase): def test_RigidBody_EnablingGravityWorksUsingNotificationsPoC(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_EnablingGravityWorksUsingNotificationsPoC as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_LocalSpaceForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_LocalSpaceForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_DynamicFriction.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_DynamicFriction(self, request, workspace, editor, launcher_platform): from .tests.material import Material_DynamicFriction as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_SameCollisionGroupDiffLayersCollide(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_SameCollisionGroupDiffLayersCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_CharacterController_SwitchLevels(self, request, workspace, editor, launcher_platform): from .tests.character_controller import CharacterController_SwitchLevels as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Ragdoll_AddPhysxRagdollComponentWorks(self, request, workspace, editor, launcher_platform): from .tests.ragdoll import Ragdoll_AddPhysxRagdollComponentWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ScriptCanvas_MultipleRaycastNode(self, request, workspace, editor, launcher_platform): @@ -64,31 +64,43 @@ class TestAutomation(TestAutomationBase): # Fixme: This test previously relied on unexpected lines log reading with is now not supported. # Now the log reading must be done inside the test, preferably with the Tracer() utility # unexpected_lines = ["Assert"] + test_module.Lines.unexpected - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Collider_DiffCollisionGroupDiffCollidingLayersNotCollide.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Collider_DiffCollisionGroupDiffCollidingLayersNotCollide(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_DiffCollisionGroupDiffCollidingLayersNotCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Joints_HingeLeadFollowerCollide(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_HingeLeadFollowerCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_PxMeshConvexMeshCollides(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_PxMeshConvexMeshCollides as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ShapeCollider_CylinderShapeCollides(self, request, workspace, editor, launcher_platform): from .tests.shape_collider import ShapeCollider_CylinderShapeCollides as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_C15425929_Undo_Redo(self, request, workspace, editor, launcher_platform): from .tests import Physics_UndoRedoWorksOnEntityWithPhysComponents as test_module - self._run_test(request, workspace, editor, test_module) \ No newline at end of file + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) + + @pytest.mark.GROUP_tick + @pytest.mark.xfail(reason="Test still under development.") + def test_Tick_InterpolatedRigidBodyMotionIsSmooth(self, request, workspace, editor, launcher_platform): + from .tests.tick import Tick_InterpolatedRigidBodyMotionIsSmooth as test_module + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) + + @pytest.mark.GROUP_tick + @pytest.mark.xfail(reason="Test still under development.") + def test_Tick_CharacterGameplayComponentMotionIsSmooth(self, request, workspace, editor, launcher_platform): + from .tests.tick import Tick_CharacterGameplayComponentMotionIsSmooth as test_module + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main_Optimized.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main_Optimized.py index 3d668a2085..b25b4340e8 100644 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Main_Optimized.py @@ -59,9 +59,6 @@ class EditorSingleTest_WithFileOverrides(EditorSingleTest): @pytest.mark.parametrize("project", ["AutomatedTesting"]) class TestAutomationWithPrefabSystemEnabled(EditorTestSuite): - global_extra_cmdline_args = ['-BatchMode', '-autotest_mode', - 'extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"]'] - @staticmethod def get_number_parallel_editors(): return 16 @@ -81,6 +78,8 @@ class TestAutomationWithPrefabSystemEnabled(EditorTestSuite): @pytest.mark.parametrize("project", ["AutomatedTesting"]) class TestAutomation(EditorTestSuite): + enable_prefab_system = False + @staticmethod def get_number_parallel_editors(): return 16 diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Periodic.py index 55e51dd2f8..6daf852708 100755 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Periodic.py @@ -31,223 +31,223 @@ class TestAutomation(TestAutomationBase): @revert_physics_config def test_Terrain_NoPhysTerrainComponentNoCollision(self, request, workspace, editor, launcher_platform): from .tests.terrain import Terrain_NoPhysTerrainComponentNoCollision as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_InitialLinearVelocity(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_InitialLinearVelocity as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_StartGravityEnabledWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_StartGravityEnabledWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_KinematicModeWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_KinematicModeWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_LinearDampingForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_LinearDampingForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_SimpleDragForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SimpleDragForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_CapsuleShapedForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_CapsuleShapedForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ImpulsesCapsuleShapedRigidBody(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ImpulsesCapsuleShapedRigidBody as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_MomentOfInertiaManualSetting(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_MomentOfInertiaManualSetting as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_COM_ManualSettingWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_COM_ManualSettingWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_AddRigidBodyComponent(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_AddRigidBodyComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_SplineForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SplineForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_RestitutionCombine.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_RestitutionCombine(self, request, workspace, editor, launcher_platform): from .tests.material import Material_RestitutionCombine as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_FrictionCombine.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_FrictionCombine(self, request, workspace, editor, launcher_platform): from .tests.material import Material_FrictionCombine as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_ColliderPositionOffset(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_ColliderPositionOffset as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_AngularDampingAffectsRotation(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_AngularDampingAffectsRotation as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Physics_VerifyColliderRigidBodyMeshAndTerrainWorkTogether(self, request, workspace, editor, launcher_platform): from .tests import Physics_VerifyColliderRigidBodyMeshAndTerrainWorkTogether as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_MultipleForcesInSameComponentCombineForces(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_MultipleForcesInSameComponentCombineForces as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ImpulsesPxMeshShapedRigidBody(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ImpulsesPxMeshShapedRigidBody as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ScriptCanvas_TriggerEvents(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_TriggerEvents as test_module # FIXME: expected_lines = test_module.LogLines.expected_lines - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroPointForceDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroPointForceDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroWorldSpaceForceDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroWorldSpaceForceDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroLinearDampingDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroLinearDampingDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_MovingForceRegionChangesNetForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_MovingForceRegionChangesNetForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ScriptCanvas_CollisionEvents(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_CollisionEvents as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_DirectionHasNoAffectOnTotalForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_DirectionHasNoAffectOnTotalForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_StartAsleepWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_StartAsleepWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_SliceFileInstantiates(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SliceFileInstantiates as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroLocalSpaceForceDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroLocalSpaceForceDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroSimpleDragForceDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroSimpleDragForceDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_COM_ComputingWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_COM_ComputingWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_MassDifferentValuesWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_MassDifferentValuesWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_RestitutionCombinePriorityOrder.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_RestitutionCombinePriorityOrder(self, request, workspace, editor, launcher_platform): from .tests.material import Material_RestitutionCombinePriorityOrder as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_SplineRegionWithModifiedTransform(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SplineRegionWithModifiedTransform as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ScriptCanvas_ShapeCast(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_ShapeCast as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_InitialAngularVelocity(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_InitialAngularVelocity as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ZeroSplineForceDoesNothing(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ZeroSplineForceDoesNothing as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Physics_DynamicSliceWithPhysNotSpawnsStaticSlice(self, request, workspace, editor, launcher_platform): from .tests import Physics_DynamicSliceWithPhysNotSpawnsStaticSlice as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_PositionOffset(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_PositionOffset as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_FrictionCombinePriorityOrder.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_FrictionCombinePriorityOrder(self, request, workspace, editor, launcher_platform): from .tests.material import Material_FrictionCombinePriorityOrder as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail( reason="Something with the CryRenderer disabling is causing this test to fail now.") @revert_physics_config def test_Ragdoll_LevelSwitchDoesNotCrash(self, request, workspace, editor, launcher_platform): from .tests.ragdoll import Ragdoll_LevelSwitchDoesNotCrash as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_MultipleComponentsCombineForces(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_MultipleComponentsCombineForces as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Marking the test as an expected failure due to sporadic failure on Automated Review: LYN-2580 # The test still runs, but a failure of the test doesn't result in the test run failing @@ -258,102 +258,102 @@ class TestAutomation(TestAutomationBase): 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_PerFaceMaterialGetsCorrectMaterial(self, request, workspace, editor, launcher_platform): from .tests.material import Material_PerFaceMaterialGetsCorrectMaterial as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail( reason="This test will sometimes fail as the ball will continue to roll before the timeout is reached.") @revert_physics_config def test_RigidBody_SleepWhenBelowKineticThreshold(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_SleepWhenBelowKineticThreshold as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_COM_NotIncludesTriggerShapes(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_COM_NotIncludesTriggerShapes as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Material_NoEffectIfNoColliderShape(self, request, workspace, editor, launcher_platform): from .tests.material import Material_NoEffectIfNoColliderShape as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_TriggerPassThrough(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_TriggerPassThrough as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_SetGravityWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_SetGravityWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_CharacterController.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_CharacterController(self, request, workspace, editor, launcher_platform): from .tests.material import Material_CharacterController as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Material_EmptyLibraryUsesDefault(self, request, workspace, editor, launcher_platform): from .tests.material import Material_EmptyLibraryUsesDefault as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_NoQuiverOnHighLinearDampingForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_NoQuiverOnHighLinearDampingForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_RigidBody_ComputeInertiaWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_ComputeInertiaWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ScriptCanvas_PostPhysicsUpdate(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_PostPhysicsUpdate as test_module # Fixme: unexpected_lines = ["Assert"] + test_module.Lines.unexpected - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Collider_NoneCollisionGroupSameLayerNotCollide.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Collider_NoneCollisionGroupSameLayerNotCollide(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_NoneCollisionGroupSameLayerNotCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Collider_SameCollisionGroupSameCustomLayerCollide.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Collider_SameCollisionGroupSameCustomLayerCollide(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_SameCollisionGroupSameCustomLayerCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxdefaultsceneconfiguration.setreg','ScriptCanvas_PostUpdateEvent.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_ScriptCanvas_PostUpdateEvent(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_PostUpdateEvent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Material_Restitution.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Material_Restitution(self, request, workspace, editor, launcher_platform): from .tests.material import Material_Restitution as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxdefaultsceneconfiguration.setreg', 'ScriptCanvas_PreUpdateEvent.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_ScriptCanvas_PreUpdateEvent(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_PreUpdateEvent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_PxMeshShapedForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_PxMeshShapedForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # Marking the Test as expected to fail using the xfail decorator due to sporadic failure on Automated Review: SPEC-3146 # The test still runs, but a failure of the test doesn't result in the test run failing @@ -361,176 +361,173 @@ class TestAutomation(TestAutomationBase): @revert_physics_config def test_RigidBody_MaxAngularVelocityWorks(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_MaxAngularVelocityWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Joints_HingeSoftLimitsConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_HingeSoftLimitsConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Joints_BallSoftLimitsConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_BallSoftLimitsConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Joints_BallLeadFollowerCollide(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_BallLeadFollowerCollide as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Collider_AddingNewGroupWorks.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Collider_AddingNewGroupWorks(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_AddingNewGroupWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ShapeCollider_InactiveWhenNoShapeComponent(self, request, workspace, editor, launcher_platform): from .tests.shape_collider import ShapeCollider_InactiveWhenNoShapeComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_CheckDefaultShapeSettingIsPxMesh(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_CheckDefaultShapeSettingIsPxMesh as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ShapeCollider_LargeNumberOfShapeCollidersWontCrashEditor(self, request, workspace, editor, launcher_platform): from .tests.shape_collider import ShapeCollider_LargeNumberOfShapeCollidersWontCrashEditor as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_SphereShapeEditing(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_SphereShapeEditing as test_module - self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"]) + self._run_test(request, workspace, editor, test_module) @revert_physics_config def test_Collider_BoxShapeEditing(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_BoxShapeEditing as test_module - self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"]) + self._run_test(request, workspace, editor, test_module) @revert_physics_config def test_Collider_CapsuleShapeEditing(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_CapsuleShapeEditing as test_module - self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"]) + self._run_test(request, workspace, editor, test_module) def test_ForceRegion_WithNonTriggerColliderWarning(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_WithNonTriggerColliderWarning as test_module # Fixme: expected_lines = ["[Warning] (PhysX Force Region) - Please ensure collider component marked as trigger exists in entity"] - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_WorldSpaceForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_WorldSpaceForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_PointForceOnRigidBodies(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_PointForceOnRigidBodies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_SphereShapedForce(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_SphereShapedForce as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ForceRegion_RotationalOffset(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_RotationalOffset as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Material_LibraryClearingAssignsDefault(self, request, workspace, editor, launcher_platform): from .tests.material import Material_LibraryClearingAssignsDefault as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_AddColliderComponent(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_AddColliderComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail( reason="This will fail due to this issue ATOM-15487.") def test_Collider_PxMeshAutoAssignedWhenModifyingRenderMeshComponent(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_PxMeshAutoAssignedWhenModifyingRenderMeshComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_PxMeshAutoAssignedWhenAddingRenderMeshComponent(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_PxMeshAutoAssignedWhenAddingRenderMeshComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_MultipleSurfaceSlots(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_MultipleSurfaceSlots as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Collider_PxMeshNotAutoAssignedWhenNoPhysicsFbx(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_PxMeshNotAutoAssignedWhenNoPhysicsFbx as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_RigidBody_EnablingGravityWorksPoC(self, request, workspace, editor, launcher_platform): from .tests.rigid_body import RigidBody_EnablingGravityWorksPoC as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config @fm.file_override('physxsystemconfiguration.setreg','Collider_CollisionGroupsWorkflow.setreg_override', 'AutomatedTesting/Registry', search_subdirs=True) def test_Collider_CollisionGroupsWorkflow(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_CollisionGroupsWorkflow as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Collider_ColliderRotationOffset(self, request, workspace, editor, launcher_platform): from .tests.collider import Collider_ColliderRotationOffset as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ForceRegion_ParentChildForcesCombineForces(self, request, workspace, editor, launcher_platform): from .tests.force_region import ForceRegion_ParentChildForcesCombineForces as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_ShapeCollider_CanBeAddedWitNoWarnings(self, request, workspace, editor, launcher_platform): from .tests.shape_collider import ShapeCollider_CanBeAddedWitNoWarnings as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Physics_UndoRedoWorksOnEntityWithPhysComponents(self, request, workspace, editor, launcher_platform): from .tests import Physics_UndoRedoWorksOnEntityWithPhysComponents as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_Fixed2BodiesConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_Fixed2BodiesConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_Hinge2BodiesConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_Hinge2BodiesConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_Ball2BodiesConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_Ball2BodiesConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_FixedBreakable(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_FixedBreakable as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_HingeBreakable(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_HingeBreakable as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_BallBreakable(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_BallBreakable as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_HingeNoLimitsConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_HingeNoLimitsConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_BallNoLimitsConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_BallNoLimitsConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Joints_GlobalFrameConstrained(self, request, workspace, editor, launcher_platform): from .tests.joints import Joints_GlobalFrameConstrained as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @revert_physics_config def test_Material_DefaultLibraryUpdatedAcrossLevels(self, request, workspace, editor, launcher_platform): @@ -540,7 +537,7 @@ class TestAutomation(TestAutomationBase): search_subdirs=True) def levels_before(self, request, workspace, editor, launcher_platform): from .tests.material import Material_DefaultLibraryUpdatedAcrossLevels_before as test_module_0 - self._run_test(request, workspace, editor, test_module_0) + self._run_test(request, workspace, editor, test_module_0, enable_prefab_system=False) # File override replaces the previous physxconfiguration file with another where the only difference is the default material library @fm.file_override("physxsystemconfiguration.setreg", @@ -549,7 +546,7 @@ class TestAutomation(TestAutomationBase): search_subdirs=True) def levels_after(self, request, workspace, editor, launcher_platform): from .tests.material import Material_DefaultLibraryUpdatedAcrossLevels_after as test_module_1 - self._run_test(request, workspace, editor, test_module_1) + self._run_test(request, workspace, editor, test_module_1, enable_prefab_system=False) levels_before(self, request, workspace, editor, launcher_platform) levels_after(self, request, workspace, editor, launcher_platform) \ No newline at end of file diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Sandbox.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Sandbox.py index 81fdc747d5..9b104ccfcf 100644 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Sandbox.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Sandbox.py @@ -32,7 +32,7 @@ class TestAutomation(TestAutomationBase): def test_ScriptCanvas_GetCollisionNameReturnsName(self, request, workspace, editor, launcher_platform): from .tests.script_canvas import ScriptCanvas_GetCollisionNameReturnsName as test_module # Fixme: expected_lines=["Layer Name: Right"] - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) ## Seems to be flaky, need to investigate def test_ScriptCanvas_GetCollisionNameReturnsNothingWhenHasToggledLayer(self, request, workspace, editor, launcher_platform): @@ -42,4 +42,4 @@ class TestAutomation(TestAutomationBase): # Fixme: for group in collision_groups: # Fixme: unexpected_lines.append(f"GroupName: {group}") # Fixme: expected_lines=["GroupName: "] - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Utils.py b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Utils.py index 61c2816f50..3e00b684c0 100755 --- a/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Utils.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/TestSuite_Utils.py @@ -30,11 +30,11 @@ class TestUtils(TestAutomationBase): expected_lines = [] unexpected_lines = ["Assert"] - self._run_test(request, workspace, editor, physmaterial_editor_test_module, expected_lines, unexpected_lines) + self._run_test(request, workspace, editor, physmaterial_editor_test_module, expected_lines, unexpected_lines, enable_prefab_system=False) def test_UtilTest_Tracer_PicksErrorsAndWarnings(self, request, workspace, launcher_platform, editor): from .utils import UtilTest_Tracer_PicksErrorsAndWarnings as testcase_module - self._run_test(request, workspace, editor, testcase_module, [], []) + self._run_test(request, workspace, editor, testcase_module, [], [], enable_prefab_system=False) def test_FileManagement_FindingFiles(self, workspace, launcher_platform): """ @@ -263,4 +263,4 @@ class TestUtils(TestAutomationBase): expected_lines = [] unexpected_lines = ["Assert"] - self._run_test(request, workspace, editor, test_module, expected_lines, unexpected_lines) + self._run_test(request, workspace, editor, test_module, expected_lines, unexpected_lines, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_CharacterGameplayComponentMotionIsSmooth.py b/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_CharacterGameplayComponentMotionIsSmooth.py new file mode 100644 index 0000000000..fe718d7247 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_CharacterGameplayComponentMotionIsSmooth.py @@ -0,0 +1,99 @@ +""" +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 + +Test Case Title : Verify that an entity with a character gameplay component moves smoothly. +""" + + +# fmt: off +class Tests(): + create_entity = ("Created test entity", "Failed to create test entity") + character_controller_added = ("Added PhysX Character Controller component", "Failed to add PhysX Character Controller component") + character_gameplay_added = ("Added PhysX Character Gameplay component", "Failed to add PhysX Character Gameplay component") + enter_game_mode = ("Entered game mode", "Failed to enter game mode") + exit_game_mode = ("Exited game mode", "Failed to exit game mode") + character_motion_smooth = ("Character motion passed smoothness threshold", "Failed to meet smoothness threshold for character motion") +# fmt: on + + +def Tick_CharacterGameplayComponentMotionIsSmooth(): + """ + Summary: + Create entity with PhysX Character Controller and PhysX Character Gameplay components. + Verify that the motion of the character controller under gravity is smooth. + + Expected Behavior: + 1) The motion of the character controller under gravity is a smooth curve, rather than an erratic/jittery movement. + + Test Steps: + 1) Load the empty level + 2) Create an entity + 3) Add a PhysX Character Controller Component and PhysX Character Gameplay component + 4) Enter game mode and collect data for the character controller's z co-ordinate and the time values for a series of frames + 5) Check if the motion of the character controller was sufficiently smooth + + :return: None + """ + # imports + import os + import azlmbr.legacy.general as general + import azlmbr.math as math + from editor_python_test_tools.editor_entity_utils import EditorEntity as Entity + from editor_python_test_tools.utils import Report + from editor_python_test_tools.utils import TestHelper as helper + from editor_python_test_tools.asset_utils import Asset + import numpy as np + + # constants + COEFFICIENT_OF_DETERMINATION_THRESHOLD = 1 - 1e-4 # curves with values below this are not considered sufficiently smooth + + helper.init_idle() + # 1) Load the empty level + helper.open_level("", "Base") + + # 2) Create an entity + test_entity = Entity.create_editor_entity("test_entity") + Report.result(Tests.create_entity, test_entity.id.IsValid()) + + azlmbr.components.TransformBus( + azlmbr.bus.Event, "SetWorldTranslation", test_entity.id, math.Vector3(0.0, 0.0, 0.0)) + + # 3) Add character controller and character gameplay components + character_controller_component = test_entity.add_component("PhysX Character Controller") + Report.result(Tests.character_controller_added, test_entity.has_component("PhysX Character Controller")) + character_gameplay_component = test_entity.add_component("PhysX Character Gameplay") + Report.result(Tests.character_gameplay_added, test_entity.has_component("PhysX Character Gameplay")) + + # 4) Enter game mode and collect data for the rigid body's z co-ordinate and the time values for a series of frames + t = [] + z = [] + helper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + game_entity_id = general.find_game_entity("test_entity") + for frame in range(100): + t.append(azlmbr.components.TickRequestBus(azlmbr.bus.Broadcast, "GetTimeAtCurrentTick").GetSeconds()) + z.append(azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldZ", game_entity_id)) + general.idle_wait_frames(1) + helper.exit_game_mode(Tests.exit_game_mode) + + # 5) Test that the z vs t curve is sufficiently smooth (if the interpolation is not working well, the curve will be less smooth) + # normalize the t and z data + t = np.array(t) - np.mean(t) + z = np.array(z) - np.mean(z) + # fit a polynomial to the z vs t curve + fit = np.poly1d(np.polyfit(t, z, 4)) + residual = fit(t) - z + # calculate the coefficient of determination (a measure of how closely the polynomial curve fits the data) + # if the coefficient is very close to 1, then the curve fits the data very well, suggesting that the rigid body motion is smooth + # if the coefficient is significantly less than 1, then the z values vary more erratically relative to the smooth curve, + # indicating that the motion of the rigid body is not smooth + coefficient_of_determination = (1 - np.sum(residual * residual) / np.sum(z * z)) + Report.result(Tests.character_motion_smooth, bool(coefficient_of_determination > COEFFICIENT_OF_DETERMINATION_THRESHOLD)) + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(Tick_CharacterGameplayComponentMotionIsSmooth) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_InterpolatedRigidBodyMotionIsSmooth.py b/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_InterpolatedRigidBodyMotionIsSmooth.py new file mode 100644 index 0000000000..19e79355d9 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Physics/tests/tick/Tick_InterpolatedRigidBodyMotionIsSmooth.py @@ -0,0 +1,98 @@ +""" +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 + +Test Case Title : Verify that a rigid body with "Interpolate motion" option selected moves smoothly. +""" + + +# fmt: off +class Tests(): + create_entity = ("Created test entity", "Failed to create test entity") + rigid_body_added = ("Added PhysX Rigid Body component", "Failed to add PhysX Rigid Body component") + enter_game_mode = ("Entered game mode", "Failed to enter game mode") + exit_game_mode = ("Exited game mode", "Failed to exit game mode") + rigid_body_smooth = ("Rigid body motion passed smoothness threshold", "Failed to meet smoothness threshold for rigid body motion") +# fmt: on + + +def Tick_InterpolatedRigidBodyMotionIsSmooth(): + """ + Summary: + Create entity with PhysX Rigid Body component and turn on the Interpolate motion setting. + Verify that the position of the rigid body varies smoothly with time. + + Expected Behavior: + 1) The motion of the rigid body under gravity is a smooth curve, rather than an erratic/jittery movement. + + Test Steps: + 1) Load the empty level + 2) Create an entity + 3) Add rigid body component + 4) Enter game mode and collect data for the rigid body's z co-ordinate and the time values for a series of frames + 5) Check if the motion of the rigid body was sufficiently smooth + + :return: None + """ + # imports + import os + import azlmbr.legacy.general as general + import azlmbr.math as math + from editor_python_test_tools.editor_entity_utils import EditorEntity as Entity + from editor_python_test_tools.utils import Report + from editor_python_test_tools.utils import TestHelper as helper + from editor_python_test_tools.asset_utils import Asset + import numpy as np + + # constants + COEFFICIENT_OF_DETERMINATION_THRESHOLD = 1 - 1e-4 # curves with values below this are not considered sufficiently smooth + + helper.init_idle() + # 1) Load the empty level + helper.open_level("", "Base") + + # 2) Create an entity + test_entity = Entity.create_editor_entity("test_entity") + Report.result(Tests.create_entity, test_entity.id.IsValid()) + + azlmbr.components.TransformBus( + azlmbr.bus.Event, "SetWorldTranslation", test_entity.id, math.Vector3(0.0, 0.0, 0.0)) + + # 3) Add rigid body component + rigid_body_component = test_entity.add_component("PhysX Rigid Body") + rigid_body_component.set_component_property_value("Configuration|Interpolate motion", True) + azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "SetLinearDamping", test_entity.id, 0.0) + Report.result(Tests.rigid_body_added, test_entity.has_component("PhysX Rigid Body")) + + # 4) Enter game mode and collect data for the rigid body's z co-ordinate and the time values for a series of frames + t = [] + z = [] + helper.enter_game_mode(Tests.enter_game_mode) + general.idle_wait_frames(1) + game_entity_id = general.find_game_entity("test_entity") + for frame in range(100): + t.append(azlmbr.components.TickRequestBus(azlmbr.bus.Broadcast, "GetTimeAtCurrentTick").GetSeconds()) + z.append(azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldZ", game_entity_id)) + general.idle_wait_frames(1) + helper.exit_game_mode(Tests.exit_game_mode) + + # 5) Test that the z vs t curve is sufficiently smooth (if the interpolation is not working well, the curve will be less smooth) + # normalize the t and z data + t = np.array(t) - np.mean(t) + z = np.array(z) - np.mean(z) + # fit a polynomial to the z vs t curve + fit = np.poly1d(np.polyfit(t, z, 4)) + residual = fit(t) - z + # calculate the coefficient of determination (a measure of how closely the polynomial curve fits the data) + # if the coefficient is very close to 1, then the curve fits the data very well, suggesting that the rigid body motion is smooth + # if the coefficient is significantly less than 1, then the z values vary more erratically relative to the smooth curve, + # indicating that the motion of the rigid body is not smooth + coefficient_of_determination = (1 - np.sum(residual * residual) / np.sum(z * z)) + Report.result(Tests.rigid_body_smooth, bool(coefficient_of_determination > COEFFICIENT_OF_DETERMINATION_THRESHOLD)) + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + Report.start_test(Tick_InterpolatedRigidBodyMotionIsSmooth) diff --git a/AutomatedTesting/Gem/PythonTests/Physics/utils/FileManagement.py b/AutomatedTesting/Gem/PythonTests/Physics/utils/FileManagement.py index 22a41de4ea..017bb672a4 100644 --- a/AutomatedTesting/Gem/PythonTests/Physics/utils/FileManagement.py +++ b/AutomatedTesting/Gem/PythonTests/Physics/utils/FileManagement.py @@ -218,6 +218,7 @@ class FileManagement: src_file_path = os.path.join(src_path, src_file) if os.path.exists(target_file_path): fs.unlock_file(target_file_path) + os.makedirs(target_path, exist_ok=True) shutil.copyfile(src_file_path, target_file_path) @staticmethod diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Prefab/TestSuite_Main.py index 479915752f..c1d4836342 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/TestSuite_Main.py @@ -12,7 +12,6 @@ import pytest import os import sys -from ly_test_tools import LAUNCHERS sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../automatedtesting_shared') from base import TestAutomationBase @@ -24,42 +23,41 @@ class TestAutomation(TestAutomationBase): def _run_prefab_test(self, request, workspace, editor, test_module, batch_mode=True, autotest_mode=True): self._run_test(request, workspace, editor, test_module, - extra_cmdline_args=["--regset=/Amazon/Preferences/EnablePrefabSystem=true"], batch_mode=batch_mode, autotest_mode=autotest_mode) - def test_PrefabLevel_OpensLevelWithEntities(self, request, workspace, editor, launcher_platform): - from .tests import PrefabLevel_OpensLevelWithEntities as test_module + def test_OpenLevel_ContainingTwoEntities(self, request, workspace, editor, launcher_platform): + from Prefab.tests.open_level import OpenLevel_ContainingTwoEntities as test_module self._run_prefab_test(request, workspace, editor, test_module) - def test_PrefabBasicWorkflow_CreatePrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_CreatePrefab as test_module + def test_CreatePrefab_WithSingleEntity(self, request, workspace, editor, launcher_platform): + from Prefab.tests.create_prefab import CreatePrefab_WithSingleEntity as test_module self._run_prefab_test(request, workspace, editor, test_module) - def test_PrefabBasicWorkflow_InstantiatePrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_InstantiatePrefab as test_module + def test_InstantiatePrefab_ContainingASingleEntity(self, request, workspace, editor, launcher_platform): + from Prefab.tests.instantiate_prefab import InstantiatePrefab_ContainingASingleEntity as test_module self._run_prefab_test(request, workspace, editor, test_module) - def test_PrefabBasicWorkflow_CreateAndDeletePrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_CreateAndDeletePrefab as test_module + def test_DeletePrefab_ContainingASingleEntity(self, request, workspace, editor, launcher_platform): + from Prefab.tests.delete_prefab import DeletePrefab_ContainingASingleEntity as test_module self._run_prefab_test(request, workspace, editor, test_module) - def test_PrefabBasicWorkflow_CreateAndReparentPrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_CreateAndReparentPrefab as test_module + def test_ReparentPrefab_UnderAnotherPrefab(self, request, workspace, editor, launcher_platform): + from Prefab.tests.reparent_prefab import ReparentPrefab_UnderAnotherPrefab as test_module self._run_prefab_test(request, workspace, editor, test_module, autotest_mode=False) - def test_PrefabBasicWorkflow_CreateReparentAndDetachPrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_CreateReparentAndDetachPrefab as test_module + def test_DetachPrefab_UnderAnotherPrefab(self, request, workspace, editor, launcher_platform): + from Prefab.tests.detach_prefab import DetachPrefab_UnderAnotherPrefab as test_module self._run_prefab_test(request, workspace, editor, test_module, autotest_mode=False) - def test_PrefabBasicWorkflow_CreateAndDuplicatePrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabBasicWorkflow_CreateAndDuplicatePrefab as test_module + def test_DuplicatePrefab_ContainingASingleEntity(self, request, workspace, editor, launcher_platform): + from Prefab.tests.duplicate_prefab import DuplicatePrefab_ContainingASingleEntity as test_module self._run_prefab_test(request, workspace, editor, test_module) - def test_PrefabComplexWorflow_CreatePrefabOfChildEntity(self, request, workspace, editor, launcher_platform): - from .tests import PrefabComplexWorflow_CreatePrefabOfChildEntity as test_module + def test_CreatePrefab_UnderAnEntity(self, request, workspace, editor, launcher_platform): + from Prefab.tests.create_prefab import CreatePrefab_UnderAnEntity as test_module self._run_prefab_test(request, workspace, editor, test_module, autotest_mode=False) - def test_PrefabComplexWorflow_CreatePrefabInsidePrefab(self, request, workspace, editor, launcher_platform): - from .tests import PrefabComplexWorflow_CreatePrefabInsidePrefab as test_module + def test_CreatePrefab_UnderAnotherPrefab(self, request, workspace, editor, launcher_platform): + from Prefab.tests.create_prefab import CreatePrefab_UnderAnotherPrefab as test_module self._run_prefab_test(request, workspace, editor, test_module, autotest_mode=False) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabOfChildEntity.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnEntity.py similarity index 94% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabOfChildEntity.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnEntity.py index dec44d52be..5033c1da9c 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabOfChildEntity.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnEntity.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabComplexWorflow_CreatePrefabOfChildEntity(): +def CreatePrefab_UnderAnEntity(): """ Test description: - Creates two entities, parent and child. Child entity has Parent entity as its parent. @@ -18,7 +18,7 @@ def PrefabComplexWorflow_CreatePrefabOfChildEntity(): from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -49,4 +49,4 @@ def PrefabComplexWorflow_CreatePrefabOfChildEntity(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabComplexWorflow_CreatePrefabOfChildEntity) + Report.start_test(CreatePrefab_UnderAnEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabInsidePrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnotherPrefab.py similarity index 94% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabInsidePrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnotherPrefab.py index e14fc96449..429da49434 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabComplexWorflow_CreatePrefabInsidePrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_UnderAnotherPrefab.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabComplexWorflow_CreatePrefabInsidePrefab(): +def CreatePrefab_UnderAnotherPrefab(): """ Test description: - Creates an entity with a physx collider @@ -17,7 +17,7 @@ def PrefabComplexWorflow_CreatePrefabInsidePrefab(): from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -54,4 +54,4 @@ def PrefabComplexWorflow_CreatePrefabInsidePrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabComplexWorflow_CreatePrefabInsidePrefab) + Report.start_test(CreatePrefab_UnderAnotherPrefab) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreatePrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_WithSingleEntity.py similarity index 84% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreatePrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_WithSingleEntity.py index cae105a9a9..80f4c0e596 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreatePrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/create_prefab/CreatePrefab_WithSingleEntity.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_CreatePrefab(): +def CreatePrefab_WithSingleEntity(): CAR_PREFAB_FILE_NAME = 'car_prefab' @@ -13,7 +13,7 @@ def PrefabBasicWorkflow_CreatePrefab(): from editor_python_test_tools.utils import Report from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -26,4 +26,4 @@ def PrefabBasicWorkflow_CreatePrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_CreatePrefab) + Report.start_test(CreatePrefab_WithSingleEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDeletePrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/delete_prefab/DeletePrefab_ContainingASingleEntity.py similarity index 83% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDeletePrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/delete_prefab/DeletePrefab_ContainingASingleEntity.py index bbebd70e04..919168019e 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDeletePrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/delete_prefab/DeletePrefab_ContainingASingleEntity.py @@ -5,14 +5,14 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_CreateAndDeletePrefab(): +def DeletePrefab_ContainingASingleEntity(): CAR_PREFAB_FILE_NAME = 'car_prefab' from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -29,4 +29,4 @@ def PrefabBasicWorkflow_CreateAndDeletePrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_CreateAndDeletePrefab) + Report.start_test(DeletePrefab_ContainingASingleEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateReparentAndDetachPrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/detach_prefab/DetachPrefab_UnderAnotherPrefab.py similarity index 89% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateReparentAndDetachPrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/detach_prefab/DetachPrefab_UnderAnotherPrefab.py index bdf77c4bf3..5c97f0b032 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateReparentAndDetachPrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/detach_prefab/DetachPrefab_UnderAnotherPrefab.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_CreateReparentAndDetachPrefab(): +def DetachPrefab_UnderAnotherPrefab(): CAR_PREFAB_FILE_NAME = 'car_prefab' WHEEL_PREFAB_FILE_NAME = 'wheel_prefab' @@ -18,7 +18,7 @@ def PrefabBasicWorkflow_CreateReparentAndDetachPrefab(): from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -48,4 +48,4 @@ def PrefabBasicWorkflow_CreateReparentAndDetachPrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_CreateReparentAndDetachPrefab) + Report.start_test(DetachPrefab_UnderAnotherPrefab) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDuplicatePrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/duplicate_prefab/DuplicatePrefab_ContainingASingleEntity.py similarity index 83% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDuplicatePrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/duplicate_prefab/DuplicatePrefab_ContainingASingleEntity.py index 2479ae549e..e611303fbb 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndDuplicatePrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/duplicate_prefab/DuplicatePrefab_ContainingASingleEntity.py @@ -5,14 +5,14 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_CreateAndDuplicatePrefab(): +def DuplicatePrefab_ContainingASingleEntity(): CAR_PREFAB_FILE_NAME = 'car_prefab' from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -29,4 +29,4 @@ def PrefabBasicWorkflow_CreateAndDuplicatePrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_CreateAndDuplicatePrefab) + Report.start_test(DuplicatePrefab_ContainingASingleEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_InstantiatePrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/instantiate_prefab/InstantiatePrefab_ContainingASingleEntity.py similarity index 85% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_InstantiatePrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/instantiate_prefab/InstantiatePrefab_ContainingASingleEntity.py index a701802cd4..a81608ee8a 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_InstantiatePrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/instantiate_prefab/InstantiatePrefab_ContainingASingleEntity.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_InstantiatePrefab(): +def InstantiatePrefab_ContainingASingleEntity(): from azlmbr.math import Vector3 @@ -15,7 +15,7 @@ def PrefabBasicWorkflow_InstantiatePrefab(): from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -30,4 +30,4 @@ def PrefabBasicWorkflow_InstantiatePrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_InstantiatePrefab) + Report.start_test(InstantiatePrefab_ContainingASingleEntity) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabLevel_OpensLevelWithEntities.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/open_level/OpenLevel_ContainingTwoEntities.py similarity index 96% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabLevel_OpensLevelWithEntities.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/open_level/OpenLevel_ContainingTwoEntities.py index 0eb7e86a9e..787d7000d2 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabLevel_OpensLevelWithEntities.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/open_level/OpenLevel_ContainingTwoEntities.py @@ -14,7 +14,7 @@ class Tests(): # fmt:on -def PrefabLevel_OpensLevelWithEntities(): +def OpenLevel_ContainingTwoEntities(): """ Opens the level that contains 2 entities, "EmptyEntity" and "EntityWithPxCollider". This test makes sure that both entities exist after opening the level and that: @@ -70,4 +70,4 @@ def PrefabLevel_OpensLevelWithEntities(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabLevel_OpensLevelWithEntities) + Report.start_test(OpenLevel_ContainingTwoEntities) diff --git a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndReparentPrefab.py b/AutomatedTesting/Gem/PythonTests/Prefab/tests/reparent_prefab/ReparentPrefab_UnderAnotherPrefab.py similarity index 89% rename from AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndReparentPrefab.py rename to AutomatedTesting/Gem/PythonTests/Prefab/tests/reparent_prefab/ReparentPrefab_UnderAnotherPrefab.py index 1cbc591c29..2c460a3298 100644 --- a/AutomatedTesting/Gem/PythonTests/Prefab/tests/PrefabBasicWorkflow_CreateAndReparentPrefab.py +++ b/AutomatedTesting/Gem/PythonTests/Prefab/tests/reparent_prefab/ReparentPrefab_UnderAnotherPrefab.py @@ -5,7 +5,7 @@ For complete copyright and license terms please see the LICENSE at the root of t SPDX-License-Identifier: Apache-2.0 OR MIT """ -def PrefabBasicWorkflow_CreateAndReparentPrefab(): +def ReparentPrefab_UnderAnotherPrefab(): CAR_PREFAB_FILE_NAME = 'car_prefab' WHEEL_PREFAB_FILE_NAME = 'wheel_prefab' @@ -18,7 +18,7 @@ def PrefabBasicWorkflow_CreateAndReparentPrefab(): from editor_python_test_tools.editor_entity_utils import EditorEntity from editor_python_test_tools.prefab_utils import Prefab - import PrefabTestUtils as prefab_test_utils + import Prefab.tests.PrefabTestUtils as prefab_test_utils prefab_test_utils.open_base_tests_level() @@ -45,4 +45,4 @@ def PrefabBasicWorkflow_CreateAndReparentPrefab(): if __name__ == "__main__": from editor_python_test_tools.utils import Report - Report.start_test(PrefabBasicWorkflow_CreateAndReparentPrefab) + Report.start_test(ReparentPrefab_UnderAnotherPrefab) diff --git a/AutomatedTesting/Gem/PythonTests/PythonAssetBuilder/AssetBuilder_test_case.py b/AutomatedTesting/Gem/PythonTests/PythonAssetBuilder/AssetBuilder_test_case.py index 8d418222ce..112fd013bf 100644 --- a/AutomatedTesting/Gem/PythonTests/PythonAssetBuilder/AssetBuilder_test_case.py +++ b/AutomatedTesting/Gem/PythonTests/PythonAssetBuilder/AssetBuilder_test_case.py @@ -8,42 +8,54 @@ import azlmbr.bus import azlmbr.asset import azlmbr.editor import azlmbr.math -import azlmbr.legacy.general -def raise_and_stop(msg): - print (msg) +print('Starting mock asset tests') +handler = azlmbr.editor.EditorEventBusHandler() + +def on_notify_editor_initialized(args): + # These tests are meant to check that the test_asset.mock source asset turned into + # a test_asset.mock_asset product asset via the Python asset builder system + mockAssetType = azlmbr.math.Uuid_CreateString('{9274AD17-3212-4651-9F3B-7DCCB080E467}', 0) + mockAssetPath = 'gem/pythontests/pythonassetbuilder/test_asset.mock_asset' + assetId = azlmbr.asset.AssetCatalogRequestBus(azlmbr.bus.Broadcast, 'GetAssetIdByPath', mockAssetPath, mockAssetType, False) + if (assetId.is_valid() is False): + print(f'Mock AssetId is not valid! Got {assetId.to_string()} instead') + else: + print(f'Mock AssetId is valid!') + + assetIdString = assetId.to_string() + if (assetIdString.endswith(':528cca58') is False): + print(f'Mock AssetId {assetIdString} has unexpected sub-id for {mockAssetPath}!') + else: + print(f'Mock AssetId has expected sub-id for {mockAssetPath}!') + + print ('Mock asset exists') + + # These tests detect if the geom_group.fbx file turns into a number of azmodel product assets + def test_azmodel_product(generatedModelAssetPath): + azModelAssetType = azlmbr.math.Uuid_CreateString('{2C7477B6-69C5-45BE-8163-BCD6A275B6D8}', 0) + assetId = azlmbr.asset.AssetCatalogRequestBus(azlmbr.bus.Broadcast, 'GetAssetIdByPath', generatedModelAssetPath, azModelAssetType, False) + assetIdString = assetId.to_string() + if (assetId.is_valid()): + print(f'AssetId found for asset ({generatedModelAssetPath}) found') + else: + print(f'Asset at path {generatedModelAssetPath} has unexpected asset ID ({assetIdString})!') + + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_z_positive_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_z_negative_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_y_positive_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_y_negative_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_x_positive_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_x_negative_1.azmodel') + test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_center_1.azmodel') + + # clear up notification handler + global handler + handler.disconnect() + handler = None + + print('Finished mock asset tests') azlmbr.editor.EditorToolsApplicationRequestBus(azlmbr.bus.Broadcast, 'ExitNoPrompt') -# These tests are meant to check that the test_asset.mock source asset turned into -# a test_asset.mock_asset product asset via the Python asset builder system -mockAssetType = azlmbr.math.Uuid_CreateString('{9274AD17-3212-4651-9F3B-7DCCB080E467}', 0) -mockAssetPath = 'gem/pythontests/pythonassetbuilder/test_asset.mock_asset' -assetId = azlmbr.asset.AssetCatalogRequestBus(azlmbr.bus.Broadcast, 'GetAssetIdByPath', mockAssetPath, mockAssetType, False) -if (assetId.is_valid() is False): - raise_and_stop(f'Mock AssetId is not valid! Got {assetId.to_string()} instead') - -assetIdString = assetId.to_string() -if (assetIdString.endswith(':528cca58') is False): - raise_and_stop(f'Mock AssetId {assetIdString} has unexpected sub-id for {mockAssetPath}!') - -print ('Mock asset exists') - -# These tests detect if the geom_group.fbx file turns into a number of azmodel product assets -def test_azmodel_product(generatedModelAssetPath): - azModelAssetType = azlmbr.math.Uuid_CreateString('{2C7477B6-69C5-45BE-8163-BCD6A275B6D8}', 0) - assetId = azlmbr.asset.AssetCatalogRequestBus(azlmbr.bus.Broadcast, 'GetAssetIdByPath', generatedModelAssetPath, azModelAssetType, False) - assetIdString = assetId.to_string() - if (assetId.is_valid()): - print(f'AssetId found for asset ({generatedModelAssetPath}) found') - else: - raise_and_stop(f'Asset at path {generatedModelAssetPath} has unexpected asset ID ({assetIdString})!') - -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_z_positive_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_z_negative_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_y_positive_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_y_negative_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_x_positive_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_x_negative_1.azmodel') -test_azmodel_product('gem/pythontests/pythonassetbuilder/geom_group_fbx_cube_100cm_center_1.azmodel') - -azlmbr.editor.EditorToolsApplicationRequestBus(azlmbr.bus.Broadcast, 'ExitNoPrompt') +handler.connect() +handler.add_callback('NotifyEditorInitialized', on_notify_editor_initialized) diff --git a/AutomatedTesting/Gem/PythonTests/Terrain/CMakeLists.txt b/AutomatedTesting/Gem/PythonTests/Terrain/CMakeLists.txt new file mode 100644 index 0000000000..9f8ba06829 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Terrain/CMakeLists.txt @@ -0,0 +1,24 @@ +# +# 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 +# +# + +if(PAL_TRAIT_BUILD_TESTS_SUPPORTED AND PAL_TRAIT_BUILD_HOST_TOOLS) + + ly_add_pytest( + NAME AutomatedTesting::TerrainTests_Main + TEST_SUITE main + TEST_SERIAL + PATH ${CMAKE_CURRENT_LIST_DIR}/TestSuite_Main.py + RUNTIME_DEPENDENCIES + Legacy::Editor + AZ::AssetProcessor + AutomatedTesting.Assets + COMPONENT + Terrain + ) + +endif() diff --git a/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges.py b/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges.py new file mode 100644 index 0000000000..f4c2a19884 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges.py @@ -0,0 +1,89 @@ +""" +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 +""" + +#fmt: off +class Tests(): + create_test_entity = ("Entity created successfully", "Failed to create Entity") + add_axis_aligned_box_shape = ("Axis Aligned Box Shape component added", "Failed to add Axis Aligned Box Shape component") + add_terrain_collider = ("Terrain Physics Heightfield Collider component added", "Failed to add a Terrain Physics Heightfield Collider component") + box_dimensions_changed = ("Aabb dimensions changed successfully", "Failed change Aabb dimensions") + configuration_changed = ("Terrain size changed successfully", "Failed terrain size change") +#fmt: on + +def TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges(): + """ + Summary: + Test aspects of the Terrain Physics Heightfield Collider through the BehaviorContext and the Property Tree. + + Test Steps: + Expected Behavior: + The Editor is stable there are no warnings or errors. + + Test Steps: + 1) Load the base level + 2) Create test entity + 3) Start the Tracer to catch any errors and warnings + 4) Add the Axis Aligned Box Shape and Terrain Physics Heightfield Collider components + 5) Change the Axis Aligned Box Shape dimensions + 6) Check the Heightfield provider is returning the correct size + 7) Verify there are no errors and warnings in the logs + + + :return: None + """ + + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import TestHelper as helper + from editor_python_test_tools.utils import Report, Tracer + import azlmbr.legacy.general as general + import azlmbr.physics as physics + import azlmbr.math as azmath + import azlmbr.bus as bus + import sys + import math + + SET_BOX_X_SIZE = 5.0 + SET_BOX_Y_SIZE = 6.0 + EXPECTED_COLUMN_SIZE = SET_BOX_X_SIZE + 1 + EXPECTED_ROW_SIZE = SET_BOX_Y_SIZE + 1 + helper.init_idle() + + # 1) Load the level + helper.open_level("", "Base") + + # 2) Create test entity + test_entity = EditorEntity.create_editor_entity("TestEntity") + Report.result(Tests.create_test_entity, test_entity.id.IsValid()) + + # 3) Start the Tracer to catch any errors and warnings + with Tracer() as section_tracer: + # 4) Add the Axis Aligned Box Shape and Terrain Physics Heightfield Collider components + aaBoxShape_component = test_entity.add_component("Axis Aligned Box Shape") + Report.result(Tests.add_axis_aligned_box_shape, test_entity.has_component("Axis Aligned Box Shape")) + terrainPhysics_component = test_entity.add_component("Terrain Physics Heightfield Collider") + Report.result(Tests.add_terrain_collider, test_entity.has_component("Terrain Physics Heightfield Collider")) + + # 5) Change the Axis Aligned Box Shape dimensions + aaBoxShape_component.set_component_property_value("Axis Aligned Box Shape|Box Configuration|Dimensions", azmath.Vector3(SET_BOX_X_SIZE, SET_BOX_Y_SIZE, 1.0)) + add_check = aaBoxShape_component.get_component_property_value("Axis Aligned Box Shape|Box Configuration|Dimensions") == azmath.Vector3(SET_BOX_X_SIZE, SET_BOX_Y_SIZE, 1.0) + Report.result(Tests.box_dimensions_changed, add_check) + + # 6) Check the Heightfield provider is returning the correct size + columns = physics.HeightfieldProviderRequestsBus(bus.Broadcast, "GetHeightfieldGridColumns") + rows = physics.HeightfieldProviderRequestsBus(bus.Broadcast, "GetHeightfieldGridRows") + Report.result(Tests.configuration_changed, math.isclose(columns, EXPECTED_COLUMN_SIZE) and math.isclose(rows, EXPECTED_ROW_SIZE)) + + helper.wait_for_condition(lambda: section_tracer.has_errors or section_tracer.has_asserts, 1.0) + for error_info in section_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in section_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + +if __name__ == "__main__": + + from editor_python_test_tools.utils import Report + Report.start_test(TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges) diff --git a/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/Terrain_SupportsPhysics.py b/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/Terrain_SupportsPhysics.py new file mode 100644 index 0000000000..e68b0932c2 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Terrain/EditorScripts/Terrain_SupportsPhysics.py @@ -0,0 +1,164 @@ +""" +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 +""" + +#fmt: off +class Tests(): + create_terrain_spawner_entity = ("Terrain_spawner_entity created successfully", "Failed to create terrain_spawner_entity") + create_height_provider_entity = ("Height_provider_entity created successfully", "Failed to create height_provider_entity") + create_test_ball = ("Ball created successfully", "Failed to create Ball") + box_dimensions_changed = ("Aabb dimensions changed successfully", "Failed change Aabb dimensions") + shape_changed = ("Shape changed successfully", "Failed Shape change") + entity_added = ("Entity added successfully", "Failed Entity add") + frequency_changed = ("Frequency changed successfully", "Failed Frequency change") + shape_set = ("Shape set to Sphere successfully", "Failed to set Sphere shape") + test_collision = ("Ball collided with terrain", "Ball failed to collide with terrain") + no_errors_and_warnings_found = ("No errors and warnings found", "Found errors and warnings") +#fmt: on + +def Terrain_SupportsPhysics(): + """ + Summary: + Test aspects of the TerrainHeightGradientList through the BehaviorContext and the Property Tree. + + Test Steps: + Expected Behavior: + The Editor is stable there are no warnings or errors. + + Test Steps: + 1) Load the base level + 2) Create 2 test entities, one parent at 512.0, 512.0, 50.0 and one child at the default position and add the required components + 2a) Create a ball at 600.0, 600.0, 46.0 - This position is not too high over the heightfield so will collide in a reasonable time + 3) Start the Tracer to catch any errors and warnings + 4) Change the Axis Aligned Box Shape dimensions + 5) Set the Vegetation Shape reference to TestEntity1 + 6) Set the FastNoise gradient frequency to 0.01 + 7) Set the Gradient List to TestEntity2 + 8) Set the PhysX Collider to Sphere mode + 9) Disable and Enable the Terrain Gradient List so that it is recognised + 10) Enter game mode and test if the ball hits the heightfield within 3 seconds + 11) Verify there are no errors and warnings in the logs + + + :return: None + """ + + from editor_python_test_tools.editor_entity_utils import EditorEntity + from editor_python_test_tools.utils import TestHelper as helper, Report + from editor_python_test_tools.utils import Report, Tracer + import editor_python_test_tools.hydra_editor_utils as hydra + import azlmbr.math as azmath + import azlmbr.legacy.general as general + import azlmbr.bus as bus + import azlmbr.editor as editor + import math + + SET_BOX_X_SIZE = 1024.0 + SET_BOX_Y_SIZE = 1024.0 + SET_BOX_Z_SIZE = 100.0 + + helper.init_idle() + + # 1) Load the level + helper.open_level("", "Base") + helper.wait_for_condition(lambda: general.get_current_level_name() == "Base", 2.0) + + #1a) Load the level components + hydra.add_level_component("Terrain World") + hydra.add_level_component("Terrain World Renderer") + + # 2) Create 2 test entities, one parent at 512.0, 512.0, 50.0 and one child at the default position and add the required components + entity1_components_to_add = ["Axis Aligned Box Shape", "Terrain Layer Spawner", "Terrain Height Gradient List", "Terrain Physics Heightfield Collider", "PhysX Heightfield Collider"] + entity2_components_to_add = ["Vegetation Reference Shape", "Gradient Transform Modifier", "FastNoise Gradient"] + ball_components_to_add = ["Sphere Shape", "PhysX Collider", "PhysX Rigid Body"] + terrain_spawner_entity = hydra.Entity("TestEntity1") + terrain_spawner_entity.create_entity(azmath.Vector3(512.0, 512.0, 50.0), entity1_components_to_add) + Report.result(Tests.create_terrain_spawner_entity, terrain_spawner_entity.id.IsValid()) + height_provider_entity = hydra.Entity("TestEntity2") + height_provider_entity.create_entity(azmath.Vector3(0.0, 0.0, 0.0), entity2_components_to_add,terrain_spawner_entity.id) + Report.result(Tests.create_height_provider_entity, height_provider_entity.id.IsValid()) + # 2a) Create a ball at 600.0, 600.0, 46.0 - This position is not too high over the heightfield so will collide in a reasonable time + ball = hydra.Entity("Ball") + ball.create_entity(azmath.Vector3(600.0, 600.0, 46.0), ball_components_to_add) + Report.result(Tests.create_test_ball, ball.id.IsValid()) + # Give everything a chance to finish initializing. + general.idle_wait_frames(1) + + # 3) Start the Tracer to catch any errors and warnings + with Tracer() as section_tracer: + # 4) Change the Axis Aligned Box Shape dimensions + box_dimensions = azmath.Vector3(SET_BOX_X_SIZE, SET_BOX_Y_SIZE, SET_BOX_Z_SIZE) + terrain_spawner_entity.get_set_test(0, "Axis Aligned Box Shape|Box Configuration|Dimensions", box_dimensions) + box_shape_dimensions = hydra.get_component_property_value(terrain_spawner_entity.components[0], "Axis Aligned Box Shape|Box Configuration|Dimensions") + Report.result(Tests.box_dimensions_changed, box_dimensions == box_shape_dimensions) + + # 5) Set the Vegetaion Shape reference to TestEntity1 + height_provider_entity.get_set_test(0, "Configuration|Shape Entity Id", terrain_spawner_entity.id) + entityId = hydra.get_component_property_value(height_provider_entity.components[0], "Configuration|Shape Entity Id") + Report.result(Tests.shape_changed, entityId == terrain_spawner_entity.id) + + # 6) Set the FastNoise Gradient frequency to 0.01 + Frequency = 0.01 + height_provider_entity.get_set_test(2, "Configuration|Frequency", Frequency) + FrequencyVal = hydra.get_component_property_value(height_provider_entity.components[2], "Configuration|Frequency") + Report.result(Tests.frequency_changed, math.isclose(Frequency, FrequencyVal, abs_tol = 0.00001)) + + # 7) Set the Gradient List to TestEntity2 + propertyTree = hydra.get_property_tree(terrain_spawner_entity.components[2]) + propertyTree.add_container_item("Configuration|Gradient Entities", 0, height_provider_entity.id) + checkID = propertyTree.get_container_item("Configuration|Gradient Entities", 0) + Report.result(Tests.entity_added, checkID.GetValue() == height_provider_entity.id) + + # 8) Set the PhysX Collider to Sphere mode + shape = 0 + hydra.get_set_test(ball, 1, "Shape Configuration|Shape", shape) + setShape = hydra.get_component_property_value(ball.components[1], "Shape Configuration|Shape") + Report.result(Tests.shape_set, shape == setShape) + + # 9) Disable and Enable the Terrain Gradient List so that it is recognised + editor.EditorComponentAPIBus(bus.Broadcast, 'EnableComponents', [terrain_spawner_entity.components[2]]) + + general.enter_game_mode() + + general.idle_wait_frames(1) + + # 10) Enter game mode and test if the ball hits the heightfield within 3 seconds + TIMEOUT = 3.0 + + class Collider: + id = general.find_game_entity("Ball") + touched_ground = False + + terrain_id = general.find_game_entity("TestEntity1") + + def on_collision_begin(args): + other_id = args[0] + if other_id.Equal(terrain_id): + Report.info("Touched ground") + Collider.touched_ground = True + + handler = azlmbr.physics.CollisionNotificationBusHandler() + handler.connect(Collider.id) + handler.add_callback("OnCollisionBegin", on_collision_begin) + + helper.wait_for_condition(lambda: Collider.touched_ground, TIMEOUT) + Report.result(Tests.test_collision, Collider.touched_ground) + + general.exit_game_mode() + + # 11) Verify there are no errors and warnings in the logs + helper.wait_for_condition(lambda: section_tracer.has_errors or section_tracer.has_asserts, 1.0) + for error_info in section_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in section_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + + from editor_python_test_tools.utils import Report + Report.start_test(Terrain_SupportsPhysics) + diff --git a/AutomatedTesting/Gem/PythonTests/Terrain/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/Terrain/TestSuite_Main.py new file mode 100644 index 0000000000..786651713c --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Terrain/TestSuite_Main.py @@ -0,0 +1,29 @@ +""" +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 + +""" + +# This suite consists of all test cases that are passing and have been verified. + +import pytest +import os +import sys + +from ly_test_tools import LAUNCHERS +from ly_test_tools.o3de.editor_test import EditorTestSuite, EditorSharedTest + +@pytest.mark.SUITE_main +@pytest.mark.parametrize("launcher_platform", ['windows_editor']) +@pytest.mark.parametrize("project", ["AutomatedTesting"]) +class TestAutomation(EditorTestSuite): + + enable_prefab_system = False + + class test_AxisAlignedBoxShape_ConfigurationWorks(EditorSharedTest): + from .EditorScripts import TerrainPhysicsCollider_ChangesSizeWithAxisAlignedBoxShapeChanges as test_module + + class test_Terrain_SupportsPhysics(EditorSharedTest): + from .EditorScripts import Terrain_SupportsPhysics as test_module diff --git a/AutomatedTesting/Gem/PythonTests/Terrain/__init__.py b/AutomatedTesting/Gem/PythonTests/Terrain/__init__.py new file mode 100644 index 0000000000..f5193b300e --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/Terrain/__init__.py @@ -0,0 +1,6 @@ +""" +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 +""" diff --git a/AutomatedTesting/Gem/PythonTests/WhiteBox/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/WhiteBox/TestSuite_Main.py index fa6c81c98a..2fc717e256 100644 --- a/AutomatedTesting/Gem/PythonTests/WhiteBox/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/WhiteBox/TestSuite_Main.py @@ -24,12 +24,12 @@ from base import TestAutomationBase class TestAutomation(TestAutomationBase): def test_WhiteBox_AddComponentToEntity(self, request, workspace, editor, launcher_platform): from .tests import WhiteBox_AddComponentToEntity as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_WhiteBox_SetDefaultShape(self, request, workspace, editor, launcher_platform): from .tests import WhiteBox_SetDefaultShape as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_WhiteBox_SetInvisible(self, request, workspace, editor, launcher_platform): from .tests import WhiteBox_SetInvisible as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/assetpipeline/ap_fixtures/bundler_batch_setup_fixture.py b/AutomatedTesting/Gem/PythonTests/assetpipeline/ap_fixtures/bundler_batch_setup_fixture.py index ff362c732c..9281d5947e 100755 --- a/AutomatedTesting/Gem/PythonTests/assetpipeline/ap_fixtures/bundler_batch_setup_fixture.py +++ b/AutomatedTesting/Gem/PythonTests/assetpipeline/ap_fixtures/bundler_batch_setup_fixture.py @@ -158,7 +158,7 @@ def bundler_batch_setup_fixture(request, workspace, asset_processor, timeout) -> else: cmd.append(f"--{key}") if append_defaults: - cmd.append(f"--project-path={workspace.project}") + cmd.append(f"--project-path={workspace.paths.project()}") return cmd # ****** diff --git a/AutomatedTesting/Gem/PythonTests/assetpipeline/asset_processor_tests/asset_bundler_batch_tests.py b/AutomatedTesting/Gem/PythonTests/assetpipeline/asset_processor_tests/asset_bundler_batch_tests.py index 7f85e5e317..f5e5642573 100755 --- a/AutomatedTesting/Gem/PythonTests/assetpipeline/asset_processor_tests/asset_bundler_batch_tests.py +++ b/AutomatedTesting/Gem/PythonTests/assetpipeline/asset_processor_tests/asset_bundler_batch_tests.py @@ -88,214 +88,6 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): bundler_batch_helper.call_bundles(help="") bundler_batch_helper.call_bundleSeed(help="") - @pytest.mark.BAT - @pytest.mark.assetpipeline - @pytest.mark.test_case_id("C16877175") - @pytest.mark.skip("'animations/animationeditorfiles/sample1.animgraph' missing, needs investigation") - def test_WindowsAndMac_CreateAssetList_DependenciesCorrect(self, workspace, bundler_batch_helper): - r""" - Tests that an asset list created maps dependencies correctly. - testdependencieslevel\level.pak and lists of known dependencies are used for validation - - Test Steps: - 1. Create an asset list from the level.pak - 2. Create Lists of expected assets in the level.pak - 3. Add lists of expected assets to a single list - 4. Compare list of expected assets to actual assets - """ - helper = bundler_batch_helper - - # Create the asset list file - helper.call_assetLists( - addSeed=r"levels\testdependencieslevel\level.pak", - assetListFile=helper['asset_info_file_request'] - ) - - assert os.path.isfile(helper["asset_info_file_result"]) - - # Lists of known relative locations of assets - default_level_assets = [ - "engineassets/texturemsg/defaultnouvs.dds", - "engineassets/texturemsg/defaultnouvs.dds.1", - "engineassets/texturemsg/defaultnouvs.dds.2", - "engineassets/texturemsg/defaultnouvs.dds.3", - "engineassets/texturemsg/defaultnouvs.dds.4", - "engineassets/texturemsg/defaultnouvs.dds.5", - "engineassets/texturemsg/defaultnouvs.dds.6", - "engineassets/texturemsg/defaultnouvs.dds.7", - "engineassets/texturemsg/defaultnouvs_ddn.dds", - "engineassets/texturemsg/defaultnouvs_ddn.dds.1", - "engineassets/texturemsg/defaultnouvs_ddn.dds.2", - "engineassets/texturemsg/defaultnouvs_ddn.dds.3", - "engineassets/texturemsg/defaultnouvs_ddn.dds.4", - "engineassets/texturemsg/defaultnouvs_ddn.dds.5", - "engineassets/texturemsg/defaultnouvs_spec.dds", - "engineassets/texturemsg/defaultnouvs_spec.dds.1", - "engineassets/texturemsg/defaultnouvs_spec.dds.2", - "engineassets/texturemsg/defaultnouvs_spec.dds.3", - "engineassets/texturemsg/defaultnouvs_spec.dds.4", - "engineassets/texturemsg/defaultnouvs_spec.dds.5", - "engineassets/textures/defaults/16_grey.dds", - "engineassets/textures/cubemap/default_level_cubemap.dds", - "engineassets/textures/cubemap/default_level_cubemap.dds.1", - "engineassets/textures/cubemap/default_level_cubemap.dds.2", - "engineassets/textures/cubemap/default_level_cubemap.dds.3", - "engineassets/textures/cubemap/default_level_cubemap.dds.4", - "engineassets/textures/cubemap/default_level_cubemap_diff.dds", - "engineassets/materials/water/ocean_default.mtl", - "engineassets/textures/defaults/spot_default.dds", - "engineassets/textures/defaults/spot_default.dds.1", - "engineassets/textures/defaults/spot_default.dds.2", - "engineassets/textures/defaults/spot_default.dds.3", - "engineassets/textures/defaults/spot_default.dds.4", - "engineassets/textures/defaults/spot_default.dds.5", - "materials/material_terrain_default.mtl", - "textures/skys/night/half_moon.dds", - "textures/skys/night/half_moon.dds.1", - "textures/skys/night/half_moon.dds.2", - "textures/skys/night/half_moon.dds.3", - "textures/skys/night/half_moon.dds.4", - "textures/skys/night/half_moon.dds.5", - "textures/skys/night/half_moon.dds.6", - "engineassets/materials/sky/sky.mtl", - "levels/testdependencieslevel/level.pak", - "levels/testdependencieslevel/terrain/cover.ctc", - "levels/testdependencieslevel/terraintexture.pak", - ] - - sequence_material_cube_assets = [ - "textures/test_texture_sequence/test_texture_sequence000.dds", - "textures/test_texture_sequence/test_texture_sequence001.dds", - "textures/test_texture_sequence/test_texture_sequence002.dds", - "textures/test_texture_sequence/test_texture_sequence003.dds", - "textures/test_texture_sequence/test_texture_sequence004.dds", - "textures/test_texture_sequence/test_texture_sequence005.dds", - "objects/_primitives/_box_1x1.cgf", - "materials/test_texture_sequence.mtl", - "objects/_primitives/_box_1x1.mtl", - "textures/_primitives/middle_gray_checker.dds", - "textures/_primitives/middle_gray_checker.dds.1", - "textures/_primitives/middle_gray_checker.dds.2", - "textures/_primitives/middle_gray_checker.dds.3", - "textures/_primitives/middle_gray_checker.dds.4", - "textures/_primitives/middle_gray_checker.dds.5", - "textures/_primitives/middle_gray_checker_ddn.dds", - "textures/_primitives/middle_gray_checker_ddn.dds.1", - "textures/_primitives/middle_gray_checker_ddn.dds.2", - "textures/_primitives/middle_gray_checker_ddn.dds.3", - "textures/_primitives/middle_gray_checker_ddn.dds.4", - "textures/_primitives/middle_gray_checker_ddn.dds.5", - "textures/_primitives/middle_gray_checker_spec.dds", - "textures/_primitives/middle_gray_checker_spec.dds.1", - "textures/_primitives/middle_gray_checker_spec.dds.2", - "textures/_primitives/middle_gray_checker_spec.dds.3", - "textures/_primitives/middle_gray_checker_spec.dds.4", - "textures/_primitives/middle_gray_checker_spec.dds.5", - ] - - character_with_simplified_material_assets = [ - "objects/characters/jack/jack.actor", - "objects/characters/jack/jack.mtl", - "objects/characters/jack/textures/jack_diff.dds", - "objects/characters/jack/textures/jack_diff.dds.1", - "objects/characters/jack/textures/jack_diff.dds.2", - "objects/characters/jack/textures/jack_diff.dds.3", - "objects/characters/jack/textures/jack_diff.dds.4", - "objects/characters/jack/textures/jack_diff.dds.5", - "objects/characters/jack/textures/jack_diff.dds.6", - "objects/characters/jack/textures/jack_diff.dds.7", - "objects/characters/jack/textures/jack_spec.dds", - "objects/characters/jack/textures/jack_spec.dds.1", - "objects/characters/jack/textures/jack_spec.dds.2", - "objects/characters/jack/textures/jack_spec.dds.3", - "objects/characters/jack/textures/jack_spec.dds.4", - "objects/characters/jack/textures/jack_spec.dds.5", - "objects/characters/jack/textures/jack_spec.dds.6", - "objects/characters/jack/textures/jack_spec.dds.7", - "objects/default/editorprimitive.mtl", - "engineassets/textures/grey.dds", - "animations/animationeditorfiles/sample0.animgraph", - "animations/motions/jack_death_fall_back_zup.motion", - "animations/animationeditorfiles/sample1.animgraph", - "animations/animationeditorfiles/sample0.motionset", - "animations/motions/rin_jump.motion", - "animations/animationeditorfiles/sample1.motionset", - "animations/motions/rin_idle.motion", - "animations/motions/jack_idle_aim_zup.motion", - ] - - spawner_assets = [ - "slices/sphere.dynamicslice", - "objects/default/primitive_sphere.cgf", - "test1.luac", - "test2.luac", - ] - - ui_canvas_assets = [ - "fonts/vera.ttf", - "fonts/vera.font", - "scriptcanvas/mainmenu.scriptcanvas_compiled", - "fonts/vera.fontfamily", - "ui/canvas/start.uicanvas", - "fonts/vera-italic.font", - "ui/textureatlas/sample.texatlasidx", - "fonts/vera-bold-italic.ttf", - "fonts/vera-bold.font", - "ui/textures/prefab/button_normal.dds", - "ui/textures/prefab/button_normal.sprite", - "fonts/vera-italic.ttf", - "ui/textureatlas/sample.dds", - "fonts/vera-bold-italic.font", - "fonts/vera-bold.ttf", - "ui/textures/prefab/button_disabled.dds", - "ui/textures/prefab/button_disabled.sprite", - ] - - wwise_and_atl_assets = [ - "libs/gameaudio/wwise/levels/testdependencieslevel/test_dependencies_level.xml", - "sounds/wwise/test_bank3.bnk", - "sounds/wwise/test_bank4.bnk", - "sounds/wwise/test_bank5.bnk", - "sounds/wwise/test_bank1.bnk", - "sounds/wwise/init.bnk", - "sounds/wwise/499820003.wem", - "sounds/wwise/196049145.wem", - ] - - particle_library_assets = [ - "libs/particles/milestone2particles.xml", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds.1", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds.2", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds.3", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds.4", - "textures/milestone2/particles/fx_launchermuzzlering_01.dds.5", - "textures/milestone2/particles/fx_sparkstreak_01.dds", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds.1", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds.2", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds.3", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds.4", - "textures/milestone2/particles/fx_launchermuzzlefront_01.dds.5", - ] - - lens_flares_library_assets = ["libs/flares/flares.xml", "textures/lights/flare01.dds"] - - expected_assets_list = default_level_assets - expected_assets_list.extend(sequence_material_cube_assets) - expected_assets_list.extend(character_with_simplified_material_assets) - expected_assets_list.extend(spawner_assets) - expected_assets_list.extend(ui_canvas_assets) - expected_assets_list.extend(wwise_and_atl_assets) - expected_assets_list.extend(particle_library_assets) - expected_assets_list.extend(lens_flares_library_assets) # All expected assets - - # Get actual calculated dependencies from the asset list created - actual_assets_list = [] - for rel_path in helper.get_asset_relative_paths(helper["asset_info_file_result"]): - actual_assets_list.append(rel_path) - - assert sorted(actual_assets_list) == sorted(expected_assets_list) @pytest.mark.BAT @pytest.mark.assetpipeline @@ -310,9 +102,9 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): 3. Read and store contents of asset list into memory 4. Attempt to create a new asset list in without using --allowOverwrites 5. Verify that Asset Bundler returns false - 6. Verify that file contents of the orignally created asset list did not change from what was stored in memory + 6. Verify that file contents of the originally created asset list did not change from what was stored in memory 7. Attempt to create a new asset list without debug while allowing overwrites - 8. Verify that file contents of the orignally created asset list changed from what was stored in memory + 8. Verify that file contents of the originally created asset list changed from what was stored in memory """ helper = bundler_batch_helper seed_list = os.path.join(workspace.paths.engine_root(), "Assets", "Engine", "SeedAssetList.seed") # Engine seed list @@ -919,7 +711,7 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): # Extra arguments for pattern comparison cmd.extend([f"--filePatternType={pattern_type}", f"--filePattern={pattern}"]) if workspace.project: - cmd.append(f'--project-path={project_name}') + cmd.append(f'--project-path={workspace.paths.project()}') return cmd # End generate_compare_command() @@ -960,7 +752,7 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): output_mac_asset_list = helper.platform_file_name(last_output_arg, platform) # Build execution command - cmd = generate_compare_command(platform_arg, workspace.project) + cmd = generate_compare_command(platform_arg, workspace.paths.project()) # Execute command subprocess.check_call(cmd) @@ -995,7 +787,7 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): f"--comparisonRulesFile={rule_file}", f"--comparisonType={args[1]}", r"--addComparison", - f"--project-path={workspace.project}", + f"--project-path={workspace.paths.project()}", ] if args[1] == "4": # If pattern comparison, append a few extra arguments @@ -1117,7 +909,7 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): "--addDefaultSeedListFiles", "--platform=pc", "--print", - f"--project-path={workspace.project}" + f"--project-path={workspace.paths.project()}" ], universal_newlines=True, ) @@ -1189,7 +981,7 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): # Make sure file gets deleted on teardown request.addfinalizer(lambda: fs.delete([bundle_result_path], True, False)) - bundles_folder = os.path.join(workspace.paths.engine_root(), workspace.project, "Bundles") + bundles_folder = os.path.join(workspace.paths.project(), "Bundles") level_pak = r"levels\testdependencieslevel\level.pak" bundle_request_path = os.path.join(bundles_folder, "bundle.pak") bundle_result_path = os.path.join(bundles_folder, @@ -1243,23 +1035,64 @@ class TestsAssetBundlerBatch_WindowsAndMac(object): 2. Verify file was created 3. Verify that only the expected assets are present in the created asset list """ - expected_assets = [ + expected_assets = sorted([ "ui/canvases/lyshineexamples/animation/multiplesequences.uicanvas", - "ui/textures/prefab/button_normal.sprite" - ] + "ui/textures/prefab/button_disabled.tif.streamingimage", + "ui/textures/prefab/tooltip_sliced.tif.streamingimage", + "ui/textures/prefab/button_normal.tif.streamingimage" + ]) + # Printing these lists out can save a step in debugging if this test fails on Jenkins. + logger.info(f"expected_assets: {expected_assets}") + + skip_assets = sorted([ + "ui/scripts/lyshineexamples/animation/multiplesequences.luac", + "ui/scripts/lyshineexamples/unloadthiscanvasbutton.luac", + "fonts/vera.fontfamily", + "fonts/vera-italic.font", + "fonts/vera.font", + "fonts/vera-bold.font", + "fonts/vera-bold-italic.font", + "fonts/vera-italic.ttf", + "fonts/vera.ttf", + "fonts/vera-bold.ttf", + "fonts/vera-bold-italic.ttf" + ]) + logger.info(f"skip_assets: {skip_assets}") + + expected_and_skip_assets = sorted(expected_assets + skip_assets) + # Printing both together to make it quick to compare the results in the logs for a test failure on Jenkins + logger.info(f"expected_and_skip_assets: {expected_and_skip_assets}") + + # First, generate an asset info file without skipping, to get a list that can be used as a baseline to verify + # the files were actually skipped, and not just missing. + bundler_batch_helper.call_assetLists( + assetListFile=bundler_batch_helper['asset_info_file_request'], + addSeed="ui/canvases/lyshineexamples/animation/multiplesequences.uicanvas" + ) + assert os.path.isfile(bundler_batch_helper["asset_info_file_result"]) + assets_in_no_skip_list = [] + for rel_path in bundler_batch_helper.get_asset_relative_paths(bundler_batch_helper["asset_info_file_result"]): + assets_in_no_skip_list.append(rel_path) + assets_in_no_skip_list = sorted(assets_in_no_skip_list) + logger.info(f"assets_in_no_skip_list: {assets_in_no_skip_list}") + assert assets_in_no_skip_list == expected_and_skip_assets + + # Now generate an asset info file using the skip command, and verify the skip files are not in the list. bundler_batch_helper.call_assetLists( assetListFile=bundler_batch_helper['asset_info_file_request'], addSeed="ui/canvases/lyshineexamples/animation/multiplesequences.uicanvas", - skip="ui/textures/prefab/button_disabled.sprite,ui/scripts/lyshineexamples/animation/multiplesequences.luac," - "ui/textures/prefab/tooltip_sliced.sprite,ui/scripts/lyshineexamples/unloadthiscanvasbutton.luac,fonts/vera.fontfamily,fonts/vera-italic.font," - "fonts/vera.font,fonts/vera-bold.font,fonts/vera-bold-italic.font,fonts/vera-italic.ttf,fonts/vera.ttf,fonts/vera-bold.ttf,fonts/vera-bold-italic.ttf" + allowOverwrites="", + skip=','.join(skip_assets) ) + assert os.path.isfile(bundler_batch_helper["asset_info_file_result"]) assets_in_list = [] for rel_path in bundler_batch_helper.get_asset_relative_paths(bundler_batch_helper["asset_info_file_result"]): assets_in_list.append(rel_path) + assets_in_list = sorted(assets_in_list) + logger.info(f"assets_in_list: {assets_in_list}") + assert assets_in_list == expected_assets - assert sorted(assets_in_list) == sorted(expected_assets) @pytest.mark.BAT @pytest.mark.assetpipeline diff --git a/AutomatedTesting/Gem/PythonTests/automatedtesting_shared/base.py b/AutomatedTesting/Gem/PythonTests/automatedtesting_shared/base.py index cbb6102a44..4543888a7c 100755 --- a/AutomatedTesting/Gem/PythonTests/automatedtesting_shared/base.py +++ b/AutomatedTesting/Gem/PythonTests/automatedtesting_shared/base.py @@ -52,7 +52,7 @@ class TestAutomationBase: cls._kill_ly_processes() def _run_test(self, request, workspace, editor, testcase_module, extra_cmdline_args=[], batch_mode=True, - autotest_mode=True, use_null_renderer=True): + autotest_mode=True, use_null_renderer=True, enable_prefab_system=True): test_starttime = time.time() self.logger = logging.getLogger(__name__) errors = [] @@ -97,6 +97,11 @@ class TestAutomationBase: pycmd += ["-BatchMode"] if autotest_mode: pycmd += ["-autotest_mode"] + if enable_prefab_system: + pycmd += ["--regset=/Amazon/Preferences/EnablePrefabSystem=true"] + else: + pycmd += ["--regset=/Amazon/Preferences/EnablePrefabSystem=false"] + pycmd += extra_cmdline_args editor.args.extend(pycmd) # args are added to the WinLauncher start command editor.start(backupFiles = False, launch_ap = False) @@ -165,7 +170,7 @@ class TestAutomationBase: for line in f.readlines(): error_str += f"|{log_basename}| {line}" except Exception as ex: - error_str += "-- No log available --" + error_str += f"-- No log available ({ex})--" pytest.fail(error_str) diff --git a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_SearchFiltering.py b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_SearchFiltering.py index 33c48c7a77..7366faafdc 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_SearchFiltering.py +++ b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_SearchFiltering.py @@ -62,7 +62,7 @@ def AssetBrowser_SearchFiltering(): from editor_python_test_tools.utils import Report from editor_python_test_tools.utils import TestHelper as helper - def verify_files_appeared(model, allowed_asset_extentions, parent_index=QtCore.QModelIndex()): + def verify_files_appeared(model, allowed_asset_extensions, parent_index=QtCore.QModelIndex()): indexes = [parent_index] while len(indexes) > 0: parent_index = indexes.pop(0) @@ -71,7 +71,7 @@ def AssetBrowser_SearchFiltering(): cur_data = cur_index.data(Qt.DisplayRole) if ( "." in cur_data - and (cur_data.lower().split(".")[-1] not in allowed_asset_extentions) + and (cur_data.lower().split(".")[-1] not in allowed_asset_extensions) and not cur_data[-1] == ")" ): Report.info(f"Incorrect file found: {cur_data}") @@ -94,16 +94,21 @@ def AssetBrowser_SearchFiltering(): Report.info("Asset Browser is already open") editor_window = pyside_utils.get_editor_main_window() app = QtWidgets.QApplication.instance() - - # 3) Type the name of an asset in the search bar and make sure only one asset is filtered in Asset browser + + # 3) Type the name of an asset in the search bar and make sure it is filtered to and selectable asset_browser = editor_window.findChild(QtWidgets.QDockWidget, "Asset Browser") search_bar = asset_browser.findChild(QtWidgets.QLineEdit, "textSearch") search_bar.setText("cedar.fbx") asset_browser_tree = asset_browser.findChild(QtWidgets.QTreeView, "m_assetBrowserTreeViewWidget") - model_index = pyside_utils.find_child_by_pattern(asset_browser_tree, "cedar.fbx") - pyside_utils.item_view_index_mouse_click(asset_browser_tree, model_index) + asset_browser_table = asset_browser.findChild(QtWidgets.QTreeView, "m_assetBrowserTableViewWidget") + found = await pyside_utils.wait_for_condition(lambda: pyside_utils.find_child_by_pattern(asset_browser_table, "cedar.fbx"), 5.0) + if found: + model_index = pyside_utils.find_child_by_pattern(asset_browser_table, "cedar.fbx") + else: + Report.result(Tests.asset_filtered, found) + pyside_utils.item_view_index_mouse_click(asset_browser_table, model_index) is_filtered = await pyside_utils.wait_for_condition( - lambda: asset_browser_tree.indexBelow(asset_browser_tree.currentIndex()) == QtCore.QModelIndex(), 5.0) + lambda: asset_browser_table.currentIndex() == model_index, 5.0) Report.result(Tests.asset_filtered, is_filtered) # 4) Click the "X" in the search bar. diff --git a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_TreeNavigation.py b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_TreeNavigation.py index b4f0dc7f6c..ecc77778cc 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_TreeNavigation.py +++ b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/AssetBrowser_TreeNavigation.py @@ -84,8 +84,8 @@ def AssetBrowser_TreeNavigation(): # 3) Collapse all files initially main_window = editor_window.findChild(QtWidgets.QMainWindow) - asset_browser = pyside_utils.find_child_by_hierarchy(main_window, ..., "Asset Browser") - tree = pyside_utils.find_child_by_hierarchy(asset_browser, ..., "m_assetBrowserTreeViewWidget") + asset_browser = pyside_utils.find_child_by_pattern(main_window, text="Asset Browser", type=QtWidgets.QDockWidget) + tree = pyside_utils.find_child_by_pattern(asset_browser, "m_assetBrowserTreeViewWidget") scroll_area = tree.findChild(QtWidgets.QWidget, "qt_scrollarea_vcontainer") scroll_bar = scroll_area.findChild(QtWidgets.QScrollBar) tree.collapseAll() diff --git a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/Docking_BasicDockedTools.py b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/Docking_BasicDockedTools.py index 2a91e7a374..6683fc952a 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/Docking_BasicDockedTools.py +++ b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/Docking_BasicDockedTools.py @@ -140,13 +140,13 @@ def Docking_BasicDockedTools(): # 2.5,6) Send a console command. console_line_edit = console.findChild(QtWidgets.QLineEdit, "lineEdit") - console_line_edit.setText("t_Scale 2") + console_line_edit.setText("t_simulationTickScale 2") QtTest.QTest.keyClick(console_line_edit, QtCore.Qt.Key_Enter) - general.get_cvar("t_Scale") - Report.result(Tests.docked_console_works, general.get_cvar("t_Scale") == "2") + general.get_cvar("t_simulationTickScale") + Report.result(Tests.docked_console_works, general.get_cvar("t_simulationTickScale") == "2") # Reset the altered cvar - console_line_edit.setText("t_Scale 1") + console_line_edit.setText("t_simulationTickScale 1") QtTest.QTest.keyClick(console_line_edit, QtCore.Qt.Key_Enter) run_test() diff --git a/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/EntityOutliner_EntityOrdering.py b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/EntityOutliner_EntityOrdering.py new file mode 100644 index 0000000000..5fa8130302 --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/editor/EditorScripts/EntityOutliner_EntityOrdering.py @@ -0,0 +1,147 @@ +""" +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 +""" + + +class Tests: + entities_sorted = ( + "Entities sorted in the expected order", + "Entities sorted in an incorrect order", + ) + + +def EntityOutliner_EntityOrdering(): + """ + Summary: + Verify that manual entity ordering in the entity outliner works and is stable. + + Expected Behavior: + Several entities are created, some are manually ordered, and their order + is maintained, even when new entities are added. + + Test Steps: + 1) Open the empty Prefab Base level + 2) Add 5 entities to the outliner + 3) Move "Entity1" to the top of the order + 4) Move "Entity4" to the bottom of the order + 5) Add another new entity, ensure the rest of the order is unchanged + """ + + import editor_python_test_tools.pyside_utils as pyside_utils + import azlmbr.legacy.general as general + from editor_python_test_tools.utils import Report + from editor_python_test_tools.utils import TestHelper as helper + from PySide2 import QtCore, QtWidgets, QtGui, QtTest + + # Grab the Editor, Entity Outliner, and Outliner Model + editor_window = pyside_utils.get_editor_main_window() + entity_outliner = pyside_utils.find_child_by_hierarchy( + editor_window, ..., "EntityOutlinerWidgetUI", ..., "m_objectTree" + ) + entity_outliner_model = entity_outliner.model() + + # Get the outliner index for the root prefab container entity + def get_root_prefab_container_index(): + return entity_outliner_model.index(0, 0) + + # Get the outliner index for the top level entity of a given name + def index_for_name(name): + root_index = get_root_prefab_container_index() + for row in range(entity_outliner_model.rowCount(root_index)): + row_index = entity_outliner_model.index(row, 0, root_index) + if row_index.data() == name: + return row_index + return None + + # Validate that the outliner top level entity order matches the expected order + def verify_entities_sorted(expected_order): + actual_order = [] + root_index = get_root_prefab_container_index() + for row in range(entity_outliner_model.rowCount(root_index)): + row_index = entity_outliner_model.index(row, 0, root_index) + actual_order.append(row_index.data()) + + sorted_correctly = actual_order == expected_order + Report.result(Tests.entities_sorted, sorted_correctly) + if not sorted_correctly: + print(f"Expected entity order: {expected_order}") + print(f"Actual entity order: {actual_order}") + + # Creates an entity from the outliner context menu + def create_entity(): + pyside_utils.trigger_context_menu_entry( + entity_outliner, "Create entity", index=get_root_prefab_container_index() + ) + # Wait a tick after entity creation to let events process + general.idle_wait(0.0) + + # Moves an entity (wrapped by move_entity_before and move_entity_after) + def _move_entity(source_name, target_name, move_after=False): + source_index = index_for_name(source_name) + target_index = index_for_name(target_name) + + target_row = target_index.row() + if move_after: + target_row += 1 + + # Generate MIME data and directly inject it into the model instead of + # generating mouse click operations, as it's more reliable and we're + # testing the underlying drag & drop logic as opposed to Qt's mouse + # handling here + mime_data = entity_outliner_model.mimeData([source_index]) + entity_outliner_model.dropMimeData( + mime_data, QtCore.Qt.MoveAction, target_row, 0, target_index.parent() + ) + # Wait after move to let events (i.e. prefab propagation) process + general.idle_wait(1.0) + + # Move an entity before another entity in the order by dragging the source above the target + move_entity_before = lambda source_name, target_name: _move_entity( + source_name, target_name, move_after=False + ) + # Move an entity after another entity in the order by dragging the source beloew the target + move_entity_after = lambda source_name, target_name: _move_entity( + source_name, target_name, move_after=True + ) + + expected_order = [] + + # 1) Open the empty Prefab Base level + helper.init_idle() + helper.open_level("Prefab", "Base") + + # 2) Add 5 entities to the outliner + ENTITIES_TO_ADD = 5 + for i in range(ENTITIES_TO_ADD): + create_entity() + + # Our new entity should be given a name with a number automatically + new_entity = f"Entity{i+1}" + # The new entity should be added to the bottom of its parent entity + expected_order = expected_order + [new_entity] + + verify_entities_sorted(expected_order) + + # 3) Move "Entity5" to the top of the order + move_entity_before("Entity5", "Entity1") + expected_order = ["Entity5", "Entity1", "Entity2", "Entity3", "Entity4"] + verify_entities_sorted(expected_order) + + # 4) Move "Entity2" to the bottom of the order + move_entity_after("Entity2", "Entity4") + expected_order = ["Entity5", "Entity1", "Entity3", "Entity4", "Entity2"] + verify_entities_sorted(expected_order) + + # 5) Add another new entity, ensure the rest of the order is unchanged + create_entity() + expected_order = ["Entity5", "Entity1", "Entity3", "Entity4", "Entity2", "Entity6"] + verify_entities_sorted(expected_order) + + +if __name__ == "__main__": + from editor_python_test_tools.utils import Report + + Report.start_test(EntityOutliner_EntityOrdering) diff --git a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main.py index 26b254ae71..c9e91687e0 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main.py @@ -33,11 +33,22 @@ class TestAutomation(TestAutomationBase): def test_BasicEditorWorkflows_LevelEntityComponentCRUD(self, request, workspace, editor, launcher_platform, remove_test_level): from .EditorScripts import BasicEditorWorkflows_LevelEntityComponentCRUD as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False, autotest_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, autotest_mode=False, enable_prefab_system=False) @pytest.mark.REQUIRES_gpu def test_BasicEditorWorkflows_GPU_LevelEntityComponentCRUD(self, request, workspace, editor, launcher_platform, remove_test_level): from .EditorScripts import BasicEditorWorkflows_LevelEntityComponentCRUD as test_module self._run_test(request, workspace, editor, test_module, batch_mode=False, autotest_mode=False, - use_null_renderer=False) + use_null_renderer=False, enable_prefab_system=False) + + def test_EntityOutlienr_EntityOrdering(self, request, workspace, editor, launcher_platform): + from .EditorScripts import EntityOutliner_EntityOrdering as test_module + self._run_test( + request, + workspace, + editor, + test_module, + batch_mode=False, + autotest_mode=True, + ) diff --git a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main_Optimized.py b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main_Optimized.py index afc52f962d..ccc4ee9737 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Main_Optimized.py @@ -21,6 +21,8 @@ class TestAutomationNoAutoTestMode(EditorTestSuite): # Disable -autotest_mode and -BatchMode. Tests cannot run in -BatchMode due to UI interactions, and these tests # interact with modal dialogs global_extra_cmdline_args = [] + + enable_prefab_system = False class test_BasicEditorWorkflows_LevelEntityComponentCRUD(EditorSingleTest): # Custom teardown to remove slice asset created during test @@ -43,7 +45,6 @@ class TestAutomationNoAutoTestMode(EditorTestSuite): class test_InputBindings_Add_Remove_Input_Events(EditorSharedTest): from .EditorScripts import InputBindings_Add_Remove_Input_Events as test_module - @pytest.mark.skip(reason="Crashes Editor: ATOM-15493") class test_AssetPicker_UI_UX(EditorSharedTest): from .EditorScripts import AssetPicker_UI_UX as test_module @@ -57,10 +58,11 @@ class TestAutomationAutoTestMode(EditorTestSuite): # Enable only -autotest_mode for these tests. Tests cannot run in -BatchMode due to UI interactions global_extra_cmdline_args = ["-autotest_mode"] + enable_prefab_system = False + class test_AssetBrowser_TreeNavigation(EditorSharedTest): from .EditorScripts import AssetBrowser_TreeNavigation as test_module - @pytest.mark.skip(reason="Crashes Editor: ATOM-15493") class test_AssetBrowser_SearchFiltering(EditorSharedTest): from .EditorScripts import AssetBrowser_SearchFiltering as test_module @@ -74,6 +76,5 @@ class TestAutomationAutoTestMode(EditorTestSuite): class test_Menus_FileMenuOptions_Work(EditorSharedTest): from .EditorScripts import Menus_FileMenuOptions as test_module - class test_BasicEditorWorkflows_ExistingLevel_EntityComponentCRUD(EditorSharedTest): from .EditorScripts import BasicEditorWorkflows_ExistingLevel_EntityComponentCRUD as test_module diff --git a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Periodic.py index 398b64bc87..1bd1d7f987 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Periodic.py @@ -32,31 +32,29 @@ class TestAutomation(TestAutomationBase): def test_AssetBrowser_TreeNavigation(self, request, workspace, editor, launcher_platform): from .EditorScripts import AssetBrowser_TreeNavigation as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) - @pytest.mark.skip(reason="Crashes Editor: ATOM-15493") def test_AssetBrowser_SearchFiltering(self, request, workspace, editor, launcher_platform): from .EditorScripts import AssetBrowser_SearchFiltering as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) - @pytest.mark.skip(reason="Crashes Editor: ATOM-15493") def test_AssetPicker_UI_UX(self, request, workspace, editor, launcher_platform): from .EditorScripts import AssetPicker_UI_UX as test_module - self._run_test(request, workspace, editor, test_module, autotest_mode=False, batch_mode=False) + self._run_test(request, workspace, editor, test_module, autotest_mode=False, batch_mode=False, enable_prefab_system=False) def test_ComponentCRUD_Add_Delete_Components(self, request, workspace, editor, launcher_platform): from .EditorScripts import ComponentCRUD_Add_Delete_Components as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) def test_InputBindings_Add_Remove_Input_Events(self, request, workspace, editor, launcher_platform): from .EditorScripts import InputBindings_Add_Remove_Input_Events as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False, autotest_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, autotest_mode=False, enable_prefab_system=False) def test_Menus_ViewMenuOptions_Work(self, request, workspace, editor, launcher_platform): from .EditorScripts import Menus_ViewMenuOptions as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) @pytest.mark.skip(reason="Times out due to dialogs failing to dismiss: LYN-4208") def test_Menus_FileMenuOptions_Work(self, request, workspace, editor, launcher_platform): from .EditorScripts import Menus_FileMenuOptions as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox.py b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox.py index 98a6620d9c..8a56a2dbfd 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox.py +++ b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox.py @@ -20,8 +20,8 @@ class TestAutomation(TestAutomationBase): def test_Menus_EditMenuOptions_Work(self, request, workspace, editor, launcher_platform): from .EditorScripts import Menus_EditMenuOptions as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) def test_Docking_BasicDockedTools(self, request, workspace, editor, launcher_platform): from .EditorScripts import Docking_BasicDockedTools as test_module - self._run_test(request, workspace, editor, test_module, batch_mode=False) + self._run_test(request, workspace, editor, test_module, batch_mode=False, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox_Optimized.py b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox_Optimized.py index 4a472095ae..ce0d5e43e9 100644 --- a/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/editor/TestSuite_Sandbox_Optimized.py @@ -19,6 +19,8 @@ class TestAutomationAutoTestMode(EditorTestSuite): # Enable only -autotest_mode for these tests. Tests cannot run in -BatchMode due to UI interactions global_extra_cmdline_args = ["-autotest_mode"] + enable_prefab_system = False + class test_Docking_BasicDockedTools(EditorSharedTest): from .EditorScripts import Docking_BasicDockedTools as test_module diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/CMakeLists.txt b/AutomatedTesting/Gem/PythonTests/largeworlds/CMakeLists.txt index c2123d683c..98801b49d6 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/CMakeLists.txt +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/CMakeLists.txt @@ -24,7 +24,6 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED AND PAL_TRAIT_BUILD_HOST_TOOLS AND PAL_TRAIT_ LargeWorlds ) - ly_add_pytest( NAME AutomatedTesting::DynamicVegetationTests_Periodic TEST_SERIAL @@ -39,6 +38,20 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED AND PAL_TRAIT_BUILD_HOST_TOOLS AND PAL_TRAIT_ LargeWorlds ) + ly_add_pytest( + NAME AutomatedTesting::DynamicVegetationTests_Periodic_Optimized + TEST_SERIAL + TEST_SUITE periodic + PATH ${CMAKE_CURRENT_LIST_DIR}/dyn_veg/TestSuite_Periodic_Optimized.py + RUNTIME_DEPENDENCIES + AZ::AssetProcessor + Legacy::Editor + AutomatedTesting.Assets + AutomatedTesting.GameLauncher + COMPONENT + LargeWorlds + ) + ly_add_pytest( NAME AutomatedTesting::DynamicVegetationTests_Main_Optimized TEST_SERIAL diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/EditorScripts/DynVegUtils_TempPrefabCreationWorks.py b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/EditorScripts/DynVegUtils_TempPrefabCreationWorks.py new file mode 100644 index 0000000000..d016473d5e --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/EditorScripts/DynVegUtils_TempPrefabCreationWorks.py @@ -0,0 +1,85 @@ +""" +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 +""" + + +def DynVegUtils_TempPrefabCreationWorks(): + """ + Summary: + An existing level is opened. Each Prefab setup to be spawned by Dynamic Vegetation tests is created in memory and + validated against existing test slice components/mesh assignments. + + Expected Behavior: + Temporary prefabs contain the expected components/assets. + + Test Steps: + 1) Open an existing level + 2) Create each of the necessary temporary Mesh prefabs, and validate the component/mesh setups + 3) Create the necessary temporary PhysX Collider, and validate the component setup + 4) Report errors/asserts + + :return: None + """ + + import os + + import azlmbr.asset as asset + import azlmbr.bus as bus + import azlmbr.math as math + + from Prefab.tests import PrefabTestUtils as prefab_test_utils + from largeworlds.large_worlds_utils import editor_dynveg_test_helper as dynveg + from editor_python_test_tools.utils import Report, Tracer + from editor_python_test_tools.utils import TestHelper as helper + from editor_python_test_tools.prefab_utils import PrefabInstance + + with Tracer() as error_tracer: + # Create dictionary for prefab filenames and paths to create using helper function + mesh_prefabs = { + "PinkFlower": os.path.join("assets", "objects", "foliage", "grass_flower_pink.azmodel"), + "PurpleFlower": os.path.join("assets", "objects", "foliage", "grass_flower_purple.azmodel"), + "1m_Cube": os.path.join("objects", "_primitives", "_box_1x1.azmodel"), + "CedarTree": os.path.join("assets", "objects", "foliage", "cedar.azmodel"), + "Bush": os.path.join("assets", "objects", "foliage", "bush_privet_01.azmodel"), + } + + # 1) Open an existing simple level + prefab_test_utils.open_base_tests_level() + + # 2) Create each of the Mesh asset prefabs and validate that the prefab created successfully + for prefab_filename, asset_path in mesh_prefabs.items(): + mesh_prefab_created = ( + f"Temporary mesh prefab: {prefab_filename} created successfully", + f"Failed to create temporary mesh prefab: {prefab_filename}" + ) + prefab = dynveg.create_temp_mesh_prefab(asset_path, prefab_filename) + Report.result(mesh_prefab_created, helper.wait_for_condition(lambda: + PrefabInstance.is_valid(prefab[1]), 3.0)) + + # 3) Create temp PhysX Collider prefab and validate that the prefab created successfully + physx_prefab_filename = "CedarTree_Collision" + physx_collider_prefab_created = ( + f"Temporary mesh prefab: {physx_prefab_filename} created successfully", + f"Failed to create temporary mesh prefab: {physx_prefab_filename}" + ) + test_physx_mesh_asset_id = asset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetIdByPath", os.path.join( + "assets", "objects", "foliage", "cedar.pxmesh"), math.Uuid(), False) + dynveg.create_temp_physx_mesh_collider(test_physx_mesh_asset_id, physx_prefab_filename) + Report.result(physx_collider_prefab_created, helper.wait_for_condition(lambda: + PrefabInstance.is_valid(prefab[1]), 3.0)) + + # 4) Report errors/asserts + helper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0) + for error_info in error_tracer.errors: + Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}") + for assert_info in error_tracer.asserts: + Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}") + + +if __name__ == "__main__": + + from editor_python_test_tools.utils import Report + Report.start_test(DynVegUtils_TempPrefabCreationWorks) diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main.py index 4c02c887ef..06c9c5f615 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main.py @@ -20,8 +20,8 @@ class TestAutomation(TestAutomationBase): def test_DynamicSliceInstanceSpawner_DynamicSliceSpawnerWorks(self, request, workspace, editor, launcher_platform): from .EditorScripts import DynamicSliceInstanceSpawner_DynamicSliceSpawnerWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_EmptyInstanceSpawner_EmptySpawnerWorks(self, request, workspace, editor, launcher_platform): from .EditorScripts import EmptyInstanceSpawner_EmptySpawnerWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main_Optimized.py b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main_Optimized.py index ded2dda4e9..ed23af51ad 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Main_Optimized.py @@ -18,6 +18,8 @@ from ly_test_tools.o3de.editor_test import EditorSingleTest, EditorSharedTest, E @pytest.mark.parametrize("project", ["AutomatedTesting"]) class TestAutomation(EditorTestSuite): + enable_prefab_system = False + class test_DynamicSliceInstanceSpawner_DynamicSliceSpawnerWorks(EditorParallelTest): from .EditorScripts import DynamicSliceInstanceSpawner_DynamicSliceSpawnerWorks as test_module diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic.py index 2780c0f471..d0d570950b 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic.py @@ -52,158 +52,158 @@ class TestAutomation(TestAutomationBase): def test_AltitudeFilter_ComponentAndOverrides_InstancesPlantAtSpecifiedAltitude(self, request, workspace, editor, launcher_platform): from .EditorScripts import AltitudeFilter_ComponentAndOverrides_InstancesPlantAtSpecifiedAltitude as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_AltitudeFilter_ShapeSample_InstancesPlantAtSpecifiedAltitude(self, request, workspace, editor, launcher_platform): from .EditorScripts import AltitudeFilter_ShapeSample_InstancesPlantAtSpecifiedAltitude as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_AltitudeFilter_FilterStageToggle(self, request, workspace, editor, launcher_platform): from .EditorScripts import AltitudeFilter_FilterStageToggle as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SpawnerSlices_SliceCreationAndVisibilityToggleWorks(self, request, workspace, editor, remove_test_slice, launcher_platform): from .EditorScripts import SpawnerSlices_SliceCreationAndVisibilityToggleWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_AssetListCombiner_CombinedDescriptorsExpressInConfiguredArea(self, request, workspace, editor, launcher_platform): from .EditorScripts import AssetListCombiner_CombinedDescriptorsExpressInConfiguredArea as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_AssetWeightSelector_InstancesExpressBasedOnWeight(self, request, workspace, editor, launcher_platform): from .EditorScripts import AssetWeightSelector_InstancesExpressBasedOnWeight as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="https://github.com/o3de/o3de/issues/4155") def test_DistanceBetweenFilter_InstancesPlantAtSpecifiedRadius(self, request, workspace, editor, launcher_platform): from .EditorScripts import DistanceBetweenFilter_InstancesPlantAtSpecifiedRadius as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="https://github.com/o3de/o3de/issues/4155") def test_DistanceBetweenFilterOverrides_InstancesPlantAtSpecifiedRadius(self, request, workspace, editor, launcher_platform): from .EditorScripts import DistanceBetweenFilterOverrides_InstancesPlantAtSpecifiedRadius as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SurfaceDataRefreshes_RemainsStable(self, request, workspace, editor, launcher_platform): from .EditorScripts import SurfaceDataRefreshes_RemainsStable as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_VegetationInstances_DespawnWhenOutOfRange(self, request, workspace, editor, launcher_platform): from .EditorScripts import VegetationInstances_DespawnWhenOutOfRange as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_InstanceSpawnerPriority_LayerAndSubPriority_HigherValuesPlantOverLower(self, request, workspace, editor, launcher_platform): from .EditorScripts import InstanceSpawnerPriority_LayerAndSubPriority as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LayerBlocker_InstancesBlockedInConfiguredArea(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerBlocker_InstancesBlockedInConfiguredArea as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LayerSpawner_InheritBehaviorFlag(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerSpawner_InheritBehaviorFlag as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LayerSpawner_InstancesPlantInAllSupportedShapes(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerSpawner_InstancesPlantInAllSupportedShapes as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LayerSpawner_FilterStageToggle(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerSpawner_FilterStageToggle as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="https://github.com/o3de/o3de/issues/2038") def test_LayerSpawner_InstancesRefreshUsingCorrectViewportCamera(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerSpawner_InstancesRefreshUsingCorrectViewportCamera as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_MeshBlocker_InstancesBlockedByMesh(self, request, workspace, editor, launcher_platform): from .EditorScripts import MeshBlocker_InstancesBlockedByMesh as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_MeshBlocker_InstancesBlockedByMeshHeightTuning(self, request, workspace, editor, launcher_platform): from .EditorScripts import MeshBlocker_InstancesBlockedByMeshHeightTuning as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_MeshSurfaceTagEmitter_DependentOnMeshComponent(self, request, workspace, editor, launcher_platform): from .EditorScripts import MeshSurfaceTagEmitter_DependentOnMeshComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_MeshSurfaceTagEmitter_SurfaceTagsAddRemoveSuccessfully(self, request, workspace, editor, launcher_platform): from .EditorScripts import MeshSurfaceTagEmitter_SurfaceTagsAddRemoveSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_PhysXColliderSurfaceTagEmitter_E2E_Editor(self, request, workspace, editor, launcher_platform): from .EditorScripts import PhysXColliderSurfaceTagEmitter_E2E_Editor as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_PositionModifier_ComponentAndOverrides_InstancesPlantAtSpecifiedOffsets(self, request, workspace, editor, launcher_platform): from .EditorScripts import PositionModifier_ComponentAndOverrides_InstancesPlantAtSpecifiedOffsets as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_PositionModifier_AutoSnapToSurfaceWorks(self, request, workspace, editor, launcher_platform): from .EditorScripts import PositionModifier_AutoSnapToSurfaceWorks as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_RotationModifier_InstancesRotateWithinRange(self, request, workspace, editor, launcher_platform): from .EditorScripts import RotationModifier_InstancesRotateWithinRange as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_RotationModifierOverrides_InstancesRotateWithinRange(self, request, workspace, editor, launcher_platform): from .EditorScripts import RotationModifierOverrides_InstancesRotateWithinRange as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ScaleModifier_InstancesProperlyScale(self, request, workspace, editor, launcher_platform): from .EditorScripts import ScaleModifier_InstancesProperlyScale as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ScaleModifierOverrides_InstancesProperlyScale(self, request, workspace, editor, launcher_platform): from .EditorScripts import ScaleModifierOverrides_InstancesProperlyScale as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ShapeIntersectionFilter_InstancesPlantInAssignedShape(self, request, workspace, editor, launcher_platform): from .EditorScripts import ShapeIntersectionFilter_InstancesPlantInAssignedShape as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ShapeIntersectionFilter_FilterStageToggle(self, request, workspace, editor, launcher_platform): from .EditorScripts import ShapeIntersectionFilter_FilterStageToggle as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SlopeAlignmentModifier_InstanceSurfaceAlignment(self, request, workspace, editor, launcher_platform): from .EditorScripts import SlopeAlignmentModifier_InstanceSurfaceAlignment as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SlopeAlignmentModifierOverrides_InstanceSurfaceAlignment(self, request, workspace, editor, launcher_platform): from .EditorScripts import SlopeAlignmentModifierOverrides_InstanceSurfaceAlignment as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SurfaceMaskFilter_BasicSurfaceTagCreation(self, request, workspace, editor, launcher_platform): from .EditorScripts import SurfaceMaskFilter_BasicSurfaceTagCreation as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SurfaceMaskFilter_ExclusiveSurfaceTags_Function(self, request, workspace, editor, launcher_platform): from .EditorScripts import SurfaceMaskFilter_ExclusionList as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SurfaceMaskFilter_InclusiveSurfaceTags_Function(self, request, workspace, editor, launcher_platform): from .EditorScripts import SurfaceMaskFilter_InclusionList as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SurfaceMaskFilterOverrides_MultipleDescriptorOverridesPlantAsExpected(self, request, workspace, editor, launcher_platform): from .EditorScripts import SurfaceMaskFilterOverrides_MultipleDescriptorOverridesPlantAsExpected as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SystemSettings_SectorPointDensity(self, request, workspace, editor, launcher_platform): from .EditorScripts import SystemSettings_SectorPointDensity as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SystemSettings_SectorSize(self, request, workspace, editor, launcher_platform): from .EditorScripts import SystemSettings_SectorSize as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_SlopeFilter_ComponentAndOverrides_InstancesPlantOnValidSlopes(self, request, workspace, editor, launcher_platform): from .EditorScripts import SlopeFilter_ComponentAndOverrides_InstancesPlantOnValidSlope as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.SUITE_periodic @@ -219,7 +219,7 @@ class TestAutomationE2E(TestAutomationBase): file_system.delete([os.path.join(workspace.paths.engine_root(), project, "Levels", level)], True, True) from .EditorScripts import DynamicSliceInstanceSpawner_Embedded_E2E as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.parametrize("launcher_platform", ['windows']) def test_DynamicSliceInstanceSpawner_Embedded_E2E_Launcher(self, workspace, launcher, level, @@ -240,7 +240,7 @@ class TestAutomationE2E(TestAutomationBase): file_system.delete([os.path.join(workspace.paths.engine_root(), project, "Levels", level)], True, True) from .EditorScripts import DynamicSliceInstanceSpawner_External_E2E as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.parametrize("launcher_platform", ['windows']) def test_DynamicSliceInstanceSpawner_External_E2E_Launcher(self, workspace, launcher, level, @@ -261,7 +261,7 @@ class TestAutomationE2E(TestAutomationBase): file_system.delete([os.path.join(workspace.paths.engine_root(), project, "Levels", level)], True, True) from .EditorScripts import LayerBlender_E2E_Editor as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.parametrize("launcher_platform", ['windows']) @pytest.mark.xfail(reason="https://github.com/o3de/o3de/issues/4170") diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic_Optimized.py b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic_Optimized.py new file mode 100644 index 0000000000..f87d6567ef --- /dev/null +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/dyn_veg/TestSuite_Periodic_Optimized.py @@ -0,0 +1,23 @@ +""" +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 +""" + +import os +import pytest + +import ly_test_tools.environment.file_system as file_system +from ly_test_tools.o3de.editor_test import EditorSingleTest, EditorSharedTest, EditorParallelTest, EditorTestSuite + + +@pytest.mark.SUITE_main +@pytest.mark.parametrize("launcher_platform", ['windows_editor']) +@pytest.mark.parametrize("project", ["AutomatedTesting"]) +class TestAutomation(EditorTestSuite): + + global_extra_cmdline_args = ["-BatchMode", "-autotest_mode"] + + class test_DynVegUtils_TempPrefabCreationWorks(EditorSharedTest): + from .EditorScripts import DynVegUtils_TempPrefabCreationWorks as test_module diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic.py index 21eecf642c..3ad27eb973 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic.py @@ -20,52 +20,52 @@ class TestAutomation(TestAutomationBase): def test_GradientGenerators_Incompatibilities(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientGenerators_Incompatibilities as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientModifiers_Incompatibilities(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientModifiers_Incompatibilities as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientPreviewSettings_DefaultPinnedEntityIsSelf(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientPreviewSettings_DefaultPinnedEntityIsSelf as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientPreviewSettings_ClearingPinnedEntitySetsPreviewToOrigin(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientPreviewSettings_ClearingPinnedEntitySetsPreviewToOrigin as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientSampling_GradientReferencesAddRemoveSuccessfully(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientSampling_GradientReferencesAddRemoveSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientSurfaceTagEmitter_ComponentDependencies(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientSurfaceTagEmitter_ComponentDependencies as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientSurfaceTagEmitter_SurfaceTagsAddRemoveSuccessfully(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientSurfaceTagEmitter_SurfaceTagsAddRemoveSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientTransform_RequiresShape(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientTransform_RequiresShape as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientTransform_FrequencyZoomCanBeSetBeyondSliderRange(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientTransform_FrequencyZoomCanBeSetBeyondSliderRange as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientTransform_ComponentIncompatibleWithSpawners(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientTransform_ComponentIncompatibleWithSpawners as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_GradientTransform_ComponentIncompatibleWithExpectedGradients(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientTransform_ComponentIncompatibleWithExpectedGradients as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ImageGradient_RequiresShape(self, request, workspace, editor, launcher_platform): from .EditorScripts import ImageGradient_RequiresShape as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ImageGradient_ProcessedImageAssignedSuccessfully(self, request, workspace, editor, launcher_platform): from .EditorScripts import ImageGradient_ProcessedImageAssignedSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic_Optimized.py b/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic_Optimized.py index 514504d324..6ac0658b4d 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/gradient_signal/TestSuite_Periodic_Optimized.py @@ -15,6 +15,8 @@ from ly_test_tools.o3de.editor_test import EditorSingleTest, EditorSharedTest, E @pytest.mark.parametrize("project", ["AutomatedTesting"]) class TestAutomation(EditorTestSuite): + enable_prefab_system = False + class test_GradientGenerators_Incompatibilities(EditorSharedTest): from .EditorScripts import GradientGenerators_Incompatibilities as test_module diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main.py b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main.py index af4855a546..dedc9c70c0 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main.py @@ -22,8 +22,8 @@ class TestAutomation(TestAutomationBase): def test_LandscapeCanvas_SlotConnections_UpdateComponentReferences(self, request, workspace, editor, launcher_platform): from .EditorScripts import SlotConnections_UpdateComponentReferences as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientMixer_NodeConstruction(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientMixer_NodeConstruction as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main_Optimized.py b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main_Optimized.py index 1c3652cae9..402df133cb 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main_Optimized.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Main_Optimized.py @@ -18,6 +18,8 @@ from ly_test_tools.o3de.editor_test import EditorSingleTest, EditorSharedTest, E @pytest.mark.parametrize("project", ["AutomatedTesting"]) class TestAutomation(EditorTestSuite): + enable_prefab_system = False + class test_LandscapeCanvas_SlotConnections_UpdateComponentReferences(EditorSharedTest): from .EditorScripts import SlotConnections_UpdateComponentReferences as test_module diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Periodic.py index ef8b3e492b..a719b1a7c0 100644 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/landscape_canvas/TestSuite_Periodic.py @@ -33,89 +33,89 @@ class TestAutomation(TestAutomationBase): def test_LandscapeCanvas_AreaNodes_DependentComponentsAdded(self, request, workspace, editor, launcher_platform): from .EditorScripts import AreaNodes_DependentComponentsAdded as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_AreaNodes_EntityCreatedOnNodeAdd(self, request, workspace, editor, launcher_platform): from .EditorScripts import AreaNodes_EntityCreatedOnNodeAdd as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_AreaNodes_EntityRemovedOnNodeDelete(self, request, workspace, editor, launcher_platform): from .EditorScripts import AreaNodes_EntityRemovedOnNodeDelete as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_LayerExtenderNodes_ComponentEntitySync(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerExtenderNodes_ComponentEntitySync as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_Edit_DisabledNodeDuplication(self, request, workspace, editor, launcher_platform): from .EditorScripts import Edit_DisabledNodeDuplication as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_Edit_UndoNodeDelete_SliceEntity(self, request, workspace, editor, launcher_platform): from .EditorScripts import Edit_UndoNodeDelete_SliceEntity as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_NewGraph_CreatedSuccessfully(self, request, workspace, editor, launcher_platform): from .EditorScripts import NewGraph_CreatedSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_Component_AddedRemoved(self, request, workspace, editor, launcher_platform): from .EditorScripts import Component_AddedRemoved as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GraphClosed_OnLevelChange(self, request, workspace, editor, launcher_platform): from .EditorScripts import GraphClosed_OnLevelChange as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="https://github.com/o3de/o3de/issues/2201") def test_LandscapeCanvas_GraphClosed_OnEntityDelete(self, request, workspace, editor, launcher_platform): from .EditorScripts import GraphClosed_OnEntityDelete as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GraphClosed_TabbedGraphClosesIndependently(self, request, workspace, editor, launcher_platform): from .EditorScripts import GraphClosed_TabbedGraph as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_Slice_CreateInstantiate(self, request, workspace, editor, remove_test_slice, launcher_platform): from .EditorScripts import Slice_CreateInstantiate as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientModifierNodes_EntityCreatedOnNodeAdd(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientModifierNodes_EntityCreatedOnNodeAdd as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientModifierNodes_EntityRemovedOnNodeDelete(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientModifierNodes_EntityRemovedOnNodeDelete as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientNodes_DependentComponentsAdded(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientNodes_DependentComponentsAdded as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientNodes_EntityCreatedOnNodeAdd(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientNodes_EntityCreatedOnNodeAdd as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GradientNodes_EntityRemovedOnNodeDelete(self, request, workspace, editor, launcher_platform): from .EditorScripts import GradientNodes_EntityRemovedOnNodeDelete as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_GraphUpdates_UpdateComponents(self, request, workspace, editor, launcher_platform): from .EditorScripts import GraphUpdates_UpdateComponents as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_ComponentUpdates_UpdateGraph(self, request, workspace, editor, launcher_platform): from .EditorScripts import ComponentUpdates_UpdateGraph as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_LayerBlender_NodeConstruction(self, request, workspace, editor, launcher_platform): from .EditorScripts import LayerBlender_NodeConstruction as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_ShapeNodes_EntityCreatedOnNodeAdd(self, request, workspace, editor, launcher_platform): from .EditorScripts import ShapeNodes_EntityCreatedOnNodeAdd as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_LandscapeCanvas_ShapeNodes_EntityRemovedOnNodeDelete(self, request, workspace, editor, launcher_platform): from .EditorScripts import ShapeNodes_EntityRemovedOnNodeDelete as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/largeworlds/large_worlds_utils/editor_dynveg_test_helper.py b/AutomatedTesting/Gem/PythonTests/largeworlds/large_worlds_utils/editor_dynveg_test_helper.py index 957536fffb..d7d5842518 100755 --- a/AutomatedTesting/Gem/PythonTests/largeworlds/large_worlds_utils/editor_dynveg_test_helper.py +++ b/AutomatedTesting/Gem/PythonTests/largeworlds/large_worlds_utils/editor_dynveg_test_helper.py @@ -19,6 +19,45 @@ import azlmbr.paths sys.path.append(os.path.join(azlmbr.paths.projectroot, 'Gem', 'PythonTests')) import editor_python_test_tools.hydra_editor_utils as hydra +from editor_python_test_tools.editor_entity_utils import EditorEntity +from editor_python_test_tools.prefab_utils import Prefab + + +def create_temp_mesh_prefab(mesh_asset_path, prefab_filename): + # Create initial entity + root = EditorEntity.create_editor_entity(name=prefab_filename) + assert root.exists(), "Failed to create entity" + # Add mesh component + mesh_component = root.add_component("Mesh") + assert root.has_component("Mesh") and mesh_component.is_enabled(), "Failed to add/activate Mesh component" + # Assign the specified mesh asset + mesh_asset = asset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetIdByPath", mesh_asset_path, math.Uuid(), False) + mesh_component.set_component_property_value("Controller|Configuration|Mesh Asset", mesh_asset) + assert mesh_component.get_component_property_value("Controller|Configuration|Mesh Asset") == mesh_asset, \ + "Failed to set Mesh asset" + # Create and return the temporary/in-memory prefab + temp_prefab = Prefab.create_prefab([root], prefab_filename) + return temp_prefab + + +def create_temp_physx_mesh_collider(physx_mesh_id, prefab_filename): + # Create initial entity + root = EditorEntity.create_editor_entity(name=prefab_filename) + assert root.exists(), "Failed to create entity" + # Add PhysX Collider component + collider_component = root.add_component("PhysX Collider") + assert root.has_component("PhysX Collider") and collider_component.is_enabled(), \ + "Failed to add/activate PhysX Collider component" + # Set the Collider's Shape Configuration field to PhysicsAsset, and assign the specified PhysX Mesh asset + collider_component.set_component_property_value("Shape Configuration|Shape", 7) + assert collider_component.get_component_property_value("Shape Configuration|Shape") == 7, \ + "Failed to set Collider Shape to PhysicsAsset" + collider_component.set_component_property_value("Shape Configuration|Asset|PhysX Mesh", physx_mesh_id) + assert collider_component.get_component_property_value("Shape Configuration|Asset|PhysX Mesh") == physx_mesh_id, \ + "Failed to assign PhysX Mesh asset" + # Create and return the temporary/in-memory prefab + temp_prefab = Prefab.create_prefab([root], prefab_filename) + return temp_prefab def create_surface_entity(name, center_point, box_size_x, box_size_y, box_size_z): diff --git a/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Periodic.py b/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Periodic.py index b2001e6825..27af63ddbc 100755 --- a/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Periodic.py +++ b/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Periodic.py @@ -27,15 +27,15 @@ TEST_DIRECTORY = os.path.dirname(__file__) class TestAutomation(TestAutomationBase): def test_Pane_HappyPath_OpenCloseSuccessfully(self, request, workspace, editor, launcher_platform): from . import Pane_HappyPath_OpenCloseSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Pane_HappyPath_DocksProperly(self, request, workspace, editor, launcher_platform): from . import Pane_HappyPath_DocksProperly as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Pane_HappyPath_ResizesProperly(self, request, workspace, editor, launcher_platform): from . import Pane_HappyPath_ResizesProperly as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -45,7 +45,7 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptCanvas_TwoComponents_InteractSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -55,15 +55,15 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptCanvas_ChangingAssets_ComponentStable as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Graph_HappyPath_ZoomInZoomOut(self, request, workspace, editor, launcher_platform): from . import Graph_HappyPath_ZoomInZoomOut as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_NodePalette_HappyPath_CanSelectNode(self, request, workspace, editor, launcher_platform): from . import NodePalette_HappyPath_CanSelectNode as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -73,11 +73,11 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_NodePalette_HappyPath_ClearSelection(self, request, workspace, editor, launcher_platform, project): from . import NodePalette_HappyPath_ClearSelection as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -87,7 +87,7 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptCanvas_TwoEntities_UseSimultaneously as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ScriptEvent_HappyPath_CreatedWithoutError(self, request, workspace, editor, launcher_platform, project): def teardown(): @@ -99,19 +99,19 @@ class TestAutomation(TestAutomationBase): [os.path.join(workspace.paths.project(), "ScriptCanvas", "test_file.scriptevent")], True, True ) from . import ScriptEvent_HappyPath_CreatedWithoutError as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ScriptCanvasTools_Toggle_OpenCloseSuccess(self, request, workspace, editor, launcher_platform): from . import ScriptCanvasTools_Toggle_OpenCloseSuccess as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_NodeInspector_HappyPath_VariableRenames(self, request, workspace, editor, launcher_platform, project): from . import NodeInspector_HappyPath_VariableRenames as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Debugger_HappyPath_TargetMultipleGraphs(self, request, workspace, editor, launcher_platform, project): from . import Debugger_HappyPath_TargetMultipleGraphs as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.parametrize("level", ["tmp_level"]) def test_Debugger_HappyPath_TargetMultipleEntities(self, request, workspace, editor, launcher_platform, project, level): @@ -120,16 +120,16 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import Debugger_HappyPath_TargetMultipleEntities as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") def test_EditMenu_Default_UndoRedo(self, request, workspace, editor, launcher_platform, project): from . import EditMenu_Default_UndoRedo as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Pane_Undocked_ClosesSuccessfully(self, request, workspace, editor, launcher_platform): from . import Pane_Undocked_ClosesSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.parametrize("level", ["tmp_level"]) def test_Entity_HappyPath_AddScriptCanvasComponent(self, request, workspace, editor, launcher_platform, project, level): @@ -138,11 +138,11 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import Entity_HappyPath_AddScriptCanvasComponent as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Pane_Default_RetainOnSCRestart(self, request, workspace, editor, launcher_platform): from . import Pane_Default_RetainOnSCRestart as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -152,7 +152,7 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptEvents_HappyPath_SendReceiveAcrossMultiple as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -162,7 +162,7 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptEvents_Default_SendReceiveSuccessfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @pytest.mark.parametrize("level", ["tmp_level"]) @@ -172,24 +172,24 @@ class TestAutomation(TestAutomationBase): request.addfinalizer(teardown) file_system.delete([os.path.join(workspace.paths.project(), "Levels", level)], True, True) from . import ScriptEvents_ReturnSetType_Successfully as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_NodeCategory_ExpandOnClick(self, request, workspace, editor, launcher_platform): from . import NodeCategory_ExpandOnClick as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_NodePalette_SearchText_Deletion(self, request, workspace, editor, launcher_platform): from . import NodePalette_SearchText_Deletion as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") def test_VariableManager_UnpinVariableType_Works(self, request, workspace, editor, launcher_platform): from . import VariableManager_UnpinVariableType_Works as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_Node_HappyPath_DuplicateNode(self, request, workspace, editor, launcher_platform): from . import Node_HappyPath_DuplicateNode as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) def test_ScriptEvent_AddRemoveParameter_ActionsSuccessful(self, request, workspace, editor, launcher_platform): def teardown(): @@ -201,7 +201,7 @@ class TestAutomation(TestAutomationBase): [os.path.join(workspace.paths.project(), "ScriptCanvas", "test_file.scriptevent")], True, True ) from . import ScriptEvent_AddRemoveParameter_ActionsSuccessful as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) # NOTE: We had to use hydra_test_utils.py, as TestAutomationBase run_test method # fails because of pyside_utils import @@ -220,7 +220,14 @@ class TestScriptCanvasTests(object): "File->Open action working as expected: True", ] hydra.launch_and_validate_results( - request, TEST_DIRECTORY, editor, "FileMenu_Default_NewAndOpen.py", expected_lines, auto_test_mode=False, timeout=60, + request, + TEST_DIRECTORY, + editor, + "FileMenu_Default_NewAndOpen.py", + expected_lines, + auto_test_mode=False, + timeout=60, + enable_prefab_system=False, ) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @@ -239,6 +246,7 @@ class TestScriptCanvasTests(object): expected_lines, auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) def test_GraphClose_Default_SavePrompt(self, request, editor, launcher_platform): @@ -255,6 +263,7 @@ class TestScriptCanvasTests(object): expected_lines, auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) def test_VariableManager_Default_CreateDeleteVars(self, request, editor, launcher_platform): @@ -269,6 +278,7 @@ class TestScriptCanvasTests(object): expected_lines, auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) @pytest.mark.parametrize( @@ -304,6 +314,7 @@ class TestScriptCanvasTests(object): cfg_args=[config.get('cfg_args')], auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @@ -332,6 +343,7 @@ class TestScriptCanvasTests(object): expected_lines, auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) @pytest.mark.xfail(reason="Test fails to find expected lines, it needs to be fixed.") @@ -359,5 +371,6 @@ class TestScriptCanvasTests(object): expected_lines, auto_test_mode=False, timeout=60, + enable_prefab_system=False, ) \ No newline at end of file diff --git a/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Sandbox.py b/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Sandbox.py index 91b01d8d08..071ae2286c 100644 --- a/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Sandbox.py +++ b/AutomatedTesting/Gem/PythonTests/scripting/TestSuite_Sandbox.py @@ -23,4 +23,4 @@ class TestAutomation(TestAutomationBase): def test_Opening_Closing_Pane(self, request, workspace, editor, launcher_platform): from . import Opening_Closing_Pane as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Gem/PythonTests/smoke/test_CLITool_SerializeContextTools_Works.py b/AutomatedTesting/Gem/PythonTests/smoke/test_CLITool_SerializeContextTools_Works.py index a3f6b8b09f..917ff17f82 100644 --- a/AutomatedTesting/Gem/PythonTests/smoke/test_CLITool_SerializeContextTools_Works.py +++ b/AutomatedTesting/Gem/PythonTests/smoke/test_CLITool_SerializeContextTools_Works.py @@ -13,7 +13,10 @@ import os import pytest import subprocess +import ly_test_tools + +@pytest.mark.skipif(not ly_test_tools.WINDOWS, reason="Only succeeds on windows https://github.com/o3de/o3de/issues/5539") @pytest.mark.SUITE_smoke class TestCLIToolSerializeContextToolsWorks(object): def test_CLITool_SerializeContextTools_Works(self, build_directory): diff --git a/AutomatedTesting/Gem/PythonTests/smoke/test_Editor_NewExistingLevels_Works.py b/AutomatedTesting/Gem/PythonTests/smoke/test_Editor_NewExistingLevels_Works.py index 6f654b9107..5caf7744c4 100644 --- a/AutomatedTesting/Gem/PythonTests/smoke/test_Editor_NewExistingLevels_Works.py +++ b/AutomatedTesting/Gem/PythonTests/smoke/test_Editor_NewExistingLevels_Works.py @@ -11,10 +11,13 @@ Test should run in both gpu and non gpu import pytest import os from automatedtesting_shared.base import TestAutomationBase + +import ly_test_tools import ly_test_tools.environment.file_system as file_system @pytest.mark.SUITE_smoke +@pytest.mark.skipif(not ly_test_tools.WINDOWS, reason="Only succeeds on windows https://github.com/o3de/o3de/issues/5539") @pytest.mark.parametrize("launcher_platform", ["windows_editor"]) @pytest.mark.parametrize("project", ["AutomatedTesting"]) @pytest.mark.parametrize("level", ["temp_level"]) @@ -28,4 +31,4 @@ class TestAutomation(TestAutomationBase): from . import Editor_NewExistingLevels_Works as test_module - self._run_test(request, workspace, editor, test_module) + self._run_test(request, workspace, editor, test_module, enable_prefab_system=False) diff --git a/AutomatedTesting/Levels/Base/Base.prefab b/AutomatedTesting/Levels/Base/Base.prefab index 98495663b7..7765fe488e 100644 --- a/AutomatedTesting/Levels/Base/Base.prefab +++ b/AutomatedTesting/Levels/Base/Base.prefab @@ -1,52 +1,61 @@ { "ContainerEntity": { - "Id": "ContainerEntity", - "Name": "Base", + "Id": "Entity_[1146574390643]", + "Name": "Level", "Components": { - "Component_[10182366347512475253]": { - "$type": "EditorPrefabComponent", - "Id": 10182366347512475253 + "Component_[10641544592923449938]": { + "$type": "EditorInspectorComponent", + "Id": 10641544592923449938 }, - "Component_[12917798267488243668]": { - "$type": "EditorPendingCompositionComponent", - "Id": 12917798267488243668 - }, - "Component_[3261249813163778338]": { + "Component_[12039882709170782873]": { "$type": "EditorOnlyEntityComponent", - "Id": 3261249813163778338 + "Id": 12039882709170782873 }, - "Component_[3837204912784440039]": { - "$type": "EditorDisabledCompositionComponent", - "Id": 3837204912784440039 + "Component_[12265484671603697631]": { + "$type": "EditorPendingCompositionComponent", + "Id": 12265484671603697631 }, - "Component_[4272963378099646759]": { + "Component_[14126657869720434043]": { + "$type": "EditorEntitySortComponent", + "Id": 14126657869720434043, + "ChildEntityOrderEntryArray": [ + { + "EntityId": "" + }, + { + "EntityId": "", + "SortIndex": 1 + } + ] + }, + "Component_[15230859088967841193]": { "$type": "{27F1E1A1-8D9D-4C3B-BD3A-AFB9762449C0} TransformComponent", - "Id": 4272963378099646759, + "Id": 15230859088967841193, "Parent Entity": "" }, - "Component_[4848458548047175816]": { - "$type": "EditorVisibilityComponent", - "Id": 4848458548047175816 + "Component_[16239496886950819870]": { + "$type": "EditorDisabledCompositionComponent", + "Id": 16239496886950819870 }, - "Component_[5787060997243919943]": { - "$type": "EditorInspectorComponent", - "Id": 5787060997243919943 - }, - "Component_[7804170251266531779]": { - "$type": "EditorLockComponent", - "Id": 7804170251266531779 - }, - "Component_[7874177159288365422]": { - "$type": "EditorEntitySortComponent", - "Id": 7874177159288365422 - }, - "Component_[8018146290632383969]": { + "Component_[5688118765544765547]": { "$type": "EditorEntityIconComponent", - "Id": 8018146290632383969 + "Id": 5688118765544765547 }, - "Component_[8452360690590857075]": { + "Component_[6545738857812235305]": { "$type": "SelectionComponent", - "Id": 8452360690590857075 + "Id": 6545738857812235305 + }, + "Component_[7247035804068349658]": { + "$type": "EditorPrefabComponent", + "Id": 7247035804068349658 + }, + "Component_[9307224322037797205]": { + "$type": "EditorLockComponent", + "Id": 9307224322037797205 + }, + "Component_[9562516168917670048]": { + "$type": "EditorVisibilityComponent", + "Id": 9562516168917670048 } } } diff --git a/AutomatedTesting/Levels/Physics/ForceRegion_ImpulsesPxMeshShapedRigidBody/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo b/AutomatedTesting/Levels/Physics/ForceRegion_ImpulsesPxMeshShapedRigidBody/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Levels/Physics/ForceRegion_ImpulsesPxMeshShapedRigidBody/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo rename to AutomatedTesting/Levels/Physics/ForceRegion_ImpulsesPxMeshShapedRigidBody/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo diff --git a/AutomatedTesting/Levels/Physics/ForceRegion_PxMeshShapedForce/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo b/AutomatedTesting/Levels/Physics/ForceRegion_PxMeshShapedForce/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Levels/Physics/ForceRegion_PxMeshShapedForce/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo rename to AutomatedTesting/Levels/Physics/ForceRegion_PxMeshShapedForce/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo diff --git a/AutomatedTesting/Levels/Physics/Material_DefaultMaterialLibraryChangesWork/rin_skeleton_newgeo - copy.fbx.assetinfo b/AutomatedTesting/Levels/Physics/Material_DefaultMaterialLibraryChangesWork/rin_skeleton_newgeo - Copy.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Levels/Physics/Material_DefaultMaterialLibraryChangesWork/rin_skeleton_newgeo - copy.fbx.assetinfo rename to AutomatedTesting/Levels/Physics/Material_DefaultMaterialLibraryChangesWork/rin_skeleton_newgeo - Copy.fbx.assetinfo diff --git a/AutomatedTesting/Levels/Physics/Physics_WorldBodyBusWorksOnEditorComponents/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo b/AutomatedTesting/Levels/Physics/Physics_WorldBodyBusWorksOnEditorComponents/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Levels/Physics/Physics_WorldBodyBusWorksOnEditorComponents/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo rename to AutomatedTesting/Levels/Physics/Physics_WorldBodyBusWorksOnEditorComponents/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo diff --git a/AutomatedTesting/Levels/Physics/RigidBody_COM_ComputingWorks/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo b/AutomatedTesting/Levels/Physics/RigidBody_COM_ComputingWorks/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo similarity index 100% rename from AutomatedTesting/Levels/Physics/RigidBody_COM_ComputingWorks/PhysXSedan/_dev_sedan_r0-b.fbx.assetinfo rename to AutomatedTesting/Levels/Physics/RigidBody_COM_ComputingWorks/PhysXSedan/_dev_Sedan_r0-b.fbx.assetinfo diff --git a/AutomatedTesting/Registry/editorpreferences.setreg b/AutomatedTesting/Registry/editorpreferences.setreg index b338d6acad..e76e04f9ba 100644 --- a/AutomatedTesting/Registry/editorpreferences.setreg +++ b/AutomatedTesting/Registry/editorpreferences.setreg @@ -1,7 +1,7 @@ { "Amazon": { "Preferences": { - "EnablePrefabSystem": false + "EnablePrefabSystem": true } } } \ No newline at end of file diff --git a/AutomatedTesting/cmake/CompilerSettings.cmake b/AutomatedTesting/cmake/CompilerSettings.cmake new file mode 100644 index 0000000000..60bda1d45b --- /dev/null +++ b/AutomatedTesting/cmake/CompilerSettings.cmake @@ -0,0 +1,13 @@ +# +# 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 +# +# + +# File to tweak compiler settings before compiler detection happens (before project() is called) +# We dont have PAL enabled at this point, so we can only use pure-CMake variables +if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") + include(cmake/Platform/Linux/CompilerSettings_linux.cmake) +endif() diff --git a/AutomatedTesting/EngineFinder.cmake b/AutomatedTesting/cmake/EngineFinder.cmake similarity index 63% rename from AutomatedTesting/EngineFinder.cmake rename to AutomatedTesting/cmake/EngineFinder.cmake index 0a34a43b77..15b96eb8a9 100644 --- a/AutomatedTesting/EngineFinder.cmake +++ b/AutomatedTesting/cmake/EngineFinder.cmake @@ -1,3 +1,4 @@ +# {BEGIN_LICENSE} # # 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. @@ -5,18 +6,34 @@ # SPDX-License-Identifier: Apache-2.0 OR MIT # # +# {END_LICENSE} # This file is copied during engine registration. Edits to this file will be lost next # time a registration happens. include_guard() # Read the engine name from the project_json file -file(READ ${CMAKE_CURRENT_LIST_DIR}/project.json project_json) -set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/project.json) +file(READ ${CMAKE_CURRENT_SOURCE_DIR}/project.json project_json) +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.json) string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine) if(json_error) - message(FATAL_ERROR "Unable to read key 'engine' from 'project.json', error: ${json_error}") + message(FATAL_ERROR "Unable to read key 'engine' from 'project.json'\nError: ${json_error}") +endif() + +if(CMAKE_MODULE_PATH) + foreach(module_path ${CMAKE_MODULE_PATH}) + if(EXISTS ${module_path}/Findo3de.cmake) + file(READ ${module_path}/../engine.json engine_json) + string(JSON engine_name ERROR_VARIABLE json_error GET ${engine_json} engine_name) + if(json_error) + message(FATAL_ERROR "Unable to read key 'engine_name' from 'engine.json'\nError: ${json_error}") + endif() + if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name) + return() # Engine being forced through CMAKE_MODULE_PATH + endif() + endif() + endforeach() endif() if(DEFINED ENV{USERPROFILE} AND EXISTS $ENV{USERPROFILE}) @@ -25,6 +42,11 @@ else() set(manifest_path $ENV{HOME}/.o3de/o3de_manifest.json) # Unix endif() +set(registration_error [=[ +Engine registration is required before configuring a project. +Run 'scripts/o3de register --this-engine' from the engine root. +]=]) + # Read the ~/.o3de/o3de_manifest.json file and look through the 'engines_path' object. # Find a key that matches LY_ENGINE_NAME_TO_USE and use that as the engine path. if(EXISTS ${manifest_path}) @@ -33,36 +55,38 @@ if(EXISTS ${manifest_path}) string(JSON engines_path_count ERROR_VARIABLE json_error LENGTH ${manifest_json} engines_path) if(json_error) - message(FATAL_ERROR "Unable to read key 'engines_path' from '${manifest_path}', error: ${json_error}") + message(FATAL_ERROR "Unable to read key 'engines_path' from '${manifest_path}'\nError: ${json_error}\n${registration_error}") endif() string(JSON engines_path_type ERROR_VARIABLE json_error TYPE ${manifest_json} engines_path) if(json_error OR NOT ${engines_path_type} STREQUAL "OBJECT") - message(FATAL_ERROR "Type of 'engines_path' in '${manifest_path}' is not a JSON Object, error: ${json_error}") + message(FATAL_ERROR "Type of 'engines_path' in '${manifest_path}' is not a JSON Object\nError: ${json_error}") endif() math(EXPR engines_path_count "${engines_path_count}-1") foreach(engine_path_index RANGE ${engines_path_count}) string(JSON engine_name ERROR_VARIABLE json_error MEMBER ${manifest_json} engines_path ${engine_path_index}) if(json_error) - message(FATAL_ERROR "Unable to read 'engines_path/${engine_path_index}' from '${manifest_path}', error: ${json_error}") + message(FATAL_ERROR "Unable to read 'engines_path/${engine_path_index}' from '${manifest_path}'\nError: ${json_error}") endif() if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name) string(JSON engine_path ERROR_VARIABLE json_error GET ${manifest_json} engines_path ${engine_name}) if(json_error) - message(FATAL_ERROR "Unable to read value from 'engines_path/${engine_name}', error: ${json_error}") + message(FATAL_ERROR "Unable to read value from 'engines_path/${engine_name}'\nError: ${json_error}") endif() if(engine_path) list(APPEND CMAKE_MODULE_PATH "${engine_path}/cmake") - break() + return() endif() endif() endforeach() + + message(FATAL_ERROR "The project.json uses engine name '${LY_ENGINE_NAME_TO_USE}' but no engine with that name has been registered.\n${registration_error}") else() # If the user is passing CMAKE_MODULE_PATH we assume thats where we will find the engine if(NOT CMAKE_MODULE_PATH) - message(FATAL_ERROR "Engine registration is required before configuring a project. Please register an engine by running 'scripts/o3de register --this-engine'") + message(FATAL_ERROR "O3DE Manifest file not found.\n${registration_error}") endif() endif() diff --git a/Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake b/AutomatedTesting/cmake/Platform/Linux/CompilerSettings_linux.cmake similarity index 100% rename from Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake rename to AutomatedTesting/cmake/Platform/Linux/CompilerSettings_linux.cmake diff --git a/Code/Editor/2DViewport.cpp b/Code/Editor/2DViewport.cpp index ed810aea29..4d0609907f 100644 --- a/Code/Editor/2DViewport.cpp +++ b/Code/Editor/2DViewport.cpp @@ -234,7 +234,7 @@ void Q2DViewport::UpdateContent(int flags) } ////////////////////////////////////////////////////////////////////////// -void Q2DViewport::OnRButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point) +void Q2DViewport::OnRButtonDown([[maybe_unused]] Qt::KeyboardModifiers modifiers, const QPoint& point) { if (GetIEditor()->IsInGameMode()) { @@ -246,9 +246,6 @@ void Q2DViewport::OnRButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& p setFocus(); } - // Check Edit Tool. - MouseCallback(eMouseRDown, point, modifiers); - SetCurrentCursor(STD_CURSOR_MOVE, QString()); // Save the mouse down position @@ -273,17 +270,8 @@ void Q2DViewport::OnRButtonUp([[maybe_unused]] Qt::KeyboardModifiers modifiers, } ////////////////////////////////////////////////////////////////////////// -void Q2DViewport::OnMButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point) +void Q2DViewport::OnMButtonDown([[maybe_unused]] Qt::KeyboardModifiers modifiers, const QPoint& point) { - //////////////////////////////////////////////////////////////////////// - // User pressed the middle mouse button - //////////////////////////////////////////////////////////////////////// - // Check Edit Tool. - if (MouseCallback(eMouseMDown, point, modifiers)) - { - return; - } - // Save the mouse down position m_RMouseDownPos = point; @@ -300,14 +288,8 @@ void Q2DViewport::OnMButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& p } ////////////////////////////////////////////////////////////////////////// -void Q2DViewport::OnMButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point) +void Q2DViewport::OnMButtonUp([[maybe_unused]] Qt::KeyboardModifiers modifiers, [[maybe_unused]] const QPoint& point) { - // Check Edit Tool. - if (MouseCallback(eMouseMUp, point, modifiers)) - { - return; - } - SetViewMode(NothingMode); ReleaseMouse(); @@ -547,13 +529,6 @@ QPoint Q2DViewport::WorldToView(const Vec3& wp) const QPoint p = QPoint(static_cast(sp.x), static_cast(sp.y)); return p; } -////////////////////////////////////////////////////////////////////////// -QPoint Q2DViewport::WorldToViewParticleEditor(const Vec3& wp, [[maybe_unused]] int width, [[maybe_unused]] int height) const //Eric@conffx implement for the children class of IDisplayViewport -{ - Vec3 sp = m_screenTM.TransformPoint(wp); - QPoint p = QPoint(static_cast(sp.x), static_cast(sp.y)); - return p; -} ////////////////////////////////////////////////////////////////////////// Vec3 Q2DViewport::ViewToWorld(const QPoint& vp, [[maybe_unused]] bool* collideWithTerrain, [[maybe_unused]] bool onlyTerrain, [[maybe_unused]] bool bSkipVegetation, [[maybe_unused]] bool bTestRenderMesh, [[maybe_unused]] bool* collideWithObject) const diff --git a/Code/Editor/2DViewport.h b/Code/Editor/2DViewport.h index 007c1a47d3..e89f4aed34 100644 --- a/Code/Editor/2DViewport.h +++ b/Code/Editor/2DViewport.h @@ -50,8 +50,6 @@ public: //! Map world space position to viewport position. QPoint WorldToView(const Vec3& wp) const override; - QPoint WorldToViewParticleEditor(const Vec3& wp, int width, int height) const override; //Eric@conffx - //! Map viewport position to world space position. Vec3 ViewToWorld(const QPoint& vp, bool* collideWithTerrain = nullptr, bool onlyTerrain = false, bool bSkipVegetation = false, bool bTestRenderMesh = false, bool* collideWithObject = nullptr) const override; //! Map viewport position to world space ray from camera. @@ -64,7 +62,6 @@ public: // ovverided from CViewport. float GetScreenScaleFactor(const Vec3& worldPoint) const override; - float GetScreenScaleFactor([[maybe_unused]] const CCamera& camera, [[maybe_unused]] const Vec3& object_position) override { return 1; } //Eric@conffx // Overrided from CViewport. void OnDragSelectRectangle(const QRect &rect, bool bNormalizeRect = false) override; diff --git a/Code/Editor/AboutDialog.ui b/Code/Editor/AboutDialog.ui index a6c5bb5d52..09a7c18841 100644 --- a/Code/Editor/AboutDialog.ui +++ b/Code/Editor/AboutDialog.ui @@ -125,7 +125,7 @@ - General Availability + development Qt::AutoText diff --git a/Code/Editor/ActionManager.cpp b/Code/Editor/ActionManager.cpp index ff74a2c208..4cc64a532e 100644 --- a/Code/Editor/ActionManager.cpp +++ b/Code/Editor/ActionManager.cpp @@ -152,15 +152,6 @@ ActionManager::ActionWrapper& ActionManager::ActionWrapper::SetMenu(DynamicMenu* return *this; } -ActionManager::ActionWrapper& ActionManager::ActionWrapper::SetApplyHoverEffect() -{ - // Our standard toolbar icons, when hovered on, get a white color effect. - // But for this to work we need .pngs that look good with this effect, so this only works with the standard toolbars - // and looks very ugly for other toolbars, including toolbars loaded from XML (which just show a white rectangle) - m_action->setProperty("IconHasHoverEffect", true); - return *this; -} - ActionManager::ActionWrapper& ActionManager::ActionWrapper::SetReserved() { m_action->setProperty("Reserved", true); diff --git a/Code/Editor/ActionManager.h b/Code/Editor/ActionManager.h index 8879bdc1fb..58504f860c 100644 --- a/Code/Editor/ActionManager.h +++ b/Code/Editor/ActionManager.h @@ -151,7 +151,6 @@ public: } ActionWrapper& SetMenu(DynamicMenu* menu); - ActionWrapper& SetApplyHoverEffect(); operator QAction*() const { return m_action; diff --git a/Code/Editor/Animation/AnimationBipedBoneNames.cpp b/Code/Editor/Animation/AnimationBipedBoneNames.cpp index d9f1b845ca..72502fe61d 100644 --- a/Code/Editor/Animation/AnimationBipedBoneNames.cpp +++ b/Code/Editor/Animation/AnimationBipedBoneNames.cpp @@ -10,24 +10,21 @@ #include "AnimationBipedBoneNames.h" -namespace EditorAnimationBones +namespace EditorAnimationBones::Biped { - namespace Biped - { - const char* Pelvis = "Bip01 Pelvis"; - const char* Head = "Bip01 Head"; - const char* Weapon = "weapon_bone"; + const char* Pelvis = "Bip01 Pelvis"; + const char* Head = "Bip01 Head"; + const char* Weapon = "weapon_bone"; - const char* LeftEye = "eye_bone_left"; - const char* RightEye = "eye_bone_right"; + const char* LeftEye = "eye_bone_left"; + const char* RightEye = "eye_bone_right"; - const char* Spine[5] = { "Bip01 Spine", "Bip01 Spine1", "Bip01 Spine2", "Bip01 Spine3", "Bip01 Spine4" }; - const char* Neck[2] = { "Bip01 Neck", "Bip01 Neck1" }; + const char* Spine[5] = { "Bip01 Spine", "Bip01 Spine1", "Bip01 Spine2", "Bip01 Spine3", "Bip01 Spine4" }; + const char* Neck[2] = { "Bip01 Neck", "Bip01 Neck1" }; - const char* LeftHeel = "Bip01 L Heel"; - const char* LeftToe[2] = { "Bip01 L Toe0", "Bip01 L Toe1" }; + const char* LeftHeel = "Bip01 L Heel"; + const char* LeftToe[2] = { "Bip01 L Toe0", "Bip01 L Toe1" }; - const char* RightHeel = "Bip01 R Heel"; - const char* RightToe[2] = { "Bip01 R Toe0", "Bip01 R Toe1" }; - } -} + const char* RightHeel = "Bip01 R Heel"; + const char* RightToe[2] = { "Bip01 R Toe0", "Bip01 R Toe1" }; +} // namespace EditorAnimationBones::Biped diff --git a/Code/Editor/AnimationContext.cpp b/Code/Editor/AnimationContext.cpp index fce1150878..51dd8deefc 100644 --- a/Code/Editor/AnimationContext.cpp +++ b/Code/Editor/AnimationContext.cpp @@ -21,6 +21,8 @@ #include "Include/IObjectManager.h" #include "Objects/EntityObject.h" +#include + ////////////////////////////////////////////////////////////////////////// // Movie Callback. ////////////////////////////////////////////////////////////////////////// @@ -499,25 +501,24 @@ void CAnimationContext::Update() return; } - ITimer* pTimer = GetIEditor()->GetSystem()->GetITimer(); + const AZ::TimeUs frameDeltaTimeUs = AZ::GetSimulationTickDeltaTimeUs(); + const float frameDeltaTime = AZ::TimeUsToSeconds(frameDeltaTimeUs); if (!m_bAutoRecording) { AnimateActiveSequence(); - float dt = pTimer->GetFrameTime(); - m_currTime += dt * m_fTimeScale; + m_currTime += frameDeltaTime * m_fTimeScale; if (!m_recording) { - GetIEditor()->GetMovieSystem()->PreUpdate(dt); - GetIEditor()->GetMovieSystem()->PostUpdate(dt); + GetIEditor()->GetMovieSystem()->PreUpdate(frameDeltaTime); + GetIEditor()->GetMovieSystem()->PostUpdate(frameDeltaTime); } } else { - float dt = pTimer->GetFrameTime(); - m_fRecordingCurrTime += dt * m_fTimeScale; + m_fRecordingCurrTime += frameDeltaTime * m_fTimeScale; if (fabs(m_fRecordingCurrTime - m_currTime) > m_fRecordingTimeStep) { m_currTime += m_fRecordingTimeStep; @@ -644,7 +645,9 @@ void CAnimationContext::OnPostRender() { SAnimContext ac; ac.dt = 0; - ac.fps = GetIEditor()->GetSystem()->GetITimer()->GetFrameRate(); + const AZ::TimeUs frameDeltaTimeUs = AZ::GetSimulationTickDeltaTimeUs(); + const float frameDeltaTime = AZ::TimeUsToSeconds(frameDeltaTimeUs); + ac.fps = 1.0f / frameDeltaTime; ac.time = m_currTime; ac.singleFrame = true; ac.forcePlay = true; @@ -797,7 +800,9 @@ void CAnimationContext::AnimateActiveSequence() SAnimContext ac; ac.dt = 0; - ac.fps = GetIEditor()->GetSystem()->GetITimer()->GetFrameRate(); + const AZ::TimeUs frameDeltaTimeUs = AZ::GetSimulationTickDeltaTimeUs(); + const float frameDeltaTime = AZ::TimeUsToSeconds(frameDeltaTimeUs); + ac.fps = 1.0f / frameDeltaTime; ac.time = m_currTime; ac.singleFrame = true; ac.forcePlay = true; diff --git a/Code/Editor/AssetImporter/AssetImporterManager/AssetImporterManager.cpp b/Code/Editor/AssetImporter/AssetImporterManager/AssetImporterManager.cpp index b721b8759e..baa287e47f 100644 --- a/Code/Editor/AssetImporter/AssetImporterManager/AssetImporterManager.cpp +++ b/Code/Editor/AssetImporter/AssetImporterManager/AssetImporterManager.cpp @@ -140,7 +140,7 @@ bool AssetImporterManager::OnBrowseFiles() bool encounteredCrate = false; QStringList invalidFiles; - for (QString path : fileDialog.selectedFiles()) + for (const QString& path : fileDialog.selectedFiles()) { QString fileName = GetFileName(path); QFileInfo info(path); diff --git a/Code/Editor/AzAssetBrowser/AzAssetBrowserRequestHandler.cpp b/Code/Editor/AzAssetBrowser/AzAssetBrowserRequestHandler.cpp index 1c2be16a3d..6fc10e379a 100644 --- a/Code/Editor/AzAssetBrowser/AzAssetBrowserRequestHandler.cpp +++ b/Code/Editor/AzAssetBrowser/AzAssetBrowserRequestHandler.cpp @@ -199,41 +199,6 @@ namespace AzAssetBrowserRequestHandlerPrivate } } } - - // Helper utility - determines if the thing being dragged is a FBX from the scene import pipeline - // This is important to differentiate. - // when someone drags a MTL file directly into the viewport, even from a FBX, we want to spawn it as a decal - // but when someone drags a FBX that contains MTL files, we want only to spawn the meshes. - // so we have to specifically differentiate here between the mimeData type that contains the source as the root - // (dragging the fbx file itself) - // and one which contains the actual product at its root. - - bool IsDragOfFBX(const QMimeData* mimeData) - { - AZStd::vector entries; - if (!AssetBrowserEntry::FromMimeData(mimeData, entries)) - { - // if mimedata does not even contain entries, no point in proceeding. - return false; - } - - for (auto entry : entries) - { - if (entry->GetEntryType() != AssetBrowserEntry::AssetEntryType::Source) - { - continue; - } - // this is a source file. Is it the filetype we're looking for? - if (SourceAssetBrowserEntry* source = azrtti_cast(entry)) - { - if (AzFramework::StringFunc::Equal(source->GetExtension().c_str(), ".fbx", false)) - { - return true; - } - } - } - return false; - } } AzAssetBrowserRequestHandler::AzAssetBrowserRequestHandler() diff --git a/Code/Editor/BaseLibraryManager.cpp b/Code/Editor/BaseLibraryManager.cpp index 7346ffaf8c..3370165ddb 100644 --- a/Code/Editor/BaseLibraryManager.cpp +++ b/Code/Editor/BaseLibraryManager.cpp @@ -606,11 +606,9 @@ void CBaseLibraryManager::RegisterItem(CBaseLibraryItem* pItem, REFGUID newGuid) if (m_bUniqGuidMap) { - bool bNewItem = true; REFGUID oldGuid = pItem->GetGUID(); if (!GuidUtil::IsEmpty(oldGuid)) { - bNewItem = false; m_itemsGuidMap.erase(oldGuid); } if (GuidUtil::IsEmpty(newGuid)) diff --git a/Code/Editor/CMakeLists.txt b/Code/Editor/CMakeLists.txt index 693799e08b..3358b49dce 100644 --- a/Code/Editor/CMakeLists.txt +++ b/Code/Editor/CMakeLists.txt @@ -131,7 +131,7 @@ ly_add_source_properties( PROPERTY COMPILE_DEFINITIONS VALUES O3DE_COPYRIGHT_YEAR=${LY_VERSION_COPYRIGHT_YEAR} - LY_BUILD=${LY_VERSION_BUILD_NUMBER} + LY_VERSION_BUILD_NUMBER=${LY_VERSION_BUILD_NUMBER} ${LY_PAL_TOOLS_DEFINES} ) ly_add_source_properties( diff --git a/Code/Editor/Controls/ReflectedPropertyControl/ReflectedPropertyCtrl.cpp b/Code/Editor/Controls/ReflectedPropertyControl/ReflectedPropertyCtrl.cpp index 1151137bfc..c5bf6a4d81 100644 --- a/Code/Editor/Controls/ReflectedPropertyControl/ReflectedPropertyCtrl.cpp +++ b/Code/Editor/Controls/ReflectedPropertyControl/ReflectedPropertyCtrl.cpp @@ -671,7 +671,7 @@ AzToolsFramework::PropertyRowWidget* ReflectedPropertyControl::FindPropertyRowWi return nullptr; } const AzToolsFramework::ReflectedPropertyEditor::WidgetList& widgets = m_editor->GetWidgets(); - for (auto instance : widgets) + for (const auto& instance : widgets) { if (instance.second->label() == item->GetPropertyName()) { diff --git a/Code/Editor/CryEdit.cpp b/Code/Editor/CryEdit.cpp index 5bcb77797c..4b725ac3d7 100644 --- a/Code/Editor/CryEdit.cpp +++ b/Code/Editor/CryEdit.cpp @@ -80,7 +80,6 @@ AZ_POP_DISABLE_WARNING #include // CryCommon -#include #include // Editor @@ -371,10 +370,8 @@ void CCryEditApp::RegisterActionHandlers() ON_COMMAND(ID_EDIT_FETCH, OnEditFetch) ON_COMMAND(ID_FILE_EXPORTTOGAMENOSURFACETEXTURE, OnFileExportToGameNoSurfaceTexture) ON_COMMAND(ID_VIEW_SWITCHTOGAME, OnViewSwitchToGame) - MainWindow::instance()->GetActionManager()->RegisterActionHandler(ID_VIEW_SWITCHTOGAME_FULLSCREEN, [this]() { - ed_previewGameInFullscreen_once = true; - OnViewSwitchToGame(); - }); + ON_COMMAND(ID_VIEW_SWITCHTOGAME_VIEWPORT, OnViewSwitchToGame) + ON_COMMAND(ID_VIEW_SWITCHTOGAME_FULLSCREEN, OnViewSwitchToGameFullScreen) ON_COMMAND(ID_MOVE_OBJECT, OnMoveObject) ON_COMMAND(ID_RENAME_OBJ, OnRenameObj) ON_COMMAND(ID_UNDO, OnUndo) @@ -914,13 +911,9 @@ namespace QWidget* g_splashScreen = nullptr; } -QString FormatVersion(const SFileVersion& v) +QString FormatVersion([[maybe_unused]] const SFileVersion& v) { -#if defined(LY_BUILD) - return QObject::tr("Version %1.%2.%3.%4 - Build %5").arg(v[3]).arg(v[2]).arg(v[1]).arg(v[0]).arg(LY_BUILD); -#else - return QObject::tr("Version %1.%2.%3.%4").arg(v[3]).arg(v[2]).arg(v[1]).arg(v[0]); -#endif + return QObject::tr("Version %1").arg(LY_VERSION_BUILD_NUMBER); } QString FormatRichTextCopyrightNotice() @@ -1361,16 +1354,6 @@ void CCryEditApp::CompileCriticalAssets() const assetsInQueueNotifcation.BusDisconnect(); CCryEditApp::OutputStartupMessage(QString("Asset Processor is now ready.")); - // VERY early on, as soon as we can, request that the asset system make sure the following assets take priority over others, - // so that by the time we ask for them there is a greater likelihood that they're already good to go. - // these can be loaded later but are still important: - AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::EscalateAssetBySearchTerm, "/texturemsg/"); - AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::EscalateAssetBySearchTerm, "engineassets/materials"); - AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::EscalateAssetBySearchTerm, "engineassets/geomcaches"); - AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::EscalateAssetBySearchTerm, "engineassets/objects"); - - // some are specifically extra important and will cause issues if missing completely: - AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::CompileAssetSync, "engineassets/objects/default.cgf"); } bool CCryEditApp::ConnectToAssetProcessor() const @@ -2584,6 +2567,12 @@ void CCryEditApp::OnViewSwitchToGame() GetIEditor()->SetInGameMode(inGame); } +void CCryEditApp::OnViewSwitchToGameFullScreen() +{ + ed_previewGameInFullscreen_once = true; + OnViewSwitchToGame(); +} + ////////////////////////////////////////////////////////////////////////// void CCryEditApp::OnExportSelectedObjects() { @@ -3974,9 +3963,8 @@ void CCryEditApp::OpenLUAEditor(const char* files) } } - const char* engineRoot = nullptr; - AzFramework::ApplicationRequests::Bus::BroadcastResult(engineRoot, &AzFramework::ApplicationRequests::GetEngineRoot); - AZ_Assert(engineRoot != nullptr, "Unable to communicate to AzFramework::ApplicationRequests::Bus"); + AZ::IO::FixedMaxPathString engineRoot = AZ::Utils::GetEnginePath(); + AZ_Assert(!engineRoot.empty(), "Unable to query Engine Path"); AZStd::string_view exePath; AZ::ComponentApplicationBus::BroadcastResult(exePath, &AZ::ComponentApplicationRequests::GetExecutableFolder); @@ -3995,7 +3983,7 @@ void CCryEditApp::OpenLUAEditor(const char* files) #endif "%s", argumentQuoteString, aznumeric_cast(exePath.size()), exePath.data(), argumentQuoteString); - AZStd::string processArgs = AZStd::string::format("%s -engine-path \"%s\"", args.c_str(), engineRoot); + AZStd::string processArgs = AZStd::string::format("%s -engine-path \"%s\"", args.c_str(), engineRoot.c_str()); StartProcessDetached(process.c_str(), processArgs.c_str()); } @@ -4196,6 +4184,8 @@ extern "C" int AZ_DLL_EXPORT CryEditMain(int argc, char* argv[]) "\nThis could be because of incorrectly configured components, or missing required gems." "\nSee other errors for more details."); + AzToolsFramework::EditorEventsBus::Broadcast(&AzToolsFramework::EditorEvents::NotifyEditorInitialized); + if (didCryEditStart) { app->EnableOnIdle(); diff --git a/Code/Editor/CryEdit.h b/Code/Editor/CryEdit.h index 53ee8f1905..8c514170ae 100644 --- a/Code/Editor/CryEdit.h +++ b/Code/Editor/CryEdit.h @@ -212,6 +212,7 @@ public: void OnEditFetch(); void OnFileExportToGameNoSurfaceTexture(); void OnViewSwitchToGame(); + void OnViewSwitchToGameFullScreen(); void OnViewDeploy(); void DeleteSelectedEntities(bool includeDescendants); void OnMoveObject(); diff --git a/Code/Editor/CryEditDoc.cpp b/Code/Editor/CryEditDoc.cpp index 212606c737..9c6c257ac0 100644 --- a/Code/Editor/CryEditDoc.cpp +++ b/Code/Editor/CryEditDoc.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -749,7 +750,9 @@ bool CCryEditDoc::OnOpenDocument(const QString& lpszPathName) bool CCryEditDoc::BeforeOpenDocument(const QString& lpszPathName, TOpenDocContext& context) { - CTimeValue loading_start_time = gEnv->pTimer->GetAsyncTime(); + const AZ::TimeMs timeMs = AZ::GetRealElapsedTimeMs(); + const double timeSec = AZ::TimeMsToSecondsDouble(timeMs); + const CTimeValue loading_start_time(timeSec); bool usePrefabSystemForLevels = false; AzFramework::ApplicationRequests::Bus::BroadcastResult( @@ -790,7 +793,7 @@ bool CCryEditDoc::BeforeOpenDocument(const QString& lpszPathName, TOpenDocContex bool CCryEditDoc::DoOpenDocument(TOpenDocContext& context) { - CTimeValue& loading_start_time = context.loading_start_time; + const CTimeValue& loading_start_time = context.loading_start_time; bool isPrefabEnabled = false; AzFramework::ApplicationRequests::Bus::BroadcastResult(isPrefabEnabled, &AzFramework::ApplicationRequests::IsPrefabSystemEnabled); @@ -860,7 +863,9 @@ bool CCryEditDoc::DoOpenDocument(TOpenDocContext& context) StartStreamingLoad(); - CTimeValue loading_end_time = gEnv->pTimer->GetAsyncTime(); + const AZ::TimeMs timeMs = AZ::GetRealElapsedTimeMs(); + const double timeSec = AZ::TimeMsToSecondsDouble(timeMs); + const CTimeValue loading_end_time(timeSec); CLogFile::FormatLine("-----------------------------------------------------------"); CLogFile::FormatLine("Successfully opened document %s", context.absoluteLevelPath.toUtf8().data()); @@ -1123,7 +1128,7 @@ bool CCryEditDoc::SaveLevel(const QString& filename) const QString oldLevelPattern = QDir(oldLevelFolder).absoluteFilePath("*.*"); const QString oldLevelName = Path::GetFile(GetLevelPathName()); const QString oldLevelXml = Path::ReplaceExtension(oldLevelName, "xml"); - AZ::IO::ArchiveFileIterator findHandle = pIPak->FindFirst(oldLevelPattern.toUtf8().data(), AZ::IO::IArchive::eFileSearchType_AllowOnDiskAndInZips); + AZ::IO::ArchiveFileIterator findHandle = pIPak->FindFirst(oldLevelPattern.toUtf8().data(), AZ::IO::FileSearchLocation::Any); if (findHandle) { do diff --git a/Code/Editor/Dialogs/PythonScriptsDialog.cpp b/Code/Editor/Dialogs/PythonScriptsDialog.cpp index 35047947ac..06b1acdbc8 100644 --- a/Code/Editor/Dialogs/PythonScriptsDialog.cpp +++ b/Code/Editor/Dialogs/PythonScriptsDialog.cpp @@ -40,10 +40,10 @@ AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING namespace { // File name extension for python files - const QString s_kPythonFileNameSpec = "*.py"; + const QString s_kPythonFileNameSpec("*.py"); // Tree root element name - const QString s_kRootElementName = "Python Scripts"; + const QString s_kRootElementName("Python Scripts"); } ////////////////////////////////////////////////////////////////////////// diff --git a/Code/Editor/EditorDefs.h b/Code/Editor/EditorDefs.h index 4115e8433a..97c03b2b45 100644 --- a/Code/Editor/EditorDefs.h +++ b/Code/Editor/EditorDefs.h @@ -105,7 +105,6 @@ #include #include #include -#include #include #include diff --git a/Code/Editor/EditorModularViewportCameraComposer.cpp b/Code/Editor/EditorModularViewportCameraComposer.cpp index 72fd37605a..f145adf72f 100644 --- a/Code/Editor/EditorModularViewportCameraComposer.cpp +++ b/Code/Editor/EditorModularViewportCameraComposer.cpp @@ -145,6 +145,15 @@ namespace SandboxEditor } }; + const auto trackingTransform = [viewportId = m_viewportId] + { + bool tracking = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + tracking, viewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::IsTrackingTransform); + + return tracking; + }; + m_firstPersonRotateCamera = AZStd::make_shared(SandboxEditor::CameraFreeLookChannelId()); m_firstPersonRotateCamera->m_rotateSpeedFn = [] @@ -152,6 +161,11 @@ namespace SandboxEditor return SandboxEditor::CameraRotateSpeed(); }; + m_firstPersonRotateCamera->m_constrainPitch = [trackingTransform] + { + return !trackingTransform(); + }; + // default behavior is to hide the cursor but this can be disabled (useful for remote desktop) // note: See CaptureCursorLook in the Settings Registry m_firstPersonRotateCamera->SetActivationBeganFn(hideCursor); @@ -255,6 +269,11 @@ namespace SandboxEditor return SandboxEditor::CameraOrbitYawRotationInverted(); }; + m_orbitRotateCamera->m_constrainPitch = [trackingTransform] + { + return !trackingTransform(); + }; + m_orbitTranslateCamera = AZStd::make_shared( translateCameraInputChannelIds, AzFramework::LookTranslation, AzFramework::TranslateOffsetOrbit); @@ -337,12 +356,12 @@ namespace SandboxEditor AZ::TransformBus::EventResult(worldFromLocal, viewEntityId, &AZ::TransformBus::Events::GetWorldTM); AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - m_viewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::SetReferenceFrame, worldFromLocal); + m_viewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StartTrackingTransform, worldFromLocal); } else { AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - m_viewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::ClearReferenceFrame); + m_viewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StopTrackingTransform); } } diff --git a/Code/Editor/EditorViewportWidget.cpp b/Code/Editor/EditorViewportWidget.cpp index 290f0fd17f..132729466c 100644 --- a/Code/Editor/EditorViewportWidget.cpp +++ b/Code/Editor/EditorViewportWidget.cpp @@ -53,7 +53,6 @@ #include // CryCommon -#include #include // AzFramework @@ -299,13 +298,9 @@ AzToolsFramework::ViewportInteraction::MousePick EditorViewportWidget::BuildMous { AzToolsFramework::ViewportInteraction::MousePick mousePick; mousePick.m_screenCoordinates = AzToolsFramework::ViewportInteraction::ScreenPointFromQPoint(point); - if (const auto& ray = m_renderViewport->ViewportScreenToWorldRay(mousePick.m_screenCoordinates); - ray.has_value()) - { - mousePick.m_rayOrigin = ray.value().origin; - mousePick.m_rayDirection = ray.value().direction; - } - + const auto[origin, direction] = m_renderViewport->ViewportScreenToWorldRay(mousePick.m_screenCoordinates); + mousePick.m_rayOrigin = origin; + mousePick.m_rayDirection = direction; return mousePick; } @@ -556,22 +551,6 @@ void EditorViewportWidget::OnEditorNotifyEvent(EEditorNotifyEvent event) // this should only occur for the main viewport and no others. ShowCursor(); - // If the user has selected game mode, enable outputting to any attached HMD and properly size the context - // to the resolution specified by the VR device. - if (gSettings.bEnableGameModeVR) - { - const AZ::VR::HMDDeviceInfo* deviceInfo = nullptr; - EBUS_EVENT_RESULT(deviceInfo, AZ::VR::HMDDeviceRequestBus, GetDeviceInfo); - AZ_Warning("Render Viewport", deviceInfo, "No VR device detected"); - - if (deviceInfo) - { - // Note: This may also need to adjust the viewport size - SetActiveWindow(); - SetFocus(); - SetSelected(true); - } - } SetCurrentCursor(STD_CURSOR_GAME); if (ShouldPreviewFullscreen()) @@ -912,23 +891,6 @@ AZ::Vector3 EditorViewportWidget::PickTerrain(const AzFramework::ScreenPoint& po return LYVec3ToAZVec3(ViewToWorld(AzToolsFramework::ViewportInteraction::QPointFromScreenPoint(point), nullptr, true)); } -AZ::EntityId EditorViewportWidget::PickEntity(const AzFramework::ScreenPoint& point) -{ - AZ::EntityId entityId; - HitContext hitInfo; - hitInfo.view = this; - if (HitTest(AzToolsFramework::ViewportInteraction::QPointFromScreenPoint(point), hitInfo)) - { - if (hitInfo.object && (hitInfo.object->GetType() == OBJTYPE_AZENTITY)) - { - auto entityObject = static_cast(hitInfo.object); - entityId = entityObject->GetAssociatedEntityId(); - } - } - - return entityId; -} - float EditorViewportWidget::TerrainHeight(const AZ::Vector2& position) { return GetIEditor()->GetTerrainElevation(position.GetX(), position.GetY()); @@ -1653,16 +1615,15 @@ void EditorViewportWidget::RenderSelectedRegion() Vec3 EditorViewportWidget::WorldToView3D(const Vec3& wp, [[maybe_unused]] int nFlags) const { Vec3 out(0, 0, 0); - float x, y, z; + float x, y; - ProjectToScreen(wp.x, wp.y, wp.z, &x, &y, &z); - if (_finite(x) && _finite(y) && _finite(z)) + ProjectToScreen(wp.x, wp.y, wp.z, &x, &y); + if (_finite(x) && _finite(y)) { out.x = (x / 100) * m_rcClient.width(); out.y = (y / 100) * m_rcClient.height(); out.x /= static_cast(QHighDpiScaling::factor(windowHandle()->screen())); out.y /= static_cast(QHighDpiScaling::factor(windowHandle()->screen())); - out.z = z; } return out; } @@ -1672,24 +1633,6 @@ QPoint EditorViewportWidget::WorldToView(const Vec3& wp) const { return AzToolsFramework::ViewportInteraction::QPointFromScreenPoint(m_renderViewport->ViewportWorldToScreen(LYVec3ToAZVec3(wp))); } -////////////////////////////////////////////////////////////////////////// -QPoint EditorViewportWidget::WorldToViewParticleEditor(const Vec3& wp, int width, int height) const -{ - QPoint p; - float x, y, z; - - ProjectToScreen(wp.x, wp.y, wp.z, &x, &y, &z); - if (_finite(x) || _finite(y)) - { - p.rx() = static_cast((x / 100) * width); - p.ry() = static_cast((y / 100) * height); - } - else - { - QPoint(0, 0); - } - return p; -} ////////////////////////////////////////////////////////////////////////// Vec3 EditorViewportWidget::ViewToWorld( @@ -1705,20 +1648,16 @@ Vec3 EditorViewportWidget::ViewToWorld( AZ_UNUSED(collideWithObject); auto ray = m_renderViewport->ViewportScreenToWorldRay(AzToolsFramework::ViewportInteraction::ScreenPointFromQPoint(vp)); - if (!ray.has_value()) - { - return Vec3(0, 0, 0); - } const float maxDistance = 10000.f; - Vec3 v = AZVec3ToLYVec3(ray.value().direction) * maxDistance; + Vec3 v = AZVec3ToLYVec3(ray.direction) * maxDistance; if (!_finite(v.x) || !_finite(v.y) || !_finite(v.z)) { return Vec3(0, 0, 0); } - Vec3 colp = AZVec3ToLYVec3(ray.value().origin) + 0.002f * v; + Vec3 colp = AZVec3ToLYVec3(ray.origin) + 0.002f * v; return colp; } @@ -1757,21 +1696,19 @@ bool EditorViewportWidget::RayRenderMeshIntersection(IRenderMesh* pRenderMesh, c return bRes;*/ } -void EditorViewportWidget::UnProjectFromScreen(float sx, float sy, float sz, float* px, float* py, float* pz) const +void EditorViewportWidget::UnProjectFromScreen(float sx, float sy, float* px, float* py, float* pz) const { - AZ::Vector3 wp; - wp = m_renderViewport->ViewportScreenToWorld(AzFramework::ScreenPoint{(int)sx, m_rcClient.bottom() - ((int)sy)}, sz).value_or(wp); + const AZ::Vector3 wp = m_renderViewport->ViewportScreenToWorld(AzFramework::ScreenPoint{(int)sx, m_rcClient.bottom() - ((int)sy)}); *px = wp.GetX(); *py = wp.GetY(); *pz = wp.GetZ(); } -void EditorViewportWidget::ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy, float* sz) const +void EditorViewportWidget::ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy) const { AzFramework::ScreenPoint screenPosition = m_renderViewport->ViewportWorldToScreen(AZ::Vector3{ptx, pty, ptz}); *sx = static_cast(screenPosition.m_x); *sy = static_cast(screenPosition.m_y); - *sz = 0.f; } ////////////////////////////////////////////////////////////////////////// @@ -1781,32 +1718,22 @@ void EditorViewportWidget::ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& Vec3 pos0, pos1; float wx, wy, wz; - UnProjectFromScreen(static_cast(vp.x()), static_cast(rc.bottom() - vp.y()), 0.0f, &wx, &wy, &wz); - if (!_finite(wx) || !_finite(wy) || !_finite(wz)) - { - return; - } - if (fabs(wx) > 1000000 || fabs(wy) > 1000000 || fabs(wz) > 1000000) - { - return; - } - pos0(wx, wy, wz); - UnProjectFromScreen(static_cast(vp.x()), static_cast(rc.bottom() - vp.y()), 1.0f, &wx, &wy, &wz); - if (!_finite(wx) || !_finite(wy) || !_finite(wz)) - { - return; - } - if (fabs(wx) > 1000000 || fabs(wy) > 1000000 || fabs(wz) > 1000000) - { - return; - } - pos1(wx, wy, wz); + UnProjectFromScreen(static_cast(vp.x()), static_cast(rc.bottom() - vp.y()), &wx, &wy, &wz); - Vec3 v = (pos1 - pos0); - v = v.GetNormalized(); + if (!_finite(wx) || !_finite(wy) || !_finite(wz)) + { + return; + } + + if (fabs(wx) > 1000000 || fabs(wy) > 1000000 || fabs(wz) > 1000000) + { + return; + } + + pos0(wx, wy, wz); raySrc = pos0; - rayDir = v; + rayDir = (pos0 - AZVec3ToLYVec3(m_renderViewport->GetCameraState().m_position)).GetNormalized(); } ////////////////////////////////////////////////////////////////////////// @@ -1815,13 +1742,6 @@ float EditorViewportWidget::GetScreenScaleFactor([[maybe_unused]] const Vec3& wo AZ_Error("CryLegacy", false, "EditorViewportWidget::GetScreenScaleFactor not implemented"); return 1.f; } -////////////////////////////////////////////////////////////////////////// -float EditorViewportWidget::GetScreenScaleFactor(const CCamera& camera, const Vec3& object_position) -{ - Vec3 camPos = camera.GetPosition(); - float dist = camPos.GetDistance(object_position); - return dist; -} ////////////////////////////////////////////////////////////////////////// bool EditorViewportWidget::CheckRespondToInput() const @@ -1842,7 +1762,6 @@ bool EditorViewportWidget::CheckRespondToInput() const ////////////////////////////////////////////////////////////////////////// bool EditorViewportWidget::HitTest(const QPoint& point, HitContext& hitInfo) { - hitInfo.camera = nullptr; hitInfo.pExcludedObject = GetCameraObject(); return QtViewport::HitTest(point, hitInfo); } @@ -2363,10 +2282,10 @@ void* EditorViewportWidget::GetSystemCursorConstraintWindow() const return systemCursorConstrained ? renderOverlayHWND() : nullptr; } -void EditorViewportWidget::BuildDragDropContext(AzQtComponents::ViewportDragContext& context, const QPoint& pt) +void EditorViewportWidget::BuildDragDropContext( + AzQtComponents::ViewportDragContext& context, const AzFramework::ViewportId viewportId, const QPoint& point) { - const auto scaledPoint = WidgetToViewport(pt); - QtViewport::BuildDragDropContext(context, scaledPoint); + QtViewport::BuildDragDropContext(context, viewportId, point); } void EditorViewportWidget::RestoreViewportAfterGameMode() @@ -2547,12 +2466,6 @@ bool EditorViewportWidget::ShouldPreviewFullscreen() const return false; } - // Not supported in VR - if (gSettings.bEnableGameModeVR) - { - return false; - } - // If level not loaded, don't preview in fullscreen (preview shouldn't work at all without a level, but it does) if (auto ge = GetIEditor()->GetGameEngine()) { diff --git a/Code/Editor/EditorViewportWidget.h b/Code/Editor/EditorViewportWidget.h index 01f6068d56..0101f2ffef 100644 --- a/Code/Editor/EditorViewportWidget.h +++ b/Code/Editor/EditorViewportWidget.h @@ -10,7 +10,6 @@ #pragma once #if !defined(Q_MOC_RUN) -#include #include @@ -166,13 +165,11 @@ private: Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPoint& point) override; void SetViewportId(int id) override; QPoint WorldToView(const Vec3& wp) const override; - QPoint WorldToViewParticleEditor(const Vec3& wp, int width, int height) const override; Vec3 WorldToView3D(const Vec3& wp, int nFlags = 0) const override; Vec3 ViewToWorld(const QPoint& vp, bool* collideWithTerrain = nullptr, bool onlyTerrain = false, bool bSkipVegetation = false, bool bTestRenderMesh = false, bool* collideWithObject = nullptr) const override; void ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& rayDir) const override; Vec3 ViewToWorldNormal(const QPoint& vp, bool onlyTerrain, bool bTestRenderMesh = false) override; float GetScreenScaleFactor(const Vec3& worldPoint) const override; - float GetScreenScaleFactor(const CCamera& camera, const Vec3& object_position) override; float GetAspectRatio() const override; bool HitTest(const QPoint& point, HitContext& hitInfo) override; bool IsBoundsVisible(const AABB& box) const override; @@ -208,7 +205,6 @@ private: void* GetSystemCursorConstraintWindow() const override; // AzToolsFramework::MainEditorViewportInteractionRequestBus overrides ... - AZ::EntityId PickEntity(const AzFramework::ScreenPoint& point) override; AZ::Vector3 PickTerrain(const AzFramework::ScreenPoint& point) override; float TerrainHeight(const AZ::Vector2& position) override; bool ShowingWorldSpace() override; @@ -275,7 +271,8 @@ private: bool CheckRespondToInput() const; - void BuildDragDropContext(AzQtComponents::ViewportDragContext& context, const QPoint& pt) override; + void BuildDragDropContext( + AzQtComponents::ViewportDragContext& context, AzFramework::ViewportId viewportId, const QPoint& point) override; void SetAsActiveViewport(); void PushDisableRendering(); @@ -306,8 +303,8 @@ private: const DisplayContext& GetDisplayContext() const { return m_displayContext; } CBaseObject* GetCameraObject() const; - void UnProjectFromScreen(float sx, float sy, float sz, float* px, float* py, float* pz) const; - void ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy, float* sz) const; + void UnProjectFromScreen(float sx, float sy, float* px, float* py, float* pz) const; + void ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy) const; AZ::RPI::ViewPtr GetCurrentAtomView() const; diff --git a/Code/Editor/GameEngine.cpp b/Code/Editor/GameEngine.cpp index ff247c1e6c..6f434eda98 100644 --- a/Code/Editor/GameEngine.cpp +++ b/Code/Editor/GameEngine.cpp @@ -822,7 +822,7 @@ void CGameEngine::Update() if (gEnv->pSystem) { gEnv->pSystem->UpdatePreTickBus(); - componentApplication->Tick(gEnv->pTimer->GetFrameTime(ITimer::ETIMER_GAME)); + componentApplication->Tick(); gEnv->pSystem->UpdatePostTickBus(); } @@ -838,7 +838,7 @@ void CGameEngine::Update() unsigned int updateFlags = ESYSUPDATE_EDITOR; GetIEditor()->GetAnimation()->Update(); GetIEditor()->GetSystem()->UpdatePreTickBus(updateFlags); - componentApplication->Tick(gEnv->pTimer->GetFrameTime(ITimer::ETIMER_GAME)); + componentApplication->Tick(); GetIEditor()->GetSystem()->UpdatePostTickBus(updateFlags); } } diff --git a/Code/Editor/Include/HitContext.h b/Code/Editor/Include/HitContext.h index 186124555f..ceff0adb22 100644 --- a/Code/Editor/Include/HitContext.h +++ b/Code/Editor/Include/HitContext.h @@ -19,7 +19,6 @@ class CBaseObject; struct IDisplayViewport; class CDeepSelection; struct AABB; -class CCamera; #include #include @@ -68,8 +67,6 @@ struct HitContext QRect rect; //! Optional limiting bounding box for hit testing. AABB* bounds; - //! Optional camera for culling perspective viewports. - CCamera* camera; //! Testing performed in 2D viewport. bool b2DViewport; @@ -120,7 +117,6 @@ struct HitContext rect = QRect(); b2DViewport = false; view = 0; - camera = 0; point2d = QPoint(); axis = 0; distanceTolerance = 0; diff --git a/Code/Editor/Include/IDisplayViewport.h b/Code/Editor/Include/IDisplayViewport.h index c7dff33e50..132c62804c 100644 --- a/Code/Editor/Include/IDisplayViewport.h +++ b/Code/Editor/Include/IDisplayViewport.h @@ -14,7 +14,6 @@ struct DisplayContext; class CBaseObjectsCache; class QPoint; -class CCamera; struct AABB; class CViewport; @@ -23,7 +22,6 @@ struct IDisplayViewport { virtual void Update() = 0; virtual float GetScreenScaleFactor(const Vec3& position) const = 0; - virtual float GetScreenScaleFactor(const CCamera& camera, const Vec3& object_position) = 0; virtual bool HitTestLine(const Vec3& lineP1, const Vec3& lineP2, const QPoint& hitpoint, int pixelRadius, float* pToCameraDistance = 0) const = 0; /** @@ -47,7 +45,6 @@ struct IDisplayViewport virtual const Matrix34& GetViewTM() const = 0; virtual const Matrix34& GetScreenTM() const = 0; virtual QPoint WorldToView(const Vec3& worldPoint) const = 0; - virtual QPoint WorldToViewParticleEditor(const Vec3& worldPoint, int width, int height) const = 0; virtual Vec3 WorldToView3D(const Vec3& worldPoint, int flags = 0) const = 0; virtual Vec3 ViewToWorld(const QPoint& vp, bool* collideWithTerrain = nullptr, bool onlyTerrain = false, bool bSkipVegetation = false, bool bTestRenderMesh = false, bool* collideWithObject = nullptr) const = 0; virtual void ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& rayDir) const = 0; diff --git a/Code/Editor/Include/IEditorMaterialManager.h b/Code/Editor/Include/IEditorMaterialManager.h index d76ec32829..6f71c5ddd1 100644 --- a/Code/Editor/Include/IEditorMaterialManager.h +++ b/Code/Editor/Include/IEditorMaterialManager.h @@ -9,10 +9,6 @@ #define CRYINCLUDE_EDITOR_MATERIAL_IEDITORMATERIALMANAGER_H #pragma once -#define MATERIAL_FILE_EXT ".mtl" -#define DCC_MATERIAL_FILE_EXT ".dccmtl" -#define MATERIALS_PATH "materials/" - #include #include diff --git a/Code/Editor/Include/IFileUtil.h b/Code/Editor/Include/IFileUtil.h index e179f892d9..036a0bc5ee 100644 --- a/Code/Editor/Include/IFileUtil.h +++ b/Code/Editor/Include/IFileUtil.h @@ -60,7 +60,6 @@ struct IFileUtil EFILE_TYPE_GEOMETRY, EFILE_TYPE_TEXTURE, EFILE_TYPE_SOUND, - EFILE_TYPE_GEOMCACHE, EFILE_TYPE_LAST, }; @@ -114,9 +113,7 @@ struct IFileUtil virtual void ShowInExplorer(const QString& path) = 0; - virtual bool CompileLuaFile(const char* luaFilename) = 0; virtual bool ExtractFile(QString& file, bool bMsgBoxAskForExtraction = true, const char* pDestinationFilename = nullptr) = 0; - virtual void EditTextFile(const char* txtFile, int line = 0, ETextFileType fileType = FILE_TYPE_SCRIPT) = 0; virtual void EditTextureFile(const char* txtureFile, bool bUseGameFolder) = 0; //! dcc filename calculation and extraction sub-routines diff --git a/Code/Editor/LayoutWnd.cpp b/Code/Editor/LayoutWnd.cpp index aa2a008844..ea125ca3d4 100644 --- a/Code/Editor/LayoutWnd.cpp +++ b/Code/Editor/LayoutWnd.cpp @@ -487,7 +487,6 @@ bool CLayoutWnd::LoadConfig() CreateLayout((EViewLayout)layout, false); - bool bRebindViewports = false; if (m_splitWnd) { const QString str = settings.value("Viewports").toString(); @@ -498,14 +497,12 @@ bool CLayoutWnd::LoadConfig() { break; } - bRebindViewports = true; if (!resToken.isEmpty()) { m_viewType[nIndex] = resToken; } nIndex++; } - ; } BindViewports(); diff --git a/Code/Editor/Lib/Tests/Camera/test_EditorCamera.cpp b/Code/Editor/Lib/Tests/Camera/test_EditorCamera.cpp index 637b9c44c5..dd7698a82e 100644 --- a/Code/Editor/Lib/Tests/Camera/test_EditorCamera.cpp +++ b/Code/Editor/Lib/Tests/Camera/test_EditorCamera.cpp @@ -27,7 +27,9 @@ namespace UnitTest AZ::Entity* m_entity = nullptr; AZ::ComponentDescriptor* m_transformComponent = nullptr; - static const AzFramework::ViewportId TestViewportId; + static inline constexpr AzFramework::ViewportId TestViewportId = 2345; + static inline constexpr float HalfInterpolateToTransformDuration = + AtomToolsFramework::ModularViewportCameraControllerRequests::InterpolateToTransformDuration * 0.5f; void SetUp() override { @@ -76,8 +78,6 @@ namespace UnitTest } }; - const AzFramework::ViewportId EditorCameraFixture::TestViewportId = AzFramework::ViewportId(1337); - TEST_F(EditorCameraFixture, ModularViewportCameraControllerReferenceFrameUpdatedWhenViewportEntityisChanged) { // Given @@ -91,8 +91,8 @@ namespace UnitTest &Camera::EditorCameraNotificationBus::Events::OnViewportViewEntityChanged, m_entity->GetId()); // ensure the viewport updates after the viewport view entity change - const float deltaTime = 1.0f / 60.0f; - m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(deltaTime), AZ::ScriptTimePoint() }); + // note: do a large step to ensure smoothing finishes (e.g. not 1.0f/60.0f) + m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(2.0f), AZ::ScriptTimePoint() }); // retrieve updated camera transform const AZ::Transform cameraTransform = m_cameraViewportContextView->GetCameraTransform(); @@ -102,61 +102,40 @@ namespace UnitTest EXPECT_THAT(cameraTransform, IsClose(entityTransform)); } - TEST_F(EditorCameraFixture, ReferenceFrameRemainsIdentityAfterExternalCameraTransformChangeWhenNotSet) + TEST_F(EditorCameraFixture, TrackingTransformIsTrueAfterTransformIsTracked) { - // Given - m_cameraViewportContextView->SetCameraTransform(AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 20.0f, 30.0f))); + // Given/When + const AZ::Transform referenceFrame = AZ::Transform::CreateFromQuaternionAndTranslation( + AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f)), AZ::Vector3(1.0f, 2.0f, 3.0f)); + AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( + TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StartTrackingTransform, referenceFrame); - // When - AZ::Transform referenceFrame = AZ::Transform::CreateTranslation(AZ::Vector3(1.0f, 2.0f, 3.0f)); + bool trackingTransform = false; AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( - referenceFrame, TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::GetReferenceFrame); + trackingTransform, TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::IsTrackingTransform); // Then - // reference frame is still the identity - EXPECT_THAT(referenceFrame, IsClose(AZ::Transform::CreateIdentity())); + EXPECT_THAT(trackingTransform, ::testing::IsTrue()); } - TEST_F(EditorCameraFixture, ExternalCameraTransformChangeWhenReferenceFrameIsSetUpdatesReferenceFrame) + TEST_F(EditorCameraFixture, TrackingTransformIsFalseAfterTransformIsStoppedBeingTracked) { // Given const AZ::Transform referenceFrame = AZ::Transform::CreateFromQuaternionAndTranslation( AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f)), AZ::Vector3(1.0f, 2.0f, 3.0f)); AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::SetReferenceFrame, referenceFrame); - - const AZ::Transform nextTransform = AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 20.0f, 30.0f)); - m_cameraViewportContextView->SetCameraTransform(nextTransform); - - // When - AZ::Transform currentReferenceFrame = AZ::Transform::CreateTranslation(AZ::Vector3(1.0f, 2.0f, 3.0f)); - AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( - currentReferenceFrame, TestViewportId, - &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::GetReferenceFrame); - - // Then - EXPECT_THAT(currentReferenceFrame, IsClose(nextTransform)); - } - - TEST_F(EditorCameraFixture, ReferenceFrameReturnedToIdentityAfterClear) - { - // Given - const AZ::Transform referenceFrame = AZ::Transform::CreateFromQuaternionAndTranslation( - AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f)), AZ::Vector3(1.0f, 2.0f, 3.0f)); - AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::SetReferenceFrame, referenceFrame); + TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StartTrackingTransform, referenceFrame); // When AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::ClearReferenceFrame); - - AZ::Transform currentReferenceFrame = AZ::Transform::CreateTranslation(AZ::Vector3(1.0f, 2.0f, 3.0f)); - AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( - currentReferenceFrame, TestViewportId, - &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::GetReferenceFrame); + TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StopTrackingTransform); // Then - EXPECT_THAT(currentReferenceFrame, IsClose(AZ::Transform::CreateIdentity())); + bool trackingTransform = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + trackingTransform, TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::IsTrackingTransform); + + EXPECT_THAT(trackingTransform, ::testing::IsFalse()); } TEST_F(EditorCameraFixture, InterpolateToTransform) @@ -169,8 +148,10 @@ namespace UnitTest transformToInterpolateTo); // simulate interpolation - m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(0.5f), AZ::ScriptTimePoint() }); - m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(0.5f), AZ::ScriptTimePoint() }); + m_controllerList->UpdateViewport( + { TestViewportId, AzFramework::FloatSeconds(HalfInterpolateToTransformDuration), AZ::ScriptTimePoint() }); + m_controllerList->UpdateViewport( + { TestViewportId, AzFramework::FloatSeconds(HalfInterpolateToTransformDuration), AZ::ScriptTimePoint() }); const auto finalTransform = m_cameraViewportContextView->GetCameraTransform(); @@ -184,7 +165,7 @@ namespace UnitTest const AZ::Transform referenceFrame = AZ::Transform::CreateFromQuaternionAndTranslation( AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f)), AZ::Vector3(1.0f, 2.0f, 3.0f)); AtomToolsFramework::ModularViewportCameraControllerRequestBus::Event( - TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::SetReferenceFrame, referenceFrame); + TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::StartTrackingTransform, referenceFrame); AZ::Transform transformToInterpolateTo = AZ::Transform::CreateFromQuaternionAndTranslation( AZ::Quaternion::CreateRotationZ(AZ::DegToRad(90.0f)), AZ::Vector3(20.0f, 40.0f, 60.0f)); @@ -195,18 +176,85 @@ namespace UnitTest transformToInterpolateTo); // simulate interpolation - m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(0.5f), AZ::ScriptTimePoint() }); - m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(0.5f), AZ::ScriptTimePoint() }); - - AZ::Transform currentReferenceFrame = AZ::Transform::CreateTranslation(AZ::Vector3(1.0f, 2.0f, 3.0f)); - AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( - currentReferenceFrame, TestViewportId, - &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::GetReferenceFrame); + m_controllerList->UpdateViewport( + { TestViewportId, AzFramework::FloatSeconds(HalfInterpolateToTransformDuration), AZ::ScriptTimePoint() }); + m_controllerList->UpdateViewport( + { TestViewportId, AzFramework::FloatSeconds(HalfInterpolateToTransformDuration), AZ::ScriptTimePoint() }); const auto finalTransform = m_cameraViewportContextView->GetCameraTransform(); // Then EXPECT_THAT(finalTransform, IsClose(transformToInterpolateTo)); - EXPECT_THAT(currentReferenceFrame, IsClose(AZ::Transform::CreateIdentity())); + } + + TEST_F(EditorCameraFixture, BeginningCameraInterpolationReturnsTrue) + { + // Given/When + bool interpolationBegan = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + interpolationBegan, TestViewportId, + &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::InterpolateToTransform, + AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 10.0f))); + + // Then + EXPECT_THAT(interpolationBegan, ::testing::IsTrue()); + } + + TEST_F(EditorCameraFixture, CameraInterpolationDoesNotBeginDuringAnExistingInterpolation) + { + // Given/When + bool initialInterpolationBegan = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + initialInterpolationBegan, TestViewportId, + &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::InterpolateToTransform, + AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 10.0f))); + + m_controllerList->UpdateViewport( + { TestViewportId, AzFramework::FloatSeconds(HalfInterpolateToTransformDuration), AZ::ScriptTimePoint() }); + + bool nextInterpolationBegan = true; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + nextInterpolationBegan, TestViewportId, + &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::InterpolateToTransform, + AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 10.0f))); + + bool interpolating = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + interpolating, TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::IsInterpolating); + + // Then + EXPECT_THAT(initialInterpolationBegan, ::testing::IsTrue()); + EXPECT_THAT(nextInterpolationBegan, ::testing::IsFalse()); + EXPECT_THAT(interpolating, ::testing::IsTrue()); + } + + TEST_F(EditorCameraFixture, CameraInterpolationCanBeginAfterAnInterpolationCompletes) + { + // Given/When + bool initialInterpolationBegan = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + initialInterpolationBegan, TestViewportId, + &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::InterpolateToTransform, + AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 10.0f))); + + m_controllerList->UpdateViewport( + { TestViewportId, + AzFramework::FloatSeconds(AtomToolsFramework::ModularViewportCameraControllerRequests::InterpolateToTransformDuration + 0.5f), + AZ::ScriptTimePoint() }); + + bool interpolating = true; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + interpolating, TestViewportId, &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::IsInterpolating); + + bool nextInterpolationBegan = false; + AtomToolsFramework::ModularViewportCameraControllerRequestBus::EventResult( + nextInterpolationBegan, TestViewportId, + &AtomToolsFramework::ModularViewportCameraControllerRequestBus::Events::InterpolateToTransform, + AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 10.0f))); + + // Then + EXPECT_THAT(initialInterpolationBegan, ::testing::IsTrue()); + EXPECT_THAT(interpolating, ::testing::IsFalse()); + EXPECT_THAT(nextInterpolationBegan, ::testing::IsTrue()); } } // namespace UnitTest diff --git a/Code/Editor/Lib/Tests/test_EditorPythonBindings.cpp b/Code/Editor/Lib/Tests/test_EditorPythonBindings.cpp index 1006c339ee..de0eb5df6f 100644 --- a/Code/Editor/Lib/Tests/test_EditorPythonBindings.cpp +++ b/Code/Editor/Lib/Tests/test_EditorPythonBindings.cpp @@ -25,7 +25,6 @@ #include #include #include -#include #include #include "IEditorMock.h" diff --git a/Code/Editor/Lib/Tests/test_ModularViewportCameraController.cpp b/Code/Editor/Lib/Tests/test_ModularViewportCameraController.cpp index 275df11784..67d89ad967 100644 --- a/Code/Editor/Lib/Tests/test_ModularViewportCameraController.cpp +++ b/Code/Editor/Lib/Tests/test_ModularViewportCameraController.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -74,7 +74,7 @@ namespace UnitTest class ModularViewportCameraControllerFixture : public AllocatorsTestFixture { public: - static const AzFramework::ViewportId TestViewportId; + static inline constexpr AzFramework::ViewportId TestViewportId = 1234; void SetUp() override { @@ -146,6 +146,17 @@ namespace UnitTest controller->SetCameraPropsBuilderCallback( [](AzFramework::CameraProps& cameraProps) { + // note: rotateSmoothness is also used for roll (not related to camera input directly) + cameraProps.m_rotateSmoothnessFn = [] + { + return 5.0f; + }; + + cameraProps.m_translateSmoothnessFn = [] + { + return 5.0f; + }; + cameraProps.m_rotateSmoothingEnabledFn = [] { return false; @@ -209,8 +220,6 @@ namespace UnitTest AZStd::unique_ptr m_editorModularViewportCameraComposer; }; - const AzFramework::ViewportId ModularViewportCameraControllerFixture::TestViewportId = AzFramework::ViewportId(0); - TEST_F(ModularViewportCameraControllerFixture, MouseMovementDoesNotAccumulateExcessiveDriftInModularViewportCameraWithVaryingDeltaTime) { SandboxEditor::SetCameraCaptureCursorForLook(false); @@ -380,6 +389,7 @@ namespace UnitTest m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(deltaTime), AZ::ScriptTimePoint() }); QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::RightButton, Qt::NoModifier, start + mouseDelta); + m_controllerList->UpdateViewport({ TestViewportId, AzFramework::FloatSeconds(deltaTime), AZ::ScriptTimePoint() }); // update the position of the widget const auto offset = QPoint(500, 500); diff --git a/Code/Editor/Lib/Tests/test_TrackViewPythonBindings.cpp b/Code/Editor/Lib/Tests/test_TrackViewPythonBindings.cpp index 191596f4ae..88a4b4f63f 100644 --- a/Code/Editor/Lib/Tests/test_TrackViewPythonBindings.cpp +++ b/Code/Editor/Lib/Tests/test_TrackViewPythonBindings.cpp @@ -94,6 +94,9 @@ namespace TrackViewPythonBindingsUnitTests m_app.Start(appDesc); m_app.RegisterComponentDescriptor(AzToolsFramework::TrackViewComponent::CreateDescriptor()); + + // Disable saving global user settings to prevent failure due to detecting file updates + AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize); } void TearDown() override diff --git a/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp b/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp index 63e59ea940..473ddd81f7 100644 --- a/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp +++ b/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp @@ -8,10 +8,11 @@ #include #include -#include +#include #include #include #include +#include namespace UnitTest { @@ -77,14 +78,15 @@ namespace UnitTest class ViewportManipulatorControllerFixture : public AllocatorsTestFixture { public: - static const AzFramework::ViewportId TestViewportId; + static inline constexpr AzFramework::ViewportId TestViewportId = 1234; + static inline const QSize WidgetSize = QSize(1920, 1080); void SetUp() override { AllocatorsTestFixture::SetUp(); m_rootWidget = AZStd::make_unique(); - m_rootWidget->setFixedSize(QSize(100, 100)); + m_rootWidget->setFixedSize(WidgetSize); QApplication::setActiveWindow(m_rootWidget.get()); m_controllerList = AZStd::make_shared(); @@ -111,8 +113,6 @@ namespace UnitTest AZStd::unique_ptr m_inputChannelMapper; }; - const AzFramework::ViewportId ViewportManipulatorControllerFixture::TestViewportId = AzFramework::ViewportId(0); - TEST_F(ViewportManipulatorControllerFixture, AnEventIsNotPropagatedToTheViewportWhenAManipulatorHandlesItFirst) { // forward input events to our controller list @@ -227,4 +227,74 @@ namespace UnitTest // the key was released (cleared) EXPECT_TRUE(endedEvent); } + + TEST_F(ViewportManipulatorControllerFixture, DoubleClickIsNotRegisteredIfMouseDeltaHasMovedMoreThanDeadzoneInClickInterval) + { + AzFramework::NativeWindowHandle nativeWindowHandle = nullptr; + + // forward input events to our controller list + QObject::connect( + m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(), + [this, nativeWindowHandle](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event) + { + m_controllerList->HandleInputChannelEvent( + AzFramework::ViewportControllerInputEvent{ TestViewportId, nativeWindowHandle, *inputChannel }); + }); + + ::testing::NiceMock mockWindowRequests; + mockWindowRequests.Connect(nativeWindowHandle); + + using ::testing::Return; + // note: WindowRequests is used internally by ViewportManipulatorController + ON_CALL(mockWindowRequests, GetClientAreaSize()) + .WillByDefault(Return(AzFramework::WindowSize(WidgetSize.width(), WidgetSize.height()))); + + EditorInteractionViewportSelectionFake editorInteractionViewportFake; + editorInteractionViewportFake.m_internalHandleMouseManipulatorInteraction = [](const MouseInteractionEvent&) + { + // report the event was not handled (manipulator was not interacted with) + return false; + }; + + bool doubleClickDetected = false; + editorInteractionViewportFake.m_internalHandleMouseViewportInteraction = + [&doubleClickDetected](const MouseInteractionEvent& mouseInteractionEvent) + { + // ensure no double click event is detected with the given inputs below + if (mouseInteractionEvent.m_mouseEvent == AzToolsFramework::ViewportInteraction::MouseEvent::DoubleClick) + { + doubleClickDetected = true; + } + + return true; + }; + + editorInteractionViewportFake.Connect(); + + m_controllerList->Add(AZStd::make_shared()); + + // simulate a click, move, click + MouseMove(m_rootWidget.get(), QPoint(0, 0), QPoint(10, 10)); + MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton); + QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10)); + MouseMove(m_rootWidget.get(), QPoint(10, 10), QPoint(20, 20)); + MousePressAndMove(m_rootWidget.get(), QPoint(20, 20), QPoint(0, 0), Qt::MouseButton::LeftButton); + QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(20, 20)); + + // ensure no double click was detected + EXPECT_FALSE(doubleClickDetected); + + // simulate double click (sanity check it still is detected correctly with no movement) + MouseMove(m_rootWidget.get(), QPoint(0, 0), QPoint(10, 10)); + MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton); + QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10)); + MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton); + QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10)); + + // ensure a double click was detected + EXPECT_TRUE(doubleClickDetected); + + mockWindowRequests.Disconnect(); + editorInteractionViewportFake.Disconnect(); + } } // namespace UnitTest diff --git a/Code/Editor/MainWindow.cpp b/Code/Editor/MainWindow.cpp index 528b4eba6b..f6744ec2d2 100644 --- a/Code/Editor/MainWindow.cpp +++ b/Code/Editor/MainWindow.cpp @@ -519,7 +519,7 @@ MainWindow* MainWindow::instance() void MainWindow::closeEvent(QCloseEvent* event) { - gSettings.Save(); + gSettings.Save(true); AzFramework::SystemCursorState currentCursorState; bool isInGameMode = false; @@ -708,14 +708,10 @@ void MainWindow::InitActions() .SetShortcut(QKeySequence::Undo) .SetReserved() .SetStatusTip(tr("Undo last operation")) - //.SetMenu(new QMenu("FIXME")) - .SetApplyHoverEffect() .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnUpdateUndo); am->AddAction(ID_REDO, tr("&Redo")) .SetShortcut(AzQtComponents::RedoKeySequence) .SetReserved() - //.SetMenu(new QMenu("FIXME")) - .SetApplyHoverEffect() .SetStatusTip(tr("Redo last undo operation")) .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnUpdateRedo); @@ -731,7 +727,6 @@ void MainWindow::InitActions() // Modify actions am->AddAction(AzToolsFramework::EditModeMove, tr("Move")) .SetIcon(Style::icon("Move")) - .SetApplyHoverEffect() .SetShortcut(tr("1")) .SetToolTip(tr("Move (1)")) .SetCheckable(true) @@ -757,7 +752,6 @@ void MainWindow::InitActions() }); am->AddAction(AzToolsFramework::EditModeRotate, tr("Rotate")) .SetIcon(Style::icon("Translate")) - .SetApplyHoverEffect() .SetShortcut(tr("2")) .SetToolTip(tr("Rotate (2)")) .SetCheckable(true) @@ -783,7 +777,6 @@ void MainWindow::InitActions() }); am->AddAction(AzToolsFramework::EditModeScale, tr("Scale")) .SetIcon(Style::icon("Scale")) - .SetApplyHoverEffect() .SetShortcut(tr("3")) .SetToolTip(tr("Scale (3)")) .SetCheckable(true) @@ -808,7 +801,6 @@ void MainWindow::InitActions() am->AddAction(AzToolsFramework::SnapToGrid, tr("Snap to grid")) .SetIcon(Style::icon("Grid")) - .SetApplyHoverEffect() .SetShortcut(tr("G")) .SetToolTip(tr("Snap to grid (G)")) .SetStatusTip(tr("Toggles snap to grid")) @@ -821,7 +813,6 @@ void MainWindow::InitActions() am->AddAction(AzToolsFramework::SnapAngle, tr("Snap angle")) .SetIcon(Style::icon("Angle")) - .SetApplyHoverEffect() .SetStatusTip(tr("Snap angle")) .SetCheckable(true) .RegisterUpdateCallback([](QAction* action) { @@ -939,29 +930,28 @@ void MainWindow::InitActions() .Connect(&QAction::triggered, this, &MainWindow::OnRefreshAudioSystem); // Game actions - am->AddAction(ID_VIEW_SWITCHTOGAME, tr("Play &Game")) + am->AddAction(ID_VIEW_SWITCHTOGAME, tr("Play Game")) .SetIcon(QIcon(":/stylesheet/img/UI20/toolbar/Play.svg")) + .SetToolTip(tr("Play Game")) + .SetStatusTip(tr("Activate the game input mode")) + .SetCheckable(true) + .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnUpdatePlayGame); + am->AddAction(ID_VIEW_SWITCHTOGAME_VIEWPORT, tr("Play Game")) .SetShortcut(tr("Ctrl+G")) .SetToolTip(tr("Play Game (Ctrl+G)")) .SetStatusTip(tr("Activate the game input mode")) - .SetApplyHoverEffect() - .SetCheckable(true) .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnUpdatePlayGame); - am->AddAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN, tr("Play &Game (Maximized)")) + am->AddAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN, tr("Play Game (Maximized)")) .SetShortcut(tr("Ctrl+Shift+G")) .SetStatusTip(tr("Activate the game input mode (maximized)")) - .SetIcon(Style::icon("Play")) - .SetApplyHoverEffect() - .SetCheckable(true); + .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnUpdatePlayGame); am->AddAction(ID_TOOLBAR_WIDGET_PLAYCONSOLE_LABEL, tr("Play Controls")) .SetText(tr("Play Controls")); am->AddAction(ID_SWITCH_PHYSICS, tr("Simulate")) .SetIcon(QIcon(":/stylesheet/img/UI20/toolbar/Simulate_Physics.svg")) .SetShortcut(tr("Ctrl+P")) .SetToolTip(tr("Simulate (Ctrl+P)")) - .SetCheckable(true) .SetStatusTip(tr("Enable processing of Physics and AI.")) - .SetApplyHoverEffect() .SetCheckable(true) .RegisterUpdateCallback(cryEdit, &CCryEditApp::OnSwitchPhysicsUpdate); am->AddAction(ID_GAME_SYNCPLAYER, tr("Move Player and Camera Separately")).SetCheckable(true) @@ -1051,8 +1041,7 @@ void MainWindow::InitActions() // Editors Toolbar actions am->AddAction(ID_OPEN_ASSET_BROWSER, tr("Asset browser")) - .SetToolTip(tr("Open Asset Browser")) - .SetApplyHoverEffect(); + .SetToolTip(tr("Open Asset Browser")); AZ::EBusReduceResult> emfxEnabled(false); using AnimationRequestBus = AzToolsFramework::EditorAnimationSystemRequestsBus; @@ -1062,8 +1051,7 @@ void MainWindow::InitActions() { QAction* action = am->AddAction(ID_OPEN_EMOTIONFX_EDITOR, tr("Animation Editor")) .SetToolTip(tr("Open Animation Editor")) - .SetIcon(QIcon(":/EMotionFX/EMFX_icon_32x32.png")) - .SetApplyHoverEffect(); + .SetIcon(QIcon(":/EMotionFX/EMFX_icon_32x32.png")); QObject::connect(action, &QAction::triggered, this, []() { QtViewPaneManager::instance()->OpenPane(LyViewPane::AnimationEditor); }); @@ -1071,12 +1059,10 @@ void MainWindow::InitActions() am->AddAction(ID_OPEN_AUDIO_CONTROLS_BROWSER, tr("Audio Controls Editor")) .SetToolTip(tr("Open Audio Controls Editor")) - .SetIcon(Style::icon("Audio")) - .SetApplyHoverEffect(); + .SetIcon(Style::icon("Audio")); am->AddAction(ID_OPEN_UICANVASEDITOR, tr(LyViewPane::UiEditor)) - .SetToolTip(tr("Open UI Editor")) - .SetApplyHoverEffect(); + .SetToolTip(tr("Open UI Editor")); // Edit Mode Toolbar Actions am->AddAction(IDC_SELECTION_MASK, tr("Selected Object Types")); @@ -1089,12 +1075,10 @@ void MainWindow::InitActions() // Object Toolbar Actions am->AddAction(ID_GOTO_SELECTED, tr("Go to selected object")) .SetIcon(Style::icon("select_object")) - .SetApplyHoverEffect() .Connect(&QAction::triggered, this, &MainWindow::OnGotoSelected); // Misc Toolbar Actions - am->AddAction(ID_OPEN_SUBSTANCE_EDITOR, tr("Open Substance Editor")) - .SetApplyHoverEffect(); + am->AddAction(ID_OPEN_SUBSTANCE_EDITOR, tr("Open Substance Editor")); } void MainWindow::InitToolActionHandlers() @@ -1266,7 +1250,9 @@ void MainWindow::OnGameModeChanged(bool inGameMode) // block signals on the switch to game actions before setting the checked state, as // setting the checked state triggers the action, which will re-enter this function // and result in an infinite loop - AZStd::vector actions = { m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME), m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN) }; + AZStd::vector actions = { m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME_VIEWPORT), + m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN), + m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME)}; for (auto action : actions) { action->blockSignals(true); diff --git a/Code/Editor/Objects/AxisGizmo.cpp b/Code/Editor/Objects/AxisGizmo.cpp index a603b2615d..7215853854 100644 --- a/Code/Editor/Objects/AxisGizmo.cpp +++ b/Code/Editor/Objects/AxisGizmo.cpp @@ -329,7 +329,6 @@ bool CAxisGizmo::MouseCallback(CViewport* view, EMouseEvent event, QPoint& point hc.b2DViewport = view->GetType() != ET_ViewportCamera; hc.point2d = point; view->ViewToWorldRay(point, hc.raySrc, hc.rayDir); - bool bHit = false; if (HitTest(hc)) { switch (hc.manipulatorMode) @@ -344,7 +343,6 @@ bool CAxisGizmo::MouseCallback(CViewport* view, EMouseEvent event, QPoint& point view->SetCurrentCursor(STD_CURSOR_SCALE); break; } - bHit = true; } } } diff --git a/Code/Editor/Objects/BaseObject.cpp b/Code/Editor/Objects/BaseObject.cpp index 4ae01faddf..b721dba56b 100644 --- a/Code/Editor/Objects/BaseObject.cpp +++ b/Code/Editor/Objects/BaseObject.cpp @@ -953,14 +953,8 @@ void CBaseObject::DrawTextureIcon(DisplayContext& dc, [[maybe_unused]] const Vec } ////////////////////////////////////////////////////////////////////////// -void CBaseObject::DrawWarningIcons(DisplayContext& dc, const Vec3& pos) +void CBaseObject::DrawWarningIcons(DisplayContext& dc, const Vec3&) { - // Don't draw warning icons if they are beyond draw distance - if ((dc.camera->GetPosition() - pos).GetLength() > gSettings.viewports.fWarningIconsDrawDistance) - { - return; - } - if (gSettings.viewports.bShowIcons || gSettings.viewports.bShowSizeBasedIcons) { const int warningIconSizeX = OBJECT_TEXTURE_ICON_SIZEX / 2; @@ -1010,11 +1004,8 @@ void CBaseObject::DrawLabel(DisplayContext& dc, const Vec3& pos, const QColor& l labelColor = QColor(0, 0, 0); } - float camDist = dc.camera->GetPosition().GetDistance(pos); - float maxDist = dc.settings->GetLabelsDistance(); - if (camDist < dc.settings->GetLabelsDistance() || (dc.flags & DISPLAY_SELECTION_HELPERS)) + if (dc.flags & DISPLAY_SELECTION_HELPERS) { - float range = maxDist / 2.0f; Vec3 c(static_cast(labelColor.redF()), static_cast(labelColor.greenF()), static_cast(labelColor.redF())); if (IsSelected()) { @@ -1032,10 +1023,6 @@ void CBaseObject::DrawLabel(DisplayContext& dc, const Vec3& pos, const QColor& l col[1] = c.y; col[2] = c.z; } - else if (camDist > range) - { - col[3] = col[3] * (1.0f - (camDist - range) / range); - } dc.SetColor(col[0], col[1], col[2], col[3] * alpha); dc.DrawTextLabel(pos, size, GetName().toUtf8().data()); @@ -1196,33 +1183,6 @@ bool CBaseObject::CanBeDrawn(const DisplayContext& dc, bool& outDisplaySelection return bResult; } -////////////////////////////////////////////////////////////////////////// -bool CBaseObject::IsInCameraView(const CCamera& camera) -{ - AABB bbox; - GetBoundBox(bbox); - return (camera.IsAABBVisible_F(AABB(bbox.min, bbox.max))); -} - -////////////////////////////////////////////////////////////////////////// -float CBaseObject::GetCameraVisRatio(const CCamera& camera) -{ - AABB bbox; - GetBoundBox(bbox); - - static const float defaultVisRatio = 1000.0f; - - const float objectHeightSq = max(1.0f, (bbox.max - bbox.min).GetLengthSquared()); - const float camdistSq = (bbox.min - camera.GetPosition()).GetLengthSquared(); - float visRatio = defaultVisRatio; - if (camdistSq > FLT_EPSILON) - { - visRatio = objectHeightSq / camdistSq; - } - - return visRatio; -} - ////////////////////////////////////////////////////////////////////////// int CBaseObject::MouseCreateCallback(CViewport* view, EMouseEvent event, QPoint& point, int flags) { @@ -1689,7 +1649,7 @@ QString CBaseObject::GetTypeName() const } QString name; - name.append(className.mid(0, className.length() - subClassName.length())); + name.append(className.midRef(0, className.length() - subClassName.length())); return name; } diff --git a/Code/Editor/Objects/BaseObject.h b/Code/Editor/Objects/BaseObject.h index 3865696ecb..ab14437659 100644 --- a/Code/Editor/Objects/BaseObject.h +++ b/Code/Editor/Objects/BaseObject.h @@ -674,11 +674,6 @@ protected: //! Returns if the object can be drawn, and if its selection helper should also be drawn. bool CanBeDrawn(const DisplayContext& dc, bool& outDisplaySelectionHelper) const; - //! Returns if object is in the camera view. - virtual bool IsInCameraView(const CCamera& camera); - //! Returns vis ratio of object in camera - virtual float GetCameraVisRatio(const CCamera& camera); - // Do basic intersection tests virtual bool IntersectRectBounds(const AABB& bbox); virtual bool IntersectRayBounds(const Ray& ray); diff --git a/Code/Editor/Objects/DisplayContext.h b/Code/Editor/Objects/DisplayContext.h index 0f0f0e665a..f4df4f95c6 100644 --- a/Code/Editor/Objects/DisplayContext.h +++ b/Code/Editor/Objects/DisplayContext.h @@ -31,7 +31,6 @@ struct IRenderer; struct IRenderAuxGeom; struct IIconManager; class CDisplaySettings; -class CCamera; class QPoint; enum DisplayFlags @@ -66,7 +65,6 @@ struct SANDBOX_API DisplayContext IDisplayViewport* view; IRenderAuxGeom* pRenderAuxGeom; IIconManager* pIconManager; - CCamera* camera; AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING AABB box; // Bounding box of volume that need to be repainted. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING diff --git a/Code/Editor/Objects/DisplayContextShared.inl b/Code/Editor/Objects/DisplayContextShared.inl index df0b1f82a1..e884f874e0 100644 --- a/Code/Editor/Objects/DisplayContextShared.inl +++ b/Code/Editor/Objects/DisplayContextShared.inl @@ -1138,10 +1138,6 @@ bool DisplayContext::IsVisible(const AABB& bounds) return true; } } - else - { - return camera->IsAABBVisible_F(AABB(bounds.min, bounds.max)); - } return false; } diff --git a/Code/Editor/Objects/EntityObject.cpp b/Code/Editor/Objects/EntityObject.cpp index 6e2354998c..ae847da160 100644 --- a/Code/Editor/Objects/EntityObject.cpp +++ b/Code/Editor/Objects/EntityObject.cpp @@ -592,11 +592,11 @@ void CEntityObject::AdjustLightProperties(CVarBlockPtr& properties, const char* if (IVariable* pCastShadowVarLegacy = FindVariableInSubBlock(properties, pSubBlockVar, "bCastShadow")) { pCastShadowVarLegacy->SetFlags(pCastShadowVarLegacy->GetFlags() | IVariable::UI_INVISIBLE); - - if (pCastShadowVarLegacy->GetDisplayValue()[0] != '0') + const QString zeroPrefix("0"); + if (!pCastShadowVarLegacy->GetDisplayValue().startsWith(zeroPrefix)) { bCastShadowLegacy = true; - pCastShadowVarLegacy->SetDisplayValue("0"); + pCastShadowVarLegacy->SetDisplayValue(zeroPrefix); } } @@ -956,11 +956,7 @@ void CEntityObject::Serialize(CObjectArchive& ar) QString attachmentType; xmlNode->getAttr("AttachmentType", attachmentType); - if (attachmentType == "GeomCacheNode") - { - m_attachmentType = eAT_GeomCacheNode; - } - else if (attachmentType == "CharacterBone") + if (attachmentType == "CharacterBone") { m_attachmentType = eAT_CharacterBone; } @@ -987,11 +983,7 @@ void CEntityObject::Serialize(CObjectArchive& ar) { if (m_attachmentType != eAT_Pivot) { - if (m_attachmentType == eAT_GeomCacheNode) - { - xmlNode->setAttr("AttachmentType", "GeomCacheNode"); - } - else if (m_attachmentType == eAT_CharacterBone) + if (m_attachmentType == eAT_CharacterBone) { xmlNode->setAttr("AttachmentType", "CharacterBone"); } @@ -1091,11 +1083,7 @@ XmlNodeRef CEntityObject::Export([[maybe_unused]] const QString& levelPath, XmlN objNode->setAttr("ParentId", parentEntity->GetEntityId()); if (m_attachmentType != eAT_Pivot) { - if (m_attachmentType == eAT_GeomCacheNode) - { - objNode->setAttr("AttachmentType", "GeomCacheNode"); - } - else if (m_attachmentType == eAT_CharacterBone) + if (m_attachmentType == eAT_CharacterBone) { objNode->setAttr("AttachmentType", "CharacterBone"); } diff --git a/Code/Editor/Objects/EntityObject.h b/Code/Editor/Objects/EntityObject.h index dcc6ff7b22..a4f4752b75 100644 --- a/Code/Editor/Objects/EntityObject.h +++ b/Code/Editor/Objects/EntityObject.h @@ -131,7 +131,6 @@ public: enum EAttachmentType { eAT_Pivot, - eAT_GeomCacheNode, eAT_CharacterBone, }; diff --git a/Code/Editor/Objects/LineGizmo.cpp b/Code/Editor/Objects/LineGizmo.cpp index 31ea341f5b..991f449cd6 100644 --- a/Code/Editor/Objects/LineGizmo.cpp +++ b/Code/Editor/Objects/LineGizmo.cpp @@ -98,16 +98,8 @@ void CLineGizmo::Display(DisplayContext& dc) Vec3 pos = 0.5f * (m_point[0] + m_point[1]); //dc.renderer->DrawLabelEx( p3+Vec3(0,0,0.3f),1.2f,col,true,true,m_name ); - float camDist = dc.camera->GetPosition().GetDistance(pos); - float maxDist = dc.settings->GetLabelsDistance(); - if (camDist < dc.settings->GetLabelsDistance()) { - float range = maxDist / 2.0f; float col[4] = { m_color[0].r, m_color[0].g, m_color[0].b, m_color[0].a }; - if (camDist > range) - { - col[3] = col[3] * (1.0f - (camDist - range) / range); - } dc.SetColor(col[0], col[1], col[2], col[3]); dc.DrawTextLabel(pos + Vec3(0, 0, 0.2f), 1.2f, m_name.toUtf8().data()); } diff --git a/Code/Editor/Objects/ObjectManager.cpp b/Code/Editor/Objects/ObjectManager.cpp index ee7e9a8e96..fb5b2b68c2 100644 --- a/Code/Editor/Objects/ObjectManager.cpp +++ b/Code/Editor/Objects/ObjectManager.cpp @@ -186,17 +186,6 @@ CBaseObject* CObjectManager::NewObject(CObjectClassDesc* cls, CBaseObject* prev, if (obj->GetType() != OBJTYPE_AZENTITY) { GetIEditor()->RecordUndo(new CUndoBaseObjectNew(obj)); - - // check for script entities - const char* scriptClassName = ""; - CEntityObject* entityObj = qobject_cast(obj); - QByteArray entityClass; // Leave it outside of the if. Otherwise buffer is deleted. - if (entityObj) - { - entityClass = entityObj->GetEntityClass().toUtf8(); - scriptClassName = entityClass.data(); - } - } } @@ -608,7 +597,7 @@ bool CObjectManager::AddObject(CBaseObject* obj) if (CEntityObject* entityObj = qobject_cast(obj)) { CEntityObject::EAttachmentType attachType = entityObj->GetAttachType(); - if (attachType == CEntityObject::EAttachmentType::eAT_GeomCacheNode || attachType == CEntityObject::EAttachmentType::eAT_CharacterBone) + if (attachType == CEntityObject::EAttachmentType::eAT_CharacterBone) { m_animatedAttachedEntities.insert(entityObj); } @@ -828,7 +817,7 @@ void CObjectManager::ShowLastHiddenObject() { uint64 mostRecentID = CBaseObject::s_invalidHiddenID; CBaseObject* mostRecentObject = nullptr; - for (auto it : m_objects) + for (const auto& it : m_objects) { CBaseObject* obj = it.second; @@ -1488,11 +1477,7 @@ bool CObjectManager::HitTestObject(CBaseObject* obj, HitContext& hc) if (!bSelectionHelperHit) { // Fast checking. - if (hc.camera && !obj->IsInCameraView(*hc.camera)) - { - return false; - } - else if (hc.bounds && !obj->IntersectRectBounds(*hc.bounds)) + if (hc.bounds && !obj->IntersectRectBounds(*hc.bounds)) { return false; } diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp index 82d9f9ede2..4961e7004d 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -57,6 +56,7 @@ #include #include #include +#include #include #include @@ -498,13 +498,11 @@ void SandboxIntegrationManager::EntityParentChanged( // before finally being saved, it will result in all of those layers saving, too. AZ::EntityId oldAncestor = oldParentId; - bool wasNotInLayer = false; AZ::EntityId oldLayer; do { if (!oldAncestor.IsValid()) { - wasNotInLayer = true; break; } @@ -530,13 +528,11 @@ void SandboxIntegrationManager::EntityParentChanged( AZ::EntityId newAncestor = newParentId; - bool isGoingToRootScene = false; AZ::EntityId newLayer; do { if (!newAncestor.IsValid()) { - isGoingToRootScene = true; break; } @@ -1394,13 +1390,13 @@ void SandboxIntegrationManager::ContextMenu_NewEntity() { AZ::Vector3 worldPosition = AZ::Vector3::CreateZero(); - CViewport* view = GetIEditor()->GetViewManager()->GetGameViewport(); // If we don't have a viewport active to aid in placement, the object // will be created at the origin. - if (view) + if (CViewport* view = GetIEditor()->GetViewManager()->GetGameViewport()) { - const QPoint viewPoint(static_cast(m_contextMenuViewPoint.GetX()), static_cast(m_contextMenuViewPoint.GetY())); - worldPosition = view->GetHitLocation(viewPoint); + worldPosition = AzToolsFramework::FindClosestPickIntersection( + view->GetViewportId(), AzFramework::ScreenPointFromVector2(m_contextMenuViewPoint), AzToolsFramework::EditorPickRayLength, + GetDefaultEntityPlacementDistance()); } CreateNewEntityAtPosition(worldPosition); @@ -1675,6 +1671,12 @@ void SandboxIntegrationManager::GoToEntitiesInViewports(const AzToolsFramework:: if (auto viewportContext = viewportContextManager->GetViewportContextById(viewIndex)) { const AZ::Transform cameraTransform = viewportContext->GetCameraTransform(); + // do not attempt to interpolate to where we currently are + if (cameraTransform.GetTranslation().IsClose(center)) + { + continue; + } + const AZ::Vector3 forward = (center - cameraTransform.GetTranslation()).GetNormalized(); // move camera 25% further back than required diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.cpp index df8d6db4a8..60fda239dc 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.cpp @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -136,14 +135,6 @@ AssetCatalogModel::AssetCatalogModel(QObject* parent) } } - // Special cases for SimpleAssets. If these get full-fledged AssetData types, these cases can be removed. - QString textureExtensions = LmbrCentral::TextureAsset::GetFileFilter(); - m_extensionToAssetType.insert(AZStd::make_pair(textureExtensions.replace("*", "").replace(" ", "").toStdString().c_str(), AZStd::vector { AZ::AzTypeInfo::Uuid() })); - QString materialExtensions = LmbrCentral::MaterialAsset::GetFileFilter(); - m_extensionToAssetType.insert(AZStd::make_pair(materialExtensions.replace("*", "").replace(" ", "").toStdString().c_str(), AZStd::vector { AZ::AzTypeInfo::Uuid() })); - QString dccMaterialExtensions = LmbrCentral::DccMaterialAsset::GetFileFilter(); - m_extensionToAssetType.insert(AZStd::make_pair(dccMaterialExtensions.replace("*", "").replace(" ", "").toStdString().c_str(), AZStd::vector { AZ::AzTypeInfo::Uuid() })); - AZ::SerializeContext* serializeContext = nullptr; EBUS_EVENT_RESULT(serializeContext, AZ::ComponentApplicationBus, GetSerializeContext); AZ_Assert(serializeContext, "Failed to acquire application serialize context."); diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerListModel.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerListModel.cpp index c25248d4f6..08d79adcf5 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerListModel.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerListModel.cpp @@ -2640,7 +2640,7 @@ QSize OutlinerItemDelegate::sizeHint(const QStyleOptionViewItem& option, const Q m_cachedBoundingRectOfTallCharacter = QRect(); }; - QTimer::singleShot(0, resetFunction); + QTimer::singleShot(0, this, resetFunction); } // And add 8 to it gives the outliner roughly the visible spacing we're looking for. diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerWidget.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerWidget.cpp index 803deb3509..af58ac4b6b 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerWidget.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerWidget.cpp @@ -121,6 +121,18 @@ namespace SortEntityChildrenRecursively(childId, comparer); } } + + QModelIndex nextIndexForTree(bool direction, OutlinerTreeView *tree, QModelIndex current) + { + if (direction) + { + return tree->indexAbove(current); + } + else + { + return tree->indexBelow(current); + } + } } OutlinerWidget::OutlinerWidget(QWidget* pParent, Qt::WindowFlags flags) @@ -891,9 +903,7 @@ void OutlinerWidget::DoSelectSliceRootNextToSelection(bool isTraversalUpwards) return; } - AZStd::function getNextIdxFunction = - AZStd::bind(isTraversalUpwards ? &QTreeView::indexAbove : &QTreeView::indexBelow, treeView, AZStd::placeholders::_1); - QModelIndex nextIdx = getNextIdxFunction(currentIdx); + QModelIndex nextIdx = nextIndexForTree(isTraversalUpwards,treeView,currentIdx); bool foundSliceRoot = false; while (nextIdx.isValid() && !foundSliceRoot) @@ -904,7 +914,7 @@ void OutlinerWidget::DoSelectSliceRootNextToSelection(bool isTraversalUpwards) AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult( foundSliceRoot, &AzToolsFramework::ToolsApplicationRequests::IsSliceRootEntity, currentEntityId); - nextIdx = getNextIdxFunction(currentIdx); + nextIdx = nextIndexForTree(isTraversalUpwards, treeView, currentIdx); } if (foundSliceRoot) @@ -934,13 +944,10 @@ void OutlinerWidget::DoSelectEdgeSliceRoot(bool shouldSelectTopMostSlice) } QModelIndex currentIdx; - AZStd::function getNextIdxFunction; + if (shouldSelectTopMostSlice) { currentIdx = itemModel->index(0, OutlinerListModel::ColumnName); - - getNextIdxFunction = - AZStd::bind(&QTreeView::indexBelow, treeView, AZStd::placeholders::_1); } else { @@ -949,9 +956,6 @@ void OutlinerWidget::DoSelectEdgeSliceRoot(bool shouldSelectTopMostSlice) { currentIdx = itemModel->index(itemModel->rowCount(currentIdx) - 1, OutlinerListModel::ColumnName, currentIdx); } - - getNextIdxFunction = - AZStd::bind(&QTreeView::indexAbove, treeView, AZStd::placeholders::_1); } QModelIndex nextIdx = currentIdx; @@ -964,7 +968,7 @@ void OutlinerWidget::DoSelectEdgeSliceRoot(bool shouldSelectTopMostSlice) AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult( foundSliceRoot, &AzToolsFramework::ToolsApplicationRequests::IsSliceRootEntity, currentEntityId); - nextIdx = getNextIdxFunction(currentIdx); + nextIdx = nextIndexForTree(shouldSelectTopMostSlice,treeView,currentIdx); } while (nextIdx.isValid() && !foundSliceRoot); if (foundSliceRoot) @@ -1416,7 +1420,10 @@ void OutlinerWidget::SortContent() } m_entitiesToSort.clear(); - auto comparer = AZStd::bind(&CompareEntitiesForSorting, AZStd::placeholders::_1, AZStd::placeholders::_2, m_sortMode); + auto comparer = [sortMode = m_sortMode](AZ::EntityId left, AZ::EntityId right) -> bool + { + return CompareEntitiesForSorting(left, right, sortMode); + }; for (const AZ::EntityId& entityId : parentsToSort) { SortEntityChildren(entityId, comparer); @@ -1433,7 +1440,10 @@ void OutlinerWidget::OnSortModeChanged(EntityOutliner::DisplaySortMode sortMode) if (sortMode != EntityOutliner::DisplaySortMode::Manually) { AZ_PROFILE_FUNCTION(AzToolsFramework); - auto comparer = AZStd::bind(&CompareEntitiesForSorting, AZStd::placeholders::_1, AZStd::placeholders::_2, sortMode); + auto comparer = [sortMode = m_sortMode](AZ::EntityId left, AZ::EntityId right) -> bool + { + return CompareEntitiesForSorting(left, right, sortMode); + }; SortEntityChildrenRecursively(AZ::EntityId(), comparer); } diff --git a/Code/Editor/Resource.h b/Code/Editor/Resource.h index b3640fac70..ba3cd39fe7 100644 --- a/Code/Editor/Resource.h +++ b/Code/Editor/Resource.h @@ -104,6 +104,7 @@ #define ID_FILE_EXPORTTOGAMENOSURFACETEXTURE 33473 #define ID_VIEW_SWITCHTOGAME 33477 #define ID_VIEW_SWITCHTOGAME_FULLSCREEN 33478 +#define ID_VIEW_SWITCHTOGAME_VIEWPORT 33479 #define ID_MOVE_OBJECT 33481 #define ID_RENAME_OBJ 33483 #define ID_FETCH 33496 diff --git a/Code/Editor/Settings.cpp b/Code/Editor/Settings.cpp index d548cffb52..acb48c3545 100644 --- a/Code/Editor/Settings.cpp +++ b/Code/Editor/Settings.cpp @@ -112,8 +112,6 @@ SEditorSettings::SEditorSettings() m_showCircularDependencyError = true; bAutoloadLastLevelAtStartup = false; bMuteAudio = false; - bEnableGameModeVR = false; - objectHideMask = 0; objectSelectMask = 0xFFFFFFFF; // Initially all selectable. @@ -473,7 +471,7 @@ void SEditorSettings::LoadValue(const char* sSection, const char* sKey, ESystemC } ////////////////////////////////////////////////////////////////////////// -void SEditorSettings::Save() +void SEditorSettings::Save(bool isEditorClosing) { QString strStringPlaceholder; @@ -640,14 +638,16 @@ void SEditorSettings::Save() // --- Settings Registry values // Prefab System UI - AzFramework::ApplicationRequests::Bus::Broadcast( - &AzFramework::ApplicationRequests::SetPrefabSystemEnabled, prefabSystem); + AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::SetPrefabSystemEnabled, prefabSystem); AzToolsFramework::Prefab::PrefabLoaderInterface* prefabLoaderInterface = AZ::Interface::Get(); prefabLoaderInterface->SetSaveAllPrefabsPreference(levelSaveSettings.saveAllPrefabsPreference); - SaveSettingsRegistryFile(); + if (!isEditorClosing) + { + SaveSettingsRegistryFile(); + } } ////////////////////////////////////////////////////////////////////////// diff --git a/Code/Editor/Settings.h b/Code/Editor/Settings.h index 426d2300d3..9276d9b715 100644 --- a/Code/Editor/Settings.h +++ b/Code/Editor/Settings.h @@ -267,7 +267,7 @@ struct SANDBOX_API SEditorSettings AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING SEditorSettings(); ~SEditorSettings() = default; - void Save(); + void Save(bool isEditorClosing = false); void Load(); void LoadCloudSettings(); @@ -305,7 +305,6 @@ AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING bool m_showCircularDependencyError; bool bAutoloadLastLevelAtStartup; bool bMuteAudio; - bool bEnableGameModeVR; //! Speed of camera movement. float cameraMoveSpeed; diff --git a/Code/Editor/StartupLogoDialog.ui b/Code/Editor/StartupLogoDialog.ui index c0b8115cb0..cbc596f53a 100644 --- a/Code/Editor/StartupLogoDialog.ui +++ b/Code/Editor/StartupLogoDialog.ui @@ -103,7 +103,7 @@ - General Availability + development diff --git a/Code/Editor/ToolbarManager.cpp b/Code/Editor/ToolbarManager.cpp index 00b7992ef0..6b610d23ce 100644 --- a/Code/Editor/ToolbarManager.cpp +++ b/Code/Editor/ToolbarManager.cpp @@ -590,6 +590,16 @@ AmazonToolbar ToolbarManager::GetObjectToolbar() const return t; } +QMenu* ToolbarManager::CreatePlayButtonMenu() const +{ + QMenu* playButtonMenu = new QMenu("Play Game"); + + playButtonMenu->addAction(m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME_VIEWPORT)); + playButtonMenu->addAction(m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN)); + + return playButtonMenu; +} + AmazonToolbar ToolbarManager::GetPlayConsoleToolbar() const { AmazonToolbar t = AmazonToolbar("PlayConsole", QObject::tr("Play Controls")); @@ -598,8 +608,17 @@ AmazonToolbar ToolbarManager::GetPlayConsoleToolbar() const t.AddAction(ID_TOOLBAR_WIDGET_SPACER_RIGHT, ORIGINAL_TOOLBAR_VERSION); t.AddAction(ID_TOOLBAR_SEPARATOR, ORIGINAL_TOOLBAR_VERSION); t.AddAction(ID_TOOLBAR_WIDGET_PLAYCONSOLE_LABEL, ORIGINAL_TOOLBAR_VERSION); - t.AddAction(ID_VIEW_SWITCHTOGAME, TOOLBARS_WITH_PLAY_GAME); - t.AddAction(ID_VIEW_SWITCHTOGAME_FULLSCREEN, TOOLBARS_WITH_PLAY_GAME); + + QAction* playAction = m_actionManager->GetAction(ID_VIEW_SWITCHTOGAME); + QToolButton* playButton = new QToolButton(t.Toolbar()); + + QMenu* menu = CreatePlayButtonMenu(); + menu->setParent(t.Toolbar()); + playAction->setMenu(menu); + + playButton->setDefaultAction(playAction); + t.AddWidget(playButton, ID_VIEW_SWITCHTOGAME, ORIGINAL_TOOLBAR_VERSION); + t.AddAction(ID_TOOLBAR_SEPARATOR, ORIGINAL_TOOLBAR_VERSION); t.AddAction(ID_SWITCH_PHYSICS, TOOLBARS_WITH_PLAY_GAME); return t; @@ -728,7 +747,14 @@ void AmazonToolbar::SetActionsOnInternalToolbar(ActionManager* actionManager) { if (actionManager->HasAction(actionId)) { - m_toolbar->addAction(actionManager->GetAction(actionId)); + if (actionData.widget != nullptr) + { + m_toolbar->addWidget(actionData.widget); + } + else + { + m_toolbar->addAction(actionManager->GetAction(actionId)); + } } } } @@ -1367,7 +1393,12 @@ void AmazonToolbar::InstantiateToolbar(QMainWindow* mainWindow, ToolbarManager* void AmazonToolbar::AddAction(int actionId, int toolbarVersionAdded) { - m_actions.push_back({ actionId, toolbarVersionAdded }); + AddWidget(nullptr, actionId, toolbarVersionAdded); +} + +void AmazonToolbar::AddWidget(QWidget* widget, int actionId, int toolbarVersionAdded) +{ + m_actions.push_back({ actionId, toolbarVersionAdded, widget }); } void AmazonToolbar::Clear() diff --git a/Code/Editor/ToolbarManager.h b/Code/Editor/ToolbarManager.h index be537533b6..ae6b0c7296 100644 --- a/Code/Editor/ToolbarManager.h +++ b/Code/Editor/ToolbarManager.h @@ -87,6 +87,7 @@ public: const QString& GetTranslatedName() const { return m_translatedName; } void AddAction(int actionId, int toolbarVersionAdded = 0); + void AddWidget(QWidget* widget, int actionId, int toolbarVersionAdded = 0); QToolBar* Toolbar() const { return m_toolbar; } @@ -117,6 +118,7 @@ private: { int actionId; int toolbarVersionAdded; + QWidget* widget; bool operator ==(const AmazonToolbar::ActionData& other) const { @@ -133,7 +135,9 @@ private: class AmazonToolBarExpanderWatcher; class ToolbarManager + : public QObject { + Q_OBJECT public: explicit ToolbarManager(ActionManager* actionManager, MainWindow* mainWindow); ~ToolbarManager(); @@ -178,6 +182,8 @@ private: void UpdateAllowedAreas(QToolBar* toolbar); bool IsDirty(const AmazonToolbar& toolbar) const; + QMenu* CreatePlayButtonMenu() const; + const AmazonToolbar* FindDefaultToolbar(const QString& toolbarName) const; AmazonToolbar* FindToolbar(const QString& toolbarName); diff --git a/Code/Editor/TrackView/AtomOutputFrameCapture.cpp b/Code/Editor/TrackView/AtomOutputFrameCapture.cpp index 94451e6914..5943e3c2d7 100644 --- a/Code/Editor/TrackView/AtomOutputFrameCapture.cpp +++ b/Code/Editor/TrackView/AtomOutputFrameCapture.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -47,18 +48,42 @@ namespace TrackView AZ::Name viewName = AZ::Name("MainCamera"); m_view = AZ::RPI::View::CreateView(viewName, AZ::RPI::View::UsageCamera); m_renderPipeline->SetDefaultView(m_view); + m_targetView = scene.GetDefaultRenderPipeline()->GetDefaultView(); + if (AZ::Render::PostProcessFeatureProcessor* fp = scene.GetFeatureProcessor()) + { + // This will be set again to mimic the active camera in UpdateView + fp->SetViewAlias(m_view, m_targetView); + } } void AtomOutputFrameCapture::DestroyPipeline(AZ::RPI::Scene& scene) { + if (AZ::Render::PostProcessFeatureProcessor* fp = scene.GetFeatureProcessor()) + { + // Remove view alias introduced in CreatePipeline and UpdateView + fp->RemoveViewAlias(m_view); + } scene.RemoveRenderPipeline(m_renderPipeline->GetId()); m_passHierarchy.clear(); m_renderPipeline.reset(); m_view.reset(); + m_targetView.reset(); } - void AtomOutputFrameCapture::UpdateView(const AZ::Matrix3x4& cameraTransform, const AZ::Matrix4x4& cameraProjection) + void AtomOutputFrameCapture::UpdateView(const AZ::Matrix3x4& cameraTransform, const AZ::Matrix4x4& cameraProjection, const AZ::RPI::ViewPtr targetView) { + if (targetView && targetView != m_targetView) + { + if (AZ::RPI::Scene* scene = SceneFromGameEntityContext()) + { + if (AZ::Render::PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor()) + { + fp->SetViewAlias(m_view, targetView); + m_targetView = targetView; + } + } + } + m_view->SetCameraTransform(cameraTransform); m_view->SetViewToClipMatrix(cameraProjection); } diff --git a/Code/Editor/TrackView/AtomOutputFrameCapture.h b/Code/Editor/TrackView/AtomOutputFrameCapture.h index 2686a81c99..4719ab08e5 100644 --- a/Code/Editor/TrackView/AtomOutputFrameCapture.h +++ b/Code/Editor/TrackView/AtomOutputFrameCapture.h @@ -39,11 +39,12 @@ namespace TrackView CaptureFinishedCallback captureFinishedCallback); //! Update the internal view that is associated with the created pipeline. - void UpdateView(const AZ::Matrix3x4& cameraTransform, const AZ::Matrix4x4& cameraProjection); + void UpdateView(const AZ::Matrix3x4& cameraTransform, const AZ::Matrix4x4& cameraProjection, const AZ::RPI::ViewPtr targetView = nullptr); private: AZ::RPI::RenderPipelinePtr m_renderPipeline; //!< The internal render pipeline. AZ::RPI::ViewPtr m_view; //!< The view associated with the render pipeline. + AZ::RPI::ViewPtr m_targetView; //!< The view that this render pipeline will mimic. AZStd::vector m_passHierarchy; //!< Pass hierarchy (includes pipelineName and CopyToSwapChain). CaptureFinishedCallback m_captureFinishedCallback; //!< Stored callback called from OnCaptureFinished. diff --git a/Code/Editor/TrackView/SequenceBatchRenderDialog.cpp b/Code/Editor/TrackView/SequenceBatchRenderDialog.cpp index d7901e338a..a796a8ce37 100644 --- a/Code/Editor/TrackView/SequenceBatchRenderDialog.cpp +++ b/Code/Editor/TrackView/SequenceBatchRenderDialog.cpp @@ -16,6 +16,7 @@ #include #include +#include // Qt #include @@ -91,9 +92,12 @@ namespace static void UpdateAtomOutputFrameCaptureView(TrackView::AtomOutputFrameCapture& atomOutputFrameCapture, const int width, const int height) { const AZ::EntityId activeCameraEntityId = TrackView::ActiveCameraEntityId(); + AZ::RPI::ViewPtr view = nullptr; + AZ::RPI::ViewProviderBus::EventResult(view, activeCameraEntityId, &AZ::RPI::ViewProvider::GetView); atomOutputFrameCapture.UpdateView( TrackView::TransformFromEntityId(activeCameraEntityId), - TrackView::ProjectionFromCameraEntityId(activeCameraEntityId, static_cast(width), static_cast(height))); + TrackView::ProjectionFromCameraEntityId(activeCameraEntityId, aznumeric_cast(width), aznumeric_cast(height)), + view); } CSequenceBatchRenderDialog::CSequenceBatchRenderDialog(float fps, QWidget* pParent /* = nullptr */) diff --git a/Code/Editor/TrackView/SoundKeyUIControls.cpp b/Code/Editor/TrackView/SoundKeyUIControls.cpp index 8c18b58aed..f001937e82 100644 --- a/Code/Editor/TrackView/SoundKeyUIControls.cpp +++ b/Code/Editor/TrackView/SoundKeyUIControls.cpp @@ -108,18 +108,15 @@ void CSoundKeyUIControls::OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selec { ISoundKey soundKey; keyHandle.GetKey(&soundKey); - bool bChangedSoundFile = false; if (pVar == mv_startTrigger.GetVar()) { QString sFilename = mv_startTrigger; - bChangedSoundFile = sFilename != soundKey.sStartTrigger.c_str(); soundKey.sStartTrigger = sFilename.toUtf8().data(); } else if (pVar == mv_stopTrigger.GetVar()) { QString sFilename = mv_stopTrigger; - bChangedSoundFile = sFilename != soundKey.sStopTrigger.c_str(); soundKey.sStopTrigger = sFilename.toUtf8().data(); } diff --git a/Code/Editor/TrackView/TrackViewDopeSheetBase.cpp b/Code/Editor/TrackView/TrackViewDopeSheetBase.cpp index a14da0ae09..2907bebdb4 100644 --- a/Code/Editor/TrackView/TrackViewDopeSheetBase.cpp +++ b/Code/Editor/TrackView/TrackViewDopeSheetBase.cpp @@ -2233,7 +2233,6 @@ void CTrackViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKey if (pTrack && inRange) { - bool keyCreated = false; if (bTryAddKeysInGroup && pNode->GetParentNode()) // Add keys in group { CTrackViewTrackBundle tracksInGroup = pNode->GetTracksByParam(pTrack->GetParameterType()); @@ -2248,8 +2247,6 @@ void CTrackViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKey AzToolsFramework::ScopedUndoBatch undoBatch("Create Key"); pCurrTrack->CreateKey(keyTime); undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId()); - - keyCreated = true; } } else // A compound track @@ -2262,8 +2259,6 @@ void CTrackViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKey AzToolsFramework::ScopedUndoBatch undoBatch("Create Key"); pSubTrack->CreateKey(keyTime); undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId()); - - keyCreated = true; } } } @@ -2276,15 +2271,13 @@ void CTrackViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKey AzToolsFramework::ScopedUndoBatch undoBatch("Create Key"); pTrack->CreateKey(keyTime); undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId()); - - keyCreated = true; } } else // A compound track { if (pTrack->GetValueType() == AnimValueType::RGB) { - keyCreated = CreateColorKey(pTrack, keyTime); + CreateColorKey(pTrack, keyTime); } else { @@ -2295,7 +2288,6 @@ void CTrackViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKey if (IsOkToAddKeyHere(pSubTrack, keyTime)) { pSubTrack->CreateKey(keyTime); - keyCreated = true; } } undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId()); @@ -2617,7 +2609,6 @@ void CTrackViewDopeSheetBase::DrawSelectTrack(const Range& timeRange, QPainter* void CTrackViewDopeSheetBase::DrawBoolTrack(const Range& timeRange, QPainter* painter, CTrackViewTrack* pTrack, const QRect& rc) { int x0 = TimeToClient(timeRange.start); - float t0 = timeRange.start; const QBrush prevBrush = painter->brush(); painter->setBrush(m_visibilityBrush); @@ -2648,7 +2639,6 @@ void CTrackViewDopeSheetBase::DrawBoolTrack(const Range& timeRange, QPainter* pa painter->fillRect(QRect(QPoint(x0, rc.top() + 4), QPoint(x, rc.bottom() - 4)), gradient); } - t0 = time; x0 = x; } int x = TimeToClient(timeRange.end); diff --git a/Code/Editor/TrackView/TrackViewKeyPropertiesDlg.cpp b/Code/Editor/TrackView/TrackViewKeyPropertiesDlg.cpp index 2fd46b0fc5..a17d92ac0a 100644 --- a/Code/Editor/TrackView/TrackViewKeyPropertiesDlg.cpp +++ b/Code/Editor/TrackView/TrackViewKeyPropertiesDlg.cpp @@ -198,7 +198,6 @@ void CTrackViewKeyPropertiesDlg::OnKeySelectionChanged(CTrackViewSequence* seque m_wndProps->setEnabled(false); m_wndTrackProps->setEnabled(false); - bool bAssigned = false; if (selectedKeys.GetKeyCount() > 0 && selectedKeys.AreAllKeysOfSameType()) { CTrackViewTrack* pTrack = selectedKeys.GetKey(0).GetTrack(); @@ -215,12 +214,6 @@ void CTrackViewKeyPropertiesDlg::OnKeySelectionChanged(CTrackViewSequence* seque { AddVars(m_keyControls[i]); } - - if (m_keyControls[i]->OnKeySelectionChange(selectedKeys)) - { - bAssigned = true; - } - break; } } diff --git a/Code/Editor/Util/FileUtil.cpp b/Code/Editor/Util/FileUtil.cpp index baca69d628..9f96d45381 100644 --- a/Code/Editor/Util/FileUtil.cpp +++ b/Code/Editor/Util/FileUtil.cpp @@ -23,9 +23,9 @@ // AzCore #include #include +#include // AzFramework -#include // AzQtComponents #include @@ -54,92 +54,14 @@ #include #endif -bool CFileUtil::s_singleFileDlgPref[IFileUtil::EFILE_TYPE_LAST] = { true, true, true, true, true }; -bool CFileUtil::s_multiFileDlgPref[IFileUtil::EFILE_TYPE_LAST] = { true, true, true, true, true }; +bool CFileUtil::s_singleFileDlgPref[IFileUtil::EFILE_TYPE_LAST] = { true, true, true, true }; +bool CFileUtil::s_multiFileDlgPref[IFileUtil::EFILE_TYPE_LAST] = { true, true, true, true }; CAutoRestorePrimaryCDRoot::~CAutoRestorePrimaryCDRoot() { QDir::setCurrent(GetIEditor()->GetPrimaryCDFolder()); } -bool CFileUtil::CompileLuaFile(const char* luaFilename) -{ - QString luaFile = luaFilename; - - if (luaFile.isEmpty()) - { - return false; - } - - // Check if this file is in Archive. - { - CCryFile file; - if (file.Open(luaFilename, "rb")) - { - // Check if in pack. - if (file.IsInPak()) - { - return true; - } - } - } - - luaFile = Path::GamePathToFullPath(luaFilename); - - // First try compiling script and see if it have any errors. - QString LuaCompiler; - QString CompilerOutput; - - // Create the filepath of the lua compiler - QString szExeFileName = qApp->applicationFilePath(); - QString exePath = Path::GetPath(szExeFileName); - -#if defined(AZ_PLATFORM_WINDOWS) - const char* luaCompiler = "LuaCompiler.exe"; -#else - const char* luaCompiler = "lua"; -#endif - LuaCompiler = Path::AddPathSlash(exePath) + luaCompiler + " "; - - AZStd::string path = luaFile.toUtf8().data(); - EBUS_EVENT(AzFramework::ApplicationRequests::Bus, NormalizePath, path); - - QString finalPath = path.c_str(); - finalPath = "\"" + finalPath + "\""; - - // Add the name of the Lua file - QString cmdLine = LuaCompiler + finalPath; - - // Execute the compiler and capture the output - if (!GetIEditor()->ExecuteConsoleApp(cmdLine, CompilerOutput)) - { - QMessageBox::critical(QApplication::activeWindow(), QString(), QObject::tr("Error while executing '%1', make sure the file is in" \ - " your Primary CD folder !").arg(luaCompiler)); - return false; - } - - // Check return string - if (!CompilerOutput.isEmpty()) - { - // Errors while compiling file. - - // Show output from Lua compiler - if (QMessageBox::critical(QApplication::activeWindow(), QObject::tr("Lua Compiler"), - QObject::tr("Error output from Lua compiler:\r\n%1\r\nDo you want to edit the file ?").arg(CompilerOutput), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) - { - int line = 0; - int index = CompilerOutput.indexOf("at line"); - if (index >= 0) - { - azsscanf(CompilerOutput.mid(index).toUtf8().data(), "at line %d", &line); - } - // Open the Lua file for editing - EditTextFile(luaFile.toUtf8().data(), line); - } - return false; - } - return true; -} ////////////////////////////////////////////////////////////////////////// bool CFileUtil::ExtractFile(QString& file, bool bMsgBoxAskForExtraction, const char* pDestinationFilename) { @@ -205,7 +127,7 @@ void CFileUtil::EditTextFile(const char* txtFile, int line, IFileUtil::ETextFile { QString file = txtFile; - QString fullPathName = Path::GamePathToFullPath(file); + QString fullPathName = Path::GamePathToFullPath(file); ExtractFile(fullPathName); QString cmd(fullPathName); #if defined (AZ_PLATFORM_WINDOWS) @@ -301,64 +223,6 @@ void CFileUtil::EditTextureFile(const char* textureFile, [[maybe_unused]] bool b } } -////////////////////////////////////////////////////////////////////////// -bool CFileUtil::EditMayaFile(const char* filepath, const bool bExtractFromPak, const bool bUseGameFolder) -{ - QString dosFilepath = PathUtil::ToDosPath(filepath).c_str(); - if (bExtractFromPak) - { - ExtractFile(dosFilepath); - } - - if (bUseGameFolder) - { - const QString sGameFolder = Path::GetEditingGameDataFolder().c_str(); - int nLength = sGameFolder.toUtf8().count(); - if (azstrnicmp(filepath, sGameFolder.toUtf8().data(), nLength) != 0) - { - dosFilepath = sGameFolder + '\\' + filepath; - } - - dosFilepath = PathUtil::ToDosPath(dosFilepath.toUtf8().data()).c_str(); - } - - const char* engineRoot; - EBUS_EVENT_RESULT(engineRoot, AzFramework::ApplicationRequests::Bus, GetEngineRoot); - - const QString fullPath = QString(engineRoot) + '\\' + dosFilepath; - - if (gSettings.animEditor.isEmpty()) - { - AzQtComponents::ShowFileOnDesktop(fullPath); - } - else - { - if (!QProcess::startDetached(gSettings.animEditor, { fullPath })) - { - CryMessageBox("Can't open the file. You can specify a source editor in Sandbox Preferences or create an association in Windows.", "Cannot open file!", MB_OK | MB_ICONERROR); - } - } - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CFileUtil::EditFile(const char* filePath, const bool bExtrackFromPak, const bool bUseGameFolder) -{ - QString extension = filePath; - extension.remove(0, extension.lastIndexOf('.')); - - if (extension.compare(".ma") == 0) - { - return EditMayaFile(filePath, bExtrackFromPak, bUseGameFolder); - } - else if ((extension.compare(".bspace") == 0) || (extension.compare(".comb") == 0)) - { - EditTextFile(filePath, 0, IFileUtil::FILE_TYPE_BSPACE); - return true; - } - - return false; -} ////////////////////////////////////////////////////////////////////////// bool CFileUtil::CalculateDccFilename(const QString& assetFilename, QString& dccFilename) diff --git a/Code/Editor/Util/FileUtil.h b/Code/Editor/Util/FileUtil.h index 5820c32081..fc0fc942fe 100644 --- a/Code/Editor/Util/FileUtil.h +++ b/Code/Editor/Util/FileUtil.h @@ -25,14 +25,9 @@ public: static void ShowInExplorer(const QString& path); - // Try to compile the given lua file: returns true if compilation succeeded, false on failure. - static bool CompileLuaFile(const char* luaFilename); - static bool ExtractFile(QString& file, bool bMsgBoxAskForExtraction = true, const char* pDestinationFilename = nullptr); static void EditTextFile(const char* txtFile, int line = 0, IFileUtil::ETextFileType fileType = IFileUtil::FILE_TYPE_SCRIPT); static void EditTextureFile(const char* txtureFile, bool bUseGameFolder); - static bool EditMayaFile(const char* mayaFile, const bool bExtractFromPak, const bool bUseGameFolder); - static bool EditFile(const char* filePath, const bool bExtrackFromPak, const bool bUseGameFolder); //! dcc filename calculation and extraction sub-routines static bool CalculateDccFilename(const QString& assetFilename, QString& dccFilename); diff --git a/Code/Editor/Util/FileUtil_impl.cpp b/Code/Editor/Util/FileUtil_impl.cpp index 0dd3a0ca87..28090d28d5 100644 --- a/Code/Editor/Util/FileUtil_impl.cpp +++ b/Code/Editor/Util/FileUtil_impl.cpp @@ -20,21 +20,11 @@ void CFileUtil_impl::ShowInExplorer(const QString& path) CFileUtil::ShowInExplorer(path); } -bool CFileUtil_impl::CompileLuaFile(const char* luaFilename) -{ - return CFileUtil::CompileLuaFile(luaFilename); -} - bool CFileUtil_impl::ExtractFile(QString& file, bool bMsgBoxAskForExtraction, const char* pDestinationFilename) { return CFileUtil::ExtractFile(file, bMsgBoxAskForExtraction, pDestinationFilename); } -void CFileUtil_impl::EditTextFile(const char* txtFile, int line, ETextFileType fileType) -{ - CFileUtil::EditTextFile(txtFile, line, fileType); -} - void CFileUtil_impl::EditTextureFile(const char* txtureFile, bool bUseGameFolder) { CFileUtil::EditTextureFile(txtureFile, bUseGameFolder); diff --git a/Code/Editor/Util/FileUtil_impl.h b/Code/Editor/Util/FileUtil_impl.h index 04d9e829b9..aa8d0bf3b5 100644 --- a/Code/Editor/Util/FileUtil_impl.h +++ b/Code/Editor/Util/FileUtil_impl.h @@ -36,9 +36,7 @@ public: void ShowInExplorer(const QString& path) override; - bool CompileLuaFile(const char* luaFilename) override; bool ExtractFile(QString& file, bool bMsgBoxAskForExtraction = true, const char* pDestinationFilename = nullptr) override; - void EditTextFile(const char* txtFile, int line = 0, ETextFileType fileType = FILE_TYPE_SCRIPT) override; void EditTextureFile(const char* txtureFile, bool bUseGameFolder) override; //! dcc filename calculation and extraction sub-routines diff --git a/Code/Editor/Util/ImageGif.cpp b/Code/Editor/Util/ImageGif.cpp index 67c5911344..336584b959 100644 --- a/Code/Editor/Util/ImageGif.cpp +++ b/Code/Editor/Util/ImageGif.cpp @@ -197,11 +197,9 @@ bool CImageGif::Load(const QString& fileName, CImageEx& outImage) return false; } - int numcols; unsigned char ch, ch1; uint8* ptr1; int i; - short transparency = -1; TImage outImageIndex; @@ -246,7 +244,6 @@ bool CImageGif::Load(const QString& fileName, CImageEx& outImage) HasColormap = ((ch & COLORMAPMASK) ? true : false); BitsPerPixel = (ch & 7) + 1; - numcols = ColorMapSize = 1 << BitsPerPixel; BitMask = ColorMapSize - 1; Background = NEXTBYTE; /* background color... not used. */ @@ -290,10 +287,6 @@ bool CImageGif::Load(const QString& fileName, CImageEx& outImage) { case GRAPHIC_EXT: ch = NEXTBYTE; - if (ptr[0] & 0x1) - { - transparency = ptr[3]; /* transparent color index */ - } ptr += ch; break; case PLAINTEXT_EXT: @@ -317,9 +310,6 @@ bool CImageGif::Load(const QString& fileName, CImageEx& outImage) } } - //if (transparency >= 0) - //mfSet_transparency(transparency); - /* Now read in values from the image descriptor */ ch = NEXTBYTE; diff --git a/Code/Editor/Util/PathUtil.cpp b/Code/Editor/Util/PathUtil.cpp index ca3481ddae..8b745ee1bc 100644 --- a/Code/Editor/Util/PathUtil.cpp +++ b/Code/Editor/Util/PathUtil.cpp @@ -14,7 +14,6 @@ #include #include #include // for ebus events -#include #include #include @@ -175,9 +174,8 @@ namespace Path ////////////////////////////////////////////////////////////////////////// QString GetEngineRootPath() { - const char* engineRoot; - EBUS_EVENT_RESULT(engineRoot, AzFramework::ApplicationRequests::Bus, GetEngineRoot); - return QString(engineRoot); + const AZ::IO::FixedMaxPathString engineRoot = AZ::Utils::GetEnginePath(); + return QString::fromUtf8(engineRoot.c_str(), static_cast(engineRoot.size())); } ////////////////////////////////////////////////////////////////////////// diff --git a/Code/Editor/Viewport.cpp b/Code/Editor/Viewport.cpp index c8f2e268d9..5a64982350 100644 --- a/Code/Editor/Viewport.cpp +++ b/Code/Editor/Viewport.cpp @@ -14,14 +14,19 @@ // Qt #include +// AzCore +#include + // AzQtComponents #include #include #include #include +#include // Editor +#include "Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.h" #include "ViewManager.h" #include "Include/ITransformManipulator.h" #include "Include/HitContext.h" @@ -32,22 +37,35 @@ #include "GameEngine.h" #include "Settings.h" - #ifdef LoadCursor #undef LoadCursor #endif +AZ_CVAR( + float, + ed_defaultEntityPlacementDistance, + 10.0f, + nullptr, + AZ::ConsoleFunctorFlags::Null, + "The default distance to place an entity from the camera if no intersection is found"); + +float GetDefaultEntityPlacementDistance() +{ + return ed_defaultEntityPlacementDistance; +} + ////////////////////////////////////////////////////////////////////// // Viewport drag and drop support ////////////////////////////////////////////////////////////////////// -void QtViewport::BuildDragDropContext(AzQtComponents::ViewportDragContext& context, const QPoint& pt) +void QtViewport::BuildDragDropContext( + AzQtComponents::ViewportDragContext& context, const AzFramework::ViewportId viewportId, const QPoint& point) { - context.m_hitLocation = AZ::Vector3::CreateZero(); - context.m_hitLocation = GetHitLocation(pt); + context.m_hitLocation = AzToolsFramework::FindClosestPickIntersection( + viewportId, AzToolsFramework::ViewportInteraction::ScreenPointFromQPoint(point), AzToolsFramework::EditorPickRayLength, + GetDefaultEntityPlacementDistance()); } - void QtViewport::dragEnterEvent(QDragEnterEvent* event) { if (!GetIEditor()->GetGameEngine()->IsLevelLoaded()) @@ -66,7 +84,7 @@ void QtViewport::dragEnterEvent(QDragEnterEvent* event) // new bus-based way of doing it (install a listener!) using namespace AzQtComponents; ViewportDragContext context; - BuildDragDropContext(context, event->pos()); + BuildDragDropContext(context, GetViewportId(), event->pos()); DragAndDropEventsBus::Event(DragAndDropContexts::EditorViewport, &DragAndDropEvents::DragEnter, event, context); } } @@ -89,7 +107,7 @@ void QtViewport::dragMoveEvent(QDragMoveEvent* event) // new bus-based way of doing it (install a listener!) using namespace AzQtComponents; ViewportDragContext context; - BuildDragDropContext(context, event->pos()); + BuildDragDropContext(context, GetViewportId(), event->pos()); DragAndDropEventsBus::Event(DragAndDropContexts::EditorViewport, &DragAndDropEvents::DragMove, event, context); } } @@ -112,7 +130,7 @@ void QtViewport::dropEvent(QDropEvent* event) { // new bus-based way of doing it (install a listener!) ViewportDragContext context; - BuildDragDropContext(context, event->pos()); + BuildDragDropContext(context, GetViewportId(), event->pos()); DragAndDropEventsBus::Event(DragAndDropContexts::EditorViewport, &DragAndDropEvents::Drop, event, context); } } @@ -340,13 +358,6 @@ void QtViewport::resizeEvent(QResizeEvent* event) Update(); } -////////////////////////////////////////////////////////////////////////// -void QtViewport::leaveEvent(QEvent* event) -{ - QWidget::leaveEvent(event); - MouseCallback(eMouseLeave, QPoint(), Qt::KeyboardModifiers(), Qt::MouseButtons()); -} - ////////////////////////////////////////////////////////////////////////// void QtViewport::paintEvent([[maybe_unused]] QPaintEvent* event) { @@ -581,63 +592,7 @@ void QtViewport::keyReleaseEvent(QKeyEvent* event) OnKeyUp(nativeKey, 1, event->nativeModifiers()); } -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnLButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - // Save the mouse down position - m_cMouseDownPos = point; - if (MouseCallback(eMouseLDown, point, modifiers)) - { - return; - } -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnLButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - // Check Edit Tool. - MouseCallback(eMouseLUp, point, modifiers); -} -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnRButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - MouseCallback(eMouseRDown, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnRButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - MouseCallback(eMouseRUp, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnMButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - // Check Edit Tool. - MouseCallback(eMouseMDown, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnMButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - // Move the viewer to the mouse location. - // Check Edit Tool. - MouseCallback(eMouseMUp, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnMButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - MouseCallback(eMouseMDblClick, point, modifiers); -} - - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnMouseMove(Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons, const QPoint& point) -{ - MouseCallback(eMouseMove, point, modifiers, buttons); -} ////////////////////////////////////////////////////////////////////////// void QtViewport::OnSetCursor() @@ -696,44 +651,6 @@ void QtViewport::OnDragSelectRectangle(const QRect& rect, bool bNormalizeRect) GetIEditor()->SetStatusText(szNewStatusText); } -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnLButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - if (GetIEditor()->IsInGameMode()) - { - // Ignore double clicks while in game. - return; - } - - MouseCallback(eMouseLDblClick, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnRButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point) -{ - MouseCallback(eMouseRDblClick, point, modifiers); -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnKeyDown([[maybe_unused]] UINT nChar, [[maybe_unused]] UINT nRepCnt, [[maybe_unused]] UINT nFlags) -{ - if (GetIEditor()->IsInGameMode()) - { - // Ignore key downs while in game. - return; - } -} - -////////////////////////////////////////////////////////////////////////// -void QtViewport::OnKeyUp([[maybe_unused]] UINT nChar, [[maybe_unused]] UINT nRepCnt, [[maybe_unused]] UINT nFlags) -{ - if (GetIEditor()->IsInGameMode()) - { - // Ignore key downs while in game. - return; - } -} - ////////////////////////////////////////////////////////////////////////// void QtViewport::SetCurrentCursor(const QCursor& hCursor, const QString& cursorString) { @@ -1119,29 +1036,6 @@ bool QtViewport::HitTest(const QPoint& point, HitContext& hitInfo) return false; } -AZ::Vector3 QtViewport::GetHitLocation(const QPoint& point) -{ - Vec3 pos = Vec3(ZERO); - HitContext hit; - if (HitTest(point, hit)) - { - pos = hit.raySrc + hit.rayDir * hit.dist; - pos = SnapToGrid(pos); - } - else - { - bool hitTerrain; - pos = ViewToWorld(point, &hitTerrain); - if (hitTerrain) - { - pos.z = GetIEditor()->GetTerrainElevation(pos.x, pos.y); - } - pos = SnapToGrid(pos); - } - - return AZ::Vector3(pos.x, pos.y, pos.z); -} - ////////////////////////////////////////////////////////////////////////// void QtViewport::SetZoomFactor(float fZoomFactor) { @@ -1315,84 +1209,6 @@ bool QtViewport::GetAdvancedSelectModeFlag() return m_bAdvancedSelectMode; } -////////////////////////////////////////////////////////////////////////// -bool QtViewport::MouseCallback(EMouseEvent event, const QPoint& point, Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons) -{ - AZ_PROFILE_FUNCTION(Editor); - - // Ignore any mouse events in game mode. - if (GetIEditor()->IsInGameMode()) - { - return true; - } - - // We must ignore mouse events when we are in the middle of an assert. - // Reason: If we have an assert called from an engine module under the editor, if we call this function, - // it may call the engine again and cause a deadlock. - // Concrete example: CryPhysics called from Trackview causing an assert, and moving the cursor over the viewport - // would cause the editor to freeze as it calls CryPhysics again for a raycast while it didn't release the lock. - if (gEnv->pSystem->IsAssertDialogVisible()) - { - return true; - } - - ////////////////////////////////////////////////////////////////////////// - // Hit test gizmo objects. - ////////////////////////////////////////////////////////////////////////// - bool bAltClick = (modifiers & Qt::AltModifier); - bool bCtrlClick = (modifiers & Qt::ControlModifier); - bool bShiftClick = (modifiers & Qt::ShiftModifier); - - int flags = (bCtrlClick ? MK_CONTROL : 0) | - (bShiftClick ? MK_SHIFT : 0) | - ((buttons& Qt::LeftButton) ? MK_LBUTTON : 0) | - ((buttons& Qt::MiddleButton) ? MK_MBUTTON : 0) | - ((buttons& Qt::RightButton) ? MK_RBUTTON : 0); - - switch (event) - { - case eMouseMove: - - if (m_nLastUpdateFrame == m_nLastMouseMoveFrame) - { - // If mouse move event generated in the same frame, ignore it. - return false; - } - m_nLastMouseMoveFrame = m_nLastUpdateFrame; - - // Skip the marker position update if anything is selected, since it is only used - // by the info bar which doesn't show the marker when there is an active selection. - // This helps a performance issue when calling ViewToWorld (which calls RayWorldIntersection) - // on every mouse movement becomes very expensive in scenes with large amounts of entities. - CSelectionGroup* selection = GetIEditor()->GetSelection(); - if (!(buttons & Qt::RightButton) /* && m_nLastUpdateFrame != m_nLastMouseMoveFrame*/ && (selection && selection->IsEmpty())) - { - //m_nLastMouseMoveFrame = m_nLastUpdateFrame; - Vec3 pos = ViewToWorld(point); - GetIEditor()->SetMarkerPosition(pos); - } - break; - } - - QPoint tempPoint(point.x(), point.y()); - - ////////////////////////////////////////////////////////////////////////// - // Handle viewport manipulators. - ////////////////////////////////////////////////////////////////////////// - if (!bAltClick) - { - ITransformManipulator* pManipulator = GetIEditor()->GetTransformManipulator(); - if (pManipulator) - { - if (pManipulator->MouseCallback(this, event, tempPoint, flags)) - { - return true; - } - } - } - - return false; -} ////////////////////////////////////////////////////////////////////////// void QtViewport::ProcessRenderLisneters(DisplayContext& rstDisplayContext) { @@ -1407,6 +1223,69 @@ void QtViewport::ProcessRenderLisneters(DisplayContext& rstDisplayContext) } ////////////////////////////////////////////////////////////////////////// #if defined(AZ_PLATFORM_WINDOWS) +// Note: Both CreateAnglesYPR and CreateOrientationYPR were copied verbatim from Cry_Camera.h which has been removed. +// +// Description +//
+//   x-YAW
+//   y-PITCH (negative=looking down / positive=looking up)
+//   z-ROLL
+//   
+// Note: If we are looking along the z-axis, its not possible to specify the x and z-angle +inline Ang3 CreateAnglesYPR(const Matrix33& m) +{ + assert(m.IsOrthonormal()); + float l = Vec3(m.m01, m.m11, 0.0f).GetLength(); + if (l > 0.0001) + { + return Ang3(atan2f(-m.m01 / l, m.m11 / l), atan2f(m.m21, l), atan2f(-m.m20 / l, m.m22 / l)); + } + else + { + return Ang3(0, atan2f(m.m21, l), 0); + } +} + +// Description +// This function builds a 3x3 orientation matrix using YPR-angles +// Rotation order for the orientation-matrix is Z-X-Y. (Zaxis=YAW / Xaxis=PITCH / Yaxis=ROLL) +// +//
+//  COORDINATE-SYSTEM
+//
+//  z-axis
+//    ^
+//    |
+//    |  y-axis
+//    |  /
+//    | /
+//    |/
+//    +--------------->   x-axis
+// 
+// +// Example: +// Matrix33 orientation=CreateOrientationYPR( Ang3(1,2,3) ); +inline Matrix33 CreateOrientationYPR(const Ang3& ypr) +{ + f32 sz, cz; + sincos_tpl(ypr.x, &sz, &cz); //Zaxis = YAW + f32 sx, cx; + sincos_tpl(ypr.y, &sx, &cx); //Xaxis = PITCH + f32 sy, cy; + sincos_tpl(ypr.z, &sy, &cy); //Yaxis = ROLL + Matrix33 c; + c.m00 = cy * cz - sy * sz * sx; + c.m01 = -sz * cx; + c.m02 = sy * cz + cy * sz * sx; + c.m10 = cy * sz + sy * sx * cz; + c.m11 = cz * cx; + c.m12 = sy * sz - cy * sx * cz; + c.m20 = -sy * cx; + c.m21 = sx; + c.m22 = cy * cx; + return c; +} + void QtViewport::OnRawInput([[maybe_unused]] UINT wParam, HRAWINPUT lParam) { static C3DConnexionDriver* p3DConnexionDriver = 0; @@ -1450,12 +1329,12 @@ void QtViewport::OnRawInput([[maybe_unused]] UINT wParam, HRAWINPUT lParam) t *= sys_scale3DMouseTranslation->GetFVal(); float as = 0.001f * gSettings.cameraMoveSpeed; - Ang3 ypr = CCamera::CreateAnglesYPR(Matrix33(viewTM)); + Ang3 ypr = CreateAnglesYPR(Matrix33(viewTM)); ypr.x += -all6DOFs[5] * as * fScaleYPR; ypr.y = AZStd::clamp(ypr.y + all6DOFs[3] * as * fScaleYPR, -1.5f, 1.5f); // to keep rotation in reasonable range ypr.z = 0; // to have camera always upward - viewTM = Matrix34(CCamera::CreateOrientationYPR(ypr), viewTM.GetTranslation()); + viewTM = Matrix34(CreateOrientationYPR(ypr), viewTM.GetTranslation()); viewTM = viewTM * Matrix34::CreateTranslationMat(t); SetViewTM(viewTM); diff --git a/Code/Editor/Viewport.h b/Code/Editor/Viewport.h index 7f8ccc4c4f..bf44b914aa 100644 --- a/Code/Editor/Viewport.h +++ b/Code/Editor/Viewport.h @@ -6,13 +6,12 @@ * */ - // Description : interface for the CViewport class. - #pragma once #if !defined(Q_MOC_RUN) +#include #include #include #include @@ -88,6 +87,9 @@ enum EStdCursor STD_CURSOR_LAST, }; +//! The default distance an entity is placed from the camera if there is no intersection +SANDBOX_API float GetDefaultEntityPlacementDistance(); + AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING class SANDBOX_API CViewport : public IDisplayViewport @@ -201,7 +203,6 @@ public: //! Performs hit testing of 2d point in view to find which object hit. virtual bool HitTest(const QPoint& point, HitContext& hitInfo) = 0; - virtual AZ::Vector3 GetHitLocation(const QPoint& point) = 0; virtual void MakeConstructionPlane(int axis) = 0; @@ -432,7 +433,6 @@ public: //! Performs hit testing of 2d point in view to find which object hit. bool HitTest(const QPoint& point, HitContext& hitInfo) override; - AZ::Vector3 GetHitLocation(const QPoint& point) override; //! Do 2D hit testing of line in world space. // pToCameraDistance is an optional output parameter in which distance from the camera to the line is returned. @@ -522,9 +522,6 @@ protected: void setRenderOverlayVisible(bool); bool isRenderOverlayVisible() const; - // called to process mouse callback inside the viewport. - virtual bool MouseCallback(EMouseEvent event, const QPoint& point, Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons = Qt::NoButton); - void ProcessRenderLisneters(DisplayContext& rstDisplayContext); void mousePressEvent(QMouseEvent* event) override; @@ -535,29 +532,29 @@ protected: void keyPressEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override; void resizeEvent(QResizeEvent* event) override; - void leaveEvent(QEvent* event) override; - void paintEvent(QPaintEvent* event) override; - virtual void OnMouseMove(Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons, const QPoint& point); - virtual void OnMouseWheel(Qt::KeyboardModifiers modifiers, short zDelta, const QPoint& pt); - virtual void OnLButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnLButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnRButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnRButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnMButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnMButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnMButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnLButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnRButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point); - virtual void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); - virtual void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags); + virtual void OnMouseMove(Qt::KeyboardModifiers, Qt::MouseButtons, const QPoint&) {} + virtual void OnMouseWheel(Qt::KeyboardModifiers, short zDelta, const QPoint&); + virtual void OnLButtonDown(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnLButtonUp(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnRButtonDown(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnRButtonUp(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnMButtonDblClk(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnMButtonDown(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnMButtonUp(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnLButtonDblClk(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnRButtonDblClk(Qt::KeyboardModifiers, const QPoint&) {} + virtual void OnKeyDown([[maybe_unused]] UINT nChar, [[maybe_unused]] UINT nRepCnt, [[maybe_unused]] UINT nFlags) {} + virtual void OnKeyUp([[maybe_unused]] UINT nChar, [[maybe_unused]] UINT nRepCnt, [[maybe_unused]] UINT nFlags) {} #if defined(AZ_PLATFORM_WINDOWS) void OnRawInput(UINT wParam, HRAWINPUT lParam); #endif void OnSetCursor(); - virtual void BuildDragDropContext(AzQtComponents::ViewportDragContext& context, const QPoint& pt); + virtual void BuildDragDropContext( + AzQtComponents::ViewportDragContext& context, AzFramework::ViewportId viewportId, const QPoint& point); + void dragEnterEvent(QDragEnterEvent* event) override; void dragMoveEvent(QDragMoveEvent* event) override; void dragLeaveEvent(QDragLeaveEvent* event) override; diff --git a/Code/Editor/ViewportManipulatorController.cpp b/Code/Editor/ViewportManipulatorController.cpp index 1766945541..e46328eb65 100644 --- a/Code/Editor/ViewportManipulatorController.cpp +++ b/Code/Editor/ViewportManipulatorController.cpp @@ -8,13 +8,15 @@ #include "ViewportManipulatorController.h" +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include +#include #include @@ -87,8 +89,14 @@ namespace SandboxEditor } using InteractionBus = AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus; - using namespace AzToolsFramework::ViewportInteraction; using AzFramework::InputChannel; + using AzToolsFramework::ViewportInteraction::KeyboardModifier; + using AzToolsFramework::ViewportInteraction::MouseButton; + using AzToolsFramework::ViewportInteraction::MouseEvent; + using AzToolsFramework::ViewportInteraction::MouseInteraction; + using AzToolsFramework::ViewportInteraction::MouseInteractionEvent; + using AzToolsFramework::ViewportInteraction::ProjectedViewportRay; + using AzToolsFramework::ViewportInteraction::ViewportInteractionRequestBus; bool interactionHandled = false; float wheelDelta = 0.0f; @@ -117,16 +125,13 @@ namespace SandboxEditor aznumeric_cast(position->m_normalizedPosition.GetX() * windowSize.m_width), aznumeric_cast(position->m_normalizedPosition.GetY() * windowSize.m_height)); - m_mouseInteraction.m_mousePick.m_screenCoordinates = screenPoint; - AZStd::optional ray; + ProjectedViewportRay ray{}; ViewportInteractionRequestBus::EventResult( ray, GetViewportId(), &ViewportInteractionRequestBus::Events::ViewportScreenToWorldRay, screenPoint); - if (ray.has_value()) - { - m_mouseInteraction.m_mousePick.m_rayOrigin = ray.value().origin; - m_mouseInteraction.m_mousePick.m_rayDirection = ray.value().direction; - } + m_mouseInteraction.m_mousePick.m_rayOrigin = ray.origin; + m_mouseInteraction.m_mousePick.m_rayDirection = ray.direction; + m_mouseInteraction.m_mousePick.m_screenCoordinates = screenPoint; } eventType = MouseEvent::Move; @@ -152,7 +157,7 @@ namespace SandboxEditor // Only insert the double click timing once we're done processing events, to avoid a false IsDoubleClick positive if (finishedProcessingEvents) { - m_pendingDoubleClicks[mouseButton] = m_curTime; + m_pendingDoubleClicks[mouseButton] = { m_currentTime, m_mouseInteraction.m_mousePick.m_screenCoordinates }; } eventType = MouseEvent::Down; } @@ -160,8 +165,8 @@ namespace SandboxEditor else if (state == InputChannel::State::Ended) { // If we've actually logged a mouse down event, forward a mouse up event. - // This prevents corner cases like the context menu thinking it should be opened even though no one clicked in this viewport, - // due to RenderViewportWidget ensuring all controllers get InputChannel::State::Ended events. + // This prevents corner cases like the context menu thinking it should be opened even though no one clicked in this + // viewport, due to RenderViewportWidget ensuring all controllers get InputChannel::State::Ended events. if (m_mouseInteraction.m_mouseButtons.m_mouseButtons & mouseButtonValue) { // Erase the button from our state if we're done processing events. @@ -246,17 +251,22 @@ namespace SandboxEditor void ViewportManipulatorControllerInstance::UpdateViewport(const AzFramework::ViewportControllerUpdateEvent& event) { - m_curTime = event.m_time; + m_currentTime = event.m_time; } bool ViewportManipulatorControllerInstance::IsDoubleClick(AzToolsFramework::ViewportInteraction::MouseButton button) const { - auto clickIt = m_pendingDoubleClicks.find(button); - if (clickIt == m_pendingDoubleClicks.end()) + if (auto clickIt = m_pendingDoubleClicks.find(button); clickIt != m_pendingDoubleClicks.end()) { - return false; + const double doubleClickThresholdMilliseconds = qApp->doubleClickInterval(); + const bool insideTimeThreshold = + (m_currentTime.GetMilliseconds() - clickIt->second.m_time.GetMilliseconds()) < doubleClickThresholdMilliseconds; + const bool insideDistanceThreshold = + AzFramework::ScreenVectorLength(clickIt->second.m_position - m_mouseInteraction.m_mousePick.m_screenCoordinates) < + AzFramework::DefaultMouseMoveDeadZone; + return insideTimeThreshold && insideDistanceThreshold; } - const double doubleClickThresholdMilliseconds = qApp->doubleClickInterval(); - return (m_curTime.GetMilliseconds() - clickIt->second.GetMilliseconds()) < doubleClickThresholdMilliseconds; + + return false; } -} //namespace SandboxEditor +} // namespace SandboxEditor diff --git a/Code/Editor/ViewportManipulatorController.h b/Code/Editor/ViewportManipulatorController.h index d551eb3647..b9c359a544 100644 --- a/Code/Editor/ViewportManipulatorController.h +++ b/Code/Editor/ViewportManipulatorController.h @@ -39,8 +39,16 @@ namespace SandboxEditor static bool IsMouseMove(const AzFramework::InputChannel& inputChannel); static AzToolsFramework::ViewportInteraction::KeyboardModifier GetKeyboardModifier(const AzFramework::InputChannel& inputChannel); + //! Represents the time and location of a click. + struct ClickEvent + { + AZ::ScriptTimePoint m_time; + AzFramework::ScreenPoint m_position; + }; + AzToolsFramework::ViewportInteraction::MouseInteraction m_mouseInteraction; - AZStd::unordered_map m_pendingDoubleClicks; - AZ::ScriptTimePoint m_curTime; + AZStd::unordered_map m_pendingDoubleClicks; + + AZ::ScriptTimePoint m_currentTime; }; } // namespace SandboxEditor diff --git a/Code/Editor/ViewportTitleDlg.cpp b/Code/Editor/ViewportTitleDlg.cpp index 75d16e9a40..49459fad0f 100644 --- a/Code/Editor/ViewportTitleDlg.cpp +++ b/Code/Editor/ViewportTitleDlg.cpp @@ -138,14 +138,11 @@ CViewportTitleDlg::CViewportTitleDlg(QWidget* pParent) connect(this, &CViewportTitleDlg::ActionTriggered, MainWindow::instance()->GetActionManager(), &ActionManager::ActionTriggered); - AZ::VR::VREventBus::Handler::BusConnect(); - OnInitDialog(); } CViewportTitleDlg::~CViewportTitleDlg() { - AZ::VR::VREventBus::Handler::BusDisconnect(); GetISystem()->GetISystemEventDispatcher()->RemoveListener(this); GetIEditor()->UnregisterNotifyListener(this); @@ -236,10 +233,6 @@ void CViewportTitleDlg::SetupOverflowMenu() connect(m_audioMuteAction, &QAction::triggered, this, &CViewportTitleDlg::OnBnClickedMuteAudio); overFlowMenu->addAction(m_audioMuteAction); - m_enableVRAction = new QAction("Enable VR Preview", overFlowMenu); - connect(m_enableVRAction, &QAction::triggered, this, &CViewportTitleDlg::OnBnClickedEnableVR); - overFlowMenu->addAction(m_enableVRAction); - overFlowMenu->addSeparator(); m_enableGridSnappingAction = new QAction("Enable Grid Snapping", overFlowMenu); @@ -305,16 +298,6 @@ void CViewportTitleDlg::OnInitDialog() connect(displayInfoHelper, &CViewportTitleDlgDisplayInfoHelper::ViewportInfoStatusUpdated, this, &CViewportTitleDlg::UpdateDisplayInfo); UpdateDisplayInfo(); - // This is here just in case this class hasn't been created before - // a VR headset was initialized - m_enableVRAction->setEnabled(false); - if (AZ::VR::HMDDeviceRequestBus::GetTotalNumOfEventHandlers() != 0) - { - m_enableVRAction->setEnabled(true); - } - - AZ::VR::VREventBus::Handler::BusConnect(); - QFontMetrics metrics({}); int width = static_cast(metrics.boundingRect("-9999.99").width() * m_fieldWidthMultiplier); @@ -931,23 +914,6 @@ void CViewportTitleDlg::UpdateMuteActionText() } } -void CViewportTitleDlg::OnHMDInitialized() -{ - m_enableVRAction->setEnabled(true); -} - -void CViewportTitleDlg::OnHMDShutdown() -{ - m_enableVRAction->setEnabled(false); -} - -void CViewportTitleDlg::OnBnClickedEnableVR() -{ - gSettings.bEnableGameModeVR = !gSettings.bEnableGameModeVR; - - m_enableVRAction->setText(gSettings.bEnableGameModeVR ? tr("Disable VR Preview") : tr("Enable VR Preview")); -} - inline double Round(double fVal, double fStep) { if (fStep > 0.f) diff --git a/Code/Editor/ViewportTitleDlg.h b/Code/Editor/ViewportTitleDlg.h index 6996fe7750..a3e19837b4 100644 --- a/Code/Editor/ViewportTitleDlg.h +++ b/Code/Editor/ViewportTitleDlg.h @@ -22,7 +22,6 @@ #include #include -#include #endif // CViewportTitleDlg dialog @@ -44,7 +43,6 @@ class CViewportTitleDlg : public QWidget , public IEditorNotifyListener , public ISystemEventListener - , public AZ::VR::VREventBus::Handler { Q_OBJECT public: @@ -85,13 +83,6 @@ protected: void OnToggleHelpers(); void UpdateDisplayInfo(); - ////////////////////////////////////////////////////////////////////////// - /// VR Event Bus Implementation - ////////////////////////////////////////////////////////////////////////// - void OnHMDInitialized() override; - void OnHMDShutdown() override; - ////////////////////////////////////////////////////////////////////////// - void SetupCameraDropdownMenu(); void SetupResolutionDropdownMenu(); void SetupViewportInformationMenu(); @@ -140,7 +131,6 @@ protected: void OnBnClickedGotoPosition(); void OnBnClickedMuteAudio(); - void OnBnClickedEnableVR(); void UpdateMuteActionText(); @@ -168,7 +158,6 @@ protected: QAction* m_fullInformationAction = nullptr; QAction* m_compactInformationAction = nullptr; QAction* m_audioMuteAction = nullptr; - QAction* m_enableVRAction = nullptr; QAction* m_enableGridSnappingAction = nullptr; QAction* m_enableAngleSnappingAction = nullptr; QComboBox* m_cameraSpeed = nullptr; diff --git a/Code/Editor/WelcomeScreen/WelcomeScreenDialog.cpp b/Code/Editor/WelcomeScreen/WelcomeScreenDialog.cpp index 17f576b5ee..89dfcaffd1 100644 --- a/Code/Editor/WelcomeScreen/WelcomeScreenDialog.cpp +++ b/Code/Editor/WelcomeScreen/WelcomeScreenDialog.cpp @@ -25,8 +25,6 @@ #include -// AzFramework -#include // AzToolsFramework #include @@ -173,9 +171,6 @@ void WelcomeScreenDialog::SetRecentFileList(RecentFileList* pList) m_pRecentList = pList; - const char* engineRoot; - EBUS_EVENT_RESULT(engineRoot, AzFramework::ApplicationRequests::Bus, GetEngineRoot); - auto projectPath = AZ::Utils::GetProjectPath(); QString gamePath{projectPath.c_str()}; Path::ConvertSlashToBackSlash(gamePath); diff --git a/Code/Framework/AtomCore/Tests/Main.cpp b/Code/Framework/AtomCore/Tests/Main.cpp index 29ef408551..eb4c6bc835 100644 --- a/Code/Framework/AtomCore/Tests/Main.cpp +++ b/Code/Framework/AtomCore/Tests/Main.cpp @@ -7,7 +7,6 @@ */ -#include #include #include #include diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetCommon.cpp b/Code/Framework/AzCore/AzCore/Asset/AssetCommon.cpp index fdc3053b5c..bf3ad20768 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetCommon.cpp +++ b/Code/Framework/AzCore/AzCore/Asset/AssetCommon.cpp @@ -14,453 +14,450 @@ #include #include -namespace AZ +namespace AZ::Data { - namespace Data + AssetFilterInfo::AssetFilterInfo(const AssetId& id, const AssetType& assetType, AssetLoadBehavior loadBehavior) + : m_assetId(id) + , m_assetType(assetType) + , m_loadBehavior(loadBehavior) { - AssetFilterInfo::AssetFilterInfo(const AssetId& id, const AssetType& assetType, AssetLoadBehavior loadBehavior) - : m_assetId(id) - , m_assetType(assetType) - , m_loadBehavior(loadBehavior) + } + + AssetFilterInfo::AssetFilterInfo(const Asset& asset) + : m_assetId(asset.GetId()) + , m_assetType(asset.GetType()) + , m_loadBehavior(asset.GetAutoLoadBehavior()) + { + } + + + AssetId AssetId::CreateString(AZStd::string_view input) + { + size_t separatorIdx = input.find(':'); + if (separatorIdx == AZStd::string_view::npos) { + return AssetId(); } - AssetFilterInfo::AssetFilterInfo(const Asset& asset) - : m_assetId(asset.GetId()) - , m_assetType(asset.GetType()) - , m_loadBehavior(asset.GetAutoLoadBehavior()) + AssetId assetId; + assetId.m_guid = Uuid::CreateString(input.data(), separatorIdx); + if (assetId.m_guid.IsNull()) { + return AssetId(); } + assetId.m_subId = strtoul(&input[separatorIdx + 1], nullptr, 16); - AssetId AssetId::CreateString(AZStd::string_view input) + return assetId; + } + + void AssetId::Reflect(AZ::ReflectContext* context) + { + if (SerializeContext* serializeContext = azrtti_cast(context)) { - size_t separatorIdx = input.find(':'); - if (separatorIdx == AZStd::string_view::npos) - { - return AssetId(); - } - - AssetId assetId; - assetId.m_guid = Uuid::CreateString(input.data(), separatorIdx); - if (assetId.m_guid.IsNull()) - { - return AssetId(); - } - - assetId.m_subId = strtoul(&input[separatorIdx + 1], nullptr, 16); - - return assetId; + serializeContext->Class() + ->Version(1) + ->Field("guid", &Data::AssetId::m_guid) + ->Field("subId", &Data::AssetId::m_subId) + ; } - void AssetId::Reflect(AZ::ReflectContext* context) + if (BehaviorContext* behaviorContext = azrtti_cast(context)) { - if (SerializeContext* serializeContext = azrtti_cast(context)) - { - serializeContext->Class() - ->Version(1) - ->Field("guid", &Data::AssetId::m_guid) - ->Field("subId", &Data::AssetId::m_subId) - ; - } + behaviorContext->Class() + ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) + ->Attribute(AZ::Script::Attributes::Category, "Asset") + ->Attribute(AZ::Script::Attributes::Module, "asset") + ->Constructor() + ->Constructor() + ->Method("CreateString", &Data::AssetId::CreateString) + ->Method("IsValid", &Data::AssetId::IsValid) + ->Attribute(AZ::Script::Attributes::Alias, "is_valid") + ->Method("ToString", [](const Data::AssetId* self) { return self->ToString(); }) + ->Attribute(AZ::Script::Attributes::Alias, "to_string") + ->Method("IsEqual", [](const Data::AssetId& self, const Data::AssetId& other) { return self == other; }) + ->Attribute(AZ::Script::Attributes::Alias, "is_equal") + ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::Equal) + ; - if (BehaviorContext* behaviorContext = azrtti_cast(context)) - { - behaviorContext->Class() - ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) - ->Attribute(AZ::Script::Attributes::Category, "Asset") - ->Attribute(AZ::Script::Attributes::Module, "asset") - ->Constructor() - ->Constructor() - ->Method("CreateString", &Data::AssetId::CreateString) - ->Method("IsValid", &Data::AssetId::IsValid) - ->Attribute(AZ::Script::Attributes::Alias, "is_valid") - ->Method("ToString", [](const Data::AssetId* self) { return self->ToString(); }) - ->Attribute(AZ::Script::Attributes::Alias, "to_string") - ->Method("IsEqual", [](const Data::AssetId& self, const Data::AssetId& other) { return self == other; }) - ->Attribute(AZ::Script::Attributes::Alias, "is_equal") - ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::Equal) - ; + behaviorContext->Class() + ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) + ->Attribute(AZ::Script::Attributes::Category, "Asset") + ->Attribute(AZ::Script::Attributes::Module, "asset") + ->Property("assetId", BehaviorValueGetter(&Data::AssetInfo::m_assetId), nullptr) + ->Property("assetType", BehaviorValueGetter(&Data::AssetInfo::m_assetType), nullptr) + ->Property("sizeBytes", BehaviorValueGetter(&Data::AssetInfo::m_sizeBytes), nullptr) + ->Property("relativePath", BehaviorValueGetter(&Data::AssetInfo::m_relativePath), nullptr) + ; + } + } - behaviorContext->Class() - ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) - ->Attribute(AZ::Script::Attributes::Category, "Asset") - ->Attribute(AZ::Script::Attributes::Module, "asset") - ->Property("assetId", BehaviorValueGetter(&Data::AssetInfo::m_assetId), nullptr) - ->Property("assetType", BehaviorValueGetter(&Data::AssetInfo::m_assetType), nullptr) - ->Property("sizeBytes", BehaviorValueGetter(&Data::AssetInfo::m_sizeBytes), nullptr) - ->Property("relativePath", BehaviorValueGetter(&Data::AssetInfo::m_relativePath), nullptr) - ; - } + namespace AssetInternal + { + Asset FindOrCreateAsset(const AssetId& id, const AssetType& type, AssetLoadBehavior assetReferenceLoadBehavior) + { + return AssetManager::Instance().FindOrCreateAsset(id, type, assetReferenceLoadBehavior); } - namespace AssetInternal + Asset GetAsset(const AssetId& id, const AssetType& type, AssetLoadBehavior assetReferenceLoadBehavior, + const AssetLoadParameters& loadParams) { - Asset FindOrCreateAsset(const AssetId& id, const AssetType& type, AssetLoadBehavior assetReferenceLoadBehavior) - { - return AssetManager::Instance().FindOrCreateAsset(id, type, assetReferenceLoadBehavior); - } - - Asset GetAsset(const AssetId& id, const AssetType& type, AssetLoadBehavior assetReferenceLoadBehavior, - const AssetLoadParameters& loadParams) - { - return AssetManager::Instance().GetAsset(id, type, assetReferenceLoadBehavior, loadParams); - } - - AssetData::AssetStatus BlockUntilLoadComplete(const Asset& asset) - { - return AssetManager::Instance().BlockUntilLoadComplete(asset); - } - - void UpdateAssetInfo(AssetId& id, AZStd::string& assetHint) - { - // it is possible that the assetID given is legacy / old and we have a new assetId we can use instead for it. - // in that case, upgrade the AssetID to the new one, so that future saves are in the new format. - // this function should only be invoked if the feature is turned on in the asset manager as it can be (slightly) expensive - - if ((!AssetManager::IsReady()) || (!AssetManager::Instance().GetAssetInfoUpgradingEnabled())) - { - return; - } - - AZ::Data::AssetInfo assetInfo; - AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, id); - if (assetInfo.m_assetId.IsValid()) - { - id = assetInfo.m_assetId; - if (!assetInfo.m_relativePath.empty()) - { - assetHint = assetInfo.m_relativePath; - } - } - } - - bool ReloadAsset(AssetData* assetData, AssetLoadBehavior assetReferenceLoadBehavior) - { - AssetManager::Instance().ReloadAsset(assetData->GetId(), assetReferenceLoadBehavior); - return true; - } - - bool SaveAsset(AssetData* assetData, AssetLoadBehavior assetReferenceLoadBehavior) - { - AssetManager::Instance().SaveAsset({ assetData, assetReferenceLoadBehavior }); - return true; - } - - Asset GetAssetData(const AssetId& id, AssetLoadBehavior assetReferenceLoadBehavior) - { - if (AssetManager::IsReady()) - { - AZStd::lock_guard assetLock(AssetManager::Instance().m_assetMutex); - auto it = AssetManager::Instance().m_assets.find(id); - if (it != AssetManager::Instance().m_assets.end()) - { - return { it->second, assetReferenceLoadBehavior }; - } - } - return {}; - } - - AssetId ResolveAssetId(const AssetId& id) - { - AZ::Data::AssetInfo assetInfo; - AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, id); - if (assetInfo.m_assetId.IsValid()) - { - return assetInfo.m_assetId; - } - else - { - return id; - } - - } + return AssetManager::Instance().GetAsset(id, type, assetReferenceLoadBehavior, loadParams); } - AssetData::~AssetData() + AssetData::AssetStatus BlockUntilLoadComplete(const Asset& asset) { - UnregisterWithHandler(); + return AssetManager::Instance().BlockUntilLoadComplete(asset); } - void AssetData::Reflect(AZ::ReflectContext* context) + void UpdateAssetInfo(AssetId& id, AZStd::string& assetHint) { - if (SerializeContext* serializeContext = azrtti_cast(context)) + // it is possible that the assetID given is legacy / old and we have a new assetId we can use instead for it. + // in that case, upgrade the AssetID to the new one, so that future saves are in the new format. + // this function should only be invoked if the feature is turned on in the asset manager as it can be (slightly) expensive + + if ((!AssetManager::IsReady()) || (!AssetManager::Instance().GetAssetInfoUpgradingEnabled())) { - serializeContext->Class() - ->Version(1) - ; - } - - if (BehaviorContext* behaviorContext = azrtti_cast(context)) - { - behaviorContext->Class("AssetData") - ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) - ->Attribute(AZ::Script::Attributes::Category, "Asset") - ->Attribute(AZ::Script::Attributes::Module, "asset") - ->Method("IsReady", &AssetData::IsReady) - ->Attribute(AZ::Script::Attributes::Alias, "is_ready") - ->Method("IsError", &AssetData::IsError) - ->Attribute(AZ::Script::Attributes::Alias, "is_error") - ->Method("IsLoading", &AssetData::IsLoading) - ->Attribute(AZ::Script::Attributes::Alias, "is_loading") - ->Method("GetId", &AssetData::GetId) - ->Attribute(AZ::Script::Attributes::Alias, "get_id") - ->Method("GetUseCount", &AssetData::GetUseCount) - ->Attribute(AZ::Script::Attributes::Alias, "get_use_count") - ; - } - } - - void AssetData::Acquire() - { - AZ_Assert(m_useCount >= 0, "AssetData has been deleted"); - - AcquireWeak(); - ++m_useCount; - } - - void AssetData::Release() - { - AZ_Assert(m_useCount > 0, "Usecount is already 0!"); - - if (m_useCount.fetch_sub(1) == 1) - { - if (AssetManager::IsReady()) - { - AssetManager::Instance().OnAssetUnused(this); - } - else - { - AZ_Assert(false, "Attempting to release asset after AssetManager has been destroyed!"); - } - } - - ReleaseWeak(); - } - - void AssetData::AcquireWeak() - { - AZ_Assert(m_useCount >= 0, "AssetData has been deleted"); - ++m_weakUseCount; - } - - void AssetData::ReleaseWeak() - { - AZ_Assert(m_weakUseCount > 0, "WeakUseCount is already 0"); - - AssetId assetId = m_assetId; - int creationToken = m_creationToken; - AssetType assetType = GetType(); - bool removeFromHash = IsRegisterReadonlyAndShareable(); - // default creation token implies that the asset was not created by the asset manager and therefore it cannot be in the asset map. - removeFromHash = creationToken == s_defaultCreationToken ? false : removeFromHash; - - if (m_weakUseCount.fetch_sub(1) == 1) - { - if (AssetManager::IsReady()) - { - AssetManager::Instance().ReleaseAsset(this, assetId, assetType, removeFromHash, creationToken); - } - else - { - AZ_Assert(false, "Attempting to release asset after AssetManager has been destroyed!"); - } - } - } - - bool AssetData::IsLoading(bool includeQueued) const - { - auto curStatus = GetStatus(); - return(curStatus == AssetStatus::Loading || curStatus == AssetStatus::LoadedPreReady || curStatus==AssetStatus::StreamReady || - (includeQueued && curStatus == AssetStatus::Queued)); - } - - void AssetData::RegisterWithHandler(AssetHandler* handler) - { - if (!handler) - { - AZ_Error("AssetData", false, "No handler to register with"); return; } - m_registeredHandler = handler; - } - void AssetData::UnregisterWithHandler() - { - if (m_registeredHandler) + AZ::Data::AssetInfo assetInfo; + AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, id); + if (assetInfo.m_assetId.IsValid()) { - m_registeredHandler = nullptr; - } - } - - bool AssetData::GetFlag(const AssetDataFlags& checkFlag) const - { - return m_flags[aznumeric_cast(checkFlag)]; - } - - void AssetData::SetFlag(const AssetDataFlags& checkFlag, bool setValue) - { - m_flags.set(aznumeric_cast(checkFlag), setValue); - } - - bool AssetData::GetRequeue() const - { - return GetFlag(AssetDataFlags::Requeue); - } - void AssetData::SetRequeue(bool requeue) - { - SetFlag(AssetDataFlags::Requeue, requeue); - } - - void AssetBusCallbacks::SetCallbacks(const AssetReadyCB& readyCB, const AssetMovedCB& movedCB, const AssetReloadedCB& reloadedCB, - const AssetSavedCB& savedCB, const AssetUnloadedCB& unloadedCB, const AssetErrorCB& errorCB, const AssetCanceledCB& cancelCB) - { - m_onAssetReadyCB = readyCB; - m_onAssetMovedCB = movedCB; - m_onAssetReloadedCB = reloadedCB; - m_onAssetSavedCB = savedCB; - m_onAssetUnloadedCB = unloadedCB; - m_onAssetErrorCB = errorCB; - m_onAssetCanceledCB = cancelCB; - } - - void AssetBusCallbacks::ClearCallbacks() - { - SetCallbacks(AssetBusCallbacks::AssetReadyCB(), - AssetBusCallbacks::AssetMovedCB(), - AssetBusCallbacks::AssetReloadedCB(), - AssetBusCallbacks::AssetSavedCB(), - AssetBusCallbacks::AssetUnloadedCB(), - AssetBusCallbacks::AssetErrorCB(), - AssetBusCallbacks::AssetCanceledCB()); - } - - - void AssetBusCallbacks::SetOnAssetReadyCallback(const AssetReadyCB& readyCB) - { - m_onAssetReadyCB = readyCB; - } - - void AssetBusCallbacks::SetOnAssetMovedCallback(const AssetMovedCB& movedCB) - { - m_onAssetMovedCB = movedCB; - } - - void AssetBusCallbacks::SetOnAssetReloadedCallback(const AssetReloadedCB& reloadedCB) - { - m_onAssetReloadedCB = reloadedCB; - } - - void AssetBusCallbacks::SetOnAssetSavedCallback(const AssetSavedCB& savedCB) - { - m_onAssetSavedCB = savedCB; - } - - void AssetBusCallbacks::SetOnAssetUnloadedCallback(const AssetUnloadedCB& unloadedCB) - { - m_onAssetUnloadedCB = unloadedCB; - } - - void AssetBusCallbacks::SetOnAssetErrorCallback(const AssetErrorCB& errorCB) - { - m_onAssetErrorCB = errorCB; - } - - void AssetBusCallbacks::SetOnAssetCanceledCallback(const AssetCanceledCB& cancelCB) - { - m_onAssetCanceledCB = cancelCB; - } - - void AssetBusCallbacks::OnAssetReady(Asset asset) - { - if (m_onAssetReadyCB) - { - m_onAssetReadyCB(asset, *this); - } - } - - void AssetBusCallbacks::OnAssetMoved(Asset asset, void* oldDataPointer) - { - if (m_onAssetMovedCB) - { - m_onAssetMovedCB(asset, oldDataPointer, *this); - } - } - - void AssetBusCallbacks::OnAssetReloaded(Asset asset) - { - if (m_onAssetReloadedCB) - { - m_onAssetReloadedCB(asset, *this); - } - } - - void AssetBusCallbacks::OnAssetSaved(Asset asset, bool isSuccessful) - { - if (m_onAssetSavedCB) - { - m_onAssetSavedCB(asset, isSuccessful, *this); - } - } - - void AssetBusCallbacks::OnAssetUnloaded(const AssetId assetId, const AssetType assetType) - { - if (m_onAssetUnloadedCB) - { - m_onAssetUnloadedCB(assetId, assetType, *this); - } - } - - void AssetBusCallbacks::OnAssetError(Asset asset) - { - if (m_onAssetErrorCB) - { - m_onAssetErrorCB(asset, *this); - } - } - - void AssetBusCallbacks::OnAssetCanceled(const AssetId assetId) - { - if (m_onAssetCanceledCB) - { - m_onAssetCanceledCB(assetId, *this); - } - } - - /*static*/ bool AssetFilterNoAssetLoading([[maybe_unused]] const AssetFilterInfo& filterInfo) - { - return false; - } - namespace ProductDependencyInfo - { - AZ::Data::AssetLoadBehavior LoadBehaviorFromFlags(const ProductDependencyFlags& dependencyFlags) - { - AZ::u8 loadBehaviorValue = 0; - for (AZ::u8 thisFlag = aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorLow); - thisFlag <= aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorHigh); ++thisFlag) + id = assetInfo.m_assetId; + if (!assetInfo.m_relativePath.empty()) { - if (dependencyFlags[thisFlag]) - { - loadBehaviorValue |= (1 << thisFlag); - } + assetHint = assetInfo.m_relativePath; } - return static_cast(loadBehaviorValue); - } - - ProductDependencyFlags CreateFlags(AZ::Data::AssetLoadBehavior autoLoadBehavior) - { - AZ::Data::ProductDependencyInfo::ProductDependencyFlags returnFlags; - AZ::u8 loadBehavior = aznumeric_caster(autoLoadBehavior); - for (AZ::u8 thisFlag = aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorLow); - thisFlag <= aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorHigh); ++thisFlag) - { - if (loadBehavior & (1 << thisFlag)) - { - returnFlags[thisFlag] = true; - } - } - return returnFlags; } } - } // namespace Data -} // namespace AZ + + bool ReloadAsset(AssetData* assetData, AssetLoadBehavior assetReferenceLoadBehavior) + { + AssetManager::Instance().ReloadAsset(assetData->GetId(), assetReferenceLoadBehavior); + return true; + } + + bool SaveAsset(AssetData* assetData, AssetLoadBehavior assetReferenceLoadBehavior) + { + AssetManager::Instance().SaveAsset({ assetData, assetReferenceLoadBehavior }); + return true; + } + + Asset GetAssetData(const AssetId& id, AssetLoadBehavior assetReferenceLoadBehavior) + { + if (AssetManager::IsReady()) + { + AZStd::lock_guard assetLock(AssetManager::Instance().m_assetMutex); + auto it = AssetManager::Instance().m_assets.find(id); + if (it != AssetManager::Instance().m_assets.end()) + { + return { it->second, assetReferenceLoadBehavior }; + } + } + return {}; + } + + AssetId ResolveAssetId(const AssetId& id) + { + AZ::Data::AssetInfo assetInfo; + AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, id); + if (assetInfo.m_assetId.IsValid()) + { + return assetInfo.m_assetId; + } + else + { + return id; + } + + } + } + + AssetData::~AssetData() + { + UnregisterWithHandler(); + } + + void AssetData::Reflect(AZ::ReflectContext* context) + { + if (SerializeContext* serializeContext = azrtti_cast(context)) + { + serializeContext->Class() + ->Version(1) + ; + } + + if (BehaviorContext* behaviorContext = azrtti_cast(context)) + { + behaviorContext->Class("AssetData") + ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) + ->Attribute(AZ::Script::Attributes::Category, "Asset") + ->Attribute(AZ::Script::Attributes::Module, "asset") + ->Method("IsReady", &AssetData::IsReady) + ->Attribute(AZ::Script::Attributes::Alias, "is_ready") + ->Method("IsError", &AssetData::IsError) + ->Attribute(AZ::Script::Attributes::Alias, "is_error") + ->Method("IsLoading", &AssetData::IsLoading) + ->Attribute(AZ::Script::Attributes::Alias, "is_loading") + ->Method("GetId", &AssetData::GetId) + ->Attribute(AZ::Script::Attributes::Alias, "get_id") + ->Method("GetUseCount", &AssetData::GetUseCount) + ->Attribute(AZ::Script::Attributes::Alias, "get_use_count") + ; + } + } + + void AssetData::Acquire() + { + AZ_Assert(m_useCount >= 0, "AssetData has been deleted"); + + AcquireWeak(); + ++m_useCount; + } + + void AssetData::Release() + { + AZ_Assert(m_useCount > 0, "Usecount is already 0!"); + + if (m_useCount.fetch_sub(1) == 1) + { + if (AssetManager::IsReady()) + { + AssetManager::Instance().OnAssetUnused(this); + } + else + { + AZ_Assert(false, "Attempting to release asset after AssetManager has been destroyed!"); + } + } + + ReleaseWeak(); + } + + void AssetData::AcquireWeak() + { + AZ_Assert(m_useCount >= 0, "AssetData has been deleted"); + ++m_weakUseCount; + } + + void AssetData::ReleaseWeak() + { + AZ_Assert(m_weakUseCount > 0, "WeakUseCount is already 0"); + + AssetId assetId = m_assetId; + int creationToken = m_creationToken; + AssetType assetType = GetType(); + bool removeFromHash = IsRegisterReadonlyAndShareable(); + // default creation token implies that the asset was not created by the asset manager and therefore it cannot be in the asset map. + removeFromHash = creationToken == s_defaultCreationToken ? false : removeFromHash; + + if (m_weakUseCount.fetch_sub(1) == 1) + { + if (AssetManager::IsReady()) + { + AssetManager::Instance().ReleaseAsset(this, assetId, assetType, removeFromHash, creationToken); + } + else + { + AZ_Assert(false, "Attempting to release asset after AssetManager has been destroyed!"); + } + } + } + + bool AssetData::IsLoading(bool includeQueued) const + { + auto curStatus = GetStatus(); + return(curStatus == AssetStatus::Loading || curStatus == AssetStatus::LoadedPreReady || curStatus==AssetStatus::StreamReady || + (includeQueued && curStatus == AssetStatus::Queued)); + } + + void AssetData::RegisterWithHandler(AssetHandler* handler) + { + if (!handler) + { + AZ_Error("AssetData", false, "No handler to register with"); + return; + } + m_registeredHandler = handler; + } + + void AssetData::UnregisterWithHandler() + { + if (m_registeredHandler) + { + m_registeredHandler = nullptr; + } + } + + bool AssetData::GetFlag(const AssetDataFlags& checkFlag) const + { + return m_flags[aznumeric_cast(checkFlag)]; + } + + void AssetData::SetFlag(const AssetDataFlags& checkFlag, bool setValue) + { + m_flags.set(aznumeric_cast(checkFlag), setValue); + } + + bool AssetData::GetRequeue() const + { + return GetFlag(AssetDataFlags::Requeue); + } + void AssetData::SetRequeue(bool requeue) + { + SetFlag(AssetDataFlags::Requeue, requeue); + } + + void AssetBusCallbacks::SetCallbacks(const AssetReadyCB& readyCB, const AssetMovedCB& movedCB, const AssetReloadedCB& reloadedCB, + const AssetSavedCB& savedCB, const AssetUnloadedCB& unloadedCB, const AssetErrorCB& errorCB, const AssetCanceledCB& cancelCB) + { + m_onAssetReadyCB = readyCB; + m_onAssetMovedCB = movedCB; + m_onAssetReloadedCB = reloadedCB; + m_onAssetSavedCB = savedCB; + m_onAssetUnloadedCB = unloadedCB; + m_onAssetErrorCB = errorCB; + m_onAssetCanceledCB = cancelCB; + } + + void AssetBusCallbacks::ClearCallbacks() + { + SetCallbacks(AssetBusCallbacks::AssetReadyCB(), + AssetBusCallbacks::AssetMovedCB(), + AssetBusCallbacks::AssetReloadedCB(), + AssetBusCallbacks::AssetSavedCB(), + AssetBusCallbacks::AssetUnloadedCB(), + AssetBusCallbacks::AssetErrorCB(), + AssetBusCallbacks::AssetCanceledCB()); + } + + + void AssetBusCallbacks::SetOnAssetReadyCallback(const AssetReadyCB& readyCB) + { + m_onAssetReadyCB = readyCB; + } + + void AssetBusCallbacks::SetOnAssetMovedCallback(const AssetMovedCB& movedCB) + { + m_onAssetMovedCB = movedCB; + } + + void AssetBusCallbacks::SetOnAssetReloadedCallback(const AssetReloadedCB& reloadedCB) + { + m_onAssetReloadedCB = reloadedCB; + } + + void AssetBusCallbacks::SetOnAssetSavedCallback(const AssetSavedCB& savedCB) + { + m_onAssetSavedCB = savedCB; + } + + void AssetBusCallbacks::SetOnAssetUnloadedCallback(const AssetUnloadedCB& unloadedCB) + { + m_onAssetUnloadedCB = unloadedCB; + } + + void AssetBusCallbacks::SetOnAssetErrorCallback(const AssetErrorCB& errorCB) + { + m_onAssetErrorCB = errorCB; + } + + void AssetBusCallbacks::SetOnAssetCanceledCallback(const AssetCanceledCB& cancelCB) + { + m_onAssetCanceledCB = cancelCB; + } + + void AssetBusCallbacks::OnAssetReady(Asset asset) + { + if (m_onAssetReadyCB) + { + m_onAssetReadyCB(asset, *this); + } + } + + void AssetBusCallbacks::OnAssetMoved(Asset asset, void* oldDataPointer) + { + if (m_onAssetMovedCB) + { + m_onAssetMovedCB(asset, oldDataPointer, *this); + } + } + + void AssetBusCallbacks::OnAssetReloaded(Asset asset) + { + if (m_onAssetReloadedCB) + { + m_onAssetReloadedCB(asset, *this); + } + } + + void AssetBusCallbacks::OnAssetSaved(Asset asset, bool isSuccessful) + { + if (m_onAssetSavedCB) + { + m_onAssetSavedCB(asset, isSuccessful, *this); + } + } + + void AssetBusCallbacks::OnAssetUnloaded(const AssetId assetId, const AssetType assetType) + { + if (m_onAssetUnloadedCB) + { + m_onAssetUnloadedCB(assetId, assetType, *this); + } + } + + void AssetBusCallbacks::OnAssetError(Asset asset) + { + if (m_onAssetErrorCB) + { + m_onAssetErrorCB(asset, *this); + } + } + + void AssetBusCallbacks::OnAssetCanceled(const AssetId assetId) + { + if (m_onAssetCanceledCB) + { + m_onAssetCanceledCB(assetId, *this); + } + } + + /*static*/ bool AssetFilterNoAssetLoading([[maybe_unused]] const AssetFilterInfo& filterInfo) + { + return false; + } + namespace ProductDependencyInfo + { + AZ::Data::AssetLoadBehavior LoadBehaviorFromFlags(const ProductDependencyFlags& dependencyFlags) + { + AZ::u8 loadBehaviorValue = 0; + for (AZ::u8 thisFlag = aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorLow); + thisFlag <= aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorHigh); ++thisFlag) + { + if (dependencyFlags[thisFlag]) + { + loadBehaviorValue |= (1 << thisFlag); + } + } + return static_cast(loadBehaviorValue); + } + + ProductDependencyFlags CreateFlags(AZ::Data::AssetLoadBehavior autoLoadBehavior) + { + AZ::Data::ProductDependencyInfo::ProductDependencyFlags returnFlags; + AZ::u8 loadBehavior = aznumeric_caster(autoLoadBehavior); + for (AZ::u8 thisFlag = aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorLow); + thisFlag <= aznumeric_cast(ProductDependencyFlagBits::LoadBehaviorHigh); ++thisFlag) + { + if (loadBehavior & (1 << thisFlag)) + { + returnFlags[thisFlag] = true; + } + } + return returnFlags; + } + } +} // namespace AZ::Data diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetCommon.h b/Code/Framework/AzCore/AzCore/Asset/AssetCommon.h index 0c8e5209ca..c45bb21c6d 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetCommon.h +++ b/Code/Framework/AzCore/AzCore/Asset/AssetCommon.h @@ -556,16 +556,24 @@ namespace AZ Asset assetData(AssetInternal::GetAssetData(actualId, AZ::Data::AssetLoadBehavior::Default)); if (assetData) { - auto curStatus = assetData->GetStatus(); + auto isReady = assetData->GetStatus() == AssetData::AssetStatus::Ready; bool isError = assetData->IsError(); - connectLock.unlock(); - if (curStatus == AssetData::AssetStatus::Ready) + + if (isReady || isError) { - handler->OnAssetReady(assetData); - } - else if (isError) - { - handler->OnAssetError(assetData); + connectLock.unlock(); + + if (isReady) + { + handler->OnAssetReady(assetData); + } + else if (isError) + { + handler->OnAssetError(assetData); + } + + // Lock the mutex again since some destructors will be modifying the context afterwards + connectLock.lock(); } } } diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetContainer.cpp b/Code/Framework/AzCore/AzCore/Asset/AssetContainer.cpp index ce15c7bc4e..bab161fc0a 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetContainer.cpp +++ b/Code/Framework/AzCore/AzCore/Asset/AssetContainer.cpp @@ -11,466 +11,469 @@ #include #include -namespace AZ +namespace AZ::Data { - namespace Data + AssetContainer::AssetContainer(Asset rootAsset, const AssetLoadParameters& loadParams) { - AssetContainer::AssetContainer(Asset rootAsset, const AssetLoadParameters& loadParams) - { - m_rootAsset = AssetInternal::WeakAsset(rootAsset); - m_containerAssetId = m_rootAsset.GetId(); + m_rootAsset = AssetInternal::WeakAsset(rootAsset); + m_containerAssetId = m_rootAsset.GetId(); - AddDependentAssets(rootAsset, loadParams); + AddDependentAssets(rootAsset, loadParams); + } + + AssetContainer::~AssetContainer() + { + // Validate that if the AssetManager is performing normal processing duties, the AssetContainer is only destroyed once all + // dependent asset loads have completed. + if (AssetManager::IsReady() && !AssetManager::Instance().ShouldCancelAllActiveJobs()) + { + AZ_Assert(m_waitingCount == 0, "Container destroyed while dependent assets are still loading. The dependent assets may " + "end up in a perpetual loading state if there is no top-level container signalling the completion of the full load."); } - AssetContainer::~AssetContainer() - { - // Validate that if the AssetManager is performing normal processing duties, the AssetContainer is only destroyed once all - // dependent asset loads have completed. - if (AssetManager::IsReady() && !AssetManager::Instance().ShouldCancelAllActiveJobs()) - { - AZ_Assert(m_waitingCount == 0, "Container destroyed while dependent assets are still loading. The dependent assets may " - "end up in a perpetual loading state if there is no top-level container signalling the completion of the full load."); - } + AssetBus::MultiHandler::BusDisconnect(); + AssetLoadBus::MultiHandler::BusDisconnect(); + } - AssetBus::MultiHandler::BusDisconnect(); - AssetLoadBus::MultiHandler::BusDisconnect(); + AZStd::vector>> AssetContainer::CreateAndQueueDependentAssets( + const AZStd::vector& dependencyInfoList, const AssetLoadParameters& loadParamsCopyWithNoLoadingFilter) + { + AZStd::vector>> dependencyAssets; + + for (auto& thisInfo : dependencyInfoList) + { + auto dependentAsset = AssetManager::Instance().FindOrCreateAsset( + thisInfo.m_assetId, thisInfo.m_assetType, AZ::Data::AssetLoadBehavior::Default); + + if (!dependentAsset || !dependentAsset.GetId().IsValid()) + { + AZ_Warning("AssetContainer", false, "Dependency Asset %s (%s) was not found\n", + thisInfo.m_assetId.ToString().c_str(), thisInfo.m_relativePath.c_str()); + RemoveWaitingAsset(thisInfo.m_assetId); + continue; + } + dependencyAssets.emplace_back(thisInfo, AZStd::move(dependentAsset)); } - void AssetContainer::AddDependentAssets(Asset rootAsset, const AssetLoadParameters& loadParams) + // Queue the loading of all of the dependent assets before loading the root asset. + for (auto& [dependentAssetInfo, dependentAsset] : dependencyAssets) { - AssetId rootAssetId = rootAsset.GetId(); - AssetType rootAssetType = rootAsset.GetType(); + // Queue each asset to load. + auto queuedDependentAsset = AssetManager::Instance().GetAssetInternal( + dependentAsset.GetId(), dependentAsset.GetType(), + AZ::Data::AssetLoadBehavior::Default, loadParamsCopyWithNoLoadingFilter, + dependentAssetInfo, HasPreloads(dependentAsset.GetId())); - // Every asset we're going to be waiting on a load for - the root and all valid dependencies - AZStd::vector waitingList; - waitingList.push_back(rootAssetId); + // Verify that the returned asset reference matches the one that we found or created and queued to load. + AZ_Assert(dependentAsset == queuedDependentAsset, "GetAssetInternal returned an unexpected asset reference for Asset %s", + dependentAsset.GetId().ToString().c_str()); + } - // Every asset dependency that we're aware of, whether or not it gets filtered out by the asset filter callback. - // This will be used at the point that asset references get serialized in to see whether or not we've received any - // unexpected assets that didn't appear in our asset catalog dependency list that need to be loaded anyways. - AZStd::vector handledAssetDependencyList; + return dependencyAssets; + } - // Cached AssetInfo to save another lookup inside Assetmanager - AZStd::vector dependencyInfoList; - Outcome, AZStd::string> getDependenciesResult = Failure(AZStd::string()); + void AssetContainer::AddDependentAssets(Asset rootAsset, const AssetLoadParameters& loadParams) + { + AssetId rootAssetId = rootAsset.GetId(); + AssetType rootAssetType = rootAsset.GetType(); - // Track preloads in an additional list - they're in our waiting/dependencyInfo lists as well, but preloads require us to - // suppress emitting "AssetReady" until everything we care about in this context is ready - PreloadAssetListType preloadDependencies; - if (loadParams.m_dependencyRules == AssetDependencyLoadRules::UseLoadBehavior) - { - AZStd::unordered_set noloadDependencies; - AssetCatalogRequestBus::BroadcastResult(getDependenciesResult, &AssetCatalogRequestBus::Events::GetLoadBehaviorProductDependencies, - rootAssetId, noloadDependencies, preloadDependencies); - if (!noloadDependencies.empty()) - { - AZStd::lock_guard dependencyLock(m_dependencyMutex); - m_unloadedDependencies.insert(noloadDependencies.begin(), noloadDependencies.end()); - } - } - else if (loadParams.m_dependencyRules == AssetDependencyLoadRules::LoadAll) - { - AssetCatalogRequestBus::BroadcastResult(getDependenciesResult, &AssetCatalogRequestBus::Events::GetAllProductDependencies, rootAssetId); - } - // Do as much validation of dependencies as we can before the AddWaitingAssets and GetAsset calls for dependencies below - if (getDependenciesResult.IsSuccess()) - { - for (const auto& thisAsset : getDependenciesResult.GetValue()) - { - AssetInfo assetInfo; - AssetCatalogRequestBus::BroadcastResult(assetInfo, &AssetCatalogRequestBus::Events::GetAssetInfoById, thisAsset.m_assetId); + // Every asset we're going to be waiting on a load for - the root and all valid dependencies + AZStd::vector waitingList; + waitingList.push_back(rootAssetId); - // No matter whether or not the asset dependency is valid, loaded, or filtered out, mark it as successfully handled. - // When we encounter the asset reference during serialization, we will know that it should intentionally be skipped. - // Otherwise, it would be treated as a missing dependency and assert. - handledAssetDependencyList.emplace_back(thisAsset.m_assetId); + // Every asset dependency that we're aware of, whether or not it gets filtered out by the asset filter callback. + // This will be used at the point that asset references get serialized in to see whether or not we've received any + // unexpected assets that didn't appear in our asset catalog dependency list that need to be loaded anyways. + AZStd::vector handledAssetDependencyList; - if (!assetInfo.m_assetId.IsValid()) - { - // Handlers may just not currently be around for a given asset type so we only warn here - AZ_Warning("AssetContainer", false, "Asset %s (%s) references/depends on asset %s which does not exist in the catalog and cannot be loaded.", - rootAsset.GetHint().c_str(), - rootAssetId.ToString().c_str(), - thisAsset.m_assetId.ToString().c_str()); - m_invalidDependencies++; - continue; - } - if (assetInfo.m_assetId == rootAssetId) - { - // Circular dependencies in our graph need to be raised as errors as they could cause problems elsewhere - AZ_Error("AssetContainer", false, "Circular dependency found under asset %s", rootAssetId.ToString().c_str()); - m_invalidDependencies++; - continue; - } - if (!AssetManager::Instance().GetHandler(assetInfo.m_assetType)) - { - // Handlers may just not currently be around for a given asset type so we only warn here - m_invalidDependencies++; - continue; - } - if (loadParams.m_assetLoadFilterCB) - { - if (!loadParams.m_assetLoadFilterCB({thisAsset.m_assetId, assetInfo.m_assetType, - AZ::Data::ProductDependencyInfo::LoadBehaviorFromFlags(thisAsset.m_flags) })) - { - continue; - } - } - dependencyInfoList.push_back(assetInfo); - } - } - for (auto& thisInfo : dependencyInfoList) - { - waitingList.push_back(thisInfo.m_assetId); - } + // Cached AssetInfo to save another lookup inside Assetmanager + AZStd::vector dependencyInfoList; + Outcome, AZStd::string> getDependenciesResult = Failure(AZStd::string()); - // Add waiting assets ahead of time to hear signals for any which may already be loading - AddWaitingAssets(waitingList); - SetupPreloadLists(move(preloadDependencies), rootAssetId); - - auto loadParamsCopyWithNoLoadingFilter = loadParams; - - // All asset dependencies below the root asset should be provided by the asset catalog, and therefore should *not* - // get triggered to load when the asset reference is serialized in. However, it's useful to detect, warn, and handle - // the case where the asset dependencies are NOT set up correctly. - loadParamsCopyWithNoLoadingFilter.m_assetLoadFilterCB = [handledAssetDependencyList](const AssetFilterInfo& filterInfo) - { - // NoLoad dependencies should always get filtered out and not loaded. - if (filterInfo.m_loadBehavior == AZ::Data::AssetLoadBehavior::NoLoad) - { - return false; - } - - // In the normal case, the dependent asset appears in the handled asset list, and we should return false so that - // the asset isn't attempted to be loaded, since the asset will already be triggered to get loaded or was possibly - // already filtered out by the load filter callback. - // In the error case, the asset dependencies haven't been produced by the builder correctly, so assets - // have shown up that the asset container hasn't triggered to load and isn't listening for. Assert that this case - // has happened so that the builder for this asset type can be fixed. - // Ideally we would proceed forward and load them by returning "true", but the triggered load would use this lambda - // function as the asset load filter for that load as well, which isn't correct. If we ever want to support that - // behavior, we would need to rework the way filters work as well as the code in AssetSerializer.cpp to pass down - // the loadParams.m_assetLoadFilterCB that was passed into the AddDependentAssets() methods to use as the dependent - // asset filter instead of this lambda function. - AZ_UNUSED(handledAssetDependencyList); // Prevent unused warning in release builds - AZ_Assert(AZStd::find(handledAssetDependencyList.begin(), handledAssetDependencyList.end(), filterInfo.m_assetId) != - handledAssetDependencyList.end(), - "Dependent Asset ID (%s) is expected to load, but the Asset Catalog has no dependency recorded. " - "Examine the asset builder for the asset relying on this to ensure it is generating the correct dependencies.", - filterInfo.m_assetId.ToString().c_str()); - - // The dependent asset should have already been created and at least queued to load prior to reaching this point. - // The asset serializer needs to get a successful result from FindAsset(), or else our asset reference will fail - // to point to the asset data once it is loaded. - if (!Data::AssetManager::Instance().FindAsset(filterInfo.m_assetId, AZ::Data::AssetLoadBehavior::Default)) - { - AZ_Assert(!Data::AssetManager::Instance().FindAsset(filterInfo.m_assetId, AZ::Data::AssetLoadBehavior::Default), - "Dependent Asset ID (%s) can't be found in the AssetManager, which means the asset referencing it has probably " - "started loading before the dependent asset has been queued to load. Verify that the asset dependencies have " - "been created correctly for the parent asset.", - filterInfo.m_assetId.ToString().c_str()); - } - - return false; - }; - - // This will contain the list of dependent assets that have been created (or found) and queued to load. - // We also keep a copy of the AssetInfo structure as a small optimization to avoid a redundant lookup in GetAssetInternal. - AZStd::vector>> dependencyAssets; - - // Make sure all the dependencies are created first before we try to load them. - // Since we've set the load filter to not load dependencies, we need to ensure all the assets are created beforehand - // so the dependencies can be hooked up as soon as each asset gets serialized in, even if they start getting serialized - // while we're still in the middle of triggering all of the asset loads below. - for (auto& thisInfo : dependencyInfoList) - { - auto dependentAsset = AssetManager::Instance().FindOrCreateAsset( - thisInfo.m_assetId, thisInfo.m_assetType, AZ::Data::AssetLoadBehavior::Default); - - if (!dependentAsset || !dependentAsset.GetId().IsValid()) - { - AZ_Warning("AssetContainer", false, "Dependency Asset %s (%s) was not found\n", - thisInfo.m_assetId.ToString().c_str(), thisInfo.m_relativePath.c_str()); - RemoveWaitingAsset(thisInfo.m_assetId); - continue; - } - dependencyAssets.emplace_back(thisInfo, AZStd::move(dependentAsset)); - } - - // Queue the loading of all of the dependent assets before loading the root asset. - for (auto& [dependentAssetInfo, dependentAsset] : dependencyAssets) - { - // Queue each asset to load. - auto queuedDependentAsset = AssetManager::Instance().GetAssetInternal( - dependentAsset.GetId(), dependentAsset.GetType(), - AZ::Data::AssetLoadBehavior::Default, loadParamsCopyWithNoLoadingFilter, - dependentAssetInfo, HasPreloads(dependentAsset.GetId())); - - // Verify that the returned asset reference matches the one that we found or created and queued to load. - AZ_Assert(dependentAsset == queuedDependentAsset, "GetAssetInternal returned an unexpected asset reference for Asset %s", - dependentAsset.GetId().ToString().c_str()); - } - - // Add all of the queued dependent assets as dependencies + // Track preloads in an additional list - they're in our waiting/dependencyInfo lists as well, but preloads require us to + // suppress emitting "AssetReady" until everything we care about in this context is ready + PreloadAssetListType preloadDependencies; + if (loadParams.m_dependencyRules == AssetDependencyLoadRules::UseLoadBehavior) + { + AZStd::unordered_set noloadDependencies; + AssetCatalogRequestBus::BroadcastResult(getDependenciesResult, &AssetCatalogRequestBus::Events::GetLoadBehaviorProductDependencies, + rootAssetId, noloadDependencies, preloadDependencies); + if (!noloadDependencies.empty()) { AZStd::lock_guard dependencyLock(m_dependencyMutex); - for (auto& [dependentAssetInfo, dependentAsset] : dependencyAssets) + m_unloadedDependencies.insert(noloadDependencies.begin(), noloadDependencies.end()); + } + } + else if (loadParams.m_dependencyRules == AssetDependencyLoadRules::LoadAll) + { + AssetCatalogRequestBus::BroadcastResult(getDependenciesResult, &AssetCatalogRequestBus::Events::GetAllProductDependencies, rootAssetId); + } + // Do as much validation of dependencies as we can before the AddWaitingAssets and GetAsset calls for dependencies below + if (getDependenciesResult.IsSuccess()) + { + for (const auto& thisAsset : getDependenciesResult.GetValue()) + { + AssetInfo assetInfo; + AssetCatalogRequestBus::BroadcastResult(assetInfo, &AssetCatalogRequestBus::Events::GetAssetInfoById, thisAsset.m_assetId); + + // No matter whether or not the asset dependency is valid, loaded, or filtered out, mark it as successfully handled. + // When we encounter the asset reference during serialization, we will know that it should intentionally be skipped. + // Otherwise, it would be treated as a missing dependency and assert. + handledAssetDependencyList.emplace_back(thisAsset.m_assetId); + + if (!assetInfo.m_assetId.IsValid()) { - AddDependency(AZStd::move(dependentAsset)); + // Handlers may just not currently be around for a given asset type so we only warn here + AZ_Warning("AssetContainer", false, "Asset %s (%s) references/depends on asset %s which does not exist in the catalog and cannot be loaded.", + rootAsset.GetHint().c_str(), + rootAssetId.ToString().c_str(), + thisAsset.m_assetId.ToString().c_str()); + m_invalidDependencies++; + continue; } + if (assetInfo.m_assetId == rootAssetId) + { + // Circular dependencies in our graph need to be raised as errors as they could cause problems elsewhere + AZ_Error("AssetContainer", false, "Circular dependency found under asset %s", rootAssetId.ToString().c_str()); + m_invalidDependencies++; + continue; + } + if (!AssetManager::Instance().GetHandler(assetInfo.m_assetType)) + { + // Handlers may just not currently be around for a given asset type so we only warn here + m_invalidDependencies++; + continue; + } + if (loadParams.m_assetLoadFilterCB) + { + if (!loadParams.m_assetLoadFilterCB({thisAsset.m_assetId, assetInfo.m_assetType, + AZ::Data::ProductDependencyInfo::LoadBehaviorFromFlags(thisAsset.m_flags) })) + { + continue; + } + } + dependencyInfoList.push_back(assetInfo); + } + } + for (auto& thisInfo : dependencyInfoList) + { + waitingList.push_back(thisInfo.m_assetId); + } + + // Add waiting assets ahead of time to hear signals for any which may already be loading + AddWaitingAssets(waitingList); + SetupPreloadLists(move(preloadDependencies), rootAssetId); + + auto loadParamsCopyWithNoLoadingFilter = loadParams; + + // All asset dependencies below the root asset should be provided by the asset catalog, and therefore should *not* + // get triggered to load when the asset reference is serialized in. However, it's useful to detect, warn, and handle + // the case where the asset dependencies are NOT set up correctly. + loadParamsCopyWithNoLoadingFilter.m_assetLoadFilterCB = [handledAssetDependencyList](const AssetFilterInfo& filterInfo) + { + // NoLoad dependencies should always get filtered out and not loaded. + if (filterInfo.m_loadBehavior == AZ::Data::AssetLoadBehavior::NoLoad) + { + return false; } - // Finally, after creating and queueing the dependent assets, queue the root asset. This is saved until last to ensure that - // it doesn't have any chance of serializing in until after all the dependent assets have been queued for loading and have - // been added to the list of dependencies. - auto thisAsset = AssetManager::Instance().GetAssetInternal(rootAssetId, rootAssetType, rootAsset.GetAutoLoadBehavior(), - loadParamsCopyWithNoLoadingFilter, AssetInfo(), HasPreloads(rootAssetId)); + // In the normal case, the dependent asset appears in the handled asset list, and we should return false so that + // the asset isn't attempted to be loaded, since the asset will already be triggered to get loaded or was possibly + // already filtered out by the load filter callback. + // In the error case, the asset dependencies haven't been produced by the builder correctly, so assets + // have shown up that the asset container hasn't triggered to load and isn't listening for. Assert that this case + // has happened so that the builder for this asset type can be fixed. + // Ideally we would proceed forward and load them by returning "true", but the triggered load would use this lambda + // function as the asset load filter for that load as well, which isn't correct. If we ever want to support that + // behavior, we would need to rework the way filters work as well as the code in AssetSerializer.cpp to pass down + // the loadParams.m_assetLoadFilterCB that was passed into the AddDependentAssets() methods to use as the dependent + // asset filter instead of this lambda function. + AZ_UNUSED(handledAssetDependencyList); // Prevent unused warning in release builds + AZ_Assert(AZStd::find(handledAssetDependencyList.begin(), handledAssetDependencyList.end(), filterInfo.m_assetId) != + handledAssetDependencyList.end(), + "Dependent Asset ID (%s) is expected to load, but the Asset Catalog has no dependency recorded. " + "Examine the asset builder for the asset relying on this to ensure it is generating the correct dependencies.", + filterInfo.m_assetId.ToString().c_str()); - if (!thisAsset) + // The dependent asset should have already been created and at least queued to load prior to reaching this point. + // The asset serializer needs to get a successful result from FindAsset(), or else our asset reference will fail + // to point to the asset data once it is loaded. + if (!Data::AssetManager::Instance().FindAsset(filterInfo.m_assetId, AZ::Data::AssetLoadBehavior::Default)) { - AZ_Assert(false, "Root asset with id %s failed to load, asset container is invalid.", - rootAssetId.ToString().c_str()); - ClearWaitingAssets(); - // initComplete remains false, because we have failed to initialize successfully. + AZ_Assert(!Data::AssetManager::Instance().FindAsset(filterInfo.m_assetId, AZ::Data::AssetLoadBehavior::Default), + "Dependent Asset ID (%s) can't be found in the AssetManager, which means the asset referencing it has probably " + "started loading before the dependent asset has been queued to load. Verify that the asset dependencies have " + "been created correctly for the parent asset.", + filterInfo.m_assetId.ToString().c_str()); + } + + return false; + }; + + // This will contain the list of dependent assets that have been created (or found) and queued to load. + // We also keep a copy of the AssetInfo structure as a small optimization to avoid a redundant lookup in GetAssetInternal. + AZStd::vector>> dependencyAssets; + + // Make sure all the dependencies are created first before we try to load them. + // Since we've set the load filter to not load dependencies, we need to ensure all the assets are created beforehand + // so the dependencies can be hooked up as soon as each asset gets serialized in, even if they start getting serialized + // while we're still in the middle of triggering all of the asset loads below. + dependencyAssets = CreateAndQueueDependentAssets(dependencyInfoList, loadParamsCopyWithNoLoadingFilter); + + // Add all of the queued dependent assets as dependencies + { + AZStd::lock_guard dependencyLock(m_dependencyMutex); + for (auto& [dependentAssetInfo, dependentAsset] : dependencyAssets) + { + AddDependency(AZStd::move(dependentAsset)); + } + } + + // Finally, after creating and queueing the dependent assets, queue the root asset. This is saved until last to ensure that + // it doesn't have any chance of serializing in until after all the dependent assets have been queued for loading and have + // been added to the list of dependencies. + auto thisAsset = AssetManager::Instance().GetAssetInternal(rootAssetId, rootAssetType, rootAsset.GetAutoLoadBehavior(), + loadParamsCopyWithNoLoadingFilter, AssetInfo(), HasPreloads(rootAssetId)); + + if (!thisAsset) + { + AZ_Assert(false, "Root asset with id %s failed to load, asset container is invalid.", + rootAssetId.ToString().c_str()); + ClearWaitingAssets(); + // initComplete remains false, because we have failed to initialize successfully. + return; + } + + m_initComplete = true; + + // *After* setting initComplete to true, check to see if the assets are already ready. + // This check needs to wait until after setting initComplete because if they *are* ready, we want the final call to + // RemoveWaitingAsset to trigger the OnAssetContainerReady/Canceled event. If we call CheckReady() *before* setting + // initComplete, if all the assets are ready, the event will never get triggered. + CheckReady(); + } + + bool AssetContainer::IsReady() const + { + return (m_rootAsset && m_waitingCount == 0); + } + + bool AssetContainer::IsLoading() const + { + return (m_rootAsset || m_waitingCount); + } + + bool AssetContainer::IsValid() const + { + return (m_containerAssetId.IsValid() && m_initComplete && m_rootAsset); + } + + void AssetContainer::CheckReady() + { + if (!m_dependencies.empty()) + { + for (auto& [assetId, dependentAsset] : m_dependencies) + { + if (dependentAsset->IsReady() || dependentAsset->IsError()) + { + HandleReadyAsset(dependentAsset); + } + } + } + if (auto asset = m_rootAsset.GetStrongReference(); asset.IsReady() || asset.IsError()) + { + HandleReadyAsset(asset); + } + } + + Asset AssetContainer::GetRootAsset() + { + return m_rootAsset.GetStrongReference(); + } + + AssetId AssetContainer::GetContainerAssetId() + { + return m_containerAssetId; + } + + void AssetContainer::ClearRootAsset() + { + AssetId rootId = m_rootAsset.GetId(); + + { + AZStd::lock_guard preloadGuard(m_preloadMutex); + + // Erase the entry in the preloadWaitList for the root asset if one exists. + m_preloadWaitList.erase(rootId); + + // It's possible that the root asset has preload dependencies, so make sure to check the preload list and remove + // the entry for the root asset if it has one. + auto rootAssetPreloadIter = m_preloadList.find(rootId); + if (rootAssetPreloadIter != m_preloadList.end()) + { + // Since the root asset has a preload list, that means the preload wait list will also have references to the + // root asset. (The preload wait list is a list of assets waiting on a preload asset to finish) Clear those + // out as well. + auto waitAssetSet = rootAssetPreloadIter->second; + for (auto& waitId : waitAssetSet) + { + auto waitAssetIter = m_preloadWaitList.find(waitId); + if (waitAssetIter != m_preloadWaitList.end()) + { + waitAssetIter->second.erase(rootId); + } + } + + m_preloadList.erase(rootAssetPreloadIter); + } + } + + // Clear out the root asset before removing it from the waiting list to ensure that we trigger an "OnAssetContainerCanceled" + // event instead of "OnAssetContainerReady". + m_rootAsset = {}; + RemoveWaitingAsset(rootId); + + } + + void AssetContainer::AddDependency(const Asset& newDependency) + { + m_dependencies[newDependency->GetId()] = newDependency; + } + void AssetContainer::AddDependency(Asset&& newDependency) + { + m_dependencies[newDependency->GetId()] = AZStd::move(newDependency); + } + + void AssetContainer::OnAssetReady(Asset asset) + { + HandleReadyAsset(asset); + } + + void AssetContainer::OnAssetError(Asset asset) + { + AZ_Warning("AssetContainer", false, "Error loading asset %s", asset->GetId().ToString().c_str()); + HandleReadyAsset(asset); + } + + void AssetContainer::HandleReadyAsset(Asset asset) + { + // Wait until we've finished initialization before allowing this + // If a ready event happens before we've gotten all the maps/structures set up, there may be some missing data + // which can lead to a crash + // We'll go through and check the ready status of every dependency immediately after finishing initialization anyway + if (m_initComplete) + { + RemoveFromAllWaitingPreloads(asset->GetId()); + RemoveWaitingAsset(asset->GetId()); + } + } + + void AssetContainer::OnAssetDataLoaded(Asset asset) + { + // Remove only from this asset's waiting list. Anything else should + // listen for OnAssetReady as the true signal. This is essentially removing the + // "marker" we placed in SetupPreloads that we need to wait for our own data + RemoveFromWaitingPreloads(asset->GetId(), asset->GetId()); + } + + void AssetContainer::RemoveFromWaitingPreloads(const AssetId& waiterId, const AssetId& preloadID) + { + { + AZStd::lock_guard preloadGuard(m_preloadMutex); + + auto remainingPreloadIter = m_preloadList.find(waiterId); + if (remainingPreloadIter == m_preloadList.end()) + { + // If we got here without an entry on the preload list, it probably means this asset was triggered to load multiple + // times, some with dependencies and some without. To ensure that we don't disturb the loads that expect the + // dependencies, just silently return and don't treat the asset as finished loading. We'll rely on the other load + // to send an OnAssetReady() whenever its expected dependencies are met. return; } - - m_initComplete = true; - - // *After* setting initComplete to true, check to see if the assets are already ready. - // This check needs to wait until after setting initComplete because if they *are* ready, we want the final call to - // RemoveWaitingAsset to trigger the OnAssetContainerReady/Canceled event. If we call CheckReady() *before* setting - // initComplete, if all the assets are ready, the event will never get triggered. - CheckReady(); - } - - bool AssetContainer::IsReady() const - { - return (m_rootAsset && m_waitingCount == 0); - } - - bool AssetContainer::IsLoading() const - { - return (m_rootAsset || m_waitingCount); - } - - bool AssetContainer::IsValid() const - { - return (m_containerAssetId.IsValid() && m_initComplete && m_rootAsset); - } - - void AssetContainer::CheckReady() - { - if (!m_dependencies.empty()) + if (!remainingPreloadIter->second.erase(preloadID)) { - for (auto& [assetId, dependentAsset] : m_dependencies) - { - if (dependentAsset->IsReady() || dependentAsset->IsError()) - { - HandleReadyAsset(dependentAsset); - } - } + AZ_Warning("AssetContainer", !m_initComplete, "Couldn't remove %s from waiting list of %s", preloadID.ToString().c_str(), waiterId.ToString().c_str()); + return; } - if (auto asset = m_rootAsset.GetStrongReference(); asset.IsReady() || asset.IsError()) + if (!remainingPreloadIter->second.empty()) { - HandleReadyAsset(asset); + return; } } + auto thisAsset = GetAssetData(waiterId); + AssetManager::Instance().ValidateAndPostLoad(thisAsset, true, false, nullptr); + } - Asset AssetContainer::GetRootAsset() + void AssetContainer::RemoveFromAllWaitingPreloads(const AssetId& thisId) + { + AZStd::unordered_set checkList; { - return m_rootAsset.GetStrongReference(); - } - - AssetId AssetContainer::GetContainerAssetId() - { - return m_containerAssetId; - } - - void AssetContainer::ClearRootAsset() - { - AssetId rootId = m_rootAsset.GetId(); - - { - AZStd::lock_guard preloadGuard(m_preloadMutex); - - // Erase the entry in the preloadWaitList for the root asset if one exists. - m_preloadWaitList.erase(rootId); - - // It's possible that the root asset has preload dependencies, so make sure to check the preload list and remove - // the entry for the root asset if it has one. - auto rootAssetPreloadIter = m_preloadList.find(rootId); - if (rootAssetPreloadIter != m_preloadList.end()) - { - // Since the root asset has a preload list, that means the preload wait list will also have references to the - // root asset. (The preload wait list is a list of assets waiting on a preload asset to finish) Clear those - // out as well. - auto waitAssetSet = rootAssetPreloadIter->second; - for (auto& waitId : waitAssetSet) - { - auto waitAssetIter = m_preloadWaitList.find(waitId); - if (waitAssetIter != m_preloadWaitList.end()) - { - waitAssetIter->second.erase(rootId); - } - } - - m_preloadList.erase(rootAssetPreloadIter); - } - } - - // Clear out the root asset before removing it from the waiting list to ensure that we trigger an "OnAssetContainerCanceled" - // event instead of "OnAssetContainerReady". - m_rootAsset = {}; - RemoveWaitingAsset(rootId); - - } - - void AssetContainer::AddDependency(const Asset& newDependency) - { - m_dependencies[newDependency->GetId()] = newDependency; - } - void AssetContainer::AddDependency(Asset&& newDependency) - { - m_dependencies[newDependency->GetId()] = AZStd::move(newDependency); - } - - void AssetContainer::OnAssetReady(Asset asset) - { - HandleReadyAsset(asset); - } - - void AssetContainer::OnAssetError(Asset asset) - { - AZ_Warning("AssetContainer", false, "Error loading asset %s", asset->GetId().ToString().c_str()); - HandleReadyAsset(asset); - } - - void AssetContainer::HandleReadyAsset(Asset asset) - { - RemoveFromAllWaitingPreloads(asset->GetId()); - RemoveWaitingAsset(asset->GetId()); - } - - void AssetContainer::OnAssetDataLoaded(Asset asset) - { - // Remove only from this asset's waiting list. Anything else should - // listen for OnAssetReady as the true signal. This is essentially removing the - // "marker" we placed in SetupPreloads that we need to wait for our own data - RemoveFromWaitingPreloads(asset->GetId(), asset->GetId()); - } - - void AssetContainer::RemoveFromWaitingPreloads(const AssetId& waiterId, const AssetId& preloadID) - { - { - AZStd::lock_guard preloadGuard(m_preloadMutex); - - auto remainingPreloadIter = m_preloadList.find(waiterId); - if (remainingPreloadIter == m_preloadList.end()) - { - // If we got here without an entry on the preload list, it probably means this asset was triggered to load multiple - // times, some with dependencies and some without. To ensure that we don't disturb the loads that expect the - // dependencies, just silently return and don't treat the asset as finished loading. We'll rely on the other load - // to send an OnAssetReady() whenever its expected dependencies are met. - return; - } - if (!remainingPreloadIter->second.erase(preloadID)) - { - AZ_Warning("AssetContainer", !m_initComplete, "Couldn't remove %s from waiting list of %s", preloadID.ToString().c_str(), waiterId.ToString().c_str()); - return; - } - if (!remainingPreloadIter->second.empty()) - { - return; - } - } - auto thisAsset = GetAssetData(waiterId); - AssetManager::Instance().ValidateAndPostLoad(thisAsset, true, false, nullptr); - } - - void AssetContainer::RemoveFromAllWaitingPreloads(const AssetId& thisId) - { - AZStd::unordered_set checkList; - { - AZStd::lock_guard preloadGuard(m_preloadMutex); - - auto waitingList = m_preloadWaitList.find(thisId); - if (waitingList != m_preloadWaitList.end()) - { - checkList = move(waitingList->second); - m_preloadWaitList.erase(waitingList); - } - } - for (auto& thisDepId : checkList) - { - if (thisDepId != thisId) - { - RemoveFromWaitingPreloads(thisDepId, thisId); - } - } - } - - void AssetContainer::ClearWaitingAssets() - { - AZStd::lock_guard lock(m_readyMutex); - m_waitingCount = 0; - for (auto& thisAsset : m_waitingAssets) - { - AssetBus::MultiHandler::BusDisconnect(thisAsset); - } - m_waitingAssets.clear(); - } - - void AssetContainer::ListWaitingAssets() const - { -#if defined(AZ_ENABLE_TRACING) - AZStd::lock_guard lock(m_readyMutex); - AZ_TracePrintf("AssetContainer", "Waiting on assets:\n"); - for (auto& thisAsset : m_waitingAssets) - { - AZ_TracePrintf("AssetContainer", " %s\n",thisAsset.ToString().c_str()); - } -#endif - } - - void AssetContainer::ListWaitingPreloads([[maybe_unused]] const AssetId& assetId) const - { -#if defined(AZ_ENABLE_TRACING) AZStd::lock_guard preloadGuard(m_preloadMutex); - auto preloadEntry = m_preloadList.find(assetId); - if (preloadEntry != m_preloadList.end()) + + auto waitingList = m_preloadWaitList.find(thisId); + if (waitingList != m_preloadWaitList.end()) { - AZ_TracePrintf("AssetContainer", "%s waiting on preloads : \n",assetId.ToString().c_str()); - for (auto& thisId : preloadEntry->second) - { - AZ_TracePrintf("AssetContainer", " %s\n",thisId.ToString().c_str()); - } + checkList = move(waitingList->second); + m_preloadWaitList.erase(waitingList); } - else + } + for (auto& thisDepId : checkList) + { + if (thisDepId != thisId) { - AZ_TracePrintf("AssetContainer", "%s isn't waiting on any preloads:\n", assetId.ToString().c_str()); + RemoveFromWaitingPreloads(thisDepId, thisId); } + } + } + + void AssetContainer::ClearWaitingAssets() + { + AZStd::lock_guard lock(m_readyMutex); + m_waitingCount = 0; + for (auto& thisAsset : m_waitingAssets) + { + AssetBus::MultiHandler::BusDisconnect(thisAsset); + } + m_waitingAssets.clear(); + } + + void AssetContainer::ListWaitingAssets() const + { +#if defined(AZ_ENABLE_TRACING) + AZStd::lock_guard lock(m_readyMutex); + AZ_TracePrintf("AssetContainer", "Waiting on assets:\n"); + for (auto& thisAsset : m_waitingAssets) + { + AZ_TracePrintf("AssetContainer", " %s\n",thisAsset.ToString().c_str()); + } #endif - } + } - void AssetContainer::AddWaitingAssets(const AZStd::vector& assetList) + void AssetContainer::ListWaitingPreloads([[maybe_unused]] const AssetId& assetId) const + { +#if defined(AZ_ENABLE_TRACING) + AZStd::lock_guard preloadGuard(m_preloadMutex); + auto preloadEntry = m_preloadList.find(assetId); + if (preloadEntry != m_preloadList.end()) { - AZStd::lock_guard lock(m_readyMutex); - for (auto& thisAsset : assetList) + AZ_TracePrintf("AssetContainer", "%s waiting on preloads : \n",assetId.ToString().c_str()); + for (auto& thisId : preloadEntry->second) { - if (m_waitingAssets.insert(thisAsset).second) - { - ++m_waitingCount; - AssetBus::MultiHandler::BusConnect(thisAsset); - AssetLoadBus::MultiHandler::BusConnect(thisAsset); - } + AZ_TracePrintf("AssetContainer", " %s\n",thisId.ToString().c_str()); } } - - void AssetContainer::AddWaitingAsset(const AssetId& thisAsset) + else + { + AZ_TracePrintf("AssetContainer", "%s isn't waiting on any preloads:\n", assetId.ToString().c_str()); + } +#endif + } + + void AssetContainer::AddWaitingAssets(const AZStd::vector& assetList) + { + AZStd::lock_guard lock(m_readyMutex); + for (auto& thisAsset : assetList) { - AZStd::lock_guard lock(m_readyMutex); if (m_waitingAssets.insert(thisAsset).second) { ++m_waitingCount; @@ -478,196 +481,207 @@ namespace AZ AssetLoadBus::MultiHandler::BusConnect(thisAsset); } } + } - void AssetContainer::RemoveWaitingAsset(const AssetId& thisAsset) + void AssetContainer::AddWaitingAsset(const AssetId& thisAsset) + { + AZStd::lock_guard lock(m_readyMutex); + if (m_waitingAssets.insert(thisAsset).second) { - bool allReady{ false }; - { - bool disconnectEbus = false; + ++m_waitingCount; + AssetBus::MultiHandler::BusConnect(thisAsset); + AssetLoadBus::MultiHandler::BusConnect(thisAsset); + } + } - { // Intentionally limiting lock scope - AZStd::lock_guard lock(m_readyMutex); - // If we're trying to remove something already removed, just ignore it - if (m_waitingAssets.erase(thisAsset)) - { - m_waitingCount -= 1; - disconnectEbus = true; + void AssetContainer::RemoveWaitingAsset(const AssetId& thisAsset) + { + bool allReady{ false }; + { + bool disconnectEbus = false; - } - if (m_waitingAssets.empty()) - { - allReady = true; - } - } - - if(disconnectEbus) + { // Intentionally limiting lock scope + AZStd::lock_guard lock(m_readyMutex); + // If we're trying to remove something already removed, just ignore it + if (m_waitingAssets.erase(thisAsset)) { - AssetBus::MultiHandler::BusDisconnect(thisAsset); - AssetLoadBus::MultiHandler::BusDisconnect(thisAsset); + m_waitingCount -= 1; + disconnectEbus = true; + + } + if (m_waitingAssets.empty()) + { + allReady = true; } } - // If there are no assets left to be loaded, trigger the final AssetContainer notification (ready or canceled). - // We guard against prematurely sending it (m_initComplete) because it's possible for assets to get removed from our waiting - // list *while* we're still building up the list, so the list would appear to be empty too soon. - // We also guard against sending it multiple times (m_finalNotificationSent), because in some error conditions, it may be - // possible to try to remove the same asset multiple times, which if it's the last asset, it could trigger multiple - // notifications. - if (allReady && m_initComplete && !m_finalNotificationSent) + if(disconnectEbus) { - m_finalNotificationSent = true; - if (m_rootAsset) - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetContainerReady, this); - } - else - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetContainerCanceled, this); - } + AssetBus::MultiHandler::BusDisconnect(thisAsset); + AssetLoadBus::MultiHandler::BusDisconnect(thisAsset); } } - AssetContainer::operator bool() const + // If there are no assets left to be loaded, trigger the final AssetContainer notification (ready or canceled). + // We guard against prematurely sending it (m_initComplete) because it's possible for assets to get removed from our waiting + // list *while* we're still building up the list, so the list would appear to be empty too soon. + // We also guard against sending it multiple times (m_finalNotificationSent), because in some error conditions, it may be + // possible to try to remove the same asset multiple times, which if it's the last asset, it could trigger multiple + // notifications. + if (allReady && m_initComplete && !m_finalNotificationSent) { - return m_rootAsset ? true : false; - } - - const AssetContainer::DependencyList& AssetContainer::GetDependencies() const - { - return m_dependencies; - } - - const AZStd::unordered_set& AssetContainer::GetUnloadedDependencies() const - { - return m_unloadedDependencies; - } - - void AssetContainer::SetupPreloadLists(PreloadAssetListType&& preloadList, const AssetId& rootAssetId) - { - if (!preloadList.empty()) + m_finalNotificationSent = true; + if (m_rootAsset) { - // This method can be entered as additional NoLoad dependency groups are loaded - the container could - // be in the middle of loading so we need to grab both mutexes. - AZStd::scoped_lock lock(m_readyMutex, m_preloadMutex); + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetContainerReady, this); + } + else + { + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetContainerCanceled, this); + } + } + } - for (auto thisListPair = preloadList.begin(); thisListPair != preloadList.end();) + AssetContainer::operator bool() const + { + return m_rootAsset ? true : false; + } + + const AssetContainer::DependencyList& AssetContainer::GetDependencies() const + { + return m_dependencies; + } + + const AZStd::unordered_set& AssetContainer::GetUnloadedDependencies() const + { + return m_unloadedDependencies; + } + + void AssetContainer::SetupPreloadLists(PreloadAssetListType&& preloadList, const AssetId& rootAssetId) + { + if (!preloadList.empty()) + { + // This method can be entered as additional NoLoad dependency groups are loaded - the container could + // be in the middle of loading so we need to grab both mutexes. + AZStd::scoped_lock lock(m_readyMutex, m_preloadMutex); + + for (auto thisListPair = preloadList.begin(); thisListPair != preloadList.end();) + { + // We only should add ourselves if we have another valid preload we're waiting on + bool foundAsset{ false }; + // It's possible this set of preload dependencies was culled out by lack of asset handler + // Or filtering rules. This is not an error, we should just remove it from the list of + // Preloads we're waiting on + if (!m_waitingAssets.count(thisListPair->first)) { - // We only should add ourselves if we have another valid preload we're waiting on - bool foundAsset{ false }; - // It's possible this set of preload dependencies was culled out by lack of asset handler - // Or filtering rules. This is not an error, we should just remove it from the list of - // Preloads we're waiting on - if (!m_waitingAssets.count(thisListPair->first)) + thisListPair = preloadList.erase(thisListPair); + continue; + } + for (auto thisAsset = thisListPair->second.begin(); thisAsset != thisListPair->second.end();) + { + // These are data errors. We'll emit the error but carry on. The container + // will load the assets but won't/can't create a circular preload dependency chain + if (*thisAsset == rootAssetId) { - thisListPair = preloadList.erase(thisListPair); + AZ_Error("AssetContainer", false, "Circular preload dependency found - %s has a preload" + "dependency back to root %s\n", + thisListPair->first.ToString().c_str(), + rootAssetId.ToString().c_str()); + thisAsset = thisListPair->second.erase(thisAsset); continue; } - for (auto thisAsset = thisListPair->second.begin(); thisAsset != thisListPair->second.end();) + else if (*thisAsset == thisListPair->first) { - // These are data errors. We'll emit the error but carry on. The container - // will load the assets but won't/can't create a circular preload dependency chain - if (*thisAsset == rootAssetId) - { - AZ_Error("AssetContainer", false, "Circular preload dependency found - %s has a preload" - "dependency back to root %s\n", - thisListPair->first.ToString().c_str(), - rootAssetId.ToString().c_str()); - thisAsset = thisListPair->second.erase(thisAsset); - continue; - } - else if (*thisAsset == thisListPair->first) - { - AZ_Error("AssetContainer", false, "Circular preload dependency found - Root asset %s has a preload" - "dependency on %s which depends back back to itself\n", - rootAssetId.ToString().c_str(), - thisListPair->first.ToString().c_str()); - thisAsset = thisListPair->second.erase(thisAsset); - continue; - } - else if (m_preloadWaitList.count(thisListPair->first) && m_preloadWaitList[thisListPair->first].count(*thisAsset)) - { - AZ_Error("AssetContainer", false, "Circular dependency found - Root asset %s has a preload" - "dependency on %s which has a circular dependency with %s\n", - rootAssetId.ToString().c_str(), - thisListPair->first.ToString().c_str(), - thisAsset->ToString().c_str()); - thisAsset = thisListPair->second.erase(thisAsset); - continue; - } - else if (m_waitingAssets.count(*thisAsset)) - { - foundAsset = true; - m_preloadWaitList[*thisAsset].insert(thisListPair->first); - ++thisAsset; - } - else - { - // This particular preload dependency of this asset was culled - // similar to the case above this can be due to no established asset handler - // or filtering rules. We'll just erase the entry because we're not loading this - thisAsset = thisListPair->second.erase(thisAsset); - continue; - } + AZ_Error("AssetContainer", false, "Circular preload dependency found - Root asset %s has a preload" + "dependency on %s which depends back back to itself\n", + rootAssetId.ToString().c_str(), + thisListPair->first.ToString().c_str()); + thisAsset = thisListPair->second.erase(thisAsset); + continue; } - if (foundAsset) + else if (m_preloadWaitList.count(thisListPair->first) && m_preloadWaitList[thisListPair->first].count(*thisAsset)) { - // We've established that this asset has at least one preload dependency it needs to wait on - // so we additionally add the waiting asset as its own preload so all of our "waiting assets" - // are managed in the same list. We can't consider this asset to be "ready" until all - // of its preloads are ready and it has been loaded. It will request an OnAssetDataLoaded - // notification from AssetManager rather than an OnAssetReady because of these additional dependencies. - thisListPair->second.insert(thisListPair->first); - m_preloadWaitList[thisListPair->first].insert(thisListPair->first); + AZ_Error("AssetContainer", false, "Circular dependency found - Root asset %s has a preload" + "dependency on %s which has a circular dependency with %s\n", + rootAssetId.ToString().c_str(), + thisListPair->first.ToString().c_str(), + thisAsset->ToString().c_str()); + thisAsset = thisListPair->second.erase(thisAsset); + continue; + } + else if (m_waitingAssets.count(*thisAsset)) + { + foundAsset = true; + m_preloadWaitList[*thisAsset].insert(thisListPair->first); + ++thisAsset; + } + else + { + // This particular preload dependency of this asset was culled + // similar to the case above this can be due to no established asset handler + // or filtering rules. We'll just erase the entry because we're not loading this + thisAsset = thisListPair->second.erase(thisAsset); + continue; } - ++thisListPair; } - for(auto& thisList : preloadList) + if (foundAsset) { - // Only save the entry to the final preload list if it has at least one dependent asset still remaining after - // the checks above. - if (!thisList.second.empty()) - { - m_preloadList[thisList.first].insert(thisList.second.begin(), thisList.second.end()); - } + // We've established that this asset has at least one preload dependency it needs to wait on + // so we additionally add the waiting asset as its own preload so all of our "waiting assets" + // are managed in the same list. We can't consider this asset to be "ready" until all + // of its preloads are ready and it has been loaded. It will request an OnAssetDataLoaded + // notification from AssetManager rather than an OnAssetReady because of these additional dependencies. + thisListPair->second.insert(thisListPair->first); + m_preloadWaitList[thisListPair->first].insert(thisListPair->first); + } + ++thisListPair; + } + for(auto& thisList : preloadList) + { + // Only save the entry to the final preload list if it has at least one dependent asset still remaining after + // the checks above. + if (!thisList.second.empty()) + { + m_preloadList[thisList.first].insert(thisList.second.begin(), thisList.second.end()); } } } + } - bool AssetContainer::HasPreloads(const AssetId& assetId) const + bool AssetContainer::HasPreloads(const AssetId& assetId) const + { + AZStd::lock_guard preloadGuard(m_preloadMutex); + auto preloadEntry = m_preloadList.find(assetId); + if (preloadEntry != m_preloadList.end()) { - AZStd::lock_guard preloadGuard(m_preloadMutex); - auto preloadEntry = m_preloadList.find(assetId); - if (preloadEntry != m_preloadList.end()) - { - return !preloadEntry->second.empty(); - } - return false; + return !preloadEntry->second.empty(); } + return false; + } - Asset AssetContainer::GetAssetData(const AssetId& assetId) const + Asset AssetContainer::GetAssetData(const AssetId& assetId) const + { + AZStd::lock_guard dependenciesGuard(m_dependencyMutex); + if (auto rootAsset = m_rootAsset.GetStrongReference(); rootAsset.GetId() == assetId) { - AZStd::lock_guard dependenciesGuard(m_dependencyMutex); - if (auto rootAsset = m_rootAsset.GetStrongReference(); rootAsset.GetId() == assetId) - { - return rootAsset; - } - auto dependencyIter = m_dependencies.find(assetId); - if (dependencyIter != m_dependencies.end()) - { - return dependencyIter->second; - } - AZ_Warning("AssetContainer", false, "Asset %s not found in container", assetId.ToString().c_str()); - return {}; + return rootAsset; } + auto dependencyIter = m_dependencies.find(assetId); + if (dependencyIter != m_dependencies.end()) + { + return dependencyIter->second; + } + AZ_Warning("AssetContainer", false, "Asset %s not found in container", assetId.ToString().c_str()); + return {}; + } - int AssetContainer::GetNumWaitingDependencies() const - { - return m_waitingCount.load(); - } + int AssetContainer::GetNumWaitingDependencies() const + { + return m_waitingCount.load(); + } - int AssetContainer::GetInvalidDependencies() const - { - return m_invalidDependencies.load(); - } - } // namespace Data -} // namespace AZ + int AssetContainer::GetInvalidDependencies() const + { + return m_invalidDependencies.load(); + } +} // namespace AZ::Data diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetContainer.h b/Code/Framework/AzCore/AzCore/Asset/AssetContainer.h index e0091d678f..a05343ed0b 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetContainer.h +++ b/Code/Framework/AzCore/AzCore/Asset/AssetContainer.h @@ -24,8 +24,8 @@ namespace AZ // AssetContainer loads an asset and all of its dependencies as a collection which is parallellized as much as possible. // With the container, the data will all load in parallel. Dependent asset loads will still obey the expected rules - // where PreLoad assets will emit OnAssetReady before the parent does, and QueueLoad assets will emit OnAssetReady in - // no guaranteed order. However, the OnAssetContainerReady signals will not emit until all PreLoad and QueueLoad assets + // where PreLoad assets will emit OnAssetReady before the parent does, and QueueLoad assets will emit OnAssetReady in + // no guaranteed order. However, the OnAssetContainerReady signals will not emit until all PreLoad and QueueLoad assets // are ready. NoLoad dependencies are not loaded by default but can be loaded along with their dependencies using the // same rules as above by using the LoadAll dependency rule. class AssetContainer : @@ -36,7 +36,7 @@ namespace AZ AZ_CLASS_ALLOCATOR(AssetContainer, SystemAllocator, 0); AssetContainer() = default; - + AssetContainer(Asset asset, const AssetLoadParameters& loadParams); ~AssetContainer(); @@ -81,6 +81,10 @@ namespace AZ // AssetLoadBus void OnAssetDataLoaded(AZ::Data::Asset asset) override; protected: + + virtual AZStd::vector>> CreateAndQueueDependentAssets( + const AZStd::vector& dependencyInfoList, const AssetLoadParameters& loadParamsCopyWithNoLoadingFilter); + // Waiting assets are those which have not yet signalled ready. In the case of PreLoad dependencies the data may have completed the load cycle but // the Assets aren't considered "Ready" yet if there are PreLoad dependencies still loading and will still be in the list until the point that asset and // All of its preload dependencies have been loaded, when it signals OnAssetReady @@ -97,7 +101,7 @@ namespace AZ void AddDependency(Asset&& addDependency); // Add a "graph section" to our list of dependencies. This checks the catalog for all Pre and Queue load assets which are dependents of the requested asset and kicks off loads - // NoLoads which are encounted are placed in another list and can be loaded on demand with the LoadDependency call. + // NoLoads which are encounted are placed in another list and can be loaded on demand with the LoadDependency call. void AddDependentAssets(Asset rootAsset, const AssetLoadParameters& loadParams); // If "PreLoad" assets are found in the graph these are cached and tracked with both OnAssetReady and OnAssetDataLoaded messages. @@ -117,7 +121,7 @@ namespace AZ // duringInit if we're coming from the checkReady method - containers that start ready don't need to signal void HandleReadyAsset(AZ::Data::Asset asset); - // Optimization to save the lookup in the dependencies map + // Optimization to save the lookup in the dependencies map AssetInternal::WeakAsset m_rootAsset; // The root asset id is stored here semi-redundantly on initialization so that we can still refer to it even if the @@ -136,7 +140,7 @@ namespace AZ AZStd::atomic_bool m_finalNotificationSent{false}; mutable AZStd::recursive_mutex m_preloadMutex; - // AssetId -> List of assets it is still waiting on + // AssetId -> List of assets it is still waiting on PreloadAssetListType m_preloadList; // AssetId -> List of assets waiting on it diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp b/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp index 3fa3b39ca5..0d28036646 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp +++ b/Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp @@ -12,207 +12,204 @@ #include #include -namespace AZ +namespace AZ::Data { - namespace Data + AZ_CLASS_ALLOCATOR_IMPL(AssetJsonSerializer, SystemAllocator, 0); + + JsonSerializationResult::Result AssetJsonSerializer::Load(void* outputValue, const Uuid& /*outputValueTypeId*/, + const rapidjson::Value& inputValue, JsonDeserializerContext& context) { - AZ_CLASS_ALLOCATOR_IMPL(AssetJsonSerializer, SystemAllocator, 0); + namespace JSR = JsonSerializationResult; - JsonSerializationResult::Result AssetJsonSerializer::Load(void* outputValue, const Uuid& /*outputValueTypeId*/, - const rapidjson::Value& inputValue, JsonDeserializerContext& context) + switch (inputValue.GetType()) { - namespace JSR = JsonSerializationResult; + case rapidjson::kObjectType: + return LoadAsset(outputValue, inputValue, context); + case rapidjson::kArrayType: // fall through + case rapidjson::kNullType: // fall through + case rapidjson::kStringType: // fall through + case rapidjson::kFalseType: // fall through + case rapidjson::kTrueType: // fall through + case rapidjson::kNumberType: + return context.Report(JSR::Tasks::ReadField, JSR::Outcomes::Unsupported, + "Unsupported type. Asset can only be read from an object."); - switch (inputValue.GetType()) + default: + return context.Report(JSR::Tasks::ReadField, JSR::Outcomes::Unknown, "Unknown json type encountered for Asset."); + } + } + + JsonSerializationResult::Result AssetJsonSerializer::Store(rapidjson::Value& outputValue, const void* inputValue, + const void* defaultValue, const Uuid& /*valueTypeId*/, JsonSerializerContext& context) + { + namespace JSR = JsonSerializationResult; + + const Asset* instance = reinterpret_cast*>(inputValue); + const Asset* defaultInstance = reinterpret_cast*>(defaultValue); + + JSR::ResultCode result(JSR::Tasks::WriteValue); + { + ScopedContextPath subPathId(context, "m_assetId"); + const auto* id = &instance->GetId(); + const auto* defaultId = defaultInstance ? &defaultInstance->GetId() : nullptr; + rapidjson::Value assetIdValue; + result = ContinueStoring(assetIdValue, id, defaultId, azrtti_typeid(), context); + if (result.GetOutcome() == JSR::Outcomes::Success || result.GetOutcome() == JSR::Outcomes::PartialDefaults) { - case rapidjson::kObjectType: - return LoadAsset(outputValue, inputValue, context); - case rapidjson::kArrayType: // fall through - case rapidjson::kNullType: // fall through - case rapidjson::kStringType: // fall through - case rapidjson::kFalseType: // fall through - case rapidjson::kTrueType: // fall through - case rapidjson::kNumberType: - return context.Report(JSR::Tasks::ReadField, JSR::Outcomes::Unsupported, - "Unsupported type. Asset can only be read from an object."); - - default: - return context.Report(JSR::Tasks::ReadField, JSR::Outcomes::Unknown, "Unknown json type encountered for Asset."); + if (!outputValue.IsObject()) + { + outputValue.SetObject(); + } + outputValue.AddMember(rapidjson::StringRef("assetId"), AZStd::move(assetIdValue), context.GetJsonAllocator()); } } - JsonSerializationResult::Result AssetJsonSerializer::Store(rapidjson::Value& outputValue, const void* inputValue, - const void* defaultValue, const Uuid& /*valueTypeId*/, JsonSerializerContext& context) { - namespace JSR = JsonSerializationResult; + const AZ::Data::AssetLoadBehavior autoLoadBehavior = instance->GetAutoLoadBehavior(); + const AZ::Data::AssetLoadBehavior defaultAutoLoadBehavior = defaultInstance ? + defaultInstance->GetAutoLoadBehavior() : AZ::Data::AssetLoadBehavior::Default; - const Asset* instance = reinterpret_cast*>(inputValue); - const Asset* defaultInstance = reinterpret_cast*>(defaultValue); - - JSR::ResultCode result(JSR::Tasks::WriteValue); - { - ScopedContextPath subPathId(context, "m_assetId"); - const auto* id = &instance->GetId(); - const auto* defaultId = defaultInstance ? &defaultInstance->GetId() : nullptr; - rapidjson::Value assetIdValue; - result = ContinueStoring(assetIdValue, id, defaultId, azrtti_typeid(), context); - if (result.GetOutcome() == JSR::Outcomes::Success || result.GetOutcome() == JSR::Outcomes::PartialDefaults) - { - if (!outputValue.IsObject()) - { - outputValue.SetObject(); - } - outputValue.AddMember(rapidjson::StringRef("assetId"), AZStd::move(assetIdValue), context.GetJsonAllocator()); - } - } - - { - const AZ::Data::AssetLoadBehavior autoLoadBehavior = instance->GetAutoLoadBehavior(); - const AZ::Data::AssetLoadBehavior defaultAutoLoadBehavior = defaultInstance ? - defaultInstance->GetAutoLoadBehavior() : AZ::Data::AssetLoadBehavior::Default; - - result.Combine( - ContinueStoringToJsonObjectField(outputValue, "loadBehavior", - &autoLoadBehavior, &defaultAutoLoadBehavior, - azrtti_typeid(), context)); - } - - { - ScopedContextPath subPathHint(context, "m_assetHint"); - const AZStd::string* hint = &instance->GetHint(); - const AZStd::string defaultHint; - rapidjson::Value assetHintValue; - JSR::ResultCode resultHint = ContinueStoring(assetHintValue, hint, &defaultHint, azrtti_typeid(), context); - if (resultHint.GetOutcome() == JSR::Outcomes::Success || resultHint.GetOutcome() == JSR::Outcomes::PartialDefaults) - { - if (!outputValue.IsObject()) - { - outputValue.SetObject(); - } - outputValue.AddMember(rapidjson::StringRef("assetHint"), AZStd::move(assetHintValue), context.GetJsonAllocator()); - } - result.Combine(resultHint); - } - - return context.Report(result, - result.GetProcessing() == JSR::Processing::Completed ? "Successfully stored Asset." : "Failed to store Asset."); + result.Combine( + ContinueStoringToJsonObjectField(outputValue, "loadBehavior", + &autoLoadBehavior, &defaultAutoLoadBehavior, + azrtti_typeid(), context)); } - JsonSerializationResult::Result AssetJsonSerializer::LoadAsset(void* outputValue, const rapidjson::Value& inputValue, - JsonDeserializerContext& context) { - namespace JSR = JsonSerializationResult; - - Asset* instance = reinterpret_cast*>(outputValue); - AssetId id; - JSR::ResultCode result(JSR::Tasks::ReadField); - - SerializedAssetTracker* assetTracker = - context.GetMetadata().Find(); - + ScopedContextPath subPathHint(context, "m_assetHint"); + const AZStd::string* hint = &instance->GetHint(); + const AZStd::string defaultHint; + rapidjson::Value assetHintValue; + JSR::ResultCode resultHint = ContinueStoring(assetHintValue, hint, &defaultHint, azrtti_typeid(), context); + if (resultHint.GetOutcome() == JSR::Outcomes::Success || resultHint.GetOutcome() == JSR::Outcomes::PartialDefaults) { - Data::AssetLoadBehavior loadBehavior = instance->GetAutoLoadBehavior(); - - result = - ContinueLoadingFromJsonObjectField(&loadBehavior, - azrtti_typeid(), - inputValue, "loadBehavior", context); - - instance->SetAutoLoadBehavior(loadBehavior); + if (!outputValue.IsObject()) + { + outputValue.SetObject(); + } + outputValue.AddMember(rapidjson::StringRef("assetHint"), AZStd::move(assetHintValue), context.GetJsonAllocator()); } + result.Combine(resultHint); + } - auto it = inputValue.FindMember("assetId"); - if (it != inputValue.MemberEnd()) + return context.Report(result, + result.GetProcessing() == JSR::Processing::Completed ? "Successfully stored Asset." : "Failed to store Asset."); + } + + JsonSerializationResult::Result AssetJsonSerializer::LoadAsset(void* outputValue, const rapidjson::Value& inputValue, + JsonDeserializerContext& context) + { + namespace JSR = JsonSerializationResult; + + Asset* instance = reinterpret_cast*>(outputValue); + AssetId id; + JSR::ResultCode result(JSR::Tasks::ReadField); + + SerializedAssetTracker* assetTracker = + context.GetMetadata().Find(); + + { + Data::AssetLoadBehavior loadBehavior = instance->GetAutoLoadBehavior(); + + result = + ContinueLoadingFromJsonObjectField(&loadBehavior, + azrtti_typeid(), + inputValue, "loadBehavior", context); + + instance->SetAutoLoadBehavior(loadBehavior); + } + + auto it = inputValue.FindMember("assetId"); + if (it != inputValue.MemberEnd()) + { + ScopedContextPath subPath(context, "assetId"); + result.Combine(ContinueLoading(&id, azrtti_typeid(), it->value, context)); + if (!id.m_guid.IsNull()) { - ScopedContextPath subPath(context, "assetId"); - result.Combine(ContinueLoading(&id, azrtti_typeid(), it->value, context)); - if (!id.m_guid.IsNull()) + *instance = AssetManager::Instance().FindOrCreateAsset(id, instance->GetType(), instance->GetAutoLoadBehavior()); + if (!instance->GetId().IsValid()) { - *instance = AssetManager::Instance().FindOrCreateAsset(id, instance->GetType(), instance->GetAutoLoadBehavior()); - if (!instance->GetId().IsValid()) - { - // If the asset failed to be created, FindOrCreateAsset returns an asset instance with a null - // id. To preserve the asset id in the source json, reset the asset to an empty one, but with - // the right id. - const auto loadBehavior = instance->GetAutoLoadBehavior(); - *instance = Asset(id, instance->GetType()); - instance->SetAutoLoadBehavior(loadBehavior); - } + // If the asset failed to be created, FindOrCreateAsset returns an asset instance with a null + // id. To preserve the asset id in the source json, reset the asset to an empty one, but with + // the right id. + const auto loadBehavior = instance->GetAutoLoadBehavior(); + *instance = Asset(id, instance->GetType()); + instance->SetAutoLoadBehavior(loadBehavior); + } - result.Combine(context.Report(result, "Successfully created Asset with id.")); - } - else if (result.GetProcessing() == JSR::Processing::Completed) - { - result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, - "Null Asset created.")); - } - else - { - result.Combine(context.Report(result, "Failed to retrieve asset id for Asset.")); - } + result.Combine(context.Report(result, "Successfully created Asset with id.")); + } + else if (result.GetProcessing() == JSR::Processing::Completed) + { + result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, + "Null Asset created.")); } else { - result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, - "The asset id is missing, so there's not enough information to create an Asset.")); - } - - it = inputValue.FindMember("assetHint"); - if (it != inputValue.MemberEnd()) - { - ScopedContextPath subPath(context, "assetHint"); - AZStd::string hint; - result.Combine(ContinueLoading(&hint, azrtti_typeid(), it->value, context)); - instance->SetHint(AZStd::move(hint)); - } - else - { - result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, - "The asset hint is missing for Asset, so it will be left empty.")); - } - - if (assetTracker) - { - assetTracker->FixUpAsset(*instance); - assetTracker->AddAsset(*instance); - } - - bool success = result.GetOutcome() <= JSR::Outcomes::PartialSkip; - bool defaulted = result.GetOutcome() == JSR::Outcomes::DefaultsUsed || result.GetOutcome() == JSR::Outcomes::PartialDefaults; - AZStd::string_view message = - success ? "Successfully loaded information and created instance of Asset." : - defaulted ? "A default id was provided for Asset, so no instance could be created." : - "Not enough information was available to create an instance of Asset or data was corrupted."; - return context.Report(result, message); - } - - void SerializedAssetTracker::SetAssetFixUp(AssetFixUp assetFixUpCallback) - { - m_assetFixUpCallback = AZStd::move(assetFixUpCallback); - } - - void SerializedAssetTracker::FixUpAsset(Asset& asset) - { - if (m_assetFixUpCallback) - { - m_assetFixUpCallback(asset); + result.Combine(context.Report(result, "Failed to retrieve asset id for Asset.")); } } - - void SerializedAssetTracker::AddAsset(Asset asset) + else { - m_serializedAssets.emplace_back(asset); + result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, + "The asset id is missing, so there's not enough information to create an Asset.")); } - const AZStd::vector>& SerializedAssetTracker::GetTrackedAssets() const + it = inputValue.FindMember("assetHint"); + if (it != inputValue.MemberEnd()) { - return m_serializedAssets; + ScopedContextPath subPath(context, "assetHint"); + AZStd::string hint; + result.Combine(ContinueLoading(&hint, azrtti_typeid(), it->value, context)); + instance->SetHint(AZStd::move(hint)); + } + else + { + result.Combine(context.Report(JSR::Tasks::ReadField, JSR::Outcomes::DefaultsUsed, + "The asset hint is missing for Asset, so it will be left empty.")); } - AZStd::vector>& SerializedAssetTracker::GetTrackedAssets() + if (assetTracker) { - return m_serializedAssets; + assetTracker->FixUpAsset(*instance); + assetTracker->AddAsset(*instance); } - } // namespace Data -} // namespace AZ + bool success = result.GetOutcome() <= JSR::Outcomes::PartialSkip; + bool defaulted = result.GetOutcome() == JSR::Outcomes::DefaultsUsed || result.GetOutcome() == JSR::Outcomes::PartialDefaults; + AZStd::string_view message = + success ? "Successfully loaded information and created instance of Asset." : + defaulted ? "A default id was provided for Asset, so no instance could be created." : + "Not enough information was available to create an instance of Asset or data was corrupted."; + return context.Report(result, message); + } + + void SerializedAssetTracker::SetAssetFixUp(AssetFixUp assetFixUpCallback) + { + m_assetFixUpCallback = AZStd::move(assetFixUpCallback); + } + + void SerializedAssetTracker::FixUpAsset(Asset& asset) + { + if (m_assetFixUpCallback) + { + m_assetFixUpCallback(asset); + } + } + + void SerializedAssetTracker::AddAsset(Asset asset) + { + m_serializedAssets.emplace_back(asset); + } + + const AZStd::vector>& SerializedAssetTracker::GetTrackedAssets() const + { + return m_serializedAssets; + } + + AZStd::vector>& SerializedAssetTracker::GetTrackedAssets() + { + return m_serializedAssets; + } + +} // namespace AZ::Data diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetManager.cpp b/Code/Framework/AzCore/AzCore/Asset/AssetManager.cpp index 06bb0b0cac..e419666a32 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetManager.cpp +++ b/Code/Framework/AzCore/AzCore/Asset/AssetManager.cpp @@ -27,2165 +27,2166 @@ #include #include -namespace AZ +namespace AZ::Data { - namespace Data + AZ_CVAR(bool, cl_assetLoadWarningEnable, false, nullptr, AZ::ConsoleFunctorFlags::Null, + "Enable warnings that show when AssetHandler::LoadAssetData has exceeded the time set in cl_assetLoadWarningMsThreshold."); + AZ_CVAR(uint32_t, cl_assetLoadWarningMsThreshold, 100, nullptr, AZ::ConsoleFunctorFlags::Null, + "Number of milliseconds that AssetHandler::LoadAssetData can execute for before printing a warning."); + AZ_CVAR(int, cl_assetLoadDelay, 0, nullptr, AZ::ConsoleFunctorFlags::Null, + "Number of milliseconds to artifically delay an asset load."); + AZ_CVAR(bool, cl_assetLoadError, false, nullptr, AZ::ConsoleFunctorFlags::Null, + "Enable failure of all asset loads."); + + static constexpr char kAssetDBInstanceVarName[] = "AssetDatabaseInstance"; + + /* + * This is the base class for Async AssetDatabase jobs + */ + class AssetDatabaseAsyncJob + : public AssetDatabaseJob + , public Job { - AZ_CVAR(bool, cl_assetLoadWarningEnable, false, nullptr, AZ::ConsoleFunctorFlags::Null, - "Enable warnings that show when AssetHandler::LoadAssetData has exceeded the time set in cl_assetLoadWarningMsThreshold."); - AZ_CVAR(uint32_t, cl_assetLoadWarningMsThreshold, 100, nullptr, AZ::ConsoleFunctorFlags::Null, - "Number of milliseconds that AssetHandler::LoadAssetData can execute for before printing a warning."); - AZ_CVAR(int, cl_assetLoadDelay, 0, nullptr, AZ::ConsoleFunctorFlags::Null, - "Number of milliseconds to artifically delay an asset load."); - AZ_CVAR(bool, cl_assetLoadError, false, nullptr, AZ::ConsoleFunctorFlags::Null, - "Enable failure of all asset loads."); - - static constexpr char kAssetDBInstanceVarName[] = "AssetDatabaseInstance"; - - /* - * This is the base class for Async AssetDatabase jobs - */ - class AssetDatabaseAsyncJob - : public AssetDatabaseJob - , public Job + public: + AssetDatabaseAsyncJob(JobContext* jobContext, bool deleteWhenDone, AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) + : AssetDatabaseJob(owner, asset, assetHandler) + , Job(deleteWhenDone, jobContext) { - public: - AssetDatabaseAsyncJob(JobContext* jobContext, bool deleteWhenDone, AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) - : AssetDatabaseJob(owner, asset, assetHandler) - , Job(deleteWhenDone, jobContext) - { - } + } - ~AssetDatabaseAsyncJob() override - { - } - }; - - /** - * Internally allows threads blocking on asset loads to be notified on load completion. - */ - class BlockingAssetLoadEvents - : public EBusTraits + ~AssetDatabaseAsyncJob() override { - public: - ////////////////////////////////////////////////////////////////////////// - // EBusTraits overrides - static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById; - using BusIdType = AssetId; - using MutexType = AZStd::recursive_mutex; - - template - struct AssetJobConnectionPolicy - : public EBusConnectionPolicy - { - static void Connect(typename Bus::BusPtr& busPtr, typename Bus::Context& context, typename Bus::HandlerNode& handler, typename Bus::Context::ConnectLockGuard& connectLock, const typename Bus::BusIdType& id = 0) - { - typename Bus::BusIdType actualId = AssetInternal::ResolveAssetId(id); - EBusConnectionPolicy::Connect(busPtr, context, handler, connectLock, actualId); - - // If the asset is loaded or failed already, deliver the status update immediately - // Note that we check IsReady here, ReadyPreNotify must be tested because there is - // a small gap between ReadyPreNotify and Ready where the callback could be missed. - // Also note because the Asset<> reference isn't passed around anywhere, it doesn't matter - // what the AssetLoadBehavior is set to, as it will never make it back to any callers. - Asset assetData(AssetInternal::GetAssetData(actualId, AssetLoadBehavior::Default)); - if (assetData) - { - if (assetData->IsReady() || assetData->IsError()) - { - connectLock.unlock(); - handler->OnLoadComplete(); - } - } - } - }; - - template - using ConnectionPolicy = AssetJobConnectionPolicy; - - virtual void OnLoadComplete() = 0; - virtual void OnLoadCanceled(AssetId assetId) = 0; - }; - - using BlockingAssetLoadBus = EBus; - - /* - * This class processes async AssetDatabase load jobs - */ - class LoadAssetJob - : public AssetDatabaseAsyncJob - { - public: - AZ_CLASS_ALLOCATOR(LoadAssetJob, ThreadPoolAllocator, 0); - - LoadAssetJob(AssetManager* owner, const Asset& asset, - AZStd::shared_ptr dataStream, bool isReload, AZ::IO::IStreamerTypes::RequestStatus requestState, - AssetHandler* handler, const AssetLoadParameters& loadParams, bool signalLoaded) - : AssetDatabaseAsyncJob(JobContext::GetGlobalContext(), true, owner, asset, handler) - , m_dataStream(dataStream) - , m_isReload(isReload) - , m_requestState(requestState) - , m_loadParams(loadParams) - , m_signalLoaded(signalLoaded) - { - AZ_Assert(m_dataStream, "Data stream pointer received through the callback from AZ::IO::Streamer is invalid."); - - AZ_Assert((m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Completed) - || (m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Canceled) - || (m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Failed), - "AssetManager::LoadAssetJob was called with an unexpected streamer state: %i", m_requestState); - } - - ~LoadAssetJob() override - { - } - - void Process() override - { - Asset asset = m_asset.GetStrongReference(); - - // Verify that we didn't somehow get here after the Asset Manager has finished shutting down. - AZ_Assert(AssetManager::IsReady(), "Asset Manager shutdown didn't clean up pending asset loads properly."); - if (!AssetManager::IsReady()) - { - return; - } - - bool shouldCancel = m_owner->ShouldCancelAllActiveJobs() - || !asset // No outstanding references, so cancel the load - || m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Canceled; - - if (shouldCancel) - { - BlockingAssetLoadBus::Event(m_asset.GetId(), &BlockingAssetLoadBus::Events::OnLoadCanceled, m_asset.GetId()); - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetCanceled, m_asset.GetId()); - } - else - { - - AZ_PROFILE_SCOPE(AzCore, "AZ::Data::LoadAssetJob::Process: %s", - asset.GetHint().c_str()); - - AZ_ASSET_ATTACH_TO_SCOPE(this); - - if (m_owner->ValidateAndRegisterAssetLoading(asset)) - { - LoadAndSignal(asset); - } - } - } - - void LoadAndSignal(Asset& asset) - { - const bool loadSucceeded = LoadData(); - - if (m_signalLoaded && loadSucceeded) - { - AZ_Assert(!m_isReload, "OnAssetDataLoaded signal isn't supported for asset reloads."); - // This asset has preload dependencies, we need to evaluate whether they're all ready before calling PostLoad - AssetLoadBus::Event(asset.GetId(), &AssetLoadBus::Events::OnAssetDataLoaded, asset); - } - else - { - // As long as we don't need to signal preload dependencies, just finish the load whether or not it was successful. - m_owner->PostLoad(asset, loadSucceeded, m_isReload, m_assetHandler); - } - } - - bool LoadData() - { - Asset asset = m_asset.GetStrongReference(); - - if(cl_assetLoadDelay > 0) - { - AZ_PROFILE_SCOPE(AzCore, "LoadData suspended"); - AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(cl_assetLoadDelay)); - } - - AZ_ASSET_NAMED_SCOPE(asset.GetHint().c_str()); - bool loadedSuccessfully = false; - - if (!cl_assetLoadError && m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Completed) - { - if (m_dataStream->IsFullyLoaded()) - { - AssetHandler::LoadResult result = - m_assetHandler->LoadAssetDataFromStream(asset, m_dataStream, m_loadParams.m_assetLoadFilterCB); - loadedSuccessfully = (result == AssetHandler::LoadResult::LoadComplete); - } - } - - return loadedSuccessfully; - } - - private: - AZStd::shared_ptr m_dataStream; - AssetLoadParameters m_loadParams{}; - AZ::IO::IStreamerTypes::RequestStatus m_requestState{ AZ::IO::IStreamerTypes::RequestStatus::Pending}; - bool m_isReload{ false }; - bool m_signalLoaded{ false }; - }; - - - /** - * Utility class to wait when a blocking load is requested for an asset that's already loading asynchronously. - * Uses the BlockingAssetLoadBus to detect completion, and a semaphore to signal it. - */ - - class WaitForAsset - : public BlockingAssetLoadBus::Handler - { - public: - AZ_CLASS_ALLOCATOR(WaitForAsset, ThreadPoolAllocator, 0); - - - WaitForAsset(const Asset& assetToWaitFor, bool shouldDispatchEvents) - : m_assetData(assetToWaitFor) - , m_shouldDispatchEvents(shouldDispatchEvents) - { - // Track all blocking requests with the AssetManager. This enables load jobs to potentially get routed - // to the thread that's currently blocking waiting on the load job to complete. - AssetManager::Instance().AddBlockingRequest(m_assetData.GetId(), this); - } - - ~WaitForAsset() override - { - // Stop tracking the blocking request, which will ensure that load jobs won't be provided to this instance - // for processing. - AssetManager::Instance().RemoveBlockingRequest(m_assetData.GetId(), this); - - // It shouldn't be possible to destroy a blocking load request before the load job that it's blocked on - // has been processed, so assert if it ever happens, but make sure to process it just in case. - if (m_loadJob) - { - // (If a valid case is ever found where this can occur, it should be safe to remove the assert) - AZ_Assert(false, "Blocking load request is being deleted before it could process the blocking load."); - ProcessLoadJob(); - } - } - - // Provides a blocked load with a LoadJob to process while it's blocking. - // Returns true if it can be queued, false if it can't. - bool QueueAssetLoadJob(LoadAssetJob* loadJob) - { - if(m_shouldDispatchEvents) - { - // Any load job that is going to be dispatching events should not accept additional work since dispatching events - // can lead to more code that's blocking on an asset load which prevents us from finishing the dispatch - // and doing the assigned work. - // Specifically, if dispatching leads to a second block call, the load job will be assigned to the first block call, - // which will never be completed until the second block call is finished. If both blocks are on the same asset, - // we end up deadlocked. - return false; - } - - AZStd::scoped_lock mutexLock(m_loadJobMutex); - - AZ_Assert(!m_loadJob, "Trying to process multiple load jobs for the same asset with the same blocking handler."); - if (!m_loadJob) - { - m_loadJob = loadJob; - m_waitEvent.release(); - return true; - } - - return false; - } - - void OnLoadComplete() override - { - Finish(); - } - - void OnLoadCanceled([[maybe_unused]] const AssetId assetId) override - { - Finish(); - } - - void WaitUntilReady() - { - BusConnect(m_assetData.GetId()); - - Wait(); - - BusDisconnect(m_assetData.GetId()); - } - - protected: - void Wait() - { - AZ_PROFILE_SCOPE(AzCore, "WaitForAsset - %s", m_assetData.GetHint().c_str()); - - // Continue to loop until the load completes. (Most of the time in the loop will be spent in a thread-blocking state) - while (!m_loadCompleted) - { - if (m_shouldDispatchEvents) - { - // The event will wake up either when the load finishes, a load job is queued for processing, or every - // N milliseconds to see if it should dispatch events. - constexpr int MaxWaitBetweenDispatchMs = 1; - while (!m_waitEvent.try_acquire_for(AZStd::chrono::milliseconds(MaxWaitBetweenDispatchMs))) - { - AssetManager::Instance().DispatchEvents(); - } - } - else - { - - // Don't wake up until a load job is queued for processing or the load is entirely finished. - m_waitEvent.acquire(); - } - - // Check to see if any load jobs have been provided for this thread to process. - // (Load jobs will attempt to reuse blocked threads before spinning off new job threads) - ProcessLoadJob(); - } - - // Pump the AssetBus function queue once more after the load has completed in case additional - // functions have been queued between the last call to DispatchEvents and the completion - // of the current load job - if (m_shouldDispatchEvents) - { - AssetManager::Instance().DispatchEvents(); - } - } - - void Finish() - { - AZ_PROFILE_FUNCTION(AzCore); - m_loadCompleted = true; - m_waitEvent.release(); - } - - bool ProcessLoadJob() - { - AZStd::scoped_lock mutexLock(m_loadJobMutex); - bool jobProcessed = false; - - if (m_loadJob) - { - m_loadJob->Process(); - if (m_loadJob->IsAutoDelete()) - { - delete m_loadJob; - } - m_loadJob = nullptr; - jobProcessed = true; - } - - return jobProcessed; - } - - Asset m_assetData; - AZStd::binary_semaphore m_waitEvent; - const bool m_shouldDispatchEvents{ false }; - LoadAssetJob* m_loadJob{ nullptr }; - AZStd::mutex m_loadJobMutex; - AZStd::atomic_bool m_loadCompleted{ false }; - }; - - - /* - * This class processes async AssetDatabase save jobs - */ - class SaveAssetJob - : public AssetDatabaseAsyncJob - { - public: - AZ_CLASS_ALLOCATOR(SaveAssetJob, ThreadPoolAllocator, 0); - - SaveAssetJob(JobContext* jobContext, AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) - : AssetDatabaseAsyncJob(jobContext, true, owner, asset, assetHandler) - { - } - - ~SaveAssetJob() override - { - } - - void Process() override - { - SaveAsset(); - } - - void SaveAsset() - { - auto asset = m_asset.GetStrongReference(); - AZ_PROFILE_FUNCTION(AzCore); - bool isSaved = false; - AssetStreamInfo saveInfo = m_owner->GetSaveStreamInfoForAsset(asset.GetId(), asset.GetType()); - if (saveInfo.IsValid()) - { - IO::FileIOStream stream(saveInfo.m_streamName.c_str(), saveInfo.m_streamFlags); - stream.Seek(saveInfo.m_dataOffset, IO::GenericStream::SeekMode::ST_SEEK_BEGIN); - isSaved = m_assetHandler->SaveAssetData(asset, &stream); - } - // queue broadcast message for delivery on game thread - AssetBus::QueueEvent(asset.GetId(), &AssetBus::Events::OnAssetSaved, asset, isSaved); - } - }; + } + }; + /** + * Internally allows threads blocking on asset loads to be notified on load completion. + */ + class BlockingAssetLoadEvents + : public EBusTraits + { + public: ////////////////////////////////////////////////////////////////////////// - // Globals - EnvironmentVariable AssetManager::s_assetDB = nullptr; - ////////////////////////////////////////////////////////////////////////// + // EBusTraits overrides + static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById; + using BusIdType = AssetId; + using MutexType = AZStd::recursive_mutex; - //========================================================================= - // AssetDatabaseJob - // [4/3/2014] - //========================================================================= - AssetDatabaseJob::AssetDatabaseJob(AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) + template + struct AssetJobConnectionPolicy + : public EBusConnectionPolicy { - m_owner = owner; - m_asset = AssetInternal::WeakAsset(asset); - m_assetHandler = assetHandler; - owner->AddJob(this); - } - - //========================================================================= - // ~AssetDatabaseJob - // [4/3/2014] - //========================================================================= - AssetDatabaseJob::~AssetDatabaseJob() - { - // Make sure that the asset reference is cleared out prior to removing the job registration. - // It's possible that clearing this reference will trigger the cleanup code for the asset, so if we wait for the - // destructor to clear it *after* the RemoveJob call, then HasActiveJobsOrStreamerRequests() will be able to return - // false even though the job is still executing asset-related code. - m_asset = {}; - m_owner->RemoveJob(this); - } - - //========================================================================= - // Create - // [6/12/2012] - //========================================================================= - bool AssetManager::Create(const Descriptor& desc) - { - AZ_Assert(!s_assetDB || !s_assetDB.Get(), "AssetManager already created!"); - - if (!s_assetDB) + static void Connect(typename Bus::BusPtr& busPtr, typename Bus::Context& context, typename Bus::HandlerNode& handler, typename Bus::Context::ConnectLockGuard& connectLock, const typename Bus::BusIdType& id = 0) { - s_assetDB = Environment::CreateVariable(kAssetDBInstanceVarName); - } - if (!s_assetDB.Get()) - { - s_assetDB.Set(aznew AssetManager(desc)); - } + typename Bus::BusIdType actualId = AssetInternal::ResolveAssetId(id); + EBusConnectionPolicy::Connect(busPtr, context, handler, connectLock, actualId); - return true; - } - - //========================================================================= - // Destroy - // [6/12/2012] - //========================================================================= - void AssetManager::Destroy() - { - AZ_Assert(s_assetDB, "AssetManager not created!"); - delete (*s_assetDB); - *s_assetDB = nullptr; - } - - //========================================================================= - // IsReady - //========================================================================= - bool AssetManager::IsReady() - { - if (!s_assetDB) - { - s_assetDB = Environment::FindVariable(kAssetDBInstanceVarName); - } - - return s_assetDB && *s_assetDB; - } - - //========================================================================= - // Instance - //========================================================================= - AssetManager& AssetManager::Instance() - { - if (!s_assetDB) - { - s_assetDB = Environment::FindVariable(kAssetDBInstanceVarName); - } - - AZ_Assert(s_assetDB && *s_assetDB, "AssetManager not created!"); - return *(*s_assetDB); - } - - bool AssetManager::SetInstance(AssetManager* assetManager) - { - if (!s_assetDB) - { - s_assetDB = Environment::CreateVariable(kAssetDBInstanceVarName); - } - - // The old instance needs to be null or else it will leak on the assignment. - AZ_Assert(!(*s_assetDB), - "AssetManager::SetInstance was called without first destroying the old instance and setting it to nullptr. " - "This will cause the previous AssetManager instance to leak." ); - - (*s_assetDB) = assetManager; - return true; - } - - //========================================================================= - // AssetDatabase - // [6/12/2012] - //========================================================================= - AssetManager::AssetManager(const AssetManager::Descriptor& desc) - : m_mainThreadId(AZStd::this_thread::get_id()) - , m_debugAssetEvents(AZ::Interface::Get()) - { - (void)desc; - - AssetManagerBus::Handler::BusConnect(); - } - - //========================================================================= - // ~AssetManager - // [6/12/2012] - //========================================================================= - AssetManager::~AssetManager() - { - PrepareShutDown(); - - // Acquire the asset lock to make sure nobody else is trying to do anything fancy with assets - AZStd::scoped_lock assetLock(m_assetMutex); - - while (!m_handlers.empty()) - { - AssetHandlerMap::iterator it = m_handlers.begin(); - AssetHandler* handler = it->second; - UnregisterHandler(handler); - delete handler; - } - - AssetManagerBus::Handler::BusDisconnect(); - } - - //========================================================================= - // DispatchEvents - // [04/02/2014] - //========================================================================= - void AssetManager::DispatchEvents() - { - AZ_PROFILE_FUNCTION(AzCore); - AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchBegin); - while (AssetBus::QueuedEventCount()) - { - AssetBus::ExecuteQueuedEvents(); - } - AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchEnd); - } - - //========================================================================= - void AssetManager::SetAssetInfoUpgradingEnabled(bool enable) - { - m_assetInfoUpgradingEnabled = enable; - } - - bool AssetManager::GetAssetInfoUpgradingEnabled() const - { -#if defined(_RELEASE) - // in release ("FINAL") builds, we never do this. - return false; -#else - return m_assetInfoUpgradingEnabled; -#endif - } - - bool AssetManager::ShouldCancelAllActiveJobs() const - { - return m_cancelAllActiveJobs; - } - - void AssetManager::SetParallelDependentLoadingEnabled(bool enable) - { - m_enableParallelDependentLoading = enable; - } - - bool AssetManager::GetParallelDependentLoadingEnabled() const - { - return m_enableParallelDependentLoading; - } - - void AssetManager::PrepareShutDown() - { - m_cancelAllActiveJobs = true; - - // We want to ensure that no active load jobs are in flight and - // therefore we need to wait till all jobs have completed. Please note that jobs get deleted automatically once they complete. - WaitForActiveJobsAndStreamerRequestsToFinish(); - - m_ownedAssetContainerLookup.clear(); - m_ownedAssetContainers.clear(); - m_assetContainers.clear(); - - // Ensure that there are no queued events on the AssetBus - DispatchEvents(); - } - - void AssetManager::WaitForActiveJobsAndStreamerRequestsToFinish() - { - while (HasActiveJobsOrStreamerRequests()) - { - DispatchEvents(); - AZStd::this_thread::yield(); - } - } - - //========================================================================= - // RegisterHandler - // [7/9/2014] - //========================================================================= - void AssetManager::RegisterHandler(AssetHandler* handler, const AssetType& assetType) - { - AZ_Error("AssetDatabase", handler != nullptr, "Attempting to register a null asset handler!"); - if (handler) - { - if (m_handlers.insert(AZStd::make_pair(assetType, handler)).second) + // If the asset is loaded or failed already, deliver the status update immediately + // Note that we check IsReady here, ReadyPreNotify must be tested because there is + // a small gap between ReadyPreNotify and Ready where the callback could be missed. + // Also note because the Asset<> reference isn't passed around anywhere, it doesn't matter + // what the AssetLoadBehavior is set to, as it will never make it back to any callers. + Asset assetData(AssetInternal::GetAssetData(actualId, AssetLoadBehavior::Default)); + if (assetData) { - handler->m_nHandledTypes++; - } - else - { - AZ_Error("AssetDatabase", false, "Asset type %s already has a handler registered! New registration ignored!", assetType.ToString().c_str()); - } - } - } - - //========================================================================= - // UnregisterHandler - // [7/9/2014] - //========================================================================= - void AssetManager::UnregisterHandler(AssetHandler* handler) - { - AZ_Error("AssetDatabase", handler != nullptr, "Attempting to unregister a null asset handler!"); - if (handler) - { - for (AssetHandlerMap::iterator it = m_handlers.begin(); it != m_handlers.end(); /*++it*/) - { - if (it->second == handler) + if (assetData->IsReady() || assetData->IsError()) { - // When unregistering asset handlers, it's possible that there are still some load jobs that have "finished" but - // haven't destroyed themselves yet by the time the asset handler gets unregistered. LoadAssetJob contains a weak - // asset reference that doesn't clear until the job is destroyed, which happens *after* the OnAssetReady - // notification is triggered. If the thread gets swapped out between the OnAssetReady and the job destruction, - // the job will still be holding onto an asset reference for this asset handler, and it will trigger the - // error below. To ensure that this case doesn't happen, we will instead call - // WaitForActiveJobsAndStreamerRequestsToFinish() to make sure that any in-process jobs have completely cleaned - // themselves up before proceeding forward. - // One example of this pattern occurs in unit tests, where the test loads an asset, validates it, destroys the - // asset, and unregisters the handler, all in rapid succession. This would extremely infrequently - // (~1 per 5000 runs) trigger the error case if we didn't wait for the jobs to finish here. - WaitForActiveJobsAndStreamerRequestsToFinish(); - - { - // this scope is used to control the scope of the lock. - AZStd::lock_guard assetLock(m_assetMutex); - for (const auto &assetEntry : m_assets) - { - // is the handler that handles this type, this handler we're removing? - if (assetEntry.second->m_registeredHandler == handler) - { - AZ_Error("AssetManager", false, "Asset handler for %s is being removed, when assetid %s is still loaded!\n", - assetEntry.second->GetType().ToString().c_str(), - assetEntry.second->GetId().ToString().c_str()); // this will write the name IF AVAILABLE - assetEntry.second->UnregisterWithHandler(); - } - } - } - it = m_handlers.erase(it); - handler->m_nHandledTypes--; - } - else - { - ++it; + connectLock.unlock(); + handler->OnLoadComplete(); } } } + }; + + template + using ConnectionPolicy = AssetJobConnectionPolicy; + + virtual void OnLoadComplete() = 0; + virtual void OnLoadCanceled(AssetId assetId) = 0; + }; + + using BlockingAssetLoadBus = EBus; + + /* + * This class processes async AssetDatabase load jobs + */ + class LoadAssetJob + : public AssetDatabaseAsyncJob + { + public: + AZ_CLASS_ALLOCATOR(LoadAssetJob, ThreadPoolAllocator, 0); + + LoadAssetJob(AssetManager* owner, const Asset& asset, + AZStd::shared_ptr dataStream, bool isReload, AZ::IO::IStreamerTypes::RequestStatus requestState, + AssetHandler* handler, const AssetLoadParameters& loadParams, bool signalLoaded) + : AssetDatabaseAsyncJob(JobContext::GetGlobalContext(), true, owner, asset, handler) + , m_dataStream(dataStream) + , m_isReload(isReload) + , m_requestState(requestState) + , m_loadParams(loadParams) + , m_signalLoaded(signalLoaded) + { + AZ_Assert(m_dataStream, "Data stream pointer received through the callback from AZ::IO::Streamer is invalid."); + + AZ_Assert((m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Completed) + || (m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Canceled) + || (m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Failed), + "AssetManager::LoadAssetJob was called with an unexpected streamer state: %i", m_requestState); } - //========================================================================= - // RegisterCatalog - // [8/27/2012] - //========================================================================= - void AssetManager::RegisterCatalog(AssetCatalog* catalog, const AssetType& assetType) + ~LoadAssetJob() override { - AZ_Error("AssetDatabase", catalog != nullptr, "Attempting to register a null catalog!"); - if (catalog) - { - AZStd::scoped_lock l(m_catalogMutex); - if (m_catalogs.insert(AZStd::make_pair(assetType, catalog)).second == false) - { - AZ_Error("AssetDatabase", false, "Asset type %s already has a catalog registered! New registration ignored!", assetType.ToString().c_str()); - } - } } - //========================================================================= - // UnregisterCatalog - // [8/27/2012] - //========================================================================= - void AssetManager::UnregisterCatalog(AssetCatalog* catalog) + void Process() override { - AZ_Error("AssetDatabase", catalog != nullptr, "Attempting to unregister a null catalog!"); - if (catalog) - { - AZStd::scoped_lock l(m_catalogMutex); - for (AssetCatalogMap::iterator iter = m_catalogs.begin(); iter != m_catalogs.end(); ) - { - if (iter->second == catalog) - { - iter = m_catalogs.erase(iter); - } - else - { - ++iter; - } + Asset asset = m_asset.GetStrongReference(); - } - } - } - - //========================================================================= - // GetHandledAssetTypes - // [6/27/2016] - //========================================================================= - void AssetManager::GetHandledAssetTypes(AssetCatalog* catalog, AZStd::vector& assetTypes) - { - for (AssetCatalogMap::iterator iter = m_catalogs.begin(); iter != m_catalogs.end(); iter++) - { - if (iter->second == catalog) - { - assetTypes.push_back(iter->first); - } - } - } - - void AssetManager::SuspendAssetRelease() - { - ++m_suspendAssetRelease; - } - - void AssetManager::ResumeAssetRelease() - { - if(--m_suspendAssetRelease != 0) + // Verify that we didn't somehow get here after the Asset Manager has finished shutting down. + AZ_Assert(AssetManager::IsReady(), "Asset Manager shutdown didn't clean up pending asset loads properly."); + if (!AssetManager::IsReady()) { return; } - AZStd::scoped_lock assetLock(m_assetMutex); - // First, release any containers that were loading this asset - for (auto asset = m_assets.begin();asset != m_assets.end();) + bool shouldCancel = m_owner->ShouldCancelAllActiveJobs() + || !asset // No outstanding references, so cancel the load + || m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Canceled; + + if (shouldCancel) { - if (asset->second->m_useCount == 0) + BlockingAssetLoadBus::Event(m_asset.GetId(), &BlockingAssetLoadBus::Events::OnLoadCanceled, m_asset.GetId()); + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetCanceled, m_asset.GetId()); + } + else + { + + AZ_PROFILE_SCOPE(AzCore, "AZ::Data::LoadAssetJob::Process: %s", + asset.GetHint().c_str()); + + AZ_ASSET_ATTACH_TO_SCOPE(this); + + if (m_owner->ValidateAndRegisterAssetLoading(asset)) { - auto releaseAsset = asset->second; - ++asset; - ReleaseAssetContainersForAsset(releaseAsset); + LoadAndSignal(asset); + } + } + } + + void LoadAndSignal(Asset& asset) + { + const bool loadSucceeded = LoadData(); + + if (m_signalLoaded && loadSucceeded) + { + AZ_Assert(!m_isReload, "OnAssetDataLoaded signal isn't supported for asset reloads."); + // This asset has preload dependencies, we need to evaluate whether they're all ready before calling PostLoad + AssetLoadBus::Event(asset.GetId(), &AssetLoadBus::Events::OnAssetDataLoaded, asset); + } + else + { + // As long as we don't need to signal preload dependencies, just finish the load whether or not it was successful. + m_owner->PostLoad(asset, loadSucceeded, m_isReload, m_assetHandler); + } + } + + bool LoadData() + { + Asset asset = m_asset.GetStrongReference(); + + if(cl_assetLoadDelay > 0) + { + AZ_PROFILE_SCOPE(AzCore, "LoadData suspended"); + AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(cl_assetLoadDelay)); + } + + AZ_ASSET_NAMED_SCOPE(asset.GetHint().c_str()); + bool loadedSuccessfully = false; + + if (!cl_assetLoadError && m_requestState == AZ::IO::IStreamerTypes::RequestStatus::Completed) + { + if (m_dataStream->IsFullyLoaded()) + { + AssetHandler::LoadResult result = + m_assetHandler->LoadAssetDataFromStream(asset, m_dataStream, m_loadParams.m_assetLoadFilterCB); + loadedSuccessfully = (result == AssetHandler::LoadResult::LoadComplete); + } + } + + return loadedSuccessfully; + } + + private: + AZStd::shared_ptr m_dataStream; + AssetLoadParameters m_loadParams{}; + AZ::IO::IStreamerTypes::RequestStatus m_requestState{ AZ::IO::IStreamerTypes::RequestStatus::Pending}; + bool m_isReload{ false }; + bool m_signalLoaded{ false }; + }; + + + /** + * Utility class to wait when a blocking load is requested for an asset that's already loading asynchronously. + * Uses the BlockingAssetLoadBus to detect completion, and a semaphore to signal it. + */ + + class WaitForAsset + : public BlockingAssetLoadBus::Handler + { + public: + AZ_CLASS_ALLOCATOR(WaitForAsset, ThreadPoolAllocator, 0); + + + WaitForAsset(const Asset& assetToWaitFor, bool shouldDispatchEvents) + : m_assetData(assetToWaitFor) + , m_shouldDispatchEvents(shouldDispatchEvents) + { + // Track all blocking requests with the AssetManager. This enables load jobs to potentially get routed + // to the thread that's currently blocking waiting on the load job to complete. + AssetManager::Instance().AddBlockingRequest(m_assetData.GetId(), this); + } + + ~WaitForAsset() override + { + // Stop tracking the blocking request, which will ensure that load jobs won't be provided to this instance + // for processing. + AssetManager::Instance().RemoveBlockingRequest(m_assetData.GetId(), this); + + // It shouldn't be possible to destroy a blocking load request before the load job that it's blocked on + // has been processed, so assert if it ever happens, but make sure to process it just in case. + if (m_loadJob) + { + // (If a valid case is ever found where this can occur, it should be safe to remove the assert) + AZ_Assert(false, "Blocking load request is being deleted before it could process the blocking load."); + ProcessLoadJob(); + } + } + + // Provides a blocked load with a LoadJob to process while it's blocking. + // Returns true if it can be queued, false if it can't. + bool QueueAssetLoadJob(LoadAssetJob* loadJob) + { + if(m_shouldDispatchEvents) + { + // Any load job that is going to be dispatching events should not accept additional work since dispatching events + // can lead to more code that's blocking on an asset load which prevents us from finishing the dispatch + // and doing the assigned work. + // Specifically, if dispatching leads to a second block call, the load job will be assigned to the first block call, + // which will never be completed until the second block call is finished. If both blocks are on the same asset, + // we end up deadlocked. + return false; + } + + AZStd::scoped_lock mutexLock(m_loadJobMutex); + + AZ_Assert(!m_loadJob, "Trying to process multiple load jobs for the same asset with the same blocking handler."); + if (!m_loadJob) + { + m_loadJob = loadJob; + m_waitEvent.release(); + return true; + } + + return false; + } + + void OnLoadComplete() override + { + Finish(); + } + + void OnLoadCanceled([[maybe_unused]] const AssetId assetId) override + { + Finish(); + } + + void WaitUntilReady() + { + BusConnect(m_assetData.GetId()); + + Wait(); + + BusDisconnect(m_assetData.GetId()); + } + + protected: + void Wait() + { + AZ_PROFILE_SCOPE(AzCore, "WaitForAsset - %s", m_assetData.GetHint().c_str()); + + // Continue to loop until the load completes. (Most of the time in the loop will be spent in a thread-blocking state) + while (!m_loadCompleted) + { + if (m_shouldDispatchEvents) + { + // The event will wake up either when the load finishes, a load job is queued for processing, or every + // N milliseconds to see if it should dispatch events. + constexpr int MaxWaitBetweenDispatchMs = 1; + while (!m_waitEvent.try_acquire_for(AZStd::chrono::milliseconds(MaxWaitBetweenDispatchMs))) + { + AssetManager::Instance().DispatchEvents(); + } } else { - ++asset; + + // Don't wake up until a load job is queued for processing or the load is entirely finished. + m_waitEvent.acquire(); } + + // Check to see if any load jobs have been provided for this thread to process. + // (Load jobs will attempt to reuse blocked threads before spinning off new job threads) + ProcessLoadJob(); } - // Second, release the assets themselves - - AZStd::vector assetsToRelease; - - for(auto&& asset : m_assets) + // Pump the AssetBus function queue once more after the load has completed in case additional + // functions have been queued between the last call to DispatchEvents and the completion + // of the current load job + if (m_shouldDispatchEvents) { - if(asset.second->m_weakUseCount == 0) + AssetManager::Instance().DispatchEvents(); + } + } + + void Finish() + { + AZ_PROFILE_FUNCTION(AzCore); + m_loadCompleted = true; + m_waitEvent.release(); + } + + bool ProcessLoadJob() + { + AZStd::scoped_lock mutexLock(m_loadJobMutex); + bool jobProcessed = false; + + if (m_loadJob) + { + m_loadJob->Process(); + if (m_loadJob->IsAutoDelete()) { - // Keep a separate list of assets to release, because releasing them will modify the m_assets list that we're - // currently looping on. - assetsToRelease.push_back(asset.second); + delete m_loadJob; + } + m_loadJob = nullptr; + jobProcessed = true; + } + + return jobProcessed; + } + + Asset m_assetData; + AZStd::binary_semaphore m_waitEvent; + const bool m_shouldDispatchEvents{ false }; + LoadAssetJob* m_loadJob{ nullptr }; + AZStd::mutex m_loadJobMutex; + AZStd::atomic_bool m_loadCompleted{ false }; + }; + + + /* + * This class processes async AssetDatabase save jobs + */ + class SaveAssetJob + : public AssetDatabaseAsyncJob + { + public: + AZ_CLASS_ALLOCATOR(SaveAssetJob, ThreadPoolAllocator, 0); + + SaveAssetJob(JobContext* jobContext, AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) + : AssetDatabaseAsyncJob(jobContext, true, owner, asset, assetHandler) + { + } + + ~SaveAssetJob() override + { + } + + void Process() override + { + SaveAsset(); + } + + void SaveAsset() + { + auto asset = m_asset.GetStrongReference(); + AZ_PROFILE_FUNCTION(AzCore); + bool isSaved = false; + AssetStreamInfo saveInfo = m_owner->GetSaveStreamInfoForAsset(asset.GetId(), asset.GetType()); + if (saveInfo.IsValid()) + { + IO::FileIOStream stream(saveInfo.m_streamName.c_str(), saveInfo.m_streamFlags); + stream.Seek(saveInfo.m_dataOffset, IO::GenericStream::SeekMode::ST_SEEK_BEGIN); + isSaved = m_assetHandler->SaveAssetData(asset, &stream); + } + // queue broadcast message for delivery on game thread + AssetBus::QueueEvent(asset.GetId(), &AssetBus::Events::OnAssetSaved, asset, isSaved); + } + }; + + ////////////////////////////////////////////////////////////////////////// + // Globals + EnvironmentVariable AssetManager::s_assetDB = nullptr; + ////////////////////////////////////////////////////////////////////////// + + //========================================================================= + // AssetDatabaseJob + // [4/3/2014] + //========================================================================= + AssetDatabaseJob::AssetDatabaseJob(AssetManager* owner, const Asset& asset, AssetHandler* assetHandler) + { + m_owner = owner; + m_asset = AssetInternal::WeakAsset(asset); + m_assetHandler = assetHandler; + owner->AddJob(this); + } + + //========================================================================= + // ~AssetDatabaseJob + // [4/3/2014] + //========================================================================= + AssetDatabaseJob::~AssetDatabaseJob() + { + // Make sure that the asset reference is cleared out prior to removing the job registration. + // It's possible that clearing this reference will trigger the cleanup code for the asset, so if we wait for the + // destructor to clear it *after* the RemoveJob call, then HasActiveJobsOrStreamerRequests() will be able to return + // false even though the job is still executing asset-related code. + m_asset = {}; + m_owner->RemoveJob(this); + } + + //========================================================================= + // Create + // [6/12/2012] + //========================================================================= + bool AssetManager::Create(const Descriptor& desc) + { + AZ_Assert(!s_assetDB || !s_assetDB.Get(), "AssetManager already created!"); + + if (!s_assetDB) + { + s_assetDB = Environment::CreateVariable(kAssetDBInstanceVarName); + } + if (!s_assetDB.Get()) + { + s_assetDB.Set(aznew AssetManager(desc)); + } + + return true; + } + + //========================================================================= + // Destroy + // [6/12/2012] + //========================================================================= + void AssetManager::Destroy() + { + AZ_Assert(s_assetDB, "AssetManager not created!"); + delete (*s_assetDB); + *s_assetDB = nullptr; + } + + //========================================================================= + // IsReady + //========================================================================= + bool AssetManager::IsReady() + { + if (!s_assetDB) + { + s_assetDB = Environment::FindVariable(kAssetDBInstanceVarName); + } + + return s_assetDB && *s_assetDB; + } + + //========================================================================= + // Instance + //========================================================================= + AssetManager& AssetManager::Instance() + { + if (!s_assetDB) + { + s_assetDB = Environment::FindVariable(kAssetDBInstanceVarName); + } + + AZ_Assert(s_assetDB && *s_assetDB, "AssetManager not created!"); + return *(*s_assetDB); + } + + bool AssetManager::SetInstance(AssetManager* assetManager) + { + if (!s_assetDB) + { + s_assetDB = Environment::CreateVariable(kAssetDBInstanceVarName); + } + + // The old instance needs to be null or else it will leak on the assignment. + AZ_Assert(!(*s_assetDB), + "AssetManager::SetInstance was called without first destroying the old instance and setting it to nullptr. " + "This will cause the previous AssetManager instance to leak." ); + + (*s_assetDB) = assetManager; + return true; + } + + //========================================================================= + // AssetDatabase + // [6/12/2012] + //========================================================================= + AssetManager::AssetManager(const AssetManager::Descriptor& desc) + : m_mainThreadId(AZStd::this_thread::get_id()) + , m_debugAssetEvents(AZ::Interface::Get()) + { + (void)desc; + + AssetManagerBus::Handler::BusConnect(); + } + + //========================================================================= + // ~AssetManager + // [6/12/2012] + //========================================================================= + AssetManager::~AssetManager() + { + PrepareShutDown(); + + // Acquire the asset lock to make sure nobody else is trying to do anything fancy with assets + AZStd::scoped_lock assetLock(m_assetMutex); + + while (!m_handlers.empty()) + { + AssetHandlerMap::iterator it = m_handlers.begin(); + AssetHandler* handler = it->second; + UnregisterHandler(handler); + delete handler; + } + + AssetManagerBus::Handler::BusDisconnect(); + } + + //========================================================================= + // DispatchEvents + // [04/02/2014] + //========================================================================= + void AssetManager::DispatchEvents() + { + AZ_PROFILE_FUNCTION(AzCore); + AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchBegin); + while (AssetBus::QueuedEventCount()) + { + AssetBus::ExecuteQueuedEvents(); + } + AssetManagerNotificationBus::Broadcast(&AssetManagerNotificationBus::Events::OnAssetEventsDispatchEnd); + } + + //========================================================================= + void AssetManager::SetAssetInfoUpgradingEnabled(bool enable) + { + m_assetInfoUpgradingEnabled = enable; + } + + bool AssetManager::GetAssetInfoUpgradingEnabled() const + { +#if defined(_RELEASE) + // in release ("FINAL") builds, we never do this. + return false; +#else + return m_assetInfoUpgradingEnabled; +#endif + } + + bool AssetManager::ShouldCancelAllActiveJobs() const + { + return m_cancelAllActiveJobs; + } + + void AssetManager::SetParallelDependentLoadingEnabled(bool enable) + { + m_enableParallelDependentLoading = enable; + } + + bool AssetManager::GetParallelDependentLoadingEnabled() const + { + return m_enableParallelDependentLoading; + } + + void AssetManager::PrepareShutDown() + { + m_cancelAllActiveJobs = true; + + // We want to ensure that no active load jobs are in flight and + // therefore we need to wait till all jobs have completed. Please note that jobs get deleted automatically once they complete. + WaitForActiveJobsAndStreamerRequestsToFinish(); + + m_ownedAssetContainerLookup.clear(); + m_ownedAssetContainers.clear(); + m_assetContainers.clear(); + + // Ensure that there are no queued events on the AssetBus + DispatchEvents(); + } + + void AssetManager::WaitForActiveJobsAndStreamerRequestsToFinish() + { + while (HasActiveJobsOrStreamerRequests()) + { + DispatchEvents(); + AZStd::this_thread::yield(); + } + } + + //========================================================================= + // RegisterHandler + // [7/9/2014] + //========================================================================= + void AssetManager::RegisterHandler(AssetHandler* handler, const AssetType& assetType) + { + AZ_Error("AssetDatabase", handler != nullptr, "Attempting to register a null asset handler!"); + if (handler) + { + if (m_handlers.insert(AZStd::make_pair(assetType, handler)).second) + { + handler->m_nHandledTypes++; + } + else + { + AZ_Error("AssetDatabase", false, "Asset type %s already has a handler registered! New registration ignored!", assetType.ToString().c_str()); + } + } + } + + //========================================================================= + // UnregisterHandler + // [7/9/2014] + //========================================================================= + void AssetManager::UnregisterHandler(AssetHandler* handler) + { + AZ_Error("AssetDatabase", handler != nullptr, "Attempting to unregister a null asset handler!"); + if (handler) + { + for (AssetHandlerMap::iterator it = m_handlers.begin(); it != m_handlers.end(); /*++it*/) + { + if (it->second == handler) + { + // When unregistering asset handlers, it's possible that there are still some load jobs that have "finished" but + // haven't destroyed themselves yet by the time the asset handler gets unregistered. LoadAssetJob contains a weak + // asset reference that doesn't clear until the job is destroyed, which happens *after* the OnAssetReady + // notification is triggered. If the thread gets swapped out between the OnAssetReady and the job destruction, + // the job will still be holding onto an asset reference for this asset handler, and it will trigger the + // error below. To ensure that this case doesn't happen, we will instead call + // WaitForActiveJobsAndStreamerRequestsToFinish() to make sure that any in-process jobs have completely cleaned + // themselves up before proceeding forward. + // One example of this pattern occurs in unit tests, where the test loads an asset, validates it, destroys the + // asset, and unregisters the handler, all in rapid succession. This would extremely infrequently + // (~1 per 5000 runs) trigger the error case if we didn't wait for the jobs to finish here. + WaitForActiveJobsAndStreamerRequestsToFinish(); + + { + // this scope is used to control the scope of the lock. + AZStd::lock_guard assetLock(m_assetMutex); + for (const auto &assetEntry : m_assets) + { + // is the handler that handles this type, this handler we're removing? + if (assetEntry.second->m_registeredHandler == handler) + { + AZ_Error("AssetManager", false, "Asset handler for %s is being removed, when assetid %s is still loaded!\n", + assetEntry.second->GetType().ToString().c_str(), + assetEntry.second->GetId().ToString().c_str()); // this will write the name IF AVAILABLE + assetEntry.second->UnregisterWithHandler(); + } + } + } + it = m_handlers.erase(it); + handler->m_nHandledTypes--; + } + else + { + ++it; } } + } + } - for(auto&& asset : assetsToRelease) + //========================================================================= + // RegisterCatalog + // [8/27/2012] + //========================================================================= + void AssetManager::RegisterCatalog(AssetCatalog* catalog, const AssetType& assetType) + { + AZ_Error("AssetDatabase", catalog != nullptr, "Attempting to register a null catalog!"); + if (catalog) + { + AZStd::scoped_lock l(m_catalogMutex); + if (m_catalogs.insert(AZStd::make_pair(assetType, catalog)).second == false) { - bool removeFromHash = asset->IsRegisterReadonlyAndShareable(); - // default creation token implies that the asset was not created by the asset manager and therefore it cannot be in the asset map. - removeFromHash = asset->m_creationToken == s_defaultCreationToken ? false : removeFromHash; + AZ_Error("AssetDatabase", false, "Asset type %s already has a catalog registered! New registration ignored!", assetType.ToString().c_str()); + } + } + } - ReleaseAsset(asset, asset->GetId(), asset->GetType(), removeFromHash, asset->m_creationToken); + //========================================================================= + // UnregisterCatalog + // [8/27/2012] + //========================================================================= + void AssetManager::UnregisterCatalog(AssetCatalog* catalog) + { + AZ_Error("AssetDatabase", catalog != nullptr, "Attempting to unregister a null catalog!"); + if (catalog) + { + AZStd::scoped_lock l(m_catalogMutex); + for (AssetCatalogMap::iterator iter = m_catalogs.begin(); iter != m_catalogs.end(); ) + { + if (iter->second == catalog) + { + iter = m_catalogs.erase(iter); + } + else + { + ++iter; + } + + } + } + } + + //========================================================================= + // GetHandledAssetTypes + // [6/27/2016] + //========================================================================= + void AssetManager::GetHandledAssetTypes(AssetCatalog* catalog, AZStd::vector& assetTypes) + { + for (AssetCatalogMap::iterator iter = m_catalogs.begin(); iter != m_catalogs.end(); iter++) + { + if (iter->second == catalog) + { + assetTypes.push_back(iter->first); + } + } + } + + void AssetManager::SuspendAssetRelease() + { + ++m_suspendAssetRelease; + } + + void AssetManager::ResumeAssetRelease() + { + if(--m_suspendAssetRelease != 0) + { + return; + } + + AZStd::scoped_lock assetLock(m_assetMutex); + // First, release any containers that were loading this asset + for (auto asset = m_assets.begin();asset != m_assets.end();) + { + if (asset->second->m_useCount == 0) + { + auto releaseAsset = asset->second; + ++asset; + ReleaseAssetContainersForAsset(releaseAsset); + } + else + { + ++asset; } } - AssetData::AssetStatus AssetManager::BlockUntilLoadComplete(const Asset& asset) + // Second, release the assets themselves + + AZStd::vector assetsToRelease; + + for(auto&& asset : m_assets) { - if(asset.GetStatus() == AssetData::AssetStatus::NotLoaded) + if(asset.second->m_weakUseCount == 0) { - AZ_Error("AssetManager", false, "BlockUntilLoadComplete must be called after an asset has been queued for load. Asset %s (%s) is not queued for load", - asset.GetHint().c_str(), asset.GetId().ToString().c_str()); + // Keep a separate list of assets to release, because releasing them will modify the m_assets list that we're + // currently looping on. + assetsToRelease.push_back(asset.second); } - else if(!asset.IsReady()) - { - // If this is the main thread we'll need to call DispatchEvents to make sure the events we're waiting on actually fire - // since the main thread is typically responsible for calling DispatchEvents elsewhere - const bool shouldDispatch = AZStd::this_thread::get_id() == m_mainThreadId; - - // Wait for the asset and all queued dependencies to finish loading. - WaitForAsset blockingWait(asset, shouldDispatch); - - blockingWait.WaitUntilReady(); - } - - return asset.GetStatus(); } - //========================================================================= - // FindAsset - //========================================================================= - Asset AssetManager::FindAsset(const AssetId& assetId, AssetLoadBehavior assetReferenceLoadBehavior) + for(auto&& asset : assetsToRelease) { - // Look up the asset id in the catalog, and use the result of that instead. - // If assetId is a legacy id, assetInfo.m_assetId will be the canonical id. Otherwise, assetInfo.m_assetID == assetId. - // This is because only canonical ids are stored in m_assets (see below). - // Only do the look up if upgrading is enabled - AZ::Data::AssetInfo assetInfo; - if (GetAssetInfoUpgradingEnabled()) + bool removeFromHash = asset->IsRegisterReadonlyAndShareable(); + // default creation token implies that the asset was not created by the asset manager and therefore it cannot be in the asset map. + removeFromHash = asset->m_creationToken == s_defaultCreationToken ? false : removeFromHash; + + ReleaseAsset(asset, asset->GetId(), asset->GetType(), removeFromHash, asset->m_creationToken); + } + } + + AssetData::AssetStatus AssetManager::BlockUntilLoadComplete(const Asset& asset) + { + if(asset.GetStatus() == AssetData::AssetStatus::NotLoaded) + { + AZ_Error("AssetManager", false, "BlockUntilLoadComplete must be called after an asset has been queued for load. Asset %s (%s) is not queued for load", + asset.GetHint().c_str(), asset.GetId().ToString().c_str()); + } + else if(!asset.IsReady()) + { + // If this is the main thread we'll need to call DispatchEvents to make sure the events we're waiting on actually fire + // since the main thread is typically responsible for calling DispatchEvents elsewhere + const bool shouldDispatch = AZStd::this_thread::get_id() == m_mainThreadId; + + // Wait for the asset and all queued dependencies to finish loading. + WaitForAsset blockingWait(asset, shouldDispatch); + + blockingWait.WaitUntilReady(); + } + + return asset.GetStatus(); + } + + //========================================================================= + // FindAsset + //========================================================================= + Asset AssetManager::FindAsset(const AssetId& assetId, AssetLoadBehavior assetReferenceLoadBehavior) + { + // Look up the asset id in the catalog, and use the result of that instead. + // If assetId is a legacy id, assetInfo.m_assetId will be the canonical id. Otherwise, assetInfo.m_assetID == assetId. + // This is because only canonical ids are stored in m_assets (see below). + // Only do the look up if upgrading is enabled + AZ::Data::AssetInfo assetInfo; + if (GetAssetInfoUpgradingEnabled()) + { + AssetCatalogRequestBus::BroadcastResult(assetInfo, &AssetCatalogRequestBus::Events::GetAssetInfoById, assetId); + } + + // If the catalog is not available, use the original assetId + const AssetId& assetToFind(assetInfo.m_assetId.IsValid() ? assetInfo.m_assetId : assetId); + + AZStd::scoped_lock assetLock(m_assetMutex); + AssetMap::iterator it = m_assets.find(assetToFind); + if (it != m_assets.end()) + { + Asset asset(assetReferenceLoadBehavior); + asset.SetData(it->second); + + return asset; + } + return Asset(assetReferenceLoadBehavior); + } + + AZStd::pair GetEffectiveDeadlineAndPriority( + const AssetHandler& handler, AssetType assetType, const AssetLoadParameters& loadParams) + { + AZStd::chrono::milliseconds deadline; + AZ::IO::IStreamerTypes::Priority priority; + + handler.GetDefaultAssetLoadPriority(assetType, deadline, priority); + + if (loadParams.m_deadline) + { + deadline = loadParams.m_deadline.value(); + } + + if (loadParams.m_priority) + { + priority = loadParams.m_priority.value(); + } + + return make_pair(deadline, priority); + } + + //========================================================================= + // GetAsset + // [6/19/2012] + //========================================================================= + Asset AssetManager::GetAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior, const AssetLoadParameters& loadParams) + { + // If parallel dependent loads are disabled, just try to load the requested asset directly, and let it trigger + // dependent loads as they're encountered. + // Parallel dependent loads are disabled during asset building because there is no guarantee that dependency information + // will be available and complete until after all assets are finished building. + if(!GetParallelDependentLoadingEnabled()) + { + return GetAssetInternal(assetId, assetType, assetReferenceLoadBehavior, loadParams); + } + + // Otherwise, use Asset Containers to load all dependent assets in parallel. + + Asset asset = FindOrCreateAsset(assetId, assetType, assetReferenceLoadBehavior); + + if(!asset || (!loadParams.m_reloadMissingDependencies && asset.IsReady())) + { + // If the asset is already ready, just return it and skip the container + return AZStd::move(asset); + } + + auto container = GetAssetContainer(asset, loadParams); + + AZStd::scoped_lock lock(m_assetContainerMutex); + + m_ownedAssetContainers.insert({ container.get(), container }); + + // Only insert a new entry into m_ownedAssetContainerLookup if one doesn't already exist for this container. + // Because it's a multimap, it is possible to add duplicate entries by mistake. + bool entryExists = false; + auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetId); + for (auto itr = rangeItr.first; itr != rangeItr.second; ++itr) + { + if (itr->second == container.get()) + { + entryExists = true; + break; + } + } + + // Entry for this container doesn't exist yet, so add it. + if (!entryExists) + { + m_ownedAssetContainerLookup.insert({ assetId, container.get() }); + } + + return asset; + } + + Asset AssetManager::GetAssetInternal(const AssetId& assetId, [[maybe_unused]] const AssetType& assetType, + AssetLoadBehavior assetReferenceLoadBehavior, const AssetLoadParameters& loadParams, AssetInfo assetInfo /*= () */, bool signalLoaded /*= false */) + { + AZ_PROFILE_FUNCTION(AzCore); + + AZ_Error("AssetDatabase", assetId.IsValid(), "GetAsset called with invalid asset Id."); + AZ_Error("AssetDatabase", !assetType.IsNull(), "GetAsset called with invalid asset type."); + bool assetMissing = false; + + { + AZ_PROFILE_SCOPE(AzCore, "GetAsset: GetAssetInfo"); + + // Attempt to look up asset info from catalog + // This is so that when assetId is a legacy id, we're operating on the canonical id anyway + if (!assetInfo.m_assetId.IsValid()) { AssetCatalogRequestBus::BroadcastResult(assetInfo, &AssetCatalogRequestBus::Events::GetAssetInfoById, assetId); } - // If the catalog is not available, use the original assetId - const AssetId& assetToFind(assetInfo.m_assetId.IsValid() ? assetInfo.m_assetId : assetId); + // If the asset was found in the catalog, ensure the type infos match + if (assetInfo.m_assetId.IsValid()) + { + AZ_Warning("AssetManager", assetInfo.m_assetType == assetType, + "Requested asset id %s with type %s, but type is actually %s.", + assetId.ToString().c_str(), assetType.ToString().c_str(), + assetInfo.m_assetType.ToString().c_str()); + } + else + { + AZ_Warning("AssetManager", false, "GetAsset called for asset which does not exist in asset catalog and cannot be loaded. Asset may be missing, not processed or moved. AssetId: %s", + assetId.ToString().c_str()); + // If asset not found, use the id and type given. We will create a valid asset, but it will likely get an error + // status below if the asset handler doesn't reroute it to a default asset. + assetInfo.m_assetId = assetId; + assetInfo.m_assetType = assetType; + assetMissing = true; + } + } + + AZ_PROFILE_SCOPE(AzCore, "GetAsset: %s", assetInfo.m_relativePath.c_str()); + AZ_ASSET_NAMED_SCOPE("GetAsset: %s", assetInfo.m_relativePath.c_str()); + + AZStd::shared_ptr dataStream; + AssetStreamInfo loadInfo; + bool triggerAssetErrorNotification = false; + bool wasUnloaded = false; + AssetHandler* handler = nullptr; + AssetData* assetData = nullptr; + Asset asset; // Used to hold a reference while job is dispatched and while outside of the assetMutex lock. + + // Control the scope of the assetMutex lock + { AZStd::scoped_lock assetLock(m_assetMutex); - AssetMap::iterator it = m_assets.find(assetToFind); - if (it != m_assets.end()) + bool isNewEntry = false; + + // check if asset already exists { - Asset asset(assetReferenceLoadBehavior); - asset.SetData(it->second); + AZ_PROFILE_SCOPE(AzCore, "GetAsset: FindAsset"); - return asset; - } - return Asset(assetReferenceLoadBehavior); - } - - AZStd::pair GetEffectiveDeadlineAndPriority( - const AssetHandler& handler, AssetType assetType, const AssetLoadParameters& loadParams) - { - AZStd::chrono::milliseconds deadline; - AZ::IO::IStreamerTypes::Priority priority; - - handler.GetDefaultAssetLoadPriority(assetType, deadline, priority); - - if (loadParams.m_deadline) - { - deadline = loadParams.m_deadline.value(); - } - - if (loadParams.m_priority) - { - priority = loadParams.m_priority.value(); - } - - return make_pair(deadline, priority); - } - - //========================================================================= - // GetAsset - // [6/19/2012] - //========================================================================= - Asset AssetManager::GetAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior, const AssetLoadParameters& loadParams) - { - // If parallel dependent loads are disabled, just try to load the requested asset directly, and let it trigger - // dependent loads as they're encountered. - // Parallel dependent loads are disabled during asset building because there is no guarantee that dependency information - // will be available and complete until after all assets are finished building. - if(!GetParallelDependentLoadingEnabled()) - { - return GetAssetInternal(assetId, assetType, assetReferenceLoadBehavior, loadParams); - } - - // Otherwise, use Asset Containers to load all dependent assets in parallel. - - Asset asset = FindOrCreateAsset(assetId, assetType, assetReferenceLoadBehavior); - - if(!asset || (!loadParams.m_reloadMissingDependencies && asset.IsReady())) - { - // If the asset is already ready, just return it and skip the container - return AZStd::move(asset); - } - - auto container = GetAssetContainer(asset, loadParams); - - AZStd::scoped_lock lock(m_assetContainerMutex); - - m_ownedAssetContainers.insert({ container.get(), container }); - - // Only insert a new entry into m_ownedAssetContainerLookup if one doesn't already exist for this container. - // Because it's a multimap, it is possible to add duplicate entries by mistake. - bool entryExists = false; - auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetId); - for (auto itr = rangeItr.first; itr != rangeItr.second; ++itr) - { - if (itr->second == container.get()) + AssetMap::iterator it = m_assets.find(assetInfo.m_assetId); + if (it != m_assets.end()) { - entryExists = true; - break; - } - } - - // Entry for this container doesn't exist yet, so add it. - if (!entryExists) - { - m_ownedAssetContainerLookup.insert({ assetId, container.get() }); - } - - return asset; - } - - Asset AssetManager::GetAssetInternal(const AssetId& assetId, [[maybe_unused]] const AssetType& assetType, - AssetLoadBehavior assetReferenceLoadBehavior, const AssetLoadParameters& loadParams, AssetInfo assetInfo /*= () */, bool signalLoaded /*= false */) - { - AZ_PROFILE_FUNCTION(AzCore); - - AZ_Error("AssetDatabase", assetId.IsValid(), "GetAsset called with invalid asset Id."); - AZ_Error("AssetDatabase", !assetType.IsNull(), "GetAsset called with invalid asset type."); - bool assetMissing = false; - - { - AZ_PROFILE_SCOPE(AzCore, "GetAsset: GetAssetInfo"); - - // Attempt to look up asset info from catalog - // This is so that when assetId is a legacy id, we're operating on the canonical id anyway - if (!assetInfo.m_assetId.IsValid()) - { - AssetCatalogRequestBus::BroadcastResult(assetInfo, &AssetCatalogRequestBus::Events::GetAssetInfoById, assetId); - } - - // If the asset was found in the catalog, ensure the type infos match - if (assetInfo.m_assetId.IsValid()) - { - AZ_Warning("AssetManager", assetInfo.m_assetType == assetType, - "Requested asset id %s with type %s, but type is actually %s.", - assetId.ToString().c_str(), assetType.ToString().c_str(), - assetInfo.m_assetType.ToString().c_str()); + assetData = it->second; + asset.SetData(assetData); } else { - AZ_Warning("AssetManager", false, "GetAsset called for asset which does not exist in asset catalog and cannot be loaded. Asset may be missing, not processed or moved. AssetId: %s", - assetId.ToString().c_str()); - - // If asset not found, use the id and type given. We will create a valid asset, but it will likely get an error - // status below if the asset handler doesn't reroute it to a default asset. - assetInfo.m_assetId = assetId; - assetInfo.m_assetType = assetType; - assetMissing = true; + isNewEntry = true; } } - AZ_PROFILE_SCOPE(AzCore, "GetAsset: %s", assetInfo.m_relativePath.c_str()); - AZ_ASSET_NAMED_SCOPE("GetAsset: %s", assetInfo.m_relativePath.c_str()); - - AZStd::shared_ptr dataStream; - AssetStreamInfo loadInfo; - bool triggerAssetErrorNotification = false; - bool wasUnloaded = false; - AssetHandler* handler = nullptr; - AssetData* assetData = nullptr; - Asset asset; // Used to hold a reference while job is dispatched and while outside of the assetMutex lock. - - // Control the scope of the assetMutex lock { - AZStd::scoped_lock assetLock(m_assetMutex); - bool isNewEntry = false; + AZ_PROFILE_SCOPE(AzCore, "GetAsset: FindAssetHandler"); - // check if asset already exists + // find the asset type handler + AssetHandlerMap::iterator handlerIt = m_handlers.find(assetInfo.m_assetType); + AZ_Error("AssetDatabase", handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", + assetInfo.m_assetType.ToString().c_str(), assetInfo.m_assetId.ToString().c_str()); + if (handlerIt != m_handlers.end()) { - AZ_PROFILE_SCOPE(AzCore, "GetAsset: FindAsset"); - - AssetMap::iterator it = m_assets.find(assetInfo.m_assetId); - if (it != m_assets.end()) + // Create the asset ptr and insert it into our asset map. + handler = handlerIt->second; + if (isNewEntry) { - assetData = it->second; - asset.SetData(assetData); - } - else - { - isNewEntry = true; - } - } + AZ_PROFILE_SCOPE(AzCore, "GetAsset: CreateAsset"); - { - AZ_PROFILE_SCOPE(AzCore, "GetAsset: FindAssetHandler"); - - // find the asset type handler - AssetHandlerMap::iterator handlerIt = m_handlers.find(assetInfo.m_assetType); - AZ_Error("AssetDatabase", handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", - assetInfo.m_assetType.ToString().c_str(), assetInfo.m_assetId.ToString().c_str()); - if (handlerIt != m_handlers.end()) - { - // Create the asset ptr and insert it into our asset map. - handler = handlerIt->second; - if (isNewEntry) + assetData = handler->CreateAsset(assetInfo.m_assetId, assetInfo.m_assetType); + if (assetData) { - AZ_PROFILE_SCOPE(AzCore, "GetAsset: CreateAsset"); - - assetData = handler->CreateAsset(assetInfo.m_assetId, assetInfo.m_assetType); - if (assetData) - { - assetData->m_assetId = assetInfo.m_assetId; - assetData->m_creationToken = ++m_creationTokenGenerator; - assetData->RegisterWithHandler(handler); - asset.SetData(assetData); - } - else - { - AZ_Error("AssetDatabase", false, "Failed to create asset with (id=%s, type=%s)", - assetInfo.m_assetId.ToString().c_str(), - assetInfo.m_assetType.ToString().c_str()); - } - } - } - } - - if (assetData) - { - if (isNewEntry && assetData->IsRegisterReadonlyAndShareable()) - { - AZ_PROFILE_SCOPE(AzCore, "GetAsset: RegisterAsset"); - m_assets.insert(AZStd::make_pair(assetInfo.m_assetId, assetData)); - } - if (assetData->GetStatus() == AssetData::AssetStatus::NotLoaded) - { - assetData->m_status = AssetData::AssetStatus::Queued; - UpdateDebugStatus(asset); - loadInfo = GetModifiedLoadStreamInfoForAsset(asset, handler); - wasUnloaded = true; - - if (loadInfo.IsValid()) - { - // Create the AssetDataStream instance here so it can claim an asset reference inside the lock (for a total - // count of 2 before starting the load), otherwise the refcount will be 1, and the load could be canceled - // before it is started, which creates state consistency issues. - - dataStream = AZStd::make_shared(handler->GetAssetBufferAllocator()); + assetData->m_assetId = assetInfo.m_assetId; + assetData->m_creationToken = ++m_creationTokenGenerator; + assetData->RegisterWithHandler(handler); + asset.SetData(assetData); } else { - // Asset creation was successful, but asset loading isn't, so trigger the OnAssetError notification - triggerAssetErrorNotification = true; + AZ_Error("AssetDatabase", false, "Failed to create asset with (id=%s, type=%s)", + assetInfo.m_assetId.ToString().c_str(), + assetInfo.m_assetType.ToString().c_str()); } } } } - if (!assetInfo.m_relativePath.empty()) + if (assetData) { - asset.m_assetHint = assetInfo.m_relativePath; - } - - asset.SetAutoLoadBehavior(assetReferenceLoadBehavior); - - // We delay queueing the async file I/O until we release m_assetMutex - if (dataStream) - { - AZ_Assert(loadInfo.IsValid(), "Expected valid stream info when dataStream is valid."); - constexpr bool isReload = false; - QueueAsyncStreamLoad(asset, dataStream, loadInfo, isReload, - handler, loadParams, signalLoaded); - } - else - { - AZ_Assert(!loadInfo.IsValid(), "Expected invalid stream info when dataStream is invalid."); - - if(!wasUnloaded && assetData && assetData->GetStatus() == AssetData::AssetStatus::Queued) + if (isNewEntry && assetData->IsRegisterReadonlyAndShareable()) { - auto&& [deadline, priority] = GetEffectiveDeadlineAndPriority(*handler, assetData->GetType(), loadParams); - - RescheduleStreamerRequest(assetData->GetId(), deadline, priority); + AZ_PROFILE_SCOPE(AzCore, "GetAsset: RegisterAsset"); + m_assets.insert(AZStd::make_pair(assetInfo.m_assetId, assetData)); } - - if (triggerAssetErrorNotification) + if (assetData->GetStatus() == AssetData::AssetStatus::NotLoaded) { - // If the asset was missing from the catalog, we already printed an error, so we can skip printing this one. - if (!assetMissing) + assetData->m_status = AssetData::AssetStatus::Queued; + UpdateDebugStatus(asset); + loadInfo = GetModifiedLoadStreamInfoForAsset(asset, handler); + wasUnloaded = true; + + if (loadInfo.IsValid()) { - AZ_Error("AssetDatabase", false, "Failed to retrieve required information for asset %s (%s)", - assetInfo.m_assetId.ToString().c_str(), - assetInfo.m_relativePath.empty() ? "" : assetInfo.m_relativePath.c_str()); - } + // Create the AssetDataStream instance here so it can claim an asset reference inside the lock (for a total + // count of 2 before starting the load), otherwise the refcount will be 1, and the load could be canceled + // before it is started, which creates state consistency issues. - PostLoad(asset, false, false, handler); - } - } - - return asset; - } - - void AssetManager::UpdateDebugStatus(const AZ::Data::Asset& asset) - { - if(!m_debugAssetEvents) - { - m_debugAssetEvents = AZ::Interface::Get(); - } - - if(m_debugAssetEvents) - { - m_debugAssetEvents->AssetStatusUpdate(asset.GetId(), asset.GetStatus()); - } - } - - Asset AssetManager::FindOrCreateAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior) - { - AZStd::scoped_lock asset_lock(m_assetMutex); - - Asset asset = FindAsset(assetId, assetReferenceLoadBehavior); - - if (!asset) - { - asset = CreateAsset(assetId, assetType, assetReferenceLoadBehavior); - } - - return asset; - } - - //========================================================================= - // CreateAsset - // [8/31/2012] - //========================================================================= - Asset AssetManager::CreateAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior) - { - AZStd::scoped_lock asset_lock(m_assetMutex); - - // check if asset already exist - AssetMap::iterator it = m_assets.find(assetId); - if (it == m_assets.end()) - { - // find the asset type handler - AssetHandlerMap::iterator handlerIt = m_handlers.find(assetType); - AZ_Error("AssetDatabase", handlerIt != m_handlers.end(), "No handler was registered for this asset (id=%s, type=%s)!", assetId.ToString().c_str(), assetType.ToString().c_str()); - if (handlerIt != m_handlers.end()) - { - // Create the asset ptr - AssetHandler* handler = handlerIt->second; - auto assetData = handler->CreateAsset(assetId, assetType); - AZ_Error("AssetDatabase", assetData, "Failed to create asset with (id=%s, type=%s)", assetId.ToString().c_str(), assetType.ToString().c_str()); - if (assetData) - { - assetData->m_assetId = assetId; - assetData->m_creationToken = ++m_creationTokenGenerator; - assetData->RegisterWithHandler(handler); - if (assetData->IsRegisterReadonlyAndShareable()) - { - m_assets.insert(AZStd::make_pair(assetId, assetData)); - } - - Asset asset(assetReferenceLoadBehavior); - asset.SetData(assetData); - - return asset; - } - } - } - else - { - AZ_Error("AssetDatabase", false, "Asset (id=%s, type=%s) already exists in the database! Asset not created!", assetId.ToString().c_str(), assetType.ToString().c_str()); - } - return Asset(assetReferenceLoadBehavior); - } - - //========================================================================= - // ReleaseAsset - //========================================================================= - void AssetManager::ReleaseAsset(AssetData* asset, AssetId assetId, AssetType assetType, bool removeAssetFromHash, int creationToken) - { - AZ_Assert(asset, "Cannot release NULL AssetPtr!"); - - if(m_suspendAssetRelease) - { - return; - } - - bool wasInAssetsHash = false; // We do support assets that are not registered in the asset manager (with the same ID too). - bool destroyAsset = false; - - if (removeAssetFromHash) - { - AZStd::scoped_lock asset_lock(m_assetMutex); - AssetMap::iterator it = m_assets.find(assetId); - // need to check the count again in here in case - // someone was trying to get the asset on another thread - // Set it to -1 so only this thread will attempt to clean up the cache and delete the asset - int expectedRefCount = 0; - // if the assetId is not in the map or if the identifierId - // do not match it implies that the asset has been already destroyed. - // if the usecount is non zero it implies that we cannot destroy this asset. - if (it != m_assets.end() && it->second->m_creationToken == creationToken && it->second->m_weakUseCount.compare_exchange_strong(expectedRefCount, -1)) - { - wasInAssetsHash = true; - m_assets.erase(it); - destroyAsset = true; - } - } - else - { - // if an asset is not shareable, it implies that that asset is not in the map - // and therefore once its ref count goes to zero it cannot go back up again and therefore we can safely destroy it - destroyAsset = true; - } - - // We have to separate the code which was removing the asset from the m_asset map while being locked, but then actually destroy the asset - // while the lock is not held since destroying the asset while holding the lock can cause a deadlock. - if (destroyAsset) - { - if(m_debugAssetEvents) - { - m_debugAssetEvents->ReleaseAsset(assetId); - } - - // find the asset type handler - AssetHandlerMap::iterator handlerIt = m_handlers.find(assetType); - if (handlerIt != m_handlers.end()) - { - AssetHandler* handler = handlerIt->second; - if (asset) - { - handler->DestroyAsset(asset); - - if (wasInAssetsHash) - { - AssetBus::QueueEvent(assetId, &AssetBus::Events::OnAssetUnloaded, assetId, assetType); - } - } - } - else - { - AZ_Assert(false, "No handler was registered for asset of type %s but it was still in the AssetManager as %s", assetType.ToString().c_str(), asset->GetId().ToString().c_str()); - } - } - } - - void AssetManager::OnAssetUnused(AssetData* asset) - { - // If we're currently suspending asset releases, don't get rid of the asset containers either. - if (m_suspendAssetRelease) - { - return; - } - - ReleaseAssetContainersForAsset(asset); - } - - void AssetManager::ReleaseAssetContainersForAsset(AssetData* asset) - { - // Release any containers that were loading this asset - AZStd::scoped_lock lock(m_assetContainerMutex); - - AssetId assetId = asset->GetId(); - - auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetId); - - for (auto itr = rangeItr.first; itr != rangeItr.second;) - { - AZ_Assert(itr->second->GetContainerAssetId() == assetId, - "Asset container is incorrectly associated with the asset being destroyed."); - itr->second->ClearRootAsset(); - - // Only remove owned asset containers if they aren't currently loading. - // If they *are* currently loading, removing them could cause dependent asset loads that were triggered to - // remain in a perpetual loading state. Instead, leave the containers for now, they will get removed during - // the OnAssetContainerReady callback. - if (!itr->second->IsLoading()) - { - m_ownedAssetContainers.erase(itr->second); - itr = m_ownedAssetContainerLookup.erase(itr); - } - else - { - ++itr; - } - } - } - - //========================================================================= - // SaveAsset - // [9/13/2012] - //========================================================================= - void AssetManager::SaveAsset(const Asset& asset) - { - AssetHandler* handler; - { - // find the asset type handler - AssetHandlerMap::iterator handlerIt = m_handlers.find(asset.GetType()); - AZ_Assert(handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", asset.GetType().ToString().c_str(), asset.GetId().ToString().c_str()); - handler = handlerIt->second; - } - - // start the data saving - SaveAssetJob* saveJob = aznew SaveAssetJob(JobContext::GetGlobalContext(), this, asset, handler); - saveJob->Start(); - } - - //========================================================================= - // ReloadAsset - //========================================================================= - void AssetManager::ReloadAsset(const AssetId& assetId, AssetLoadBehavior assetReferenceLoadBehavior, bool isAutoReload) - { - AZStd::scoped_lock assetLock(m_assetMutex); - auto assetIter = m_assets.find(assetId); - - if (assetIter == m_assets.end() || assetIter->second->IsLoading()) - { - // Only existing assets can be reloaded. - return; - } - - auto reloadIter = m_reloads.find(assetId); - if (reloadIter != m_reloads.end()) - { - auto curStatus = reloadIter->second.GetData()->GetStatus(); - // We don't need another reload if we're in "Queued" state because that reload has not actually begun yet. - // If it is in Loading state we want to pass by and allow the new assetData to be created and start the new reload - // As the current load could already be stale - if (curStatus == AssetData::AssetStatus::Queued) - { - return; - } - else if (curStatus == AssetData::AssetStatus::Loading || curStatus == AssetData::AssetStatus::StreamReady) - { - // Don't flood the tick bus - this value will be checked when the asset load completes - reloadIter->second->SetRequeue(true); - return; - } - } - - AssetData* newAssetData = nullptr; - AssetHandler* handler = nullptr; - - bool preventAutoReload = isAutoReload && assetIter->second && !assetIter->second->HandleAutoReload(); - - // when Asset's constructor is called (the one that takes an AssetData), it updates the AssetID - // of the Asset to be the real latest canonical assetId of the asset, so we cache that here instead of have it happen - // implicitly and repeatedly for anything we call. - Asset currentAsset(assetIter->second, AZ::Data::AssetLoadBehavior::Default); - - if (!assetIter->second->IsRegisterReadonlyAndShareable() && !preventAutoReload) - { - // Reloading an "instance asset" is basically a no-op. - // We'll simply notify users to reload the asset. - AssetBus::QueueFunction(&AssetManager::NotifyAssetReloaded, this, currentAsset); - return; - } - else - { - AssetBus::QueueFunction(&AssetManager::NotifyAssetPreReload, this, currentAsset); - } - - // Current AssetData has requested not to be auto reloaded - if (preventAutoReload) - { - return; - } - - // Resolve the asset handler and allocate new data for the reload. - { - AssetHandlerMap::iterator handlerIt = m_handlers.find(currentAsset.GetType()); - AZ_Assert(handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", - currentAsset.GetType().ToString().c_str(), currentAsset.GetId().ToString().c_str()); - handler = handlerIt->second; - - newAssetData = handler->CreateAsset(currentAsset.GetId(), currentAsset.GetType()); - if (newAssetData) - { - newAssetData->m_assetId = currentAsset.GetId(); - newAssetData->RegisterWithHandler(handler); - } - } - - if (newAssetData) - { - // For reloaded assets, we need to hold an internal reference to ensure the data - // isn't immediately destroyed. Since reloads are not a shipping feature, we'll - // hold this reference indefinitely, but we'll only hold the most recent one for - // a given asset Id. - - newAssetData->m_status = AssetData::AssetStatus::Queued; - Asset newAsset(newAssetData, assetReferenceLoadBehavior); - - m_reloads[newAsset.GetId()] = newAsset; - - UpdateDebugStatus(newAsset); - - AZStd::shared_ptr dataStream; - AssetStreamInfo loadInfo = GetModifiedLoadStreamInfoForAsset(newAsset, handler); - constexpr bool isReload = true; - if (loadInfo.IsValid()) - { - // Create the AssetDataStream instance here so it can claim an asset reference inside the lock (for a total - // count of 2 before starting the load), otherwise the refcount will be 1, and the load could be canceled - // before it is started, which creates state consistency issues. - - dataStream = AZStd::make_shared(handler->GetAssetBufferAllocator()); - if (dataStream) - { - // Currently there isn't a clear use case for needing to adjust priority for reloads so the default load priority is used - constexpr bool signalLoaded = false; // this is a reload, so don't signal dependent-asset loads - QueueAsyncStreamLoad(newAsset, dataStream, loadInfo, isReload, - handler, {}, signalLoaded); + dataStream = AZStd::make_shared(handler->GetAssetBufferAllocator()); } else { - AZ_Assert(false, "Failed to create dataStream to reload asset %s (%s)", - newAsset.GetId().ToString().c_str(), - newAsset.GetHint().c_str()); + // Asset creation was successful, but asset loading isn't, so trigger the OnAssetError notification + triggerAssetErrorNotification = true; } } - else + } + } + + if (!assetInfo.m_relativePath.empty()) + { + asset.m_assetHint = assetInfo.m_relativePath; + } + + asset.SetAutoLoadBehavior(assetReferenceLoadBehavior); + + // We delay queueing the async file I/O until we release m_assetMutex + if (dataStream) + { + AZ_Assert(loadInfo.IsValid(), "Expected valid stream info when dataStream is valid."); + constexpr bool isReload = false; + QueueAsyncStreamLoad(asset, dataStream, loadInfo, isReload, + handler, loadParams, signalLoaded); + } + else + { + AZ_Assert(!loadInfo.IsValid(), "Expected invalid stream info when dataStream is invalid."); + + if(!wasUnloaded && assetData && assetData->GetStatus() == AssetData::AssetStatus::Queued) + { + auto&& [deadline, priority] = GetEffectiveDeadlineAndPriority(*handler, assetData->GetType(), loadParams); + + RescheduleStreamerRequest(assetData->GetId(), deadline, priority); + } + + if (triggerAssetErrorNotification) + { + // If the asset was missing from the catalog, we already printed an error, so we can skip printing this one. + if (!assetMissing) { - // Asset creation was successful, but asset loading isn't, so trigger the OnAssetError notification AZ_Error("AssetDatabase", false, "Failed to retrieve required information for asset %s (%s)", - newAsset.GetId().ToString().c_str(), - newAsset.GetHint().c_str()); - - constexpr bool loadSucceeded = false; - AssetManager::Instance().PostLoad(newAsset, loadSucceeded, isReload, handler); + assetInfo.m_assetId.ToString().c_str(), + assetInfo.m_relativePath.empty() ? "" : assetInfo.m_relativePath.c_str()); } + PostLoad(asset, false, false, handler); } } - //========================================================================= - // ReloadAssetFromData - //========================================================================= - void AssetManager::ReloadAssetFromData(const Asset& asset) + return asset; + } + + void AssetManager::UpdateDebugStatus(const AZ::Data::Asset& asset) + { + if(!m_debugAssetEvents) { - bool shouldAssignAssetData = false; - - { - AZ_Assert(asset.Get(), "Asset data for reload is missing."); - AZStd::scoped_lock assetLock(m_assetMutex); - AZ_Assert( - m_assets.find(asset.GetId()) != m_assets.end(), - "Unable to reload asset %s because it's not in the AssetManager's asset list.", asset.ToString().c_str()); - AZ_Assert( - m_assets.find(asset.GetId()) == m_assets.end() || - asset->RTTI_GetType() == m_assets.find(asset.GetId())->second->RTTI_GetType(), - "New and old data types are mismatched!"); - - auto found = m_assets.find(asset.GetId()); - if ((found == m_assets.end()) || (asset->RTTI_GetType() != found->second->RTTI_GetType())) - { - return; // this will just lead to crashes down the line and the above asserts cover this. - } - - AssetData* newData = asset.Get(); - - if (found->second != newData) - { - // Notify users that we are about to change asset - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetPreReload, asset); - - // Resolve the asset handler and account for the new asset instance. - { - [[maybe_unused]] AssetHandlerMap::iterator handlerIt = m_handlers.find(newData->GetType()); - AZ_Assert( - handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", - newData->GetType().ToString().c_str(), newData->GetId().ToString().c_str()); - } - - shouldAssignAssetData = true; - } - } - - // We specifically perform this outside of the m_assetMutex lock so that the lock isn't held at the point that - // OnAssetReload is triggered inside of AssignAssetData. Otherwise, we open up a high potential for deadlocks. - if (shouldAssignAssetData) - { - AssignAssetData(asset); - } + m_debugAssetEvents = AZ::Interface::Get(); } - //========================================================================= - // GetHandler - //========================================================================= - AssetHandler* AssetManager::GetHandler(const AssetType& assetType) + if(m_debugAssetEvents) { - auto handlerEntry = m_handlers.find(assetType); - if (handlerEntry != m_handlers.end()) - { - return handlerEntry->second; - } - return nullptr; + m_debugAssetEvents->AssetStatusUpdate(asset.GetId(), asset.GetStatus()); + } + } + + Asset AssetManager::FindOrCreateAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior) + { + AZStd::scoped_lock asset_lock(m_assetMutex); + + Asset asset = FindAsset(assetId, assetReferenceLoadBehavior); + + if (!asset) + { + asset = CreateAsset(assetId, assetType, assetReferenceLoadBehavior); } - //========================================================================= - // AssignAssetData - //========================================================================= - void AssetManager::AssignAssetData(const Asset& asset) + return asset; + } + + //========================================================================= + // CreateAsset + // [8/31/2012] + //========================================================================= + Asset AssetManager::CreateAsset(const AssetId& assetId, const AssetType& assetType, AssetLoadBehavior assetReferenceLoadBehavior) + { + AZStd::scoped_lock asset_lock(m_assetMutex); + + // check if asset already exist + AssetMap::iterator it = m_assets.find(assetId); + if (it == m_assets.end()) { - AZ_Assert(asset.Get(), "Reloaded data is missing!"); - - const AssetId& assetId = asset.GetId(); - - asset->m_status = AssetData::AssetStatus::Ready; - UpdateDebugStatus(asset); - - if (asset->IsRegisterReadonlyAndShareable()) + // find the asset type handler + AssetHandlerMap::iterator handlerIt = m_handlers.find(assetType); + AZ_Error("AssetDatabase", handlerIt != m_handlers.end(), "No handler was registered for this asset (id=%s, type=%s)!", assetId.ToString().c_str(), assetType.ToString().c_str()); + if (handlerIt != m_handlers.end()) { - bool requeue{ false }; + // Create the asset ptr + AssetHandler* handler = handlerIt->second; + auto assetData = handler->CreateAsset(assetId, assetType); + AZ_Error("AssetDatabase", assetData, "Failed to create asset with (id=%s, type=%s)", assetId.ToString().c_str(), assetType.ToString().c_str()); + if (assetData) { - AZStd::scoped_lock assetLock(m_assetMutex); - auto found = m_assets.find(assetId); - AZ_Assert(found == m_assets.end() || asset.Get()->RTTI_GetType() == found->second->RTTI_GetType(), - "New and old data types are mismatched!"); - - // if we are here it implies that we have two assets with the same asset id, and we are - // trying to replace the old asset with the new asset which was not created using the asset manager system. - // In this scenario if any other system have cached the old asset then the asset wont be destroyed - // because of creation token mismatch when it's ref count finally goes to zero. Since the old asset is not shareable anymore - // manually setting the creationToken to default creation token will ensure that the asset is destroyed correctly. - asset.m_assetData->m_creationToken = ++m_creationTokenGenerator; - if (found != m_assets.end()) + assetData->m_assetId = assetId; + assetData->m_creationToken = ++m_creationTokenGenerator; + assetData->RegisterWithHandler(handler); + if (assetData->IsRegisterReadonlyAndShareable()) { - found->second->m_creationToken = AZ::Data::s_defaultCreationToken; + m_assets.insert(AZStd::make_pair(assetId, assetData)); } - // Held references to old data are retained, but replace the entry in the DB for future requests. - // Fire an OnAssetReloaded message so listeners can react to the new data. - m_assets[assetId] = asset.Get(); + Asset asset(assetReferenceLoadBehavior); + asset.SetData(assetData); - // Release the reload reference. - auto reloadInfo = m_reloads.find(assetId); - if (reloadInfo != m_reloads.end()) - { - requeue = reloadInfo->second->GetRequeue(); - m_reloads.erase(reloadInfo); - } + return asset; } - // Call reloaded before we can call ReloadAsset below to preserve order - AssetBus::Event(assetId, &AssetBus::Events::OnAssetReloaded, asset); - // Release the lock before we call reload - if (requeue) + } + } + else + { + AZ_Error("AssetDatabase", false, "Asset (id=%s, type=%s) already exists in the database! Asset not created!", assetId.ToString().c_str(), assetType.ToString().c_str()); + } + return Asset(assetReferenceLoadBehavior); + } + + //========================================================================= + // ReleaseAsset + //========================================================================= + void AssetManager::ReleaseAsset(AssetData* asset, AssetId assetId, AssetType assetType, bool removeAssetFromHash, int creationToken) + { + AZ_Assert(asset, "Cannot release NULL AssetPtr!"); + + if(m_suspendAssetRelease) + { + return; + } + + bool wasInAssetsHash = false; // We do support assets that are not registered in the asset manager (with the same ID too). + bool destroyAsset = false; + + if (removeAssetFromHash) + { + AZStd::scoped_lock asset_lock(m_assetMutex); + AssetMap::iterator it = m_assets.find(assetId); + // need to check the count again in here in case + // someone was trying to get the asset on another thread + // Set it to -1 so only this thread will attempt to clean up the cache and delete the asset + int expectedRefCount = 0; + // if the assetId is not in the map or if the identifierId + // do not match it implies that the asset has been already destroyed. + // if the usecount is non zero it implies that we cannot destroy this asset. + if (it != m_assets.end() && it->second->m_creationToken == creationToken && it->second->m_weakUseCount.compare_exchange_strong(expectedRefCount, -1)) + { + wasInAssetsHash = true; + m_assets.erase(it); + destroyAsset = true; + } + } + else + { + // if an asset is not shareable, it implies that that asset is not in the map + // and therefore once its ref count goes to zero it cannot go back up again and therefore we can safely destroy it + destroyAsset = true; + } + + // We have to separate the code which was removing the asset from the m_asset map while being locked, but then actually destroy the asset + // while the lock is not held since destroying the asset while holding the lock can cause a deadlock. + if (destroyAsset) + { + if(m_debugAssetEvents) + { + m_debugAssetEvents->ReleaseAsset(assetId); + } + + // find the asset type handler + AssetHandlerMap::iterator handlerIt = m_handlers.find(assetType); + if (handlerIt != m_handlers.end()) + { + AssetHandler* handler = handlerIt->second; + if (asset) { - ReloadAsset(assetId, asset.GetAutoLoadBehavior()); + handler->DestroyAsset(asset); + + if (wasInAssetsHash) + { + AssetBus::QueueEvent(assetId, &AssetBus::Events::OnAssetUnloaded, assetId, assetType); + } } } else { - AssetBus::Event(assetId, &AssetBus::Events::OnAssetReloaded, asset); + AZ_Assert(false, "No handler was registered for asset of type %s but it was still in the AssetManager as %s", assetType.ToString().c_str(), asset->GetId().ToString().c_str()); + } + } + } + + void AssetManager::OnAssetUnused(AssetData* asset) + { + // If we're currently suspending asset releases, don't get rid of the asset containers either. + if (m_suspendAssetRelease) + { + return; + } + + ReleaseAssetContainersForAsset(asset); + } + + void AssetManager::ReleaseAssetContainersForAsset(AssetData* asset) + { + // Release any containers that were loading this asset + AZStd::scoped_lock lock(m_assetContainerMutex); + + AssetId assetId = asset->GetId(); + + auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetId); + + for (auto itr = rangeItr.first; itr != rangeItr.second;) + { + AZ_Assert(itr->second->GetContainerAssetId() == assetId, + "Asset container is incorrectly associated with the asset being destroyed."); + itr->second->ClearRootAsset(); + + // Only remove owned asset containers if they aren't currently loading. + // If they *are* currently loading, removing them could cause dependent asset loads that were triggered to + // remain in a perpetual loading state. Instead, leave the containers for now, they will get removed during + // the OnAssetContainerReady callback. + if (!itr->second->IsLoading()) + { + m_ownedAssetContainers.erase(itr->second); + itr = m_ownedAssetContainerLookup.erase(itr); + } + else + { + ++itr; + } + } + } + + //========================================================================= + // SaveAsset + // [9/13/2012] + //========================================================================= + void AssetManager::SaveAsset(const Asset& asset) + { + AssetHandler* handler; + { + // find the asset type handler + AssetHandlerMap::iterator handlerIt = m_handlers.find(asset.GetType()); + AZ_Assert(handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", asset.GetType().ToString().c_str(), asset.GetId().ToString().c_str()); + handler = handlerIt->second; + } + + // start the data saving + SaveAssetJob* saveJob = aznew SaveAssetJob(JobContext::GetGlobalContext(), this, asset, handler); + saveJob->Start(); + } + + //========================================================================= + // ReloadAsset + //========================================================================= + void AssetManager::ReloadAsset(const AssetId& assetId, AssetLoadBehavior assetReferenceLoadBehavior, bool isAutoReload) + { + AZStd::scoped_lock assetLock(m_assetMutex); + auto assetIter = m_assets.find(assetId); + + if (assetIter == m_assets.end() || assetIter->second->IsLoading()) + { + // Only existing assets can be reloaded. + return; + } + + auto reloadIter = m_reloads.find(assetId); + if (reloadIter != m_reloads.end()) + { + auto curStatus = reloadIter->second.GetData()->GetStatus(); + // We don't need another reload if we're in "Queued" state because that reload has not actually begun yet. + // If it is in Loading state we want to pass by and allow the new assetData to be created and start the new reload + // As the current load could already be stale + if (curStatus == AssetData::AssetStatus::Queued) + { + return; + } + else if (curStatus == AssetData::AssetStatus::Loading || curStatus == AssetData::AssetStatus::StreamReady) + { + // Don't flood the tick bus - this value will be checked when the asset load completes + reloadIter->second->SetRequeue(true); + return; } } - //========================================================================= - // GetModifiedLoadStreamInfoForAsset - //========================================================================= - AssetStreamInfo AssetManager::GetModifiedLoadStreamInfoForAsset(const Asset& asset, AssetHandler* handler) + AssetData* newAssetData = nullptr; + AssetHandler* handler = nullptr; + + bool preventAutoReload = isAutoReload && assetIter->second && !assetIter->second->HandleAutoReload(); + + // when Asset's constructor is called (the one that takes an AssetData), it updates the AssetID + // of the Asset to be the real latest canonical assetId of the asset, so we cache that here instead of have it happen + // implicitly and repeatedly for anything we call. + Asset currentAsset(assetIter->second, AZ::Data::AssetLoadBehavior::Default); + + if (!assetIter->second->IsRegisterReadonlyAndShareable() && !preventAutoReload) { - AssetStreamInfo loadInfo = GetLoadStreamInfoForAsset(asset.GetId(), asset.GetType()); - if (!loadInfo.IsValid()) - { - // opportunity for handler to do default substitution: - AZ::Data::AssetId fallbackId = handler->AssetMissingInCatalog(asset); - if (fallbackId.IsValid()) - { - loadInfo = GetLoadStreamInfoForAsset(fallbackId, asset.GetType()); - } - } - - // Give the handler an opportunity to modify any of the load info before creating the dataStream. - handler->GetCustomAssetStreamInfoForLoad(loadInfo); - - return loadInfo; + // Reloading an "instance asset" is basically a no-op. + // We'll simply notify users to reload the asset. + AssetBus::QueueFunction(&AssetManager::NotifyAssetReloaded, this, currentAsset); + return; + } + else + { + AssetBus::QueueFunction(&AssetManager::NotifyAssetPreReload, this, currentAsset); } - //========================================================================= - // QueueAsyncStreamLoad - //========================================================================= - void AssetManager::QueueAsyncStreamLoad(Asset asset, AZStd::shared_ptr dataStream, - const AZ::Data::AssetStreamInfo& streamInfo, bool isReload, - AssetHandler* handler, const AssetLoadParameters& loadParams, bool signalLoaded) + // Current AssetData has requested not to be auto reloaded + if (preventAutoReload) { - AZ_PROFILE_FUNCTION(AzCore); + return; + } - // Set up the callback that will process the asset data once the raw file load is finished. - // The callback is declared as mutable so that we can clear weakAsset within the callback. The refcount in weakAsset - // can trigger an AssetManager::ReleaseAsset call. If this occurs during lambda cleanup, it could happen at any time - // on the file streamer thread as streamer requests get recycled, including during (or after) AssetManager shutdown. - // By controlling when the refcount is changed, we can ensure that it occurs while the AssetManager is still active. - auto assetDataStreamCallback = [this, loadParams, handler, dataStream, signalLoaded, isReload, - weakAsset = AssetInternal::WeakAsset(asset)] - (AZ::IO::IStreamerTypes::RequestStatus status) mutable + // Resolve the asset handler and allocate new data for the reload. + { + AssetHandlerMap::iterator handlerIt = m_handlers.find(currentAsset.GetType()); + AZ_Assert(handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", + currentAsset.GetType().ToString().c_str(), currentAsset.GetId().ToString().c_str()); + handler = handlerIt->second; + + newAssetData = handler->CreateAsset(currentAsset.GetId(), currentAsset.GetType()); + if (newAssetData) { - auto assetId = weakAsset.GetId(); + newAssetData->m_assetId = currentAsset.GetId(); + newAssetData->RegisterWithHandler(handler); + } + } - Asset loadingAsset = weakAsset.GetStrongReference(); + if (newAssetData) + { + // For reloaded assets, we need to hold an internal reference to ensure the data + // isn't immediately destroyed. Since reloads are not a shipping feature, we'll + // hold this reference indefinitely, but we'll only hold the most recent one for + // a given asset Id. - if (loadingAsset) + newAssetData->m_status = AssetData::AssetStatus::Queued; + Asset newAsset(newAssetData, assetReferenceLoadBehavior); + + m_reloads[newAsset.GetId()] = newAsset; + + UpdateDebugStatus(newAsset); + + AZStd::shared_ptr dataStream; + AssetStreamInfo loadInfo = GetModifiedLoadStreamInfoForAsset(newAsset, handler); + constexpr bool isReload = true; + if (loadInfo.IsValid()) + { + // Create the AssetDataStream instance here so it can claim an asset reference inside the lock (for a total + // count of 2 before starting the load), otherwise the refcount will be 1, and the load could be canceled + // before it is started, which creates state consistency issues. + + dataStream = AZStd::make_shared(handler->GetAssetBufferAllocator()); + if (dataStream) { - AZ_PROFILE_SCOPE(AzCore, "AZ::Data::LoadAssetStreamerCallback %s", - loadingAsset.GetHint().c_str()); - { - AZStd::scoped_lock assetLock(m_assetMutex); - AssetData* data = loadingAsset.Get(); - if (data->GetStatus() != AssetData::AssetStatus::Queued) - { - AZ_Warning("AssetManager", false, "Asset %s no longer in Queued state, abandoning load", loadingAsset.GetId().ToString().c_str()); - return; - } - data->m_status = AssetData::AssetStatus::StreamReady; - } - - // The callback from AZ Streamer blocks the streaming thread until this function completes. To minimize the overhead, - // do the majority of the work in a separate job. - auto loadJob = aznew LoadAssetJob(this, loadingAsset, - dataStream, isReload, status, handler, loadParams, signalLoaded); - - bool jobQueued = false; - - // If there's already an active blocking request waiting for this load to complete, let that thread handle - // the load itself instead of consuming a second thread. - { - AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); - auto range = m_activeBlockingRequests.equal_range(assetId); - for(auto blockingRequest = range.first; blockingRequest != range.second; ++blockingRequest) - { - if(blockingRequest->second->QueueAssetLoadJob(loadJob)) - { - jobQueued = true; - break; - } - } - } - - if (!jobQueued) - { - loadJob->Start(); - } + // Currently there isn't a clear use case for needing to adjust priority for reloads so the default load priority is used + constexpr bool signalLoaded = false; // this is a reload, so don't signal dependent-asset loads + QueueAsyncStreamLoad(newAsset, dataStream, loadInfo, isReload, + handler, {}, signalLoaded); } else { - BlockingAssetLoadBus::Event(assetId, &BlockingAssetLoadBus::Events::OnLoadCanceled, assetId); - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetCanceled, assetId); + AZ_Assert(false, "Failed to create dataStream to reload asset %s (%s)", + newAsset.GetId().ToString().c_str(), + newAsset.GetHint().c_str()); + } + } + else + { + // Asset creation was successful, but asset loading isn't, so trigger the OnAssetError notification + AZ_Error("AssetDatabase", false, "Failed to retrieve required information for asset %s (%s)", + newAsset.GetId().ToString().c_str(), + newAsset.GetHint().c_str()); + + constexpr bool loadSucceeded = false; + AssetManager::Instance().PostLoad(newAsset, loadSucceeded, isReload, handler); + } + + } + } + + //========================================================================= + // ReloadAssetFromData + //========================================================================= + void AssetManager::ReloadAssetFromData(const Asset& asset) + { + bool shouldAssignAssetData = false; + + { + AZ_Assert(asset.Get(), "Asset data for reload is missing."); + AZStd::scoped_lock assetLock(m_assetMutex); + AZ_Assert( + m_assets.find(asset.GetId()) != m_assets.end(), + "Unable to reload asset %s because it's not in the AssetManager's asset list.", asset.ToString().c_str()); + AZ_Assert( + m_assets.find(asset.GetId()) == m_assets.end() || + asset->RTTI_GetType() == m_assets.find(asset.GetId())->second->RTTI_GetType(), + "New and old data types are mismatched!"); + + auto found = m_assets.find(asset.GetId()); + if ((found == m_assets.end()) || (asset->RTTI_GetType() != found->second->RTTI_GetType())) + { + return; // this will just lead to crashes down the line and the above asserts cover this. + } + + AssetData* newData = asset.Get(); + + if (found->second != newData) + { + // Notify users that we are about to change asset + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetPreReload, asset); + + // Resolve the asset handler and account for the new asset instance. + { + [[maybe_unused]] AssetHandlerMap::iterator handlerIt = m_handlers.find(newData->GetType()); + AZ_Assert( + handlerIt != m_handlers.end(), "No handler was registered for this asset [type:%s id:%s]!", + newData->GetType().ToString().c_str(), newData->GetId().ToString().c_str()); } - // *After* the loadJob has been created, clear our asset references and remove the active streamer requests. - // This needs to happen after the loadJob creation to ensure that on AssetManager shutdown, there are no brief - // windows in which requests and/or jobs are still active after we've removed our tracking of the requests and jobs. - - // Also, if the asset references don't get cleared until after the callback completes, or at some indeterminate later - // time when the File Streamer cleans up the file requests (for the weakAsset lambda parameter), then it's possible that - // they will trigger a ReleaseAsset call sometime after the AssetManager has begun to shut down, which can lead to - // race conditions. - - weakAsset = {}; - loadingAsset.Reset(); - RemoveActiveStreamerRequest(assetId); - }; - - auto&& [deadline, priority] = GetEffectiveDeadlineAndPriority(*handler, asset.GetType(), loadParams); - - // Track the load request and queue the asset data stream load. - AddActiveStreamerRequest(asset.GetId(), dataStream); - dataStream->Open( - streamInfo.m_streamName, - streamInfo.m_dataOffset, - streamInfo.m_dataLen, - deadline, priority, assetDataStreamCallback); + shouldAssignAssetData = true; + } } - //========================================================================= - // NotifyAssetReady - //========================================================================= - void AssetManager::NotifyAssetReady(Asset asset) - { - AssetData* data = asset.Get(); - AZ_Assert(data, "NotifyAssetReady: asset is missing info!"); - data->m_status = AssetData::AssetStatus::Ready; - - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetReady, asset); - } - - //========================================================================= - // NotifyAssetPreReload - //========================================================================= - void AssetManager::NotifyAssetPreReload(Asset asset) - { - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetPreReload, asset); - } - - //========================================================================= - // NotifyAssetReloaded - //========================================================================= - void AssetManager::NotifyAssetReloaded(Asset asset) + // We specifically perform this outside of the m_assetMutex lock so that the lock isn't held at the point that + // OnAssetReload is triggered inside of AssignAssetData. Otherwise, we open up a high potential for deadlocks. + if (shouldAssignAssetData) { AssignAssetData(asset); } + } - //========================================================================= - // NotifyAssetReloaded - //========================================================================= - void AssetManager::NotifyAssetReloadError(Asset asset) + //========================================================================= + // GetHandler + //========================================================================= + AssetHandler* AssetManager::GetHandler(const AssetType& assetType) + { + auto handlerEntry = m_handlers.find(assetType); + if (handlerEntry != m_handlers.end()) { - // Failed reloads have no side effects. Just notify observers (error reporting, etc). + return handlerEntry->second; + } + return nullptr; + } + + //========================================================================= + // AssignAssetData + //========================================================================= + void AssetManager::AssignAssetData(const Asset& asset) + { + AZ_Assert(asset.Get(), "Reloaded data is missing!"); + + const AssetId& assetId = asset.GetId(); + + asset->m_status = AssetData::AssetStatus::Ready; + UpdateDebugStatus(asset); + + if (asset->IsRegisterReadonlyAndShareable()) + { + bool requeue{ false }; { - AZStd::lock_guard assetLock(m_assetMutex); - m_reloads.erase(asset.GetId()); - } - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetReloadError, asset); - } - - //========================================================================= - // NotifyAssetError - //========================================================================= - void AssetManager::NotifyAssetError(Asset asset) - { - asset.Get()->m_status = AssetData::AssetStatus::Error; - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetError, asset); - } - - void AssetManager::NotifyAssetCanceled(AssetId assetId) - { - AssetBus::Event(assetId, &AssetBus::Events::OnAssetCanceled, assetId); - } - - void AssetManager::NotifyAssetContainerReady(Asset asset) - { - AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetContainerReady, asset); - } - - //========================================================================= - // AddJob - // [04/02/2014] - //========================================================================= - void AssetManager::AddJob(AssetDatabaseJob* job) - { - AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); - - m_activeJobs.push_back(*job); - } - - //========================================================================= - // ValidateAndRegisterAssetLoading - //========================================================================= - bool AssetManager::ValidateAndRegisterAssetLoading(const Asset& asset) - { - AssetData* data = asset.Get(); - { - AZStd::scoped_lock assetLock(m_assetMutex); - if (data) + auto found = m_assets.find(assetId); + AZ_Assert(found == m_assets.end() || asset.Get()->RTTI_GetType() == found->second->RTTI_GetType(), + "New and old data types are mismatched!"); + + // if we are here it implies that we have two assets with the same asset id, and we are + // trying to replace the old asset with the new asset which was not created using the asset manager system. + // In this scenario if any other system have cached the old asset then the asset wont be destroyed + // because of creation token mismatch when it's ref count finally goes to zero. Since the old asset is not shareable anymore + // manually setting the creationToken to default creation token will ensure that the asset is destroyed correctly. + asset.m_assetData->m_creationToken = ++m_creationTokenGenerator; + if (found != m_assets.end()) { - // The purpose of this function is to validate this asset is still in a StreamReady - // and only then continue the load. We change status to loading if everything - // is expected which the blocking RegisterAssetLoading call does not do because it - // is already in loading status - if (data->GetStatus() != AssetData::AssetStatus::StreamReady) - { - // Something else has attempted to load this asset - return false; - } - data->m_status = AssetData::AssetStatus::Loading; - UpdateDebugStatus(asset); + found->second->m_creationToken = AZ::Data::s_defaultCreationToken; + } + + // Held references to old data are retained, but replace the entry in the DB for future requests. + // Fire an OnAssetReloaded message so listeners can react to the new data. + m_assets[assetId] = asset.Get(); + + // Release the reload reference. + auto reloadInfo = m_reloads.find(assetId); + if (reloadInfo != m_reloads.end()) + { + requeue = reloadInfo->second->GetRequeue(); + m_reloads.erase(reloadInfo); } } + // Call reloaded before we can call ReloadAsset below to preserve order + AssetBus::Event(assetId, &AssetBus::Events::OnAssetReloaded, asset); + // Release the lock before we call reload + if (requeue) + { + ReloadAsset(assetId, asset.GetAutoLoadBehavior()); + } + } + else + { + AssetBus::Event(assetId, &AssetBus::Events::OnAssetReloaded, asset); + } + } - return true; + //========================================================================= + // GetModifiedLoadStreamInfoForAsset + //========================================================================= + AssetStreamInfo AssetManager::GetModifiedLoadStreamInfoForAsset(const Asset& asset, AssetHandler* handler) + { + AssetStreamInfo loadInfo = GetLoadStreamInfoForAsset(asset.GetId(), asset.GetType()); + if (!loadInfo.IsValid()) + { + // opportunity for handler to do default substitution: + AZ::Data::AssetId fallbackId = handler->AssetMissingInCatalog(asset); + if (fallbackId.IsValid()) + { + loadInfo = GetLoadStreamInfoForAsset(fallbackId, asset.GetType()); + } } - //========================================================================= - // RegisterAssetLoading - //========================================================================= - void AssetManager::RegisterAssetLoading(const Asset& asset) - { - AZ_PROFILE_FUNCTION(AzCore); + // Give the handler an opportunity to modify any of the load info before creating the dataStream. + handler->GetCustomAssetStreamInfoForLoad(loadInfo); - AssetData* data = asset.Get(); + return loadInfo; + } + + //========================================================================= + // QueueAsyncStreamLoad + //========================================================================= + void AssetManager::QueueAsyncStreamLoad(Asset asset, AZStd::shared_ptr dataStream, + const AZ::Data::AssetStreamInfo& streamInfo, bool isReload, + AssetHandler* handler, const AssetLoadParameters& loadParams, bool signalLoaded) + { + AZ_PROFILE_FUNCTION(AzCore); + + // Set up the callback that will process the asset data once the raw file load is finished. + // The callback is declared as mutable so that we can clear weakAsset within the callback. The refcount in weakAsset + // can trigger an AssetManager::ReleaseAsset call. If this occurs during lambda cleanup, it could happen at any time + // on the file streamer thread as streamer requests get recycled, including during (or after) AssetManager shutdown. + // By controlling when the refcount is changed, we can ensure that it occurs while the AssetManager is still active. + auto assetDataStreamCallback = [this, loadParams, handler, dataStream, signalLoaded, isReload, + weakAsset = AssetInternal::WeakAsset(asset)] + (AZ::IO::IStreamerTypes::RequestStatus status) mutable + { + auto assetId = weakAsset.GetId(); + + Asset loadingAsset = weakAsset.GetStrongReference(); + + if (loadingAsset) + { + AZ_PROFILE_SCOPE(AzCore, "AZ::Data::LoadAssetStreamerCallback %s", + loadingAsset.GetHint().c_str()); + { + AZStd::scoped_lock assetLock(m_assetMutex); + AssetData* data = loadingAsset.Get(); + if (data->GetStatus() != AssetData::AssetStatus::Queued) + { + AZ_Warning("AssetManager", false, "Asset %s no longer in Queued state, abandoning load", loadingAsset.GetId().ToString().c_str()); + return; + } + data->m_status = AssetData::AssetStatus::StreamReady; + } + + // The callback from AZ Streamer blocks the streaming thread until this function completes. To minimize the overhead, + // do the majority of the work in a separate job. + auto loadJob = aznew LoadAssetJob(this, loadingAsset, + dataStream, isReload, status, handler, loadParams, signalLoaded); + + bool jobQueued = false; + + // If there's already an active blocking request waiting for this load to complete, let that thread handle + // the load itself instead of consuming a second thread. + { + AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); + auto range = m_activeBlockingRequests.equal_range(assetId); + for(auto blockingRequest = range.first; blockingRequest != range.second; ++blockingRequest) + { + if(blockingRequest->second->QueueAssetLoadJob(loadJob)) + { + jobQueued = true; + break; + } + } + } + + if (!jobQueued) + { + loadJob->Start(); + } + } + else + { + BlockingAssetLoadBus::Event(assetId, &BlockingAssetLoadBus::Events::OnLoadCanceled, assetId); + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetCanceled, assetId); + } + + // *After* the loadJob has been created, clear our asset references and remove the active streamer requests. + // This needs to happen after the loadJob creation to ensure that on AssetManager shutdown, there are no brief + // windows in which requests and/or jobs are still active after we've removed our tracking of the requests and jobs. + + // Also, if the asset references don't get cleared until after the callback completes, or at some indeterminate later + // time when the File Streamer cleans up the file requests (for the weakAsset lambda parameter), then it's possible that + // they will trigger a ReleaseAsset call sometime after the AssetManager has begun to shut down, which can lead to + // race conditions. + + // Make sure the streamer request is removed first before the asset is released + // If the asset is released first it could lead to a race condition where another thread starts loading the asset + // again and attempts to add a new streamer request with the same ID before the old one has been removed, causing + // that load request to fail + RemoveActiveStreamerRequest(assetId); + weakAsset = {}; + loadingAsset.Reset(); + }; + + auto&& [deadline, priority] = GetEffectiveDeadlineAndPriority(*handler, asset.GetType(), loadParams); + + // Track the load request and queue the asset data stream load. + AddActiveStreamerRequest(asset.GetId(), dataStream); + dataStream->Open( + streamInfo.m_streamName, + streamInfo.m_dataOffset, + streamInfo.m_dataLen, + deadline, priority, assetDataStreamCallback); + } + + //========================================================================= + // NotifyAssetReady + //========================================================================= + void AssetManager::NotifyAssetReady(Asset asset) + { + AssetData* data = asset.Get(); + AZ_Assert(data, "NotifyAssetReady: asset is missing info!"); + data->m_status = AssetData::AssetStatus::Ready; + + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetReady, asset); + } + + //========================================================================= + // NotifyAssetPreReload + //========================================================================= + void AssetManager::NotifyAssetPreReload(Asset asset) + { + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetPreReload, asset); + } + + //========================================================================= + // NotifyAssetReloaded + //========================================================================= + void AssetManager::NotifyAssetReloaded(Asset asset) + { + AssignAssetData(asset); + } + + //========================================================================= + // NotifyAssetReloaded + //========================================================================= + void AssetManager::NotifyAssetReloadError(Asset asset) + { + // Failed reloads have no side effects. Just notify observers (error reporting, etc). + { + AZStd::lock_guard assetLock(m_assetMutex); + m_reloads.erase(asset.GetId()); + } + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetReloadError, asset); + } + + //========================================================================= + // NotifyAssetError + //========================================================================= + void AssetManager::NotifyAssetError(Asset asset) + { + asset.Get()->m_status = AssetData::AssetStatus::Error; + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetError, asset); + } + + void AssetManager::NotifyAssetCanceled(AssetId assetId) + { + AssetBus::Event(assetId, &AssetBus::Events::OnAssetCanceled, assetId); + } + + void AssetManager::NotifyAssetContainerReady(Asset asset) + { + AssetBus::Event(asset.GetId(), &AssetBus::Events::OnAssetContainerReady, asset); + } + + //========================================================================= + // AddJob + // [04/02/2014] + //========================================================================= + void AssetManager::AddJob(AssetDatabaseJob* job) + { + AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); + + m_activeJobs.push_back(*job); + } + + //========================================================================= + // ValidateAndRegisterAssetLoading + //========================================================================= + bool AssetManager::ValidateAndRegisterAssetLoading(const Asset& asset) + { + AssetData* data = asset.Get(); + { + + AZStd::scoped_lock assetLock(m_assetMutex); if (data) { + // The purpose of this function is to validate this asset is still in a StreamReady + // and only then continue the load. We change status to loading if everything + // is expected which the blocking RegisterAssetLoading call does not do because it + // is already in loading status + if (data->GetStatus() != AssetData::AssetStatus::StreamReady) + { + // Something else has attempted to load this asset + return false; + } data->m_status = AssetData::AssetStatus::Loading; UpdateDebugStatus(asset); } } - //========================================================================= - // UnregisterAssetLoadingByThread - //========================================================================= - void AssetManager::UnregisterAssetLoading([[maybe_unused]] const Asset& asset) + return true; + } + + //========================================================================= + // RegisterAssetLoading + //========================================================================= + void AssetManager::RegisterAssetLoading(const Asset& asset) + { + AZ_PROFILE_FUNCTION(AzCore); + + AssetData* data = asset.Get(); + if (data) { - AZ_PROFILE_FUNCTION(AzCore); - } - - //========================================================================= - // RemoveJob - // [04/02/2014] - //========================================================================= - void AssetManager::RemoveJob(AssetDatabaseJob* job) - { - AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); - - m_activeJobs.erase(*job); - } - - //========================================================================= - // AddActiveStreamerRequest - //========================================================================= - void AssetManager::AddActiveStreamerRequest(AssetId assetId, AZStd::shared_ptr readRequest) - { - AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); - - // Track the request to allow for manual cancellation and for validating completion before AssetManager shutdown - [[maybe_unused]] auto inserted = - m_activeAssetDataStreamRequests.insert(AZStd::make_pair(assetId, readRequest)); - AZ_Assert(inserted.second, "Failed to insert streaming request into map for later retrieval by asset."); - - } - - void AssetManager::RescheduleStreamerRequest(AssetId assetId, AZStd::chrono::milliseconds newDeadline, AZ::IO::IStreamerTypes::Priority newPriority) - { - AZStd::scoped_lock lock(m_activeJobOrRequestMutex); - - auto iterator = m_activeAssetDataStreamRequests.find(assetId); - - if (iterator != m_activeAssetDataStreamRequests.end()) - { - iterator->second->Reschedule(newDeadline, newPriority); - } - } - - //========================================================================= - // RemoveActiveStreamerRequest - //========================================================================= - void AssetManager::RemoveActiveStreamerRequest(AssetId assetData) - { - AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); - m_activeAssetDataStreamRequests.erase(assetData); - } - - //========================================================================= - // HasActiveJobsOrStreamerRequests - //========================================================================= - bool AssetManager::HasActiveJobsOrStreamerRequests() - { - AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); - - return (!(m_activeJobs.empty() && m_activeAssetDataStreamRequests.empty())); - } - - //========================================================================= - // AddBlockingRequest - //========================================================================= - void AssetManager::AddBlockingRequest(AssetId assetId, WaitForAsset* blockingRequest) - { - AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); - - [[maybe_unused]] auto inserted = m_activeBlockingRequests.insert(AZStd::make_pair(assetId, blockingRequest)); - AZ_Assert(inserted.second, "Failed to track blocking request for asset %s", assetId.ToString().c_str()); - } - - //========================================================================= - // RemoveBlockingRequest - //========================================================================= - void AssetManager::RemoveBlockingRequest(AssetId assetId, WaitForAsset* blockingRequest) - { - AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); - [[maybe_unused]] bool requestFound = false; - for (auto assetIdIterator = m_activeBlockingRequests.find(assetId); assetIdIterator != m_activeBlockingRequests.end(); ) - { - if (assetIdIterator->second == blockingRequest) - { - m_activeBlockingRequests.erase(assetIdIterator); - requestFound = true; - break; - } - else - { - assetIdIterator++; - } - } - - AZ_Assert(requestFound, "Failed to erase blocking request for asset %s", assetId.ToString().c_str()); - } - - - //========================================================================= - // GetLoadStreamInfoForAsset() - // [04/04/2014] - //========================================================================= - AssetStreamInfo AssetManager::GetLoadStreamInfoForAsset(const AssetId& assetId, const AssetType& assetType) - { - AZStd::scoped_lock catalogLock(m_catalogMutex); - AssetCatalogMap::iterator catIt = m_catalogs.find(assetType); - if (catIt == m_catalogs.end()) - { - AZ_Error("Asset", false, "Asset [type:%s id:%s] with this type doesn't have a catalog!", assetType.template ToString().c_str(), assetId.ToString().c_str()); - return AssetStreamInfo(); - } - return catIt->second->GetStreamInfoForLoad(assetId, assetType); - } - - //========================================================================= - // GetSaveStreamInfoForAsset() - // [04/04/2014] - //========================================================================= - AssetStreamInfo AssetManager::GetSaveStreamInfoForAsset(const AssetId& assetId, const AssetType& assetType) - { - AZStd::scoped_lock catalogLock(m_catalogMutex); - AssetCatalogMap::iterator catIt = m_catalogs.find(assetType); - if (catIt == m_catalogs.end()) - { - AZ_Error("Asset", false, "Asset [type:%s id:%s] with this type doesn't have a catalog!", assetType.template ToString().c_str(), assetId.ToString().c_str()); - return AssetStreamInfo(); - } - return catIt->second->GetStreamInfoForSave(assetId, assetType); - } - - //========================================================================= - // OnAssetReady - // [04/02/2014] - //========================================================================= - void AssetManager::OnAssetReady(const Asset& asset) - { - AZ_Assert(asset.Get(), "OnAssetReady fired for an asset with no data."); - - // Set status immediately from within the AssetManagerBus dispatch, so it's committed before anyone is notified (e.g. job to job, via AssetJobBus). - asset.Get()->m_status = AssetData::AssetStatus::ReadyPreNotify; + data->m_status = AssetData::AssetStatus::Loading; UpdateDebugStatus(asset); - - // Queue broadcast message for delivery on game thread. - AssetBus::QueueFunction(&AssetManager::NotifyAssetReady, this, Asset(asset)); } + } - //========================================================================= - // OnAssetError - //========================================================================= - void AssetManager::OnAssetError(const Asset& asset) + //========================================================================= + // UnregisterAssetLoadingByThread + //========================================================================= + void AssetManager::UnregisterAssetLoading([[maybe_unused]] const Asset& asset) + { + AZ_PROFILE_FUNCTION(AzCore); + } + + //========================================================================= + // RemoveJob + // [04/02/2014] + //========================================================================= + void AssetManager::RemoveJob(AssetDatabaseJob* job) + { + AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); + + m_activeJobs.erase(*job); + } + + //========================================================================= + // AddActiveStreamerRequest + //========================================================================= + void AssetManager::AddActiveStreamerRequest(AssetId assetId, AZStd::shared_ptr readRequest) + { + AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); + + // Track the request to allow for manual cancellation and for validating completion before AssetManager shutdown + [[maybe_unused]] auto inserted = + m_activeAssetDataStreamRequests.insert(AZStd::make_pair(assetId, readRequest)); + AZ_Assert(inserted.second, "Failed to insert streaming request into map for later retrieval by asset."); + + } + + void AssetManager::RescheduleStreamerRequest(AssetId assetId, AZStd::chrono::milliseconds newDeadline, AZ::IO::IStreamerTypes::Priority newPriority) + { + AZStd::scoped_lock lock(m_activeJobOrRequestMutex); + + auto iterator = m_activeAssetDataStreamRequests.find(assetId); + + if (iterator != m_activeAssetDataStreamRequests.end()) { - // Set status immediately from within the AssetManagerBus dispatch, so it's committed before anyone is notified (e.g. job to job, via AssetJobBus). - asset.Get()->m_status = AssetData::AssetStatus::Error; - UpdateDebugStatus(asset); - - // Queue broadcast message for delivery on game thread. - AssetBus::QueueFunction(&AssetManager::NotifyAssetError, this, Asset(asset)); + iterator->second->Reschedule(newDeadline, newPriority); } + } - void AssetManager::OnAssetCanceled(AssetId assetId) + //========================================================================= + // RemoveActiveStreamerRequest + //========================================================================= + void AssetManager::RemoveActiveStreamerRequest(AssetId assetData) + { + AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); + m_activeAssetDataStreamRequests.erase(assetData); + } + + //========================================================================= + // HasActiveJobsOrStreamerRequests + //========================================================================= + bool AssetManager::HasActiveJobsOrStreamerRequests() + { + AZStd::scoped_lock assetLock(m_activeJobOrRequestMutex); + + return (!(m_activeJobs.empty() && m_activeAssetDataStreamRequests.empty())); + } + + //========================================================================= + // AddBlockingRequest + //========================================================================= + void AssetManager::AddBlockingRequest(AssetId assetId, WaitForAsset* blockingRequest) + { + AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); + + [[maybe_unused]] auto inserted = m_activeBlockingRequests.insert(AZStd::make_pair(assetId, blockingRequest)); + AZ_Assert(inserted.second, "Failed to track blocking request for asset %s", assetId.ToString().c_str()); + } + + //========================================================================= + // RemoveBlockingRequest + //========================================================================= + void AssetManager::RemoveBlockingRequest(AssetId assetId, WaitForAsset* blockingRequest) + { + AZStd::scoped_lock requestLock(m_activeBlockingRequestMutex); + [[maybe_unused]] bool requestFound = false; + for (auto assetIdIterator = m_activeBlockingRequests.find(assetId); assetIdIterator != m_activeBlockingRequests.end(); ) { - // Queue broadcast message for delivery on game thread. - AssetBus::QueueFunction(&AssetManager::NotifyAssetCanceled, this, assetId); - } - - void AssetManager::ReleaseOwnedAssetContainer(AssetContainer* assetContainer) - { - AZ_Assert(assetContainer, "Trying to release a null assetContainer pointer!"); - AZStd::scoped_lock lock(m_assetContainerMutex); - auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetContainer->GetContainerAssetId()); - - for (auto itr = rangeItr.first; itr != rangeItr.second; ++itr) + if (assetIdIterator->second == blockingRequest) { - if (itr->second == assetContainer) - { - m_ownedAssetContainerLookup.erase(itr); - break; - } - } - - m_ownedAssetContainers.erase(assetContainer); - } - - void AssetManager::OnAssetContainerReady(AssetContainer* assetContainer) - { - AssetBus::QueueFunction([this, assetContainer, asset = assetContainer->GetRootAsset()]() - { - NotifyAssetContainerReady(asset); - ReleaseOwnedAssetContainer(assetContainer); - }); - } - - void AssetManager::OnAssetContainerCanceled(AssetContainer* assetContainer) - { - AssetBus::QueueFunction([this, assetContainer]() - { - ReleaseOwnedAssetContainer(assetContainer); - }); - } - - //========================================================================= - // OnAssetReloaded - //========================================================================= - void AssetManager::OnAssetReloaded(const Asset& asset) - { - // Queue broadcast message for delivery on game thread. - AssetBus::QueueFunction(&AssetManager::NotifyAssetReloaded, this, Asset(asset)); - } - - //========================================================================= - // OnAssetReloadError - //========================================================================= - void AssetManager::OnAssetReloadError(const Asset& asset) - { - // Queue broadcast message for delivery on game thread. - AssetBus::QueueFunction(&AssetManager::NotifyAssetReloadError, this, Asset(asset)); - } - - - //========================================================================= - // AssetHandler - // [04/03/2014] - //========================================================================= - AssetHandler::AssetHandler() - : m_nHandledTypes(0) - { - } - - //========================================================================= - // ~AssetHandler - // [04/03/2014] - //========================================================================= - AssetHandler::~AssetHandler() - { - if (m_nHandledTypes > 0) - { - AssetManager::Instance().UnregisterHandler(this); - } - - AZ_Error("AssetDatabase", m_nHandledTypes == 0, "Asset handler is being destroyed but there are still %d asset types being handled by it!", (int)m_nHandledTypes); - } - - //========================================================================= - // LoadAssetDataFromStream - //========================================================================= - AssetHandler::LoadResult AssetHandler::LoadAssetDataFromStream( - const Asset& asset, - AZStd::shared_ptr stream, - const AssetFilterCB& assetLoadFilterCB) - { - AZ_PROFILE_SCOPE(AzCore, "AssetHandler::LoadAssetData - %s", asset.GetHint().c_str()); - -#ifdef AZ_ENABLE_TRACING - auto start = AZStd::chrono::system_clock::now(); -#endif - - LoadResult result = LoadAssetData(asset, stream, assetLoadFilterCB); - -#ifdef AZ_ENABLE_TRACING - auto loadMs = AZStd::chrono::duration_cast( - AZStd::chrono::system_clock::now() - start); - AZ_Warning("AssetDatabase", (!cl_assetLoadWarningEnable) || - loadMs <= AZStd::chrono::milliseconds(cl_assetLoadWarningMsThreshold), - "Load time threshold exceeded: LoadAssetData call for %s took %" PRId64 " ms", - asset.GetHint().c_str(), loadMs.count()); -#endif - - return result; - } - - //========================================================================= - // InitAsset - // [04/03/2014] - //========================================================================= - void AssetHandler::InitAsset(const Asset& asset, bool loadStageSucceeded, bool isReload) - { - if (loadStageSucceeded) - { - if (isReload) - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReloaded, asset); - } - else - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReady, asset); - } + m_activeBlockingRequests.erase(assetIdIterator); + requestFound = true; + break; } else { - if (!isReload) - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetError, asset); - } - else - { - AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReloadError, asset); - } + assetIdIterator++; } } - void AssetManager::ValidateAndPostLoad(AZ::Data::Asset& asset, bool loadSucceeded, - bool isReload, AZ::Data::AssetHandler* assetHandler) + AZ_Assert(requestFound, "Failed to erase blocking request for asset %s", assetId.ToString().c_str()); + } + + + //========================================================================= + // GetLoadStreamInfoForAsset() + // [04/04/2014] + //========================================================================= + AssetStreamInfo AssetManager::GetLoadStreamInfoForAsset(const AssetId& assetId, const AssetType& assetType) + { + AZStd::scoped_lock catalogLock(m_catalogMutex); + AssetCatalogMap::iterator catIt = m_catalogs.find(assetType); + if (catIt == m_catalogs.end()) { + AZ_Error("Asset", false, "Asset [type:%s id:%s] with this type doesn't have a catalog!", assetType.template ToString().c_str(), assetId.ToString().c_str()); + return AssetStreamInfo(); + } + return catIt->second->GetStreamInfoForLoad(assetId, assetType); + } + + //========================================================================= + // GetSaveStreamInfoForAsset() + // [04/04/2014] + //========================================================================= + AssetStreamInfo AssetManager::GetSaveStreamInfoForAsset(const AssetId& assetId, const AssetType& assetType) + { + AZStd::scoped_lock catalogLock(m_catalogMutex); + AssetCatalogMap::iterator catIt = m_catalogs.find(assetType); + if (catIt == m_catalogs.end()) + { + AZ_Error("Asset", false, "Asset [type:%s id:%s] with this type doesn't have a catalog!", assetType.template ToString().c_str(), assetId.ToString().c_str()); + return AssetStreamInfo(); + } + return catIt->second->GetStreamInfoForSave(assetId, assetType); + } + + //========================================================================= + // OnAssetReady + // [04/02/2014] + //========================================================================= + void AssetManager::OnAssetReady(const Asset& asset) + { + AZ_Assert(asset.Get(), "OnAssetReady fired for an asset with no data."); + + // Set status immediately from within the AssetManagerBus dispatch, so it's committed before anyone is notified (e.g. job to job, via AssetJobBus). + asset.Get()->m_status = AssetData::AssetStatus::ReadyPreNotify; + UpdateDebugStatus(asset); + + // Queue broadcast message for delivery on game thread. + AssetBus::QueueFunction(&AssetManager::NotifyAssetReady, this, Asset(asset)); + } + + //========================================================================= + // OnAssetError + //========================================================================= + void AssetManager::OnAssetError(const Asset& asset) + { + // Set status immediately from within the AssetManagerBus dispatch, so it's committed before anyone is notified (e.g. job to job, via AssetJobBus). + asset.Get()->m_status = AssetData::AssetStatus::Error; + UpdateDebugStatus(asset); + + // Queue broadcast message for delivery on game thread. + AssetBus::QueueFunction(&AssetManager::NotifyAssetError, this, Asset(asset)); + } + + void AssetManager::OnAssetCanceled(AssetId assetId) + { + // Queue broadcast message for delivery on game thread. + AssetBus::QueueFunction(&AssetManager::NotifyAssetCanceled, this, assetId); + } + + void AssetManager::ReleaseOwnedAssetContainer(AssetContainer* assetContainer) + { + AZ_Assert(assetContainer, "Trying to release a null assetContainer pointer!"); + AZStd::scoped_lock lock(m_assetContainerMutex); + auto rangeItr = m_ownedAssetContainerLookup.equal_range(assetContainer->GetContainerAssetId()); + + for (auto itr = rangeItr.first; itr != rangeItr.second; ++itr) + { + if (itr->second == assetContainer) { - // We may need to revalidate that this asset hasn't already passed through postLoad - AZStd::scoped_lock assetLock(m_assetMutex); - if (asset->IsReady() || asset->m_status == AssetData::AssetStatus::LoadedPreReady) - { - return; - } - asset->m_status = AssetData::AssetStatus::LoadedPreReady; - UpdateDebugStatus(asset); + m_ownedAssetContainerLookup.erase(itr); + break; } - PostLoad(asset, loadSucceeded, isReload, assetHandler); } - void AssetManager::PostLoad(AZ::Data::Asset& asset, bool loadSucceeded, - bool isReload, AZ::Data::AssetHandler* assetHandler) - { - AZ_PROFILE_FUNCTION(AzCore); - if (!assetHandler) - { - assetHandler = GetHandler(asset.GetType()); - } + m_ownedAssetContainers.erase(assetContainer); + } - if (assetHandler) + void AssetManager::OnAssetContainerReady(AssetContainer* assetContainer) + { + AssetBus::QueueFunction([this, assetContainer, asset = assetContainer->GetRootAsset()]() + { + NotifyAssetContainerReady(asset); + ReleaseOwnedAssetContainer(assetContainer); + }); + } + + void AssetManager::OnAssetContainerCanceled(AssetContainer* assetContainer) + { + AssetBus::QueueFunction([this, assetContainer]() + { + ReleaseOwnedAssetContainer(assetContainer); + }); + } + + //========================================================================= + // OnAssetReloaded + //========================================================================= + void AssetManager::OnAssetReloaded(const Asset& asset) + { + // Queue broadcast message for delivery on game thread. + AssetBus::QueueFunction(&AssetManager::NotifyAssetReloaded, this, Asset(asset)); + } + + //========================================================================= + // OnAssetReloadError + //========================================================================= + void AssetManager::OnAssetReloadError(const Asset& asset) + { + // Queue broadcast message for delivery on game thread. + AssetBus::QueueFunction(&AssetManager::NotifyAssetReloadError, this, Asset(asset)); + } + + + //========================================================================= + // AssetHandler + // [04/03/2014] + //========================================================================= + AssetHandler::AssetHandler() + : m_nHandledTypes(0) + { + } + + //========================================================================= + // ~AssetHandler + // [04/03/2014] + //========================================================================= + AssetHandler::~AssetHandler() + { + if (m_nHandledTypes > 0) + { + AssetManager::Instance().UnregisterHandler(this); + } + + AZ_Error("AssetDatabase", m_nHandledTypes == 0, "Asset handler is being destroyed but there are still %d asset types being handled by it!", (int)m_nHandledTypes); + } + + //========================================================================= + // LoadAssetDataFromStream + //========================================================================= + AssetHandler::LoadResult AssetHandler::LoadAssetDataFromStream( + const Asset& asset, + AZStd::shared_ptr stream, + const AssetFilterCB& assetLoadFilterCB) + { + AZ_PROFILE_SCOPE(AzCore, "AssetHandler::LoadAssetData - %s", asset.GetHint().c_str()); + +#ifdef AZ_ENABLE_TRACING + auto start = AZStd::chrono::system_clock::now(); +#endif + + LoadResult result = LoadAssetData(asset, stream, assetLoadFilterCB); + +#ifdef AZ_ENABLE_TRACING + auto loadMs = AZStd::chrono::duration_cast( + AZStd::chrono::system_clock::now() - start); + AZ_Warning("AssetDatabase", (!cl_assetLoadWarningEnable) || + loadMs <= AZStd::chrono::milliseconds(cl_assetLoadWarningMsThreshold), + "Load time threshold exceeded: LoadAssetData call for %s took %" PRId64 " ms", + asset.GetHint().c_str(), loadMs.count()); +#endif + + return result; + } + + //========================================================================= + // InitAsset + // [04/03/2014] + //========================================================================= + void AssetHandler::InitAsset(const Asset& asset, bool loadStageSucceeded, bool isReload) + { + if (loadStageSucceeded) + { + if (isReload) { - // Queue the result for dispatch to main thread. - assetHandler->InitAsset(asset, loadSucceeded, isReload); + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReloaded, asset); } else { - AZ_Warning("AssetManager", false, "Couldn't find handler for asset %s (%s)", asset.GetId().ToString().c_str(), asset.GetHint().c_str()); + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReady, asset); } + } + else + { + if (!isReload) + { + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetError, asset); + } + else + { + AssetManagerBus::Broadcast(&AssetManagerBus::Events::OnAssetReloadError, asset); + } + } + } - // Notify any dependent jobs. - BlockingAssetLoadBus::Event(asset.GetId(), &BlockingAssetLoadBus::Events::OnLoadComplete); + void AssetManager::ValidateAndPostLoad(AZ::Data::Asset& asset, bool loadSucceeded, + bool isReload, AZ::Data::AssetHandler* assetHandler) + { + { + // We may need to revalidate that this asset hasn't already passed through postLoad + AZStd::scoped_lock assetLock(m_assetMutex); + if (asset->IsReady() || asset->m_status == AssetData::AssetStatus::LoadedPreReady) + { + return; + } + asset->m_status = AssetData::AssetStatus::LoadedPreReady; + UpdateDebugStatus(asset); + } + PostLoad(asset, loadSucceeded, isReload, assetHandler); + } - UnregisterAssetLoading(asset); + void AssetManager::PostLoad(AZ::Data::Asset& asset, bool loadSucceeded, + bool isReload, AZ::Data::AssetHandler* assetHandler) + { + AZ_PROFILE_FUNCTION(AzCore); + if (!assetHandler) + { + assetHandler = GetHandler(asset.GetType()); } - AZStd::shared_ptr AssetManager::GetAssetContainer(Asset asset, const AssetLoadParameters& loadParams) + if (assetHandler) { - // If we're doing a custom load through a filter just hand back a one off container - if (loadParams.m_assetLoadFilterCB) - { - return CreateAssetContainer(asset, loadParams); - } + // Queue the result for dispatch to main thread. + assetHandler->InitAsset(asset, loadSucceeded, isReload); + } + else + { + AZ_Warning("AssetManager", false, "Couldn't find handler for asset %s (%s)", asset.GetId().ToString().c_str(), asset.GetHint().c_str()); + } - AZStd::scoped_lock containerLock(m_assetContainerMutex); - AssetContainerKey containerKey{ asset.GetId(), loadParams }; + // Notify any dependent jobs. + BlockingAssetLoadBus::Event(asset.GetId(), &BlockingAssetLoadBus::Events::OnLoadComplete); - auto curIter = m_assetContainers.find(containerKey); - if (curIter != m_assetContainers.end()) + UnregisterAssetLoading(asset); + } + + AZStd::shared_ptr AssetManager::GetAssetContainer(Asset asset, const AssetLoadParameters& loadParams) + { + // If we're doing a custom load through a filter just hand back a one off container + if (loadParams.m_assetLoadFilterCB) + { + return CreateAssetContainer(asset, loadParams); + } + + AZStd::scoped_lock containerLock(m_assetContainerMutex); + AssetContainerKey containerKey{ asset.GetId(), loadParams }; + + auto curIter = m_assetContainers.find(containerKey); + if (curIter != m_assetContainers.end()) + { + auto newRef = curIter->second.lock(); + if (newRef && newRef->IsValid()) { - auto newRef = curIter->second.lock(); - if (newRef && newRef->IsValid()) - { - return newRef; - } - auto newContainer = CreateAssetContainer(asset, loadParams); - curIter->second = newContainer; - return newContainer; + return newRef; } auto newContainer = CreateAssetContainer(asset, loadParams); - - m_assetContainers.insert({ containerKey, newContainer }); - + curIter->second = newContainer; return newContainer; } + auto newContainer = CreateAssetContainer(asset, loadParams); - AZStd::shared_ptr AssetManager::CreateAssetContainer(Asset asset, const AssetLoadParameters& loadParams) const - { - return AZStd::shared_ptr( aznew AssetContainer(AZStd::move(asset), loadParams)); - } - } // namespace Data -} // namespace AZ + m_assetContainers.insert({ containerKey, newContainer }); + + return newContainer; + } + + AZStd::shared_ptr AssetManager::CreateAssetContainer(Asset asset, const AssetLoadParameters& loadParams) const + { + return AZStd::shared_ptr( aznew AssetContainer(AZStd::move(asset), loadParams)); + } +} // namespace AZ::Data size_t AZStd::hash::operator()(const AZ::Data::AssetContainerKey& obj) const { diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetManager.h b/Code/Framework/AzCore/AzCore/Asset/AssetManager.h index f109bb278c..9666d434c1 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetManager.h +++ b/Code/Framework/AzCore/AzCore/Asset/AssetManager.h @@ -169,14 +169,14 @@ namespace AZ /// Register handler with the system for a particular asset type. /// A handler should be registered for each asset type it handles. /// Please note that all the handlers are registered just once during app startup from the main thread - /// and therefore this is not a thread safe method and should not be invoked from different threads. + /// and therefore this is not a thread safe method and should not be invoked from different threads. void RegisterHandler(AssetHandler* handler, const AssetType& assetType); /// Unregister handler from the asset system. /// Please note that all the handlers are unregistered just once during app shutdown from the main thread /// and therefore this is not a thread safe method and should not be invoked from different threads. void UnregisterHandler(AssetHandler* handler); // @} - + // @{ Asset catalog management /// Register a catalog with the system for a particular asset type. /// A catalog should be registered for each asset type it is responsible for. @@ -295,7 +295,7 @@ namespace AZ /** * Old 'legacy' assetIds and asset hints can be automatically replaced with new ones during deserialize / assignment. * This operation can be somewhat costly, and its only useful if the program subsequently re-saves the files its loading so that - * the asset hints and assetIds actually persist. Thus, it can be disabled in situations where you know you are not going to be + * the asset hints and assetIds actually persist. Thus, it can be disabled in situations where you know you are not going to be * saving over or creating new source files (for example builders/background apps) * By default, it is enabled. */ @@ -316,7 +316,7 @@ namespace AZ * This method must be invoked before you start unregistering handlers manually and shutting down the asset manager. * This method ensures that all jobs in flight are either canceled or completed. * This method is automatically called in the destructor but if you are unregistering handlers manually, - * you must invoke it yourself. + * you must invoke it yourself. */ void PrepareShutDown(); @@ -366,7 +366,7 @@ namespace AZ /** * Creates a new shared AssetContainer with an optional loadFilter * **/ - AZStd::shared_ptr CreateAssetContainer(Asset asset, const AssetLoadParameters& loadParams = AssetLoadParameters{}) const; + virtual AZStd::shared_ptr CreateAssetContainer(Asset asset, const AssetLoadParameters& loadParams = AssetLoadParameters{}) const; /** @@ -452,7 +452,7 @@ namespace AZ // Variant of RegisterAssetLoading used for jobs which have been queued and need to verify the status of the asset - // before loading in order to prevent cases where a load is queued, then a blocking load goes through, then the queued + // before loading in order to prevent cases where a load is queued, then a blocking load goes through, then the queued // load is processed. This validation step leaves the loaded (And potentially modified) data as is in that case. bool ValidateAndRegisterAssetLoading(const Asset& asset); @@ -482,7 +482,7 @@ namespace AZ * the blocking. That will result in a single thread deadlock. * * If you need to queue work, the logic needs to be similar to this: - * + * AssetHandler::LoadResult MyAssetHandler::LoadAssetData(const Asset& asset, AZStd::shared_ptr stream, const AZ::Data::AssetFilterCB& assetLoadFilterCB) { @@ -496,13 +496,13 @@ namespace AZ } else { - // queue job to load asset in thread identified by m_loadingThreadId + // queue job to load asset in thread identified by m_loadingThreadId auto* queuedJob = QueueLoadingOnOtherThread(...); // block waiting for queued job to complete queuedJob->BlockUntilComplete(); } - + . . . @@ -525,7 +525,7 @@ namespace AZ //! Result from LoadAssetData - it either finished loading, didn't finish and is waiting for more data, or had an error. enum class LoadResult : u8 { - + Error, // The provided data failed to load correctly MoreDataRequired, // The provided data loaded correctly, but more data is required to finish the asset load LoadComplete // The provided data loaded correctly, and the asset has been created diff --git a/Code/Framework/AzCore/AzCore/Asset/AssetManagerBus.h b/Code/Framework/AzCore/AzCore/Asset/AssetManagerBus.h index f76ea19589..44707645d0 100644 --- a/Code/Framework/AzCore/AzCore/Asset/AssetManagerBus.h +++ b/Code/Framework/AzCore/AzCore/Asset/AssetManagerBus.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -129,7 +130,8 @@ namespace AZ /// Remove a catalog from our delta list and rebuild the catalog from remaining items virtual bool RemoveDeltaCatalog(AZStd::shared_ptr /*deltaCatalog*/) { return true; } /// Creates a manifest with the given DeltaCatalog name - virtual bool CreateBundleManifest(const AZStd::string& /*deltaCatalogPath*/, const AZStd::vector& /*dependentBundleNames*/, const AZStd::string& /*fileDirectory*/, int /*bundleVersion*/, const AZStd::vector& /*levelDirs*/) { return false; } + virtual bool CreateBundleManifest(const AZStd::string& /*deltaCatalogPath*/, const AZStd::vector& /*dependentBundleNames*/, + const AZStd::string& /*fileDirectory*/, int /*bundleVersion*/, const AZStd::vector& /*levelDirs*/) { return false; } /// Creates an instance of a registry containing info for just the specified files, and writes it out to a file at the specified path virtual bool CreateDeltaCatalog(const AZStd::vector& /*files*/, const AZStd::string& /*filePath*/) { return false; } diff --git a/Code/Framework/AzCore/AzCore/AzCoreModule.cpp b/Code/Framework/AzCore/AzCore/AzCoreModule.cpp index 67123ed826..e7e87559b0 100644 --- a/Code/Framework/AzCore/AzCore/AzCoreModule.cpp +++ b/Code/Framework/AzCore/AzCore/AzCoreModule.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -40,7 +39,6 @@ namespace AZ SliceComponent::CreateDescriptor(), SliceSystemComponent::CreateDescriptor(), SliceMetadataInfoComponent::CreateDescriptor(), - TimeSystemComponent::CreateDescriptor(), LoggerSystemComponent::CreateDescriptor(), EventSchedulerSystemComponent::CreateDescriptor(), TaskGraphSystemComponent::CreateDescriptor(), @@ -59,7 +57,6 @@ namespace AZ { return AZ::ComponentTypeList { - azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp index ad7e44c2ae..cf23d2b4d7 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp @@ -72,6 +72,7 @@ #include #include +#include static void PrintEntityName(const AZ::ConsoleCommandContainer& arguments) { @@ -416,6 +417,7 @@ namespace AZ ComponentApplication::ComponentApplication(int argC, char** argV) : m_eventLogger{} + , m_timeSystem(AZStd::make_unique()) { if (Interface::Get() == nullptr) { @@ -485,16 +487,6 @@ namespace AZ constexpr bool executeRegDumpCommands = false; SettingsRegistryMergeUtils::MergeSettingsToRegistry_CommandLine(*m_settingsRegistry, m_commandLine, executeRegDumpCommands); - // Query for the Executable Path using OS specific functions - CalculateExecutablePath(); - - // Determine the path to the engine - CalculateEngineRoot(); - - // If the current platform returns an engaged optional from Utils::GetDefaultAppRootPath(), that is used - // for the application root. - CalculateAppRoot(); - SettingsRegistryMergeUtils::MergeSettingsToRegistry_O3deUserRegistry(*m_settingsRegistry, AZ_TRAIT_OS_PLATFORM_CODENAME, {}); SettingsRegistryMergeUtils::MergeSettingsToRegistry_CommandLine(*m_settingsRegistry, m_commandLine, executeRegDumpCommands); SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*m_settingsRegistry); @@ -584,7 +576,6 @@ namespace AZ DestroyAllocator(); } - void ReportBadEngineRoot() { AZStd::string errorMessage = {"Unable to determine a valid path to the engine.\n" @@ -614,7 +605,8 @@ namespace AZ { AZ_Assert(!m_isStarted, "Component application already started!"); - if (m_engineRoot.empty()) + using Type = AZ::SettingsRegistryInterface::Type; + if (m_settingsRegistry->GetType(SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder) == Type::NoType) { ReportBadEngineRoot(); return nullptr; @@ -686,7 +678,6 @@ namespace AZ ComponentApplicationBus::Handler::BusConnect(); - m_currentTime = AZStd::chrono::system_clock::now(); TickRequestBus::Handler::BusConnect(); #if defined(AZ_ENABLE_DEBUG_TOOLS) @@ -1180,6 +1171,24 @@ namespace AZ return ReflectionEnvironment::GetReflectionManager() ? ReflectionEnvironment::GetReflectionManager()->GetReflectContext() : nullptr; } + /// Returns the path to the engine. + + const char* ComponentApplication::GetEngineRoot() const + { + static IO::FixedMaxPathString engineRoot; + engineRoot.clear(); + m_settingsRegistry->Get(engineRoot, SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder); + return engineRoot.c_str(); + } + + const char* ComponentApplication::GetExecutableFolder() const + { + static IO::FixedMaxPathString exeFolder; + exeFolder.clear(); + m_settingsRegistry->Get(exeFolder, SettingsRegistryMergeUtils::FilePathKey_BinaryFolder); + return exeFolder.c_str(); + } + //========================================================================= // CreateReflectionManager //========================================================================= @@ -1404,31 +1413,23 @@ namespace AZ #endif } - void ComponentApplication::Tick(float deltaOverride /*= -1.f*/) + void ComponentApplication::Tick() { + AZ_PROFILE_SCOPE(System, "Component application simulation tick"); + { - AZ_PROFILE_SCOPE(System, "Component application simulation tick"); - - AZStd::chrono::system_clock::time_point now = AZStd::chrono::system_clock::now(); - - m_deltaTime = 0.0f; - - if (now >= m_currentTime) - { - AZStd::chrono::duration delta = now - m_currentTime; - m_deltaTime = deltaOverride >= 0.f ? deltaOverride : delta.count(); - } - - { - AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:ExecuteQueuedEvents"); - TickBus::ExecuteQueuedEvents(); - } - m_currentTime = now; - { - AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:OnTick"); - EBUS_EVENT(TickBus, OnTick, m_deltaTime, ScriptTimePoint(now)); - } + AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:ExecuteQueuedEvents"); + TickBus::ExecuteQueuedEvents(); } + + { + AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:OnTick"); + const AZ::TimeUs deltaTimeUs = m_timeSystem->AdvanceTickDeltaTimes(); + const float deltaTimeSeconds = AZ::TimeUsToSeconds(deltaTimeUs); + AZ::TickBus::Broadcast(&TickEvents::OnTick, deltaTimeSeconds, GetTimeAtCurrentTick()); + } + + m_timeSystem->ApplyTickRateLimiterIfNeeded(); } void ComponentApplication::TickSystem() @@ -1485,27 +1486,6 @@ namespace AZ } } - //========================================================================= - // CalculateExecutablePath - //========================================================================= - void ComponentApplication::CalculateExecutablePath() - { - m_exeDirectory = Utils::GetExecutableDirectory(); - } - - void ComponentApplication::CalculateAppRoot() - { - if (AZStd::optional appRootPath = Utils::GetDefaultAppRootPath(); appRootPath) - { - m_appRoot = AZStd::move(*appRootPath); - } - } - - void ComponentApplication::CalculateEngineRoot() - { - m_engineRoot = AZ::SettingsRegistryMergeUtils::FindEngineRoot(*m_settingsRegistry).Native(); - } - void ComponentApplication::ResolveModulePath([[maybe_unused]] AZ::OSString& modulePath) { // No special parsing of the Module Path is done by the Component Application anymore @@ -1531,13 +1511,10 @@ namespace AZ appType.m_maskValue = AZ::ApplicationTypeQuery::Masks::Invalid; } - //========================================================================= - // GetFrameTime - // [1/22/2016] - //========================================================================= float ComponentApplication::GetTickDeltaTime() { - return m_deltaTime; + const AZ::TimeUs gameTickTime = m_timeSystem->GetSimulationTickDeltaTimeUs(); + return AZ::TimeUsToSeconds(gameTickTime); } //========================================================================= @@ -1546,7 +1523,8 @@ namespace AZ //========================================================================= ScriptTimePoint ComponentApplication::GetTimeAtCurrentTick() { - return ScriptTimePoint(m_currentTime); + const AZ::TimeUs lastGameTickTime = m_timeSystem->GetLastSimulationTickTime(); + return ScriptTimePoint(AZ::TimeUsToChrono(lastGameTickTime)); } //========================================================================= diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h index 6df93aff4e..f245b5f5da 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h @@ -30,12 +30,14 @@ #include #include + namespace AZ { class BehaviorContext; class IConsole; class Module; class ModuleManager; + class TimeSystem; } namespace AZ::Debug { @@ -221,13 +223,10 @@ namespace AZ BehaviorContext* GetBehaviorContext() override; /// Returns the json registration context that has been registered with the app, if there is one. JsonRegistrationContext* GetJsonRegistrationContext() override; - /// Returns the working root folder that has been registered with the app, if there is one. - /// It's expected that derived applications will implement an application root. - const char* GetAppRoot() const override { return m_appRoot.c_str(); } /// Returns the path to the engine. - const char* GetEngineRoot() const override { return m_engineRoot.c_str(); } + const char* GetEngineRoot() const override; /// Returns the path to the folder the executable is in. - const char* GetExecutableFolder() const override { return m_exeDirectory.c_str(); } + const char* GetExecutableFolder() const override; ////////////////////////////////////////////////////////////////////////// /// TickRequestBus @@ -240,7 +239,7 @@ namespace AZ /** * Ticks all components using the \ref AZ::TickBus during simulation time. May not tick if the application is not active (i.e. not in focus) */ - virtual void Tick(float deltaOverride = -1.f); + virtual void Tick(); /** * Ticks all using the \ref AZ::SystemTickBus at all times. Should always tick even if the application is not active. @@ -352,15 +351,6 @@ namespace AZ /// Adds system components requested by modules and the application to the system entity. void AddRequiredSystemComponents(AZ::Entity* systemEntity); - /// Calculates the directory the application executable comes from. - void CalculateExecutablePath(); - - /// Calculates the root directory of the engine. - void CalculateEngineRoot(); - - /// Deprecated: The term "AppRoot" has no meaning - void CalculateAppRoot(); - template static void NormalizePath(Iterator begin, Iterator end, bool doLowercase = true) { @@ -371,8 +361,6 @@ namespace AZ } } - AZStd::chrono::system_clock::time_point m_currentTime{ AZStd::chrono::system_clock::time_point::max() }; - float m_deltaTime{ 0.0f }; AZStd::unique_ptr m_moduleManager; AZStd::unique_ptr m_settingsRegistry; EntityAddedEvent m_entityAddedEvent; @@ -388,14 +376,13 @@ namespace AZ void* m_fixedMemoryBlock{ nullptr }; //!< Pointer to the memory block allocator, so we can free it OnDestroy. IAllocatorAllocate* m_osAllocator{ nullptr }; EntitySetType m_entities; - AZ::IO::FixedMaxPath m_exeDirectory; - AZ::IO::FixedMaxPath m_engineRoot; - AZ::IO::FixedMaxPath m_appRoot; AZ::SettingsRegistryInterface::NotifyEventHandler m_projectPathChangedHandler; AZ::SettingsRegistryInterface::NotifyEventHandler m_projectNameChangedHandler; AZ::SettingsRegistryInterface::NotifyEventHandler m_commandLineUpdatedHandler; + AZStd::unique_ptr m_timeSystem; + // ConsoleFunctorHandle is responsible for unregistering the Settings Registry Console // from the m_console member when it goes out of scope AZ::SettingsRegistryConsoleUtils::ConsoleFunctorHandle m_settingsRegistryConsoleFunctors; diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplicationBus.h b/Code/Framework/AzCore/AzCore/Component/ComponentApplicationBus.h index 0c0977384a..feefa95973 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplicationBus.h +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplicationBus.h @@ -175,10 +175,6 @@ namespace AZ //! the serializers used by the best-effort json serialization. virtual class JsonRegistrationContext* GetJsonRegistrationContext() = 0; - //! Gets the name of the working root folder that was registered with the app. - //! @return a pointer to the name of the app's root folder, if a root folder was registered. - virtual const char* GetAppRoot() const = 0; - //! Gets the path of the working engine folder that the app is a part of. //! @return a pointer to the engine path. virtual const char* GetEngineRoot() const = 0; diff --git a/Code/Framework/AzCore/AzCore/Component/EntityUtils.cpp b/Code/Framework/AzCore/AzCore/Component/EntityUtils.cpp index fcc8cd6424..54e40c1fc0 100644 --- a/Code/Framework/AzCore/AzCore/Component/EntityUtils.cpp +++ b/Code/Framework/AzCore/AzCore/Component/EntityUtils.cpp @@ -10,292 +10,289 @@ #include #include -namespace AZ +namespace AZ::EntityUtils { - namespace EntityUtils + //========================================================================= + // Reflect + //========================================================================= + void Reflect(ReflectContext* context) { - //========================================================================= - // Reflect - //========================================================================= - void Reflect(ReflectContext* context) + if (auto serializeContext = azrtti_cast(context)) { - if (auto serializeContext = azrtti_cast(context)) + serializeContext->Class()-> + Version(1)-> + Field("Entities", &SerializableEntityContainer::m_entities); + } + } + + struct StackDataType + { + const SerializeContext::ClassData* m_classData; + const SerializeContext::ClassElement* m_elementData; + void* m_dataPtr; + bool m_isModifiedContainer; + }; + + //========================================================================= + // EnumerateEntityIds + //========================================================================= + void EnumerateEntityIds(const void* classPtr, const Uuid& classUuid, const EntityIdVisitor& visitor, SerializeContext* context) + { + AZ_PROFILE_FUNCTION(AzCore); + + if (!context) + { + context = GetApplicationSerializeContext(); + if (!context) { - serializeContext->Class()-> - Version(1)-> - Field("Entities", &SerializableEntityContainer::m_entities); + AZ_Error("Serialization", false, "No serialize context provided! Failed to get component application default serialize context! ComponentApp is not started or input serialize context should not be null!"); + return; } } + AZStd::vector parentStack; + parentStack.reserve(30); + auto beginCB = [ &](void* ptr, const SerializeContext::ClassData* classData, const SerializeContext::ClassElement* elementData) -> bool + { + (void)elementData; - struct StackDataType + if (classData->m_typeId == SerializeTypeInfo::GetUuid()) + { + // determine if this is entity ref or just entityId (please refer to the function documentation for more info) + bool isEntityId = false; + if (!parentStack.empty() && parentStack.back()->m_typeId == SerializeTypeInfo::GetUuid()) + { + // our parent in the entity (currently entity has only one EntityId member, but we can check the offset for future proof + AZ_Assert(elementData && strcmp(elementData->m_name, "Id") == 0, "class Entity, should have only ONE EntityId member, the actual entity id!"); + isEntityId = true; + } + + EntityId* entityIdPtr = (elementData->m_flags & SerializeContext::ClassElement::FLG_POINTER) ? + *reinterpret_cast(ptr) : reinterpret_cast(ptr); + visitor(*entityIdPtr, isEntityId, elementData); + } + + parentStack.push_back(classData); + return true; + }; + + auto endCB = [ &]() -> bool + { + parentStack.pop_back(); + return true; + }; + + SerializeContext::EnumerateInstanceCallContext callContext( + beginCB, + endCB, + context, + SerializeContext::ENUM_ACCESS_FOR_READ, + nullptr + ); + + context->EnumerateInstanceConst( + &callContext, + classPtr, + classUuid, + nullptr, + nullptr + ); + } + + //========================================================================= + // GetApplicationSerializeContext + //========================================================================= + SerializeContext* GetApplicationSerializeContext() + { + SerializeContext* context = nullptr; + EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); + return context; + } + + //========================================================================= + // FindFirstDerivedComponent + //========================================================================= + Component* FindFirstDerivedComponent(const Entity* entity, const Uuid& typeId) + { + for (AZ::Component* component : entity->GetComponents()) { - const SerializeContext::ClassData* m_classData; - const SerializeContext::ClassElement* m_elementData; - void* m_dataPtr; - bool m_isModifiedContainer; + if (azrtti_istypeof(typeId, component)) + { + return component; + } + } + return nullptr; + } + + Component* FindFirstDerivedComponent(EntityId entityId, const Uuid& typeId) + { + Entity* entity{}; + ComponentApplicationBus::BroadcastResult(entity, &ComponentApplicationRequests::FindEntity, entityId); + return entity ? FindFirstDerivedComponent(entity, typeId) : nullptr; + } + + //========================================================================= + // FindDerivedComponents + //========================================================================= + Entity::ComponentArrayType FindDerivedComponents(const Entity* entity, const Uuid& typeId) + { + Entity::ComponentArrayType result; + for (AZ::Component* component : entity->GetComponents()) + { + if (azrtti_istypeof(typeId, component)) + { + result.push_back(component); + } + } + return result; + } + + Entity::ComponentArrayType FindDerivedComponents(EntityId entityId, const Uuid& typeId) + { + Entity* entity{}; + ComponentApplicationBus::BroadcastResult(entity, &ComponentApplicationRequests::FindEntity, entityId); + return entity ? FindDerivedComponents(entity, typeId) : Entity::ComponentArrayType(); + } + + bool EnumerateBaseRecursive(SerializeContext* context, const EnumerateBaseRecursiveVisitor& baseClassVisitor, const TypeId& typeToExamine) + { + AZ_Assert(context, "CheckDeclaresSerializeBaseClass called with no serialize context."); + if (!context) + { + return false; + } + + AZStd::fixed_vector knownBaseClasses = { typeToExamine }; // avoid allocating heap here if possible. 64 types are 64*sizeof(Uuid) which is only 1k. + bool foundBaseClass = false; + auto enumerateBaseVisitor = [&baseClassVisitor, &knownBaseClasses](const AZ::SerializeContext::ClassData* classData, const TypeId& examineTypeId) + { + if (!classData) + { + return false; + } + + if (AZStd::find(knownBaseClasses.begin(), knownBaseClasses.end(), classData->m_typeId) == knownBaseClasses.end()) + { + if (knownBaseClasses.size() == 64) + { + // this should be pretty unlikely since a single class would have to have many other classes in its heirarchy + // and it'd all have to be basically in one layer, as we are popping as we explore. + AZ_WarningOnce("EntityUtils", false, "While trying to find a base class, all available slots were consumed. consider increasing the size of knownBaseClasses.\n"); + // we cannot continue any further, assume we did not find it. + return false; + } + knownBaseClasses.push_back(classData->m_typeId); + } + + return baseClassVisitor(classData, examineTypeId); }; - //========================================================================= - // EnumerateEntityIds - //========================================================================= - void EnumerateEntityIds(const void* classPtr, const Uuid& classUuid, const EntityIdVisitor& visitor, SerializeContext* context) + while (!knownBaseClasses.empty() && !foundBaseClass) { - AZ_PROFILE_FUNCTION(AzCore); + TypeId toExamine = knownBaseClasses.back(); + knownBaseClasses.pop_back(); - if (!context) - { - context = GetApplicationSerializeContext(); - if (!context) - { - AZ_Error("Serialization", false, "No serialize context provided! Failed to get component application default serialize context! ComponentApp is not started or input serialize context should not be null!"); - return; - } - } - AZStd::vector parentStack; - parentStack.reserve(30); - auto beginCB = [ &](void* ptr, const SerializeContext::ClassData* classData, const SerializeContext::ClassElement* elementData) -> bool - { - (void)elementData; - - if (classData->m_typeId == SerializeTypeInfo::GetUuid()) - { - // determine if this is entity ref or just entityId (please refer to the function documentation for more info) - bool isEntityId = false; - if (!parentStack.empty() && parentStack.back()->m_typeId == SerializeTypeInfo::GetUuid()) - { - // our parent in the entity (currently entity has only one EntityId member, but we can check the offset for future proof - AZ_Assert(elementData && strcmp(elementData->m_name, "Id") == 0, "class Entity, should have only ONE EntityId member, the actual entity id!"); - isEntityId = true; - } - - EntityId* entityIdPtr = (elementData->m_flags & SerializeContext::ClassElement::FLG_POINTER) ? - *reinterpret_cast(ptr) : reinterpret_cast(ptr); - visitor(*entityIdPtr, isEntityId, elementData); - } - - parentStack.push_back(classData); - return true; - }; - - auto endCB = [ &]() -> bool - { - parentStack.pop_back(); - return true; - }; - - SerializeContext::EnumerateInstanceCallContext callContext( - beginCB, - endCB, - context, - SerializeContext::ENUM_ACCESS_FOR_READ, - nullptr - ); - - context->EnumerateInstanceConst( - &callContext, - classPtr, - classUuid, - nullptr, - nullptr - ); + context->EnumerateBase(enumerateBaseVisitor, toExamine); } - //========================================================================= - // GetApplicationSerializeContext - //========================================================================= - SerializeContext* GetApplicationSerializeContext() - { - SerializeContext* context = nullptr; - EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); - return context; - } + return foundBaseClass; + } - //========================================================================= - // FindFirstDerivedComponent - //========================================================================= - Component* FindFirstDerivedComponent(const Entity* entity, const Uuid& typeId) + bool CheckIfClassIsDeprecated(SerializeContext* context, const TypeId& typeToExamine) + { + bool isDeprecated = false; + auto classVisitorFn = [&isDeprecated](const AZ::SerializeContext::ClassData* classData, const TypeId& /*rttiBase*/) { - for (AZ::Component* component : entity->GetComponents()) - { - if (azrtti_istypeof(typeId, component)) - { - return component; - } - } - return nullptr; - } - - Component* FindFirstDerivedComponent(EntityId entityId, const Uuid& typeId) - { - Entity* entity{}; - ComponentApplicationBus::BroadcastResult(entity, &ComponentApplicationRequests::FindEntity, entityId); - return entity ? FindFirstDerivedComponent(entity, typeId) : nullptr; - } - - //========================================================================= - // FindDerivedComponents - //========================================================================= - Entity::ComponentArrayType FindDerivedComponents(const Entity* entity, const Uuid& typeId) - { - Entity::ComponentArrayType result; - for (AZ::Component* component : entity->GetComponents()) - { - if (azrtti_istypeof(typeId, component)) - { - result.push_back(component); - } - } - return result; - } - - Entity::ComponentArrayType FindDerivedComponents(EntityId entityId, const Uuid& typeId) - { - Entity* entity{}; - ComponentApplicationBus::BroadcastResult(entity, &ComponentApplicationRequests::FindEntity, entityId); - return entity ? FindDerivedComponents(entity, typeId) : Entity::ComponentArrayType(); - } - - bool EnumerateBaseRecursive(SerializeContext* context, const EnumerateBaseRecursiveVisitor& baseClassVisitor, const TypeId& typeToExamine) - { - AZ_Assert(context, "CheckDeclaresSerializeBaseClass called with no serialize context."); - if (!context) + // Stop iterating once we stop receiving SerializeContext::ClassData*. + if (!classData) { return false; } - AZStd::fixed_vector knownBaseClasses = { typeToExamine }; // avoid allocating heap here if possible. 64 types are 64*sizeof(Uuid) which is only 1k. - bool foundBaseClass = false; - auto enumerateBaseVisitor = [&baseClassVisitor, &knownBaseClasses](const AZ::SerializeContext::ClassData* classData, const TypeId& examineTypeId) - { - if (!classData) - { - return false; - } - - if (AZStd::find(knownBaseClasses.begin(), knownBaseClasses.end(), classData->m_typeId) == knownBaseClasses.end()) - { - if (knownBaseClasses.size() == 64) - { - // this should be pretty unlikely since a single class would have to have many other classes in its heirarchy - // and it'd all have to be basically in one layer, as we are popping as we explore. - AZ_WarningOnce("EntityUtils", false, "While trying to find a base class, all available slots were consumed. consider increasing the size of knownBaseClasses.\n"); - // we cannot continue any further, assume we did not find it. - return false; - } - knownBaseClasses.push_back(classData->m_typeId); - } - - return baseClassVisitor(classData, examineTypeId); - }; - - while (!knownBaseClasses.empty() && !foundBaseClass) - { - TypeId toExamine = knownBaseClasses.back(); - knownBaseClasses.pop_back(); - - context->EnumerateBase(enumerateBaseVisitor, toExamine); - } - - return foundBaseClass; - } - - bool CheckIfClassIsDeprecated(SerializeContext* context, const TypeId& typeToExamine) - { - bool isDeprecated = false; - auto classVisitorFn = [&isDeprecated](const AZ::SerializeContext::ClassData* classData, const TypeId& /*rttiBase*/) - { - // Stop iterating once we stop receiving SerializeContext::ClassData*. - if (!classData) - { - return false; - } - - // Stop iterating if we've found that the class is deprecated - if (classData->IsDeprecated()) - { - isDeprecated = true; - return false; - } - - return true; // keep iterating - }; - - // Check if the type is deprecated - const AZ::SerializeContext::ClassData* classData = context->FindClassData(typeToExamine); + // Stop iterating if we've found that the class is deprecated if (classData->IsDeprecated()) { - return true; - } - - // Check if any of its bases are deprecated - EnumerateBaseRecursive(context, classVisitorFn, typeToExamine); - - return isDeprecated; - } - - bool CheckDeclaresSerializeBaseClass(SerializeContext* context, const TypeId& typeToFind, const TypeId& typeToExamine) - { - AZ_Assert(context, "CheckDeclaresSerializeBaseClass called with no serialize context."); - if (!context) - { + isDeprecated = true; return false; } - bool foundBaseClass = false; - auto baseClassVisitorFn = [&typeToFind, &foundBaseClass](const AZ::SerializeContext::ClassData* reflectedBase, const TypeId& /*rttiBase*/) - { - if (!reflectedBase) - { - foundBaseClass = false; - return false; // stop iterating - } + return true; // keep iterating + }; - foundBaseClass = (reflectedBase->m_typeId == typeToFind); - if (foundBaseClass) - { - return false; // we have a base, stop iterating - } - - return true; // keep iterating - }; - - EnumerateBaseRecursive(context, baseClassVisitorFn, typeToExamine); - - return foundBaseClass; - } - - bool RemoveDuplicateServicesOfAndAfterIterator( - const ComponentDescriptor::DependencyArrayType::iterator& iterator, - ComponentDescriptor::DependencyArrayType& providedServiceArray, - const Entity* entity) + // Check if the type is deprecated + const AZ::SerializeContext::ClassData* classData = context->FindClassData(typeToExamine); + if (classData->IsDeprecated()) { - // Build types that strip out AZ_Warnings will complain that entity is unused without this. - (void)entity; - if (iterator == providedServiceArray.end()) - { - return false; - } - - bool duplicateFound = false; - - for (ComponentDescriptor::DependencyArrayType::iterator duplicateCheckIter = AZStd::next(iterator); - duplicateCheckIter != providedServiceArray.end();) - { - if (*iterator == *duplicateCheckIter) - { - AZ_Warning("Entity", false, "Duplicate service %d found on entity %s [%s]", - *duplicateCheckIter, - entity ? entity->GetName().c_str() : "Entity not provided", - entity ? entity->GetId().ToString().c_str() : ""); - duplicateCheckIter = providedServiceArray.erase(duplicateCheckIter); - duplicateFound = true; - } - else - { - ++duplicateCheckIter; - } - } - return duplicateFound; + return true; } - } // namespace EntityUtils -} // namespace AZ + + // Check if any of its bases are deprecated + EnumerateBaseRecursive(context, classVisitorFn, typeToExamine); + + return isDeprecated; + } + + bool CheckDeclaresSerializeBaseClass(SerializeContext* context, const TypeId& typeToFind, const TypeId& typeToExamine) + { + AZ_Assert(context, "CheckDeclaresSerializeBaseClass called with no serialize context."); + if (!context) + { + return false; + } + + bool foundBaseClass = false; + auto baseClassVisitorFn = [&typeToFind, &foundBaseClass](const AZ::SerializeContext::ClassData* reflectedBase, const TypeId& /*rttiBase*/) + { + if (!reflectedBase) + { + foundBaseClass = false; + return false; // stop iterating + } + + foundBaseClass = (reflectedBase->m_typeId == typeToFind); + if (foundBaseClass) + { + return false; // we have a base, stop iterating + } + + return true; // keep iterating + }; + + EnumerateBaseRecursive(context, baseClassVisitorFn, typeToExamine); + + return foundBaseClass; + } + + bool RemoveDuplicateServicesOfAndAfterIterator( + const ComponentDescriptor::DependencyArrayType::iterator& iterator, + ComponentDescriptor::DependencyArrayType& providedServiceArray, + const Entity* entity) + { + // Build types that strip out AZ_Warnings will complain that entity is unused without this. + (void)entity; + if (iterator == providedServiceArray.end()) + { + return false; + } + + bool duplicateFound = false; + + for (ComponentDescriptor::DependencyArrayType::iterator duplicateCheckIter = AZStd::next(iterator); + duplicateCheckIter != providedServiceArray.end();) + { + if (*iterator == *duplicateCheckIter) + { + AZ_Warning("Entity", false, "Duplicate service %d found on entity %s [%s]", + *duplicateCheckIter, + entity ? entity->GetName().c_str() : "Entity not provided", + entity ? entity->GetId().ToString().c_str() : ""); + duplicateCheckIter = providedServiceArray.erase(duplicateCheckIter); + duplicateFound = true; + } + else + { + ++duplicateCheckIter; + } + } + return duplicateFound; + } +} // namespace AZ::EntityUtils diff --git a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp index 9db59bc781..ecab62d123 100644 --- a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp +++ b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp @@ -57,6 +57,7 @@ void ZStd::StartCompressor(unsigned int compressionLevel) ZSTD_customMem customAlloc; customAlloc.customAlloc = reinterpret_cast(&AllocateMem); customAlloc.customFree = &FreeMem; + customAlloc.opaque = nullptr; AZ_UNUSED(compressionLevel); m_streamCompression = (ZSTD_createCStream_advanced(customAlloc)); diff --git a/Code/Framework/AzCore/AzCore/Debug/AssetTracking.cpp b/Code/Framework/AzCore/AzCore/Debug/AssetTracking.cpp index 3cb895c992..c03b75d118 100644 --- a/Code/Framework/AzCore/AzCore/Debug/AssetTracking.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/AssetTracking.cpp @@ -14,323 +14,313 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + namespace { - namespace + struct AssetTreeNode; + + // Per-thread data that needs to be stored. + struct ThreadData { - struct AssetTreeNode; - - // Per-thread data that needs to be stored. - struct ThreadData - { - AZStd::vector m_currentAssetStack; - }; - - // Access thread data through a virtual function to ensure that the same thread-local data is being shared across DLLs. - // Otherwise, the thread_local variables are replicated across DLLs that link the AzCore library, and you'll get a - // different version in each module. - class ThreadDataProvider - { - public: - virtual ThreadData& GetThreadData() = 0; - }; - } - - class AssetTrackingImpl final : - public ThreadDataProvider - { - public: - AZ_TYPE_INFO(AssetTrackingImpl, "{01E2A099-3523-40BE-80E0-E0ADD861BEE1}"); - AZ_CLASS_ALLOCATOR(AssetTrackingImpl, OSAllocator, 0); - - AssetTrackingImpl(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable); - ~AssetTrackingImpl(); - - void AssetBegin(const char* id, const char* file, int line); - void AssetAttach(void* otherAllocation, const char* file, int line); - void AssetEnd(); - - ThreadData& GetThreadData() override; - - private: - static EnvironmentVariable& GetEnvironmentVariable(); - static AssetTrackingImpl* GetSharedInstance(); - static ThreadData& GetSharedThreadData(); - - using PrimaryAssets = AZStd::unordered_map, AZStd::equal_to, AZStdAssetTrackingAllocator>; - using ThreadData = ThreadData; - using mutex_type = AZStd::mutex; - using lock_type = AZStd::lock_guard; - - mutex_type m_mutex; - PrimaryAssets m_primaryAssets; - AssetTreeNodeBase* m_assetRoot = nullptr; - AssetAllocationTableBase* m_allocationTable = nullptr; - bool m_performingAnalysis = false; - - friend class AssetTracking; - friend class AssetTracking::Scope; + AZStd::vector m_currentAssetStack; }; + // Access thread data through a virtual function to ensure that the same thread-local data is being shared across DLLs. + // Otherwise, the thread_local variables are replicated across DLLs that link the AzCore library, and you'll get a + // different version in each module. + class ThreadDataProvider + { + public: + virtual ThreadData& GetThreadData() = 0; + }; } -} + + class AssetTrackingImpl final : + public ThreadDataProvider + { + public: + AZ_TYPE_INFO(AssetTrackingImpl, "{01E2A099-3523-40BE-80E0-E0ADD861BEE1}"); + AZ_CLASS_ALLOCATOR(AssetTrackingImpl, OSAllocator, 0); + + AssetTrackingImpl(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable); + ~AssetTrackingImpl(); + + void AssetBegin(const char* id, const char* file, int line); + void AssetAttach(void* otherAllocation, const char* file, int line); + void AssetEnd(); + + ThreadData& GetThreadData() override; + + private: + static EnvironmentVariable& GetEnvironmentVariable(); + static AssetTrackingImpl* GetSharedInstance(); + static ThreadData& GetSharedThreadData(); + + using PrimaryAssets = AZStd::unordered_map, AZStd::equal_to, AZStdAssetTrackingAllocator>; + using ThreadData = ThreadData; + using mutex_type = AZStd::mutex; + using lock_type = AZStd::lock_guard; + + mutex_type m_mutex; + PrimaryAssets m_primaryAssets; + AssetTreeNodeBase* m_assetRoot = nullptr; + AssetAllocationTableBase* m_allocationTable = nullptr; + bool m_performingAnalysis = false; + + friend class AssetTracking; + friend class AssetTracking::Scope; + }; + /////////////////////////////////////////////////////////////////////////////// // AssetTrackingImpl methods /////////////////////////////////////////////////////////////////////////////// -namespace AZ -{ - namespace Debug + AssetTrackingImpl::AssetTrackingImpl(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable) : + m_assetRoot(&assetTree->GetRoot()), + m_allocationTable(allocationTable) { - AssetTrackingImpl::AssetTrackingImpl(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable) : - m_assetRoot(&assetTree->GetRoot()), - m_allocationTable(allocationTable) - { - AZ_Assert(!GetSharedInstance(), "Only one AssetTrackingImpl can exist!"); + AZ_Assert(!GetSharedInstance(), "Only one AssetTrackingImpl can exist!"); - GetEnvironmentVariable().Set(this); - AllocatorManager::Instance().EnterProfilingMode(); + GetEnvironmentVariable().Set(this); + AllocatorManager::Instance().EnterProfilingMode(); + } + + AssetTrackingImpl::~AssetTrackingImpl() + { + AllocatorManager::Instance().ExitProfilingMode(); + GetEnvironmentVariable().Reset(); + } + + void AssetTrackingImpl::AssetBegin(const char* id, const char* file, int line) + { + // In the future it may be desirable to organize assets based on where in code the asset was entered into. + // For now these are ignored. + AZ_UNUSED(file); + AZ_UNUSED(line); + + using namespace Internal; + + AssetTrackingId assetId(id); + auto& threadData = GetSharedThreadData(); + AssetTreeNodeBase* parentAsset = threadData.m_currentAssetStack.empty() ? nullptr : threadData.m_currentAssetStack.back(); + AssetTreeNodeBase* childAsset; + AssetPrimaryInfo* assetPrimaryInfo; + + if (!parentAsset) + { + parentAsset = m_assetRoot; } - AssetTrackingImpl::~AssetTrackingImpl() { - AllocatorManager::Instance().ExitProfilingMode(); - GetEnvironmentVariable().Reset(); - } + lock_type lock(m_mutex); - void AssetTrackingImpl::AssetBegin(const char* id, const char* file, int line) - { - // In the future it may be desirable to organize assets based on where in code the asset was entered into. - // For now these are ignored. - AZ_UNUSED(file); - AZ_UNUSED(line); + // Locate or create the primary record for this asset + auto primaryItr = m_primaryAssets.find(assetId); - using namespace Internal; - - AssetTrackingId assetId(id); - auto& threadData = GetSharedThreadData(); - AssetTreeNodeBase* parentAsset = threadData.m_currentAssetStack.empty() ? nullptr : threadData.m_currentAssetStack.back(); - AssetTreeNodeBase* childAsset; - AssetPrimaryInfo* assetPrimaryInfo; - - if (!parentAsset) + if (primaryItr != m_primaryAssets.end()) { - parentAsset = m_assetRoot; - } - - { - lock_type lock(m_mutex); - - // Locate or create the primary record for this asset - auto primaryItr = m_primaryAssets.find(assetId); - - if (primaryItr != m_primaryAssets.end()) - { - assetPrimaryInfo = &primaryItr->second; - } - else - { - auto insertResult = m_primaryAssets.emplace(assetId, AssetPrimaryInfo()); - assetPrimaryInfo = &insertResult.first->second; - assetPrimaryInfo->m_id = &insertResult.first->first; - } - - // Add this asset to the stack for this thread's context - childAsset = parentAsset->FindOrAddChild(assetId, assetPrimaryInfo); - } - - threadData.m_currentAssetStack.push_back(childAsset); - } - - void AssetTrackingImpl::AssetAttach(void* otherAllocation, const char* file, int line) - { - AZ_UNUSED(file); - AZ_UNUSED(line); - - using namespace Internal; - - AssetTreeNodeBase* assetInfo = m_allocationTable->FindAllocation(otherAllocation); - - // We will push back a nullptr if there is no asset, this is necessary to balance the call to AssetEnd() - GetSharedThreadData().m_currentAssetStack.push_back(assetInfo); - } - - void AssetTrackingImpl::AssetEnd() - { - AZ_Assert(!GetSharedThreadData().m_currentAssetStack.empty(), "AssetEnd() called without matching AssetBegin() or AssetAttach. Use the AZ_ASSET_NAMED_SCOPE and AZ_ASSET_ATTACH_TO_SCOPE macros to avoid this!"); - GetSharedThreadData().m_currentAssetStack.pop_back(); - } - - AssetTrackingImpl* AssetTrackingImpl::GetSharedInstance() - { - auto environmentVariable = GetEnvironmentVariable(); - - if(environmentVariable) - { - return *environmentVariable; - } - - return nullptr; - } - - ThreadData& AssetTrackingImpl::GetSharedThreadData() - { - // Cast to the base type so our virtual call doesn't get optimized away. We require GetThreadData() to be executed in the same DLL every time. - return static_cast(GetSharedInstance())->GetThreadData(); - } - - AssetTrackingImpl::ThreadData& AssetTrackingImpl::GetThreadData() - { - static thread_local ThreadData* data = nullptr; - static thread_local typename AZStd::aligned_storage_t storage; - - if (!data) - { - data = new (&storage) ThreadData; - } - - return *data; - } - - EnvironmentVariable& AssetTrackingImpl::GetEnvironmentVariable() - { - static EnvironmentVariable assetTrackingImpl = Environment::CreateVariable(AzTypeInfo::Name()); - - return assetTrackingImpl; - } - - /////////////////////////////////////////////////////////////////////////////// - // AssetTracking::Scope functions - /////////////////////////////////////////////////////////////////////////////// - - AssetTracking::Scope AssetTracking::Scope::ScopeFromAssetId(const char* file, int line, const char* fmt, ...) - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - static const int BUFFER_SIZE = 1024; - - char buffer[BUFFER_SIZE]; - va_list args; - va_start(args, fmt); - azvsnprintf(buffer, BUFFER_SIZE, fmt, args); - va_end(args); - - impl->AssetBegin(buffer, file, line); - } - - return Scope(); - } - - AssetTracking::Scope AssetTracking::Scope::ScopeFromAttachment(void* attachTo, const char* file, int line) - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - impl->AssetAttach(attachTo, file, line); - } - - return Scope(); - } - - AssetTracking::Scope::~Scope() - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - impl->AssetEnd(); - } - } - - AssetTracking::Scope::Scope() - { - } - - /////////////////////////////////////////////////////////////////////////////// - // AssetTracking functions - /////////////////////////////////////////////////////////////////////////////// - - void AssetTracking::EnterScopeByAssetId(const char* file, int line, const char* fmt, ...) - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - static const int BUFFER_SIZE = 1024; - - char buffer[BUFFER_SIZE]; - va_list args; - va_start(args, fmt); - azvsnprintf(buffer, BUFFER_SIZE, fmt, args); - va_end(args); - - impl->AssetBegin(buffer, file, line); - } - } - - void AssetTracking::EnterScopeByAttachment(void* attachTo, const char* file, int line) - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - impl->AssetAttach(attachTo, file, line); - } - } - - void AssetTracking::ExitScope() - { - if (auto impl = AssetTrackingImpl::GetSharedInstance()) - { - impl->AssetEnd(); - } - } - - const char* AssetTracking::GetDebugScope() - { - // Output debug information about the current asset scope in the current thread. - // Do not use in production code. -#ifndef RELEASE - static const int BUFFER_SIZE = 1024; - static char buffer[BUFFER_SIZE]; - const auto& assetStack = AssetTrackingImpl::GetSharedInstance()->GetThreadData().m_currentAssetStack; - - if (assetStack.empty()) - { - azsnprintf(buffer, BUFFER_SIZE, ""); + assetPrimaryInfo = &primaryItr->second; } else { - char* pos = buffer; - for (auto itr = assetStack.rbegin(); itr != assetStack.rend(); ++itr) - { - pos += azsnprintf(pos, BUFFER_SIZE - (pos - buffer), "%s\n", (*itr)->GetAssetPrimaryInfo()->m_id->m_id.c_str()); - - if (pos >= buffer + BUFFER_SIZE) - { - break; - } - } + auto insertResult = m_primaryAssets.emplace(assetId, AssetPrimaryInfo()); + assetPrimaryInfo = &insertResult.first->second; + assetPrimaryInfo->m_id = &insertResult.first->first; } - return buffer; -#else - return ""; -#endif + // Add this asset to the stack for this thread's context + childAsset = parentAsset->FindOrAddChild(assetId, assetPrimaryInfo); } - AssetTracking::AssetTracking(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable) + threadData.m_currentAssetStack.push_back(childAsset); + } + + void AssetTrackingImpl::AssetAttach(void* otherAllocation, const char* file, int line) + { + AZ_UNUSED(file); + AZ_UNUSED(line); + + using namespace Internal; + + AssetTreeNodeBase* assetInfo = m_allocationTable->FindAllocation(otherAllocation); + + // We will push back a nullptr if there is no asset, this is necessary to balance the call to AssetEnd() + GetSharedThreadData().m_currentAssetStack.push_back(assetInfo); + } + + void AssetTrackingImpl::AssetEnd() + { + AZ_Assert(!GetSharedThreadData().m_currentAssetStack.empty(), "AssetEnd() called without matching AssetBegin() or AssetAttach. Use the AZ_ASSET_NAMED_SCOPE and AZ_ASSET_ATTACH_TO_SCOPE macros to avoid this!"); + GetSharedThreadData().m_currentAssetStack.pop_back(); + } + + AssetTrackingImpl* AssetTrackingImpl::GetSharedInstance() + { + auto environmentVariable = GetEnvironmentVariable(); + + if(environmentVariable) { - m_impl.reset(aznew AssetTrackingImpl(assetTree, allocationTable)); + return *environmentVariable; } - AssetTracking::~AssetTracking() + return nullptr; + } + + ThreadData& AssetTrackingImpl::GetSharedThreadData() + { + // Cast to the base type so our virtual call doesn't get optimized away. We require GetThreadData() to be executed in the same DLL every time. + return static_cast(GetSharedInstance())->GetThreadData(); + } + + AssetTrackingImpl::ThreadData& AssetTrackingImpl::GetThreadData() + { + static thread_local ThreadData* data = nullptr; + static thread_local typename AZStd::aligned_storage_t storage; + + if (!data) { + data = new (&storage) ThreadData; } - AssetTreeNodeBase* AssetTracking::GetCurrentThreadAsset() const - { - const auto& assetStack = m_impl->GetThreadData().m_currentAssetStack; - AssetTreeNodeBase* result = assetStack.empty() ? nullptr : assetStack.back(); + return *data; + } - return result; + EnvironmentVariable& AssetTrackingImpl::GetEnvironmentVariable() + { + static EnvironmentVariable assetTrackingImpl = Environment::CreateVariable(AzTypeInfo::Name()); + + return assetTrackingImpl; + } + + /////////////////////////////////////////////////////////////////////////////// + // AssetTracking::Scope functions + /////////////////////////////////////////////////////////////////////////////// + + AssetTracking::Scope AssetTracking::Scope::ScopeFromAssetId(const char* file, int line, const char* fmt, ...) + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + static const int BUFFER_SIZE = 1024; + + char buffer[BUFFER_SIZE]; + va_list args; + va_start(args, fmt); + azvsnprintf(buffer, BUFFER_SIZE, fmt, args); + va_end(args); + + impl->AssetBegin(buffer, file, line); + } + + return Scope(); + } + + AssetTracking::Scope AssetTracking::Scope::ScopeFromAttachment(void* attachTo, const char* file, int line) + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + impl->AssetAttach(attachTo, file, line); + } + + return Scope(); + } + + AssetTracking::Scope::~Scope() + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + impl->AssetEnd(); } } + AssetTracking::Scope::Scope() + { + } + + /////////////////////////////////////////////////////////////////////////////// + // AssetTracking functions + /////////////////////////////////////////////////////////////////////////////// + + void AssetTracking::EnterScopeByAssetId(const char* file, int line, const char* fmt, ...) + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + static const int BUFFER_SIZE = 1024; + + char buffer[BUFFER_SIZE]; + va_list args; + va_start(args, fmt); + azvsnprintf(buffer, BUFFER_SIZE, fmt, args); + va_end(args); + + impl->AssetBegin(buffer, file, line); + } + } + + void AssetTracking::EnterScopeByAttachment(void* attachTo, const char* file, int line) + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + impl->AssetAttach(attachTo, file, line); + } + } + + void AssetTracking::ExitScope() + { + if (auto impl = AssetTrackingImpl::GetSharedInstance()) + { + impl->AssetEnd(); + } + } + + const char* AssetTracking::GetDebugScope() + { + // Output debug information about the current asset scope in the current thread. + // Do not use in production code. +#ifndef RELEASE + static const int BUFFER_SIZE = 1024; + static char buffer[BUFFER_SIZE]; + const auto& assetStack = AssetTrackingImpl::GetSharedInstance()->GetThreadData().m_currentAssetStack; + + if (assetStack.empty()) + { + azsnprintf(buffer, BUFFER_SIZE, ""); + } + else + { + char* pos = buffer; + for (auto itr = assetStack.rbegin(); itr != assetStack.rend(); ++itr) + { + pos += azsnprintf(pos, BUFFER_SIZE - (pos - buffer), "%s\n", (*itr)->GetAssetPrimaryInfo()->m_id->m_id.c_str()); + + if (pos >= buffer + BUFFER_SIZE) + { + break; + } + } + } + + return buffer; +#else + return ""; +#endif + } + + AssetTracking::AssetTracking(AssetTreeBase* assetTree, AssetAllocationTableBase* allocationTable) + { + m_impl.reset(aznew AssetTrackingImpl(assetTree, allocationTable)); + } + + AssetTracking::~AssetTracking() + { + } + + AssetTreeNodeBase* AssetTracking::GetCurrentThreadAsset() const + { + const auto& assetStack = m_impl->GetThreadData().m_currentAssetStack; + AssetTreeNodeBase* result = assetStack.empty() ? nullptr : assetStack.back(); + + return result; + } } // namespace AzFramework diff --git a/Code/Framework/AzCore/AzCore/Debug/EventTrace.cpp b/Code/Framework/AzCore/AzCore/Debug/EventTrace.cpp index 88a78de031..58fa1f6cca 100644 --- a/Code/Framework/AzCore/AzCore/Debug/EventTrace.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/EventTrace.cpp @@ -11,19 +11,19 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + EventTrace::ScopedSlice::ScopedSlice(const char* name, const char* category) + : m_Name(name) + , m_Category(category) + , m_Time(AZStd::GetTimeNowMicroSecond()) { - EventTrace::ScopedSlice::ScopedSlice(const char* name, const char* category) - : m_Name(name) - , m_Category(category) - , m_Time(AZStd::GetTimeNowMicroSecond()) - {} - - EventTrace::ScopedSlice::~ScopedSlice() - { - EventTraceDrillerBus::TryQueueBroadcast(&EventTraceDrillerInterface::RecordSlice, m_Name, m_Category, AZStd::this_thread::get_id(), m_Time, (uint32_t)(AZStd::GetTimeNowMicroSecond() - m_Time)); - } } -} + + EventTrace::ScopedSlice::~ScopedSlice() + { + EventTraceDrillerBus::TryQueueBroadcast( + &EventTraceDrillerInterface::RecordSlice, m_Name, m_Category, AZStd::this_thread::get_id(), m_Time, + (uint32_t)(AZStd::GetTimeNowMicroSecond() - m_Time)); + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Debug/EventTraceDriller.cpp b/Code/Framework/AzCore/AzCore/Debug/EventTraceDriller.cpp index 658021b018..d0722616f3 100644 --- a/Code/Framework/AzCore/AzCore/Debug/EventTraceDriller.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/EventTraceDriller.cpp @@ -11,151 +11,149 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + namespace Crc { - namespace Crc + constexpr u32 EventTraceDriller = AZ_CRC_CE("EventTraceDriller"); + constexpr u32 Slice = AZ_CRC_CE("Slice"); + constexpr u32 ThreadInfo = AZ_CRC_CE("ThreadInfo"); + constexpr u32 Name = AZ_CRC_CE("Name"); + constexpr u32 Category = AZ_CRC_CE("Category"); + constexpr u32 ThreadId = AZ_CRC_CE("ThreadId"); + constexpr u32 Timestamp = AZ_CRC_CE("Timestamp"); + constexpr u32 Duration = AZ_CRC_CE("Duration"); + constexpr u32 Instant = AZ_CRC_CE("Instant"); + } + + EventTraceDriller::EventTraceDriller() + { + EventTraceDrillerSetupBus::Handler::BusConnect(); + AZStd::ThreadDrillerEventBus::Handler::BusConnect(); + } + + EventTraceDriller::~EventTraceDriller() + { + AZStd::ThreadDrillerEventBus::Handler::BusDisconnect(); + EventTraceDrillerSetupBus::Handler::BusDisconnect(); + } + + void EventTraceDriller::Start(const Param* params, int numParams) + { + (void)params; + (void)numParams; + + EventTraceDrillerBus::Handler::BusConnect(); + TickBus::Handler::BusConnect(); + + EventTraceDrillerBus::AllowFunctionQueuing(true); + } + + void EventTraceDriller::Stop() + { + EventTraceDrillerBus::AllowFunctionQueuing(false); + EventTraceDrillerBus::ClearQueuedEvents(); + + EventTraceDrillerBus::Handler::BusDisconnect(); + TickBus::Handler::BusDisconnect(); + } + + void EventTraceDriller::OnTick(float deltaTime, ScriptTimePoint time) + { + (void)deltaTime; + (void)time; + + AZ_TRACE_METHOD(); + RecordThreads(); + EventTraceDrillerBus::ExecuteQueuedEvents(); + } + + void EventTraceDriller::SetThreadName(const AZStd::thread_id& id, const char* name) + { + AZStd::lock_guard lock(m_ThreadMutex); + m_Threads[(size_t)id.m_id] = ThreadData{ name }; + } + + void EventTraceDriller::OnThreadEnter(const AZStd::thread::id& id, const AZStd::thread_desc* desc) + { + if (desc && desc->m_name) { - const u32 EventTraceDriller = AZ_CRC("EventTraceDriller", 0xf7aeae55); - const u32 Slice = AZ_CRC("Slice", 0x3dae78a5); - const u32 ThreadInfo = AZ_CRC("ThreadInfo", 0x89bf78be); - const u32 Name = AZ_CRC("Name", 0x5e237e06); - const u32 Category = AZ_CRC("Category", 0x064c19c1); - const u32 ThreadId = AZ_CRC("ThreadId", 0xd0fd9043); - const u32 Timestamp = AZ_CRC("Timestamp", 0xa5d6e63e); - const u32 Duration = AZ_CRC("Duration", 0x865f80c0); - const u32 Instant = AZ_CRC("Instant", 0x0e9047ad); + SetThreadName(id, desc->m_name); } + } - EventTraceDriller::EventTraceDriller() + void EventTraceDriller::OnThreadExit(const AZStd::thread::id& id) + { + AZStd::lock_guard lock(m_ThreadMutex); + m_Threads.erase((size_t)id.m_id); + } + + void EventTraceDriller::RecordThreads() + { + if (!m_output || m_Threads.empty()) { - EventTraceDrillerSetupBus::Handler::BusConnect(); - AZStd::ThreadDrillerEventBus::Handler::BusConnect(); + return; } + // Main bus mutex guards m_output. + auto& context = EventTraceDrillerBus::GetOrCreateContext(); - EventTraceDriller::~EventTraceDriller() - { - AZStd::ThreadDrillerEventBus::Handler::BusDisconnect(); - EventTraceDrillerSetupBus::Handler::BusDisconnect(); - } - - void EventTraceDriller::Start(const Param* params, int numParams) - { - (void)params; - (void)numParams; - - EventTraceDrillerBus::Handler::BusConnect(); - TickBus::Handler::BusConnect(); - - EventTraceDrillerBus::AllowFunctionQueuing(true); - } - - void EventTraceDriller::Stop() - { - EventTraceDrillerBus::AllowFunctionQueuing(false); - EventTraceDrillerBus::ClearQueuedEvents(); - - EventTraceDrillerBus::Handler::BusDisconnect(); - TickBus::Handler::BusDisconnect(); - } - - void EventTraceDriller::OnTick(float deltaTime, ScriptTimePoint time) - { - (void)deltaTime; - (void)time; - - AZ_TRACE_METHOD(); - RecordThreads(); - EventTraceDrillerBus::ExecuteQueuedEvents(); - } - - void EventTraceDriller::SetThreadName(const AZStd::thread_id& id, const char* name) - { - AZStd::lock_guard lock(m_ThreadMutex); - m_Threads[(size_t)id.m_id] = ThreadData{ name }; - } - - void EventTraceDriller::OnThreadEnter(const AZStd::thread::id& id, const AZStd::thread_desc* desc) - { - if (desc && desc->m_name) - { - SetThreadName(id, desc->m_name); - } - } - - void EventTraceDriller::OnThreadExit(const AZStd::thread::id& id) - { - AZStd::lock_guard lock(m_ThreadMutex); - m_Threads.erase((size_t)id.m_id); - } - - void EventTraceDriller::RecordThreads() - { - if (m_output && m_Threads.size()) - { - // Main bus mutex guards m_output. - auto& context = EventTraceDrillerBus::GetOrCreateContext(); - - AZStd::scoped_lock lock(context.m_contextMutex, m_ThreadMutex); - for (const auto& keyValue : m_Threads) - { - m_output->BeginTag(Crc::EventTraceDriller); - m_output->BeginTag(Crc::ThreadInfo); - m_output->Write(Crc::ThreadId, keyValue.first); - m_output->Write(Crc::Name, keyValue.second.name); - m_output->EndTag(Crc::ThreadInfo); - m_output->EndTag(Crc::EventTraceDriller); - } - } - } - - void EventTraceDriller::RecordSlice( - const char* name, - const char* category, - const AZStd::thread_id threadId, - AZ::u64 timestamp, - AZ::u32 duration) + AZStd::scoped_lock lock(context.m_contextMutex, m_ThreadMutex); + for (const auto& keyValue : m_Threads) { m_output->BeginTag(Crc::EventTraceDriller); - m_output->BeginTag(Crc::Slice); - m_output->Write(Crc::Name, name); - m_output->Write(Crc::Category, category); - m_output->Write(Crc::ThreadId, (size_t)threadId.m_id); - m_output->Write(Crc::Timestamp, timestamp); - m_output->Write(Crc::Duration, std::max(duration, 1u)); - m_output->EndTag(Crc::Slice); - m_output->EndTag(Crc::EventTraceDriller); - } - - void EventTraceDriller::RecordInstantGlobal( - const char* name, - const char* category, - AZ::u64 timestamp) - { - m_output->BeginTag(Crc::EventTraceDriller); - m_output->BeginTag(Crc::Instant); - m_output->Write(Crc::Name, name); - m_output->Write(Crc::Category, category); - m_output->Write(Crc::Timestamp, timestamp); - m_output->EndTag(Crc::Instant); - m_output->EndTag(Crc::EventTraceDriller); - } - - void EventTraceDriller::RecordInstantThread( - const char* name, - const char* category, - const AZStd::thread_id threadId, - AZ::u64 timestamp) - { - m_output->BeginTag(Crc::EventTraceDriller); - m_output->BeginTag(Crc::Instant); - m_output->Write(Crc::Name, name); - m_output->Write(Crc::Category, category); - m_output->Write(Crc::ThreadId, (size_t)threadId.m_id); - m_output->Write(Crc::Timestamp, timestamp); - m_output->EndTag(Crc::Instant); + m_output->BeginTag(Crc::ThreadInfo); + m_output->Write(Crc::ThreadId, keyValue.first); + m_output->Write(Crc::Name, keyValue.second.name); + m_output->EndTag(Crc::ThreadInfo); m_output->EndTag(Crc::EventTraceDriller); } } -} + + void EventTraceDriller::RecordSlice( + const char* name, + const char* category, + const AZStd::thread_id threadId, + AZ::u64 timestamp, + AZ::u32 duration) + { + m_output->BeginTag(Crc::EventTraceDriller); + m_output->BeginTag(Crc::Slice); + m_output->Write(Crc::Name, name); + m_output->Write(Crc::Category, category); + m_output->Write(Crc::ThreadId, (size_t)threadId.m_id); + m_output->Write(Crc::Timestamp, timestamp); + m_output->Write(Crc::Duration, std::max(duration, 1u)); + m_output->EndTag(Crc::Slice); + m_output->EndTag(Crc::EventTraceDriller); + } + + void EventTraceDriller::RecordInstantGlobal( + const char* name, + const char* category, + AZ::u64 timestamp) + { + m_output->BeginTag(Crc::EventTraceDriller); + m_output->BeginTag(Crc::Instant); + m_output->Write(Crc::Name, name); + m_output->Write(Crc::Category, category); + m_output->Write(Crc::Timestamp, timestamp); + m_output->EndTag(Crc::Instant); + m_output->EndTag(Crc::EventTraceDriller); + } + + void EventTraceDriller::RecordInstantThread( + const char* name, + const char* category, + const AZStd::thread_id threadId, + AZ::u64 timestamp) + { + m_output->BeginTag(Crc::EventTraceDriller); + m_output->BeginTag(Crc::Instant); + m_output->Write(Crc::Name, name); + m_output->Write(Crc::Category, category); + m_output->Write(Crc::ThreadId, (size_t)threadId.m_id); + m_output->Write(Crc::Timestamp, timestamp); + m_output->EndTag(Crc::Instant); + m_output->EndTag(Crc::EventTraceDriller); + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Debug/Trace.cpp b/Code/Framework/AzCore/AzCore/Debug/Trace.cpp index b9e4003500..2c2b8215c0 100644 --- a/Code/Framework/AzCore/AzCore/Debug/Trace.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/Trace.cpp @@ -78,6 +78,7 @@ namespace AZ::Debug constexpr LogLevel DefaultLogLevel = LogLevel::Info; AZ_CVAR_SCOPED(int, bg_traceLogLevel, DefaultLogLevel, nullptr, ConsoleFunctorFlags::Null, "Enable trace message logging in release mode. 0=disabled, 1=errors, 2=warnings, 3=info."); + AZ_CVAR_SCOPED(bool, bg_alwaysShowCallstack, false, nullptr, ConsoleFunctorFlags::Null, "Force stack trace output without allowing ebus interception."); /** * If any listener returns true, store the result so we don't outputs detailed information. @@ -279,6 +280,13 @@ namespace AZ::Debug TraceMessageResult result; EBUS_EVENT_RESULT(result, TraceMessageBus, OnPreAssert, fileName, line, funcName, message); + + if (bg_alwaysShowCallstack) + { + // If we're always showing the callstack, print it now before there's any chance of an ebus handler interrupting + PrintCallstack(g_dbgSystemWnd, 1); + } + if (result.m_value) { g_alreadyHandlingAssertOrFatal = false; @@ -304,7 +312,10 @@ namespace AZ::Debug } Output(g_dbgSystemWnd, "------------------------------------------------\n"); - PrintCallstack(g_dbgSystemWnd, 1); + if (!bg_alwaysShowCallstack) + { + PrintCallstack(g_dbgSystemWnd, 1); + } Output(g_dbgSystemWnd, "==================================================================\n"); char dialogBoxText[g_maxMessageLength]; @@ -529,6 +540,16 @@ namespace AZ::Debug } } + RawOutput(window, message); + } + + void Trace::RawOutput(const char* window, const char* message) + { + if (!window) + { + window = g_dbgSystemWnd; + } + // printf on Windows platforms seem to have a buffer length limit of 4096 characters // Therefore fwrite is used directly to write the window and message to stdout AZStd::string_view windowView{ window }; @@ -572,9 +593,19 @@ namespace AZ::Debug } azstrcat(lines[i], AZ_ARRAY_SIZE(lines[i]), "\n"); + // Use Output instead of AZ_Printf to be consistent with the exception output code and avoid // this accidentally being suppressed as a normal message - Output(window, lines[i]); + + if (bg_alwaysShowCallstack) + { + // Use Raw Output as this cannot be suppressed + RawOutput(window, lines[i]); + } + else + { + Output(window, lines[i]); + } } } } diff --git a/Code/Framework/AzCore/AzCore/Debug/Trace.h b/Code/Framework/AzCore/AzCore/Debug/Trace.h index 507ba48e53..fe33bf1b9c 100644 --- a/Code/Framework/AzCore/AzCore/Debug/Trace.h +++ b/Code/Framework/AzCore/AzCore/Debug/Trace.h @@ -73,6 +73,9 @@ namespace AZ static void Output(const char* window, const char* message); + /// Called by output to handle the actual output, does not interact with ebus or allow interception + static void RawOutput(const char* window, const char* message); + static void PrintCallstack(const char* window, unsigned int suppressCount = 0, void* nativeContext = 0); /// PEXCEPTION_POINTERS on Windows, always NULL on other platforms diff --git a/Code/Framework/AzCore/AzCore/Debug/TraceMessagesDriller.cpp b/Code/Framework/AzCore/AzCore/Debug/TraceMessagesDriller.cpp index 7e9e5b146e..dbe66838a4 100644 --- a/Code/Framework/AzCore/AzCore/Debug/TraceMessagesDriller.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/TraceMessagesDriller.cpp @@ -9,94 +9,91 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + //========================================================================= + // Start + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::Start(const Param* params, int numParams) { - //========================================================================= - // Start - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::Start(const Param* params, int numParams) - { - (void)params; - (void)numParams; - BusConnect(); - } + (void)params; + (void)numParams; + BusConnect(); + } - //========================================================================= - // Stop - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::Stop() - { - BusDisconnect(); - } + //========================================================================= + // Stop + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::Stop() + { + BusDisconnect(); + } - //========================================================================= - // OnAssert - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::OnAssert(const char* message) - { - // Not sure if we can really capture assert since the code will stop executing very soon. - m_output->BeginTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - m_output->Write(AZ_CRC("OnAssert", 0xb74db4ce), message); - m_output->EndTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - } + //========================================================================= + // OnAssert + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::OnAssert(const char* message) + { + // Not sure if we can really capture assert since the code will stop executing very soon. + m_output->BeginTag(AZ_CRC_CE("TraceMessagesDriller")); + m_output->Write(AZ_CRC_CE("OnAssert"), message); + m_output->EndTag(AZ_CRC_CE("TraceMessagesDriller")); + } - //========================================================================= - // OnException - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::OnException(const char* message) - { - // Not sure if we can really capture exception since the code will stop executing very soon. - m_output->BeginTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - m_output->Write(AZ_CRC("OnException", 0xfe457d12), message); - m_output->EndTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - } + //========================================================================= + // OnException + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::OnException(const char* message) + { + // Not sure if we can really capture exception since the code will stop executing very soon. + m_output->BeginTag(AZ_CRC_CE("TraceMessagesDriller")); + m_output->Write(AZ_CRC_CE("OnException"), message); + m_output->EndTag(AZ_CRC_CE("TraceMessagesDriller")); + } - //========================================================================= - // OnError - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::OnError(const char* window, const char* message) - { - m_output->BeginTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - m_output->BeginTag(AZ_CRC("OnError", 0x4993c634)); - m_output->Write(AZ_CRC("Window", 0x8be4f9dd), window); - m_output->Write(AZ_CRC("Message", 0xb6bd307f), message); - m_output->EndTag(AZ_CRC("OnError", 0x4993c634)); - m_output->EndTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - } + //========================================================================= + // OnError + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::OnError(const char* window, const char* message) + { + m_output->BeginTag(AZ_CRC_CE("TraceMessagesDriller")); + m_output->BeginTag(AZ_CRC_CE("OnError")); + m_output->Write(AZ_CRC_CE("Window"), window); + m_output->Write(AZ_CRC_CE("Message"), message); + m_output->EndTag(AZ_CRC_CE("OnError")); + m_output->EndTag(AZ_CRC_CE("TraceMessagesDriller")); + } - //========================================================================= - // OnWarning - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::OnWarning(const char* window, const char* message) - { - m_output->BeginTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - m_output->BeginTag(AZ_CRC("OnWarning", 0x7d90abea)); - m_output->Write(AZ_CRC("Window", 0x8be4f9dd), window); - m_output->Write(AZ_CRC("Message", 0xb6bd307f), message); - m_output->EndTag(AZ_CRC("OnWarning", 0x7d90abea)); - m_output->EndTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - } + //========================================================================= + // OnWarning + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::OnWarning(const char* window, const char* message) + { + m_output->BeginTag(AZ_CRC_CE("TraceMessagesDriller")); + m_output->BeginTag(AZ_CRC_CE("OnWarning")); + m_output->Write(AZ_CRC_CE("Window"), window); + m_output->Write(AZ_CRC_CE("Message"), message); + m_output->EndTag(AZ_CRC_CE("OnWarning")); + m_output->EndTag(AZ_CRC_CE("TraceMessagesDriller")); + } - //========================================================================= - // OnPrintf - // [2/6/2013] - //========================================================================= - void TraceMessagesDriller::OnPrintf(const char* window, const char* message) - { - m_output->BeginTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - m_output->BeginTag(AZ_CRC("OnPrintf", 0xd4b5c294)); - m_output->Write(AZ_CRC("Window", 0x8be4f9dd), window); - m_output->Write(AZ_CRC("Message", 0xb6bd307f), message); - m_output->EndTag(AZ_CRC("OnPrintf", 0xd4b5c294)); - m_output->EndTag(AZ_CRC("TraceMessagesDriller", 0xa61d1b00)); - } - } // namespace Debug + //========================================================================= + // OnPrintf + // [2/6/2013] + //========================================================================= + void TraceMessagesDriller::OnPrintf(const char* window, const char* message) + { + m_output->BeginTag(AZ_CRC_CE("TraceMessagesDriller")); + m_output->BeginTag(AZ_CRC_CE("OnPrintf")); + m_output->Write(AZ_CRC_CE("Window"), window); + m_output->Write(AZ_CRC_CE("Message"), message); + m_output->EndTag(AZ_CRC_CE("OnPrintf")); + m_output->EndTag(AZ_CRC_CE("TraceMessagesDriller")); + } } // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Debug/TraceReflection.cpp b/Code/Framework/AzCore/AzCore/Debug/TraceReflection.cpp index debbea5235..329a709994 100644 --- a/Code/Framework/AzCore/AzCore/Debug/TraceReflection.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/TraceReflection.cpp @@ -12,283 +12,280 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + //! Trace Message Event Handler for Automation. + //! Since TraceMessageBus will be called from multiple threads and + //! python interpreter is single threaded, all the bus calls are + //! queued into a list and called at the end of the frame in the main thread. + //! @note this class is not using the usual AZ_EBUS_BEHAVIOR_BINDER + //! macro as the signature needs to be changed to connect to Tick bus. + class TraceMessageBusHandler + : public AZ::Debug::TraceMessageBus::Handler + , public AZ::BehaviorEBusHandler + , public AZ::TickBus::Handler { - //! Trace Message Event Handler for Automation. - //! Since TraceMessageBus will be called from multiple threads and - //! python interpreter is single threaded, all the bus calls are - //! queued into a list and called at the end of the frame in the main thread. - //! @note this class is not using the usual AZ_EBUS_BEHAVIOR_BINDER - //! macro as the signature needs to be changed to connect to Tick bus. - class TraceMessageBusHandler - : public AZ::Debug::TraceMessageBus::Handler - , public AZ::BehaviorEBusHandler - , public AZ::TickBus::Handler + public: + AZ_CLASS_ALLOCATOR(TraceMessageBusHandler, AZ::SystemAllocator, 0); + AZ_RTTI(TraceMessageBusHandler, "{5CDBAF09-5EB0-48AC-B327-2AF8601BB550}", AZ::BehaviorEBusHandler); + + TraceMessageBusHandler(); + + using EventFunctionsParameterPack = AZStd::Internal::pack_traits_arg_sequence< + decltype(&TraceMessageBusHandler::OnPreAssert), + decltype(&TraceMessageBusHandler::OnPreError), + decltype(&TraceMessageBusHandler::OnPreWarning), + decltype(&TraceMessageBusHandler::OnAssert), + decltype(&TraceMessageBusHandler::OnError), + decltype(&TraceMessageBusHandler::OnWarning), + decltype(&TraceMessageBusHandler::OnException), + decltype(&TraceMessageBusHandler::OnPrintf), + decltype(&TraceMessageBusHandler::OnOutput) + >; + + enum { - public: - AZ_CLASS_ALLOCATOR(TraceMessageBusHandler, AZ::SystemAllocator, 0); - AZ_RTTI(TraceMessageBusHandler, "{5CDBAF09-5EB0-48AC-B327-2AF8601BB550}", AZ::BehaviorEBusHandler); - - TraceMessageBusHandler(); - - using EventFunctionsParameterPack = AZStd::Internal::pack_traits_arg_sequence< - decltype(&TraceMessageBusHandler::OnPreAssert), - decltype(&TraceMessageBusHandler::OnPreError), - decltype(&TraceMessageBusHandler::OnPreWarning), - decltype(&TraceMessageBusHandler::OnAssert), - decltype(&TraceMessageBusHandler::OnError), - decltype(&TraceMessageBusHandler::OnWarning), - decltype(&TraceMessageBusHandler::OnException), - decltype(&TraceMessageBusHandler::OnPrintf), - decltype(&TraceMessageBusHandler::OnOutput) - >; - - enum - { - FN_OnPreAssert = 0, - FN_OnPreError, - FN_OnPreWarning, - FN_OnAssert, - FN_OnError, - FN_OnWarning, - FN_OnException, - FN_OnPrintf, - FN_OnOutput, - FN_MAX - }; - - static inline constexpr const char* m_functionNames[FN_MAX] = - { - "OnPreAssert", - "OnPreError", - "OnPreWarning", - "OnAssert", - "OnError", - "OnWarning", - "OnException", - "OnPrintf", - "OnOutput" - }; - - // AZ::BehaviorEBusHandler overrides... - int GetFunctionIndex(const char* functionName) const override; - void Disconnect() override; - bool Connect(AZ::BehaviorValueParameter* id = nullptr) override; - bool IsConnected() override; - bool IsConnectedId(AZ::BehaviorValueParameter* id) override; - - // TraceMessageBus - /* - * Note: Since at editor runtime there is already have a handler, for automation (OnPreAssert, OnPreWarning, OnPreWarning) - * must be used instead of (OnAssert, OnWarning, OnError) - */ - bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override; - bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override; - bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override; - bool OnAssert(const char* message) override; - bool OnError(const char* window, const char* message) override; - bool OnWarning(const char* window, const char* message) override; - bool OnException(const char* message) override; - bool OnPrintf(const char* window, const char* message) override; - bool OnOutput(const char* window, const char* message) override; - - // AZ::TickBus::Handler overrides ... - void OnTick(float deltaTime, AZ::ScriptTimePoint time) override; - int GetTickOrder() override; - - private: - void QueueMessageCall(AZStd::function messageCall); - void FlushMessageCalls(); - - AZStd::list> m_messageCalls; - AZStd::mutex m_messageCallsLock; + FN_OnPreAssert = 0, + FN_OnPreError, + FN_OnPreWarning, + FN_OnAssert, + FN_OnError, + FN_OnWarning, + FN_OnException, + FN_OnPrintf, + FN_OnOutput, + FN_MAX }; - TraceMessageBusHandler::TraceMessageBusHandler() + static inline constexpr const char* m_functionNames[FN_MAX] = { - m_events.resize(FN_MAX); + "OnPreAssert", + "OnPreError", + "OnPreWarning", + "OnAssert", + "OnError", + "OnWarning", + "OnException", + "OnPrintf", + "OnOutput" + }; - SetEvent(&TraceMessageBusHandler::OnPreAssert, m_functionNames[FN_OnPreAssert]); - SetEvent(&TraceMessageBusHandler::OnPreError, m_functionNames[FN_OnPreError]); - SetEvent(&TraceMessageBusHandler::OnPreWarning, m_functionNames[FN_OnPreWarning]); - SetEvent(&TraceMessageBusHandler::OnAssert, m_functionNames[FN_OnAssert]); - SetEvent(&TraceMessageBusHandler::OnError, m_functionNames[FN_OnError]); - SetEvent(&TraceMessageBusHandler::OnWarning, m_functionNames[FN_OnWarning]); - SetEvent(&TraceMessageBusHandler::OnException, m_functionNames[FN_OnException]); - SetEvent(&TraceMessageBusHandler::OnPrintf, m_functionNames[FN_OnPrintf]); - SetEvent(&TraceMessageBusHandler::OnOutput, m_functionNames[FN_OnOutput]); - } + // AZ::BehaviorEBusHandler overrides... + int GetFunctionIndex(const char* functionName) const override; + void Disconnect() override; + bool Connect(AZ::BehaviorValueParameter* id = nullptr) override; + bool IsConnected() override; + bool IsConnectedId(AZ::BehaviorValueParameter* id) override; - int TraceMessageBusHandler::GetFunctionIndex(const char* functionName) const + // TraceMessageBus + /* + * Note: Since at editor runtime there is already have a handler, for automation (OnPreAssert, OnPreWarning, OnPreWarning) + * must be used instead of (OnAssert, OnWarning, OnError) + */ + bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override; + bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override; + bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override; + bool OnAssert(const char* message) override; + bool OnError(const char* window, const char* message) override; + bool OnWarning(const char* window, const char* message) override; + bool OnException(const char* message) override; + bool OnPrintf(const char* window, const char* message) override; + bool OnOutput(const char* window, const char* message) override; + + // AZ::TickBus::Handler overrides ... + void OnTick(float deltaTime, AZ::ScriptTimePoint time) override; + int GetTickOrder() override; + + private: + void QueueMessageCall(AZStd::function messageCall); + void FlushMessageCalls(); + + AZStd::list> m_messageCalls; + AZStd::mutex m_messageCallsLock; + }; + + TraceMessageBusHandler::TraceMessageBusHandler() + { + m_events.resize(FN_MAX); + + SetEvent(&TraceMessageBusHandler::OnPreAssert, m_functionNames[FN_OnPreAssert]); + SetEvent(&TraceMessageBusHandler::OnPreError, m_functionNames[FN_OnPreError]); + SetEvent(&TraceMessageBusHandler::OnPreWarning, m_functionNames[FN_OnPreWarning]); + SetEvent(&TraceMessageBusHandler::OnAssert, m_functionNames[FN_OnAssert]); + SetEvent(&TraceMessageBusHandler::OnError, m_functionNames[FN_OnError]); + SetEvent(&TraceMessageBusHandler::OnWarning, m_functionNames[FN_OnWarning]); + SetEvent(&TraceMessageBusHandler::OnException, m_functionNames[FN_OnException]); + SetEvent(&TraceMessageBusHandler::OnPrintf, m_functionNames[FN_OnPrintf]); + SetEvent(&TraceMessageBusHandler::OnOutput, m_functionNames[FN_OnOutput]); + } + + int TraceMessageBusHandler::GetFunctionIndex(const char* functionName) const + { + for (int i = 0; i < FN_MAX; ++i) { - for (int i = 0; i < FN_MAX; ++i) + if (azstricmp(functionName, m_functionNames[i]) == 0) { - if (azstricmp(functionName, m_functionNames[i]) == 0) - { - return i; - } + return i; } - return -1; } + return -1; + } - void TraceMessageBusHandler::Disconnect() + void TraceMessageBusHandler::Disconnect() + { + AZ::Debug::TraceMessageBus::Handler::BusDisconnect(); + AZ::TickBus::Handler::BusDisconnect(); + } + + bool TraceMessageBusHandler::Connect(AZ::BehaviorValueParameter* id) + { + AZ::TickBus::Handler::BusConnect(); + return AZ::Internal::EBusConnector::Connect(this, id); + } + + bool TraceMessageBusHandler::IsConnected() + { + return AZ::Internal::EBusConnector::IsConnected(this); + } + + bool TraceMessageBusHandler::IsConnectedId(AZ::BehaviorValueParameter* id) + { + return AZ::Internal::EBusConnector::IsConnectedId(this, id); + } + + ////////////////////////////////////////////////////////////////////////// + // TraceMessageBusHandler Implementation + inline bool TraceMessageBusHandler::OnPreAssert(const char* fileName, int line, const char* func, const char* message) + { + QueueMessageCall( + [this, fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() { - AZ::Debug::TraceMessageBus::Handler::BusDisconnect(); - AZ::TickBus::Handler::BusDisconnect(); - } + Call(FN_OnPreAssert, fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); + }); + return false; + } - bool TraceMessageBusHandler::Connect(AZ::BehaviorValueParameter* id) + inline bool TraceMessageBusHandler::OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() { - AZ::TickBus::Handler::BusConnect(); - return AZ::Internal::EBusConnector::Connect(this, id); - } + Call(FN_OnPreError, windowString.c_str(), fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); + }); + return false; + } - bool TraceMessageBusHandler::IsConnected() + inline bool TraceMessageBusHandler::OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() { - return AZ::Internal::EBusConnector::IsConnected(this); - } + return Call(FN_OnPreWarning, windowString.c_str(), fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); + }); + return false; + } - bool TraceMessageBusHandler::IsConnectedId(AZ::BehaviorValueParameter* id) + inline bool TraceMessageBusHandler::OnAssert(const char* message) + { + QueueMessageCall( + [this, messageString = AZStd::string(message)]() { - return AZ::Internal::EBusConnector::IsConnectedId(this, id); - } + return Call(FN_OnAssert, messageString.c_str()); + }); + return false; + } - ////////////////////////////////////////////////////////////////////////// - // TraceMessageBusHandler Implementation - inline bool TraceMessageBusHandler::OnPreAssert(const char* fileName, int line, const char* func, const char* message) + inline bool TraceMessageBusHandler::OnError(const char* window, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() { - QueueMessageCall( - [this, fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() - { - Call(FN_OnPreAssert, fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); - }); - return false; - } + return Call(FN_OnError, windowString.c_str(), messageString.c_str()); + }); + return false; + } - inline bool TraceMessageBusHandler::OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) + inline bool TraceMessageBusHandler::OnWarning(const char* window, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() { - QueueMessageCall( - [this, windowString = AZStd::string(window), fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() - { - Call(FN_OnPreError, windowString.c_str(), fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); - }); - return false; - } + return Call(FN_OnWarning, windowString.c_str(), messageString.c_str()); + }); + return false; + } - inline bool TraceMessageBusHandler::OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) + inline bool TraceMessageBusHandler::OnException(const char* message) + { + QueueMessageCall( + [this, messageString = AZStd::string(message)]() { - QueueMessageCall( - [this, windowString = AZStd::string(window), fileNameString = AZStd::string(fileName), line, funcString = AZStd::string(func), messageString = AZStd::string(message)]() - { - return Call(FN_OnPreWarning, windowString.c_str(), fileNameString.c_str(), line, funcString.c_str(), messageString.c_str()); - }); - return false; - } + return Call(FN_OnException, messageString.c_str()); + }); + return false; + } - inline bool TraceMessageBusHandler::OnAssert(const char* message) + inline bool TraceMessageBusHandler::OnPrintf(const char* window, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() { - QueueMessageCall( - [this, messageString = AZStd::string(message)]() - { - return Call(FN_OnAssert, messageString.c_str()); - }); - return false; - } + return Call(FN_OnPrintf, windowString.c_str(), messageString.c_str()); + }); + return false; + } - inline bool TraceMessageBusHandler::OnError(const char* window, const char* message) + inline bool TraceMessageBusHandler::OnOutput(const char* window, const char* message) + { + QueueMessageCall( + [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() { - QueueMessageCall( - [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() - { - return Call(FN_OnError, windowString.c_str(), messageString.c_str()); - }); - return false; - } + return Call(FN_OnOutput, windowString.c_str(), messageString.c_str()); + }); + return false; + } - inline bool TraceMessageBusHandler::OnWarning(const char* window, const char* message) - { - QueueMessageCall( - [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() - { - return Call(FN_OnWarning, windowString.c_str(), messageString.c_str()); - }); - return false; - } + void TraceMessageBusHandler::OnTick( + [[maybe_unused]] float deltaTime, + [[maybe_unused]] AZ::ScriptTimePoint time) + { + FlushMessageCalls(); + } - inline bool TraceMessageBusHandler::OnException(const char* message) - { - QueueMessageCall( - [this, messageString = AZStd::string(message)]() - { - return Call(FN_OnException, messageString.c_str()); - }); - return false; - } + int TraceMessageBusHandler::GetTickOrder() + { + return AZ::TICK_LAST; + } - inline bool TraceMessageBusHandler::OnPrintf(const char* window, const char* message) - { - QueueMessageCall( - [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() - { - return Call(FN_OnPrintf, windowString.c_str(), messageString.c_str()); - }); - return false; - } + void TraceMessageBusHandler::QueueMessageCall(AZStd::function messageCall) + { + AZStd::lock_guard lock(m_messageCallsLock); + m_messageCalls.emplace_back(messageCall); + } - inline bool TraceMessageBusHandler::OnOutput(const char* window, const char* message) - { - QueueMessageCall( - [this, windowString = AZStd::string(window), messageString = AZStd::string(message)]() - { - return Call(FN_OnOutput, windowString.c_str(), messageString.c_str()); - }); - return false; - } - - void TraceMessageBusHandler::OnTick( - [[maybe_unused]] float deltaTime, - [[maybe_unused]] AZ::ScriptTimePoint time) - { - FlushMessageCalls(); - } - - int TraceMessageBusHandler::GetTickOrder() - { - return AZ::TICK_LAST; - } - - void TraceMessageBusHandler::QueueMessageCall(AZStd::function messageCall) + void TraceMessageBusHandler::FlushMessageCalls() + { + AZStd::list> messageCalls; { AZStd::lock_guard lock(m_messageCallsLock); - m_messageCalls.push_back(messageCall); + m_messageCalls.swap(messageCalls); // Move calls to a new list to release the lock as soon as possible } - void TraceMessageBusHandler::FlushMessageCalls() + for (auto& messageCall : messageCalls) { - AZStd::list> messageCalls; - { - AZStd::lock_guard lock(m_messageCallsLock); - m_messageCalls.swap(messageCalls); // Move calls to a new list to release the lock as soon as possible - } - - for (auto& messageCall : messageCalls) - { - messageCall(); - } - } - - void TraceReflect(ReflectContext* context) - { - if (BehaviorContext* behaviorContext = azrtti_cast(context)) - { - behaviorContext->EBus("TraceMessageBus") - ->Attribute(AZ::Script::Attributes::Module, "debug") - ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation) - ->Handler() - ; - } + messageCall(); } } -} + + void TraceReflect(ReflectContext* context) + { + if (BehaviorContext* behaviorContext = azrtti_cast(context)) + { + behaviorContext->EBus("TraceMessageBus") + ->Attribute(AZ::Script::Attributes::Module, "debug") + ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation) + ->Handler() + ; + } + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Driller/Driller.cpp b/Code/Framework/AzCore/AzCore/Driller/Driller.cpp index 41abd7793e..7e986be81e 100644 --- a/Code/Framework/AzCore/AzCore/Driller/Driller.cpp +++ b/Code/Framework/AzCore/AzCore/Driller/Driller.cpp @@ -14,291 +14,288 @@ #include -namespace AZ +namespace AZ::Debug { - namespace Debug + class DrillerManagerImpl + : public DrillerManager { - class DrillerManagerImpl - : public DrillerManager + public: + AZ_CLASS_ALLOCATOR(DrillerManagerImpl, OSAllocator, 0); + + using SessionListType = forward_list::type; + SessionListType m_sessions; + using DrillerArrayType = vector::type; + DrillerArrayType m_drillers; + + ~DrillerManagerImpl() override; + + void Register(Driller* factory) override; + void Unregister(Driller* factory) override; + + void FrameUpdate() override; + + DrillerSession* Start(DrillerOutputStream& output, const DrillerListType& drillerList, int numFrames = -1) override; + void Stop(DrillerSession* session) override; + + int GetNumDrillers() const override { return static_cast(m_drillers.size()); } + Driller* GetDriller(int index) override { return m_drillers[index]; } + }; + + ////////////////////////////////////////////////////////////////////////// + // Driller + + //========================================================================= + // Register + // [3/17/2011] + //========================================================================= + AZ::u32 Driller::GetId() const + { + return AZ::Crc32(GetName()); + } + + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Driller Manager + + //========================================================================= + // Register + // [3/17/2011] + //========================================================================= + DrillerManager* DrillerManager::Create(/*const Descriptor& desc*/) + { + const bool createAllocator = !AZ::AllocatorInstance::IsReady(); + if (createAllocator) { - public: - AZ_CLASS_ALLOCATOR(DrillerManagerImpl, OSAllocator, 0); - - typedef forward_list::type SessionListType; - SessionListType m_sessions; - typedef vector::type DrillerArrayType; - DrillerArrayType m_drillers; - - ~DrillerManagerImpl() override; - - void Register(Driller* factory) override; - void Unregister(Driller* factory) override; - - void FrameUpdate() override; - - DrillerSession* Start(DrillerOutputStream& output, const DrillerListType& drillerList, int numFrames = -1) override; - void Stop(DrillerSession* session) override; - - int GetNumDrillers() const override { return static_cast(m_drillers.size()); } - Driller* GetDriller(int index) override { return m_drillers[index]; } - }; - - ////////////////////////////////////////////////////////////////////////// - // Driller - - //========================================================================= - // Register - // [3/17/2011] - //========================================================================= - AZ::u32 Driller::GetId() const - { - return AZ::Crc32(GetName()); + AZ::AllocatorInstance::Create(); } - ////////////////////////////////////////////////////////////////////////// + DrillerManagerImpl* impl = aznew DrillerManagerImpl; + impl->m_ownsOSAllocator = createAllocator; + return impl; + } - ////////////////////////////////////////////////////////////////////////// - // Driller Manager - - //========================================================================= - // Register - // [3/17/2011] - //========================================================================= - DrillerManager* DrillerManager::Create(/*const Descriptor& desc*/) + //========================================================================= + // Register + // [3/17/2011] + //========================================================================= + void DrillerManager::Destroy(DrillerManager* manager) + { + const bool allocatorCreated = manager->m_ownsOSAllocator; + delete manager; + if (allocatorCreated) { - const bool createAllocator = !AZ::AllocatorInstance::IsReady(); - if (createAllocator) - { - AZ::AllocatorInstance::Create(); - } + AZ::AllocatorInstance::Destroy(); + } + } - DrillerManagerImpl* impl = aznew DrillerManagerImpl; - impl->m_ownsOSAllocator = createAllocator; - return impl; + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // DrillerManagerImpl + + //========================================================================= + // ~DrillerManagerImpl + // [3/17/2011] + //========================================================================= + DrillerManagerImpl::~DrillerManagerImpl() + { + while (!m_sessions.empty()) + { + Stop(&m_sessions.front()); } - //========================================================================= - // Register - // [3/17/2011] - //========================================================================= - void DrillerManager::Destroy(DrillerManager* manager) + while (!m_drillers.empty()) { - const bool allocatorCreated = manager->m_ownsOSAllocator; - delete manager; - if (allocatorCreated) - { - AZ::AllocatorInstance::Destroy(); - } + Driller* driller = m_drillers[0]; + Unregister(driller); + delete driller; } + } - ////////////////////////////////////////////////////////////////////////// - - ////////////////////////////////////////////////////////////////////////// - // DrillerManagerImpl - - //========================================================================= - // ~DrillerManagerImpl - // [3/17/2011] - //========================================================================= - DrillerManagerImpl::~DrillerManagerImpl() + //========================================================================= + // Register + // [3/17/2011] + //========================================================================= + void + DrillerManagerImpl::Register(Driller* driller) + { + AZ_Assert(driller, "You must provide a valid factory!"); + for (size_t i = 0; i < m_drillers.size(); ++i) { - while (!m_sessions.empty()) - { - Stop(&m_sessions.front()); - } - - while (!m_drillers.empty()) - { - Driller* driller = m_drillers[0]; - Unregister(driller); - delete driller; - } - } - - //========================================================================= - // Register - // [3/17/2011] - //========================================================================= - void - DrillerManagerImpl::Register(Driller* driller) - { - AZ_Assert(driller, "You must provide a valid factory!"); - for (size_t i = 0; i < m_drillers.size(); ++i) - { - if (m_drillers[i]->GetId() == driller->GetId()) - { - AZ_Error("Debug", false, "Driller with id %08x has already been registered! You can't have two factory instances for the same driller type", driller->GetId()); - return; - } - } - m_drillers.push_back(driller); - } - - //========================================================================= - // Unregister - // [3/17/2011] - //========================================================================= - void - DrillerManagerImpl::Unregister(Driller* driller) - { - AZ_Assert(driller, "You must provide a valid factory!"); - for (DrillerArrayType::iterator iter = m_drillers.begin(); iter != m_drillers.end(); ++iter) - { - if ((*iter)->GetId() == driller->GetId()) - { - m_drillers.erase(iter); - return; - } - } - - AZ_Error("Debug", false, "Failed to find driller factory with id %08x", driller->GetId()); - } - - //========================================================================= - // FrameUpdate - // [3/17/2011] - //========================================================================= - void - DrillerManagerImpl::FrameUpdate() - { - if (m_sessions.empty()) + if (m_drillers[i]->GetId() == driller->GetId()) { + AZ_Error("Debug", false, "Driller with id %08x has already been registered! You can't have two factory instances for the same driller type", driller->GetId()); return; } + } + m_drillers.push_back(driller); + } - AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); ///< Make sure no driller is writing to the stream - for (SessionListType::iterator sessionIter = m_sessions.begin(); sessionIter != m_sessions.end(); ) + //========================================================================= + // Unregister + // [3/17/2011] + //========================================================================= + void + DrillerManagerImpl::Unregister(Driller* driller) + { + AZ_Assert(driller, "You must provide a valid factory!"); + for (DrillerArrayType::iterator iter = m_drillers.begin(); iter != m_drillers.end(); ++iter) + { + if ((*iter)->GetId() == driller->GetId()) { - DrillerSession& s = *sessionIter; - - // tick the drillers directly if they care. - for (size_t i = 0; i < s.drillers.size(); ++i) - { - s.drillers[i]->Update(); - } - - s.output->EndTag(AZ_CRC("Frame", 0xb5f83ccd)); - - s.output->OnEndOfFrame(); - - s.curFrame++; - - if (s.numFrames != -1) - { - if (s.curFrame == s.numFrames) - { - Stop(&s); - continue; - } - } - - s.output->BeginTag(AZ_CRC("Frame", 0xb5f83ccd)); - s.output->Write(AZ_CRC("FrameNum", 0x85a1a919), s.curFrame); - - ++sessionIter; + m_drillers.erase(iter); + return; } } - //========================================================================= - // Start - // [3/17/2011] - //========================================================================= - DrillerSession* - DrillerManagerImpl::Start(DrillerOutputStream& output, const DrillerListType& drillerList, int numFrames) + AZ_Error("Debug", false, "Failed to find driller factory with id %08x", driller->GetId()); + } + + //========================================================================= + // FrameUpdate + // [3/17/2011] + //========================================================================= + void + DrillerManagerImpl::FrameUpdate() + { + if (m_sessions.empty()) { - if (drillerList.empty()) + return; + } + + AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); ///< Make sure no driller is writing to the stream + for (SessionListType::iterator sessionIter = m_sessions.begin(); sessionIter != m_sessions.end(); ) + { + DrillerSession& s = *sessionIter; + + // tick the drillers directly if they care. + for (size_t i = 0; i < s.drillers.size(); ++i) { - return nullptr; + s.drillers[i]->Update(); } - m_sessions.push_back(); - DrillerSession& s = m_sessions.back(); - s.curFrame = 0; - s.numFrames = numFrames; - s.output = &output; + s.output->EndTag(AZ_CRC("Frame", 0xb5f83ccd)); - s.output->WriteHeader(); // first write the header in the stream + s.output->OnEndOfFrame(); - s.output->BeginTag(AZ_CRC("StartData", 0xecf3f53f)); - s.output->Write(AZ_CRC("Platform", 0x3952d0cb), (unsigned int)g_currentPlatform); - for (DrillerListType::const_iterator iDriller = drillerList.begin(); iDriller != drillerList.end(); ++iDriller) + s.curFrame++; + + if (s.numFrames != -1) { - const DrillerInfo& di = *iDriller; - s.output->BeginTag(AZ_CRC("Driller", 0xa6e1fb73)); - s.output->Write(AZ_CRC("Name", 0x5e237e06), di.id); - for (int iParam = 0; iParam < (int)di.params.size(); ++iParam) + if (s.curFrame == s.numFrames) { - s.output->BeginTag(AZ_CRC("Param", 0xa4fa7c89)); - s.output->Write(AZ_CRC("Name", 0x5e237e06), di.params[iParam].name); - s.output->Write(AZ_CRC("Description", 0x6de44026), di.params[iParam].desc); - s.output->Write(AZ_CRC("Type", 0x8cde5729), di.params[iParam].type); - s.output->Write(AZ_CRC("Value", 0x1d775834), di.params[iParam].value); - s.output->EndTag(AZ_CRC("Param", 0xa4fa7c89)); + Stop(&s); + continue; } - s.output->EndTag(AZ_CRC("Driller", 0xa6e1fb73)); } - s.output->EndTag(AZ_CRC("StartData", 0xecf3f53f)); s.output->BeginTag(AZ_CRC("Frame", 0xb5f83ccd)); s.output->Write(AZ_CRC("FrameNum", 0x85a1a919), s.curFrame); - { - AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); ///< Make sure no driller is writing to the stream - for (DrillerListType::const_iterator iDriller = drillerList.begin(); iDriller != drillerList.end(); ++iDriller) - { - Driller* driller = nullptr; - const DrillerInfo& di = *iDriller; - for (size_t iDesc = 0; iDesc < m_drillers.size(); ++iDesc) - { - if (m_drillers[iDesc]->GetId() == di.id) - { - driller = m_drillers[iDesc]; - AZ_Assert(driller->m_output == nullptr, "Driller with id %08x is already have an output stream %p (currently we support only 1 at a time)", di.id, driller->m_output); - driller->m_output = &output; - driller->Start(di.params.data(), static_cast(di.params.size())); - s.drillers.push_back(driller); - break; - } - } - AZ_Warning("Driller", driller != nullptr, "We can't start a driller with id %d!", di.id); - } - } - return &s; + ++sessionIter; } + } - - //========================================================================= - // Stop - // [3/17/2011] - //========================================================================= - void - DrillerManagerImpl::Stop(DrillerSession* session) + //========================================================================= + // Start + // [3/17/2011] + //========================================================================= + DrillerSession* + DrillerManagerImpl::Start(DrillerOutputStream& output, const DrillerListType& drillerList, int numFrames) + { + if (drillerList.empty()) { - SessionListType::iterator iter; - for (iter = m_sessions.begin(); iter != m_sessions.end(); ++iter) + return nullptr; + } + + m_sessions.push_back(); + DrillerSession& s = m_sessions.back(); + s.curFrame = 0; + s.numFrames = numFrames; + s.output = &output; + + s.output->WriteHeader(); // first write the header in the stream + + s.output->BeginTag(AZ_CRC("StartData", 0xecf3f53f)); + s.output->Write(AZ_CRC("Platform", 0x3952d0cb), (unsigned int)g_currentPlatform); + for (DrillerListType::const_iterator iDriller = drillerList.begin(); iDriller != drillerList.end(); ++iDriller) + { + const DrillerInfo& di = *iDriller; + s.output->BeginTag(AZ_CRC("Driller", 0xa6e1fb73)); + s.output->Write(AZ_CRC("Name", 0x5e237e06), di.id); + for (int iParam = 0; iParam < (int)di.params.size(); ++iParam) { - if (&*iter == session) - { - break; - } + s.output->BeginTag(AZ_CRC("Param", 0xa4fa7c89)); + s.output->Write(AZ_CRC("Name", 0x5e237e06), di.params[iParam].name); + s.output->Write(AZ_CRC("Description", 0x6de44026), di.params[iParam].desc); + s.output->Write(AZ_CRC("Type", 0x8cde5729), di.params[iParam].type); + s.output->Write(AZ_CRC("Value", 0x1d775834), di.params[iParam].value); + s.output->EndTag(AZ_CRC("Param", 0xa4fa7c89)); } + s.output->EndTag(AZ_CRC("Driller", 0xa6e1fb73)); + } + s.output->EndTag(AZ_CRC("StartData", 0xecf3f53f)); - AZ_Assert(iter != m_sessions.end(), "We did not find session ID 0x%08x in the list!", session); - if (iter != m_sessions.end()) + s.output->BeginTag(AZ_CRC("Frame", 0xb5f83ccd)); + s.output->Write(AZ_CRC("FrameNum", 0x85a1a919), s.curFrame); + + { + AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); ///< Make sure no driller is writing to the stream + for (DrillerListType::const_iterator iDriller = drillerList.begin(); iDriller != drillerList.end(); ++iDriller) { - DrillerSession& s = *session; - + Driller* driller = nullptr; + const DrillerInfo& di = *iDriller; + for (size_t iDesc = 0; iDesc < m_drillers.size(); ++iDesc) { - AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); - for (size_t i = 0; i < s.drillers.size(); ++i) + if (m_drillers[iDesc]->GetId() == di.id) { - s.drillers[i]->Stop(); - s.drillers[i]->m_output = nullptr; + driller = m_drillers[iDesc]; + AZ_Assert(driller->m_output == nullptr, "Driller with id %08x is already have an output stream %p (currently we support only 1 at a time)", di.id, driller->m_output); + driller->m_output = &output; + driller->Start(di.params.data(), static_cast(di.params.size())); + s.drillers.push_back(driller); + break; } } - s.output->EndTag(AZ_CRC("Frame", 0xb5f83ccd)); - m_sessions.erase(iter); + AZ_Warning("Driller", driller != nullptr, "We can't start a driller with id %d!", di.id); } } - } // namespace Debug -} // namespace AZ + return &s; + } + + + //========================================================================= + // Stop + // [3/17/2011] + //========================================================================= + void + DrillerManagerImpl::Stop(DrillerSession* session) + { + SessionListType::iterator iter; + for (iter = m_sessions.begin(); iter != m_sessions.end(); ++iter) + { + if (&*iter == session) + { + break; + } + } + + AZ_Assert(iter != m_sessions.end(), "We did not find session ID 0x%08x in the list!", session); + if (iter != m_sessions.end()) + { + DrillerSession& s = *session; + + { + AZStd::lock_guard lock(DrillerEBusMutex::GetMutex()); + for (size_t i = 0; i < s.drillers.size(); ++i) + { + s.drillers[i]->Stop(); + s.drillers[i]->m_output = nullptr; + } + } + s.output->EndTag(AZ_CRC("Frame", 0xb5f83ccd)); + m_sessions.erase(iter); + } + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Driller/DrillerBus.cpp b/Code/Framework/AzCore/AzCore/Driller/DrillerBus.cpp index 163db8a68b..a46736aa87 100644 --- a/Code/Framework/AzCore/AzCore/Driller/DrillerBus.cpp +++ b/Code/Framework/AzCore/AzCore/Driller/DrillerBus.cpp @@ -12,57 +12,54 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + ////////////////////////////////////////////////////////////////////////// + // Globals + // We need to synchronize all driller evens, so we have proper order, and access to the data + // We use a global mutex which should be used for all driller operations. + // The mutex is held in an environment variable so it works across DLLs. + EnvironmentVariable s_drillerGlobalMutex; + ////////////////////////////////////////////////////////////////////////// + + + //========================================================================= + // lock + // [4/11/2011] + //========================================================================= + void DrillerEBusMutex::lock() { - ////////////////////////////////////////////////////////////////////////// - // Globals - // We need to synchronize all driller evens, so we have proper order, and access to the data - // We use a global mutex which should be used for all driller operations. - // The mutex is held in an environment variable so it works across DLLs. - EnvironmentVariable s_drillerGlobalMutex; - ////////////////////////////////////////////////////////////////////////// + GetMutex().lock(); + } - - //========================================================================= - // lock - // [4/11/2011] - //========================================================================= - void DrillerEBusMutex::lock() - { - GetMutex().lock(); - } + //========================================================================= + // try_lock + // [4/11/2011] + //========================================================================= + bool DrillerEBusMutex::try_lock() + { + return GetMutex().try_lock(); + } - //========================================================================= - // try_lock - // [4/11/2011] - //========================================================================= - bool DrillerEBusMutex::try_lock() - { - return GetMutex().try_lock(); - } + //========================================================================= + // unlock + // [4/11/2011] + //========================================================================= + void DrillerEBusMutex::unlock() + { + GetMutex().unlock(); + } - //========================================================================= - // unlock - // [4/11/2011] - //========================================================================= - void DrillerEBusMutex::unlock() + //========================================================================= + // unlock + // [4/11/2011] + //========================================================================= + AZStd::recursive_mutex& DrillerEBusMutex::GetMutex() + { + if (!s_drillerGlobalMutex) { - GetMutex().unlock(); + s_drillerGlobalMutex = Environment::CreateVariable(AZ_FUNCTION_SIGNATURE); } - - //========================================================================= - // unlock - // [4/11/2011] - //========================================================================= - AZStd::recursive_mutex& DrillerEBusMutex::GetMutex() - { - if (!s_drillerGlobalMutex) - { - s_drillerGlobalMutex = Environment::CreateVariable(AZ_FUNCTION_SIGNATURE); - } - return *s_drillerGlobalMutex; - } - } // namespace Debug -} // namespace AZ + return *s_drillerGlobalMutex; + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Driller/Stream.cpp b/Code/Framework/AzCore/AzCore/Driller/Stream.cpp index 14e983b09c..c86755149c 100644 --- a/Code/Framework/AzCore/AzCore/Driller/Stream.cpp +++ b/Code/Framework/AzCore/AzCore/Driller/Stream.cpp @@ -24,873 +24,870 @@ # include #endif // AZ_FILE_STREAM_COMPRESSION -namespace AZ +namespace AZ::Debug { - namespace Debug + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // Driller output stream + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + void DrillerOutputStream::Write(u32 name, const AZ::Vector3& v) { - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // Driller output stream - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - void DrillerOutputStream::Write(u32 name, const AZ::Vector3& v) - { - float data[4]; - unsigned int dataSize = 3 * sizeof(float); - v.StoreToFloat4(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Vector4& v) - { - float data[4]; - unsigned int dataSize = 4 * sizeof(float); - v.StoreToFloat4(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Aabb& aabb) - { - float data[7]; - unsigned int dataSize = 6 * sizeof(float); - aabb.GetMin().StoreToFloat4(data); - aabb.GetMax().StoreToFloat4(&data[3]); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Obb& obb) - { - float data[10]; - unsigned int dataSize = 10 * sizeof(float); // position (Vector3), rotation (Quaternion) and halfLengths (Vector3) - obb.GetPosition().StoreToFloat3(data); - obb.GetRotation().StoreToFloat4(&data[3]); - obb.GetHalfLengths().StoreToFloat3(&data[7]); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Transform& tm) - { - float data[12]; - unsigned int dataSize = 12 * sizeof(float); - const Matrix3x4 matrix3x4 = Matrix3x4::CreateFromTransform(tm); - matrix3x4.StoreToRowMajorFloat12(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Matrix3x3& tm) - { - float data[9]; - unsigned int dataSize = 9 * sizeof(float); - tm.StoreToRowMajorFloat9(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Matrix4x4& tm) - { - float data[16]; - unsigned int dataSize = 16 * sizeof(float); - tm.StoreToRowMajorFloat16(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Quaternion& tm) - { - float data[4]; - unsigned int dataSize = 4 * sizeof(float); - tm.StoreToFloat4(data); - StreamEntry de; - de.name = name; - de.sizeAndFlags = dataSize; - WriteBinary(de); - WriteBinary(data, dataSize); - } - void DrillerOutputStream::Write(u32 name, const AZ::Plane& plane) - { - Write(name, plane.GetPlaneEquationCoefficients()); - } - void DrillerOutputStream::WriteHeader() - { - StreamHeader sh; // StreamHeader should be endianess independent. - WriteBinary(&sh, sizeof(sh)); - } + float data[4]; + unsigned int dataSize = 3 * sizeof(float); + v.StoreToFloat4(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Vector4& v) + { + float data[4]; + unsigned int dataSize = 4 * sizeof(float); + v.StoreToFloat4(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Aabb& aabb) + { + float data[7]; + unsigned int dataSize = 6 * sizeof(float); + aabb.GetMin().StoreToFloat4(data); + aabb.GetMax().StoreToFloat4(&data[3]); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Obb& obb) + { + float data[10]; + unsigned int dataSize = 10 * sizeof(float); // position (Vector3), rotation (Quaternion) and halfLengths (Vector3) + obb.GetPosition().StoreToFloat3(data); + obb.GetRotation().StoreToFloat4(&data[3]); + obb.GetHalfLengths().StoreToFloat3(&data[7]); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Transform& tm) + { + float data[12]; + unsigned int dataSize = 12 * sizeof(float); + const Matrix3x4 matrix3x4 = Matrix3x4::CreateFromTransform(tm); + matrix3x4.StoreToRowMajorFloat12(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Matrix3x3& tm) + { + float data[9]; + unsigned int dataSize = 9 * sizeof(float); + tm.StoreToRowMajorFloat9(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Matrix4x4& tm) + { + float data[16]; + unsigned int dataSize = 16 * sizeof(float); + tm.StoreToRowMajorFloat16(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Quaternion& tm) + { + float data[4]; + unsigned int dataSize = 4 * sizeof(float); + tm.StoreToFloat4(data); + StreamEntry de; + de.name = name; + de.sizeAndFlags = dataSize; + WriteBinary(de); + WriteBinary(data, dataSize); + } + void DrillerOutputStream::Write(u32 name, const AZ::Plane& plane) + { + Write(name, plane.GetPlaneEquationCoefficients()); + } + void DrillerOutputStream::WriteHeader() + { + StreamHeader sh; // StreamHeader should be endianess independent. + WriteBinary(&sh, sizeof(sh)); + } - void DrillerOutputStream::WriteTimeUTC(u32 name) - { - AZStd::sys_time_t now = AZStd::GetTimeUTCMilliSecond(); - Write(name, now); - } + void DrillerOutputStream::WriteTimeUTC(u32 name) + { + AZStd::sys_time_t now = AZStd::GetTimeUTCMilliSecond(); + Write(name, now); + } - void DrillerOutputStream::WriteTimeMicrosecond(u32 name) - { - AZStd::sys_time_t now = AZStd::GetTimeNowMicroSecond(); - Write(name, now); - } + void DrillerOutputStream::WriteTimeMicrosecond(u32 name) + { + AZStd::sys_time_t now = AZStd::GetTimeNowMicroSecond(); + Write(name, now); + } - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // Driller Input Stream - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - bool DrillerInputStream::ReadHeader() + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // Driller Input Stream + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + bool DrillerInputStream::ReadHeader() + { + DrillerOutputStream::StreamHeader sh; // StreamHeader should be endianess independent. + unsigned int numRead = ReadBinary(&sh, sizeof(sh)); + (void)numRead; + AZ_Error("IO", numRead == sizeof(sh), "We should have atleast %d bytes in the stream to read the header!", sizeof(sh)); + if (numRead != sizeof(sh)) { - DrillerOutputStream::StreamHeader sh; // StreamHeader should be endianess independent. - unsigned int numRead = ReadBinary(&sh, sizeof(sh)); - (void)numRead; - AZ_Error("IO", numRead == sizeof(sh), "We should have atleast %d bytes in the stream to read the header!", sizeof(sh)); - if (numRead != sizeof(sh)) - { - return false; - } - m_isEndianSwap = AZ::IsBigEndian(static_cast(sh.platform)) != AZ::IsBigEndian(AZ::g_currentPlatform); - return true; - } - - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // Driller file stream - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - //========================================================================= - // DrillerOutputFileStream::DrillerOutputFileStream - // [3/23/2011] - //========================================================================= - DrillerOutputFileStream::DrillerOutputFileStream() - { -#if defined(AZ_FILE_STREAM_COMPRESSION) - m_zlib = azcreate(ZLib, (&AllocatorInstance::GetAllocator()), OSAllocator); - m_zlib->StartCompressor(2); -#endif - } - - //========================================================================= - // DrillerOutputFileStream::~DrillerOutputFileStream - // [3/23/2011] - //========================================================================= - DrillerOutputFileStream::~DrillerOutputFileStream() - { -#if defined(AZ_FILE_STREAM_COMPRESSION) - azdestroy(m_zlib, OSAllocator); -#endif - } - - //========================================================================= - // DrillerOutputFileStream::Open - // [3/23/2011] - //========================================================================= - bool DrillerOutputFileStream::Open(const char* fileName, int mode, int platformFlags) - { - if (IO::SystemFile::Open(fileName, mode, platformFlags)) - { - m_dataBuffer.reserve(100 * 1024); -#if defined(AZ_FILE_STREAM_COMPRESSION) - // // Enable optional: encode the file in the same format as the streamer so they are interchangeable - // IO::CompressorHeader ch; - // ch.SetAZCS(); - // ch.m_compressorId = IO::CompressorZLib::TypeId(); - // ch.m_uncompressedSize = 0; // will be updated later - // AZStd::endian_swap(ch.m_compressorId); - // AZStd::endian_swap(ch.m_uncompressedSize); - // IO::SystemFile::Write(&ch,sizeof(ch)); - // IO::CompressorZLibHeader zlibHdr; - // zlibHdr.m_numSeekPoints = 0; - // IO::SystemFile::Write(&zlibHdr,sizeof(zlibHdr)); -#endif - return true; - } return false; } + m_isEndianSwap = AZ::IsBigEndian(static_cast(sh.platform)) != AZ::IsBigEndian(AZ::g_currentPlatform); + return true; + } - //========================================================================= - // DrillerOutputFileStream::Close - // [3/23/2011] - //========================================================================= - void DrillerOutputFileStream::Close() + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // Driller file stream + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + //========================================================================= + // DrillerOutputFileStream::DrillerOutputFileStream + // [3/23/2011] + //========================================================================= + DrillerOutputFileStream::DrillerOutputFileStream() + { +#if defined(AZ_FILE_STREAM_COMPRESSION) + m_zlib = azcreate(ZLib, (&AllocatorInstance::GetAllocator()), OSAllocator); + m_zlib->StartCompressor(2); +#endif + } + + //========================================================================= + // DrillerOutputFileStream::~DrillerOutputFileStream + // [3/23/2011] + //========================================================================= + DrillerOutputFileStream::~DrillerOutputFileStream() + { +#if defined(AZ_FILE_STREAM_COMPRESSION) + azdestroy(m_zlib, OSAllocator); +#endif + } + + //========================================================================= + // DrillerOutputFileStream::Open + // [3/23/2011] + //========================================================================= + bool DrillerOutputFileStream::Open(const char* fileName, int mode, int platformFlags) + { + if (IO::SystemFile::Open(fileName, mode, platformFlags)) { - unsigned int dataSizeInBuffer = static_cast(m_dataBuffer.size()); + m_dataBuffer.reserve(100 * 1024); +#if defined(AZ_FILE_STREAM_COMPRESSION) + // // Enable optional: encode the file in the same format as the streamer so they are interchangeable + // IO::CompressorHeader ch; + // ch.SetAZCS(); + // ch.m_compressorId = IO::CompressorZLib::TypeId(); + // ch.m_uncompressedSize = 0; // will be updated later + // AZStd::endian_swap(ch.m_compressorId); + // AZStd::endian_swap(ch.m_uncompressedSize); + // IO::SystemFile::Write(&ch,sizeof(ch)); + // IO::CompressorZLibHeader zlibHdr; + // zlibHdr.m_numSeekPoints = 0; + // IO::SystemFile::Write(&zlibHdr,sizeof(zlibHdr)); +#endif + return true; + } + return false; + } + + //========================================================================= + // DrillerOutputFileStream::Close + // [3/23/2011] + //========================================================================= + void DrillerOutputFileStream::Close() + { + unsigned int dataSizeInBuffer = static_cast(m_dataBuffer.size()); + { +#if defined(AZ_FILE_STREAM_COMPRESSION) + unsigned int minCompressBufferSize = m_zlib->GetMinCompressedBufferSize(dataSizeInBuffer); + if (m_compressionBuffer.size() < minCompressBufferSize) // grow compression buffer if needed + { + m_compressionBuffer.clear(); + m_compressionBuffer.resize(minCompressBufferSize); + } + unsigned int compressedSize; + do + { + compressedSize = m_zlib->Compress(m_dataBuffer.data(), dataSizeInBuffer, m_compressionBuffer.data(), (unsigned)m_compressionBuffer.size(), ZLib::FT_FINISH); + if (compressedSize) + { + IO::SystemFile::Write(m_compressionBuffer.data(), compressedSize); + } + } while (compressedSize > 0); + m_zlib->ResetCompressor(); +#else + if (dataSizeInBuffer) + { + IO::SystemFile::Write(m_dataBuffer.data(), m_dataBuffer.size()); + } +#endif + m_dataBuffer.clear(); + } + IO::SystemFile::Close(); + } + //========================================================================= + // DrillerOutputFileStream::WriteBinary + // [3/23/2011] + //========================================================================= + void DrillerOutputFileStream::WriteBinary(const void* data, unsigned int dataSize) + { + size_t dataSizeInBuffer = m_dataBuffer.size(); + if (dataSizeInBuffer + dataSize > m_dataBuffer.capacity()) + { + if (dataSizeInBuffer > 0) { #if defined(AZ_FILE_STREAM_COMPRESSION) - unsigned int minCompressBufferSize = m_zlib->GetMinCompressedBufferSize(dataSizeInBuffer); + // we need to flush the data + unsigned int dataToCompress = static_cast(dataSizeInBuffer); + unsigned int minCompressBufferSize = m_zlib->GetMinCompressedBufferSize(dataToCompress); if (m_compressionBuffer.size() < minCompressBufferSize) // grow compression buffer if needed { m_compressionBuffer.clear(); m_compressionBuffer.resize(minCompressBufferSize); } - unsigned int compressedSize; - do + while (dataToCompress > 0) { - compressedSize = m_zlib->Compress(m_dataBuffer.data(), dataSizeInBuffer, m_compressionBuffer.data(), (unsigned)m_compressionBuffer.size(), ZLib::FT_FINISH); + unsigned int compressedSize = m_zlib->Compress(m_dataBuffer.data(), dataToCompress, m_compressionBuffer.data(), (unsigned)m_compressionBuffer.size()); if (compressedSize) { IO::SystemFile::Write(m_compressionBuffer.data(), compressedSize); } - } while (compressedSize > 0); - m_zlib->ResetCompressor(); -#else - if (dataSizeInBuffer) - { - IO::SystemFile::Write(m_dataBuffer.data(), m_dataBuffer.size()); } +#else + IO::SystemFile::Write(m_dataBuffer.data(), m_dataBuffer.size()); #endif m_dataBuffer.clear(); } - IO::SystemFile::Close(); } - //========================================================================= - // DrillerOutputFileStream::WriteBinary - // [3/23/2011] - //========================================================================= - void DrillerOutputFileStream::WriteBinary(const void* data, unsigned int dataSize) - { - size_t dataSizeInBuffer = m_dataBuffer.size(); - if (dataSizeInBuffer + dataSize > m_dataBuffer.capacity()) - { - if (dataSizeInBuffer > 0) - { + m_dataBuffer.insert(m_dataBuffer.end(), reinterpret_cast(data), reinterpret_cast(data) + dataSize); + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // Driller file input stream + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + + //========================================================================= + // DrillerInputFileStream::DrillerInputFileStream + // [3/23/2011] + //========================================================================= + DrillerInputFileStream::DrillerInputFileStream() + { #if defined(AZ_FILE_STREAM_COMPRESSION) - // we need to flush the data - unsigned int dataToCompress = static_cast(dataSizeInBuffer); - unsigned int minCompressBufferSize = m_zlib->GetMinCompressedBufferSize(dataToCompress); - if (m_compressionBuffer.size() < minCompressBufferSize) // grow compression buffer if needed - { - m_compressionBuffer.clear(); - m_compressionBuffer.resize(minCompressBufferSize); - } - while (dataToCompress > 0) - { - unsigned int compressedSize = m_zlib->Compress(m_dataBuffer.data(), dataToCompress, m_compressionBuffer.data(), (unsigned)m_compressionBuffer.size()); - if (compressedSize) - { - IO::SystemFile::Write(m_compressionBuffer.data(), compressedSize); - } - } + m_zlib = azcreate(ZLib, (&AllocatorInstance::GetAllocator()), OSAllocator); + m_zlib->StartDecompressor(); +#endif + } + + //========================================================================= + // DrillerInputFileStream::DrillerInputFileStream + // [3/23/2011] + //========================================================================= + DrillerInputFileStream::~DrillerInputFileStream() + { +#if defined(AZ_FILE_STREAM_COMPRESSION) + azdestroy(m_zlib, OSAllocator); +#endif + } + + //========================================================================= + // DrillerInputFileStream::Open + // [3/23/2011] + //========================================================================= + bool DrillerInputFileStream::Open(const char* fileName, int mode, int platformFlags) + { + if (IO::SystemFile::Open(fileName, mode, platformFlags)) + { + DrillerOutputStream::StreamHeader sh; +#if defined(AZ_FILE_STREAM_COMPRESSION) + // TODO: optional encode the file in the same format as the streamer so they are interchangeable +#endif + // first read the header of the stream file. + return ReadHeader(); + } + return false; + } + //========================================================================= + // DrillerInputFileStream::ReadBinary + // [3/23/2011] + //========================================================================= + unsigned int DrillerInputFileStream::ReadBinary(void* data, unsigned int maxDataSize) + { + // make sure the compressed buffer if full enough... + size_t dataToLoad = maxDataSize * 2; + m_compressedData.reserve(dataToLoad); + while (m_compressedData.size() < dataToLoad) + { + unsigned char buffer[10 * 1024]; + IO::SystemFile::SizeType bytesRead = Read(AZ_ARRAY_SIZE(buffer), buffer); + if (bytesRead > 0) + { + m_compressedData.insert(m_compressedData.end(), (unsigned char*)buffer, buffer + bytesRead); + } + if (bytesRead < AZ_ARRAY_SIZE(buffer)) + { + break; + } + } +#if defined(AZ_FILE_STREAM_COMPRESSION) + unsigned int dataSize = maxDataSize; + unsigned int bytesProcessed = m_zlib->Decompress(m_compressedData.data(), (unsigned)m_compressedData.size(), data, dataSize); + unsigned int readSize = maxDataSize - dataSize; // Zlib::Decompress decrements the dataSize parameter by the amount uncompressed #else - IO::SystemFile::Write(m_dataBuffer.data(), m_dataBuffer.size()); + unsigned int bytesProcessed = AZStd::GetMin((unsigned int)m_compressedData.size(), maxDataSize); + unsigned int readSize = bytesProcessed; + memcpy(data, m_compressedData.data(), readSize); #endif - m_dataBuffer.clear(); - } - } - m_dataBuffer.insert(m_dataBuffer.end(), reinterpret_cast(data), reinterpret_cast(data) + dataSize); - } + m_compressedData.erase(m_compressedData.begin(), m_compressedData.begin() + bytesProcessed); + return readSize; + } - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // Driller file input stream - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - - //========================================================================= - // DrillerInputFileStream::DrillerInputFileStream - // [3/23/2011] - //========================================================================= - DrillerInputFileStream::DrillerInputFileStream() - { + //========================================================================= + // DrillerInputFileStream::Close + // [3/23/2011] + //========================================================================= + void DrillerInputFileStream::Close() + { #if defined(AZ_FILE_STREAM_COMPRESSION) - m_zlib = azcreate(ZLib, (&AllocatorInstance::GetAllocator()), OSAllocator); - m_zlib->StartDecompressor(); -#endif - } - - //========================================================================= - // DrillerInputFileStream::DrillerInputFileStream - // [3/23/2011] - //========================================================================= - DrillerInputFileStream::~DrillerInputFileStream() + if (m_zlib) { -#if defined(AZ_FILE_STREAM_COMPRESSION) - azdestroy(m_zlib, OSAllocator); -#endif + m_zlib->ResetDecompressor(); } +#endif // AZ_FILE_STREAM_COMPRESSION + AZ::IO::SystemFile::Close(); + } - //========================================================================= - // DrillerInputFileStream::Open - // [3/23/2011] - //========================================================================= - bool DrillerInputFileStream::Open(const char* fileName, int mode, int platformFlags) + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // DrillerSAXParser + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + //========================================================================= + // DrillerSAXParser + // [3/23/2011] + //========================================================================= + DrillerSAXParser::DrillerSAXParser(const TagCallbackType& tcb, const DataCallbackType& dcb) + : m_tagCallback(tcb) + , m_dataCallback(dcb) + { + } + + //========================================================================= + // ProcessStream + // [3/23/2011] + //========================================================================= + void + DrillerSAXParser::ProcessStream(DrillerInputStream& stream) + { + static const int processChunkSize = 15 * 1024; + char buffer[processChunkSize]; + unsigned int dataSize; + bool isEndianSwap = stream.IsEndianSwap(); + while ((dataSize = stream.ReadBinary(buffer, processChunkSize)) > 0) { - if (IO::SystemFile::Open(fileName, mode, platformFlags)) + char* dataStart = buffer; + char* dataEnd = dataStart + dataSize; + bool dataInBuffer = false; + if (!m_buffer.empty()) { - DrillerOutputStream::StreamHeader sh; -#if defined(AZ_FILE_STREAM_COMPRESSION) - // TODO: optional encode the file in the same format as the streamer so they are interchangeable -#endif - // first read the header of the stream file. - return ReadHeader(); + m_buffer.insert(m_buffer.end(), dataStart, dataEnd); + dataStart = m_buffer.data(); + dataEnd = dataStart + m_buffer.size(); + dataInBuffer = true; } - return false; - } - //========================================================================= - // DrillerInputFileStream::ReadBinary - // [3/23/2011] - //========================================================================= - unsigned int DrillerInputFileStream::ReadBinary(void* data, unsigned int maxDataSize) - { - // make sure the compressed buffer if full enough... - size_t dataToLoad = maxDataSize * 2; - m_compressedData.reserve(dataToLoad); - while (m_compressedData.size() < dataToLoad) + const int entrySize = sizeof(DrillerOutputStream::StreamEntry); + while (dataStart != dataEnd) { - unsigned char buffer[10 * 1024]; - IO::SystemFile::SizeType bytesRead = Read(AZ_ARRAY_SIZE(buffer), buffer); - if (bytesRead > 0) - { - m_compressedData.insert(m_compressedData.end(), (unsigned char*)buffer, buffer + bytesRead); - } - if (bytesRead < AZ_ARRAY_SIZE(buffer)) + if ((dataEnd - dataStart) < entrySize) // we need at least one entry to proceed { + // not enough data to process, buffer it. + if (!dataInBuffer) + { + m_buffer.insert(m_buffer.end(), dataStart, dataEnd); + } break; } - } -#if defined(AZ_FILE_STREAM_COMPRESSION) - unsigned int dataSize = maxDataSize; - unsigned int bytesProcessed = m_zlib->Decompress(m_compressedData.data(), (unsigned)m_compressedData.size(), data, dataSize); - unsigned int readSize = maxDataSize - dataSize; // Zlib::Decompress decrements the dataSize parameter by the amount uncompressed -#else - unsigned int bytesProcessed = AZStd::GetMin((unsigned int)m_compressedData.size(), maxDataSize); - unsigned int readSize = bytesProcessed; - memcpy(data, m_compressedData.data(), readSize); -#endif - m_compressedData.erase(m_compressedData.begin(), m_compressedData.begin() + bytesProcessed); - return readSize; - } - //========================================================================= - // DrillerInputFileStream::Close - // [3/23/2011] - //========================================================================= - void DrillerInputFileStream::Close() - { -#if defined(AZ_FILE_STREAM_COMPRESSION) - if (m_zlib) - { - m_zlib->ResetDecompressor(); - } -#endif // AZ_FILE_STREAM_COMPRESSION - AZ::IO::SystemFile::Close(); - } - - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // DrillerSAXParser - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - //========================================================================= - // DrillerSAXParser - // [3/23/2011] - //========================================================================= - DrillerSAXParser::DrillerSAXParser(const TagCallbackType& tcb, const DataCallbackType& dcb) - : m_tagCallback(tcb) - , m_dataCallback(dcb) - { - } - - //========================================================================= - // ProcessStream - // [3/23/2011] - //========================================================================= - void - DrillerSAXParser::ProcessStream(DrillerInputStream& stream) - { - static const int processChunkSize = 15 * 1024; - char buffer[processChunkSize]; - unsigned int dataSize; - bool isEndianSwap = stream.IsEndianSwap(); - while ((dataSize = stream.ReadBinary(buffer, processChunkSize)) > 0) - { - char* dataStart = buffer; - char* dataEnd = dataStart + dataSize; - bool dataInBuffer = false; - if (!m_buffer.empty()) + DrillerOutputStream::StreamEntry* se = reinterpret_cast(dataStart); + if (isEndianSwap) { - m_buffer.insert(m_buffer.end(), dataStart, dataEnd); - dataStart = m_buffer.data(); - dataEnd = dataStart + m_buffer.size(); - dataInBuffer = true; + // endian swap + AZStd::endian_swap(se->name); + AZStd::endian_swap(se->sizeAndFlags); } - const int entrySize = sizeof(DrillerOutputStream::StreamEntry); - while (dataStart != dataEnd) + + u32 dataType = (se->sizeAndFlags & DrillerOutputStream::StreamEntry::dataInternalMask) >> DrillerOutputStream::StreamEntry::dataInternalShift; + u32 value = se->sizeAndFlags & DrillerOutputStream::StreamEntry::dataSizeMask; + Data de; + de.m_name = se->name; + de.m_stringPool = stream.GetStringPool(); + de.m_isPooledString = false; + de.m_isPooledStringCrc32 = false; + switch (dataType) { - if ((dataEnd - dataStart) < entrySize) // we need at least one entry to proceed + case DrillerOutputStream::StreamEntry::INT_TAG: + { + bool isStart = (value != 0); + m_tagCallback(se->name, isStart); + dataStart += entrySize; + } break; + case DrillerOutputStream::StreamEntry::INT_DATA_U8: + { + u8 value8 = static_cast(value); + de.m_data = &value8; + de.m_dataSize = 1; + de.m_isEndianSwap = false; + m_dataCallback(de); + dataStart += entrySize; + } break; + case DrillerOutputStream::StreamEntry::INT_DATA_U16: + { + u16 value16 = static_cast(value); + de.m_data = &value16; + de.m_dataSize = 2; + de.m_isEndianSwap = false; + m_dataCallback(de); + dataStart += entrySize; + } break; + case DrillerOutputStream::StreamEntry::INT_DATA_U29: + { + de.m_data = &value; + de.m_dataSize = 4; + de.m_isEndianSwap = false; + m_dataCallback(de); + dataStart += entrySize; + } break; + case DrillerOutputStream::StreamEntry::INT_POOLED_STRING: + { + unsigned int userDataSize = value; + if ((userDataSize + entrySize) <= (unsigned)(dataEnd - dataStart)) { - // not enough data to process, buffer it. + // Add string to the pool + AZ_Assert(de.m_stringPool != nullptr, "We require a string pool to parse this stream"); + AZ::u32 crc32; + const char* stringPtr; + dataStart += entrySize; + de.m_stringPool->InsertCopy(reinterpret_cast(dataStart), userDataSize, crc32, &stringPtr); + de.m_dataSize = userDataSize; + de.m_isEndianSwap = isEndianSwap; + de.m_isPooledString = true; + de.m_data = const_cast(static_cast(stringPtr)); + m_dataCallback(de); + dataStart += userDataSize; + } + else + { + // we can't process data right now add it to the buffer (if we have not done that already) if (!dataInBuffer) { m_buffer.insert(m_buffer.end(), dataStart, dataEnd); } - break; + dataEnd = dataStart; // exit the loop } - - DrillerOutputStream::StreamEntry* se = reinterpret_cast(dataStart); - if (isEndianSwap) + } break; + case DrillerOutputStream::StreamEntry::INT_POOLED_STRING_CRC32: + { + de.m_isPooledStringCrc32 = true; + AZ_Assert(value == 4, "The data size for a pooled string crc32 should be 4 bytes!"); + } // continue to INT_SIZE + case DrillerOutputStream::StreamEntry::INT_SIZE: + { + unsigned int userDataSize = value; + if ((userDataSize + entrySize) <= (unsigned)(dataEnd - dataStart)) // do we have all the date we need to process... { - // endian swap - AZStd::endian_swap(se->name); - AZStd::endian_swap(se->sizeAndFlags); - } - - u32 dataType = (se->sizeAndFlags & DrillerOutputStream::StreamEntry::dataInternalMask) >> DrillerOutputStream::StreamEntry::dataInternalShift; - u32 value = se->sizeAndFlags & DrillerOutputStream::StreamEntry::dataSizeMask; - Data de; - de.m_name = se->name; - de.m_stringPool = stream.GetStringPool(); - de.m_isPooledString = false; - de.m_isPooledStringCrc32 = false; - switch (dataType) - { - case DrillerOutputStream::StreamEntry::INT_TAG: - { - bool isStart = (value != 0); - m_tagCallback(se->name, isStart); dataStart += entrySize; - } break; - case DrillerOutputStream::StreamEntry::INT_DATA_U8: - { - u8 value8 = static_cast(value); - de.m_data = &value8; - de.m_dataSize = 1; - de.m_isEndianSwap = false; + de.m_data = dataStart; + de.m_dataSize = userDataSize; + de.m_isEndianSwap = isEndianSwap; m_dataCallback(de); - dataStart += entrySize; - } break; - case DrillerOutputStream::StreamEntry::INT_DATA_U16: - { - u16 value16 = static_cast(value); - de.m_data = &value16; - de.m_dataSize = 2; - de.m_isEndianSwap = false; - m_dataCallback(de); - dataStart += entrySize; - } break; - case DrillerOutputStream::StreamEntry::INT_DATA_U29: - { - de.m_data = &value; - de.m_dataSize = 4; - de.m_isEndianSwap = false; - m_dataCallback(de); - dataStart += entrySize; - } break; - case DrillerOutputStream::StreamEntry::INT_POOLED_STRING: - { - unsigned int userDataSize = value; - if ((userDataSize + entrySize) <= (unsigned)(dataEnd - dataStart)) - { - // Add string to the pool - AZ_Assert(de.m_stringPool != nullptr, "We require a string pool to parse this stream"); - AZ::u32 crc32; - const char* stringPtr; - dataStart += entrySize; - de.m_stringPool->InsertCopy(reinterpret_cast(dataStart), userDataSize, crc32, &stringPtr); - de.m_dataSize = userDataSize; - de.m_isEndianSwap = isEndianSwap; - de.m_isPooledString = true; - de.m_data = const_cast(static_cast(stringPtr)); - m_dataCallback(de); - dataStart += userDataSize; - } - else - { - // we can't process data right now add it to the buffer (if we have not done that already) - if (!dataInBuffer) - { - m_buffer.insert(m_buffer.end(), dataStart, dataEnd); - } - dataEnd = dataStart; // exit the loop - } - } break; - case DrillerOutputStream::StreamEntry::INT_POOLED_STRING_CRC32: - { - de.m_isPooledStringCrc32 = true; - AZ_Assert(value == 4, "The data size for a pooled string crc32 should be 4 bytes!"); - } // continue to INT_SIZE - case DrillerOutputStream::StreamEntry::INT_SIZE: - { - unsigned int userDataSize = value; - if ((userDataSize + entrySize) <= (unsigned)(dataEnd - dataStart)) // do we have all the date we need to process... - { - dataStart += entrySize; - de.m_data = dataStart; - de.m_dataSize = userDataSize; - de.m_isEndianSwap = isEndianSwap; - m_dataCallback(de); - dataStart += userDataSize; - } - else - { - // we can't process data right now add it to the buffer (if we have not done that already) - if (!dataInBuffer) - { - m_buffer.insert(m_buffer.end(), dataStart, dataEnd); - } - dataEnd = dataStart; // exit the loop - } - } break; - default: - { - AZ_Error("DrillerSAXParser",false,"Encounted unknown symbol (%i) while processing stream (%s). Aborting stream.\n",dataType, stream.GetIdentifier()); - - // If we can't process anything, we want to just escape the loop, to avoid spinning infinitely - dataEnd = dataStart; - } break; + dataStart += userDataSize; } - } - if (dataInBuffer) // if the data was in the buffer remove the processed data! - { - m_buffer.erase(m_buffer.begin(), m_buffer.begin() + (dataStart - m_buffer.data())); - } - } - } - - void DrillerSAXParser::Data::Read(AZ::Vector3& v) const - { - AZ_Assert(m_dataSize == sizeof(float) * 3, "We are expecting 3 floats for Vector3 element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 3); - m_isEndianSwap = false; - } - v = Vector3::CreateFromFloat3(data); - } - void DrillerSAXParser::Data::Read(AZ::Vector4& v) const - { - AZ_Assert(m_dataSize == sizeof(float) * 4, "We are expecting 4 floats for Vector4 element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 4); - m_isEndianSwap = false; - } - v = Vector4::CreateFromFloat4(data); - } - void DrillerSAXParser::Data::Read(AZ::Aabb& aabb) const - { - AZ_Assert(m_dataSize == sizeof(float) * 6, "We are expecting 6 floats for Aabb element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 6); - m_isEndianSwap = false; - } - Vector3 min = Vector3::CreateFromFloat3(data); - Vector3 max = Vector3::CreateFromFloat3(&data[3]); - aabb = Aabb::CreateFromMinMax(min, max); - } - void DrillerSAXParser::Data::Read(AZ::Obb& obb) const - { - AZ_Assert(m_dataSize == sizeof(float) * 10, "We are expecting 10 floats for Obb element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 10); - m_isEndianSwap = false; - } - Vector3 position = Vector3::CreateFromFloat3(data); - Quaternion rotation = Quaternion::CreateFromFloat4(&data[3]); - Vector3 halfLengths = Vector3::CreateFromFloat3(&data[7]); - obb = Obb::CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths); - } - void DrillerSAXParser::Data::Read(AZ::Transform& tm) const - { - AZ_Assert(m_dataSize == sizeof(float) * 12, "We are expecting 12 floats for Transform element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 12); - m_isEndianSwap = false; - } - const Matrix3x4 matrix3x4 = Matrix3x4::CreateFromRowMajorFloat12(data); - tm = Transform::CreateFromMatrix3x4(matrix3x4); - } - void DrillerSAXParser::Data::Read(AZ::Matrix3x3& tm) const - { - AZ_Assert(m_dataSize == sizeof(float) * 9, "We are expecting 9 floats for Matrix3x3 element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 9); - m_isEndianSwap = false; - } - tm = Matrix3x3::CreateFromRowMajorFloat9(data); - } - void DrillerSAXParser::Data::Read(AZ::Matrix4x4& tm) const - { - AZ_Assert(m_dataSize == sizeof(float) * 16, "We are expecting 16 floats for Matrix4x4 element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 16); - m_isEndianSwap = false; - } - tm = Matrix4x4::CreateFromRowMajorFloat16(data); - } - void DrillerSAXParser::Data::Read(AZ::Quaternion& tm) const - { - AZ_Assert(m_dataSize == sizeof(float) * 4, "We are expecting 4 floats for Quaternion element 0x%08x with size %d bytes", m_name, m_dataSize); - float* data = reinterpret_cast(m_data); - if (m_isEndianSwap) - { - AZStd::endian_swap(data, data + 4); - m_isEndianSwap = false; - } - tm = Quaternion::CreateFromFloat4(data); - } - void DrillerSAXParser::Data::Read(AZ::Plane& plane) const - { - AZ::Vector4 coeff; - Read(coeff); - plane = Plane::CreateFromCoefficients(coeff.GetX(), coeff.GetY(), coeff.GetZ(), coeff.GetW()); - } - - const char* DrillerSAXParser::Data::PrepareString(unsigned int& stringLength) const - { - const char* srcData = reinterpret_cast(m_data); - stringLength = m_dataSize; - if (m_stringPool) - { - AZ::u32 crc32; - const char* stringPtr; - if (m_isPooledStringCrc32) - { - crc32 = *reinterpret_cast(m_data); - if (m_isEndianSwap) + else { - AZStd::endian_swap(crc32); + // we can't process data right now add it to the buffer (if we have not done that already) + if (!dataInBuffer) + { + m_buffer.insert(m_buffer.end(), dataStart, dataEnd); + } + dataEnd = dataStart; // exit the loop } - stringPtr = m_stringPool->Find(crc32); - AZ_Assert(stringPtr != nullptr, "Failed to find string with id 0x%08x in the string pool, proper stream read is impossible!", crc32); - stringLength = static_cast(strlen(stringPtr)); - } - else if (m_isPooledString) + } break; + default: { - stringPtr = srcData; // already stored in the pool just transfer the pointer + AZ_Error("DrillerSAXParser",false,"Encounted unknown symbol (%i) while processing stream (%s). Aborting stream.\n",dataType, stream.GetIdentifier()); + + // If we can't process anything, we want to just escape the loop, to avoid spinning infinitely + dataEnd = dataStart; + } break; } - else + } + if (dataInBuffer) // if the data was in the buffer remove the processed data! + { + m_buffer.erase(m_buffer.begin(), m_buffer.begin() + (dataStart - m_buffer.data())); + } + } + } + + void DrillerSAXParser::Data::Read(AZ::Vector3& v) const + { + AZ_Assert(m_dataSize == sizeof(float) * 3, "We are expecting 3 floats for Vector3 element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 3); + m_isEndianSwap = false; + } + v = Vector3::CreateFromFloat3(data); + } + void DrillerSAXParser::Data::Read(AZ::Vector4& v) const + { + AZ_Assert(m_dataSize == sizeof(float) * 4, "We are expecting 4 floats for Vector4 element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 4); + m_isEndianSwap = false; + } + v = Vector4::CreateFromFloat4(data); + } + void DrillerSAXParser::Data::Read(AZ::Aabb& aabb) const + { + AZ_Assert(m_dataSize == sizeof(float) * 6, "We are expecting 6 floats for Aabb element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 6); + m_isEndianSwap = false; + } + Vector3 min = Vector3::CreateFromFloat3(data); + Vector3 max = Vector3::CreateFromFloat3(&data[3]); + aabb = Aabb::CreateFromMinMax(min, max); + } + void DrillerSAXParser::Data::Read(AZ::Obb& obb) const + { + AZ_Assert(m_dataSize == sizeof(float) * 10, "We are expecting 10 floats for Obb element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 10); + m_isEndianSwap = false; + } + Vector3 position = Vector3::CreateFromFloat3(data); + Quaternion rotation = Quaternion::CreateFromFloat4(&data[3]); + Vector3 halfLengths = Vector3::CreateFromFloat3(&data[7]); + obb = Obb::CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths); + } + void DrillerSAXParser::Data::Read(AZ::Transform& tm) const + { + AZ_Assert(m_dataSize == sizeof(float) * 12, "We are expecting 12 floats for Transform element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 12); + m_isEndianSwap = false; + } + const Matrix3x4 matrix3x4 = Matrix3x4::CreateFromRowMajorFloat12(data); + tm = Transform::CreateFromMatrix3x4(matrix3x4); + } + void DrillerSAXParser::Data::Read(AZ::Matrix3x3& tm) const + { + AZ_Assert(m_dataSize == sizeof(float) * 9, "We are expecting 9 floats for Matrix3x3 element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 9); + m_isEndianSwap = false; + } + tm = Matrix3x3::CreateFromRowMajorFloat9(data); + } + void DrillerSAXParser::Data::Read(AZ::Matrix4x4& tm) const + { + AZ_Assert(m_dataSize == sizeof(float) * 16, "We are expecting 16 floats for Matrix4x4 element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 16); + m_isEndianSwap = false; + } + tm = Matrix4x4::CreateFromRowMajorFloat16(data); + } + void DrillerSAXParser::Data::Read(AZ::Quaternion& tm) const + { + AZ_Assert(m_dataSize == sizeof(float) * 4, "We are expecting 4 floats for Quaternion element 0x%08x with size %d bytes", m_name, m_dataSize); + float* data = reinterpret_cast(m_data); + if (m_isEndianSwap) + { + AZStd::endian_swap(data, data + 4); + m_isEndianSwap = false; + } + tm = Quaternion::CreateFromFloat4(data); + } + void DrillerSAXParser::Data::Read(AZ::Plane& plane) const + { + AZ::Vector4 coeff; + Read(coeff); + plane = Plane::CreateFromCoefficients(coeff.GetX(), coeff.GetY(), coeff.GetZ(), coeff.GetW()); + } + + const char* DrillerSAXParser::Data::PrepareString(unsigned int& stringLength) const + { + const char* srcData = reinterpret_cast(m_data); + stringLength = m_dataSize; + if (m_stringPool) + { + AZ::u32 crc32; + const char* stringPtr; + if (m_isPooledStringCrc32) + { + crc32 = *reinterpret_cast(m_data); + if (m_isEndianSwap) { - // Store copy of the string in the pool to save memory (keep only one reference of the string). - m_stringPool->InsertCopy(reinterpret_cast(srcData), stringLength, crc32, &stringPtr); + AZStd::endian_swap(crc32); } - srcData = stringPtr; + stringPtr = m_stringPool->Find(crc32); + AZ_Assert(stringPtr != nullptr, "Failed to find string with id 0x%08x in the string pool, proper stream read is impossible!", crc32); + stringLength = static_cast(strlen(stringPtr)); + } + else if (m_isPooledString) + { + stringPtr = srcData; // already stored in the pool just transfer the pointer } else { - AZ_Assert(m_isPooledString == false && m_isPooledStringCrc32 == false, "This stream requires using of a string pool as the string is send only once and afterwards only the Crc32 is used!"); + // Store copy of the string in the pool to save memory (keep only one reference of the string). + m_stringPool->InsertCopy(reinterpret_cast(srcData), stringLength, crc32, &stringPtr); } - return srcData; + srcData = stringPtr; + } + else + { + AZ_Assert(m_isPooledString == false && m_isPooledStringCrc32 == false, "This stream requires using of a string pool as the string is send only once and afterwards only the Crc32 is used!"); + } + return srcData; + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // DrillerDOMParser + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + + //========================================================================= + // Node::GetTag + // [1/23/2013] + //========================================================================= + const DrillerDOMParser::Node* DrillerDOMParser::Node::GetTag(u32 tagName) const + { + const Node* tagNode = nullptr; + for (Node::NodeListType::const_iterator i = m_tags.begin(); i != m_tags.end(); ++i) + { + if ((*i).m_name == tagName) + { + tagNode = &*i; + break; + } + } + return tagNode; + } + + //========================================================================= + // Node::GetData + // [3/23/2011] + //========================================================================= + const DrillerDOMParser::Data* DrillerDOMParser::Node::GetData(u32 dataName) const + { + const Data* dataNode = nullptr; + for (Node::DataListType::const_iterator i = m_data.begin(); i != m_data.end(); ++i) + { + if (i->m_name == dataName) + { + dataNode = &*i; + break; + } + } + return dataNode; + } + + //========================================================================= + // DrillerDOMParser + // [3/23/2011] + //========================================================================= + DrillerDOMParser::DrillerDOMParser(bool isPersistentInputData) + : DrillerSAXParser(TagCallbackType(this, &DrillerDOMParser::OnTag), DataCallbackType(this, &DrillerDOMParser::OnData)) + , m_isPersistentInputData(isPersistentInputData) + { + m_root.m_name = 0; + m_root.m_parent = nullptr; + m_topNode = &m_root; + } + static int g_numFree = 0; + //========================================================================= + // ~DrillerDOMParser + // [3/23/2011] + //========================================================================= + DrillerDOMParser::~DrillerDOMParser() + { + DeleteNode(m_root); + } + + //========================================================================= + // OnTag + // [3/23/2011] + //========================================================================= + void + DrillerDOMParser::OnTag(AZ::u32 name, bool isOpen) + { + if (isOpen) + { + m_topNode->m_tags.push_back(); + Node& node = m_topNode->m_tags.back(); + node.m_name = name; + node.m_parent = m_topNode; + + m_topNode = &node; + } + else + { + AZ_Assert(m_topNode->m_name == name, "We have opened tag with name 0x%08x and closing with name 0x%08x", m_topNode->m_name, name); + m_topNode = m_topNode->m_parent; + } + } + //========================================================================= + // OnData + // [3/23/2011] + //========================================================================= + void + DrillerDOMParser::OnData(const Data& data) + { + Data de = data; + if (!m_isPersistentInputData) + { + de.m_data = azmalloc(data.m_dataSize, 1, OSAllocator); + memcpy(const_cast(de.m_data), data.m_data, data.m_dataSize); + } + m_topNode->m_data.push_back(de); + } + //========================================================================= + // DeleteNode + // [3/23/2011] + //========================================================================= + void + DrillerDOMParser::DeleteNode(Node& node) + { + if (!m_isPersistentInputData) + { + for (Node::DataListType::iterator iter = node.m_data.begin(); iter != node.m_data.end(); ++iter) + { + azfree(iter->m_data, OSAllocator, iter->m_dataSize); + ++g_numFree; + } + node.m_data.clear(); + } + for (Node::NodeListType::iterator iter = node.m_tags.begin(); iter != node.m_tags.end(); ++iter) + { + DeleteNode(*iter); + } + node.m_tags.clear(); + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // DrillerSAXParserHandler + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + + //========================================================================= + // DrillerSAXParserHandler + // [3/14/2013] + //========================================================================= + DrillerSAXParserHandler::DrillerSAXParserHandler(DrillerHandlerParser* rootHandler) + : DrillerSAXParser(TagCallbackType(this, &DrillerSAXParserHandler::OnTag), DataCallbackType(this, &DrillerSAXParserHandler::OnData)) + { + // Push the root element + m_stack.push_back(rootHandler); + } + + //========================================================================= + // OnTag + // [3/14/2013] + //========================================================================= + void DrillerSAXParserHandler::OnTag(u32 name, bool isOpen) + { + if (m_stack.empty()) + { + return; } - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // DrillerDOMParser - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - - //========================================================================= - // Node::GetTag - // [1/23/2013] - //========================================================================= - const DrillerDOMParser::Node* DrillerDOMParser::Node::GetTag(u32 tagName) const + DrillerHandlerParser* childHandler = nullptr; + DrillerHandlerParser* currentHandler = m_stack.back(); + if (isOpen) { - const Node* tagNode = nullptr; - for (Node::NodeListType::const_iterator i = m_tags.begin(); i != m_tags.end(); ++i) + if (currentHandler != nullptr) { - if ((*i).m_name == tagName) + childHandler = currentHandler->OnEnterTag(name); + AZ_Warning("Driller", !currentHandler->IsWarnOnUnsupportedTags() || childHandler != nullptr, "Could not find handler for tag 0x%08x", name); + } + m_stack.push_back(childHandler); + } + else + { + m_stack.pop_back(); + if (!m_stack.empty()) + { + DrillerHandlerParser* parentHandler = m_stack.back(); + if (parentHandler) { - tagNode = &*i; - break; - } - } - return tagNode; - } - - //========================================================================= - // Node::GetData - // [3/23/2011] - //========================================================================= - const DrillerDOMParser::Data* DrillerDOMParser::Node::GetData(u32 dataName) const - { - const Data* dataNode = nullptr; - for (Node::DataListType::const_iterator i = m_data.begin(); i != m_data.end(); ++i) - { - if (i->m_name == dataName) - { - dataNode = &*i; - break; - } - } - return dataNode; - } - - //========================================================================= - // DrillerDOMParser - // [3/23/2011] - //========================================================================= - DrillerDOMParser::DrillerDOMParser(bool isPersistentInputData) - : DrillerSAXParser(TagCallbackType(this, &DrillerDOMParser::OnTag), DataCallbackType(this, &DrillerDOMParser::OnData)) - , m_isPersistentInputData(isPersistentInputData) - { - m_root.m_name = 0; - m_root.m_parent = nullptr; - m_topNode = &m_root; - } - static int g_numFree = 0; - //========================================================================= - // ~DrillerDOMParser - // [3/23/2011] - //========================================================================= - DrillerDOMParser::~DrillerDOMParser() - { - DeleteNode(m_root); - } - - //========================================================================= - // OnTag - // [3/23/2011] - //========================================================================= - void - DrillerDOMParser::OnTag(AZ::u32 name, bool isOpen) - { - if (isOpen) - { - m_topNode->m_tags.push_back(); - Node& node = m_topNode->m_tags.back(); - node.m_name = name; - node.m_parent = m_topNode; - - m_topNode = &node; - } - else - { - AZ_Assert(m_topNode->m_name == name, "We have opened tag with name 0x%08x and closing with name 0x%08x", m_topNode->m_name, name); - m_topNode = m_topNode->m_parent; - } - } - //========================================================================= - // OnData - // [3/23/2011] - //========================================================================= - void - DrillerDOMParser::OnData(const Data& data) - { - Data de = data; - if (!m_isPersistentInputData) - { - de.m_data = azmalloc(data.m_dataSize, 1, OSAllocator); - memcpy(const_cast(de.m_data), data.m_data, data.m_dataSize); - } - m_topNode->m_data.push_back(de); - } - //========================================================================= - // DeleteNode - // [3/23/2011] - //========================================================================= - void - DrillerDOMParser::DeleteNode(Node& node) - { - if (!m_isPersistentInputData) - { - for (Node::DataListType::iterator iter = node.m_data.begin(); iter != node.m_data.end(); ++iter) - { - azfree(iter->m_data, OSAllocator, iter->m_dataSize); - ++g_numFree; - } - node.m_data.clear(); - } - for (Node::NodeListType::iterator iter = node.m_tags.begin(); iter != node.m_tags.end(); ++iter) - { - DeleteNode(*iter); - } - node.m_tags.clear(); - } - - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // DrillerSAXParserHandler - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - - //========================================================================= - // DrillerSAXParserHandler - // [3/14/2013] - //========================================================================= - DrillerSAXParserHandler::DrillerSAXParserHandler(DrillerHandlerParser* rootHandler) - : DrillerSAXParser(TagCallbackType(this, &DrillerSAXParserHandler::OnTag), DataCallbackType(this, &DrillerSAXParserHandler::OnData)) - { - // Push the root element - m_stack.push_back(rootHandler); - } - - //========================================================================= - // OnTag - // [3/14/2013] - //========================================================================= - void DrillerSAXParserHandler::OnTag(u32 name, bool isOpen) - { - if (m_stack.size() == 0) - { - return; - } - - DrillerHandlerParser* childHandler = nullptr; - DrillerHandlerParser* currentHandler = m_stack.back(); - if (isOpen) - { - if (currentHandler != nullptr) - { - childHandler = currentHandler->OnEnterTag(name); - AZ_Warning("Driller", !currentHandler->IsWarnOnUnsupportedTags() || childHandler != nullptr, "Could not find handler for tag 0x%08x", name); - } - m_stack.push_back(childHandler); - } - else - { - m_stack.pop_back(); - if (m_stack.size() > 0) - { - DrillerHandlerParser* parentHandler = m_stack.back(); - if (parentHandler) - { - parentHandler->OnExitTag(currentHandler, name); - } + parentHandler->OnExitTag(currentHandler, name); } } } + } - //========================================================================= - // OnData - // [3/14/2013] - //========================================================================= - void DrillerSAXParserHandler::OnData(const DrillerSAXParser::Data& data) + //========================================================================= + // OnData + // [3/14/2013] + //========================================================================= + void DrillerSAXParserHandler::OnData(const DrillerSAXParser::Data& data) + { + if (m_stack.empty()) { - if (m_stack.size() == 0) - { - return; - } - - DrillerHandlerParser* currentHandler = m_stack.back(); - if (currentHandler) - { - currentHandler->OnData(data); - } + return; } - } // namespace Debug -} // namespace AZ + + DrillerHandlerParser* currentHandler = m_stack.back(); + if (currentHandler) + { + currentHandler->OnData(data); + } + } +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/EBus/EventSchedulerSystemComponent.cpp b/Code/Framework/AzCore/AzCore/EBus/EventSchedulerSystemComponent.cpp index 3cdc3fc461..dd92b340c5 100644 --- a/Code/Framework/AzCore/AzCore/EBus/EventSchedulerSystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/EBus/EventSchedulerSystemComponent.cpp @@ -60,7 +60,7 @@ namespace AZ void EventSchedulerSystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) { - TimeMs startTime = GetElapsedTimeMs(); + TimeMs startTime = AZ::GetElapsedTimeMs(); bool usingTimeslice = bg_maxScheduledEventProcessTimeMs != TimeMs{ 0 }; while (!m_queue.empty()) @@ -76,7 +76,7 @@ namespace AZ while (!m_pendingQueue.empty()) { - if (usingTimeslice && (GetElapsedTimeMs() - startTime > bg_maxScheduledEventProcessTimeMs)) + if (usingTimeslice && (AZ::GetElapsedTimeMs() - startTime > bg_maxScheduledEventProcessTimeMs)) { AZLOG_WARN("Failed to trigger all pending scheduled events, %u events remain on the pending queue", aznumeric_cast(m_pendingQueue.size())); break; @@ -103,7 +103,7 @@ namespace AZ durationMs = TimeMs{ 0 }; } - TimeMs currentMilliseconds = GetElapsedTimeMs(); + TimeMs currentMilliseconds = AZ::GetElapsedTimeMs(); if (timedEvent->m_handle == nullptr) { timedEvent->m_handle = AllocateHandle(); @@ -122,7 +122,7 @@ namespace AZ durationMs = TimeMs{ 0 }; } - TimeMs currentMilliseconds = GetElapsedTimeMs(); + TimeMs currentMilliseconds = AZ::GetElapsedTimeMs(); ScheduledEvent* timedEvent = AllocateManagedEvent(callback, eventName); const bool ownsScheduledEvent = true; *(timedEvent->m_handle) = ScheduledEventHandle(TimeMs(currentMilliseconds + durationMs), durationMs, timedEvent, ownsScheduledEvent); diff --git a/Code/Framework/AzCore/AzCore/EBus/ScheduledEvent.cpp b/Code/Framework/AzCore/AzCore/EBus/ScheduledEvent.cpp index 8e496c3dbc..41230958e4 100644 --- a/Code/Framework/AzCore/AzCore/EBus/ScheduledEvent.cpp +++ b/Code/Framework/AzCore/AzCore/EBus/ScheduledEvent.cpp @@ -76,7 +76,7 @@ namespace AZ TimeMs ScheduledEvent::TimeInQueueMs() const { - return GetElapsedTimeMs() - m_timeInserted; + return AZ::GetElapsedTimeMs() - m_timeInserted; } TimeMs ScheduledEvent::RemainingTimeInQueueMs() const diff --git a/Code/Framework/AzCore/AzCore/IO/CompressionBus.cpp b/Code/Framework/AzCore/AzCore/IO/CompressionBus.cpp index fb9f8841a2..7878ec6e9e 100644 --- a/Code/Framework/AzCore/AzCore/IO/CompressionBus.cpp +++ b/Code/Framework/AzCore/AzCore/IO/CompressionBus.cpp @@ -8,38 +8,35 @@ #include -namespace AZ +namespace AZ::IO { - namespace IO + CompressionInfo::CompressionInfo(CompressionInfo&& rhs) { - CompressionInfo::CompressionInfo(CompressionInfo&& rhs) - { - *this = AZStd::move(rhs); - } + *this = AZStd::move(rhs); + } - CompressionInfo& CompressionInfo::operator=(CompressionInfo&& rhs) - { - m_decompressor = AZStd::move(rhs.m_decompressor); - m_archiveFilename = AZStd::move(rhs.m_archiveFilename); - m_compressionTag = rhs.m_compressionTag; - m_offset = rhs.m_offset; - m_compressedSize = rhs.m_compressedSize; - m_uncompressedSize = rhs.m_uncompressedSize; - m_conflictResolution = rhs.m_conflictResolution; - m_isCompressed = rhs.m_isCompressed; - m_isSharedPak = rhs.m_isSharedPak; + CompressionInfo& CompressionInfo::operator=(CompressionInfo&& rhs) + { + m_decompressor = AZStd::move(rhs.m_decompressor); + m_archiveFilename = AZStd::move(rhs.m_archiveFilename); + m_compressionTag = rhs.m_compressionTag; + m_offset = rhs.m_offset; + m_compressedSize = rhs.m_compressedSize; + m_uncompressedSize = rhs.m_uncompressedSize; + m_conflictResolution = rhs.m_conflictResolution; + m_isCompressed = rhs.m_isCompressed; + m_isSharedPak = rhs.m_isSharedPak; - return *this; - } + return *this; + } - namespace CompressionUtils + namespace CompressionUtils + { + bool FindCompressionInfo(CompressionInfo& info, const AZStd::string_view filename) { - bool FindCompressionInfo(CompressionInfo& info, const AZStd::string_view filename) - { - bool result = false; - CompressionBus::Broadcast(&CompressionBus::Events::FindCompressionInfo, result, info, filename); - return result; - } + bool result = false; + CompressionBus::Broadcast(&CompressionBus::Events::FindCompressionInfo, result, info, filename); + return result; } } -} +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Compressor.cpp b/Code/Framework/AzCore/AzCore/IO/Compressor.cpp index e223730ce8..16527422ad 100644 --- a/Code/Framework/AzCore/AzCore/IO/Compressor.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Compressor.cpp @@ -10,32 +10,29 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + //========================================================================= + // WriteHeaderAndData + // [12/13/2012] + //========================================================================= + bool Compressor::WriteHeaderAndData(CompressorStream* compressorStream) { - //========================================================================= - // WriteHeaderAndData - // [12/13/2012] - //========================================================================= - bool Compressor::WriteHeaderAndData(CompressorStream* compressorStream) + AZ_Assert(compressorStream->CanWrite(), "Stream is not open for write!"); + AZ_Assert(compressorStream->GetCompressorData(), "Stream doesn't have attached compressor, call WriteCompressed first!"); + AZ_Assert(compressorStream->GetCompressorData()->m_compressor == this, "Invalid compressor data! Data belongs to a different compressor"); + CompressorHeader header; + header.SetAZCS(); + header.m_compressorId = GetTypeId(); + header.m_uncompressedSize = compressorStream->GetCompressorData()->m_uncompressedSize; + AZStd::endian_swap(header.m_compressorId); + AZStd::endian_swap(header.m_uncompressedSize); + GenericStream* baseStream = compressorStream->GetWrappedStream(); + if (baseStream->WriteAtOffset(sizeof(CompressorHeader), &header, 0U) == sizeof(CompressorHeader)) { - AZ_Assert(compressorStream->CanWrite(), "Stream is not open for write!"); - AZ_Assert(compressorStream->GetCompressorData(), "Stream doesn't have attached compressor, call WriteCompressed first!"); - AZ_Assert(compressorStream->GetCompressorData()->m_compressor == this, "Invalid compressor data! Data belongs to a different compressor"); - CompressorHeader header; - header.SetAZCS(); - header.m_compressorId = GetTypeId(); - header.m_uncompressedSize = compressorStream->GetCompressorData()->m_uncompressedSize; - AZStd::endian_swap(header.m_compressorId); - AZStd::endian_swap(header.m_uncompressedSize); - GenericStream* baseStream = compressorStream->GetWrappedStream(); - if (baseStream->WriteAtOffset(sizeof(CompressorHeader), &header, 0U) == sizeof(CompressorHeader)) - { - return true; - } - - return false; + return true; } - } // namespace IO -} // namespace AZ + + return false; + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Compressor.h b/Code/Framework/AzCore/AzCore/IO/Compressor.h index 9b910a0ea4..340366d82f 100644 --- a/Code/Framework/AzCore/AzCore/IO/Compressor.h +++ b/Code/Framework/AzCore/AzCore/IO/Compressor.h @@ -5,74 +5,67 @@ * SPDX-License-Identifier: Apache-2.0 OR MIT * */ -#ifndef AZCORE_IO_COMPRESSOR_H -#define AZCORE_IO_COMPRESSOR_H +#pragma once #include -namespace AZ +namespace AZ::IO { - namespace IO + class CompressorStream; + + /** + * Compressor/Decompressor base interface. + * Used for all stream compressors. + */ + class Compressor { - class CompressorStream; + public: + typedef AZ::u64 SizeType; + static const int m_maxHeaderSize = 4096; /// When we open a stream to check if it's compressed we read the first m_maxHeaderSize bytes. - /** - * Compressor/Decompressor base interface. - * Used for all stream compressors. - */ - class Compressor - { - public: - typedef AZ::u64 SizeType; - static const int m_maxHeaderSize = 4096; /// When we open a stream to check if it's compressed we read the first m_maxHeaderSize bytes. + virtual ~Compressor() {} + /// Return compressor type id. + virtual AZ::u32 GetTypeId() const = 0; + /// Called when we open a stream to Read for the first time. Data contains the first. dataSize <= m_maxHeaderSize. + virtual bool ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) = 0; + /// Called when we are about to start writing to a compressed stream. (Must be called first to write compressor header) + virtual bool WriteHeaderAndData(CompressorStream* stream); + /// Forwarded function from the Device when we from a compressed stream. + virtual SizeType Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) = 0; + /// Forwarded function from the Device when we write to a compressed stream. + virtual SizeType Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset = SizeType(-1)) = 0; + /// Write a seek point. + virtual bool WriteSeekPoint(CompressorStream* stream) { (void)stream; return false; } + /// Initializes Compressor for writing data. + virtual bool StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) { (void)stream; (void)compressionLevel; (void)autoSeekDataSize; return false; } + /// Called just before we close the stream. All compression data will be flushed and finalized. (You can't add data afterwards). + virtual bool Close(CompressorStream* stream) = 0; + }; - virtual ~Compressor() {} - /// Return compressor type id. - virtual AZ::u32 GetTypeId() const = 0; - /// Called when we open a stream to Read for the first time. Data contains the first. dataSize <= m_maxHeaderSize. - virtual bool ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) = 0; - /// Called when we are about to start writing to a compressed stream. (Must be called first to write compressor header) - virtual bool WriteHeaderAndData(CompressorStream* stream); - /// Forwarded function from the Device when we from a compressed stream. - virtual SizeType Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) = 0; - /// Forwarded function from the Device when we write to a compressed stream. - virtual SizeType Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset = SizeType(-1)) = 0; - /// Write a seek point. - virtual bool WriteSeekPoint(CompressorStream* stream) { (void)stream; return false; } - /// Initializes Compressor for writing data. - virtual bool StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) { (void)stream; (void)compressionLevel; (void)autoSeekDataSize; return false; } - /// Called just before we close the stream. All compression data will be flushed and finalized. (You can't add data afterwards). - virtual bool Close(CompressorStream* stream) = 0; - }; + /** + * Base compressor data assigned for all compressors. + */ + class CompressorData + { + public: + virtual ~CompressorData() {} - /** - * Base compressor data assigned for all compressors. - */ - class CompressorData - { - public: - virtual ~CompressorData() {} + Compressor* m_compressor; + AZ::u64 m_uncompressedSize; + }; - Compressor* m_compressor; - AZ::u64 m_uncompressedSize; - }; + /** + * All data is stored in network order (big endian). + */ + struct CompressorHeader + { + CompressorHeader() { m_azcs[0] = 0; m_azcs[1] = 0; m_azcs[2] = 0; m_azcs[3] = 0; } - /** - * All data is stored in network order (big endian). - */ - struct CompressorHeader - { - CompressorHeader() { m_azcs[0] = 0; m_azcs[1] = 0; m_azcs[2] = 0; m_azcs[3] = 0; } + bool IsValid() const { return (m_azcs[0] == 'A' && m_azcs[1] == 'Z' && m_azcs[2] == 'C' && m_azcs[3] == 'S'); } + void SetAZCS() { m_azcs[0] = 'A'; m_azcs[1] = 'Z'; m_azcs[2] = 'C'; m_azcs[3] = 'S'; } - inline bool IsValid() const { return (m_azcs[0] == 'A' && m_azcs[1] == 'Z' && m_azcs[2] == 'C' && m_azcs[3] == 'S'); } - void SetAZCS() { m_azcs[0] = 'A'; m_azcs[1] = 'Z'; m_azcs[2] = 'C'; m_azcs[3] = 'S'; } - - char m_azcs[4]; ///< String contains 'AZCS' AmaZon Compressed Stream - AZ::u32 m_compressorId; ///< Compression method. - AZ::u64 m_uncompressedSize; ///< Uncompressed file size. - }; - } // namespace IO -} // namespace AZ - -#endif // AZCORE_IO_COMPRESSOR_H -#pragma once + char m_azcs[4]; ///< String contains 'AZCS' AmaZon Compressed Stream + AZ::u32 m_compressorId; ///< Compression method. + AZ::u64 m_uncompressedSize; ///< Uncompressed file size. + }; +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/CompressorStream.cpp b/Code/Framework/AzCore/AzCore/IO/CompressorStream.cpp index 6f82cc428d..a1012ee404 100644 --- a/Code/Framework/AzCore/AzCore/IO/CompressorStream.cpp +++ b/Code/Framework/AzCore/AzCore/IO/CompressorStream.cpp @@ -15,9 +15,7 @@ #include #include -namespace AZ -{ -namespace IO +namespace AZ::IO { /*! \brief Constructs a compressor stream using the supplied filename and OpenFlags to open a file on disk @@ -300,7 +298,4 @@ Compressor* CompressorStream::CreateCompressor(AZ::u32 compressorId) return m_compressor.get(); } -} // namespace IO -} // namespace AZ - - +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/CompressorZLib.cpp b/Code/Framework/AzCore/AzCore/IO/CompressorZLib.cpp index f973c0e95a..03a218dbfe 100644 --- a/Code/Framework/AzCore/AzCore/IO/CompressorZLib.cpp +++ b/Code/Framework/AzCore/AzCore/IO/CompressorZLib.cpp @@ -13,543 +13,540 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + //========================================================================= + // CompressorZLib + // [12/13/2012] + //========================================================================= + CompressorZLib::CompressorZLib(unsigned int decompressionCachePerStream, unsigned int dataBufferSize) + : m_lastReadStream(nullptr) + , m_lastReadStreamOffset(0) + , m_lastReadStreamSize(0) + , m_compressedDataBuffer(nullptr) + , m_compressedDataBufferSize(dataBufferSize) + , m_compressedDataBufferUseCount(0) + , m_decompressionCachePerStream(decompressionCachePerStream) + { - //========================================================================= - // CompressorZLib - // [12/13/2012] - //========================================================================= - CompressorZLib::CompressorZLib(unsigned int decompressionCachePerStream, unsigned int dataBufferSize) - : m_lastReadStream(nullptr) - , m_lastReadStreamOffset(0) - , m_lastReadStreamSize(0) - , m_compressedDataBuffer(nullptr) - , m_compressedDataBufferSize(dataBufferSize) - , m_compressedDataBufferUseCount(0) - , m_decompressionCachePerStream(decompressionCachePerStream) + AZ_Assert((dataBufferSize % (32 * 1024)) == 0, "Data buffer size %d must be multiple of 32 KB!", dataBufferSize); + AZ_Assert((decompressionCachePerStream % (32 * 1024)) == 0, "Decompress cache size %d must be multiple of 32 KB!", decompressionCachePerStream); + } + //========================================================================= + // !CompressorZLib + // [12/13/2012] + //========================================================================= + CompressorZLib::~CompressorZLib() + { + AZ_Warning("IO", m_compressedDataBufferUseCount == 0, "CompressorZLib has it's data buffer still referenced, it means that %d compressed streams have NOT closed! Freeing data...", m_compressedDataBufferUseCount); + while (m_compressedDataBufferUseCount) { - AZ_Assert((dataBufferSize % (32 * 1024)) == 0, "Data buffer size %d must be multiple of 32 KB!", dataBufferSize); - AZ_Assert((decompressionCachePerStream % (32 * 1024)) == 0, "Decompress cache size %d must be multiple of 32 KB!", decompressionCachePerStream); + ReleaseDataBuffer(); } + } - //========================================================================= - // !CompressorZLib - // [12/13/2012] - //========================================================================= - CompressorZLib::~CompressorZLib() + //========================================================================= + // GetTypeId + // [12/13/2012] + //========================================================================= + AZ::u32 CompressorZLib::TypeId() + { + return AZ_CRC("ZLib", 0x73887d3a); + } + + //========================================================================= + // ReadHeaderAndData + // [12/13/2012] + //========================================================================= + bool CompressorZLib::ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) + { + if (stream->GetCompressorData() != nullptr) // we already have compressor data { - AZ_Warning("IO", m_compressedDataBufferUseCount == 0, "CompressorZLib has it's data buffer still referenced, it means that %d compressed streams have NOT closed! Freeing data...", m_compressedDataBufferUseCount); - while (m_compressedDataBufferUseCount) - { - ReleaseDataBuffer(); - } - } - - //========================================================================= - // GetTypeId - // [12/13/2012] - //========================================================================= - AZ::u32 CompressorZLib::TypeId() - { - return AZ_CRC("ZLib", 0x73887d3a); - } - - //========================================================================= - // ReadHeaderAndData - // [12/13/2012] - //========================================================================= - bool CompressorZLib::ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) - { - if (stream->GetCompressorData() != nullptr) // we already have compressor data - { - return false; - } - - // Read the ZLib header should be after the default compression header... - // We should not be in this function otherwise. - if (dataSize < sizeof(CompressorZLibHeader) + sizeof(ZLib::Header)) - { - AZ_Assert(false, "We did not read enough data, we have only %d bytes left in the buffer and we need %d!", dataSize, sizeof(CompressorZLibHeader) + sizeof(ZLib::Header)); - return false; - } - - AcquireDataBuffer(); - - CompressorZLibHeader* hdr = reinterpret_cast(data); - AZStd::endian_swap(hdr->m_numSeekPoints); - dataSize -= sizeof(CompressorZLibHeader); - data += sizeof(CompressorZLibHeader); - - CompressorZLibData* zlibData = aznew CompressorZLibData; - zlibData->m_compressor = this; - zlibData->m_uncompressedSize = 0; - zlibData->m_zlibHeader = *reinterpret_cast(data); - dataSize -= sizeof(zlibData->m_zlibHeader); - data += sizeof(zlibData->m_zlibHeader); - zlibData->m_decompressNextOffset = sizeof(CompressorHeader) + sizeof(CompressorZLibHeader) + sizeof(ZLib::Header); // start after the headers - - AZ_Assert(hdr->m_numSeekPoints > 0, "We should have at least one seek point for the entire stream!"); - - // go the end of the file and read all sync points. - SizeType compressedFileEnd = stream->GetLength(); - if (compressedFileEnd == 0) - { - delete zlibData; - return false; - } - - zlibData->m_seekPoints.resize(hdr->m_numSeekPoints); - SizeType dataToRead = sizeof(CompressorZLibSeekPoint) * static_cast(hdr->m_numSeekPoints); - SizeType seekPointOffset = compressedFileEnd - dataToRead; - AZ_Assert(seekPointOffset <= compressedFileEnd, "We have an invalid archive, this is impossible!"); - GenericStream* baseStream = stream->GetWrappedStream(); - if (baseStream->ReadAtOffset(dataToRead, zlibData->m_seekPoints.data(), seekPointOffset) != dataToRead) - { - delete zlibData; - return false; - } - for (size_t i = 0; i < zlibData->m_seekPoints.size(); ++i) - { - AZStd::endian_swap(zlibData->m_seekPoints[i].m_compressedOffset); - AZStd::endian_swap(zlibData->m_seekPoints[i].m_uncompressedOffset); - } - - if (m_decompressionCachePerStream) - { - zlibData->m_decompressedCache = reinterpret_cast(azmalloc(m_decompressionCachePerStream, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZLib")); - } - - zlibData->m_decompressLastOffset = seekPointOffset; // set the start address of the seek points as the last valid read address for the compressed stream. - - zlibData->m_zlib.StartDecompressor(&zlibData->m_zlibHeader); - - stream->SetCompressorData(zlibData); - - return true; - } - - //========================================================================= - // WriteHeaderAndData - // [12/13/2012] - //========================================================================= - bool CompressorZLib::WriteHeaderAndData(CompressorStream* stream) - { - if (!Compressor::WriteHeaderAndData(stream)) - { - return false; - } - - CompressorZLibData* compressorData = static_cast(stream->GetCompressorData()); - CompressorZLibHeader header; - header.m_numSeekPoints = static_cast(compressorData->m_seekPoints.size()); - AZStd::endian_swap(header.m_numSeekPoints); - GenericStream* baseStream = stream->GetWrappedStream(); - if (baseStream->WriteAtOffset(sizeof(header), &header, sizeof(CompressorHeader)) == sizeof(header)) - { - return true; - } - return false; } - //========================================================================= - // FillFromDecompressCache - // [12/14/2012] - //========================================================================= - inline CompressorZLib::SizeType CompressorZLib::FillFromDecompressCache(CompressorZLibData* zlibData, void*& buffer, SizeType& byteSize, SizeType& offset) + // Read the ZLib header should be after the default compression header... + // We should not be in this function otherwise. + if (dataSize < sizeof(CompressorZLibHeader) + sizeof(ZLib::Header)) { - SizeType firstOffsetInCache = zlibData->m_decompressedCacheOffset; - SizeType lastOffsetInCache = firstOffsetInCache + zlibData->m_decompressedCacheDataSize; - SizeType firstDataOffset = offset; - SizeType lastDataOffset = offset + byteSize; - SizeType numCopied = 0; - if (firstOffsetInCache < lastDataOffset && lastOffsetInCache > firstDataOffset) // check if there is data in the cache - { - size_t copyOffsetStart = 0; - size_t copyOffsetEnd = zlibData->m_decompressedCacheDataSize; - - size_t bufferCopyOffset = 0; - - if (firstOffsetInCache < firstDataOffset) - { - copyOffsetStart = static_cast(firstDataOffset - firstOffsetInCache); - } - else - { - bufferCopyOffset = static_cast(firstOffsetInCache - firstDataOffset); - } - - if (lastOffsetInCache >= lastDataOffset) - { - copyOffsetEnd -= static_cast(lastOffsetInCache - lastDataOffset); - } - else if (bufferCopyOffset > 0) // the cache block is in the middle of the data, we can't use it (since we need to split buffer request into 2) - { - return 0; - } - - numCopied = copyOffsetEnd - copyOffsetStart; - memcpy(static_cast(buffer) + bufferCopyOffset, zlibData->m_decompressedCache + copyOffsetStart, static_cast(numCopied)); - - // adjust pointers and sizes - byteSize -= numCopied; - if (bufferCopyOffset == 0) - { - // copied in the start - buffer = reinterpret_cast(buffer) + numCopied; - offset += numCopied; - } - } - - return numCopied; + AZ_Assert(false, "We did not read enough data, we have only %d bytes left in the buffer and we need %d!", dataSize, sizeof(CompressorZLibHeader) + sizeof(ZLib::Header)); + return false; } - //========================================================================= - // FillFromCompressedCache - // [12/17/2012] - //========================================================================= - inline CompressorZLib::SizeType CompressorZLib::FillCompressedBuffer(CompressorStream* stream) + AcquireDataBuffer(); + + CompressorZLibHeader* hdr = reinterpret_cast(data); + AZStd::endian_swap(hdr->m_numSeekPoints); + dataSize -= sizeof(CompressorZLibHeader); + data += sizeof(CompressorZLibHeader); + + CompressorZLibData* zlibData = aznew CompressorZLibData; + zlibData->m_compressor = this; + zlibData->m_uncompressedSize = 0; + zlibData->m_zlibHeader = *reinterpret_cast(data); + dataSize -= sizeof(zlibData->m_zlibHeader); + data += sizeof(zlibData->m_zlibHeader); + zlibData->m_decompressNextOffset = sizeof(CompressorHeader) + sizeof(CompressorZLibHeader) + sizeof(ZLib::Header); // start after the headers + + AZ_Assert(hdr->m_numSeekPoints > 0, "We should have at least one seek point for the entire stream!"); + + // go the end of the file and read all sync points. + SizeType compressedFileEnd = stream->GetLength(); + if (compressedFileEnd == 0) { - CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); - SizeType dataFromBuffer = 0; - if (stream == m_lastReadStream) // if the buffer is filled with data from the current stream, try to reuse - { - if (zlibData->m_decompressNextOffset > m_lastReadStreamOffset) - { - SizeType offsetInCache = zlibData->m_decompressNextOffset - m_lastReadStreamOffset; - if (offsetInCache < m_lastReadStreamSize) // last check if there is data overlap - { - // copy the usable part at the start of the - SizeType toMove = m_lastReadStreamSize - offsetInCache; - memmove(m_compressedDataBuffer, &m_compressedDataBuffer[static_cast(offsetInCache)], static_cast(toMove)); - dataFromBuffer += toMove; - } - } - } - - SizeType toReadFromStream = m_compressedDataBufferSize - dataFromBuffer; - SizeType readOffset = zlibData->m_decompressNextOffset + dataFromBuffer; - if (readOffset + toReadFromStream > zlibData->m_decompressLastOffset) - { - // don't read pass the end - AZ_Assert(readOffset <= zlibData->m_decompressLastOffset, "Read offset should always be before the end of stream!"); - toReadFromStream = zlibData->m_decompressLastOffset - readOffset; - } - - SizeType numReadFromStream = 0; - if (toReadFromStream) // if we did not reuse the whole buffer, read some data from the stream - { - GenericStream* baseStream = stream->GetWrappedStream(); - numReadFromStream = baseStream->ReadAtOffset(toReadFromStream, &m_compressedDataBuffer[static_cast(dataFromBuffer)], readOffset); - } - - // update what's actually in the read data buffer. - m_lastReadStream = stream; - m_lastReadStreamOffset = zlibData->m_decompressNextOffset; - m_lastReadStreamSize = dataFromBuffer + numReadFromStream; - return m_lastReadStreamSize; + delete zlibData; + return false; } - /** - * Helper class to find the best seek point for a specific offset. - */ - struct CompareUpper + zlibData->m_seekPoints.resize(hdr->m_numSeekPoints); + SizeType dataToRead = sizeof(CompressorZLibSeekPoint) * static_cast(hdr->m_numSeekPoints); + SizeType seekPointOffset = compressedFileEnd - dataToRead; + AZ_Assert(seekPointOffset <= compressedFileEnd, "We have an invalid archive, this is impossible!"); + GenericStream* baseStream = stream->GetWrappedStream(); + if (baseStream->ReadAtOffset(dataToRead, zlibData->m_seekPoints.data(), seekPointOffset) != dataToRead) { - inline bool operator()(const AZ::u64& offset, const CompressorZLibSeekPoint& sp) const {return offset < sp.m_uncompressedOffset; } - }; - - //========================================================================= - // Read - // [12/13/2012] - //========================================================================= - CompressorZLib::SizeType CompressorZLib::Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) + delete zlibData; + return false; + } + for (size_t i = 0; i < zlibData->m_seekPoints.size(); ++i) { - AZ_Assert(stream->GetCompressorData(), "This stream doesn't have decompression enabled!"); - CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); - AZ_Assert(!zlibData->m_zlib.IsCompressorStarted(), "You can't read/decompress while writing a compressed stream %s!"); + AZStd::endian_swap(zlibData->m_seekPoints[i].m_compressedOffset); + AZStd::endian_swap(zlibData->m_seekPoints[i].m_uncompressedOffset); + } - // check if the request can be finished from the decompressed cache - SizeType numRead = FillFromDecompressCache(zlibData, buffer, byteSize, offset); - if (byteSize == 0) // are we done + if (m_decompressionCachePerStream) + { + zlibData->m_decompressedCache = reinterpret_cast(azmalloc(m_decompressionCachePerStream, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZLib")); + } + + zlibData->m_decompressLastOffset = seekPointOffset; // set the start address of the seek points as the last valid read address for the compressed stream. + + zlibData->m_zlib.StartDecompressor(&zlibData->m_zlibHeader); + + stream->SetCompressorData(zlibData); + + return true; + } + + //========================================================================= + // WriteHeaderAndData + // [12/13/2012] + //========================================================================= + bool CompressorZLib::WriteHeaderAndData(CompressorStream* stream) + { + if (!Compressor::WriteHeaderAndData(stream)) + { + return false; + } + + CompressorZLibData* compressorData = static_cast(stream->GetCompressorData()); + CompressorZLibHeader header; + header.m_numSeekPoints = static_cast(compressorData->m_seekPoints.size()); + AZStd::endian_swap(header.m_numSeekPoints); + GenericStream* baseStream = stream->GetWrappedStream(); + if (baseStream->WriteAtOffset(sizeof(header), &header, sizeof(CompressorHeader)) == sizeof(header)) + { + return true; + } + + return false; + } + + //========================================================================= + // FillFromDecompressCache + // [12/14/2012] + //========================================================================= + inline CompressorZLib::SizeType CompressorZLib::FillFromDecompressCache(CompressorZLibData* zlibData, void*& buffer, SizeType& byteSize, SizeType& offset) + { + SizeType firstOffsetInCache = zlibData->m_decompressedCacheOffset; + SizeType lastOffsetInCache = firstOffsetInCache + zlibData->m_decompressedCacheDataSize; + SizeType firstDataOffset = offset; + SizeType lastDataOffset = offset + byteSize; + SizeType numCopied = 0; + if (firstOffsetInCache < lastDataOffset && lastOffsetInCache > firstDataOffset) // check if there is data in the cache + { + size_t copyOffsetStart = 0; + size_t copyOffsetEnd = zlibData->m_decompressedCacheDataSize; + + size_t bufferCopyOffset = 0; + + if (firstOffsetInCache < firstDataOffset) { - return numRead; + copyOffsetStart = static_cast(firstDataOffset - firstOffsetInCache); + } + else + { + bufferCopyOffset = static_cast(firstOffsetInCache - firstDataOffset); } - // find the best seek point for current offset - CompressorZLibData::SeekPointArray::iterator it = AZStd::upper_bound(zlibData->m_seekPoints.begin(), zlibData->m_seekPoints.end(), offset, CompareUpper()); - AZ_Assert(it != zlibData->m_seekPoints.begin(), "This should be impossible, we should always have a valid seek point at 0 offset!"); - const CompressorZLibSeekPoint& bestSeekPoint = *(--it); // get the previous (so it includes the current offset) - - // if read is continuous continue with decompression - bool isJumpToSeekPoint = false; - SizeType lastOffsetInCache = zlibData->m_decompressedCacheOffset + zlibData->m_decompressedCacheDataSize; - if (bestSeekPoint.m_uncompressedOffset > lastOffsetInCache) // if the best seek point is forward, jump forward to it. + if (lastOffsetInCache >= lastDataOffset) { - isJumpToSeekPoint = true; + copyOffsetEnd -= static_cast(lastOffsetInCache - lastDataOffset); } - else if (offset < lastOffsetInCache) // if the seek point is in the past and the requested offset is not in the cache jump back to it. + else if (bufferCopyOffset > 0) // the cache block is in the middle of the data, we can't use it (since we need to split buffer request into 2) { - isJumpToSeekPoint = true; + return 0; } - if (isJumpToSeekPoint) - { - zlibData->m_decompressNextOffset = bestSeekPoint.m_compressedOffset; // set next read point - zlibData->m_decompressedCacheOffset = bestSeekPoint.m_uncompressedOffset; // set uncompressed offset - zlibData->m_decompressedCacheDataSize = 0; // invalidate the cache - zlibData->m_zlib.ResetDecompressor(&zlibData->m_zlibHeader); // reset decompressor and setup the header. - } + numCopied = copyOffsetEnd - copyOffsetStart; + memcpy(static_cast(buffer) + bufferCopyOffset, zlibData->m_decompressedCache + copyOffsetStart, static_cast(numCopied)); - // decompress and move forward until the request is done - while (byteSize > 0) + // adjust pointers and sizes + byteSize -= numCopied; + if (bufferCopyOffset == 0) { - // fill buffer with compressed data - SizeType compressedDataSize = FillCompressedBuffer(stream); - if (compressedDataSize == 0) + // copied in the start + buffer = reinterpret_cast(buffer) + numCopied; + offset += numCopied; + } + } + + return numCopied; + } + + //========================================================================= + // FillFromCompressedCache + // [12/17/2012] + //========================================================================= + inline CompressorZLib::SizeType CompressorZLib::FillCompressedBuffer(CompressorStream* stream) + { + CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + SizeType dataFromBuffer = 0; + if (stream == m_lastReadStream) // if the buffer is filled with data from the current stream, try to reuse + { + if (zlibData->m_decompressNextOffset > m_lastReadStreamOffset) + { + SizeType offsetInCache = zlibData->m_decompressNextOffset - m_lastReadStreamOffset; + if (offsetInCache < m_lastReadStreamSize) // last check if there is data overlap { - return numRead; // we are done reading and obviously we did not managed to read all data + // copy the usable part at the start of the + SizeType toMove = m_lastReadStreamSize - offsetInCache; + memmove(m_compressedDataBuffer, &m_compressedDataBuffer[static_cast(offsetInCache)], static_cast(toMove)); + dataFromBuffer += toMove; } - unsigned int processedCompressedData = 0; - while (byteSize > 0 && processedCompressedData < compressedDataSize) // decompressed data either until we are done with the request (byteSize == 0) or we need to fill the compression buffer again. - { - // if we have data in the cache move to the next offset, we always move forward by default. - zlibData->m_decompressedCacheOffset += zlibData->m_decompressedCacheDataSize; - - // decompress in the cache buffer - u32 availDecompressedCacheSize = m_decompressionCachePerStream; // reset buffer size - unsigned int processed = zlibData->m_zlib.Decompress(&m_compressedDataBuffer[processedCompressedData], static_cast(compressedDataSize) - processedCompressedData, zlibData->m_decompressedCache, availDecompressedCacheSize); - zlibData->m_decompressedCacheDataSize = m_decompressionCachePerStream - availDecompressedCacheSize; - if (processed == 0) - { - break; // we processed everything we could, load more compressed data. - } - processedCompressedData += processed; - // fill what we can from the cache - numRead += FillFromDecompressCache(zlibData, buffer, byteSize, offset); - } - // update next read position the the compressed stream - zlibData->m_decompressNextOffset += processedCompressedData; } + } + + SizeType toReadFromStream = m_compressedDataBufferSize - dataFromBuffer; + SizeType readOffset = zlibData->m_decompressNextOffset + dataFromBuffer; + if (readOffset + toReadFromStream > zlibData->m_decompressLastOffset) + { + // don't read pass the end + AZ_Assert(readOffset <= zlibData->m_decompressLastOffset, "Read offset should always be before the end of stream!"); + toReadFromStream = zlibData->m_decompressLastOffset - readOffset; + } + + SizeType numReadFromStream = 0; + if (toReadFromStream) // if we did not reuse the whole buffer, read some data from the stream + { + GenericStream* baseStream = stream->GetWrappedStream(); + numReadFromStream = baseStream->ReadAtOffset(toReadFromStream, &m_compressedDataBuffer[static_cast(dataFromBuffer)], readOffset); + } + + // update what's actually in the read data buffer. + m_lastReadStream = stream; + m_lastReadStreamOffset = zlibData->m_decompressNextOffset; + m_lastReadStreamSize = dataFromBuffer + numReadFromStream; + return m_lastReadStreamSize; + } + + /** + * Helper class to find the best seek point for a specific offset. + */ + struct CompareUpper + { + inline bool operator()(const AZ::u64& offset, const CompressorZLibSeekPoint& sp) const {return offset < sp.m_uncompressedOffset; } + }; + + //========================================================================= + // Read + // [12/13/2012] + //========================================================================= + CompressorZLib::SizeType CompressorZLib::Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) + { + AZ_Assert(stream->GetCompressorData(), "This stream doesn't have decompression enabled!"); + CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + AZ_Assert(!zlibData->m_zlib.IsCompressorStarted(), "You can't read/decompress while writing a compressed stream %s!"); + + // check if the request can be finished from the decompressed cache + SizeType numRead = FillFromDecompressCache(zlibData, buffer, byteSize, offset); + if (byteSize == 0) // are we done + { return numRead; } - //========================================================================= - // Write - // [12/13/2012] - //========================================================================= - CompressorZLib::SizeType CompressorZLib::Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset) + // find the best seek point for current offset + CompressorZLibData::SeekPointArray::iterator it = AZStd::upper_bound(zlibData->m_seekPoints.begin(), zlibData->m_seekPoints.end(), offset, CompareUpper()); + AZ_Assert(it != zlibData->m_seekPoints.begin(), "This should be impossible, we should always have a valid seek point at 0 offset!"); + const CompressorZLibSeekPoint& bestSeekPoint = *(--it); // get the previous (so it includes the current offset) + + // if read is continuous continue with decompression + bool isJumpToSeekPoint = false; + SizeType lastOffsetInCache = zlibData->m_decompressedCacheOffset + zlibData->m_decompressedCacheDataSize; + if (bestSeekPoint.m_uncompressedOffset > lastOffsetInCache) // if the best seek point is forward, jump forward to it. { - (void)offset; + isJumpToSeekPoint = true; + } + else if (offset < lastOffsetInCache) // if the seek point is in the past and the requested offset is not in the cache jump back to it. + { + isJumpToSeekPoint = true; + } - AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled! Call Stream::WriteCompressed after you create the file!"); - AZ_Assert(offset == SizeType(-1) || offset == stream->GetCurPos(), "We can write compressed data only at the end of the stream!"); + if (isJumpToSeekPoint) + { + zlibData->m_decompressNextOffset = bestSeekPoint.m_compressedOffset; // set next read point + zlibData->m_decompressedCacheOffset = bestSeekPoint.m_uncompressedOffset; // set uncompressed offset + zlibData->m_decompressedCacheDataSize = 0; // invalidate the cache + zlibData->m_zlib.ResetDecompressor(&zlibData->m_zlibHeader); // reset decompressor and setup the header. + } - m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). - - CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); - AZ_Assert(!zlibData->m_zlib.IsDecompressorStarted(), "You can't write while reading/decompressing a compressed stream!"); - - const u8* bytes = reinterpret_cast(data); - unsigned int dataToCompress = static_cast(byteSize); - while (dataToCompress != 0) + // decompress and move forward until the request is done + while (byteSize > 0) + { + // fill buffer with compressed data + SizeType compressedDataSize = FillCompressedBuffer(stream); + if (compressedDataSize == 0) { - unsigned int oldDataToCompress = dataToCompress; - unsigned int compressedSize = zlibData->m_zlib.Compress(bytes, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize); - if (compressedSize) - { - GenericStream* baseStream = stream->GetWrappedStream(); - SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); - if (numWritten != compressedSize) - { - return numWritten; // error we could not write all data - } - } - bytes += oldDataToCompress - dataToCompress; + return numRead; // we are done reading and obviously we did not managed to read all data } - zlibData->m_uncompressedSize += byteSize; - - if (zlibData->m_autoSeekSize > 0) + unsigned int processedCompressedData = 0; + while (byteSize > 0 && processedCompressedData < compressedDataSize) // decompressed data either until we are done with the request (byteSize == 0) or we need to fill the compression buffer again. { - // insert a seek point if needed. - if (zlibData->m_seekPoints.empty()) + // if we have data in the cache move to the next offset, we always move forward by default. + zlibData->m_decompressedCacheOffset += zlibData->m_decompressedCacheDataSize; + + // decompress in the cache buffer + u32 availDecompressedCacheSize = m_decompressionCachePerStream; // reset buffer size + unsigned int processed = zlibData->m_zlib.Decompress(&m_compressedDataBuffer[processedCompressedData], static_cast(compressedDataSize) - processedCompressedData, zlibData->m_decompressedCache, availDecompressedCacheSize); + zlibData->m_decompressedCacheDataSize = m_decompressionCachePerStream - availDecompressedCacheSize; + if (processed == 0) { - if (zlibData->m_uncompressedSize >= zlibData->m_autoSeekSize) - { - WriteSeekPoint(stream); - } + break; // we processed everything we could, load more compressed data. } - else if ((zlibData->m_uncompressedSize - zlibData->m_seekPoints.back().m_uncompressedOffset) > zlibData->m_autoSeekSize) + processedCompressedData += processed; + // fill what we can from the cache + numRead += FillFromDecompressCache(zlibData, buffer, byteSize, offset); + } + // update next read position the the compressed stream + zlibData->m_decompressNextOffset += processedCompressedData; + } + return numRead; + } + + //========================================================================= + // Write + // [12/13/2012] + //========================================================================= + CompressorZLib::SizeType CompressorZLib::Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset) + { + (void)offset; + + AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled! Call Stream::WriteCompressed after you create the file!"); + AZ_Assert(offset == SizeType(-1) || offset == stream->GetCurPos(), "We can write compressed data only at the end of the stream!"); + + m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + + CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + AZ_Assert(!zlibData->m_zlib.IsDecompressorStarted(), "You can't write while reading/decompressing a compressed stream!"); + + const u8* bytes = reinterpret_cast(data); + unsigned int dataToCompress = static_cast(byteSize); + while (dataToCompress != 0) + { + unsigned int oldDataToCompress = dataToCompress; + unsigned int compressedSize = zlibData->m_zlib.Compress(bytes, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize); + if (compressedSize) + { + GenericStream* baseStream = stream->GetWrappedStream(); + SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); + if (numWritten != compressedSize) + { + return numWritten; // error we could not write all data + } + } + bytes += oldDataToCompress - dataToCompress; + } + zlibData->m_uncompressedSize += byteSize; + + if (zlibData->m_autoSeekSize > 0) + { + // insert a seek point if needed. + if (zlibData->m_seekPoints.empty()) + { + if (zlibData->m_uncompressedSize >= zlibData->m_autoSeekSize) { WriteSeekPoint(stream); } } - return byteSize; + else if ((zlibData->m_uncompressedSize - zlibData->m_seekPoints.back().m_uncompressedOffset) > zlibData->m_autoSeekSize) + { + WriteSeekPoint(stream); + } } + return byteSize; + } - //========================================================================= - // WriteSeekPoint - // [12/13/2012] - //========================================================================= - bool CompressorZLib::WriteSeekPoint(CompressorStream* stream) + //========================================================================= + // WriteSeekPoint + // [12/13/2012] + //========================================================================= + bool CompressorZLib::WriteSeekPoint(CompressorStream* stream) + { + AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled! Call Stream::WriteCompressed after you create the file!"); + CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + + m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + + unsigned int compressedSize; + unsigned int dataToCompress = 0; + do { - AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled! Call Stream::WriteCompressed after you create the file!"); - CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + compressedSize = zlibData->m_zlib.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZLib::FT_FULL_FLUSH); + if (compressedSize) + { + GenericStream* baseStream = stream->GetWrappedStream(); + SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); + if (numWritten != compressedSize) + { + return false; // error we wrote less than than requested! + } + } + } while (dataToCompress != 0); + CompressorZLibSeekPoint sp; + sp.m_compressedOffset = stream->GetLength(); + sp.m_uncompressedOffset = zlibData->m_uncompressedSize; + zlibData->m_seekPoints.push_back(sp); + return true; + } + + //========================================================================= + // StartCompressor + // [12/13/2012] + //========================================================================= + bool CompressorZLib::StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) + { + AZ_Assert(stream && stream->GetCompressorData() == nullptr, "Stream has compressor already enabled!"); + + AcquireDataBuffer(); + + CompressorZLibData* zlibData = aznew CompressorZLibData; + zlibData->m_compressor = this; + zlibData->m_zlibHeader = 0; // not used for compression + zlibData->m_uncompressedSize = 0; + zlibData->m_autoSeekSize = autoSeekDataSize; + compressionLevel = AZ::GetClamp(compressionLevel, 1, 9); // remap to zlib levels + + zlibData->m_zlib.StartCompressor(compressionLevel); + + stream->SetCompressorData(zlibData); + + if (WriteHeaderAndData(stream)) + { + // add the first and always present seek point at the start of the compressed stream + CompressorZLibSeekPoint sp; + sp.m_compressedOffset = sizeof(CompressorHeader) + sizeof(CompressorZLibHeader) + sizeof(zlibData->m_zlibHeader); + sp.m_uncompressedOffset = 0; + zlibData->m_seekPoints.push_back(sp); + return true; + } + return false; + } + + //========================================================================= + // Close + // [12/13/2012] + //========================================================================= + bool CompressorZLib::Close(CompressorStream* stream) + { + AZ_Assert(stream->IsOpen(), "Stream is not open to be closed!"); + + CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); + GenericStream* baseStream = stream->GetWrappedStream(); + + bool result = true; + if (zlibData->m_zlib.IsCompressorStarted()) + { m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + // flush all compressed data unsigned int compressedSize; unsigned int dataToCompress = 0; do { - compressedSize = zlibData->m_zlib.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZLib::FT_FULL_FLUSH); + compressedSize = zlibData->m_zlib.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZLib::FT_FINISH); if (compressedSize) { - GenericStream* baseStream = stream->GetWrappedStream(); - SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); - if (numWritten != compressedSize) - { - return false; // error we wrote less than than requested! - } + baseStream->Write(compressedSize, m_compressedDataBuffer); } } while (dataToCompress != 0); - CompressorZLibSeekPoint sp; - sp.m_compressedOffset = stream->GetLength(); - sp.m_uncompressedOffset = zlibData->m_uncompressedSize; - zlibData->m_seekPoints.push_back(sp); - return true; - } - - //========================================================================= - // StartCompressor - // [12/13/2012] - //========================================================================= - bool CompressorZLib::StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) - { - AZ_Assert(stream && stream->GetCompressorData() == nullptr, "Stream has compressor already enabled!"); - - AcquireDataBuffer(); - - CompressorZLibData* zlibData = aznew CompressorZLibData; - zlibData->m_compressor = this; - zlibData->m_zlibHeader = 0; // not used for compression - zlibData->m_uncompressedSize = 0; - zlibData->m_autoSeekSize = autoSeekDataSize; - compressionLevel = AZ::GetClamp(compressionLevel, 1, 9); // remap to zlib levels - - zlibData->m_zlib.StartCompressor(compressionLevel); - - stream->SetCompressorData(zlibData); - - if (WriteHeaderAndData(stream)) + result = WriteHeaderAndData(stream); + if (result) { - // add the first and always present seek point at the start of the compressed stream - CompressorZLibSeekPoint sp; - sp.m_compressedOffset = sizeof(CompressorHeader) + sizeof(CompressorZLibHeader) + sizeof(zlibData->m_zlibHeader); - sp.m_uncompressedOffset = 0; - zlibData->m_seekPoints.push_back(sp); - return true; - } - return false; - } - - //========================================================================= - // Close - // [12/13/2012] - //========================================================================= - bool CompressorZLib::Close(CompressorStream* stream) - { - AZ_Assert(stream->IsOpen(), "Stream is not open to be closed!"); - - CompressorZLibData* zlibData = static_cast(stream->GetCompressorData()); - GenericStream* baseStream = stream->GetWrappedStream(); - - bool result = true; - if (zlibData->m_zlib.IsCompressorStarted()) - { - m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). - - // flush all compressed data - unsigned int compressedSize; - unsigned int dataToCompress = 0; - do + // now write the seek points and the end of the file + for (size_t i = 0; i < zlibData->m_seekPoints.size(); ++i) { - compressedSize = zlibData->m_zlib.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZLib::FT_FINISH); - if (compressedSize) - { - baseStream->Write(compressedSize, m_compressedDataBuffer); - } - } while (dataToCompress != 0); - - result = WriteHeaderAndData(stream); - if (result) - { - // now write the seek points and the end of the file - for (size_t i = 0; i < zlibData->m_seekPoints.size(); ++i) - { - AZStd::endian_swap(zlibData->m_seekPoints[i].m_compressedOffset); - AZStd::endian_swap(zlibData->m_seekPoints[i].m_uncompressedOffset); - } - SizeType dataToWrite = zlibData->m_seekPoints.size() * sizeof(CompressorZLibSeekPoint); - baseStream->Seek(0U, GenericStream::SeekMode::ST_SEEK_END); - result = (baseStream->Write(dataToWrite, zlibData->m_seekPoints.data()) == dataToWrite); + AZStd::endian_swap(zlibData->m_seekPoints[i].m_compressedOffset); + AZStd::endian_swap(zlibData->m_seekPoints[i].m_uncompressedOffset); } + SizeType dataToWrite = zlibData->m_seekPoints.size() * sizeof(CompressorZLibSeekPoint); + baseStream->Seek(0U, GenericStream::SeekMode::ST_SEEK_END); + result = (baseStream->Write(dataToWrite, zlibData->m_seekPoints.data()) == dataToWrite); } - else - { - if (m_lastReadStream == stream) - { - m_lastReadStream = nullptr; // invalidate the data in m_dataBuffer if it was from the current stream. - } - } - - // if we have decompressor cache delete it - if (zlibData->m_decompressedCache) - { - azfree(zlibData->m_decompressedCache, AZ::SystemAllocator, m_decompressionCachePerStream, m_CompressedDataBufferAlignment); - } - - ReleaseDataBuffer(); - - // last step reset strream compressor data. - stream->SetCompressorData(nullptr); - return result; } - - //========================================================================= - // AcquireDataBuffer - // [2/27/2013] - //========================================================================= - void CompressorZLib::AcquireDataBuffer() + else { - if (m_compressedDataBuffer == nullptr) + if (m_lastReadStream == stream) { - AZ_Assert(m_compressedDataBufferUseCount == 0, "Buffer usecount should be 0 if the buffer is NULL"); - m_compressedDataBuffer = reinterpret_cast(azmalloc(m_compressedDataBufferSize, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZLib")); - m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer + m_lastReadStream = nullptr; // invalidate the data in m_dataBuffer if it was from the current stream. } - ++m_compressedDataBufferUseCount; } - //========================================================================= - // ReleaseDataBuffer - // [2/27/2013] - //========================================================================= - void CompressorZLib::ReleaseDataBuffer() + // if we have decompressor cache delete it + if (zlibData->m_decompressedCache) { - --m_compressedDataBufferUseCount; - if (m_compressedDataBufferUseCount == 0) - { - AZ_Assert(m_compressedDataBuffer != nullptr, "Invalid data buffer! We should have a non null pointer!"); - azfree(m_compressedDataBuffer, AZ::SystemAllocator, m_compressedDataBufferSize, m_CompressedDataBufferAlignment); - m_compressedDataBuffer = nullptr; - m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer - } + azfree(zlibData->m_decompressedCache, AZ::SystemAllocator, m_decompressionCachePerStream, m_CompressedDataBufferAlignment); } - } // namespace IO -} // namespace AZ + + ReleaseDataBuffer(); + + // last step reset strream compressor data. + stream->SetCompressorData(nullptr); + return result; + } + + //========================================================================= + // AcquireDataBuffer + // [2/27/2013] + //========================================================================= + void CompressorZLib::AcquireDataBuffer() + { + if (m_compressedDataBuffer == nullptr) + { + AZ_Assert(m_compressedDataBufferUseCount == 0, "Buffer usecount should be 0 if the buffer is NULL"); + m_compressedDataBuffer = reinterpret_cast(azmalloc(m_compressedDataBufferSize, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZLib")); + m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer + } + ++m_compressedDataBufferUseCount; + } + + //========================================================================= + // ReleaseDataBuffer + // [2/27/2013] + //========================================================================= + void CompressorZLib::ReleaseDataBuffer() + { + --m_compressedDataBufferUseCount; + if (m_compressedDataBufferUseCount == 0) + { + AZ_Assert(m_compressedDataBuffer != nullptr, "Invalid data buffer! We should have a non null pointer!"); + azfree(m_compressedDataBuffer, AZ::SystemAllocator, m_compressedDataBufferSize, m_CompressedDataBufferAlignment); + m_compressedDataBuffer = nullptr; + m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer + } + } +} // namespace AZ::IO #endif // #if !defined(AZCORE_EXCLUDE_ZLIB) diff --git a/Code/Framework/AzCore/AzCore/IO/CompressorZStd.cpp b/Code/Framework/AzCore/AzCore/IO/CompressorZStd.cpp index b1631d2d22..91f380d73b 100644 --- a/Code/Framework/AzCore/AzCore/IO/CompressorZStd.cpp +++ b/Code/Framework/AzCore/AzCore/IO/CompressorZStd.cpp @@ -14,478 +14,475 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + CompressorZStd::CompressorZStd(unsigned int decompressionCachePerStream, unsigned int dataBufferSize) + : m_compressedDataBufferSize(dataBufferSize) + , m_decompressionCachePerStream(decompressionCachePerStream) + { - CompressorZStd::CompressorZStd(unsigned int decompressionCachePerStream, unsigned int dataBufferSize) - : m_compressedDataBufferSize(dataBufferSize) - , m_decompressionCachePerStream(decompressionCachePerStream) + AZ_Assert((dataBufferSize % (32 * 1024)) == 0, "Data buffer size %d must be multiple of 32 KB.", dataBufferSize); + AZ_Assert((decompressionCachePerStream % (32 * 1024)) == 0, "Decompress cache size %d must be multiple of 32 KB.", decompressionCachePerStream); + } + CompressorZStd::~CompressorZStd() + { + AZ_Warning("IO", m_compressedDataBufferUseCount == 0, "CompressorZStd has it's data buffer still referenced, it means that %d compressed streams have NOT closed. Freeing data...", m_compressedDataBufferUseCount); + while (m_compressedDataBufferUseCount) { - AZ_Assert((dataBufferSize % (32 * 1024)) == 0, "Data buffer size %d must be multiple of 32 KB.", dataBufferSize); - AZ_Assert((decompressionCachePerStream % (32 * 1024)) == 0, "Decompress cache size %d must be multiple of 32 KB.", decompressionCachePerStream); + ReleaseDataBuffer(); } + } - CompressorZStd::~CompressorZStd() + AZ::u32 CompressorZStd::TypeId() + { + return AZ_CRC("ZStd", 0x72fd505e); + } + + bool CompressorZStd::ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) + { + if (stream->GetCompressorData() != nullptr) // we already have compressor data { - AZ_Warning("IO", m_compressedDataBufferUseCount == 0, "CompressorZStd has it's data buffer still referenced, it means that %d compressed streams have NOT closed. Freeing data...", m_compressedDataBufferUseCount); - while (m_compressedDataBufferUseCount) - { - ReleaseDataBuffer(); - } - } - - AZ::u32 CompressorZStd::TypeId() - { - return AZ_CRC("ZStd", 0x72fd505e); - } - - bool CompressorZStd::ReadHeaderAndData(CompressorStream* stream, AZ::u8* data, unsigned int dataSize) - { - if (stream->GetCompressorData() != nullptr) // we already have compressor data - { - return false; - } - - // Read the ZStd header should be after the default compression header... - // We should not be in this function otherwise. - if (dataSize < sizeof(CompressorZStdHeader) + sizeof(ZStd::Header)) - { - AZ_Error("CompressorZStd", false, "We did not read enough data, we have only %d bytes left in the buffer and we need %d.", dataSize, sizeof(CompressorZStdHeader) + sizeof(ZStd::Header)); - return false; - } - - AcquireDataBuffer(); - - CompressorZStdHeader* hdr = reinterpret_cast(data); - dataSize -= sizeof(CompressorZStdHeader); - data += sizeof(CompressorZStdHeader); - - AZStd::unique_ptr zstdData = AZStd::make_unique(); - zstdData->m_compressor = this; - zstdData->m_uncompressedSize = 0; - zstdData->m_zstdHeader = *reinterpret_cast(data); - dataSize -= sizeof(zstdData->m_zstdHeader); - data += sizeof(zstdData->m_zstdHeader); - zstdData->m_decompressNextOffset = sizeof(CompressorHeader) + sizeof(CompressorZStdHeader) + sizeof(ZStd::Header); // start after the headers - - AZ_Error("CompressorZStd", hdr->m_numSeekPoints > 0, "We should have at least one seek point for the entire stream."); - - // go the end of the file and read all sync points. - SizeType compressedFileEnd = stream->GetLength(); - if (compressedFileEnd == 0) - { - return false; - } - - zstdData->m_seekPoints.resize(hdr->m_numSeekPoints); - SizeType dataToRead = sizeof(CompressorZStdSeekPoint) * static_cast(hdr->m_numSeekPoints); - SizeType seekPointOffset = compressedFileEnd - dataToRead; - - if (seekPointOffset > compressedFileEnd) - { - AZ_Error("CompressorZStd", false, "We have an invalid archive, this is impossible."); - return false; - } - - GenericStream* baseStream = stream->GetWrappedStream(); - if (baseStream->ReadAtOffset(dataToRead, zstdData->m_seekPoints.data(), seekPointOffset) != dataToRead) - { - return false; - } - - if (m_decompressionCachePerStream) - { - zstdData->m_decompressedCache = reinterpret_cast(azmalloc(m_decompressionCachePerStream, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZStd")); - } - - zstdData->m_decompressLastOffset = seekPointOffset; // set the start address of the seek points as the last valid read address for the compressed stream. - - zstdData->m_zstd.StartDecompressor(); - - stream->SetCompressorData(zstdData.release()); - - return true; - } - - bool CompressorZStd::WriteHeaderAndData(CompressorStream* stream) - { - if (!Compressor::WriteHeaderAndData(stream)) - { - return false; - } - - CompressorZStdData* compressorData = static_cast(stream->GetCompressorData()); - CompressorZStdHeader header; - header.m_numSeekPoints = aznumeric_caster(compressorData->m_seekPoints.size()); - GenericStream* baseStream = stream->GetWrappedStream(); - if (baseStream->WriteAtOffset(sizeof(header), &header, sizeof(CompressorHeader)) == sizeof(header)) - { - return true; - } - return false; } - inline CompressorZStd::SizeType CompressorZStd::FillFromDecompressCache(CompressorZStdData* zstdData, void*& buffer, SizeType& byteSize, SizeType& offset) + // Read the ZStd header should be after the default compression header... + // We should not be in this function otherwise. + if (dataSize < sizeof(CompressorZStdHeader) + sizeof(ZStd::Header)) { - SizeType firstOffsetInCache = zstdData->m_decompressedCacheOffset; - SizeType lastOffsetInCache = firstOffsetInCache + zstdData->m_decompressedCacheDataSize; - SizeType firstDataOffset = offset; - SizeType lastDataOffset = offset + byteSize; - SizeType numCopied = 0; - if (firstOffsetInCache < lastDataOffset && lastOffsetInCache >= firstDataOffset) // check if there is data in the cache - { - size_t copyOffsetStart = 0; - size_t copyOffsetEnd = zstdData->m_decompressedCacheDataSize; - - size_t bufferCopyOffset = 0; - - if (firstOffsetInCache < firstDataOffset) - { - copyOffsetStart = aznumeric_caster(firstDataOffset - firstOffsetInCache); - } - else - { - bufferCopyOffset = aznumeric_caster(firstOffsetInCache - firstDataOffset); - } - - if (lastOffsetInCache >= lastDataOffset) - { - copyOffsetEnd -= static_cast(lastOffsetInCache - lastDataOffset); - } - else if (bufferCopyOffset > 0) // the cache block is in the middle of the data, we can't use it (since we need to split buffer request into 2) - { - return 0; - } - - numCopied = copyOffsetEnd - copyOffsetStart; - memcpy(static_cast(buffer) + bufferCopyOffset, zstdData->m_decompressedCache + copyOffsetStart, static_cast(numCopied)); - - // adjust pointers and sizes - byteSize -= numCopied; - if (bufferCopyOffset == 0) - { - // copied in the start - buffer = reinterpret_cast(buffer) + numCopied; - offset += numCopied; - } - } - - return numCopied; + AZ_Error("CompressorZStd", false, "We did not read enough data, we have only %d bytes left in the buffer and we need %d.", dataSize, sizeof(CompressorZStdHeader) + sizeof(ZStd::Header)); + return false; } - inline CompressorZStd::SizeType CompressorZStd::FillCompressedBuffer(CompressorStream* stream) + AcquireDataBuffer(); + + CompressorZStdHeader* hdr = reinterpret_cast(data); + dataSize -= sizeof(CompressorZStdHeader); + data += sizeof(CompressorZStdHeader); + + AZStd::unique_ptr zstdData = AZStd::make_unique(); + zstdData->m_compressor = this; + zstdData->m_uncompressedSize = 0; + zstdData->m_zstdHeader = *reinterpret_cast(data); + dataSize -= sizeof(zstdData->m_zstdHeader); + data += sizeof(zstdData->m_zstdHeader); + zstdData->m_decompressNextOffset = sizeof(CompressorHeader) + sizeof(CompressorZStdHeader) + sizeof(ZStd::Header); // start after the headers + + AZ_Error("CompressorZStd", hdr->m_numSeekPoints > 0, "We should have at least one seek point for the entire stream."); + + // go the end of the file and read all sync points. + SizeType compressedFileEnd = stream->GetLength(); + if (compressedFileEnd == 0) { - CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); - SizeType dataFromBuffer = 0; - if (stream == m_lastReadStream) // if the buffer is filled with data from the current stream, try to reuse - { - if (zstdData->m_decompressNextOffset > m_lastReadStreamOffset) - { - SizeType offsetInCache = zstdData->m_decompressNextOffset - m_lastReadStreamOffset; - if (offsetInCache < m_lastReadStreamSize) // last check if there is data overlap - { - // copy the usable part at the start of the buffer - SizeType toMove = m_lastReadStreamSize - offsetInCache; - memcpy(m_compressedDataBuffer, &m_compressedDataBuffer[static_cast(offsetInCache)], static_cast(toMove)); - dataFromBuffer += toMove; - } - } - } - - SizeType toReadFromStream = m_compressedDataBufferSize - dataFromBuffer; - SizeType readOffset = zstdData->m_decompressNextOffset + dataFromBuffer; - if (readOffset + toReadFromStream > zstdData->m_decompressLastOffset) - { - // don't read past the end - AZ_Assert(readOffset <= zstdData->m_decompressLastOffset, "Read offset should always be before the end of stream."); - toReadFromStream = zstdData->m_decompressLastOffset - readOffset; - } - - SizeType numReadFromStream = 0; - if (toReadFromStream) // if we did not reuse the whole buffer, read some data from the stream - { - GenericStream* baseStream = stream->GetWrappedStream(); - numReadFromStream = baseStream->ReadAtOffset(toReadFromStream, &m_compressedDataBuffer[static_cast(dataFromBuffer)], readOffset); - } - - // update what's actually in the read data buffer. - m_lastReadStream = stream; - m_lastReadStreamOffset = zstdData->m_decompressNextOffset; - m_lastReadStreamSize = dataFromBuffer + numReadFromStream; - return m_lastReadStreamSize; + return false; } - struct ZStdCompareUpper + zstdData->m_seekPoints.resize(hdr->m_numSeekPoints); + SizeType dataToRead = sizeof(CompressorZStdSeekPoint) * static_cast(hdr->m_numSeekPoints); + SizeType seekPointOffset = compressedFileEnd - dataToRead; + + if (seekPointOffset > compressedFileEnd) { - bool operator()(const AZ::u64& offset, const CompressorZStdSeekPoint& sp) const - { - return offset < sp.m_uncompressedOffset; - } - }; + AZ_Error("CompressorZStd", false, "We have an invalid archive, this is impossible."); + return false; + } - CompressorZStd::SizeType CompressorZStd::Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) + GenericStream* baseStream = stream->GetWrappedStream(); + if (baseStream->ReadAtOffset(dataToRead, zstdData->m_seekPoints.data(), seekPointOffset) != dataToRead) { - AZ_Assert(stream->GetCompressorData(), "This stream doesn't have decompression enabled."); - CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); - AZ_Assert(!zstdData->m_zstd.IsCompressorStarted(), "You can't read/decompress while writing a compressed stream %s."); + return false; + } - // check if the request can be finished from the decompressed cache - SizeType numRead = FillFromDecompressCache(zstdData, buffer, byteSize, offset); - if (byteSize == 0) // are we done + if (m_decompressionCachePerStream) + { + zstdData->m_decompressedCache = reinterpret_cast(azmalloc(m_decompressionCachePerStream, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZStd")); + } + + zstdData->m_decompressLastOffset = seekPointOffset; // set the start address of the seek points as the last valid read address for the compressed stream. + + zstdData->m_zstd.StartDecompressor(); + + stream->SetCompressorData(zstdData.release()); + + return true; + } + + bool CompressorZStd::WriteHeaderAndData(CompressorStream* stream) + { + if (!Compressor::WriteHeaderAndData(stream)) + { + return false; + } + + CompressorZStdData* compressorData = static_cast(stream->GetCompressorData()); + CompressorZStdHeader header; + header.m_numSeekPoints = aznumeric_caster(compressorData->m_seekPoints.size()); + GenericStream* baseStream = stream->GetWrappedStream(); + if (baseStream->WriteAtOffset(sizeof(header), &header, sizeof(CompressorHeader)) == sizeof(header)) + { + return true; + } + + return false; + } + + inline CompressorZStd::SizeType CompressorZStd::FillFromDecompressCache(CompressorZStdData* zstdData, void*& buffer, SizeType& byteSize, SizeType& offset) + { + SizeType firstOffsetInCache = zstdData->m_decompressedCacheOffset; + SizeType lastOffsetInCache = firstOffsetInCache + zstdData->m_decompressedCacheDataSize; + SizeType firstDataOffset = offset; + SizeType lastDataOffset = offset + byteSize; + SizeType numCopied = 0; + if (firstOffsetInCache < lastDataOffset && lastOffsetInCache >= firstDataOffset) // check if there is data in the cache + { + size_t copyOffsetStart = 0; + size_t copyOffsetEnd = zstdData->m_decompressedCacheDataSize; + + size_t bufferCopyOffset = 0; + + if (firstOffsetInCache < firstDataOffset) { - return numRead; + copyOffsetStart = aznumeric_caster(firstDataOffset - firstOffsetInCache); + } + else + { + bufferCopyOffset = aznumeric_caster(firstOffsetInCache - firstDataOffset); } - // find the best seek point for current offset - CompressorZStdData::SeekPointArray::iterator it = AZStd::upper_bound(zstdData->m_seekPoints.begin(), zstdData->m_seekPoints.end(), offset, ZStdCompareUpper()); - AZ_Assert(it != zstdData->m_seekPoints.begin(), "This should be impossible, we should always have a valid seek point at 0 offset."); - const CompressorZStdSeekPoint& bestSeekPoint = *(--it); // get the previous (so it includes the current offset) - - // if read is continuous continue with decompression - bool isJumpToSeekPoint = false; - SizeType lastOffsetInCache = zstdData->m_decompressedCacheOffset + zstdData->m_decompressedCacheDataSize; - if (bestSeekPoint.m_uncompressedOffset > lastOffsetInCache) // if the best seek point is forward, jump forward to it. + if (lastOffsetInCache >= lastDataOffset) { - isJumpToSeekPoint = true; + copyOffsetEnd -= static_cast(lastOffsetInCache - lastDataOffset); } - else if (offset < lastOffsetInCache) // if the seek point is in the past and the requested offset is not in the cache jump back to it. + else if (bufferCopyOffset > 0) // the cache block is in the middle of the data, we can't use it (since we need to split buffer request into 2) { - isJumpToSeekPoint = true; + return 0; } - if (isJumpToSeekPoint) - { - zstdData->m_decompressNextOffset = bestSeekPoint.m_compressedOffset; // set next read point - zstdData->m_decompressedCacheOffset = bestSeekPoint.m_uncompressedOffset; // set uncompressed offset - zstdData->m_decompressedCacheDataSize = 0; // invalidate the cache - zstdData->m_zstd.ResetDecompressor(&zstdData->m_zstdHeader); // reset decompressor and setup the header. - } + numCopied = copyOffsetEnd - copyOffsetStart; + memcpy(static_cast(buffer) + bufferCopyOffset, zstdData->m_decompressedCache + copyOffsetStart, static_cast(numCopied)); - // decompress and move forward until the request is done - while (byteSize > 0) + // adjust pointers and sizes + byteSize -= numCopied; + if (bufferCopyOffset == 0) { - // fill buffer with compressed data - SizeType compressedDataSize = FillCompressedBuffer(stream); - if (compressedDataSize == 0) + // copied in the start + buffer = reinterpret_cast(buffer) + numCopied; + offset += numCopied; + } + } + + return numCopied; + } + + inline CompressorZStd::SizeType CompressorZStd::FillCompressedBuffer(CompressorStream* stream) + { + CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + SizeType dataFromBuffer = 0; + if (stream == m_lastReadStream) // if the buffer is filled with data from the current stream, try to reuse + { + if (zstdData->m_decompressNextOffset > m_lastReadStreamOffset) + { + SizeType offsetInCache = zstdData->m_decompressNextOffset - m_lastReadStreamOffset; + if (offsetInCache < m_lastReadStreamSize) // last check if there is data overlap { - return numRead; // we are done reading and obviously we did not managed to read all data + // copy the usable part at the start of the buffer + SizeType toMove = m_lastReadStreamSize - offsetInCache; + memcpy(m_compressedDataBuffer, &m_compressedDataBuffer[static_cast(offsetInCache)], static_cast(toMove)); + dataFromBuffer += toMove; } - unsigned int processedCompressedData = 0; - while (byteSize > 0 && processedCompressedData < compressedDataSize) // decompressed data either until we are done with the request (byteSize == 0) or we need to fill the compression buffer again. - { - // if we have data in the cache move to the next offset, we always move forward by default. - zstdData->m_decompressedCacheOffset += zstdData->m_decompressedCacheDataSize; - - // decompress in the cache buffer - u32 availDecompressedCacheSize = m_decompressionCachePerStream; // reset buffer size - size_t nextBlockSize; - unsigned int processed = zstdData->m_zstd.Decompress(&m_compressedDataBuffer[processedCompressedData], - static_cast(compressedDataSize) - processedCompressedData, - zstdData->m_decompressedCache, - availDecompressedCacheSize, - &nextBlockSize); - zstdData->m_decompressedCacheDataSize = m_decompressionCachePerStream - availDecompressedCacheSize; - if (processed == 0) - { - break; // we processed everything we could, load more compressed data. - } - processedCompressedData += processed; - // fill what we can from the cache - numRead += FillFromDecompressCache(zstdData, buffer, byteSize, offset); - } - // update next read position the the compressed stream - zstdData->m_decompressNextOffset += processedCompressedData; } + } + + SizeType toReadFromStream = m_compressedDataBufferSize - dataFromBuffer; + SizeType readOffset = zstdData->m_decompressNextOffset + dataFromBuffer; + if (readOffset + toReadFromStream > zstdData->m_decompressLastOffset) + { + // don't read past the end + AZ_Assert(readOffset <= zstdData->m_decompressLastOffset, "Read offset should always be before the end of stream."); + toReadFromStream = zstdData->m_decompressLastOffset - readOffset; + } + + SizeType numReadFromStream = 0; + if (toReadFromStream) // if we did not reuse the whole buffer, read some data from the stream + { + GenericStream* baseStream = stream->GetWrappedStream(); + numReadFromStream = baseStream->ReadAtOffset(toReadFromStream, &m_compressedDataBuffer[static_cast(dataFromBuffer)], readOffset); + } + + // update what's actually in the read data buffer. + m_lastReadStream = stream; + m_lastReadStreamOffset = zstdData->m_decompressNextOffset; + m_lastReadStreamSize = dataFromBuffer + numReadFromStream; + return m_lastReadStreamSize; + } + + struct ZStdCompareUpper + { + bool operator()(const AZ::u64& offset, const CompressorZStdSeekPoint& sp) const + { + return offset < sp.m_uncompressedOffset; + } + }; + + CompressorZStd::SizeType CompressorZStd::Read(CompressorStream* stream, SizeType byteSize, SizeType offset, void* buffer) + { + AZ_Assert(stream->GetCompressorData(), "This stream doesn't have decompression enabled."); + CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + AZ_Assert(!zstdData->m_zstd.IsCompressorStarted(), "You can't read/decompress while writing a compressed stream %s."); + + // check if the request can be finished from the decompressed cache + SizeType numRead = FillFromDecompressCache(zstdData, buffer, byteSize, offset); + if (byteSize == 0) // are we done + { return numRead; } - CompressorZStd::SizeType CompressorZStd::Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset) + // find the best seek point for current offset + CompressorZStdData::SeekPointArray::iterator it = AZStd::upper_bound(zstdData->m_seekPoints.begin(), zstdData->m_seekPoints.end(), offset, ZStdCompareUpper()); + AZ_Assert(it != zstdData->m_seekPoints.begin(), "This should be impossible, we should always have a valid seek point at 0 offset."); + const CompressorZStdSeekPoint& bestSeekPoint = *(--it); // get the previous (so it includes the current offset) + + // if read is continuous continue with decompression + bool isJumpToSeekPoint = false; + SizeType lastOffsetInCache = zstdData->m_decompressedCacheOffset + zstdData->m_decompressedCacheDataSize; + if (bestSeekPoint.m_uncompressedOffset > lastOffsetInCache) // if the best seek point is forward, jump forward to it. { - AZ_UNUSED(offset); + isJumpToSeekPoint = true; + } + else if (offset < lastOffsetInCache) // if the seek point is in the past and the requested offset is not in the cache jump back to it. + { + isJumpToSeekPoint = true; + } - AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled. Call Stream::WriteCompressed after you create the file."); - AZ_Assert(offset == SizeType(-1) || offset == stream->GetCurPos(), "We can write compressed data only at the end of the stream."); + if (isJumpToSeekPoint) + { + zstdData->m_decompressNextOffset = bestSeekPoint.m_compressedOffset; // set next read point + zstdData->m_decompressedCacheOffset = bestSeekPoint.m_uncompressedOffset; // set uncompressed offset + zstdData->m_decompressedCacheDataSize = 0; // invalidate the cache + zstdData->m_zstd.ResetDecompressor(&zstdData->m_zstdHeader); // reset decompressor and setup the header. + } - m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). - - CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); - AZ_Assert(!zstdData->m_zstd.IsDecompressorStarted(), "You can't write while reading/decompressing a compressed stream."); - - const u8* bytes = reinterpret_cast(data); - unsigned int dataToCompress = aznumeric_caster(byteSize); - while (dataToCompress != 0) + // decompress and move forward until the request is done + while (byteSize > 0) + { + // fill buffer with compressed data + SizeType compressedDataSize = FillCompressedBuffer(stream); + if (compressedDataSize == 0) { - unsigned int oldDataToCompress = dataToCompress; - unsigned int compressedSize = zstdData->m_zstd.Compress(bytes, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize); - if (compressedSize) - { - GenericStream* baseStream = stream->GetWrappedStream(); - SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); - if (numWritten != compressedSize) - { - return numWritten; // error we could not write all data - } - } - bytes += oldDataToCompress - dataToCompress; + return numRead; // we are done reading and obviously we did not managed to read all data } - zstdData->m_uncompressedSize += byteSize; - - if (zstdData->m_autoSeekSize > 0) + unsigned int processedCompressedData = 0; + while (byteSize > 0 && processedCompressedData < compressedDataSize) // decompressed data either until we are done with the request (byteSize == 0) or we need to fill the compression buffer again. { - // insert a seek point if needed. - if (zstdData->m_seekPoints.empty()) + // if we have data in the cache move to the next offset, we always move forward by default. + zstdData->m_decompressedCacheOffset += zstdData->m_decompressedCacheDataSize; + + // decompress in the cache buffer + u32 availDecompressedCacheSize = m_decompressionCachePerStream; // reset buffer size + size_t nextBlockSize; + unsigned int processed = zstdData->m_zstd.Decompress(&m_compressedDataBuffer[processedCompressedData], + static_cast(compressedDataSize) - processedCompressedData, + zstdData->m_decompressedCache, + availDecompressedCacheSize, + &nextBlockSize); + zstdData->m_decompressedCacheDataSize = m_decompressionCachePerStream - availDecompressedCacheSize; + if (processed == 0) { - if (zstdData->m_uncompressedSize >= zstdData->m_autoSeekSize) - { - WriteSeekPoint(stream); - } + break; // we processed everything we could, load more compressed data. } - else if ((zstdData->m_uncompressedSize - zstdData->m_seekPoints.back().m_uncompressedOffset) > zstdData->m_autoSeekSize) + processedCompressedData += processed; + // fill what we can from the cache + numRead += FillFromDecompressCache(zstdData, buffer, byteSize, offset); + } + // update next read position the the compressed stream + zstdData->m_decompressNextOffset += processedCompressedData; + } + return numRead; + } + + CompressorZStd::SizeType CompressorZStd::Write(CompressorStream* stream, SizeType byteSize, const void* data, SizeType offset) + { + AZ_UNUSED(offset); + + AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled. Call Stream::WriteCompressed after you create the file."); + AZ_Assert(offset == SizeType(-1) || offset == stream->GetCurPos(), "We can write compressed data only at the end of the stream."); + + m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + + CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + AZ_Assert(!zstdData->m_zstd.IsDecompressorStarted(), "You can't write while reading/decompressing a compressed stream."); + + const u8* bytes = reinterpret_cast(data); + unsigned int dataToCompress = aznumeric_caster(byteSize); + while (dataToCompress != 0) + { + unsigned int oldDataToCompress = dataToCompress; + unsigned int compressedSize = zstdData->m_zstd.Compress(bytes, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize); + if (compressedSize) + { + GenericStream* baseStream = stream->GetWrappedStream(); + SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); + if (numWritten != compressedSize) + { + return numWritten; // error we could not write all data + } + } + bytes += oldDataToCompress - dataToCompress; + } + zstdData->m_uncompressedSize += byteSize; + + if (zstdData->m_autoSeekSize > 0) + { + // insert a seek point if needed. + if (zstdData->m_seekPoints.empty()) + { + if (zstdData->m_uncompressedSize >= zstdData->m_autoSeekSize) { WriteSeekPoint(stream); } } - return byteSize; + else if ((zstdData->m_uncompressedSize - zstdData->m_seekPoints.back().m_uncompressedOffset) > zstdData->m_autoSeekSize) + { + WriteSeekPoint(stream); + } } + return byteSize; + } - bool CompressorZStd::WriteSeekPoint(CompressorStream* stream) + bool CompressorZStd::WriteSeekPoint(CompressorStream* stream) + { + AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled. Call Stream::WriteCompressed after you create the file."); + CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + + m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + + unsigned int compressedSize; + unsigned int dataToCompress = 0; + do { - AZ_Assert(stream && stream->GetCompressorData(), "This stream doesn't have compression enabled. Call Stream::WriteCompressed after you create the file."); - CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + compressedSize = zstdData->m_zstd.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZStd::FT_FULL_FLUSH); + if (compressedSize) + { + GenericStream* baseStream = stream->GetWrappedStream(); + SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); + if (numWritten != compressedSize) + { + return false; // error we wrote less than than requested! + } + } + } while (dataToCompress != 0); + CompressorZStdSeekPoint sp; + sp.m_compressedOffset = stream->GetLength(); + sp.m_uncompressedOffset = zstdData->m_uncompressedSize; + zstdData->m_seekPoints.push_back(sp); + return true; + } + + bool CompressorZStd::StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) + { + AZ_Assert(stream && !stream->GetCompressorData(), "Stream has compressor already enabled."); + + AcquireDataBuffer(); + + CompressorZStdData* zstdData = aznew CompressorZStdData; + zstdData->m_compressor = this; + zstdData->m_zstdHeader = 0; // not used for compression + zstdData->m_uncompressedSize = 0; + zstdData->m_autoSeekSize = autoSeekDataSize; + compressionLevel = AZ::GetClamp(compressionLevel, 1, 9); // remap to zlib levels + + zstdData->m_zstd.StartCompressor(compressionLevel); + + stream->SetCompressorData(zstdData); + + if (WriteHeaderAndData(stream)) + { + // add the first and always present seek point at the start of the compressed stream + CompressorZStdSeekPoint sp; + sp.m_compressedOffset = sizeof(CompressorHeader) + sizeof(CompressorZStdHeader) + sizeof(zstdData->m_zstdHeader); + sp.m_uncompressedOffset = 0; + zstdData->m_seekPoints.push_back(sp); + return true; + } + return false; + } + + bool CompressorZStd::Close(CompressorStream* stream) + { + AZ_Assert(stream->IsOpen(), "Stream is not open to be closed."); + + CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); + GenericStream* baseStream = stream->GetWrappedStream(); + + bool result = true; + if (zstdData->m_zstd.IsCompressorStarted()) + { m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). + // flush all compressed data unsigned int compressedSize; unsigned int dataToCompress = 0; do { - compressedSize = zstdData->m_zstd.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZStd::FT_FULL_FLUSH); + compressedSize = zstdData->m_zstd.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZStd::FT_FINISH); if (compressedSize) { - GenericStream* baseStream = stream->GetWrappedStream(); - SizeType numWritten = baseStream->Write(compressedSize, m_compressedDataBuffer); - if (numWritten != compressedSize) - { - return false; // error we wrote less than than requested! - } + baseStream->Write(compressedSize, m_compressedDataBuffer); } } while (dataToCompress != 0); - CompressorZStdSeekPoint sp; - sp.m_compressedOffset = stream->GetLength(); - sp.m_uncompressedOffset = zstdData->m_uncompressedSize; - zstdData->m_seekPoints.push_back(sp); - return true; + result = WriteHeaderAndData(stream); + if (result) + { + SizeType dataToWrite = zstdData->m_seekPoints.size() * sizeof(CompressorZStdSeekPoint); + baseStream->Seek(0U, GenericStream::SeekMode::ST_SEEK_END); + result = (baseStream->Write(dataToWrite, zstdData->m_seekPoints.data()) == dataToWrite); + } } - - bool CompressorZStd::StartCompressor(CompressorStream* stream, int compressionLevel, SizeType autoSeekDataSize) + else { - AZ_Assert(stream && !stream->GetCompressorData(), "Stream has compressor already enabled."); - - AcquireDataBuffer(); - - CompressorZStdData* zstdData = aznew CompressorZStdData; - zstdData->m_compressor = this; - zstdData->m_zstdHeader = 0; // not used for compression - zstdData->m_uncompressedSize = 0; - zstdData->m_autoSeekSize = autoSeekDataSize; - compressionLevel = AZ::GetClamp(compressionLevel, 1, 9); // remap to zlib levels - - zstdData->m_zstd.StartCompressor(compressionLevel); - - stream->SetCompressorData(zstdData); - - if (WriteHeaderAndData(stream)) + if (m_lastReadStream == stream) { - // add the first and always present seek point at the start of the compressed stream - CompressorZStdSeekPoint sp; - sp.m_compressedOffset = sizeof(CompressorHeader) + sizeof(CompressorZStdHeader) + sizeof(zstdData->m_zstdHeader); - sp.m_uncompressedOffset = 0; - zstdData->m_seekPoints.push_back(sp); - return true; + m_lastReadStream = nullptr; // invalidate the data in m_dataBuffer if it was from the current stream. } - return false; } - bool CompressorZStd::Close(CompressorStream* stream) + // if we have decompressor cache delete it + if (zstdData->m_decompressedCache) { - AZ_Assert(stream->IsOpen(), "Stream is not open to be closed."); - - CompressorZStdData* zstdData = static_cast(stream->GetCompressorData()); - GenericStream* baseStream = stream->GetWrappedStream(); - - bool result = true; - if (zstdData->m_zstd.IsCompressorStarted()) - { - m_lastReadStream = nullptr; // invalidate last read position, otherwise m_dataBuffer will be corrupted (as we are about to write in it). - - // flush all compressed data - unsigned int compressedSize; - unsigned int dataToCompress = 0; - do - { - compressedSize = zstdData->m_zstd.Compress(nullptr, dataToCompress, m_compressedDataBuffer, m_compressedDataBufferSize, ZStd::FT_FINISH); - if (compressedSize) - { - baseStream->Write(compressedSize, m_compressedDataBuffer); - } - } while (dataToCompress != 0); - - result = WriteHeaderAndData(stream); - if (result) - { - SizeType dataToWrite = zstdData->m_seekPoints.size() * sizeof(CompressorZStdSeekPoint); - baseStream->Seek(0U, GenericStream::SeekMode::ST_SEEK_END); - result = (baseStream->Write(dataToWrite, zstdData->m_seekPoints.data()) == dataToWrite); - } - } - else - { - if (m_lastReadStream == stream) - { - m_lastReadStream = nullptr; // invalidate the data in m_dataBuffer if it was from the current stream. - } - } - - // if we have decompressor cache delete it - if (zstdData->m_decompressedCache) - { - azfree(zstdData->m_decompressedCache, AZ::SystemAllocator, m_decompressionCachePerStream, m_CompressedDataBufferAlignment); - } - - ReleaseDataBuffer(); - - // last step reset strream compressor data. - stream->SetCompressorData(nullptr); - return result; + azfree(zstdData->m_decompressedCache, AZ::SystemAllocator, m_decompressionCachePerStream, m_CompressedDataBufferAlignment); } - void CompressorZStd::AcquireDataBuffer() + ReleaseDataBuffer(); + + // last step reset strream compressor data. + stream->SetCompressorData(nullptr); + return result; + } + + void CompressorZStd::AcquireDataBuffer() + { + if (m_compressedDataBuffer == nullptr) { - if (m_compressedDataBuffer == nullptr) - { - AZ_Assert(m_compressedDataBufferUseCount == 0, "Buffer usecount should be 0 if the buffer is NULL"); - m_compressedDataBuffer = reinterpret_cast(azmalloc(m_compressedDataBufferSize, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZStd")); - m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer - } - ++m_compressedDataBufferUseCount; + AZ_Assert(m_compressedDataBufferUseCount == 0, "Buffer usecount should be 0 if the buffer is NULL"); + m_compressedDataBuffer = reinterpret_cast(azmalloc(m_compressedDataBufferSize, m_CompressedDataBufferAlignment, AZ::SystemAllocator, "CompressorZStd")); + m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer } + ++m_compressedDataBufferUseCount; + } - void CompressorZStd::ReleaseDataBuffer() + void CompressorZStd::ReleaseDataBuffer() + { + --m_compressedDataBufferUseCount; + if (m_compressedDataBufferUseCount == 0) { - --m_compressedDataBufferUseCount; - if (m_compressedDataBufferUseCount == 0) - { - AZ_Assert(m_compressedDataBuffer, "Invalid data buffer. We should have a non null pointer."); - azfree(m_compressedDataBuffer, AZ::SystemAllocator, m_compressedDataBufferSize, m_CompressedDataBufferAlignment); - m_compressedDataBuffer = nullptr; - m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer - } + AZ_Assert(m_compressedDataBuffer, "Invalid data buffer. We should have a non null pointer."); + azfree(m_compressedDataBuffer, AZ::SystemAllocator, m_compressedDataBufferSize, m_CompressedDataBufferAlignment); + m_compressedDataBuffer = nullptr; + m_lastReadStream = nullptr; // reset the cache info in the m_dataBuffer } - } // namespace IO -} // namespace AZ + } +} // namespace AZ::IO #endif // #if !defined(AZCORE_EXCLUDE_ZSTD) diff --git a/Code/Framework/AzCore/AzCore/IO/FileIO.cpp b/Code/Framework/AzCore/AzCore/IO/FileIO.cpp index 837aca8d84..3522187131 100644 --- a/Code/Framework/AzCore/AzCore/IO/FileIO.cpp +++ b/Code/Framework/AzCore/AzCore/IO/FileIO.cpp @@ -21,485 +21,482 @@ # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ #endif -namespace AZ +namespace AZ::IO { - namespace IO + static EnvironmentVariable g_fileIOInstance; + static EnvironmentVariable g_directFileIOInstance; + static const char* s_EngineFileIOName = "EngineFileIO"; + static const char* s_DirectFileIOName = "DirectFileIO"; + + FileIOBase* FileIOBase::GetInstance() { - static EnvironmentVariable g_fileIOInstance; - static EnvironmentVariable g_directFileIOInstance; - static const char* s_EngineFileIOName = "EngineFileIO"; - static const char* s_DirectFileIOName = "DirectFileIO"; - - FileIOBase* FileIOBase::GetInstance() + if (!g_fileIOInstance) { - if (!g_fileIOInstance) - { - g_fileIOInstance = Environment::FindVariable(s_EngineFileIOName); - } - - return g_fileIOInstance ? (*g_fileIOInstance) : nullptr; + g_fileIOInstance = Environment::FindVariable(s_EngineFileIOName); } - void FileIOBase::SetInstance(FileIOBase* instance) + return g_fileIOInstance ? (*g_fileIOInstance) : nullptr; + } + + void FileIOBase::SetInstance(FileIOBase* instance) + { + if (!g_fileIOInstance) { - if (!g_fileIOInstance) - { - g_fileIOInstance = Environment::CreateVariable(s_EngineFileIOName); - (*g_fileIOInstance) = nullptr; - } - - // at this point we're guaranteed to have g_fileIOInstance. Its value might be null. - - if ((instance) && (g_fileIOInstance) && (*g_fileIOInstance)) - { - AZ_Error("FileIO", false, "FileIOBase::SetInstance was called without first destroying the old instance and setting it to nullptr"); - } - - (*g_fileIOInstance) = instance; + g_fileIOInstance = Environment::CreateVariable(s_EngineFileIOName); + (*g_fileIOInstance) = nullptr; } - FileIOBase* FileIOBase::GetDirectInstance() + // at this point we're guaranteed to have g_fileIOInstance. Its value might be null. + + if ((instance) && (g_fileIOInstance) && (*g_fileIOInstance)) { - if (!g_directFileIOInstance) - { - g_directFileIOInstance = Environment::FindVariable(s_DirectFileIOName); - } - - // for backwards compatibilty, return the regular instance if this is not attached - if (!g_directFileIOInstance) - { - return GetInstance(); - } - - return g_directFileIOInstance ? (*g_directFileIOInstance) : nullptr; + AZ_Error("FileIO", false, "FileIOBase::SetInstance was called without first destroying the old instance and setting it to nullptr"); } - void FileIOBase::SetDirectInstance(FileIOBase* instance) + (*g_fileIOInstance) = instance; + } + + FileIOBase* FileIOBase::GetDirectInstance() + { + if (!g_directFileIOInstance) { - if (!g_directFileIOInstance) - { - g_directFileIOInstance = Environment::CreateVariable(s_DirectFileIOName); - (*g_directFileIOInstance) = nullptr; - } - - // at this point we're guaranteed to have g_directFileIOInstance. Its value might be null. - - if ((instance) && (g_directFileIOInstance) && (*g_directFileIOInstance)) - { - AZ_Error("FileIO", false, "FileIOBase::SetDirectInstance was called without first destroying the old instance and setting it to nullptr"); - } - - (*g_directFileIOInstance) = instance; + g_directFileIOInstance = Environment::FindVariable(s_DirectFileIOName); } - AZStd::optional FileIOBase::ConvertToAlias(const AZ::IO::PathView& path) const + // for backwards compatibilty, return the regular instance if this is not attached + if (!g_directFileIOInstance) { - AZ::IO::FixedMaxPath convertedPath; - if (ConvertToAlias(convertedPath, path)) - { - return convertedPath; - } - - return AZStd::nullopt; + return GetInstance(); } - AZStd::optional FileIOBase::ResolvePath(const AZ::IO::PathView& path) const - { - AZ::IO::FixedMaxPath resolvedPath; - if (ResolvePath(resolvedPath, path)) - { - return resolvedPath; - } + return g_directFileIOInstance ? (*g_directFileIOInstance) : nullptr; + } - return AZStd::nullopt; + void FileIOBase::SetDirectInstance(FileIOBase* instance) + { + if (!g_directFileIOInstance) + { + g_directFileIOInstance = Environment::CreateVariable(s_DirectFileIOName); + (*g_directFileIOInstance) = nullptr; } - SeekType GetSeekTypeFromFSeekMode(int mode) - { - switch (mode) - { - case SEEK_SET: - return SeekType::SeekFromStart; - case SEEK_CUR: - return SeekType::SeekFromCurrent; - case SEEK_END: - return SeekType::SeekFromEnd; - } + // at this point we're guaranteed to have g_directFileIOInstance. Its value might be null. - // Must have some default, hitting here means some random int mode + if ((instance) && (g_directFileIOInstance) && (*g_directFileIOInstance)) + { + AZ_Error("FileIO", false, "FileIOBase::SetDirectInstance was called without first destroying the old instance and setting it to nullptr"); + } + + (*g_directFileIOInstance) = instance; + } + + AZStd::optional FileIOBase::ConvertToAlias(const AZ::IO::PathView& path) const + { + AZ::IO::FixedMaxPath convertedPath; + if (ConvertToAlias(convertedPath, path)) + { + return convertedPath; + } + + return AZStd::nullopt; + } + + AZStd::optional FileIOBase::ResolvePath(const AZ::IO::PathView& path) const + { + AZ::IO::FixedMaxPath resolvedPath; + if (ResolvePath(resolvedPath, path)) + { + return resolvedPath; + } + + return AZStd::nullopt; + } + + SeekType GetSeekTypeFromFSeekMode(int mode) + { + switch (mode) + { + case SEEK_SET: return SeekType::SeekFromStart; + case SEEK_CUR: + return SeekType::SeekFromCurrent; + case SEEK_END: + return SeekType::SeekFromEnd; } - int GetFSeekModeFromSeekType(SeekType type) - { - switch (type) - { - case SeekType::SeekFromStart: - return SEEK_SET; - case SeekType::SeekFromCurrent: - return SEEK_CUR; - case SeekType::SeekFromEnd: - return SEEK_END; - } + // Must have some default, hitting here means some random int mode + return SeekType::SeekFromStart; + } + int GetFSeekModeFromSeekType(SeekType type) + { + switch (type) + { + case SeekType::SeekFromStart: return SEEK_SET; + case SeekType::SeekFromCurrent: + return SEEK_CUR; + case SeekType::SeekFromEnd: + return SEEK_END; } - void UpdateOpenModeForReading(OpenMode& openMode) + return SEEK_SET; + } + + void UpdateOpenModeForReading(OpenMode& openMode) + { + if (AnyFlag(openMode & OpenMode::ModeRead)) { - if (AnyFlag(openMode & OpenMode::ModeRead)) + if (AnyFlag(openMode & OpenMode::ModeText)) { - if (AnyFlag(openMode & OpenMode::ModeText)) - { - OpenMode extraModes = openMode & (OpenMode::ModeUpdate | OpenMode::ModeAppend); - openMode = OpenMode::ModeRead | OpenMode::ModeBinary | extraModes; - } - else if (!AnyFlag(openMode & OpenMode::ModeBinary)) - { - // if you haven't supplied any flag, supply binary - openMode = openMode | OpenMode::ModeBinary; - } + OpenMode extraModes = openMode & (OpenMode::ModeUpdate | OpenMode::ModeAppend); + openMode = OpenMode::ModeRead | OpenMode::ModeBinary | extraModes; + } + else if (!AnyFlag(openMode & OpenMode::ModeBinary)) + { + // if you haven't supplied any flag, supply binary + openMode = openMode | OpenMode::ModeBinary; } } + } - OpenMode GetOpenModeFromStringMode(const char* mode) + OpenMode GetOpenModeFromStringMode(const char* mode) + { + OpenMode openMode = OpenMode::Invalid; + + if (strstr(mode, "w")) { - OpenMode openMode = OpenMode::Invalid; - - if (strstr(mode, "w")) - { - openMode |= OpenMode::ModeWrite; - } - - if (strstr(mode, "r")) - { - openMode |= OpenMode::ModeRead; - } - - if (strstr(mode, "a")) - { - openMode |= OpenMode::ModeAppend; - } - - if (strstr(mode, "b")) - { - openMode |= OpenMode::ModeBinary; - } - - if (strstr(mode, "t")) - { - openMode |= OpenMode::ModeText; - } - - if (strstr(mode, "+")) - { - openMode |= OpenMode::ModeUpdate; - } - - UpdateOpenModeForReading(openMode); - - return openMode; + openMode |= OpenMode::ModeWrite; } - const char* GetStringModeFromOpenMode(OpenMode mode) + if (strstr(mode, "r")) { - UpdateOpenModeForReading(mode); - // Append is highest priority, followed by write and then read - // APPEND - if (AnyFlag(mode & OpenMode::ModeAppend)) + openMode |= OpenMode::ModeRead; + } + + if (strstr(mode, "a")) + { + openMode |= OpenMode::ModeAppend; + } + + if (strstr(mode, "b")) + { + openMode |= OpenMode::ModeBinary; + } + + if (strstr(mode, "t")) + { + openMode |= OpenMode::ModeText; + } + + if (strstr(mode, "+")) + { + openMode |= OpenMode::ModeUpdate; + } + + UpdateOpenModeForReading(openMode); + + return openMode; + } + + const char* GetStringModeFromOpenMode(OpenMode mode) + { + UpdateOpenModeForReading(mode); + // Append is highest priority, followed by write and then read + // APPEND + if (AnyFlag(mode & OpenMode::ModeAppend)) + { + if (AnyFlag(mode & OpenMode::ModeUpdate)) { - if (AnyFlag(mode & OpenMode::ModeUpdate)) - { - if (AnyFlag(mode & OpenMode::ModeBinary)) - { - return "a+b"; - } - if (AnyFlag(mode & OpenMode::ModeText)) - { - return "a+t"; - } - return "a+"; - } if (AnyFlag(mode & OpenMode::ModeBinary)) { - return "ab"; + return "a+b"; } if (AnyFlag(mode & OpenMode::ModeText)) { - return "at"; + return "a+t"; } - return "a"; + return "a+"; } - - // WRITE - if (AnyFlag(mode & OpenMode::ModeWrite)) + if (AnyFlag(mode & OpenMode::ModeBinary)) + { + return "ab"; + } + if (AnyFlag(mode & OpenMode::ModeText)) + { + return "at"; + } + return "a"; + } + + // WRITE + if (AnyFlag(mode & OpenMode::ModeWrite)) + { + if (AnyFlag(mode & OpenMode::ModeUpdate)) { - if (AnyFlag(mode & OpenMode::ModeUpdate)) - { - if (AnyFlag(mode & OpenMode::ModeBinary)) - { - return "w+b"; - } - if (AnyFlag(mode & OpenMode::ModeText)) - { - return "w+t"; - } - return "w+"; - } if (AnyFlag(mode & OpenMode::ModeBinary)) { - return "wb"; + return "w+b"; } if (AnyFlag(mode & OpenMode::ModeText)) { - return "wt"; + return "w+t"; } - return "w"; + return "w+"; } - - // READ - if (AnyFlag(mode & OpenMode::ModeRead)) + if (AnyFlag(mode & OpenMode::ModeBinary)) + { + return "wb"; + } + if (AnyFlag(mode & OpenMode::ModeText)) + { + return "wt"; + } + return "w"; + } + + // READ + if (AnyFlag(mode & OpenMode::ModeRead)) + { + if (AnyFlag(mode & OpenMode::ModeUpdate)) { - if (AnyFlag(mode & OpenMode::ModeUpdate)) - { - if (AnyFlag(mode & OpenMode::ModeBinary)) - { - return "r+b"; - } - if (AnyFlag(mode & OpenMode::ModeText)) - { - return "r+t"; - } - return "r+"; - } if (AnyFlag(mode & OpenMode::ModeBinary)) { - return "rb"; + return "r+b"; } if (AnyFlag(mode & OpenMode::ModeText)) { - return "rt"; + return "r+t"; } - return "r"; + return "r+"; } - - // Bad open mode passed in - AZ_Error("FileIO", false, "A bad open mode was sent to GetStringModeFromOpenMode()"); - return ""; + if (AnyFlag(mode & OpenMode::ModeBinary)) + { + return "rb"; + } + if (AnyFlag(mode & OpenMode::ModeText)) + { + return "rt"; + } + return "r"; } - bool NameMatchesFilter(const char* name, const char* filter) + // Bad open mode passed in + AZ_Error("FileIO", false, "A bad open mode was sent to GetStringModeFromOpenMode()"); + return ""; + } + + bool NameMatchesFilter(const char* name, const char* filter) + { + return AZStd::wildcard_match(filter, name); + } + + FileIOStream::FileIOStream() + : m_handle(InvalidHandle) + , m_mode(OpenMode::Invalid) + , m_ownsHandle(true) + { + + } + + FileIOStream::FileIOStream(HandleType fileHandle, AZ::IO::OpenMode mode, bool ownsHandle) + : m_handle(fileHandle) + , m_mode(mode) + , m_ownsHandle(ownsHandle) + { + + FileIOBase* fileIO = FileIOBase::GetInstance(); + AZ_Assert(fileIO, "FileIO is not initialized."); + AZStd::array resolvedPath{ {0} }; + fileIO->GetFilename(m_handle, resolvedPath.data(), resolvedPath.size() - 1); + m_filename = resolvedPath.data(); + } + + FileIOStream::FileIOStream(const char* path, AZ::IO::OpenMode mode, bool errorOnFailure) + : m_handle(InvalidHandle) + , m_mode(mode) + , m_errorOnFailure(errorOnFailure) + { + Open(path, mode); + } + + FileIOStream::~FileIOStream() + { + if (m_ownsHandle) { - return AZStd::wildcard_match(filter, name); + Close(); } + } - FileIOStream::FileIOStream() - : m_handle(InvalidHandle) - , m_mode(OpenMode::Invalid) - , m_ownsHandle(true) + bool FileIOStream::Open(const char* path, OpenMode mode) + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + FileIOBase* fileIO = FileIOBase::GetInstance(); + + Close(); + + const Result result = fileIO->Open(path, mode, m_handle); + m_ownsHandle = IsOpen(); + m_mode = mode; + + if (IsOpen()) { - - } - - FileIOStream::FileIOStream(HandleType fileHandle, AZ::IO::OpenMode mode, bool ownsHandle) - : m_handle(fileHandle) - , m_mode(mode) - , m_ownsHandle(ownsHandle) - { - - FileIOBase* fileIO = FileIOBase::GetInstance(); - AZ_Assert(fileIO, "FileIO is not initialized."); + // Not using supplied path parameter as it may be unresolved AZStd::array resolvedPath{ {0} }; fileIO->GetFilename(m_handle, resolvedPath.data(), resolvedPath.size() - 1); m_filename = resolvedPath.data(); } - - FileIOStream::FileIOStream(const char* path, AZ::IO::OpenMode mode, bool errorOnFailure) - : m_handle(InvalidHandle) - , m_mode(mode) - , m_errorOnFailure(errorOnFailure) + else { - Open(path, mode); + // remember the file name so you can try again with ReOpen + m_filename = path; } - FileIOStream::~FileIOStream() - { - if (m_ownsHandle) - { - Close(); - } - } + AZ_PROFILE_INTERVAL_START_COLORED(AzCore, &m_filename, 0xff0000ff, "FileIO: %s", m_filename.c_str()); + return result; + } - bool FileIOStream::Open(const char* path, OpenMode mode) + bool FileIOStream::ReOpen() + { + Close(); + return (m_mode != OpenMode::Invalid) ? Open(m_filename.data(), m_mode) : false; + } + + void FileIOStream::Close() + { + if (m_handle != InvalidHandle) { AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - FileIOBase* fileIO = FileIOBase::GetInstance(); - Close(); - - const Result result = fileIO->Open(path, mode, m_handle); - m_ownsHandle = IsOpen(); - m_mode = mode; - - if (IsOpen()) - { - // Not using supplied path parameter as it may be unresolved - AZStd::array resolvedPath{ {0} }; - fileIO->GetFilename(m_handle, resolvedPath.data(), resolvedPath.size() - 1); - m_filename = resolvedPath.data(); - } - else - { - // remember the file name so you can try again with ReOpen - m_filename = path; - } - - AZ_PROFILE_INTERVAL_START_COLORED(AzCore, &m_filename, 0xff0000ff, "FileIO: %s", m_filename.c_str()); - return result; + FileIOBase::GetInstance()->Close(m_handle); + m_handle = InvalidHandle; + m_ownsHandle = false; + AZ_PROFILE_INTERVAL_END(AzCore, &m_filename); } + } - bool FileIOStream::ReOpen() + bool FileIOStream::IsOpen() const + { + return (m_handle != InvalidHandle); + } + + /*! + \brief Retrieves underlying FileIO Handle from file stream + \return HandleType + */ + HandleType FileIOStream::GetHandle() const + { + return m_handle; + } + + /*! + \brief Retrieves filename + \return const char* + */ + const char* FileIOStream::GetFilename() const + { + return m_filename.data(); + } + + /*! + \brief Retrieves OpenMode flags used to open this file + \return OpenMode + */ + AZ::IO::OpenMode FileIOStream::GetModeFlags() const + { + return m_mode; + } + + bool FileIOStream::CanSeek() const + { + return true; + } + + bool FileIOStream::CanRead() const + { + return (m_mode & (OpenMode::ModeRead | OpenMode::ModeUpdate)) != OpenMode::Invalid; + } + + bool FileIOStream::CanWrite() const + { + return (m_mode & (OpenMode::ModeWrite | OpenMode::ModeAppend | OpenMode::ModeUpdate)) != OpenMode::Invalid; + } + + void FileIOStream::Seek(OffsetType bytes, SeekMode mode) + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + AZ_Assert(IsOpen(), "Cannot seek on a FileIOStream that is not open."); + + SeekType seekType = SeekType::SeekFromCurrent; + switch (mode) { - Close(); - return (m_mode != OpenMode::Invalid) ? Open(m_filename.data(), m_mode) : false; + case GenericStream::ST_SEEK_BEGIN: + seekType = SeekType::SeekFromStart; + break; + case GenericStream::ST_SEEK_CUR: + seekType = SeekType::SeekFromCurrent; + break; + case GenericStream::ST_SEEK_END: + seekType = SeekType::SeekFromEnd; + break; + default: + seekType = SeekType::SeekFromCurrent; + break; } - void FileIOStream::Close() + const Result result = FileIOBase::GetInstance()->Seek(m_handle, static_cast(bytes), seekType); + (void)result; + AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Seek failed."); + } + + SizeType FileIOStream::Read(SizeType bytes, void* oBuffer) + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + AZ_Assert(IsOpen(), "Cannot read from a FileIOStream that is not open."); + + AZ::u64 bytesRead = 0; + const Result result = FileIOBase::GetInstance()->Read(m_handle, oBuffer, bytes, m_errorOnFailure, &bytesRead); + (void)result; + AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Read failed in file %s.", m_filename.empty() ? "NULL" : m_filename.c_str()); + return static_cast(bytesRead); + } + + SizeType FileIOStream::Write(SizeType bytes, const void* iBuffer) + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + AZ_Assert(IsOpen(), "Cannot write to a FileIOStream that is not open."); + + AZ::u64 bytesWritten = 0; + const Result result = FileIOBase::GetInstance()->Write(m_handle, iBuffer, bytes, &bytesWritten); + (void)result; + AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Write failed."); + return static_cast(bytesWritten); + } + + SizeType FileIOStream::GetCurPos() const + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + AZ_Assert(IsOpen(), "Cannot use a FileIOStream that is not open."); + + AZ::u64 currentPosition = 0; + const Result result = FileIOBase::GetInstance()->Tell(m_handle, currentPosition); + (void)result; + AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "GetCurPos failed."); + return static_cast(currentPosition); + } + + SizeType FileIOStream::GetLength() const + { + AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); + AZ_Assert(IsOpen(), "Cannot use a FileIOStream that is not open."); + + SizeType fileLengthBytes = 0; + if (!FileIOBase::GetInstance()->Size(m_handle, fileLengthBytes)) { - if (m_handle != InvalidHandle) - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - - FileIOBase::GetInstance()->Close(m_handle); - m_handle = InvalidHandle; - m_ownsHandle = false; - AZ_PROFILE_INTERVAL_END(AzCore, &m_filename); - } + AZ_Error("FileIOStream", false, "GetLength failed."); } - bool FileIOStream::IsOpen() const - { - return (m_handle != InvalidHandle); - } + return fileLengthBytes; + } - /*! - \brief Retrieves underlying FileIO Handle from file stream - \return HandleType - */ - HandleType FileIOStream::GetHandle() const - { - return m_handle; - } - - /*! - \brief Retrieves filename - \return const char* - */ - const char* FileIOStream::GetFilename() const - { - return m_filename.data(); - } - - /*! - \brief Retrieves OpenMode flags used to open this file - \return OpenMode - */ - AZ::IO::OpenMode FileIOStream::GetModeFlags() const - { - return m_mode; - } - - bool FileIOStream::CanSeek() const - { - return true; - } - - bool FileIOStream::CanRead() const - { - return (m_mode & (OpenMode::ModeRead | OpenMode::ModeUpdate)) != OpenMode::Invalid; - } - - bool FileIOStream::CanWrite() const - { - return (m_mode & (OpenMode::ModeWrite | OpenMode::ModeAppend | OpenMode::ModeUpdate)) != OpenMode::Invalid; - } - - void FileIOStream::Seek(OffsetType bytes, SeekMode mode) - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - AZ_Assert(IsOpen(), "Cannot seek on a FileIOStream that is not open."); - - SeekType seekType = SeekType::SeekFromCurrent; - switch (mode) - { - case GenericStream::ST_SEEK_BEGIN: - seekType = SeekType::SeekFromStart; - break; - case GenericStream::ST_SEEK_CUR: - seekType = SeekType::SeekFromCurrent; - break; - case GenericStream::ST_SEEK_END: - seekType = SeekType::SeekFromEnd; - break; - default: - seekType = SeekType::SeekFromCurrent; - break; - } - - const Result result = FileIOBase::GetInstance()->Seek(m_handle, static_cast(bytes), seekType); - (void)result; - AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Seek failed."); - } - - SizeType FileIOStream::Read(SizeType bytes, void* oBuffer) - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - AZ_Assert(IsOpen(), "Cannot read from a FileIOStream that is not open."); - - AZ::u64 bytesRead = 0; - const Result result = FileIOBase::GetInstance()->Read(m_handle, oBuffer, bytes, m_errorOnFailure, &bytesRead); - (void)result; - AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Read failed in file %s.", m_filename.empty() ? "NULL" : m_filename.c_str()); - return static_cast(bytesRead); - } - - SizeType FileIOStream::Write(SizeType bytes, const void* iBuffer) - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - AZ_Assert(IsOpen(), "Cannot write to a FileIOStream that is not open."); - - AZ::u64 bytesWritten = 0; - const Result result = FileIOBase::GetInstance()->Write(m_handle, iBuffer, bytes, &bytesWritten); - (void)result; - AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "Write failed."); - return static_cast(bytesWritten); - } - - SizeType FileIOStream::GetCurPos() const - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - AZ_Assert(IsOpen(), "Cannot use a FileIOStream that is not open."); - - AZ::u64 currentPosition = 0; - const Result result = FileIOBase::GetInstance()->Tell(m_handle, currentPosition); - (void)result; - AZ_Error("FileIOStream", result.GetResultCode() == ResultCode::Success, "GetCurPos failed."); - return static_cast(currentPosition); - } - - SizeType FileIOStream::GetLength() const - { - AZ_Assert(FileIOBase::GetInstance(), "FileIO is not initialized."); - AZ_Assert(IsOpen(), "Cannot use a FileIOStream that is not open."); - - SizeType fileLengthBytes = 0; - if (!FileIOBase::GetInstance()->Size(m_handle, fileLengthBytes)) - { - AZ_Error("FileIOStream", false, "GetLength failed."); - } - - return fileLengthBytes; - } - - } // namespace IO -} // namespace AZ +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/IOUtils.cpp b/Code/Framework/AzCore/AzCore/IO/IOUtils.cpp index 1e3a31f8ab..222a36ef70 100644 --- a/Code/Framework/AzCore/AzCore/IO/IOUtils.cpp +++ b/Code/Framework/AzCore/AzCore/IO/IOUtils.cpp @@ -10,69 +10,64 @@ #include #include /// this_thread sleep_for. -namespace AZ +namespace AZ::IO { - namespace IO - { - int TranslateOpenModeToSystemFileMode(const char* path, OpenMode mode) + int TranslateOpenModeToSystemFileMode(const char* path, OpenMode mode) + { + int systemFileMode = 0; + bool read = AnyFlag(mode & OpenMode::ModeRead) || AnyFlag(mode & OpenMode::ModeUpdate); + bool write = AnyFlag(mode & OpenMode::ModeWrite) || AnyFlag(mode & OpenMode::ModeUpdate) || AnyFlag(mode & OpenMode::ModeAppend); + if (write) { - int systemFileMode = 0; - bool read = AnyFlag(mode & OpenMode::ModeRead) || AnyFlag(mode & OpenMode::ModeUpdate); - bool write = AnyFlag(mode & OpenMode::ModeWrite) || AnyFlag(mode & OpenMode::ModeUpdate) || AnyFlag(mode & OpenMode::ModeAppend); - if (write) + // If writing the file, create the file in all cases (except r+) + if (!SystemFile::Exists(path) && !(AnyFlag(mode & OpenMode::ModeRead) && AnyFlag(mode & OpenMode::ModeUpdate))) { - // If writing the file, create the file in all cases (except r+) - if (!SystemFile::Exists(path) && !(AnyFlag(mode & OpenMode::ModeRead) && AnyFlag(mode & OpenMode::ModeUpdate))) - { - // LocalFileIO creates by default - systemFileMode |= SystemFile::SF_OPEN_CREATE; - } - - if (AnyFlag(mode & OpenMode::ModeCreatePath)) - { - systemFileMode |= SystemFile::SF_OPEN_CREATE_PATH; - } - - // If appending, append. - if (AnyFlag(mode & OpenMode::ModeAppend)) - { - systemFileMode |= SystemFile::SF_OPEN_APPEND; - } - // If writing and not appending, empty the file - else if (AnyFlag(mode & OpenMode::ModeWrite)) - { - systemFileMode |= SystemFile::SF_OPEN_TRUNCATE; - } - - // If reading, set read/write, otherwise just write - if (read) - { - systemFileMode |= SystemFile::SF_OPEN_READ_WRITE; - } - else - { - systemFileMode |= SystemFile::SF_OPEN_WRITE_ONLY; - } - } - else if (read) - { - systemFileMode |= SystemFile::SF_OPEN_READ_ONLY; + // LocalFileIO creates by default + systemFileMode |= SystemFile::SF_OPEN_CREATE; } - return systemFileMode; + if (AnyFlag(mode & OpenMode::ModeCreatePath)) + { + systemFileMode |= SystemFile::SF_OPEN_CREATE_PATH; + } + + // If appending, append. + if (AnyFlag(mode & OpenMode::ModeAppend)) + { + systemFileMode |= SystemFile::SF_OPEN_APPEND; + } + // If writing and not appending, empty the file + else if (AnyFlag(mode & OpenMode::ModeWrite)) + { + systemFileMode |= SystemFile::SF_OPEN_TRUNCATE; + } + + // If reading, set read/write, otherwise just write + if (read) + { + systemFileMode |= SystemFile::SF_OPEN_READ_WRITE; + } + else + { + systemFileMode |= SystemFile::SF_OPEN_WRITE_ONLY; + } + } + else if (read) + { + systemFileMode |= SystemFile::SF_OPEN_READ_ONLY; } - bool RetryOpenStream(FileIOStream& stream, int numRetries, int delayBetweenRetry) + return systemFileMode; + } + + bool RetryOpenStream(FileIOStream& stream, int numRetries, int delayBetweenRetry) + { + while ((!stream.IsOpen()) && (numRetries > 0)) { - while ((!stream.IsOpen()) && (numRetries > 0)) - { - numRetries--; - AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(delayBetweenRetry)); - stream.ReOpen(); - } - return stream.IsOpen(); + numRetries--; + AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(delayBetweenRetry)); + stream.ReOpen(); } - } // namespace IO -} // namespace AZ - - + return stream.IsOpen(); + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/BlockCache.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/BlockCache.cpp index e838324408..6c873f3050 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/BlockCache.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/BlockCache.cpp @@ -15,736 +15,733 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + AZStd::shared_ptr BlockCacheConfig::AddStreamStackEntry( + const HardwareInformation& hardware, AZStd::shared_ptr parent) { - AZStd::shared_ptr BlockCacheConfig::AddStreamStackEntry( - const HardwareInformation& hardware, AZStd::shared_ptr parent) + size_t blockSize; + switch (m_blockSize) { - size_t blockSize; - switch (m_blockSize) - { - case BlockSize::MaxTransfer: - blockSize = hardware.m_maxTransfer; - break; - case BlockSize::MemoryAlignment: - blockSize = hardware.m_maxPhysicalSectorSize; - break; - case BlockSize::SizeAlignment: - blockSize = hardware.m_maxLogicalSectorSize; - break; - default: - blockSize = m_blockSize; - break; - } - - u32 cacheSize = static_cast(m_cacheSizeMib * 1_mib); - if (blockSize * 2 > cacheSize) - { - AZ_Warning("Streamer", false, "Size (%u) for BlockCache isn't big enough to hold at least two cache blocks of size (%zu). " - "The cache size will be increased to fit 2 cache blocks.", cacheSize, blockSize); - cacheSize = aznumeric_caster(blockSize * 2); - } - - auto stackEntry = AZStd::make_shared( - cacheSize, aznumeric_cast(blockSize), aznumeric_cast(hardware.m_maxPhysicalSectorSize), false); - stackEntry->SetNext(AZStd::move(parent)); - return stackEntry; + case BlockSize::MaxTransfer: + blockSize = hardware.m_maxTransfer; + break; + case BlockSize::MemoryAlignment: + blockSize = hardware.m_maxPhysicalSectorSize; + break; + case BlockSize::SizeAlignment: + blockSize = hardware.m_maxLogicalSectorSize; + break; + default: + blockSize = m_blockSize; + break; } - void BlockCacheConfig::Reflect(AZ::ReflectContext* context) + u32 cacheSize = static_cast(m_cacheSizeMib * 1_mib); + if (blockSize * 2 > cacheSize) { - if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) - { - serializeContext->Enum() - ->Version(1) - ->Value("MaxTransfer", BlockSize::MaxTransfer) - ->Value("MemoryAlignment", BlockSize::MemoryAlignment) - ->Value("SizeAlignment", BlockSize::SizeAlignment); - - serializeContext->Class() - ->Version(1) - ->Field("CacheSizeMib", &BlockCacheConfig::m_cacheSizeMib) - ->Field("BlockSize", &BlockCacheConfig::m_blockSize); - } + AZ_Warning("Streamer", false, "Size (%u) for BlockCache isn't big enough to hold at least two cache blocks of size (%zu). " + "The cache size will be increased to fit 2 cache blocks.", cacheSize, blockSize); + cacheSize = aznumeric_caster(blockSize * 2); } - static constexpr char CacheHitRateName[] = "Cache hit rate"; - static constexpr char CacheableName[] = "Cacheable"; + auto stackEntry = AZStd::make_shared( + cacheSize, aznumeric_cast(blockSize), aznumeric_cast(hardware.m_maxPhysicalSectorSize), false); + stackEntry->SetNext(AZStd::move(parent)); + return stackEntry; + } - void BlockCache::Section::Prefix(const Section& section) + void BlockCacheConfig::Reflect(AZ::ReflectContext* context) + { + if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) { - AZ_Assert(section.m_used, "Trying to prefix an unused section"); - AZ_Assert(!m_wait && !section.m_wait, "Can't merge two section that are already waiting for data to be loaded."); + serializeContext->Enum() + ->Version(1) + ->Value("MaxTransfer", BlockSize::MaxTransfer) + ->Value("MemoryAlignment", BlockSize::MemoryAlignment) + ->Value("SizeAlignment", BlockSize::SizeAlignment); - if (m_used) + serializeContext->Class() + ->Version(1) + ->Field("CacheSizeMib", &BlockCacheConfig::m_cacheSizeMib) + ->Field("BlockSize", &BlockCacheConfig::m_blockSize); + } + } + + static constexpr char CacheHitRateName[] = "Cache hit rate"; + static constexpr char CacheableName[] = "Cacheable"; + + void BlockCache::Section::Prefix(const Section& section) + { + AZ_Assert(section.m_used, "Trying to prefix an unused section"); + AZ_Assert(!m_wait && !section.m_wait, "Can't merge two section that are already waiting for data to be loaded."); + + if (m_used) + { + AZ_Assert(m_blockOffset == 0, "Unable to add a block cache to this one as this block requires an offset upon completion."); + + AZ_Assert(section.m_readOffset < m_readOffset, "The block that's being merged needs to come before this block."); + m_readOffset = section.m_readOffset + section.m_blockOffset; // Remove any alignment that might have been added. + m_readSize += section.m_readSize - section.m_blockOffset; + + AZ_Assert(section.m_output < m_output, "The block that's being merged needs to come before this block."); + m_output = section.m_output; + m_copySize += section.m_copySize; + } + else + { + m_used = true; + m_readOffset = section.m_readOffset + section.m_blockOffset; + m_readSize = section.m_readSize - section.m_blockOffset; + m_output = section.m_output; + m_copySize = section.m_copySize; + } + m_blockOffset = 0; // Two merged sections do not support caching. + } + + BlockCache::BlockCache(u64 cacheSize, u32 blockSize, u32 alignment, bool onlyEpilogWrites) + : StreamStackEntry("Block cache") + , m_alignment(alignment) + , m_onlyEpilogWrites(onlyEpilogWrites) + { + AZ_Assert(IStreamerTypes::IsPowerOf2(alignment), "Alignment needs to be a power of 2."); + AZ_Assert(IStreamerTypes::IsAlignedTo(blockSize, alignment), "Block size needs to be a multiple of the alignment."); + + m_numBlocks = aznumeric_caster(cacheSize / blockSize); + m_cacheSize = cacheSize - (cacheSize % blockSize); // Only use the amount needed for the cache. + m_blockSize = blockSize; + if (m_numBlocks == 1) + { + m_onlyEpilogWrites = true; + } + + m_cache = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( + m_cacheSize, alignment, 0, "AZ::IO::Streamer BlockCache", __FILE__, __LINE__)); + m_cachedPaths = AZStd::unique_ptr(new RequestPath[m_numBlocks]); + m_cachedOffsets = AZStd::unique_ptr(new u64[m_numBlocks]); + m_blockLastTouched = AZStd::unique_ptr(new TimePoint[m_numBlocks]); + m_inFlightRequests = AZStd::unique_ptr(new FileRequest*[m_numBlocks]); + + ResetCache(); + } + + BlockCache::~BlockCache() + { + AZ::AllocatorInstance::Get().DeAllocate(m_cache, m_cacheSize, m_alignment); + } + + void BlockCache::QueueRequest(FileRequest* request) + { + AZ_Assert(request, "QueueRequest was provided a null request."); + + AZStd::visit([this, request](auto&& args) + { + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) { - AZ_Assert(m_blockOffset == 0, "Unable to add a block cache to this one as this block requires an offset upon completion."); - - AZ_Assert(section.m_readOffset < m_readOffset, "The block that's being merged needs to come before this block."); - m_readOffset = section.m_readOffset + section.m_blockOffset; // Remove any alignment that might have been added. - m_readSize += section.m_readSize - section.m_blockOffset; - - AZ_Assert(section.m_output < m_output, "The block that's being merged needs to come before this block."); - m_output = section.m_output; - m_copySize += section.m_copySize; + ReadFile(request, args); + return; } else { - m_used = true; - m_readOffset = section.m_readOffset + section.m_blockOffset; - m_readSize = section.m_readSize - section.m_blockOffset; - m_output = section.m_output; - m_copySize = section.m_copySize; - } - m_blockOffset = 0; // Two merged sections do not support caching. - } - - BlockCache::BlockCache(u64 cacheSize, u32 blockSize, u32 alignment, bool onlyEpilogWrites) - : StreamStackEntry("Block cache") - , m_alignment(alignment) - , m_onlyEpilogWrites(onlyEpilogWrites) - { - AZ_Assert(IStreamerTypes::IsPowerOf2(alignment), "Alignment needs to be a power of 2."); - AZ_Assert(IStreamerTypes::IsAlignedTo(blockSize, alignment), "Block size needs to be a multiple of the alignment."); - - m_numBlocks = aznumeric_caster(cacheSize / blockSize); - m_cacheSize = cacheSize - (cacheSize % blockSize); // Only use the amount needed for the cache. - m_blockSize = blockSize; - if (m_numBlocks == 1) - { - m_onlyEpilogWrites = true; - } - - m_cache = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( - m_cacheSize, alignment, 0, "AZ::IO::Streamer BlockCache", __FILE__, __LINE__)); - m_cachedPaths = AZStd::unique_ptr(new RequestPath[m_numBlocks]); - m_cachedOffsets = AZStd::unique_ptr(new u64[m_numBlocks]); - m_blockLastTouched = AZStd::unique_ptr(new TimePoint[m_numBlocks]); - m_inFlightRequests = AZStd::unique_ptr(new FileRequest*[m_numBlocks]); - - ResetCache(); - } - - BlockCache::~BlockCache() - { - AZ::AllocatorInstance::Get().DeAllocate(m_cache, m_cacheSize, m_alignment); - } - - void BlockCache::QueueRequest(FileRequest* request) - { - AZ_Assert(request, "QueueRequest was provided a null request."); - - AZStd::visit([this, request](auto&& args) - { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) + if constexpr (AZStd::is_same_v) { - ReadFile(request, args); + FlushCache(args.m_path); + } + else if constexpr (AZStd::is_same_v) + { + FlushEntireCache(); + } + StreamStackEntry::QueueRequest(request); + } + }, request->GetCommand()); + } + + bool BlockCache::ExecuteRequests() + { + size_t delayedCount = m_delayedSections.size(); + + bool delayedRequestProcessed = false; + for (size_t i = 0; i < delayedCount; ++i) + { + Section& delayed = m_delayedSections.front(); + AZ_Assert(delayed.m_parent, "Delayed section doesn't have a reference to the original request."); + auto data = AZStd::get_if(&delayed.m_parent->GetCommand()); + AZ_Assert(data, "A request in the delayed queue of the BlockCache didn't have a parent with read data."); + // This call can add the same section to the back of the queue if there's not + // enough space. Because of this the entry needs to be removed from the delayed + // list no matter what the result is of ServiceFromCache. + if (ServiceFromCache(delayed.m_parent, delayed, data->m_path, data->m_sharedRead) != CacheResult::Delayed) + { + delayedRequestProcessed = true; + } + m_delayedSections.pop_front(); + } + bool nextResult = StreamStackEntry::ExecuteRequests(); + return nextResult || delayedRequestProcessed; + } + + void BlockCache::UpdateStatus(Status& status) const + { + StreamStackEntry::UpdateStatus(status); + s32 numAvailableSlots = CalculateAvailableRequestSlots(); + status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); + status.m_isIdle = status.m_isIdle && + static_cast(numAvailableSlots) == m_numBlocks && + m_delayedSections.empty(); + } + + void BlockCache::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, AZStd::vector& internalPending, + StreamerContext::PreparedQueue::iterator pendingBegin, StreamerContext::PreparedQueue::iterator pendingEnd) + { + // Have the stack downstream estimate the completion time for the requests that are waiting for a slot to execute in. + AddDelayedRequests(internalPending); + + StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); + + // The in-flight requests don't have to be updated because the subdivided request will bubble up in order so the final + // write will be the latest completion time. Requests that have a wait on another request though will need to be update + // as the estimation of the in-flight request needs to be copied to the wait request to get an accurate prediction. + UpdatePendingRequestEstimations(); + + // Technically here the wait commands for the delayed sections should be updated as well, but it's the parent that's interesting, + // not the wait so don't waste cycles updating the wait. + } + + void BlockCache::AddDelayedRequests(AZStd::vector& internalPending) + { + for (auto& section : m_delayedSections) + { + internalPending.push_back(section.m_parent); + } + } + + void BlockCache::UpdatePendingRequestEstimations() + { + for (auto it : m_pendingRequests) + { + Section& section = it.second; + AZ_Assert(section.m_cacheBlockIndex != s_fileNotCached, "An in-flight cache section doesn't have a cache block associated with it."); + AZ_Assert(m_inFlightRequests[section.m_cacheBlockIndex], + "Cache block %i is reported as being in-flight but has no request.", section.m_cacheBlockIndex); + if (section.m_wait) + { + AZ_Assert(section.m_parent, "A cache section with a wait request pending is missing a parent to wait on."); + auto largestTime = AZStd::max(section.m_parent->GetEstimatedCompletion(), it.first->GetEstimatedCompletion()); + section.m_wait->SetEstimatedCompletion(largestTime); + } + } + } + + void BlockCache::ReadFile(FileRequest* request, FileRequest::ReadData& data) + { + if (!m_next) + { + request->SetStatus(IStreamerTypes::RequestStatus::Failed); + m_context->MarkRequestAsCompleted(request); + return; + } + + auto continueReadFile = [this, request](FileRequest& fileSizeRequest) + { + AZ_PROFILE_FUNCTION(AzCore); + AZ_Assert(m_numMetaDataRetrievalInProgress > 0, + "More requests have completed meta data retrieval in the Block Cache than were requested."); + m_numMetaDataRetrievalInProgress--; + if (fileSizeRequest.GetStatus() == IStreamerTypes::RequestStatus::Completed) + { + auto& requestInfo = AZStd::get(fileSizeRequest.GetCommand()); + if (requestInfo.m_found) + { + ContinueReadFile(request, requestInfo.m_fileSize); return; } - else + } + // Couldn't find the file size so don't try to split and pass the request to the next entry in the stack. + StreamStackEntry::QueueRequest(request); + }; + m_numMetaDataRetrievalInProgress++; + FileRequest* fileSizeRequest = m_context->GetNewInternalRequest(); + fileSizeRequest->CreateFileMetaDataRetrieval(data.m_path); + fileSizeRequest->SetCompletionCallback(AZStd::move(continueReadFile)); + StreamStackEntry::QueueRequest(fileSizeRequest); + } + void BlockCache::ContinueReadFile(FileRequest* request, u64 fileLength) + { + Section prolog; + Section main; + Section epilog; + + auto& data = AZStd::get(request->GetCommand()); + + if (!SplitRequest(prolog, main, epilog, data.m_path, fileLength, data.m_offset, data.m_size, + reinterpret_cast(data.m_output))) + { + m_context->MarkRequestAsCompleted(request); + return; + } + + if (prolog.m_used || epilog.m_used) + { + m_cacheableStat.PushSample(1.0); + Statistic::PlotImmediate(m_name, CacheableName, m_cacheableStat.GetMostRecentSample()); + } + else + { + // Nothing to cache so simply forward the call to the next entry in the stack for direct reading. + m_cacheableStat.PushSample(0.0); + Statistic::PlotImmediate(m_name, CacheableName, m_cacheableStat.GetMostRecentSample()); + m_next->QueueRequest(request); + return; + } + + bool fullyCached = true; + if (prolog.m_used) + { + if (m_onlyEpilogWrites && (main.m_used || epilog.m_used)) + { + // Only the epilog is allowed to write to the cache, but a previous read could + // still have cached the prolog, so check the cache and use the data if it's there + // otherwise merge the section with the main section to have the data read. + if (ReadFromCache(request, prolog, data.m_path) == CacheResult::CacheMiss) { - if constexpr (AZStd::is_same_v) - { - FlushCache(args.m_path); - } - else if constexpr (AZStd::is_same_v) - { - FlushEntireCache(); - } - StreamStackEntry::QueueRequest(request); - } - }, request->GetCommand()); - } - - bool BlockCache::ExecuteRequests() - { - size_t delayedCount = m_delayedSections.size(); - - bool delayedRequestProcessed = false; - for (size_t i = 0; i < delayedCount; ++i) - { - Section& delayed = m_delayedSections.front(); - AZ_Assert(delayed.m_parent, "Delayed section doesn't have a reference to the original request."); - auto data = AZStd::get_if(&delayed.m_parent->GetCommand()); - AZ_Assert(data, "A request in the delayed queue of the BlockCache didn't have a parent with read data."); - // This call can add the same section to the back of the queue if there's not - // enough space. Because of this the entry needs to be removed from the delayed - // list no matter what the result is of ServiceFromCache. - if (ServiceFromCache(delayed.m_parent, delayed, data->m_path, data->m_sharedRead) != CacheResult::Delayed) - { - delayedRequestProcessed = true; - } - m_delayedSections.pop_front(); - } - bool nextResult = StreamStackEntry::ExecuteRequests(); - return nextResult || delayedRequestProcessed; - } - - void BlockCache::UpdateStatus(Status& status) const - { - StreamStackEntry::UpdateStatus(status); - s32 numAvailableSlots = CalculateAvailableRequestSlots(); - status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); - status.m_isIdle = status.m_isIdle && - static_cast(numAvailableSlots) == m_numBlocks && - m_delayedSections.empty(); - } - - void BlockCache::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, AZStd::vector& internalPending, - StreamerContext::PreparedQueue::iterator pendingBegin, StreamerContext::PreparedQueue::iterator pendingEnd) - { - // Have the stack downstream estimate the completion time for the requests that are waiting for a slot to execute in. - AddDelayedRequests(internalPending); - - StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); - - // The in-flight requests don't have to be updated because the subdivided request will bubble up in order so the final - // write will be the latest completion time. Requests that have a wait on another request though will need to be update - // as the estimation of the in-flight request needs to be copied to the wait request to get an accurate prediction. - UpdatePendingRequestEstimations(); - - // Technically here the wait commands for the delayed sections should be updated as well, but it's the parent that's interesting, - // not the wait so don't waste cycles updating the wait. - } - - void BlockCache::AddDelayedRequests(AZStd::vector& internalPending) - { - for (auto& section : m_delayedSections) - { - internalPending.push_back(section.m_parent); - } - } - - void BlockCache::UpdatePendingRequestEstimations() - { - for (auto it : m_pendingRequests) - { - Section& section = it.second; - AZ_Assert(section.m_cacheBlockIndex != s_fileNotCached, "An in-flight cache section doesn't have a cache block associated with it."); - AZ_Assert(m_inFlightRequests[section.m_cacheBlockIndex], - "Cache block %i is reported as being in-flight but has no request.", section.m_cacheBlockIndex); - if (section.m_wait) - { - AZ_Assert(section.m_parent, "A cache section with a wait request pending is missing a parent to wait on."); - auto largestTime = AZStd::max(section.m_parent->GetEstimatedCompletion(), it.first->GetEstimatedCompletion()); - section.m_wait->SetEstimatedCompletion(largestTime); - } - } - } - - void BlockCache::ReadFile(FileRequest* request, FileRequest::ReadData& data) - { - if (!m_next) - { - request->SetStatus(IStreamerTypes::RequestStatus::Failed); - m_context->MarkRequestAsCompleted(request); - return; - } - - auto continueReadFile = [this, request](FileRequest& fileSizeRequest) - { - AZ_PROFILE_FUNCTION(AzCore); - AZ_Assert(m_numMetaDataRetrievalInProgress > 0, - "More requests have completed meta data retrieval in the Block Cache than were requested."); - m_numMetaDataRetrievalInProgress--; - if (fileSizeRequest.GetStatus() == IStreamerTypes::RequestStatus::Completed) - { - auto& requestInfo = AZStd::get(fileSizeRequest.GetCommand()); - if (requestInfo.m_found) - { - ContinueReadFile(request, requestInfo.m_fileSize); - return; - } - } - // Couldn't find the file size so don't try to split and pass the request to the next entry in the stack. - StreamStackEntry::QueueRequest(request); - }; - m_numMetaDataRetrievalInProgress++; - FileRequest* fileSizeRequest = m_context->GetNewInternalRequest(); - fileSizeRequest->CreateFileMetaDataRetrieval(data.m_path); - fileSizeRequest->SetCompletionCallback(AZStd::move(continueReadFile)); - StreamStackEntry::QueueRequest(fileSizeRequest); - } - void BlockCache::ContinueReadFile(FileRequest* request, u64 fileLength) - { - Section prolog; - Section main; - Section epilog; - - auto& data = AZStd::get(request->GetCommand()); - - if (!SplitRequest(prolog, main, epilog, data.m_path, fileLength, data.m_offset, data.m_size, - reinterpret_cast(data.m_output))) - { - m_context->MarkRequestAsCompleted(request); - return; - } - - if (prolog.m_used || epilog.m_used) - { - m_cacheableStat.PushSample(1.0); - Statistic::PlotImmediate(m_name, CacheableName, m_cacheableStat.GetMostRecentSample()); - } - else - { - // Nothing to cache so simply forward the call to the next entry in the stack for direct reading. - m_cacheableStat.PushSample(0.0); - Statistic::PlotImmediate(m_name, CacheableName, m_cacheableStat.GetMostRecentSample()); - m_next->QueueRequest(request); - return; - } - - bool fullyCached = true; - if (prolog.m_used) - { - if (m_onlyEpilogWrites && (main.m_used || epilog.m_used)) - { - // Only the epilog is allowed to write to the cache, but a previous read could - // still have cached the prolog, so check the cache and use the data if it's there - // otherwise merge the section with the main section to have the data read. - if (ReadFromCache(request, prolog, data.m_path) == CacheResult::CacheMiss) - { - // The data isn't cached so put the prolog in front of the main section - // so it's read in one read request. If main wasn't used, prefixing the prolog - // will cause it to be filled in and used. - main.Prefix(prolog); - m_hitRateStat.PushSample(0.0); - Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); - } - else - { - m_hitRateStat.PushSample(1.0); - Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); - } + // The data isn't cached so put the prolog in front of the main section + // so it's read in one read request. If main wasn't used, prefixing the prolog + // will cause it to be filled in and used. + main.Prefix(prolog); + m_hitRateStat.PushSample(0.0); + Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); } else { - // If m_onlyEpilogWrites is set but main and epilog are not filled in, it means that - // the request was so small it fits in one cache block, in which case the prolog and - // epilog are practically the same. Or this code is reached because both prolog and - // epilog are allowed to write. - bool readFromCache = (ServiceFromCache(request, prolog, data.m_path, data.m_sharedRead) == CacheResult::ReadFromCache); - fullyCached = readFromCache && fullyCached; - - m_hitRateStat.PushSample(readFromCache ? 1.0 : 0.0); + m_hitRateStat.PushSample(1.0); Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); } } - - if (main.m_used) + else { - FileRequest* mainRequest = m_context->GetNewInternalRequest(); - // No need for a callback as there's nothing to do after the read has been completed. - mainRequest->CreateRead(request, main.m_output, main.m_readSize, data.m_path, - main.m_readOffset, main.m_readSize, data.m_sharedRead); - m_next->QueueRequest(mainRequest); - fullyCached = false; - } - - if (epilog.m_used) - { - bool readFromCache = (ServiceFromCache(request, epilog, data.m_path, data.m_sharedRead) == CacheResult::ReadFromCache); + // If m_onlyEpilogWrites is set but main and epilog are not filled in, it means that + // the request was so small it fits in one cache block, in which case the prolog and + // epilog are practically the same. Or this code is reached because both prolog and + // epilog are allowed to write. + bool readFromCache = (ServiceFromCache(request, prolog, data.m_path, data.m_sharedRead) == CacheResult::ReadFromCache); fullyCached = readFromCache && fullyCached; m_hitRateStat.PushSample(readFromCache ? 1.0 : 0.0); Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); } + } - if (fullyCached) + if (main.m_used) + { + FileRequest* mainRequest = m_context->GetNewInternalRequest(); + // No need for a callback as there's nothing to do after the read has been completed. + mainRequest->CreateRead(request, main.m_output, main.m_readSize, data.m_path, + main.m_readOffset, main.m_readSize, data.m_sharedRead); + m_next->QueueRequest(mainRequest); + fullyCached = false; + } + + if (epilog.m_used) + { + bool readFromCache = (ServiceFromCache(request, epilog, data.m_path, data.m_sharedRead) == CacheResult::ReadFromCache); + fullyCached = readFromCache && fullyCached; + + m_hitRateStat.PushSample(readFromCache ? 1.0 : 0.0); + Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); + } + + if (fullyCached) + { + request->SetStatus(IStreamerTypes::RequestStatus::Completed); + m_context->MarkRequestAsCompleted(request); + } + } + + void BlockCache::FlushCache(const RequestPath& filePath) + { + for (u32 i = 0; i < m_numBlocks; ++i) + { + if (m_cachedPaths[i] == filePath) { - request->SetStatus(IStreamerTypes::RequestStatus::Completed); - m_context->MarkRequestAsCompleted(request); + ResetCacheEntry(i); } } + } - void BlockCache::FlushCache(const RequestPath& filePath) + void BlockCache::FlushEntireCache() + { + ResetCache(); + } + + void BlockCache::CollectStatistics(AZStd::vector& statistics) const + { + statistics.push_back(Statistic::CreatePercentage(m_name, CacheHitRateName, CalculateHitRatePercentage())); + statistics.push_back(Statistic::CreatePercentage(m_name, CacheableName, CalculateCacheableRatePercentage())); + statistics.push_back(Statistic::CreateInteger(m_name, "Available slots", CalculateAvailableRequestSlots())); + + StreamStackEntry::CollectStatistics(statistics); + } + + double BlockCache::CalculateHitRatePercentage() const + { + return m_hitRateStat.GetAverage(); + } + + double BlockCache::CalculateCacheableRatePercentage() const + { + return m_cacheableStat.GetAverage(); + } + + s32 BlockCache::CalculateAvailableRequestSlots() const + { + return aznumeric_cast(m_numBlocks) - m_numInFlightRequests - m_numMetaDataRetrievalInProgress - + aznumeric_cast(m_delayedSections.size()); + } + + BlockCache::CacheResult BlockCache::ReadFromCache(FileRequest* request, Section& section, const RequestPath& filePath) + { + u32 cacheLocation = FindInCache(filePath, section.m_readOffset); + if (cacheLocation != s_fileNotCached) { - for (u32 i = 0; i < m_numBlocks; ++i) - { - if (m_cachedPaths[i] == filePath) - { - ResetCacheEntry(i); - } - } + return ReadFromCache(request, section, cacheLocation); } - - void BlockCache::FlushEntireCache() + else { - ResetCache(); + return CacheResult::CacheMiss; } + } - void BlockCache::CollectStatistics(AZStd::vector& statistics) const + BlockCache::CacheResult BlockCache::ReadFromCache(FileRequest* request, Section& section, u32 cacheBlock) + { + if (!IsCacheBlockInFlight(cacheBlock)) { - statistics.push_back(Statistic::CreatePercentage(m_name, CacheHitRateName, CalculateHitRatePercentage())); - statistics.push_back(Statistic::CreatePercentage(m_name, CacheableName, CalculateCacheableRatePercentage())); - statistics.push_back(Statistic::CreateInteger(m_name, "Available slots", CalculateAvailableRequestSlots())); - - StreamStackEntry::CollectStatistics(statistics); + TouchBlock(cacheBlock); + memcpy(section.m_output, GetCacheBlockData(cacheBlock) + section.m_blockOffset, section.m_copySize); + return CacheResult::ReadFromCache; } - - double BlockCache::CalculateHitRatePercentage() const + else { - return m_hitRateStat.GetAverage(); + AZ_Assert(section.m_wait == nullptr, "A wait request has to be set on a block cache section, but one has already been assigned."); + FileRequest* wait = m_context->GetNewInternalRequest(); + wait->CreateWait(request); + section.m_cacheBlockIndex = cacheBlock; + section.m_parent = request; + section.m_wait = wait; + m_pendingRequests.emplace(m_inFlightRequests[cacheBlock], section); + return CacheResult::Queued; } + } - double BlockCache::CalculateCacheableRatePercentage() const - { - return m_cacheableStat.GetAverage(); - } + BlockCache::CacheResult BlockCache::ServiceFromCache( + FileRequest* request, Section& section, const RequestPath& filePath, bool sharedRead) + { + AZ_Assert(m_next, "ServiceFromCache in BlockCache was called when the cache doesn't have a way to read files."); - s32 BlockCache::CalculateAvailableRequestSlots() const + u32 cacheLocation = FindInCache(filePath, section.m_readOffset); + if (cacheLocation == s_fileNotCached) { - return aznumeric_cast(m_numBlocks) - m_numInFlightRequests - m_numMetaDataRetrievalInProgress - - aznumeric_cast(m_delayedSections.size()); - } + m_hitRateStat.PushSample(0.0); + Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); - BlockCache::CacheResult BlockCache::ReadFromCache(FileRequest* request, Section& section, const RequestPath& filePath) - { - u32 cacheLocation = FindInCache(filePath, section.m_readOffset); + section.m_parent = request; + cacheLocation = RecycleOldestBlock(filePath, section.m_readOffset); if (cacheLocation != s_fileNotCached) { - return ReadFromCache(request, section, cacheLocation); - } - else - { - return CacheResult::CacheMiss; - } - } + FileRequest* readRequest = m_context->GetNewInternalRequest(); + readRequest->CreateRead(request, GetCacheBlockData(cacheLocation), m_blockSize, filePath, section.m_readOffset, + section.m_readSize, sharedRead); + readRequest->SetCompletionCallback([this](FileRequest& request) + { + AZ_PROFILE_FUNCTION(AzCore); + CompleteRead(request); + }); + section.m_cacheBlockIndex = cacheLocation; + m_inFlightRequests[cacheLocation] = readRequest; + m_numInFlightRequests++; - BlockCache::CacheResult BlockCache::ReadFromCache(FileRequest* request, Section& section, u32 cacheBlock) - { - if (!IsCacheBlockInFlight(cacheBlock)) - { - TouchBlock(cacheBlock); - memcpy(section.m_output, GetCacheBlockData(cacheBlock) + section.m_blockOffset, section.m_copySize); - return CacheResult::ReadFromCache; - } - else - { - AZ_Assert(section.m_wait == nullptr, "A wait request has to be set on a block cache section, but one has already been assigned."); - FileRequest* wait = m_context->GetNewInternalRequest(); - wait->CreateWait(request); - section.m_cacheBlockIndex = cacheBlock; - section.m_parent = request; - section.m_wait = wait; - m_pendingRequests.emplace(m_inFlightRequests[cacheBlock], section); + // If set, this is the wait added by the delay. + if (section.m_wait) + { + m_context->MarkRequestAsCompleted(section.m_wait); + section.m_wait = nullptr; + } + + m_pendingRequests.emplace(readRequest, section); + m_next->QueueRequest(readRequest); return CacheResult::Queued; } - } - - BlockCache::CacheResult BlockCache::ServiceFromCache( - FileRequest* request, Section& section, const RequestPath& filePath, bool sharedRead) - { - AZ_Assert(m_next, "ServiceFromCache in BlockCache was called when the cache doesn't have a way to read files."); - - u32 cacheLocation = FindInCache(filePath, section.m_readOffset); - if (cacheLocation == s_fileNotCached) - { - m_hitRateStat.PushSample(0.0); - Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); - - section.m_parent = request; - cacheLocation = RecycleOldestBlock(filePath, section.m_readOffset); - if (cacheLocation != s_fileNotCached) - { - FileRequest* readRequest = m_context->GetNewInternalRequest(); - readRequest->CreateRead(request, GetCacheBlockData(cacheLocation), m_blockSize, filePath, section.m_readOffset, - section.m_readSize, sharedRead); - readRequest->SetCompletionCallback([this](FileRequest& request) - { - AZ_PROFILE_FUNCTION(AzCore); - CompleteRead(request); - }); - section.m_cacheBlockIndex = cacheLocation; - m_inFlightRequests[cacheLocation] = readRequest; - m_numInFlightRequests++; - - // If set, this is the wait added by the delay. - if (section.m_wait) - { - m_context->MarkRequestAsCompleted(section.m_wait); - section.m_wait = nullptr; - } - - m_pendingRequests.emplace(readRequest, section); - m_next->QueueRequest(readRequest); - return CacheResult::Queued; - } - else - { - // There's no more space in the cache to store this request to. This is because there are more in-flight requests than - // there are slots in the cache. Delay the request until there's a slot available but add a wait for the section to - // make sure the request can't complete if some parts are read. - if (!section.m_wait) - { - section.m_wait = m_context->GetNewInternalRequest(); - section.m_wait->CreateWait(request); - } - m_delayedSections.push_back(section); - return CacheResult::Delayed; - } - } else { - // If set, this is the wait added by the delay when the cache was full. - if (section.m_wait) + // There's no more space in the cache to store this request to. This is because there are more in-flight requests than + // there are slots in the cache. Delay the request until there's a slot available but add a wait for the section to + // make sure the request can't complete if some parts are read. + if (!section.m_wait) { - m_context->MarkRequestAsCompleted(section.m_wait); - section.m_wait = nullptr; + section.m_wait = m_context->GetNewInternalRequest(); + section.m_wait->CreateWait(request); } - - m_hitRateStat.PushSample(1.0); - Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); - - return ReadFromCache(request, section, cacheLocation); + m_delayedSections.push_back(section); + return CacheResult::Delayed; } } - - void BlockCache::CompleteRead(FileRequest& request) + else { - auto requestInfo = m_pendingRequests.equal_range(&request); - AZ_Assert(requestInfo.first != requestInfo.second, "Block cache was asked to complete a file request it never queued."); - - IStreamerTypes::RequestStatus requestStatus = request.GetStatus(); - bool requestWasSuccessful = requestStatus == IStreamerTypes::RequestStatus::Completed; - u32 cacheBlockIndex = requestInfo.first->second.m_cacheBlockIndex; - - for (auto it = requestInfo.first; it != requestInfo.second; ++it) + // If set, this is the wait added by the delay when the cache was full. + if (section.m_wait) { - Section& section = it->second; - AZ_Assert(section.m_cacheBlockIndex == cacheBlockIndex, - "Section associated with the file request is referencing the incorrect cache block (%u vs %u).", cacheBlockIndex, section.m_cacheBlockIndex); - if (section.m_wait) - { - section.m_wait->SetStatus(requestStatus); - m_context->MarkRequestAsCompleted(section.m_wait); - section.m_wait = nullptr; - } + m_context->MarkRequestAsCompleted(section.m_wait); + section.m_wait = nullptr; + } - if (requestWasSuccessful) - { - memcpy(section.m_output, GetCacheBlockData(cacheBlockIndex) + section.m_blockOffset, section.m_copySize); - } + m_hitRateStat.PushSample(1.0); + Statistic::PlotImmediate(m_name, CacheHitRateName, m_hitRateStat.GetMostRecentSample()); + + return ReadFromCache(request, section, cacheLocation); + } + } + + void BlockCache::CompleteRead(FileRequest& request) + { + auto requestInfo = m_pendingRequests.equal_range(&request); + AZ_Assert(requestInfo.first != requestInfo.second, "Block cache was asked to complete a file request it never queued."); + + IStreamerTypes::RequestStatus requestStatus = request.GetStatus(); + bool requestWasSuccessful = requestStatus == IStreamerTypes::RequestStatus::Completed; + u32 cacheBlockIndex = requestInfo.first->second.m_cacheBlockIndex; + + for (auto it = requestInfo.first; it != requestInfo.second; ++it) + { + Section& section = it->second; + AZ_Assert(section.m_cacheBlockIndex == cacheBlockIndex, + "Section associated with the file request is referencing the incorrect cache block (%u vs %u).", cacheBlockIndex, section.m_cacheBlockIndex); + if (section.m_wait) + { + section.m_wait->SetStatus(requestStatus); + m_context->MarkRequestAsCompleted(section.m_wait); + section.m_wait = nullptr; } if (requestWasSuccessful) { - TouchBlock(cacheBlockIndex); - m_inFlightRequests[cacheBlockIndex] = nullptr; + memcpy(section.m_output, GetCacheBlockData(cacheBlockIndex) + section.m_blockOffset, section.m_copySize); } - else - { - ResetCacheEntry(cacheBlockIndex); - } - AZ_Assert(m_numInFlightRequests > 0, "Clearing out an in-flight request, but there shouldn't be any in flight according to records."); - m_numInFlightRequests--; - m_pendingRequests.erase(&request); } - bool BlockCache::SplitRequest(Section& prolog, Section& main, Section& epilog, - [[maybe_unused]] const RequestPath& filePath, u64 fileLength, - u64 offset, u64 size, u8* buffer) const + if (requestWasSuccessful) { - AZ_Assert(offset + size <= fileLength, "File at path '%s' is being read past the end of the file.", filePath.GetRelativePath()); + TouchBlock(cacheBlockIndex); + m_inFlightRequests[cacheBlockIndex] = nullptr; + } + else + { + ResetCacheEntry(cacheBlockIndex); + } + AZ_Assert(m_numInFlightRequests > 0, "Clearing out an in-flight request, but there shouldn't be any in flight according to records."); + m_numInFlightRequests--; + m_pendingRequests.erase(&request); + } - // - // Prolog - // This looks at the request and sees if there's anything in front of the file that should be cached. This also - // deals with the situation where the entire file request fits inside the cache which could mean there's data - // left after the file as well that could be cached. - // - u64 roundedOffsetStart = AZ_SIZE_ALIGN_DOWN(offset, aznumeric_cast(m_blockSize)); - - u64 blockReadSizeStart = AZStd::min(fileLength - roundedOffsetStart, aznumeric_cast(m_blockSize)); - // Check if the request is on the left edge of the cache block, which means there's nothing in front of it - // that could be cached. - if (roundedOffsetStart == offset) - { - if (offset + size >= fileLength) - { - // The entire (remainder) of the file is read so there's nothing to cache - main.m_readOffset = offset; - main.m_readSize = size; - main.m_output = buffer; - main.m_used = true; - return true; - } - else if (size < blockReadSizeStart) - { - // The entire request fits inside a single cache block, but there's more file to read. - prolog.m_readOffset = offset; - prolog.m_readSize = blockReadSizeStart; - prolog.m_blockOffset = 0; - prolog.m_output = buffer; - prolog.m_copySize = size; - prolog.m_used = true; - return true; - } - // In any other case it means that the entire block would be read so caching has no effect. - } - else - { - // There is a portion of the file before that's not requested so always cache this block. - const u64 blockOffset = offset - roundedOffsetStart; - prolog.m_readOffset = roundedOffsetStart; - prolog.m_blockOffset = blockOffset; - prolog.m_output = buffer; - prolog.m_used = true; + bool BlockCache::SplitRequest(Section& prolog, Section& main, Section& epilog, + [[maybe_unused]] const RequestPath& filePath, u64 fileLength, + u64 offset, u64 size, u8* buffer) const + { + AZ_Assert(offset + size <= fileLength, "File at path '%s' is being read past the end of the file.", filePath.GetRelativePath()); - const bool isEntirelyInCache = blockOffset + size <= blockReadSizeStart; - if (isEntirelyInCache) - { - // The read size is already clamped to the file size above when blockReadSizeStart is set. - AZ_Assert(roundedOffsetStart + blockReadSizeStart <= fileLength, - "Read size in block cache was set to %llu but this is beyond the file length of %llu.", - roundedOffsetStart + blockReadSizeStart, fileLength); - prolog.m_readSize = blockReadSizeStart; - prolog.m_copySize = size; + // + // Prolog + // This looks at the request and sees if there's anything in front of the file that should be cached. This also + // deals with the situation where the entire file request fits inside the cache which could mean there's data + // left after the file as well that could be cached. + // + u64 roundedOffsetStart = AZ_SIZE_ALIGN_DOWN(offset, aznumeric_cast(m_blockSize)); - // There won't be anything else coming after this so continue reading. - return true; - } - else - { - prolog.m_readSize = blockReadSizeStart; - prolog.m_copySize = blockReadSizeStart - blockOffset; - } - } - - - // - // Epilog - // Since the prolog already takes care of the situation where the file fits entirely in the cache the epilog is - // much simpler as it only has to look at the case where there is more file after the request to read for caching. - // - u64 roundedOffsetEnd = AZ_SIZE_ALIGN_DOWN(offset + size, aznumeric_cast(m_blockSize)); - u64 copySize = offset + size - roundedOffsetEnd; - u64 blockReadSizeEnd = m_blockSize; - if ((roundedOffsetEnd + blockReadSizeEnd) > fileLength) + u64 blockReadSizeStart = AZStd::min(fileLength - roundedOffsetStart, aznumeric_cast(m_blockSize)); + // Check if the request is on the left edge of the cache block, which means there's nothing in front of it + // that could be cached. + if (roundedOffsetStart == offset) + { + if (offset + size >= fileLength) { - blockReadSizeEnd = fileLength - roundedOffsetEnd; - } - - // If the read doesn't align with the edge of the cache - if (copySize != 0 && copySize < blockReadSizeEnd) - { - epilog.m_readOffset = roundedOffsetEnd; - epilog.m_readSize = blockReadSizeEnd; - epilog.m_blockOffset = 0; - epilog.m_output = buffer + (roundedOffsetEnd - offset); - epilog.m_copySize = copySize; - epilog.m_used = true; - } - - // - // Main - // If this point is reached there's potentially a block between the prolog and epilog that can be directly read. - // - u64 adjustedOffset = offset; - if (prolog.m_used) - { - adjustedOffset += prolog.m_copySize; - size -= prolog.m_copySize; - } - if (epilog.m_used) - { - size -= epilog.m_copySize; - } - AZ_Assert(IStreamerTypes::IsAlignedTo(adjustedOffset, m_blockSize), - "The adjustments made by the prolog should guarantee the offset is aligned to a cache block."); - if (size != 0) - { - main.m_readOffset = adjustedOffset; + // The entire (remainder) of the file is read so there's nothing to cache + main.m_readOffset = offset; main.m_readSize = size; - main.m_output = buffer + (adjustedOffset - offset); + main.m_output = buffer; main.m_used = true; + return true; } - - return true; - } - - u8* BlockCache::GetCacheBlockData(u32 index) - { - AZ_Assert(index < m_numBlocks, "Index for touch a cache entry in the BlockCache is out of bounds."); - return m_cache + (index * m_blockSize); - } - - void BlockCache::TouchBlock(u32 index) - { - AZ_Assert(index < m_numBlocks, "Index for touch a cache entry in the BlockCache is out of bounds."); - m_blockLastTouched[index] = AZStd::chrono::high_resolution_clock::now(); - } - - u32 BlockCache::RecycleOldestBlock(const RequestPath& filePath, u64 offset) - { - AZ_Assert((offset & (m_blockSize - 1)) == 0, "The offset used to recycle a block cache needs to be a multiple of the block size."); - - // Find the oldest cache block. - TimePoint oldest = m_blockLastTouched[0]; - u32 oldestIndex = 0; - for (u32 i = 1; i < m_numBlocks; ++i) + else if (size < blockReadSizeStart) { - if (m_blockLastTouched[i] < oldest && !m_inFlightRequests[i]) - { - oldest = m_blockLastTouched[i]; - oldestIndex = i; - } + // The entire request fits inside a single cache block, but there's more file to read. + prolog.m_readOffset = offset; + prolog.m_readSize = blockReadSizeStart; + prolog.m_blockOffset = 0; + prolog.m_output = buffer; + prolog.m_copySize = size; + prolog.m_used = true; + return true; } + // In any other case it means that the entire block would be read so caching has no effect. + } + else + { + // There is a portion of the file before that's not requested so always cache this block. + const u64 blockOffset = offset - roundedOffsetStart; + prolog.m_readOffset = roundedOffsetStart; + prolog.m_blockOffset = blockOffset; + prolog.m_output = buffer; + prolog.m_used = true; - if (!IsCacheBlockInFlight(oldestIndex)) + const bool isEntirelyInCache = blockOffset + size <= blockReadSizeStart; + if (isEntirelyInCache) { - // Recycle the block. - m_cachedPaths[oldestIndex] = filePath; - m_cachedOffsets[oldestIndex] = offset; - TouchBlock(oldestIndex); - return oldestIndex; + // The read size is already clamped to the file size above when blockReadSizeStart is set. + AZ_Assert(roundedOffsetStart + blockReadSizeStart <= fileLength, + "Read size in block cache was set to %llu but this is beyond the file length of %llu.", + roundedOffsetStart + blockReadSizeStart, fileLength); + prolog.m_readSize = blockReadSizeStart; + prolog.m_copySize = size; + + // There won't be anything else coming after this so continue reading. + return true; } else { - return s_fileNotCached; + prolog.m_readSize = blockReadSizeStart; + prolog.m_copySize = blockReadSizeStart - blockOffset; } } - u32 BlockCache::FindInCache(const RequestPath& filePath, u64 offset) const - { - AZ_Assert((offset & (m_blockSize - 1)) == 0, "The offset used to find a block in the block cache needs to be a multiple of the block size."); - for (u32 i = 0; i < m_numBlocks; ++i) - { - if (m_cachedPaths[i] == filePath && m_cachedOffsets[i] == offset) - { - return i; - } - } + // + // Epilog + // Since the prolog already takes care of the situation where the file fits entirely in the cache the epilog is + // much simpler as it only has to look at the case where there is more file after the request to read for caching. + // + u64 roundedOffsetEnd = AZ_SIZE_ALIGN_DOWN(offset + size, aznumeric_cast(m_blockSize)); + u64 copySize = offset + size - roundedOffsetEnd; + u64 blockReadSizeEnd = m_blockSize; + if ((roundedOffsetEnd + blockReadSizeEnd) > fileLength) + { + blockReadSizeEnd = fileLength - roundedOffsetEnd; + } + + // If the read doesn't align with the edge of the cache + if (copySize != 0 && copySize < blockReadSizeEnd) + { + epilog.m_readOffset = roundedOffsetEnd; + epilog.m_readSize = blockReadSizeEnd; + epilog.m_blockOffset = 0; + epilog.m_output = buffer + (roundedOffsetEnd - offset); + epilog.m_copySize = copySize; + epilog.m_used = true; + } + + // + // Main + // If this point is reached there's potentially a block between the prolog and epilog that can be directly read. + // + u64 adjustedOffset = offset; + if (prolog.m_used) + { + adjustedOffset += prolog.m_copySize; + size -= prolog.m_copySize; + } + if (epilog.m_used) + { + size -= epilog.m_copySize; + } + AZ_Assert(IStreamerTypes::IsAlignedTo(adjustedOffset, m_blockSize), + "The adjustments made by the prolog should guarantee the offset is aligned to a cache block."); + if (size != 0) + { + main.m_readOffset = adjustedOffset; + main.m_readSize = size; + main.m_output = buffer + (adjustedOffset - offset); + main.m_used = true; + } + + return true; + } + + u8* BlockCache::GetCacheBlockData(u32 index) + { + AZ_Assert(index < m_numBlocks, "Index for touch a cache entry in the BlockCache is out of bounds."); + return m_cache + (index * m_blockSize); + } + + void BlockCache::TouchBlock(u32 index) + { + AZ_Assert(index < m_numBlocks, "Index for touch a cache entry in the BlockCache is out of bounds."); + m_blockLastTouched[index] = AZStd::chrono::high_resolution_clock::now(); + } + + u32 BlockCache::RecycleOldestBlock(const RequestPath& filePath, u64 offset) + { + AZ_Assert((offset & (m_blockSize - 1)) == 0, "The offset used to recycle a block cache needs to be a multiple of the block size."); + + // Find the oldest cache block. + TimePoint oldest = m_blockLastTouched[0]; + u32 oldestIndex = 0; + for (u32 i = 1; i < m_numBlocks; ++i) + { + if (m_blockLastTouched[i] < oldest && !m_inFlightRequests[i]) + { + oldest = m_blockLastTouched[i]; + oldestIndex = i; + } + } + + if (!IsCacheBlockInFlight(oldestIndex)) + { + // Recycle the block. + m_cachedPaths[oldestIndex] = filePath; + m_cachedOffsets[oldestIndex] = offset; + TouchBlock(oldestIndex); + return oldestIndex; + } + else + { return s_fileNotCached; } + } - bool BlockCache::IsCacheBlockInFlight(u32 index) const + u32 BlockCache::FindInCache(const RequestPath& filePath, u64 offset) const + { + AZ_Assert((offset & (m_blockSize - 1)) == 0, "The offset used to find a block in the block cache needs to be a multiple of the block size."); + for (u32 i = 0; i < m_numBlocks; ++i) { - AZ_Assert(index < m_numBlocks, "Index for checking if a cache block is in flight is out of bounds."); - return m_inFlightRequests[index] != nullptr; - } - - void BlockCache::ResetCacheEntry(u32 index) - { - AZ_Assert(index < m_numBlocks, "Index for resetting a cache entry in the BlockCache is out of bounds."); - - m_cachedPaths[index].Clear(); - m_cachedOffsets[index] = 0; - m_blockLastTouched[index] = TimePoint::min(); - m_inFlightRequests[index] = nullptr; - } - - void BlockCache::ResetCache() - { - for (u32 i = 0; i < m_numBlocks; ++i) + if (m_cachedPaths[i] == filePath && m_cachedOffsets[i] == offset) { - ResetCacheEntry(i); + return i; } - m_numInFlightRequests = 0; } - } // namespace IO -} // namespace AZ + + return s_fileNotCached; + } + + bool BlockCache::IsCacheBlockInFlight(u32 index) const + { + AZ_Assert(index < m_numBlocks, "Index for checking if a cache block is in flight is out of bounds."); + return m_inFlightRequests[index] != nullptr; + } + + void BlockCache::ResetCacheEntry(u32 index) + { + AZ_Assert(index < m_numBlocks, "Index for resetting a cache entry in the BlockCache is out of bounds."); + + m_cachedPaths[index].Clear(); + m_cachedOffsets[index] = 0; + m_blockLastTouched[index] = TimePoint::min(); + m_inFlightRequests[index] = nullptr; + } + + void BlockCache::ResetCache() + { + for (u32 i = 0; i < m_numBlocks; ++i) + { + ResetCacheEntry(i); + } + m_numInFlightRequests = 0; + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/DedicatedCache.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/DedicatedCache.cpp index e0e512e21f..b80a1ea724 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/DedicatedCache.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/DedicatedCache.cpp @@ -12,320 +12,317 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + AZStd::shared_ptr DedicatedCacheConfig::AddStreamStackEntry( + const HardwareInformation& hardware, AZStd::shared_ptr parent) { - AZStd::shared_ptr DedicatedCacheConfig::AddStreamStackEntry( - const HardwareInformation& hardware, AZStd::shared_ptr parent) + size_t blockSize; + switch (m_blockSize) { - size_t blockSize; - switch (m_blockSize) + case BlockCacheConfig::BlockSize::MaxTransfer: + blockSize = hardware.m_maxTransfer; + break; + case BlockCacheConfig::BlockSize::MemoryAlignment: + blockSize = hardware.m_maxPhysicalSectorSize; + break; + case BlockCacheConfig::BlockSize::SizeAlignment: + blockSize = hardware.m_maxLogicalSectorSize; + break; + default: + blockSize = m_blockSize; + break; + } + + u32 cacheSize = static_cast(m_cacheSizeMib * 1_mib); + if (blockSize > cacheSize) + { + AZ_Warning("Streamer", false, "Size (%u) for DedicatedCache isn't big enough to hold at least one cache blocks of size (%zu). " + "The cache size will be increased to fit one cache block.", cacheSize, blockSize); + cacheSize = aznumeric_caster(blockSize); + } + + auto stackEntry = AZStd::make_shared( + cacheSize, aznumeric_cast(blockSize), aznumeric_cast(hardware.m_maxPhysicalSectorSize), m_writeOnlyEpilog); + stackEntry->SetNext(AZStd::move(parent)); + return stackEntry; + } + + void DedicatedCacheConfig::Reflect(AZ::ReflectContext* context) + { + if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) + { + serializeContext->Class() + ->Version(1) + ->Field("CacheSizeMib", &DedicatedCacheConfig::m_cacheSizeMib) + ->Field("BlockSize", &DedicatedCacheConfig::m_blockSize) + ->Field("WriteOnlyEpilog", &DedicatedCacheConfig::m_writeOnlyEpilog); + } + } + + + + // + // DedicatedCache + // + + DedicatedCache::DedicatedCache(u64 cacheSize, u32 blockSize, u32 alignment, bool onlyEpilogWrites) + : StreamStackEntry("Dedicated cache") + , m_cacheSize(cacheSize) + , m_alignment(alignment) + , m_blockSize(blockSize) + , m_onlyEpilogWrites(onlyEpilogWrites) + { + } + + void DedicatedCache::SetNext(AZStd::shared_ptr next) + { + m_next = AZStd::move(next); + for (AZStd::unique_ptr& cache : m_cachedFileCaches) + { + cache->SetNext(m_next); + } + } + + void DedicatedCache::SetContext(StreamerContext& context) + { + StreamStackEntry::SetContext(context); + for (AZStd::unique_ptr& cache : m_cachedFileCaches) + { + cache->SetContext(context); + } + } + + void DedicatedCache::PrepareRequest(FileRequest* request) + { + AZ_Assert(request, "PrepareRequest was provided a null request."); + + // Claim the requests so other entries can't claim it and make updates. + AZStd::visit([this, request](auto&& args) + { + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) { - case BlockCacheConfig::BlockSize::MaxTransfer: - blockSize = hardware.m_maxTransfer; - break; - case BlockCacheConfig::BlockSize::MemoryAlignment: - blockSize = hardware.m_maxPhysicalSectorSize; - break; - case BlockCacheConfig::BlockSize::SizeAlignment: - blockSize = hardware.m_maxLogicalSectorSize; - break; - default: - blockSize = m_blockSize; - break; + args.m_range = FileRange::CreateRangeForEntireFile(); + m_context->PushPreparedRequest(request); } - - u32 cacheSize = static_cast(m_cacheSizeMib * 1_mib); - if (blockSize > cacheSize) + else if constexpr (AZStd::is_same_v) { - AZ_Warning("Streamer", false, "Size (%u) for DedicatedCache isn't big enough to hold at least one cache blocks of size (%zu). " - "The cache size will be increased to fit one cache block.", cacheSize, blockSize); - cacheSize = aznumeric_caster(blockSize); - } - - auto stackEntry = AZStd::make_shared( - cacheSize, aznumeric_cast(blockSize), aznumeric_cast(hardware.m_maxPhysicalSectorSize), m_writeOnlyEpilog); - stackEntry->SetNext(AZStd::move(parent)); - return stackEntry; - } - - void DedicatedCacheConfig::Reflect(AZ::ReflectContext* context) - { - if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) - { - serializeContext->Class() - ->Version(1) - ->Field("CacheSizeMib", &DedicatedCacheConfig::m_cacheSizeMib) - ->Field("BlockSize", &DedicatedCacheConfig::m_blockSize) - ->Field("WriteOnlyEpilog", &DedicatedCacheConfig::m_writeOnlyEpilog); - } - } - - - - // - // DedicatedCache - // - - DedicatedCache::DedicatedCache(u64 cacheSize, u32 blockSize, u32 alignment, bool onlyEpilogWrites) - : StreamStackEntry("Dedicated cache") - , m_cacheSize(cacheSize) - , m_alignment(alignment) - , m_blockSize(blockSize) - , m_onlyEpilogWrites(onlyEpilogWrites) - { - } - - void DedicatedCache::SetNext(AZStd::shared_ptr next) - { - m_next = AZStd::move(next); - for (AZStd::unique_ptr& cache : m_cachedFileCaches) - { - cache->SetNext(m_next); - } - } - - void DedicatedCache::SetContext(StreamerContext& context) - { - StreamStackEntry::SetContext(context); - for (AZStd::unique_ptr& cache : m_cachedFileCaches) - { - cache->SetContext(context); - } - } - - void DedicatedCache::PrepareRequest(FileRequest* request) - { - AZ_Assert(request, "PrepareRequest was provided a null request."); - - // Claim the requests so other entries can't claim it and make updates. - AZStd::visit([this, request](auto&& args) - { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) - { - args.m_range = FileRange::CreateRangeForEntireFile(); - m_context->PushPreparedRequest(request); - } - else if constexpr (AZStd::is_same_v) - { - args.m_range = FileRange::CreateRangeForEntireFile(); - m_context->PushPreparedRequest(request); - } - else - { - StreamStackEntry::PrepareRequest(request); - } - }, request->GetCommand()); - } - - void DedicatedCache::QueueRequest(FileRequest* request) - { - AZ_Assert(request, "QueueRequest was provided a null request."); - - AZStd::visit([this, request](auto&& args) - { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) - { - ReadFile(request, args); - return; - } - else if constexpr (AZStd::is_same_v) - { - CreateDedicatedCache(request, args); - return; - } - else if constexpr (AZStd::is_same_v) - { - DestroyDedicatedCache(request, args); - return; - } - else - { - if constexpr (AZStd::is_same_v) - { - FlushCache(args.m_path); - } - else if constexpr (AZStd::is_same_v) - { - FlushEntireCache(); - } - StreamStackEntry::QueueRequest(request); - } - }, request->GetCommand()); - } - - bool DedicatedCache::ExecuteRequests() - { - bool hasProcessedRequest = false; - for (AZStd::unique_ptr& cache : m_cachedFileCaches) - { - hasProcessedRequest = cache->ExecuteRequests() || hasProcessedRequest; - } - return StreamStackEntry::ExecuteRequests() || hasProcessedRequest; - } - - void DedicatedCache::UpdateStatus(Status& status) const - { - // Available slots are not updated because the dedicated caches are often - // small and specific to a tiny subset of files that are loaded. It would therefore - // return a small number of slots that would needlessly hamper streaming as it doesn't - // apply to the majority of files. - - bool isIdle = true; - for (auto& cache : m_cachedFileCaches) - { - Status blockStatus; - cache->UpdateStatus(blockStatus); - isIdle = isIdle && blockStatus.m_isIdle; - } - status.m_isIdle = status.m_isIdle && isIdle; - StreamStackEntry::UpdateStatus(status); - } - - void DedicatedCache::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, - AZStd::vector& internalPending, StreamerContext::PreparedQueue::iterator pendingBegin, - StreamerContext::PreparedQueue::iterator pendingEnd) - { - for (auto& cache : m_cachedFileCaches) - { - cache->AddDelayedRequests(internalPending); - } - - StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); - - for (auto& cache : m_cachedFileCaches) - { - cache->UpdatePendingRequestEstimations(); - } - } - - void DedicatedCache::ReadFile(FileRequest* request, FileRequest::ReadData& data) - { - size_t index = FindCache(data.m_path, data.m_offset); - if (index == s_fileNotFound) - { - m_usagePercentageStat.PushSample(0.0); - if (m_next) - { - m_next->QueueRequest(request); - } + args.m_range = FileRange::CreateRangeForEntireFile(); + m_context->PushPreparedRequest(request); } else { - m_usagePercentageStat.PushSample(1.0); - BlockCache& cache = *m_cachedFileCaches[index]; - cache.QueueRequest(request); -#if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - m_overallHitRateStat.PushSample(cache.CalculateHitRatePercentage()); - m_overallCacheableRateStat.PushSample(cache.CalculateCacheableRatePercentage()); -#endif + StreamStackEntry::PrepareRequest(request); } - } + }, request->GetCommand()); + } - void DedicatedCache::FlushCache(const RequestPath& filePath) + void DedicatedCache::QueueRequest(FileRequest* request) + { + AZ_Assert(request, "QueueRequest was provided a null request."); + + AZStd::visit([this, request](auto&& args) { - size_t count = m_cachedFileNames.size(); - for (size_t i = 0; i < count; ++i) + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) { - if (m_cachedFileNames[i] == filePath) - { - // Flush the entire block cache as it's entirely dedicated to the found file. - m_cachedFileCaches[i]->FlushEntireCache(); - } + ReadFile(request, args); + return; } - } - - void DedicatedCache::FlushEntireCache() - { - for (AZStd::unique_ptr& cache : m_cachedFileCaches) + else if constexpr (AZStd::is_same_v) { - cache->FlushEntireCache(); + CreateDedicatedCache(request, args); + return; } - } - - void DedicatedCache::CollectStatistics(AZStd::vector& statistics) const - { - statistics.push_back(Statistic::CreatePercentage(m_name, "Reads from dedicated cache", m_usagePercentageStat.GetAverage())); -#if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - statistics.push_back(Statistic::CreatePercentage(m_name, "Overall cacheable rate", m_overallCacheableRateStat.GetAverage())); - statistics.push_back(Statistic::CreatePercentage(m_name, "Overall hit rate", m_overallHitRateStat.GetAverage())); -#endif - statistics.push_back(Statistic::CreateInteger(m_name, "Num dedicated caches", aznumeric_caster(m_cachedFileNames.size()))); - StreamStackEntry::CollectStatistics(statistics); - } - - void DedicatedCache::CreateDedicatedCache(FileRequest* request, FileRequest::CreateDedicatedCacheData& data) - { - size_t index = FindCache(data.m_path, data.m_range); - if (index == s_fileNotFound) + else if constexpr (AZStd::is_same_v) { - index = m_cachedFileCaches.size(); - m_cachedFileNames.push_back(data.m_path); - m_cachedFileRanges.push_back(data.m_range); - m_cachedFileCaches.push_back(AZStd::make_unique(m_cacheSize, m_blockSize, m_alignment, m_onlyEpilogWrites)); - m_cachedFileCaches[index]->SetNext(m_next); - m_cachedFileCaches[index]->SetContext(*m_context); - m_cachedFileRefCounts.push_back(1); + DestroyDedicatedCache(request, args); + return; } else { - ++m_cachedFileRefCounts[index]; + if constexpr (AZStd::is_same_v) + { + FlushCache(args.m_path); + } + else if constexpr (AZStd::is_same_v) + { + FlushEntireCache(); + } + StreamStackEntry::QueueRequest(request); } - request->SetStatus(IStreamerTypes::RequestStatus::Completed); - m_context->MarkRequestAsCompleted(request); + }, request->GetCommand()); + } + + bool DedicatedCache::ExecuteRequests() + { + bool hasProcessedRequest = false; + for (AZStd::unique_ptr& cache : m_cachedFileCaches) + { + hasProcessedRequest = cache->ExecuteRequests() || hasProcessedRequest; + } + return StreamStackEntry::ExecuteRequests() || hasProcessedRequest; + } + + void DedicatedCache::UpdateStatus(Status& status) const + { + // Available slots are not updated because the dedicated caches are often + // small and specific to a tiny subset of files that are loaded. It would therefore + // return a small number of slots that would needlessly hamper streaming as it doesn't + // apply to the majority of files. + + bool isIdle = true; + for (auto& cache : m_cachedFileCaches) + { + Status blockStatus; + cache->UpdateStatus(blockStatus); + isIdle = isIdle && blockStatus.m_isIdle; + } + status.m_isIdle = status.m_isIdle && isIdle; + StreamStackEntry::UpdateStatus(status); + } + + void DedicatedCache::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, + AZStd::vector& internalPending, StreamerContext::PreparedQueue::iterator pendingBegin, + StreamerContext::PreparedQueue::iterator pendingEnd) + { + for (auto& cache : m_cachedFileCaches) + { + cache->AddDelayedRequests(internalPending); } - void DedicatedCache::DestroyDedicatedCache(FileRequest* request, FileRequest::DestroyDedicatedCacheData& data) - { - size_t index = FindCache(data.m_path, data.m_range); - if (index != s_fileNotFound) - { - if (m_cachedFileRefCounts[index] > 0) - { - --m_cachedFileRefCounts[index]; - if (m_cachedFileRefCounts[index] == 0) - { - m_cachedFileNames.erase(m_cachedFileNames.begin() + index); - m_cachedFileRanges.erase(m_cachedFileRanges.begin() + index); - m_cachedFileCaches.erase(m_cachedFileCaches.begin() + index); - m_cachedFileRefCounts.erase(m_cachedFileRefCounts.begin() + index); - } - request->SetStatus(IStreamerTypes::RequestStatus::Completed); - m_context->MarkRequestAsCompleted(request); - return; - } - } - request->SetStatus(IStreamerTypes::RequestStatus::Failed); - m_context->MarkRequestAsCompleted(request); - } + StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); - size_t DedicatedCache::FindCache(const RequestPath& filename, FileRange range) + for (auto& cache : m_cachedFileCaches) { - size_t count = m_cachedFileNames.size(); - for (size_t i = 0; i < count; ++i) - { - if (m_cachedFileNames[i] == filename && m_cachedFileRanges[i] == range) - { - return i; - } - } - return s_fileNotFound; + cache->UpdatePendingRequestEstimations(); } + } - size_t DedicatedCache::FindCache(const RequestPath& filename, u64 offset) + void DedicatedCache::ReadFile(FileRequest* request, FileRequest::ReadData& data) + { + size_t index = FindCache(data.m_path, data.m_offset); + if (index == s_fileNotFound) { - size_t count = m_cachedFileNames.size(); - for (size_t i = 0; i < count; ++i) + m_usagePercentageStat.PushSample(0.0); + if (m_next) { - if (m_cachedFileNames[i] == filename && m_cachedFileRanges[i].IsInRange(offset)) - { - return i; - } + m_next->QueueRequest(request); } - return s_fileNotFound; } - } // namespace IO -} // namespace AZ + else + { + m_usagePercentageStat.PushSample(1.0); + BlockCache& cache = *m_cachedFileCaches[index]; + cache.QueueRequest(request); +#if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO + m_overallHitRateStat.PushSample(cache.CalculateHitRatePercentage()); + m_overallCacheableRateStat.PushSample(cache.CalculateCacheableRatePercentage()); +#endif + } + } + + void DedicatedCache::FlushCache(const RequestPath& filePath) + { + size_t count = m_cachedFileNames.size(); + for (size_t i = 0; i < count; ++i) + { + if (m_cachedFileNames[i] == filePath) + { + // Flush the entire block cache as it's entirely dedicated to the found file. + m_cachedFileCaches[i]->FlushEntireCache(); + } + } + } + + void DedicatedCache::FlushEntireCache() + { + for (AZStd::unique_ptr& cache : m_cachedFileCaches) + { + cache->FlushEntireCache(); + } + } + + void DedicatedCache::CollectStatistics(AZStd::vector& statistics) const + { + statistics.push_back(Statistic::CreatePercentage(m_name, "Reads from dedicated cache", m_usagePercentageStat.GetAverage())); +#if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO + statistics.push_back(Statistic::CreatePercentage(m_name, "Overall cacheable rate", m_overallCacheableRateStat.GetAverage())); + statistics.push_back(Statistic::CreatePercentage(m_name, "Overall hit rate", m_overallHitRateStat.GetAverage())); +#endif + statistics.push_back(Statistic::CreateInteger(m_name, "Num dedicated caches", aznumeric_caster(m_cachedFileNames.size()))); + StreamStackEntry::CollectStatistics(statistics); + } + + void DedicatedCache::CreateDedicatedCache(FileRequest* request, FileRequest::CreateDedicatedCacheData& data) + { + size_t index = FindCache(data.m_path, data.m_range); + if (index == s_fileNotFound) + { + index = m_cachedFileCaches.size(); + m_cachedFileNames.push_back(data.m_path); + m_cachedFileRanges.push_back(data.m_range); + m_cachedFileCaches.push_back(AZStd::make_unique(m_cacheSize, m_blockSize, m_alignment, m_onlyEpilogWrites)); + m_cachedFileCaches[index]->SetNext(m_next); + m_cachedFileCaches[index]->SetContext(*m_context); + m_cachedFileRefCounts.push_back(1); + } + else + { + ++m_cachedFileRefCounts[index]; + } + request->SetStatus(IStreamerTypes::RequestStatus::Completed); + m_context->MarkRequestAsCompleted(request); + } + + void DedicatedCache::DestroyDedicatedCache(FileRequest* request, FileRequest::DestroyDedicatedCacheData& data) + { + size_t index = FindCache(data.m_path, data.m_range); + if (index != s_fileNotFound) + { + if (m_cachedFileRefCounts[index] > 0) + { + --m_cachedFileRefCounts[index]; + if (m_cachedFileRefCounts[index] == 0) + { + m_cachedFileNames.erase(m_cachedFileNames.begin() + index); + m_cachedFileRanges.erase(m_cachedFileRanges.begin() + index); + m_cachedFileCaches.erase(m_cachedFileCaches.begin() + index); + m_cachedFileRefCounts.erase(m_cachedFileRefCounts.begin() + index); + } + request->SetStatus(IStreamerTypes::RequestStatus::Completed); + m_context->MarkRequestAsCompleted(request); + return; + } + } + request->SetStatus(IStreamerTypes::RequestStatus::Failed); + m_context->MarkRequestAsCompleted(request); + } + + size_t DedicatedCache::FindCache(const RequestPath& filename, FileRange range) + { + size_t count = m_cachedFileNames.size(); + for (size_t i = 0; i < count; ++i) + { + if (m_cachedFileNames[i] == filename && m_cachedFileRanges[i] == range) + { + return i; + } + } + return s_fileNotFound; + } + + size_t DedicatedCache::FindCache(const RequestPath& filename, u64 offset) + { + size_t count = m_cachedFileNames.size(); + for (size_t i = 0; i < count; ++i) + { + if (m_cachedFileNames[i] == filename && m_cachedFileRanges[i].IsInRange(offset)) + { + return i; + } + } + return s_fileNotFound; + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/FileRange.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/FileRange.cpp index df4f77b722..3a568d3f47 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/FileRange.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/FileRange.cpp @@ -8,104 +8,101 @@ #include -namespace AZ +namespace AZ::IO { - namespace IO + FileRange FileRange::CreateRange(u64 offset, u64 size) { - FileRange FileRange::CreateRange(u64 offset, u64 size) - { - FileRange result; - result.m_hasOffsetEndSet = true; - result.m_isEntireFile = false; - result.m_offsetBegin = offset; - result.m_offsetEnd = offset + size; - return result; - } + FileRange result; + result.m_hasOffsetEndSet = true; + result.m_isEntireFile = false; + result.m_offsetBegin = offset; + result.m_offsetEnd = offset + size; + return result; + } - FileRange FileRange::CreateRangeForEntireFile() - { - FileRange result; - result.m_hasOffsetEndSet = false; - result.m_isEntireFile = true; - result.m_offsetBegin = 0; - result.m_offsetEnd = (static_cast(1) << 63) - 1; - return result; - } + FileRange FileRange::CreateRangeForEntireFile() + { + FileRange result; + result.m_hasOffsetEndSet = false; + result.m_isEntireFile = true; + result.m_offsetBegin = 0; + result.m_offsetEnd = (static_cast(1) << 63) - 1; + return result; + } - FileRange FileRange::CreateRangeForEntireFile(u64 fileSize) - { - FileRange result; - result.m_hasOffsetEndSet = true; - result.m_isEntireFile = true; - result.m_offsetBegin = 0; - result.m_offsetEnd = fileSize; - return result; - } + FileRange FileRange::CreateRangeForEntireFile(u64 fileSize) + { + FileRange result; + result.m_hasOffsetEndSet = true; + result.m_isEntireFile = true; + result.m_offsetBegin = 0; + result.m_offsetEnd = fileSize; + return result; + } - FileRange::FileRange() - : m_isEntireFile(false) - , m_offsetBegin(0) - , m_hasOffsetEndSet(false) - , m_offsetEnd(0) - { - } + FileRange::FileRange() + : m_isEntireFile(false) + , m_offsetBegin(0) + , m_hasOffsetEndSet(false) + , m_offsetEnd(0) + { + } - bool FileRange::operator==(const FileRange& rhs) const + bool FileRange::operator==(const FileRange& rhs) const + { + if (m_isEntireFile) { - if (m_isEntireFile) - { - return rhs.m_isEntireFile && m_offsetBegin == rhs.m_offsetBegin; - } - else - { - return m_offsetBegin == rhs.m_offsetBegin && m_offsetEnd == rhs.m_offsetEnd; - } + return rhs.m_isEntireFile && m_offsetBegin == rhs.m_offsetBegin; } + else + { + return m_offsetBegin == rhs.m_offsetBegin && m_offsetEnd == rhs.m_offsetEnd; + } + } - bool FileRange::operator!=(const FileRange& rhs) const + bool FileRange::operator!=(const FileRange& rhs) const + { + if (m_isEntireFile) { - if (m_isEntireFile) - { - return !rhs.m_isEntireFile || m_offsetBegin != rhs.m_offsetBegin; - } - else - { - return m_offsetBegin != rhs.m_offsetBegin || m_offsetEnd != rhs.m_offsetEnd; - } + return !rhs.m_isEntireFile || m_offsetBegin != rhs.m_offsetBegin; } + else + { + return m_offsetBegin != rhs.m_offsetBegin || m_offsetEnd != rhs.m_offsetEnd; + } + } - bool FileRange::IsEntireFile() const - { - return m_isEntireFile != 0; - } + bool FileRange::IsEntireFile() const + { + return m_isEntireFile != 0; + } - bool FileRange::IsSizeKnown() const - { - // m_hasOffsetEndSet being zero has the special meaning that the file size has not - // specifically been set yet. - return m_hasOffsetEndSet != 0; - } + bool FileRange::IsSizeKnown() const + { + // m_hasOffsetEndSet being zero has the special meaning that the file size has not + // specifically been set yet. + return m_hasOffsetEndSet != 0; + } - bool FileRange::IsInRange(u64 offset) const - { - return m_offsetBegin <= offset && offset < m_offsetEnd; - } + bool FileRange::IsInRange(u64 offset) const + { + return m_offsetBegin <= offset && offset < m_offsetEnd; + } - u64 FileRange::GetOffset() const - { - return m_offsetBegin; - } + u64 FileRange::GetOffset() const + { + return m_offsetBegin; + } - u64 FileRange::GetSize() const - { - AZ_Assert(m_hasOffsetEndSet, "Calling GetSize on a FileRange that doesn't have a size specified."); - return m_offsetEnd - m_offsetBegin; - } + u64 FileRange::GetSize() const + { + AZ_Assert(m_hasOffsetEndSet, "Calling GetSize on a FileRange that doesn't have a size specified."); + return m_offsetEnd - m_offsetBegin; + } - u64 FileRange::GetEndPoint() const - { - AZ_Assert(m_hasOffsetEndSet, "Calling GetEndPoint on a FileRange that doesn't have an end offset specified."); - return m_offsetEnd; - } - } // namespace IO -} // namesapce AZ + u64 FileRange::GetEndPoint() const + { + AZ_Assert(m_hasOffsetEndSet, "Calling GetEndPoint on a FileRange that doesn't have an end offset specified."); + return m_offsetEnd; + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/FileRequest.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/FileRequest.cpp index 7b9cde76d3..fc05b77b36 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/FileRequest.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/FileRequest.cpp @@ -12,469 +12,466 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + // + // Command structures. + // + + FileRequest::ExternalRequestData::ExternalRequestData(FileRequestPtr&& request) + : m_request(AZStd::move(request)) + {} + + FileRequest::RequestPathStoreData::RequestPathStoreData(RequestPath path) + : m_path(AZStd::move(path)) + {} + + FileRequest::ReadRequestData::ReadRequestData(RequestPath path, void* output, u64 outputSize, u64 offset, u64 size, + AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) + : m_path(AZStd::move(path)) + , m_allocator(nullptr) + , m_deadline(deadline) + , m_output(output) + , m_outputSize(outputSize) + , m_offset(offset) + , m_size(size) + , m_priority(priority) + , m_memoryType(IStreamerTypes::MemoryType::ReadWrite) // Only generic memory can be assigned externally. + {} + + FileRequest::ReadRequestData::ReadRequestData(RequestPath path, IStreamerTypes::RequestMemoryAllocator* allocator, + u64 offset, u64 size, AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) + : m_path(AZStd::move(path)) + , m_allocator(allocator) + , m_deadline(deadline) + , m_output(nullptr) + , m_outputSize(0) + , m_offset(offset) + , m_size(size) + , m_priority(priority) + , m_memoryType(IStreamerTypes::MemoryType::ReadWrite) // Only generic memory can be assigned externally. + {} + + FileRequest::ReadRequestData::~ReadRequestData() { - // - // Command structures. - // - - FileRequest::ExternalRequestData::ExternalRequestData(FileRequestPtr&& request) - : m_request(AZStd::move(request)) - {} - - FileRequest::RequestPathStoreData::RequestPathStoreData(RequestPath path) - : m_path(AZStd::move(path)) - {} - - FileRequest::ReadRequestData::ReadRequestData(RequestPath path, void* output, u64 outputSize, u64 offset, u64 size, - AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) - : m_path(AZStd::move(path)) - , m_allocator(nullptr) - , m_deadline(deadline) - , m_output(output) - , m_outputSize(outputSize) - , m_offset(offset) - , m_size(size) - , m_priority(priority) - , m_memoryType(IStreamerTypes::MemoryType::ReadWrite) // Only generic memory can be assigned externally. - {} - - FileRequest::ReadRequestData::ReadRequestData(RequestPath path, IStreamerTypes::RequestMemoryAllocator* allocator, - u64 offset, u64 size, AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) - : m_path(AZStd::move(path)) - , m_allocator(allocator) - , m_deadline(deadline) - , m_output(nullptr) - , m_outputSize(0) - , m_offset(offset) - , m_size(size) - , m_priority(priority) - , m_memoryType(IStreamerTypes::MemoryType::ReadWrite) // Only generic memory can be assigned externally. - {} - - FileRequest::ReadRequestData::~ReadRequestData() + if (m_allocator != nullptr) { - if (m_allocator != nullptr) + if (m_output != nullptr) { - if (m_output != nullptr) - { - m_allocator->Release(m_output); - } - m_allocator->UnlockAllocator(); + m_allocator->Release(m_output); } + m_allocator->UnlockAllocator(); } + } - FileRequest::ReadData::ReadData(void* output, u64 outputSize, const RequestPath& path, u64 offset, u64 size, bool sharedRead) - : m_output(output) - , m_outputSize(outputSize) - , m_path(path) - , m_offset(offset) - , m_size(size) - , m_sharedRead(sharedRead) - {} + FileRequest::ReadData::ReadData(void* output, u64 outputSize, const RequestPath& path, u64 offset, u64 size, bool sharedRead) + : m_output(output) + , m_outputSize(outputSize) + , m_path(path) + , m_offset(offset) + , m_size(size) + , m_sharedRead(sharedRead) + {} - FileRequest::CompressedReadData::CompressedReadData(CompressionInfo&& compressionInfo, void* output, u64 readOffset, u64 readSize) - : m_compressionInfo(AZStd::move(compressionInfo)) - , m_output(output) - , m_readOffset(readOffset) - , m_readSize(readSize) - {} + FileRequest::CompressedReadData::CompressedReadData(CompressionInfo&& compressionInfo, void* output, u64 readOffset, u64 readSize) + : m_compressionInfo(AZStd::move(compressionInfo)) + , m_output(output) + , m_readOffset(readOffset) + , m_readSize(readSize) + {} - FileRequest::FileExistsCheckData::FileExistsCheckData(const RequestPath& path) - : m_path(path) - {} + FileRequest::FileExistsCheckData::FileExistsCheckData(const RequestPath& path) + : m_path(path) + {} - FileRequest::FileMetaDataRetrievalData::FileMetaDataRetrievalData(const RequestPath& path) - : m_path(path) - {} + FileRequest::FileMetaDataRetrievalData::FileMetaDataRetrievalData(const RequestPath& path) + : m_path(path) + {} - FileRequest::CancelData::CancelData(FileRequestPtr target) - : m_target(AZStd::move(target)) - {} + FileRequest::CancelData::CancelData(FileRequestPtr target) + : m_target(AZStd::move(target)) + {} - FileRequest::FlushData::FlushData(RequestPath path) - : m_path(AZStd::move(path)) - {} + FileRequest::FlushData::FlushData(RequestPath path) + : m_path(AZStd::move(path)) + {} - FileRequest::RescheduleData::RescheduleData(FileRequestPtr target, AZStd::chrono::system_clock::time_point newDeadline, - IStreamerTypes::Priority newPriority) - : m_target(AZStd::move(target)) - , m_newDeadline(newDeadline) - , m_newPriority(newPriority) - {} + FileRequest::RescheduleData::RescheduleData(FileRequestPtr target, AZStd::chrono::system_clock::time_point newDeadline, + IStreamerTypes::Priority newPriority) + : m_target(AZStd::move(target)) + , m_newDeadline(newDeadline) + , m_newPriority(newPriority) + {} - FileRequest::CreateDedicatedCacheData::CreateDedicatedCacheData(RequestPath path, const FileRange& range) - : m_path(AZStd::move(path)) - , m_range(range) - {} + FileRequest::CreateDedicatedCacheData::CreateDedicatedCacheData(RequestPath path, const FileRange& range) + : m_path(AZStd::move(path)) + , m_range(range) + {} - FileRequest::DestroyDedicatedCacheData::DestroyDedicatedCacheData(RequestPath path, const FileRange& range) - : m_path(AZStd::move(path)) - , m_range(range) - {} + FileRequest::DestroyDedicatedCacheData::DestroyDedicatedCacheData(RequestPath path, const FileRange& range) + : m_path(AZStd::move(path)) + , m_range(range) + {} - FileRequest::ReportData::ReportData(ReportType reportType) - : m_reportType(reportType) - {} + FileRequest::ReportData::ReportData(ReportType reportType) + : m_reportType(reportType) + {} - FileRequest::CustomData::CustomData(AZStd::any data, bool failWhenUnhandled) - : m_data(AZStd::move(data)) - , m_failWhenUnhandled(failWhenUnhandled) - {} + FileRequest::CustomData::CustomData(AZStd::any data, bool failWhenUnhandled) + : m_data(AZStd::move(data)) + , m_failWhenUnhandled(failWhenUnhandled) + {} - // - // FileRequest - // + // + // FileRequest + // - FileRequest::FileRequest(Usage usage) - : m_usage(usage) + FileRequest::FileRequest(Usage usage) + : m_usage(usage) + { + Reset(); + } + + FileRequest::~FileRequest() + { + Reset(); + } + + void FileRequest::CreateRequestLink(FileRequestPtr&& request) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'RequestLink', but another task was already assigned."); + m_parent = request->m_request.m_parent; + request->m_request.m_parent = this; + m_dependencies++; + m_command.emplace(AZStd::move(request)); + } + + void FileRequest::CreateRequestPathStore(FileRequest* parent, RequestPath path) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'CreateRequestPathStore', but another task was already assigned."); + m_command.emplace(AZStd::move(path)); + SetOptionalParent(parent); + } + + void FileRequest::CreateReadRequest(RequestPath path, void* output, u64 outputSize, u64 offset, u64 size, + AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'ReadRequest', but another task was already assigned."); + m_command.emplace(AZStd::move(path), output, outputSize, offset, size, deadline, priority); + } + + void FileRequest::CreateReadRequest(RequestPath path, IStreamerTypes::RequestMemoryAllocator* allocator, u64 offset, u64 size, + AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'ReadRequest', but another task was already assigned."); + m_command.emplace(AZStd::move(path), allocator, offset, size, deadline, priority); + } + + void FileRequest::CreateRead(FileRequest* parent, void* output, u64 outputSize, const RequestPath& path, + u64 offset, u64 size, bool sharedRead) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Read', but another task was already assigned."); + m_command.emplace(output, outputSize, AZStd::move(path), offset, size, sharedRead); + SetOptionalParent(parent); + } + + void FileRequest::CreateCompressedRead(FileRequest* parent, const CompressionInfo& compressionInfo, + void* output, u64 readOffset, u64 readSize) + { + CreateCompressedRead(parent, CompressionInfo(compressionInfo), output, readOffset, readSize); + } + + void FileRequest::CreateCompressedRead(FileRequest* parent, CompressionInfo&& compressionInfo, + void* output, u64 readOffset, u64 readSize) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'CompressedRead', but another task was already assigned."); + m_command.emplace(AZStd::move(compressionInfo), output, readOffset, readSize); + SetOptionalParent(parent); + } + + void FileRequest::CreateWait(FileRequest* parent) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Wait', but another task was already assigned."); + m_command.emplace(); + SetOptionalParent(parent); + } + + void FileRequest::CreateFileExistsCheck(const RequestPath& path) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'FileExistsCheck', but another task was already assigned."); + m_command.emplace(path); + } + + void FileRequest::CreateFileMetaDataRetrieval(const RequestPath& path) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'FileMetaDataRetrieval', but another task was already assigned."); + m_command.emplace(path); + } + + void FileRequest::CreateCancel(FileRequestPtr target) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Cancel', but another task was already assigned."); + m_command.emplace(AZStd::move(target)); + } + + void FileRequest::CreateReschedule(FileRequestPtr target, AZStd::chrono::system_clock::time_point newDeadline, + IStreamerTypes::Priority newPriority) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Reschedule', but another task was already assigned."); + m_command.emplace(AZStd::move(target), newDeadline, newPriority); + } + + void FileRequest::CreateFlush(RequestPath path) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Flush', but another task was already assigned."); + m_command.emplace(AZStd::move(path)); + } + + void FileRequest::CreateFlushAll() + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'FlushAll', but another task was already assigned."); + m_command.emplace(); + } + + void FileRequest::CreateDedicatedCacheCreation(RequestPath path, const FileRange& range, FileRequest* parent) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'CreateDedicateCache', but another task was already assigned."); + m_command.emplace(AZStd::move(path), range); + SetOptionalParent(parent); + } + + void FileRequest::CreateDedicatedCacheDestruction(RequestPath path, const FileRange& range, FileRequest* parent) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'DestroyDedicateCache', but another task was already assigned."); + m_command.emplace(AZStd::move(path), range); + SetOptionalParent(parent); + } + + void FileRequest::CreateReport(ReportData::ReportType reportType) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Report', but another task was already assigned."); + m_command.emplace(reportType); + } + + void FileRequest::CreateCustom(AZStd::any data, bool failWhenUnhandled, FileRequest* parent) + { + AZ_Assert(AZStd::holds_alternative(m_command), + "Attempting to set FileRequest to 'Custom', but another task was already assigned."); + m_command.emplace(AZStd::move(data), failWhenUnhandled); + SetOptionalParent(parent); + } + + void FileRequest::SetCompletionCallback(OnCompletionCallback callback) + { + m_onCompletion = AZStd::move(callback); + } + + FileRequest::CommandVariant& FileRequest::GetCommand() + { + return m_command; + } + + const FileRequest::CommandVariant& FileRequest::GetCommand() const + { + return m_command; + } + + IStreamerTypes::RequestStatus FileRequest::GetStatus() const + { + return m_status; + } + + void FileRequest::SetStatus(IStreamerTypes::RequestStatus newStatus) + { + IStreamerTypes::RequestStatus currentStatus = m_status; + switch (newStatus) { - Reset(); - } - - FileRequest::~FileRequest() - { - Reset(); - } - - void FileRequest::CreateRequestLink(FileRequestPtr&& request) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'RequestLink', but another task was already assigned."); - m_parent = request->m_request.m_parent; - request->m_request.m_parent = this; - m_dependencies++; - m_command.emplace(AZStd::move(request)); - } - - void FileRequest::CreateRequestPathStore(FileRequest* parent, RequestPath path) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'CreateRequestPathStore', but another task was already assigned."); - m_command.emplace(AZStd::move(path)); - SetOptionalParent(parent); - } - - void FileRequest::CreateReadRequest(RequestPath path, void* output, u64 outputSize, u64 offset, u64 size, - AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'ReadRequest', but another task was already assigned."); - m_command.emplace(AZStd::move(path), output, outputSize, offset, size, deadline, priority); - } - - void FileRequest::CreateReadRequest(RequestPath path, IStreamerTypes::RequestMemoryAllocator* allocator, u64 offset, u64 size, - AZStd::chrono::system_clock::time_point deadline, IStreamerTypes::Priority priority) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'ReadRequest', but another task was already assigned."); - m_command.emplace(AZStd::move(path), allocator, offset, size, deadline, priority); - } - - void FileRequest::CreateRead(FileRequest* parent, void* output, u64 outputSize, const RequestPath& path, - u64 offset, u64 size, bool sharedRead) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Read', but another task was already assigned."); - m_command.emplace(output, outputSize, AZStd::move(path), offset, size, sharedRead); - SetOptionalParent(parent); - } - - void FileRequest::CreateCompressedRead(FileRequest* parent, const CompressionInfo& compressionInfo, - void* output, u64 readOffset, u64 readSize) - { - CreateCompressedRead(parent, CompressionInfo(compressionInfo), output, readOffset, readSize); - } - - void FileRequest::CreateCompressedRead(FileRequest* parent, CompressionInfo&& compressionInfo, - void* output, u64 readOffset, u64 readSize) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'CompressedRead', but another task was already assigned."); - m_command.emplace(AZStd::move(compressionInfo), output, readOffset, readSize); - SetOptionalParent(parent); - } - - void FileRequest::CreateWait(FileRequest* parent) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Wait', but another task was already assigned."); - m_command.emplace(); - SetOptionalParent(parent); - } - - void FileRequest::CreateFileExistsCheck(const RequestPath& path) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'FileExistsCheck', but another task was already assigned."); - m_command.emplace(path); - } - - void FileRequest::CreateFileMetaDataRetrieval(const RequestPath& path) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'FileMetaDataRetrieval', but another task was already assigned."); - m_command.emplace(path); - } - - void FileRequest::CreateCancel(FileRequestPtr target) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Cancel', but another task was already assigned."); - m_command.emplace(AZStd::move(target)); - } - - void FileRequest::CreateReschedule(FileRequestPtr target, AZStd::chrono::system_clock::time_point newDeadline, - IStreamerTypes::Priority newPriority) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Reschedule', but another task was already assigned."); - m_command.emplace(AZStd::move(target), newDeadline, newPriority); - } - - void FileRequest::CreateFlush(RequestPath path) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Flush', but another task was already assigned."); - m_command.emplace(AZStd::move(path)); - } - - void FileRequest::CreateFlushAll() - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'FlushAll', but another task was already assigned."); - m_command.emplace(); - } - - void FileRequest::CreateDedicatedCacheCreation(RequestPath path, const FileRange& range, FileRequest* parent) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'CreateDedicateCache', but another task was already assigned."); - m_command.emplace(AZStd::move(path), range); - SetOptionalParent(parent); - } - - void FileRequest::CreateDedicatedCacheDestruction(RequestPath path, const FileRange& range, FileRequest* parent) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'DestroyDedicateCache', but another task was already assigned."); - m_command.emplace(AZStd::move(path), range); - SetOptionalParent(parent); - } - - void FileRequest::CreateReport(ReportData::ReportType reportType) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Report', but another task was already assigned."); - m_command.emplace(reportType); - } - - void FileRequest::CreateCustom(AZStd::any data, bool failWhenUnhandled, FileRequest* parent) - { - AZ_Assert(AZStd::holds_alternative(m_command), - "Attempting to set FileRequest to 'Custom', but another task was already assigned."); - m_command.emplace(AZStd::move(data), failWhenUnhandled); - SetOptionalParent(parent); - } - - void FileRequest::SetCompletionCallback(OnCompletionCallback callback) - { - m_onCompletion = AZStd::move(callback); - } - - FileRequest::CommandVariant& FileRequest::GetCommand() - { - return m_command; - } - - const FileRequest::CommandVariant& FileRequest::GetCommand() const - { - return m_command; - } - - IStreamerTypes::RequestStatus FileRequest::GetStatus() const - { - return m_status; - } - - void FileRequest::SetStatus(IStreamerTypes::RequestStatus newStatus) - { - IStreamerTypes::RequestStatus currentStatus = m_status; - switch (newStatus) + case IStreamerTypes::RequestStatus::Pending: + [[fallthrough]]; + case IStreamerTypes::RequestStatus::Queued: + [[fallthrough]]; + case IStreamerTypes::RequestStatus::Processing: + if (currentStatus == IStreamerTypes::RequestStatus::Failed || + currentStatus == IStreamerTypes::RequestStatus::Canceled || + currentStatus == IStreamerTypes::RequestStatus::Completed) { - case IStreamerTypes::RequestStatus::Pending: - [[fallthrough]]; - case IStreamerTypes::RequestStatus::Queued: - [[fallthrough]]; - case IStreamerTypes::RequestStatus::Processing: - if (currentStatus == IStreamerTypes::RequestStatus::Failed || - currentStatus == IStreamerTypes::RequestStatus::Canceled || - currentStatus == IStreamerTypes::RequestStatus::Completed) - { - return; - } - break; - case IStreamerTypes::RequestStatus::Completed: - if (currentStatus == IStreamerTypes::RequestStatus::Failed || currentStatus == IStreamerTypes::RequestStatus::Canceled) - { - return; - } - break; - case IStreamerTypes::RequestStatus::Canceled: - if (currentStatus == IStreamerTypes::RequestStatus::Failed || currentStatus == IStreamerTypes::RequestStatus::Completed) - { - return; - } - break; - case IStreamerTypes::RequestStatus::Failed: - [[fallthrough]]; - default: - break; + return; } - m_status = newStatus; - } - - FileRequest* FileRequest::GetParent() - { - return m_parent; - } - - const FileRequest* FileRequest::GetParent() const - { - return m_parent; - } - - size_t FileRequest::GetNumDependencies() const - { - return m_dependencies; - } - - bool FileRequest::FailsWhenUnhandled() const - { - return AZStd::visit([](auto&& args) + break; + case IStreamerTypes::RequestStatus::Completed: + if (currentStatus == IStreamerTypes::RequestStatus::Failed || currentStatus == IStreamerTypes::RequestStatus::Canceled) { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) - { - AZ_Assert(false, - "Request does not contain a valid command. It may have been reset already or was never assigned a command."); - return true; - } - else if constexpr (AZStd::is_same_v) - { - return args.m_failWhenUnhandled; - } - else - { - return Command::s_failWhenUnhandled; - } - }, m_command); - } - - void FileRequest::Reset() - { - m_command = AZStd::monostate{}; - m_onCompletion = &OnCompletionPlaceholder; - m_estimatedCompletion = AZStd::chrono::system_clock::time_point(); - m_parent = nullptr; - m_status = IStreamerTypes::RequestStatus::Pending; - m_dependencies = 0; - } - - void FileRequest::SetOptionalParent(FileRequest* parent) - { - if (parent) - { - m_parent = parent; - AZ_Assert(parent->m_dependencies < std::numeric_limitsm_dependencies)>::max(), - "A file request dependency was added, but the parent can't have any more dependencies."); - ++parent->m_dependencies; + return; } - } - - bool FileRequest::WorksOn(FileRequestPtr& request) const - { - const FileRequest* current = this; - while (current) + break; + case IStreamerTypes::RequestStatus::Canceled: + if (currentStatus == IStreamerTypes::RequestStatus::Failed || currentStatus == IStreamerTypes::RequestStatus::Completed) { - auto* link = AZStd::get_if(¤t->m_command); - if (!link) - { - current = current->m_parent; - } - else - { - return link->m_request == request; - } + return; } - return false; + break; + case IStreamerTypes::RequestStatus::Failed: + [[fallthrough]]; + default: + break; } + m_status = newStatus; + } - size_t FileRequest::GetPendingId() const - { - return m_pendingId; - } + FileRequest* FileRequest::GetParent() + { + return m_parent; + } - void FileRequest::SetEstimatedCompletion(AZStd::chrono::system_clock::time_point time) + const FileRequest* FileRequest::GetParent() const + { + return m_parent; + } + + size_t FileRequest::GetNumDependencies() const + { + return m_dependencies; + } + + bool FileRequest::FailsWhenUnhandled() const + { + return AZStd::visit([](auto&& args) { - FileRequest* current = this; - do + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) + { + AZ_Assert(false, + "Request does not contain a valid command. It may have been reset already or was never assigned a command."); + return true; + } + else if constexpr (AZStd::is_same_v) + { + return args.m_failWhenUnhandled; + } + else + { + return Command::s_failWhenUnhandled; + } + }, m_command); + } + + void FileRequest::Reset() + { + m_command = AZStd::monostate{}; + m_onCompletion = &OnCompletionPlaceholder; + m_estimatedCompletion = AZStd::chrono::system_clock::time_point(); + m_parent = nullptr; + m_status = IStreamerTypes::RequestStatus::Pending; + m_dependencies = 0; + } + + void FileRequest::SetOptionalParent(FileRequest* parent) + { + if (parent) + { + m_parent = parent; + AZ_Assert(parent->m_dependencies < std::numeric_limitsm_dependencies)>::max(), + "A file request dependency was added, but the parent can't have any more dependencies."); + ++parent->m_dependencies; + } + } + + bool FileRequest::WorksOn(FileRequestPtr& request) const + { + const FileRequest* current = this; + while (current) + { + auto* link = AZStd::get_if(¤t->m_command); + if (!link) { - current->m_estimatedCompletion = time; current = current->m_parent; - } while (current); - } - - AZStd::chrono::system_clock::time_point FileRequest::GetEstimatedCompletion() const - { - return m_estimatedCompletion; - } - - // - // ExternalFileRequest - // - - ExternalFileRequest::ExternalFileRequest(StreamerContext* owner) - : m_request(FileRequest::Usage::External) - , m_owner(owner) - { - } - - void ExternalFileRequest::add_ref() - { - m_refCount++; - } - - void ExternalFileRequest::release() - { - if (--m_refCount == 0) + } + else { - AZ_Assert(m_owner, "No owning context set for the file request."); - m_owner->RecycleRequest(this); + return link->m_request == request; } } + return false; + } - bool operator==(const FileRequestHandle& lhs, const FileRequestPtr& rhs) - { - return lhs.m_request == &rhs->m_request; - } + size_t FileRequest::GetPendingId() const + { + return m_pendingId; + } - bool operator==(const FileRequestPtr& lhs, const FileRequestHandle& rhs) + void FileRequest::SetEstimatedCompletion(AZStd::chrono::system_clock::time_point time) + { + FileRequest* current = this; + do { - return rhs == lhs; - } + current->m_estimatedCompletion = time; + current = current->m_parent; + } while (current); + } - bool operator!=(const FileRequestHandle& lhs, const FileRequestPtr& rhs) - { - return !(lhs == rhs); - } + AZStd::chrono::system_clock::time_point FileRequest::GetEstimatedCompletion() const + { + return m_estimatedCompletion; + } - bool operator!=(const FileRequestPtr& lhs, const FileRequestHandle& rhs) + // + // ExternalFileRequest + // + + ExternalFileRequest::ExternalFileRequest(StreamerContext* owner) + : m_request(FileRequest::Usage::External) + , m_owner(owner) + { + } + + void ExternalFileRequest::add_ref() + { + m_refCount++; + } + + void ExternalFileRequest::release() + { + if (--m_refCount == 0) { - return !(rhs == lhs); + AZ_Assert(m_owner, "No owning context set for the file request."); + m_owner->RecycleRequest(this); } - } // namespace IO -} // namespace AZ + } + + bool operator==(const FileRequestHandle& lhs, const FileRequestPtr& rhs) + { + return lhs.m_request == &rhs->m_request; + } + + bool operator==(const FileRequestPtr& lhs, const FileRequestHandle& rhs) + { + return rhs == lhs; + } + + bool operator!=(const FileRequestHandle& lhs, const FileRequestPtr& rhs) + { + return !(lhs == rhs); + } + + bool operator!=(const FileRequestPtr& lhs, const FileRequestHandle& rhs) + { + return !(rhs == lhs); + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/FullFileDecompressor.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/FullFileDecompressor.cpp index 6427571f10..723a5d62c8 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/FullFileDecompressor.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/FullFileDecompressor.cpp @@ -21,719 +21,717 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + AZStd::shared_ptr FullFileDecompressorConfig::AddStreamStackEntry( + const HardwareInformation& hardware, AZStd::shared_ptr parent) { - AZStd::shared_ptr FullFileDecompressorConfig::AddStreamStackEntry( - const HardwareInformation& hardware, AZStd::shared_ptr parent) - { - auto stackEntry = AZStd::make_shared( - m_maxNumReads, m_maxNumJobs, aznumeric_caster(hardware.m_maxPhysicalSectorSize)); - stackEntry->SetNext(AZStd::move(parent)); - return stackEntry; - } + auto stackEntry = AZStd::make_shared( + m_maxNumReads, m_maxNumJobs, aznumeric_caster(hardware.m_maxPhysicalSectorSize)); + stackEntry->SetNext(AZStd::move(parent)); + return stackEntry; + } - void FullFileDecompressorConfig::Reflect(AZ::ReflectContext* context) + void FullFileDecompressorConfig::Reflect(AZ::ReflectContext* context) + { + if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) { - if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) - { - serializeContext->Class() - ->Version(1) - ->Field("MaxNumReads", &FullFileDecompressorConfig::m_maxNumReads) - ->Field("MaxNumJobs", &FullFileDecompressorConfig::m_maxNumJobs); - } + serializeContext->Class() + ->Version(1) + ->Field("MaxNumReads", &FullFileDecompressorConfig::m_maxNumReads) + ->Field("MaxNumJobs", &FullFileDecompressorConfig::m_maxNumJobs); } + } #if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - static constexpr char DecompBoundName[] = "Decompression bound"; - static constexpr char ReadBoundName[] = "Read bound"; + static constexpr char DecompBoundName[] = "Decompression bound"; + static constexpr char ReadBoundName[] = "Read bound"; #endif // AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - bool FullFileDecompressor::DecompressionInformation::IsProcessing() const + bool FullFileDecompressor::DecompressionInformation::IsProcessing() const + { + return !!m_compressedData; + } + + FullFileDecompressor::FullFileDecompressor(u32 maxNumReads, u32 maxNumJobs, u32 alignment) + : StreamStackEntry("Full file decompressor") + , m_maxNumReads(maxNumReads) + , m_maxNumJobs(maxNumJobs) + , m_alignment(alignment) + { + JobManagerDesc jobDesc; + jobDesc.m_jobManagerName = "Full File Decompressor"; + u32 numThreads = AZ::GetMin(maxNumJobs, AZStd::thread::hardware_concurrency()); + for (u32 i = 0; i < numThreads; ++i) { - return !!m_compressedData; + jobDesc.m_workerThreads.push_back(JobManagerThreadDesc()); } - - FullFileDecompressor::FullFileDecompressor(u32 maxNumReads, u32 maxNumJobs, u32 alignment) - : StreamStackEntry("Full file decompressor") - , m_maxNumReads(maxNumReads) - , m_maxNumJobs(maxNumJobs) - , m_alignment(alignment) + m_decompressionJobManager = AZStd::make_unique(jobDesc); + m_decompressionjobContext = AZStd::make_unique(*m_decompressionJobManager); + + m_processingJobs = AZStd::make_unique(maxNumJobs); + + m_readBuffers = AZStd::make_unique(maxNumReads); + m_readRequests = AZStd::make_unique(maxNumReads); + m_readBufferStatus = AZStd::make_unique(maxNumReads); + for (u32 i = 0; i < maxNumReads; ++i) { - JobManagerDesc jobDesc; - u32 numThreads = AZ::GetMin(maxNumJobs, AZStd::thread::hardware_concurrency()); - for (u32 i = 0; i < numThreads; ++i) - { - jobDesc.m_workerThreads.push_back(JobManagerThreadDesc()); - } - m_decompressionJobManager = AZStd::make_unique(jobDesc); - m_decompressionjobContext = AZStd::make_unique(*m_decompressionJobManager); - - m_processingJobs = AZStd::make_unique(maxNumJobs); - - m_readBuffers = AZStd::make_unique(maxNumReads); - m_readRequests = AZStd::make_unique(maxNumReads); - m_readBufferStatus = AZStd::make_unique(maxNumReads); - for (u32 i = 0; i < maxNumReads; ++i) - { - m_readBufferStatus[i] = ReadBufferStatus::Unused; - } - - // Add initial dummy values to the stats to avoid division by zero later on and avoid needing branches. - m_bytesDecompressed.PushEntry(1); - m_decompressionDurationMicroSec.PushEntry(1); + m_readBufferStatus[i] = ReadBufferStatus::Unused; } - void FullFileDecompressor::PrepareRequest(FileRequest* request) - { - AZ_Assert(request, "PrepareRequest was provided a null request."); + // Add initial dummy values to the stats to avoid division by zero later on and avoid needing branches. + m_bytesDecompressed.PushEntry(1); + m_decompressionDurationMicroSec.PushEntry(1); + } - AZStd::visit([this, request](auto&& args) + void FullFileDecompressor::PrepareRequest(FileRequest* request) + { + AZ_Assert(request, "PrepareRequest was provided a null request."); + + AZStd::visit([this, request](auto&& args) + { + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) - { - PrepareReadRequest(request, args); - } - else if constexpr (AZStd::is_same_v || - AZStd::is_same_v) - { - PrepareDedicatedCache(request, args.m_path); - } - else - { - StreamStackEntry::PrepareRequest(request); - } - }, request->GetCommand()); + PrepareReadRequest(request, args); + } + else if constexpr (AZStd::is_same_v || + AZStd::is_same_v) + { + PrepareDedicatedCache(request, args.m_path); + } + else + { + StreamStackEntry::PrepareRequest(request); + } + }, request->GetCommand()); + } + + void FullFileDecompressor::QueueRequest(FileRequest* request) + { + AZ_Assert(request, "QueueRequest was provided a null request."); + + AZStd::visit([this, request](auto&& args) + { + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) + { + m_pendingReads.push_back(request); + } + else if constexpr (AZStd::is_same_v) + { + m_pendingFileExistChecks.push_back(request); + } + else + { + StreamStackEntry::QueueRequest(request); + } + }, request->GetCommand()); + } + + bool FullFileDecompressor::ExecuteRequests() + { + bool result = false; + // First queue jobs as this might open up new read slots. + if (m_numInFlightReads > 0 && m_numRunningJobs < m_maxNumJobs) + { + result = StartDecompressions(); } - void FullFileDecompressor::QueueRequest(FileRequest* request) + // Queue as many new reads as possible. + while (!m_pendingReads.empty() && m_numInFlightReads < m_maxNumReads) { - AZ_Assert(request, "QueueRequest was provided a null request."); - - AZStd::visit([this, request](auto&& args) - { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) - { - m_pendingReads.push_back(request); - } - else if constexpr (AZStd::is_same_v) - { - m_pendingFileExistChecks.push_back(request); - } - else - { - StreamStackEntry::QueueRequest(request); - } - }, request->GetCommand()); + StartArchiveRead(m_pendingReads.front()); + m_pendingReads.pop_front(); + result = true; } - bool FullFileDecompressor::ExecuteRequests() + // If nothing else happened and there is at least one pending file exist check request, run one of those. + if (!result && !m_pendingFileExistChecks.empty()) { - bool result = false; - // First queue jobs as this might open up new read slots. - if (m_numInFlightReads > 0 && m_numRunningJobs < m_maxNumJobs) - { - result = StartDecompressions(); - } - - // Queue as many new reads as possible. - while (!m_pendingReads.empty() && m_numInFlightReads < m_maxNumReads) - { - StartArchiveRead(m_pendingReads.front()); - m_pendingReads.pop_front(); - result = true; - } - - // If nothing else happened and there is at least one pending file exist check request, run one of those. - if (!result && !m_pendingFileExistChecks.empty()) - { - FileExistsCheck(m_pendingFileExistChecks.front()); - m_pendingFileExistChecks.pop_front(); - result = true; - } + FileExistsCheck(m_pendingFileExistChecks.front()); + m_pendingFileExistChecks.pop_front(); + result = true; + } #if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - bool allPendingDecompression = true; - bool allReading = true; - for (u32 i = 0; i < m_maxNumReads; ++i) - { - allPendingDecompression = - allPendingDecompression && (m_readBufferStatus[i] == ReadBufferStatus::PendingDecompression); - allReading = - allReading && (m_readBufferStatus[i] == ReadBufferStatus::ReadInFlight); - } + bool allPendingDecompression = true; + bool allReading = true; + for (u32 i = 0; i < m_maxNumReads; ++i) + { + allPendingDecompression = + allPendingDecompression && (m_readBufferStatus[i] == ReadBufferStatus::PendingDecompression); + allReading = + allReading && (m_readBufferStatus[i] == ReadBufferStatus::ReadInFlight); + } - m_decompressionBoundStat.PushSample(allPendingDecompression ? 1.0 : 0.0); - Statistic::PlotImmediate(m_name, DecompBoundName, m_decompressionBoundStat.GetMostRecentSample()); + m_decompressionBoundStat.PushSample(allPendingDecompression ? 1.0 : 0.0); + Statistic::PlotImmediate(m_name, DecompBoundName, m_decompressionBoundStat.GetMostRecentSample()); - m_readBoundStat.PushSample(allReading && (m_numRunningJobs < m_maxNumJobs) ? 1.0 : 0.0); - Statistic::PlotImmediate(m_name, ReadBoundName, m_readBoundStat.GetMostRecentSample()); + m_readBoundStat.PushSample(allReading && (m_numRunningJobs < m_maxNumJobs) ? 1.0 : 0.0); + Statistic::PlotImmediate(m_name, ReadBoundName, m_readBoundStat.GetMostRecentSample()); #endif - return StreamStackEntry::ExecuteRequests() || result; - } + return StreamStackEntry::ExecuteRequests() || result; + } - void FullFileDecompressor::UpdateStatus(Status& status) const + void FullFileDecompressor::UpdateStatus(Status& status) const + { + StreamStackEntry::UpdateStatus(status); + s32 numAvailableSlots = aznumeric_cast(m_maxNumReads - m_numInFlightReads); + status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); + status.m_isIdle = status.m_isIdle && IsIdle(); + } + + void FullFileDecompressor::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, AZStd::vector& internalPending, + StreamerContext::PreparedQueue::iterator pendingBegin, StreamerContext::PreparedQueue::iterator pendingEnd) + { + // Create predictions for all pending requests. Some will be further processed after this. + AZStd::reverse_copy(m_pendingFileExistChecks.begin(), m_pendingFileExistChecks.end(), AZStd::back_inserter(internalPending)); + AZStd::reverse_copy(m_pendingReads.begin(), m_pendingReads.end(), AZStd::back_inserter(internalPending)); + + StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); + + double totalBytesDecompressed = aznumeric_caster(m_bytesDecompressed.GetTotal()); + double totalDecompressionDuration = aznumeric_caster(m_decompressionDurationMicroSec.GetTotal()); + AZStd::chrono::microseconds cumulativeDelay = AZStd::chrono::microseconds::max(); + + // Check the number of jobs that are processing. + for (u32 i = 0; i < m_maxNumJobs; ++i) { - StreamStackEntry::UpdateStatus(status); - s32 numAvailableSlots = aznumeric_cast(m_maxNumReads - m_numInFlightReads); - status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); - status.m_isIdle = status.m_isIdle && IsIdle(); - } - - void FullFileDecompressor::UpdateCompletionEstimates(AZStd::chrono::system_clock::time_point now, AZStd::vector& internalPending, - StreamerContext::PreparedQueue::iterator pendingBegin, StreamerContext::PreparedQueue::iterator pendingEnd) - { - // Create predictions for all pending requests. Some will be further processed after this. - AZStd::reverse_copy(m_pendingFileExistChecks.begin(), m_pendingFileExistChecks.end(), AZStd::back_inserter(internalPending)); - AZStd::reverse_copy(m_pendingReads.begin(), m_pendingReads.end(), AZStd::back_inserter(internalPending)); - - StreamStackEntry::UpdateCompletionEstimates(now, internalPending, pendingBegin, pendingEnd); - - double totalBytesDecompressed = aznumeric_caster(m_bytesDecompressed.GetTotal()); - double totalDecompressionDuration = aznumeric_caster(m_decompressionDurationMicroSec.GetTotal()); - AZStd::chrono::microseconds cumulativeDelay = AZStd::chrono::microseconds::max(); - - // Check the number of jobs that are processing. - for (u32 i = 0; i < m_maxNumJobs; ++i) + if (m_processingJobs[i].IsProcessing()) { - if (m_processingJobs[i].IsProcessing()) - { - FileRequest* compressedRequest = m_processingJobs[i].m_waitRequest->GetParent(); - AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); - auto data = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(data, "Compressed request in the decompression queue in FullFileDecompressor didn't contain compression read data."); - - size_t bytesToDecompress = data->m_compressionInfo.m_compressedSize; - auto decompressionDuration = AZStd::chrono::microseconds( - aznumeric_cast((bytesToDecompress * totalDecompressionDuration) / totalBytesDecompressed)); - auto timeInProcessing = now - m_processingJobs[i].m_jobStartTime; - auto timeLeft = decompressionDuration > timeInProcessing ? decompressionDuration - timeInProcessing : AZStd::chrono::microseconds(0); - // Get the shortest time as this indicates the next decompression to become available. - cumulativeDelay = AZStd::min(timeLeft, cumulativeDelay); - m_processingJobs[i].m_waitRequest->SetEstimatedCompletion(now + timeLeft); - } - } - if (cumulativeDelay == AZStd::chrono::microseconds::max()) - { - cumulativeDelay = AZStd::chrono::microseconds(0); - } - - // Next update all reads that are in flight. These will have an estimation for the read to complete, but will then be queued - // for decompression, so add the time needed decompression. Assume that decompression happens in parallel. - AZStd::chrono::microseconds decompressionDelay = - AZStd::chrono::microseconds(aznumeric_cast(m_decompressionJobDelayMicroSec.CalculateAverage())); - AZStd::chrono::microseconds smallestDecompressionDuration = AZStd::chrono::microseconds::max(); - for (u32 i = 0; i < m_maxNumReads; ++i) - { - AZStd::chrono::system_clock::time_point baseTime; - switch (m_readBufferStatus[i]) - { - case ReadBufferStatus::Unused: - continue; - case ReadBufferStatus::ReadInFlight: - // Internal read requests can start and complete but pending finalization before they're ever scheduled in which case - // the estimated time is not set. - baseTime = m_readRequests[i]->GetEstimatedCompletion(); - if (baseTime == AZStd::chrono::system_clock::time_point()) - { - baseTime = now; - } - break; - case ReadBufferStatus::PendingDecompression: - baseTime = now; - break; - default: - AZ_Assert(false, "Unsupported buffer type: %i.", m_readBufferStatus[i]); - continue; - } - - baseTime += cumulativeDelay; // Delay until the first decompression slot becomes available. - baseTime += decompressionDelay; // The average time it takes for the job system to pick up the decompression job. - - // Calculate the amount of time it will take to decompress the data. - FileRequest* compressedRequest = m_readRequests[i]->GetParent(); + FileRequest* compressedRequest = m_processingJobs[i].m_waitRequest->GetParent(); + AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); auto data = AZStd::get_if(&compressedRequest->GetCommand()); - + AZ_Assert(data, "Compressed request in the decompression queue in FullFileDecompressor didn't contain compression read data."); + size_t bytesToDecompress = data->m_compressionInfo.m_compressedSize; auto decompressionDuration = AZStd::chrono::microseconds( aznumeric_cast((bytesToDecompress * totalDecompressionDuration) / totalBytesDecompressed)); - smallestDecompressionDuration = AZStd::min(smallestDecompressionDuration, decompressionDuration); - baseTime += decompressionDuration; - - m_readRequests[i]->SetEstimatedCompletion(baseTime); - } - if (smallestDecompressionDuration != AZStd::chrono::microseconds::max()) - { - cumulativeDelay += smallestDecompressionDuration; // Time after which the decompression jobs and pending reads have completed. - } - - // For all internally pending compressed reads add the decompression time. The read time will have already been added downstream. - // Because this call will go from the top of the stack to the bottom, but estimation is calculated from the bottom to the top, this - // list should be processed in reverse order. - for (auto pendingIt = internalPending.rbegin(); pendingIt != internalPending.rend(); ++pendingIt) - { - EstimateCompressedReadRequest(*pendingIt, cumulativeDelay, decompressionDelay, - totalDecompressionDuration, totalBytesDecompressed); - } - - // Finally add a prediction for all the requests that are waiting to be queued. - for (auto requestIt = pendingBegin; requestIt != pendingEnd; ++requestIt) - { - EstimateCompressedReadRequest(*requestIt, cumulativeDelay, decompressionDelay, - totalDecompressionDuration, totalBytesDecompressed); + auto timeInProcessing = now - m_processingJobs[i].m_jobStartTime; + auto timeLeft = decompressionDuration > timeInProcessing ? decompressionDuration - timeInProcessing : AZStd::chrono::microseconds(0); + // Get the shortest time as this indicates the next decompression to become available. + cumulativeDelay = AZStd::min(timeLeft, cumulativeDelay); + m_processingJobs[i].m_waitRequest->SetEstimatedCompletion(now + timeLeft); } } - - void FullFileDecompressor::EstimateCompressedReadRequest(FileRequest* request, AZStd::chrono::microseconds& cumulativeDelay, - AZStd::chrono::microseconds decompressionDelay, double totalDecompressionDurationUs, double totalBytesDecompressed) const + if (cumulativeDelay == AZStd::chrono::microseconds::max()) { - auto data = AZStd::get_if(&request->GetCommand()); - if (data) - { - AZStd::chrono::microseconds processingTime = decompressionDelay; - size_t bytesToDecompress = data->m_compressionInfo.m_compressedSize; - processingTime += AZStd::chrono::microseconds( - aznumeric_cast((bytesToDecompress * totalDecompressionDurationUs) / totalBytesDecompressed)); - - cumulativeDelay += processingTime; - request->SetEstimatedCompletion(request->GetEstimatedCompletion() + processingTime); - } + cumulativeDelay = AZStd::chrono::microseconds(0); } - void FullFileDecompressor::CollectStatistics(AZStd::vector& statistics) const + // Next update all reads that are in flight. These will have an estimation for the read to complete, but will then be queued + // for decompression, so add the time needed decompression. Assume that decompression happens in parallel. + AZStd::chrono::microseconds decompressionDelay = + AZStd::chrono::microseconds(aznumeric_cast(m_decompressionJobDelayMicroSec.CalculateAverage())); + AZStd::chrono::microseconds smallestDecompressionDuration = AZStd::chrono::microseconds::max(); + for (u32 i = 0; i < m_maxNumReads; ++i) { - constexpr double bytesToMB = 1.0 / (1024.0 * 1024.0); - constexpr double usToSec = 1.0 / (1000.0 * 1000.0); - constexpr double usToMs = 1.0 / 1000.0; - - if (m_bytesDecompressed.GetNumRecorded() > 1) // There's always a default added. + AZStd::chrono::system_clock::time_point baseTime; + switch (m_readBufferStatus[i]) { - //It only makes sense to add decompression statistics when reading from PAK files. - statistics.push_back(Statistic::CreateInteger(m_name, "Available decompression slots", m_maxNumJobs - m_numRunningJobs)); - statistics.push_back(Statistic::CreateInteger(m_name, "Available read slots", m_maxNumReads - m_numInFlightReads)); - statistics.push_back(Statistic::CreateInteger(m_name, "Pending decompression", m_numPendingDecompression)); - statistics.push_back(Statistic::CreateFloat(m_name, "Buffer memory (MB)", m_memoryUsage * bytesToMB)); + case ReadBufferStatus::Unused: + continue; + case ReadBufferStatus::ReadInFlight: + // Internal read requests can start and complete but pending finalization before they're ever scheduled in which case + // the estimated time is not set. + baseTime = m_readRequests[i]->GetEstimatedCompletion(); + if (baseTime == AZStd::chrono::system_clock::time_point()) + { + baseTime = now; + } + break; + case ReadBufferStatus::PendingDecompression: + baseTime = now; + break; + default: + AZ_Assert(false, "Unsupported buffer type: %i.", m_readBufferStatus[i]); + continue; + } - double averageJobStartDelay = m_decompressionJobDelayMicroSec.CalculateAverage() * usToMs; - statistics.push_back(Statistic::CreateFloat(m_name, "Decompression job delay (avg. ms)", averageJobStartDelay)); + baseTime += cumulativeDelay; // Delay until the first decompression slot becomes available. + baseTime += decompressionDelay; // The average time it takes for the job system to pick up the decompression job. - double totalBytesDecompressedMB = m_bytesDecompressed.GetTotal() * bytesToMB; - double totalDecompressionTimeSec = m_decompressionDurationMicroSec.GetTotal() * usToSec; - statistics.push_back(Statistic::CreateFloat(m_name, "Decompression Speed per job (avg. mbps)", totalBytesDecompressedMB / totalDecompressionTimeSec)); + // Calculate the amount of time it will take to decompress the data. + FileRequest* compressedRequest = m_readRequests[i]->GetParent(); + auto data = AZStd::get_if(&compressedRequest->GetCommand()); + + size_t bytesToDecompress = data->m_compressionInfo.m_compressedSize; + auto decompressionDuration = AZStd::chrono::microseconds( + aznumeric_cast((bytesToDecompress * totalDecompressionDuration) / totalBytesDecompressed)); + smallestDecompressionDuration = AZStd::min(smallestDecompressionDuration, decompressionDuration); + baseTime += decompressionDuration; + + m_readRequests[i]->SetEstimatedCompletion(baseTime); + } + if (smallestDecompressionDuration != AZStd::chrono::microseconds::max()) + { + cumulativeDelay += smallestDecompressionDuration; // Time after which the decompression jobs and pending reads have completed. + } + + // For all internally pending compressed reads add the decompression time. The read time will have already been added downstream. + // Because this call will go from the top of the stack to the bottom, but estimation is calculated from the bottom to the top, this + // list should be processed in reverse order. + for (auto pendingIt = internalPending.rbegin(); pendingIt != internalPending.rend(); ++pendingIt) + { + EstimateCompressedReadRequest(*pendingIt, cumulativeDelay, decompressionDelay, + totalDecompressionDuration, totalBytesDecompressed); + } + + // Finally add a prediction for all the requests that are waiting to be queued. + for (auto requestIt = pendingBegin; requestIt != pendingEnd; ++requestIt) + { + EstimateCompressedReadRequest(*requestIt, cumulativeDelay, decompressionDelay, + totalDecompressionDuration, totalBytesDecompressed); + } + } + + void FullFileDecompressor::EstimateCompressedReadRequest(FileRequest* request, AZStd::chrono::microseconds& cumulativeDelay, + AZStd::chrono::microseconds decompressionDelay, double totalDecompressionDurationUs, double totalBytesDecompressed) const + { + auto data = AZStd::get_if(&request->GetCommand()); + if (data) + { + AZStd::chrono::microseconds processingTime = decompressionDelay; + size_t bytesToDecompress = data->m_compressionInfo.m_compressedSize; + processingTime += AZStd::chrono::microseconds( + aznumeric_cast((bytesToDecompress * totalDecompressionDurationUs) / totalBytesDecompressed)); + + cumulativeDelay += processingTime; + request->SetEstimatedCompletion(request->GetEstimatedCompletion() + processingTime); + } + } + + void FullFileDecompressor::CollectStatistics(AZStd::vector& statistics) const + { + constexpr double bytesToMB = 1.0 / (1024.0 * 1024.0); + constexpr double usToSec = 1.0 / (1000.0 * 1000.0); + constexpr double usToMs = 1.0 / 1000.0; + + if (m_bytesDecompressed.GetNumRecorded() > 1) // There's always a default added. + { + //It only makes sense to add decompression statistics when reading from PAK files. + statistics.push_back(Statistic::CreateInteger(m_name, "Available decompression slots", m_maxNumJobs - m_numRunningJobs)); + statistics.push_back(Statistic::CreateInteger(m_name, "Available read slots", m_maxNumReads - m_numInFlightReads)); + statistics.push_back(Statistic::CreateInteger(m_name, "Pending decompression", m_numPendingDecompression)); + statistics.push_back(Statistic::CreateFloat(m_name, "Buffer memory (MB)", m_memoryUsage * bytesToMB)); + + double averageJobStartDelay = m_decompressionJobDelayMicroSec.CalculateAverage() * usToMs; + statistics.push_back(Statistic::CreateFloat(m_name, "Decompression job delay (avg. ms)", averageJobStartDelay)); + + double totalBytesDecompressedMB = m_bytesDecompressed.GetTotal() * bytesToMB; + double totalDecompressionTimeSec = m_decompressionDurationMicroSec.GetTotal() * usToSec; + statistics.push_back(Statistic::CreateFloat(m_name, "Decompression Speed per job (avg. mbps)", totalBytesDecompressedMB / totalDecompressionTimeSec)); #if AZ_STREAMER_ADD_EXTRA_PROFILING_INFO - statistics.push_back(Statistic::CreatePercentage(m_name, DecompBoundName, m_decompressionBoundStat.GetAverage())); - statistics.push_back(Statistic::CreatePercentage(m_name, ReadBoundName, m_readBoundStat.GetAverage())); + statistics.push_back(Statistic::CreatePercentage(m_name, DecompBoundName, m_decompressionBoundStat.GetAverage())); + statistics.push_back(Statistic::CreatePercentage(m_name, ReadBoundName, m_readBoundStat.GetAverage())); #endif - } - - StreamStackEntry::CollectStatistics(statistics); } - bool FullFileDecompressor::IsIdle() const - { - return - m_pendingReads.empty() && - m_pendingFileExistChecks.empty() && - m_numInFlightReads == 0 && - m_numPendingDecompression == 0 && - m_numRunningJobs == 0; - } + StreamStackEntry::CollectStatistics(statistics); + } - void FullFileDecompressor::PrepareReadRequest(FileRequest* request, FileRequest::ReadRequestData& data) + bool FullFileDecompressor::IsIdle() const + { + return + m_pendingReads.empty() && + m_pendingFileExistChecks.empty() && + m_numInFlightReads == 0 && + m_numPendingDecompression == 0 && + m_numRunningJobs == 0; + } + + void FullFileDecompressor::PrepareReadRequest(FileRequest* request, FileRequest::ReadRequestData& data) + { + CompressionInfo info; + if (CompressionUtils::FindCompressionInfo(info, data.m_path.GetRelativePath())) { - CompressionInfo info; - if (CompressionUtils::FindCompressionInfo(info, data.m_path.GetRelativePath())) + FileRequest* nextRequest = m_context->GetNewInternalRequest(); + if (info.m_isCompressed) { - FileRequest* nextRequest = m_context->GetNewInternalRequest(); - if (info.m_isCompressed) - { - AZ_Assert(info.m_decompressor, - "FullFileDecompressor::PrepareRequest found a compressed file, but no decompressor to decompress with."); - nextRequest->CreateCompressedRead(request, AZStd::move(info), data.m_output, data.m_offset, data.m_size); - } - else - { - FileRequest* pathStorageRequest = m_context->GetNewInternalRequest(); - pathStorageRequest->CreateRequestPathStore(request, AZStd::move(info.m_archiveFilename)); - auto& pathStorage = AZStd::get(pathStorageRequest->GetCommand()); - - nextRequest->CreateRead(pathStorageRequest, data.m_output, data.m_outputSize, pathStorage.m_path, - info.m_offset + data.m_offset, data.m_size, info.m_isSharedPak); - } - - if (info.m_conflictResolution == ConflictResolution::PreferFile) - { - auto callback = [this, nextRequest](const FileRequest& checkRequest) - { - AZ_PROFILE_FUNCTION(AzCore); - auto check = AZStd::get_if(&checkRequest.GetCommand()); - AZ_Assert(check, - "Callback in FullFileDecompressor::PrepareReadRequest expected FileExistsCheck but got another command."); - if (check->m_found) - { - FileRequest* originalRequest = m_context->RejectRequest(nextRequest); - if (AZStd::holds_alternative(originalRequest->GetCommand())) - { - originalRequest = m_context->RejectRequest(originalRequest); - } - StreamStackEntry::PrepareRequest(originalRequest); - } - else - { - m_context->PushPreparedRequest(nextRequest); - } - }; - FileRequest* fileCheckRequest = m_context->GetNewInternalRequest(); - fileCheckRequest->CreateFileExistsCheck(data.m_path); - fileCheckRequest->SetCompletionCallback(AZStd::move(callback)); - StreamStackEntry::QueueRequest(fileCheckRequest); - } - else - { - m_context->PushPreparedRequest(nextRequest); - } + AZ_Assert(info.m_decompressor, + "FullFileDecompressor::PrepareRequest found a compressed file, but no decompressor to decompress with."); + nextRequest->CreateCompressedRead(request, AZStd::move(info), data.m_output, data.m_offset, data.m_size); } else { - StreamStackEntry::PrepareRequest(request); - } - } + FileRequest* pathStorageRequest = m_context->GetNewInternalRequest(); + pathStorageRequest->CreateRequestPathStore(request, AZStd::move(info.m_archiveFilename)); + auto& pathStorage = AZStd::get(pathStorageRequest->GetCommand()); - void FullFileDecompressor::PrepareDedicatedCache(FileRequest* request, const RequestPath& path) - { - CompressionInfo info; - if (CompressionUtils::FindCompressionInfo(info, path.GetRelativePath())) + nextRequest->CreateRead(pathStorageRequest, data.m_output, data.m_outputSize, pathStorage.m_path, + info.m_offset + data.m_offset, data.m_size, info.m_isSharedPak); + } + + if (info.m_conflictResolution == ConflictResolution::PreferFile) { - FileRequest* nextRequest = m_context->GetNewInternalRequest(); - AZStd::visit([request, &info, nextRequest](auto&& args) + auto callback = [this, nextRequest](const FileRequest& checkRequest) { - using Command = AZStd::decay_t; - if constexpr (AZStd::is_same_v) + AZ_PROFILE_FUNCTION(AzCore); + auto check = AZStd::get_if(&checkRequest.GetCommand()); + AZ_Assert(check, + "Callback in FullFileDecompressor::PrepareReadRequest expected FileExistsCheck but got another command."); + if (check->m_found) { - nextRequest->CreateDedicatedCacheCreation(AZStd::move(info.m_archiveFilename), - FileRange::CreateRange(info.m_offset, info.m_compressedSize), request); - } - else if constexpr (AZStd::is_same_v) - { - nextRequest->CreateDedicatedCacheDestruction(AZStd::move(info.m_archiveFilename), - FileRange::CreateRange(info.m_offset, info.m_compressedSize), request); - } - }, request->GetCommand()); - - if (info.m_conflictResolution == ConflictResolution::PreferFile) - { - auto callback = [this, nextRequest](const FileRequest& checkRequest) - { - AZ_PROFILE_FUNCTION(AzCore); - auto check = AZStd::get_if(&checkRequest.GetCommand()); - AZ_Assert(check, - "Callback in FullFileDecompressor::PrepareDedicatedCache expected FileExistsCheck but got another command."); - if (check->m_found) + FileRequest* originalRequest = m_context->RejectRequest(nextRequest); + if (AZStd::holds_alternative(originalRequest->GetCommand())) { - FileRequest* originalRequest = nextRequest->GetParent(); - m_context->RejectRequest(nextRequest); - StreamStackEntry::PrepareRequest(originalRequest); + originalRequest = m_context->RejectRequest(originalRequest); } - else - { - m_context->PushPreparedRequest(nextRequest); - } - }; - FileRequest* fileCheckRequest = m_context->GetNewInternalRequest(); - fileCheckRequest->CreateFileExistsCheck(path); - fileCheckRequest->SetCompletionCallback(AZStd::move(callback)); - StreamStackEntry::QueueRequest(fileCheckRequest); - } - else - { - m_context->PushPreparedRequest(nextRequest); - } - } - else - { - StreamStackEntry::PrepareRequest(request); - } - } - - void FullFileDecompressor::FileExistsCheck(FileRequest* checkRequest) - { - auto& fileCheckRequest = AZStd::get(checkRequest->GetCommand()); - CompressionInfo info; - if (CompressionUtils::FindCompressionInfo(info, fileCheckRequest.m_path.GetRelativePath())) - { - fileCheckRequest.m_found = true; - } - else - { - // The file isn't in the archive but might still exist as a loose file, so let the next node have a shot. - StreamStackEntry::QueueRequest(checkRequest); - } - } - - void FullFileDecompressor::StartArchiveRead(FileRequest* compressedReadRequest) - { - if (!m_next) - { - compressedReadRequest->SetStatus(IStreamerTypes::RequestStatus::Failed); - m_context->MarkRequestAsCompleted(compressedReadRequest); - return; - } - - for (u32 i = 0; i < m_maxNumReads; ++i) - { - if (m_readBufferStatus[i] == ReadBufferStatus::Unused) - { - auto data = AZStd::get_if(&compressedReadRequest->GetCommand()); - AZ_Assert(data, "Compressed request that's starting a read in FullFileDecompressor didn't contain compression read data."); - AZ_Assert(data->m_compressionInfo.m_decompressor, - "FileRequest for FullFileDecompressor is missing a decompression callback."); - - CompressionInfo& info = data->m_compressionInfo; - AZ_Assert(info.m_decompressor, "FullFileDecompressor is planning to a queue a request for reading but couldn't find a decompressor."); - - // The buffer is aligned down but the offset is not corrected. If the offset was adjusted it would mean the same data is read - // multiple times and negates the block cache's ability to detect these cases. By still adjusting it means that the reads between - // the BlockCache's prolog and epilog are read into aligned buffers. - size_t offsetAdjustment = info.m_offset - AZ_SIZE_ALIGN_DOWN(info.m_offset, aznumeric_cast(m_alignment)); - size_t bufferSize = AZ_SIZE_ALIGN_UP((info.m_compressedSize + offsetAdjustment), aznumeric_cast(m_alignment)); - m_readBuffers[i] = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( - bufferSize, m_alignment, 0, "AZ::IO::Streamer FullFileDecompressor", __FILE__, __LINE__)); - m_memoryUsage += bufferSize; - - FileRequest* archiveReadRequest = m_context->GetNewInternalRequest(); - archiveReadRequest->CreateRead(compressedReadRequest, m_readBuffers[i] + offsetAdjustment, bufferSize, info.m_archiveFilename, - info.m_offset, info.m_compressedSize, info.m_isSharedPak); - archiveReadRequest->SetCompletionCallback( - [this, readSlot = i](FileRequest& request) - { - AZ_PROFILE_FUNCTION(AzCore); - FinishArchiveRead(&request, readSlot); - }); - m_next->QueueRequest(archiveReadRequest); - - m_readRequests[i] = archiveReadRequest; - m_readBufferStatus[i] = ReadBufferStatus::ReadInFlight; - - AZ_Assert(m_numInFlightReads < m_maxNumReads, - "A FileRequest was queued for reading in FullFileDecompressor, but there's no slots available."); - m_numInFlightReads++; - - return; - } - } - AZ_Assert(false, "%u of %u read slots are use in the FullFileDecompressor, but no empty slot was found.", m_numInFlightReads, m_maxNumReads); - } - - void FullFileDecompressor::FinishArchiveRead(FileRequest* readRequest, u32 readSlot) - { - AZ_Assert(m_readRequests[readSlot] == readRequest, - "Request in the archive read slot isn't the same as request that's being completed."); - - FileRequest* compressedRequest = readRequest->GetParent(); - AZ_Assert(compressedRequest, "Read requests started by FullFileDecompressor is missing a parent request."); - - if (readRequest->GetStatus() == IStreamerTypes::RequestStatus::Completed) - { - m_readBufferStatus[readSlot] = ReadBufferStatus::PendingDecompression; - ++m_numPendingDecompression; - - // Add this wait so the compressed request isn't fully completed yet as only the read part is done. The - // job thread will finish this wait, which in turn will trigger this function again on the main streaming thread. - FileRequest* waitRequest = m_context->GetNewInternalRequest(); - waitRequest->CreateWait(compressedRequest); - m_readRequests[readSlot] = waitRequest; - } - else - { - auto data = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(data, "Compressed request in FullFileDecompressor that finished unsuccessfully didn't contain compression read data."); - CompressionInfo& info = data->m_compressionInfo; - size_t offsetAdjustment = info.m_offset - AZ_SIZE_ALIGN_DOWN(info.m_offset, aznumeric_cast(m_alignment)); - size_t bufferSize = AZ_SIZE_ALIGN_UP((info.m_compressedSize + offsetAdjustment), aznumeric_cast(m_alignment)); - m_memoryUsage -= bufferSize; - - if (m_readBuffers[readSlot] != nullptr) - { - AZ::AllocatorInstance::Get().DeAllocate(m_readBuffers[readSlot], bufferSize, m_alignment); - m_readBuffers[readSlot] = nullptr; - } - m_readRequests[readSlot] = nullptr; - m_readBufferStatus[readSlot] = ReadBufferStatus::Unused; - AZ_Assert(m_numInFlightReads > 0, - "Trying to decrement a read request after it was canceled or failed in FullFileDecompressor, " - "but no read requests are supposed to be queued."); - m_numInFlightReads--; - } - } - - bool FullFileDecompressor::StartDecompressions() - { - bool queuedJobs = false; - u32 jobSlot = 0; - for (u32 readSlot = 0; readSlot < m_maxNumReads; ++readSlot) - { - // Find completed read. - if (m_readBufferStatus[readSlot] != ReadBufferStatus::PendingDecompression) - { - continue; - } - - // Find decompression slot - for (; jobSlot < m_maxNumJobs; ++jobSlot) - { - if (m_processingJobs[jobSlot].IsProcessing()) - { - continue; - } - - FileRequest* waitRequest = m_readRequests[readSlot]; - AZ_Assert(AZStd::holds_alternative(waitRequest->GetCommand()), - "File request waiting for decompression wasn't marked as being a wait operation."); - FileRequest* compressedRequest = waitRequest->GetParent(); - AZ_Assert(compressedRequest, "Read requests started by FullFileDecompressor is missing a parent request."); - - waitRequest->SetCompletionCallback([this, jobSlot](FileRequest& request) - { - AZ_PROFILE_FUNCTION(AzCore); - FinishDecompression(&request, jobSlot); - }); - - DecompressionInformation& info = m_processingJobs[jobSlot]; - info.m_waitRequest = waitRequest; - info.m_queueStartTime = AZStd::chrono::high_resolution_clock::now(); - info.m_jobStartTime = info.m_queueStartTime; // Set these to the same in case the scheduler requests an update before the job has started. - info.m_compressedData = m_readBuffers[readSlot]; // Transfer ownership of the pointer. - m_readBuffers[readSlot] = nullptr; - - AZ::Job* decompressionJob; - auto data = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(data, "Compressed request in FullFileDecompressor that's starting decompression didn't contain compression read data."); - AZ_Assert(data->m_compressionInfo.m_decompressor, "FullFileDecompressor is queuing a decompression job but couldn't find a decompressor."); - - info.m_alignmentOffset = aznumeric_caster(data->m_compressionInfo.m_offset - - AZ_SIZE_ALIGN_DOWN(data->m_compressionInfo.m_offset, aznumeric_cast(m_alignment))); - - if (data->m_readOffset == 0 && data->m_readSize == data->m_compressionInfo.m_uncompressedSize) - { - auto job = [this, &info]() - { - FullDecompression(m_context, info); - }; - decompressionJob = AZ::CreateJobFunction(job, true, m_decompressionjobContext.get()); + StreamStackEntry::PrepareRequest(originalRequest); } else { - m_memoryUsage += data->m_compressionInfo.m_uncompressedSize; - auto job = [this, &info]() - { - PartialDecompression(m_context, info); - }; - decompressionJob = AZ::CreateJobFunction(job, true, m_decompressionjobContext.get()); + m_context->PushPreparedRequest(nextRequest); } - --m_numPendingDecompression; - ++m_numRunningJobs; - decompressionJob->Start(); - - m_readRequests[readSlot] = nullptr; - m_readBufferStatus[readSlot] = ReadBufferStatus::Unused; - AZ_Assert(m_numInFlightReads > 0, "Trying to decrement a read request after it's queued for decompression in FullFileDecompressor, but no read requests are supposed to be queued."); - m_numInFlightReads--; - - queuedJobs = true; - break; - } - - if (m_numInFlightReads == 0 || m_numRunningJobs == m_maxNumJobs) - { - return queuedJobs; - } + }; + FileRequest* fileCheckRequest = m_context->GetNewInternalRequest(); + fileCheckRequest->CreateFileExistsCheck(data.m_path); + fileCheckRequest->SetCompletionCallback(AZStd::move(callback)); + StreamStackEntry::QueueRequest(fileCheckRequest); } - return queuedJobs; + else + { + m_context->PushPreparedRequest(nextRequest); + } + } + else + { + StreamStackEntry::PrepareRequest(request); + } + } + + void FullFileDecompressor::PrepareDedicatedCache(FileRequest* request, const RequestPath& path) + { + CompressionInfo info; + if (CompressionUtils::FindCompressionInfo(info, path.GetRelativePath())) + { + FileRequest* nextRequest = m_context->GetNewInternalRequest(); + AZStd::visit([request, &info, nextRequest](auto&& args) + { + using Command = AZStd::decay_t; + if constexpr (AZStd::is_same_v) + { + nextRequest->CreateDedicatedCacheCreation(AZStd::move(info.m_archiveFilename), + FileRange::CreateRange(info.m_offset, info.m_compressedSize), request); + } + else if constexpr (AZStd::is_same_v) + { + nextRequest->CreateDedicatedCacheDestruction(AZStd::move(info.m_archiveFilename), + FileRange::CreateRange(info.m_offset, info.m_compressedSize), request); + } + }, request->GetCommand()); + + if (info.m_conflictResolution == ConflictResolution::PreferFile) + { + auto callback = [this, nextRequest](const FileRequest& checkRequest) + { + AZ_PROFILE_FUNCTION(AzCore); + auto check = AZStd::get_if(&checkRequest.GetCommand()); + AZ_Assert(check, + "Callback in FullFileDecompressor::PrepareDedicatedCache expected FileExistsCheck but got another command."); + if (check->m_found) + { + FileRequest* originalRequest = nextRequest->GetParent(); + m_context->RejectRequest(nextRequest); + StreamStackEntry::PrepareRequest(originalRequest); + } + else + { + m_context->PushPreparedRequest(nextRequest); + } + }; + FileRequest* fileCheckRequest = m_context->GetNewInternalRequest(); + fileCheckRequest->CreateFileExistsCheck(path); + fileCheckRequest->SetCompletionCallback(AZStd::move(callback)); + StreamStackEntry::QueueRequest(fileCheckRequest); + } + else + { + m_context->PushPreparedRequest(nextRequest); + } + } + else + { + StreamStackEntry::PrepareRequest(request); + } + } + + void FullFileDecompressor::FileExistsCheck(FileRequest* checkRequest) + { + auto& fileCheckRequest = AZStd::get(checkRequest->GetCommand()); + CompressionInfo info; + if (CompressionUtils::FindCompressionInfo(info, fileCheckRequest.m_path.GetRelativePath())) + { + fileCheckRequest.m_found = true; + } + else + { + // The file isn't in the archive but might still exist as a loose file, so let the next node have a shot. + StreamStackEntry::QueueRequest(checkRequest); + } + } + + void FullFileDecompressor::StartArchiveRead(FileRequest* compressedReadRequest) + { + if (!m_next) + { + compressedReadRequest->SetStatus(IStreamerTypes::RequestStatus::Failed); + m_context->MarkRequestAsCompleted(compressedReadRequest); + return; } - void FullFileDecompressor::FinishDecompression([[maybe_unused]] FileRequest* waitRequest, u32 jobSlot) + for (u32 i = 0; i < m_maxNumReads; ++i) { - DecompressionInformation& jobInfo = m_processingJobs[jobSlot]; - AZ_Assert(jobInfo.m_waitRequest == waitRequest, "Job slot didn't contain the expected wait request."); + if (m_readBufferStatus[i] == ReadBufferStatus::Unused) + { + auto data = AZStd::get_if(&compressedReadRequest->GetCommand()); + AZ_Assert(data, "Compressed request that's starting a read in FullFileDecompressor didn't contain compression read data."); + AZ_Assert(data->m_compressionInfo.m_decompressor, + "FileRequest for FullFileDecompressor is missing a decompression callback."); - auto endTime = AZStd::chrono::high_resolution_clock::now(); + CompressionInfo& info = data->m_compressionInfo; + AZ_Assert(info.m_decompressor, "FullFileDecompressor is planning to a queue a request for reading but couldn't find a decompressor."); - FileRequest* compressedRequest = jobInfo.m_waitRequest->GetParent(); - AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); + // The buffer is aligned down but the offset is not corrected. If the offset was adjusted it would mean the same data is read + // multiple times and negates the block cache's ability to detect these cases. By still adjusting it means that the reads between + // the BlockCache's prolog and epilog are read into aligned buffers. + size_t offsetAdjustment = info.m_offset - AZ_SIZE_ALIGN_DOWN(info.m_offset, aznumeric_cast(m_alignment)); + size_t bufferSize = AZ_SIZE_ALIGN_UP((info.m_compressedSize + offsetAdjustment), aznumeric_cast(m_alignment)); + m_readBuffers[i] = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( + bufferSize, m_alignment, 0, "AZ::IO::Streamer FullFileDecompressor", __FILE__, __LINE__)); + m_memoryUsage += bufferSize; + + FileRequest* archiveReadRequest = m_context->GetNewInternalRequest(); + archiveReadRequest->CreateRead(compressedReadRequest, m_readBuffers[i] + offsetAdjustment, bufferSize, info.m_archiveFilename, + info.m_offset, info.m_compressedSize, info.m_isSharedPak); + archiveReadRequest->SetCompletionCallback( + [this, readSlot = i](FileRequest& request) + { + AZ_PROFILE_FUNCTION(AzCore); + FinishArchiveRead(&request, readSlot); + }); + m_next->QueueRequest(archiveReadRequest); + + m_readRequests[i] = archiveReadRequest; + m_readBufferStatus[i] = ReadBufferStatus::ReadInFlight; + + AZ_Assert(m_numInFlightReads < m_maxNumReads, + "A FileRequest was queued for reading in FullFileDecompressor, but there's no slots available."); + m_numInFlightReads++; + + return; + } + } + AZ_Assert(false, "%u of %u read slots are use in the FullFileDecompressor, but no empty slot was found.", m_numInFlightReads, m_maxNumReads); + } + + void FullFileDecompressor::FinishArchiveRead(FileRequest* readRequest, u32 readSlot) + { + AZ_Assert(m_readRequests[readSlot] == readRequest, + "Request in the archive read slot isn't the same as request that's being completed."); + + FileRequest* compressedRequest = readRequest->GetParent(); + AZ_Assert(compressedRequest, "Read requests started by FullFileDecompressor is missing a parent request."); + + if (readRequest->GetStatus() == IStreamerTypes::RequestStatus::Completed) + { + m_readBufferStatus[readSlot] = ReadBufferStatus::PendingDecompression; + ++m_numPendingDecompression; + + // Add this wait so the compressed request isn't fully completed yet as only the read part is done. The + // job thread will finish this wait, which in turn will trigger this function again on the main streaming thread. + FileRequest* waitRequest = m_context->GetNewInternalRequest(); + waitRequest->CreateWait(compressedRequest); + m_readRequests[readSlot] = waitRequest; + } + else + { auto data = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(data, "Compressed request in FullFileDecompressor that completed decompression didn't contain compression read data."); + AZ_Assert(data, "Compressed request in FullFileDecompressor that finished unsuccessfully didn't contain compression read data."); CompressionInfo& info = data->m_compressionInfo; size_t offsetAdjustment = info.m_offset - AZ_SIZE_ALIGN_DOWN(info.m_offset, aznumeric_cast(m_alignment)); size_t bufferSize = AZ_SIZE_ALIGN_UP((info.m_compressedSize + offsetAdjustment), aznumeric_cast(m_alignment)); m_memoryUsage -= bufferSize; - if (data->m_readOffset != 0 || data->m_readSize != data->m_compressionInfo.m_uncompressedSize) + + if (m_readBuffers[readSlot] != nullptr) { - m_memoryUsage -= data->m_compressionInfo.m_uncompressedSize; + AZ::AllocatorInstance::Get().DeAllocate(m_readBuffers[readSlot], bufferSize, m_alignment); + m_readBuffers[readSlot] = nullptr; + } + m_readRequests[readSlot] = nullptr; + m_readBufferStatus[readSlot] = ReadBufferStatus::Unused; + AZ_Assert(m_numInFlightReads > 0, + "Trying to decrement a read request after it was canceled or failed in FullFileDecompressor, " + "but no read requests are supposed to be queued."); + m_numInFlightReads--; + } + } + + bool FullFileDecompressor::StartDecompressions() + { + bool queuedJobs = false; + u32 jobSlot = 0; + for (u32 readSlot = 0; readSlot < m_maxNumReads; ++readSlot) + { + // Find completed read. + if (m_readBufferStatus[readSlot] != ReadBufferStatus::PendingDecompression) + { + continue; } - m_decompressionJobDelayMicroSec.PushEntry(AZStd::chrono::duration_cast( - jobInfo.m_jobStartTime - jobInfo.m_queueStartTime).count()); - m_decompressionDurationMicroSec.PushEntry(AZStd::chrono::duration_cast( - endTime - jobInfo.m_jobStartTime).count()); - m_bytesDecompressed.PushEntry(data->m_compressionInfo.m_compressedSize); + // Find decompression slot + for (; jobSlot < m_maxNumJobs; ++jobSlot) + { + if (m_processingJobs[jobSlot].IsProcessing()) + { + continue; + } - AZ::AllocatorInstance::Get().DeAllocate(jobInfo.m_compressedData, bufferSize, m_alignment); - jobInfo.m_compressedData = nullptr; - AZ_Assert(m_numRunningJobs > 0, "About to complete a decompression job, but the internal count doesn't see a running job."); - --m_numRunningJobs; - return; + FileRequest* waitRequest = m_readRequests[readSlot]; + AZ_Assert(AZStd::holds_alternative(waitRequest->GetCommand()), + "File request waiting for decompression wasn't marked as being a wait operation."); + FileRequest* compressedRequest = waitRequest->GetParent(); + AZ_Assert(compressedRequest, "Read requests started by FullFileDecompressor is missing a parent request."); + + waitRequest->SetCompletionCallback([this, jobSlot](FileRequest& request) + { + AZ_PROFILE_FUNCTION(AzCore); + FinishDecompression(&request, jobSlot); + }); + + DecompressionInformation& info = m_processingJobs[jobSlot]; + info.m_waitRequest = waitRequest; + info.m_queueStartTime = AZStd::chrono::high_resolution_clock::now(); + info.m_jobStartTime = info.m_queueStartTime; // Set these to the same in case the scheduler requests an update before the job has started. + info.m_compressedData = m_readBuffers[readSlot]; // Transfer ownership of the pointer. + m_readBuffers[readSlot] = nullptr; + + AZ::Job* decompressionJob; + auto data = AZStd::get_if(&compressedRequest->GetCommand()); + AZ_Assert(data, "Compressed request in FullFileDecompressor that's starting decompression didn't contain compression read data."); + AZ_Assert(data->m_compressionInfo.m_decompressor, "FullFileDecompressor is queuing a decompression job but couldn't find a decompressor."); + + info.m_alignmentOffset = aznumeric_caster(data->m_compressionInfo.m_offset - + AZ_SIZE_ALIGN_DOWN(data->m_compressionInfo.m_offset, aznumeric_cast(m_alignment))); + + if (data->m_readOffset == 0 && data->m_readSize == data->m_compressionInfo.m_uncompressedSize) + { + auto job = [this, &info]() + { + FullDecompression(m_context, info); + }; + decompressionJob = AZ::CreateJobFunction(job, true, m_decompressionjobContext.get()); + } + else + { + m_memoryUsage += data->m_compressionInfo.m_uncompressedSize; + auto job = [this, &info]() + { + PartialDecompression(m_context, info); + }; + decompressionJob = AZ::CreateJobFunction(job, true, m_decompressionjobContext.get()); + } + --m_numPendingDecompression; + ++m_numRunningJobs; + decompressionJob->Start(); + + m_readRequests[readSlot] = nullptr; + m_readBufferStatus[readSlot] = ReadBufferStatus::Unused; + AZ_Assert(m_numInFlightReads > 0, "Trying to decrement a read request after it's queued for decompression in FullFileDecompressor, but no read requests are supposed to be queued."); + m_numInFlightReads--; + + queuedJobs = true; + break; + } + + if (m_numInFlightReads == 0 || m_numRunningJobs == m_maxNumJobs) + { + return queuedJobs; + } } + return queuedJobs; + } - void FullFileDecompressor::FullDecompression(StreamerContext* context, DecompressionInformation& info) + void FullFileDecompressor::FinishDecompression([[maybe_unused]] FileRequest* waitRequest, u32 jobSlot) + { + DecompressionInformation& jobInfo = m_processingJobs[jobSlot]; + AZ_Assert(jobInfo.m_waitRequest == waitRequest, "Job slot didn't contain the expected wait request."); + + auto endTime = AZStd::chrono::high_resolution_clock::now(); + + FileRequest* compressedRequest = jobInfo.m_waitRequest->GetParent(); + AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); + auto data = AZStd::get_if(&compressedRequest->GetCommand()); + AZ_Assert(data, "Compressed request in FullFileDecompressor that completed decompression didn't contain compression read data."); + CompressionInfo& info = data->m_compressionInfo; + size_t offsetAdjustment = info.m_offset - AZ_SIZE_ALIGN_DOWN(info.m_offset, aznumeric_cast(m_alignment)); + size_t bufferSize = AZ_SIZE_ALIGN_UP((info.m_compressedSize + offsetAdjustment), aznumeric_cast(m_alignment)); + m_memoryUsage -= bufferSize; + if (data->m_readOffset != 0 || data->m_readSize != data->m_compressionInfo.m_uncompressedSize) { - info.m_jobStartTime = AZStd::chrono::high_resolution_clock::now(); - - FileRequest* compressedRequest = info.m_waitRequest->GetParent(); - AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); - auto request = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(request, "Compressed request in FullFileDecompressor that's running full decompression didn't contain compression read data."); - CompressionInfo& compressionInfo = request->m_compressionInfo; - AZ_Assert(compressionInfo.m_decompressor, "Full decompressor job started, but there's no decompressor callback assigned."); - - AZ_Assert(request->m_readOffset == 0, "FullFileDecompressor is doing a full decompression on a file request with an offset (%zu).", - request->m_readOffset); - AZ_Assert(compressionInfo.m_uncompressedSize == request->m_readSize, - "FullFileDecompressor is doing a full decompression, but the target buffer size (%llu) doesn't match the decompressed size (%zu).", - request->m_readSize, compressionInfo.m_uncompressedSize); - - bool success = compressionInfo.m_decompressor(compressionInfo, info.m_compressedData + info.m_alignmentOffset, - compressionInfo.m_compressedSize, request->m_output, compressionInfo.m_uncompressedSize); - info.m_waitRequest->SetStatus(success ? IStreamerTypes::RequestStatus::Completed : IStreamerTypes::RequestStatus::Failed); - - context->MarkRequestAsCompleted(info.m_waitRequest); - context->WakeUpSchedulingThread(); + m_memoryUsage -= data->m_compressionInfo.m_uncompressedSize; } - void FullFileDecompressor::PartialDecompression(StreamerContext* context, DecompressionInformation& info) - { - info.m_jobStartTime = AZStd::chrono::high_resolution_clock::now(); + m_decompressionJobDelayMicroSec.PushEntry(AZStd::chrono::duration_cast( + jobInfo.m_jobStartTime - jobInfo.m_queueStartTime).count()); + m_decompressionDurationMicroSec.PushEntry(AZStd::chrono::duration_cast( + endTime - jobInfo.m_jobStartTime).count()); + m_bytesDecompressed.PushEntry(data->m_compressionInfo.m_compressedSize); - FileRequest* compressedRequest = info.m_waitRequest->GetParent(); - AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); - auto request = AZStd::get_if(&compressedRequest->GetCommand()); - AZ_Assert(request, "Compressed request in FullFileDecompressor that's running partial decompression didn't contain compression read data."); - CompressionInfo& compressionInfo = request->m_compressionInfo; - AZ_Assert(compressionInfo.m_decompressor, "Partial decompressor job started, but there's no decompressor callback assigned."); + AZ::AllocatorInstance::Get().DeAllocate(jobInfo.m_compressedData, bufferSize, m_alignment); + jobInfo.m_compressedData = nullptr; + AZ_Assert(m_numRunningJobs > 0, "About to complete a decompression job, but the internal count doesn't see a running job."); + --m_numRunningJobs; + return; + } - AZStd::unique_ptr decompressionBuffer = AZStd::unique_ptr(new u8[compressionInfo.m_uncompressedSize]); - bool success = compressionInfo.m_decompressor(compressionInfo, info.m_compressedData + info.m_alignmentOffset, - compressionInfo.m_compressedSize, decompressionBuffer.get(), compressionInfo.m_uncompressedSize); - info.m_waitRequest->SetStatus(success ? IStreamerTypes::RequestStatus::Completed : IStreamerTypes::RequestStatus::Failed); - - memcpy(request->m_output, decompressionBuffer.get() + request->m_readOffset, request->m_readSize); + void FullFileDecompressor::FullDecompression(StreamerContext* context, DecompressionInformation& info) + { + info.m_jobStartTime = AZStd::chrono::high_resolution_clock::now(); - context->MarkRequestAsCompleted(info.m_waitRequest); - context->WakeUpSchedulingThread(); - } - } // namespace IO -} // namespace AZ + FileRequest* compressedRequest = info.m_waitRequest->GetParent(); + AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); + auto request = AZStd::get_if(&compressedRequest->GetCommand()); + AZ_Assert(request, "Compressed request in FullFileDecompressor that's running full decompression didn't contain compression read data."); + CompressionInfo& compressionInfo = request->m_compressionInfo; + AZ_Assert(compressionInfo.m_decompressor, "Full decompressor job started, but there's no decompressor callback assigned."); + + AZ_Assert(request->m_readOffset == 0, "FullFileDecompressor is doing a full decompression on a file request with an offset (%zu).", + request->m_readOffset); + AZ_Assert(compressionInfo.m_uncompressedSize == request->m_readSize, + "FullFileDecompressor is doing a full decompression, but the target buffer size (%llu) doesn't match the decompressed size (%zu).", + request->m_readSize, compressionInfo.m_uncompressedSize); + + bool success = compressionInfo.m_decompressor(compressionInfo, info.m_compressedData + info.m_alignmentOffset, + compressionInfo.m_compressedSize, request->m_output, compressionInfo.m_uncompressedSize); + info.m_waitRequest->SetStatus(success ? IStreamerTypes::RequestStatus::Completed : IStreamerTypes::RequestStatus::Failed); + + context->MarkRequestAsCompleted(info.m_waitRequest); + context->WakeUpSchedulingThread(); + } + + void FullFileDecompressor::PartialDecompression(StreamerContext* context, DecompressionInformation& info) + { + info.m_jobStartTime = AZStd::chrono::high_resolution_clock::now(); + + FileRequest* compressedRequest = info.m_waitRequest->GetParent(); + AZ_Assert(compressedRequest, "A wait request attached to FullFileDecompressor was completed but didn't have a parent compressed request."); + auto request = AZStd::get_if(&compressedRequest->GetCommand()); + AZ_Assert(request, "Compressed request in FullFileDecompressor that's running partial decompression didn't contain compression read data."); + CompressionInfo& compressionInfo = request->m_compressionInfo; + AZ_Assert(compressionInfo.m_decompressor, "Partial decompressor job started, but there's no decompressor callback assigned."); + + AZStd::unique_ptr decompressionBuffer = AZStd::unique_ptr(new u8[compressionInfo.m_uncompressedSize]); + bool success = compressionInfo.m_decompressor(compressionInfo, info.m_compressedData + info.m_alignmentOffset, + compressionInfo.m_compressedSize, decompressionBuffer.get(), compressionInfo.m_uncompressedSize); + info.m_waitRequest->SetStatus(success ? IStreamerTypes::RequestStatus::Completed : IStreamerTypes::RequestStatus::Failed); + + memcpy(request->m_output, decompressionBuffer.get() + request->m_readOffset, request->m_readSize); + + context->MarkRequestAsCompleted(info.m_waitRequest); + context->WakeUpSchedulingThread(); + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Streamer/ReadSplitter.cpp b/Code/Framework/AzCore/AzCore/IO/Streamer/ReadSplitter.cpp index 00c1c63933..a952e31a93 100644 --- a/Code/Framework/AzCore/AzCore/IO/Streamer/ReadSplitter.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Streamer/ReadSplitter.cpp @@ -14,376 +14,373 @@ #include #include -namespace AZ +namespace AZ::IO { - namespace IO + AZStd::shared_ptr ReadSplitterConfig::AddStreamStackEntry( + const HardwareInformation& hardware, AZStd::shared_ptr parent) { - AZStd::shared_ptr ReadSplitterConfig::AddStreamStackEntry( - const HardwareInformation& hardware, AZStd::shared_ptr parent) + size_t splitSize; + switch (m_splitSize) { - size_t splitSize; - switch (m_splitSize) - { - case SplitSize::MaxTransfer: - splitSize = hardware.m_maxTransfer; - break; - case SplitSize::MemoryAlignment: - splitSize = hardware.m_maxPhysicalSectorSize; - break; - default: - splitSize = m_splitSize; - break; - } - - size_t bufferSize = m_bufferSizeMib * 1_mib; - if (bufferSize < splitSize) - { - AZ_Warning("Streamer", false, "The buffer size for the Read Splitter is smaller than the individual split size. " - "It will be increased to fit at least one split."); - bufferSize = splitSize; - } - - auto stackEntry = AZStd::make_shared( - splitSize, - aznumeric_caster(hardware.m_maxPhysicalSectorSize), - aznumeric_caster(hardware.m_maxLogicalSectorSize), - bufferSize, m_adjustOffset, m_splitAlignedRequests); - stackEntry->SetNext(AZStd::move(parent)); - return stackEntry; + case SplitSize::MaxTransfer: + splitSize = hardware.m_maxTransfer; + break; + case SplitSize::MemoryAlignment: + splitSize = hardware.m_maxPhysicalSectorSize; + break; + default: + splitSize = m_splitSize; + break; } - void ReadSplitterConfig::Reflect(AZ::ReflectContext* context) + size_t bufferSize = m_bufferSizeMib * 1_mib; + if (bufferSize < splitSize) { - if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) - { - serializeContext->Enum() - ->Version(1) - ->Value("MaxTransfer", SplitSize::MaxTransfer) - ->Value("MemoryAlignment", SplitSize::MemoryAlignment); - - serializeContext->Class() - ->Version(1) - ->Field("BufferSizeMib", &ReadSplitterConfig::m_bufferSizeMib) - ->Field("SplitSize", &ReadSplitterConfig::m_splitSize) - ->Field("AdjustOffset", &ReadSplitterConfig::m_adjustOffset) - ->Field("SplitAlignedRequests", &ReadSplitterConfig::m_splitAlignedRequests); - } + AZ_Warning("Streamer", false, "The buffer size for the Read Splitter is smaller than the individual split size. " + "It will be increased to fit at least one split."); + bufferSize = splitSize; } - static constexpr char AvgNumSubReadsName[] = "Avg. num sub reads"; - static constexpr char AlignedReadsName[] = "Aligned reads"; - static constexpr char NumAvailableBufferSlotsName[] = "Num available buffer slots"; - static constexpr char NumPendingReadsName[] = "Num pending reads"; + auto stackEntry = AZStd::make_shared( + splitSize, + aznumeric_caster(hardware.m_maxPhysicalSectorSize), + aznumeric_caster(hardware.m_maxLogicalSectorSize), + bufferSize, m_adjustOffset, m_splitAlignedRequests); + stackEntry->SetNext(AZStd::move(parent)); + return stackEntry; + } - ReadSplitter::ReadSplitter(u64 maxReadSize, u32 memoryAlignment, u32 sizeAlignment, size_t bufferSize, - bool adjustOffset, bool splitAlignedRequests) - : StreamStackEntry("Read splitter") - , m_buffer(nullptr) - , m_bufferSize(bufferSize) - , m_maxReadSize(maxReadSize) - , m_memoryAlignment(memoryAlignment) - , m_sizeAlignment(sizeAlignment) - , m_adjustOffset(adjustOffset) - , m_splitAlignedRequests(splitAlignedRequests) + void ReadSplitterConfig::Reflect(AZ::ReflectContext* context) + { + if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) { - AZ_Assert(IStreamerTypes::IsPowerOf2(memoryAlignment), "Memory alignment needs to be a power of 2"); - AZ_Assert(IStreamerTypes::IsPowerOf2(sizeAlignment), "Size alignment needs to be a power of 2"); - AZ_Assert(IStreamerTypes::IsAlignedTo(maxReadSize, sizeAlignment), - "Maximum read size isn't aligned to a multiple of the size alignment."); + serializeContext->Enum() + ->Version(1) + ->Value("MaxTransfer", SplitSize::MaxTransfer) + ->Value("MemoryAlignment", SplitSize::MemoryAlignment); - size_t numBufferSlots = bufferSize / maxReadSize; - // Don't divide the reads up in more sub-reads than there are dependencies available. - numBufferSlots = AZStd::min(numBufferSlots, FileRequest::GetMaxNumDependencies()); - m_bufferCopyInformation = AZStd::unique_ptr(new BufferCopyInformation[numBufferSlots]); - m_availableBufferSlots.reserve(numBufferSlots); - for (u32 i = aznumeric_caster(numBufferSlots); i > 0; --i) - { - m_availableBufferSlots.push_back(i - 1); - } + serializeContext->Class() + ->Version(1) + ->Field("BufferSizeMib", &ReadSplitterConfig::m_bufferSizeMib) + ->Field("SplitSize", &ReadSplitterConfig::m_splitSize) + ->Field("AdjustOffset", &ReadSplitterConfig::m_adjustOffset) + ->Field("SplitAlignedRequests", &ReadSplitterConfig::m_splitAlignedRequests); + } + } + + static constexpr char AvgNumSubReadsName[] = "Avg. num sub reads"; + static constexpr char AlignedReadsName[] = "Aligned reads"; + static constexpr char NumAvailableBufferSlotsName[] = "Num available buffer slots"; + static constexpr char NumPendingReadsName[] = "Num pending reads"; + + ReadSplitter::ReadSplitter(u64 maxReadSize, u32 memoryAlignment, u32 sizeAlignment, size_t bufferSize, + bool adjustOffset, bool splitAlignedRequests) + : StreamStackEntry("Read splitter") + , m_buffer(nullptr) + , m_bufferSize(bufferSize) + , m_maxReadSize(maxReadSize) + , m_memoryAlignment(memoryAlignment) + , m_sizeAlignment(sizeAlignment) + , m_adjustOffset(adjustOffset) + , m_splitAlignedRequests(splitAlignedRequests) + { + AZ_Assert(IStreamerTypes::IsPowerOf2(memoryAlignment), "Memory alignment needs to be a power of 2"); + AZ_Assert(IStreamerTypes::IsPowerOf2(sizeAlignment), "Size alignment needs to be a power of 2"); + AZ_Assert(IStreamerTypes::IsAlignedTo(maxReadSize, sizeAlignment), + "Maximum read size isn't aligned to a multiple of the size alignment."); + + size_t numBufferSlots = bufferSize / maxReadSize; + // Don't divide the reads up in more sub-reads than there are dependencies available. + numBufferSlots = AZStd::min(numBufferSlots, FileRequest::GetMaxNumDependencies()); + m_bufferCopyInformation = AZStd::unique_ptr(new BufferCopyInformation[numBufferSlots]); + m_availableBufferSlots.reserve(numBufferSlots); + for (u32 i = aznumeric_caster(numBufferSlots); i > 0; --i) + { + m_availableBufferSlots.push_back(i - 1); + } + } + + ReadSplitter::~ReadSplitter() + { + if (m_buffer) + { + AZ::AllocatorInstance::Get().DeAllocate(m_buffer, m_bufferSize, m_memoryAlignment); + } + } + + void ReadSplitter::QueueRequest(FileRequest* request) + { + AZ_Assert(request, "QueueRequest was provided a null request."); + if (!m_next) + { + request->SetStatus(IStreamerTypes::RequestStatus::Failed); + m_context->MarkRequestAsCompleted(request); + return; } - ReadSplitter::~ReadSplitter() + auto data = AZStd::get_if(&request->GetCommand()); + if (data == nullptr) { - if (m_buffer) - { - AZ::AllocatorInstance::Get().DeAllocate(m_buffer, m_bufferSize, m_memoryAlignment); - } + StreamStackEntry::QueueRequest(request); + return; } - void ReadSplitter::QueueRequest(FileRequest* request) - { - AZ_Assert(request, "QueueRequest was provided a null request."); - if (!m_next) - { - request->SetStatus(IStreamerTypes::RequestStatus::Failed); - m_context->MarkRequestAsCompleted(request); - return; - } + m_averageNumSubReadsStat.PushSample(aznumeric_cast((data->m_size / m_maxReadSize) + 1)); + Statistic::PlotImmediate(m_name, AvgNumSubReadsName, m_averageNumSubReadsStat.GetMostRecentSample()); - auto data = AZStd::get_if(&request->GetCommand()); - if (data == nullptr) + bool isAligned = IStreamerTypes::IsAlignedTo(data->m_output, m_memoryAlignment); + if (m_adjustOffset) + { + isAligned = isAligned && IStreamerTypes::IsAlignedTo(data->m_offset, m_sizeAlignment); + } + + if (isAligned || m_bufferSize == 0) + { + m_alignedReadsStat.PushSample(isAligned ? 1.0 : 0.0); + if (!m_splitAlignedRequests) { StreamStackEntry::QueueRequest(request); - return; - } - - m_averageNumSubReadsStat.PushSample(aznumeric_cast((data->m_size / m_maxReadSize) + 1)); - Statistic::PlotImmediate(m_name, AvgNumSubReadsName, m_averageNumSubReadsStat.GetMostRecentSample()); - - bool isAligned = IStreamerTypes::IsAlignedTo(data->m_output, m_memoryAlignment); - if (m_adjustOffset) - { - isAligned = isAligned && IStreamerTypes::IsAlignedTo(data->m_offset, m_sizeAlignment); - } - - if (isAligned || m_bufferSize == 0) - { - m_alignedReadsStat.PushSample(isAligned ? 1.0 : 0.0); - if (!m_splitAlignedRequests) - { - StreamStackEntry::QueueRequest(request); - } - else - { - QueueAlignedRead(request); - } } else { - m_alignedReadsStat.PushSample(0.0); - InitializeBuffer(); - QueueBufferedRead(request); + QueueAlignedRead(request); } } - - void ReadSplitter::QueueAlignedRead(FileRequest* request) + else { - auto data = AZStd::get_if(&request->GetCommand()); - AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); + m_alignedReadsStat.PushSample(0.0); + InitializeBuffer(); + QueueBufferedRead(request); + } + } - if (data->m_size <= m_maxReadSize) - { - StreamStackEntry::QueueRequest(request); - return; - } + void ReadSplitter::QueueAlignedRead(FileRequest* request) + { + auto data = AZStd::get_if(&request->GetCommand()); + AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); - PendingRead pendingRead; - pendingRead.m_request = request; - pendingRead.m_output = reinterpret_cast(data->m_output); - pendingRead.m_outputSize = data->m_outputSize; - pendingRead.m_readSize = data->m_size; - pendingRead.m_offset = data->m_offset; - pendingRead.m_isBuffered = false; - - if (!m_pendingReads.empty()) - { - m_pendingReads.push_back(pendingRead); - return; - } - - if (!QueueAlignedRead(pendingRead)) - { - m_pendingReads.push_back(pendingRead); - } + if (data->m_size <= m_maxReadSize) + { + StreamStackEntry::QueueRequest(request); + return; } - bool ReadSplitter::QueueAlignedRead(PendingRead& pending) + PendingRead pendingRead; + pendingRead.m_request = request; + pendingRead.m_output = reinterpret_cast(data->m_output); + pendingRead.m_outputSize = data->m_outputSize; + pendingRead.m_readSize = data->m_size; + pendingRead.m_offset = data->m_offset; + pendingRead.m_isBuffered = false; + + if (!m_pendingReads.empty()) { - auto data = AZStd::get_if(&pending.m_request->GetCommand()); - AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); + m_pendingReads.push_back(pendingRead); + return; + } - while (pending.m_readSize > 0) + if (!QueueAlignedRead(pendingRead)) + { + m_pendingReads.push_back(pendingRead); + } + } + + bool ReadSplitter::QueueAlignedRead(PendingRead& pending) + { + auto data = AZStd::get_if(&pending.m_request->GetCommand()); + AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); + + while (pending.m_readSize > 0) + { + if (pending.m_request->GetNumDependencies() >= FileRequest::GetMaxNumDependencies()) { - if (pending.m_request->GetNumDependencies() >= FileRequest::GetMaxNumDependencies()) + // Add a wait to make sure the read request isn't completed if all sub-reads completed before + // the ReadSplitter has had a chance to add new sub-reads to complete the read. + if (pending.m_wait == nullptr) { - // Add a wait to make sure the read request isn't completed if all sub-reads completed before - // the ReadSplitter has had a chance to add new sub-reads to complete the read. - if (pending.m_wait == nullptr) - { - pending.m_wait = m_context->GetNewInternalRequest(); - pending.m_wait->CreateWait(pending.m_request); - } - return false; + pending.m_wait = m_context->GetNewInternalRequest(); + pending.m_wait->CreateWait(pending.m_request); } + return false; + } - u64 readSize = m_maxReadSize; - size_t bufferSize = m_maxReadSize; - if (pending.m_readSize < m_maxReadSize) + u64 readSize = m_maxReadSize; + size_t bufferSize = m_maxReadSize; + if (pending.m_readSize < m_maxReadSize) + { + readSize = pending.m_readSize; + // This will be the last read so give the remainder of the output buffer to the final request. + bufferSize = pending.m_outputSize; + } + + FileRequest* subRequest = m_context->GetNewInternalRequest(); + subRequest->CreateRead(pending.m_request, pending.m_output, bufferSize, data->m_path, pending.m_offset, readSize, data->m_sharedRead); + subRequest->SetCompletionCallback([this](FileRequest&) { - readSize = pending.m_readSize; - // This will be the last read so give the remainder of the output buffer to the final request. - bufferSize = pending.m_outputSize; + AZ_PROFILE_FUNCTION(AzCore); + QueuePendingRequest(); + }); + m_next->QueueRequest(subRequest); + + pending.m_offset += readSize; + pending.m_readSize -= readSize; + pending.m_outputSize -= bufferSize; + pending.m_output += readSize; + } + if (pending.m_wait != nullptr) + { + m_context->MarkRequestAsCompleted(pending.m_wait); + pending.m_wait = nullptr; + } + return true; + } + + void ReadSplitter::QueueBufferedRead(FileRequest* request) + { + auto data = AZStd::get_if(&request->GetCommand()); + AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); + + PendingRead pendingRead; + pendingRead.m_request = request; + pendingRead.m_output = reinterpret_cast(data->m_output); + pendingRead.m_outputSize = data->m_outputSize; + pendingRead.m_readSize = data->m_size; + pendingRead.m_offset = data->m_offset; + pendingRead.m_isBuffered = true; + + if (!m_pendingReads.empty()) + { + m_pendingReads.push_back(pendingRead); + return; + } + + if (!QueueBufferedRead(pendingRead)) + { + m_pendingReads.push_back(pendingRead); + } + } + + bool ReadSplitter::QueueBufferedRead(PendingRead& pending) + { + auto data = AZStd::get_if(&pending.m_request->GetCommand()); + AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); + + while (pending.m_readSize > 0) + { + if (!m_availableBufferSlots.empty()) + { + u32 bufferSlot = m_availableBufferSlots.back(); + m_availableBufferSlots.pop_back(); + + u64 readSize; + u64 copySize; + u64 offset; + BufferCopyInformation& copyInfo = m_bufferCopyInformation[bufferSlot]; + copyInfo.m_target = pending.m_output; + + if (m_adjustOffset) + { + offset = AZ_SIZE_ALIGN_DOWN(pending.m_offset, aznumeric_cast(m_sizeAlignment)); + size_t bufferOffset = pending.m_offset - offset; + copyInfo.m_bufferOffset = bufferOffset; + readSize = AZStd::min(pending.m_readSize + bufferOffset, m_maxReadSize); + copySize = readSize - bufferOffset; } - + else + { + offset = pending.m_offset; + readSize = AZStd::min(pending.m_readSize, m_maxReadSize); + copySize = readSize; + } + AZ_Assert(readSize <= m_maxReadSize, "Read size %llu in read splitter exceeds the maximum split size of %llu.", + readSize, m_maxReadSize); + copyInfo.m_size = copySize; + FileRequest* subRequest = m_context->GetNewInternalRequest(); - subRequest->CreateRead(pending.m_request, pending.m_output, bufferSize, data->m_path, pending.m_offset, readSize, data->m_sharedRead); - subRequest->SetCompletionCallback([this](FileRequest&) + subRequest->CreateRead(pending.m_request, GetBufferSlot(bufferSlot), m_maxReadSize, data->m_path, + offset, readSize, data->m_sharedRead); + subRequest->SetCompletionCallback([this, bufferSlot]([[maybe_unused]] FileRequest& request) { AZ_PROFILE_FUNCTION(AzCore); + + BufferCopyInformation& copyInfo = m_bufferCopyInformation[bufferSlot]; + memcpy(copyInfo.m_target, GetBufferSlot(bufferSlot) + copyInfo.m_bufferOffset, copyInfo.m_size); + m_availableBufferSlots.push_back(bufferSlot); + QueuePendingRequest(); }); m_next->QueueRequest(subRequest); - pending.m_offset += readSize; - pending.m_readSize -= readSize; - pending.m_outputSize -= bufferSize; - pending.m_output += readSize; + pending.m_offset += copySize; + pending.m_readSize -= copySize; + pending.m_outputSize -= copySize; + pending.m_output += copySize; } - if (pending.m_wait != nullptr) + else { - m_context->MarkRequestAsCompleted(pending.m_wait); - pending.m_wait = nullptr; - } - return true; - } - - void ReadSplitter::QueueBufferedRead(FileRequest* request) - { - auto data = AZStd::get_if(&request->GetCommand()); - AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); - - PendingRead pendingRead; - pendingRead.m_request = request; - pendingRead.m_output = reinterpret_cast(data->m_output); - pendingRead.m_outputSize = data->m_outputSize; - pendingRead.m_readSize = data->m_size; - pendingRead.m_offset = data->m_offset; - pendingRead.m_isBuffered = true; - - if (!m_pendingReads.empty()) - { - m_pendingReads.push_back(pendingRead); - return; - } - - if (!QueueBufferedRead(pendingRead)) - { - m_pendingReads.push_back(pendingRead); - } - } - - bool ReadSplitter::QueueBufferedRead(PendingRead& pending) - { - auto data = AZStd::get_if(&pending.m_request->GetCommand()); - AZ_Assert(data != nullptr, "Provided request to queue by the Read Splitter did not contain a read command."); - - while (pending.m_readSize > 0) - { - if (!m_availableBufferSlots.empty()) + // Add a wait to make sure the read request isn't completed if all sub-reads completed before + // the ReadSplitter has had a chance to add new sub-reads to complete the read. + if (pending.m_wait == nullptr) { - u32 bufferSlot = m_availableBufferSlots.back(); - m_availableBufferSlots.pop_back(); - - u64 readSize; - u64 copySize; - u64 offset; - BufferCopyInformation& copyInfo = m_bufferCopyInformation[bufferSlot]; - copyInfo.m_target = pending.m_output; - - if (m_adjustOffset) - { - offset = AZ_SIZE_ALIGN_DOWN(pending.m_offset, aznumeric_cast(m_sizeAlignment)); - size_t bufferOffset = pending.m_offset - offset; - copyInfo.m_bufferOffset = bufferOffset; - readSize = AZStd::min(pending.m_readSize + bufferOffset, m_maxReadSize); - copySize = readSize - bufferOffset; - } - else - { - offset = pending.m_offset; - readSize = AZStd::min(pending.m_readSize, m_maxReadSize); - copySize = readSize; - } - AZ_Assert(readSize <= m_maxReadSize, "Read size %llu in read splitter exceeds the maximum split size of %llu.", - readSize, m_maxReadSize); - copyInfo.m_size = copySize; - - FileRequest* subRequest = m_context->GetNewInternalRequest(); - subRequest->CreateRead(pending.m_request, GetBufferSlot(bufferSlot), m_maxReadSize, data->m_path, - offset, readSize, data->m_sharedRead); - subRequest->SetCompletionCallback([this, bufferSlot]([[maybe_unused]] FileRequest& request) - { - AZ_PROFILE_FUNCTION(AzCore); - - BufferCopyInformation& copyInfo = m_bufferCopyInformation[bufferSlot]; - memcpy(copyInfo.m_target, GetBufferSlot(bufferSlot) + copyInfo.m_bufferOffset, copyInfo.m_size); - m_availableBufferSlots.push_back(bufferSlot); - - QueuePendingRequest(); - }); - m_next->QueueRequest(subRequest); - - pending.m_offset += copySize; - pending.m_readSize -= copySize; - pending.m_outputSize -= copySize; - pending.m_output += copySize; - } - else - { - // Add a wait to make sure the read request isn't completed if all sub-reads completed before - // the ReadSplitter has had a chance to add new sub-reads to complete the read. - if (pending.m_wait == nullptr) - { - pending.m_wait = m_context->GetNewInternalRequest(); - pending.m_wait->CreateWait(pending.m_request); - } - return false; + pending.m_wait = m_context->GetNewInternalRequest(); + pending.m_wait->CreateWait(pending.m_request); } + return false; } - if (pending.m_wait != nullptr) + } + if (pending.m_wait != nullptr) + { + m_context->MarkRequestAsCompleted(pending.m_wait); + pending.m_wait = nullptr; + } + return true; + } + + void ReadSplitter::QueuePendingRequest() + { + if (!m_pendingReads.empty()) + { + PendingRead& pendingRead = m_pendingReads.front(); + if (pendingRead.m_isBuffered ? QueueBufferedRead(pendingRead) : QueueAlignedRead(pendingRead)) { - m_context->MarkRequestAsCompleted(pending.m_wait); - pending.m_wait = nullptr; - } - return true; - } - - void ReadSplitter::QueuePendingRequest() - { - if (!m_pendingReads.empty()) - { - PendingRead& pendingRead = m_pendingReads.front(); - if (pendingRead.m_isBuffered ? QueueBufferedRead(pendingRead) : QueueAlignedRead(pendingRead)) - { - m_pendingReads.pop_front(); - } + m_pendingReads.pop_front(); } } + } - void ReadSplitter::UpdateStatus(Status& status) const + void ReadSplitter::UpdateStatus(Status& status) const + { + StreamStackEntry::UpdateStatus(status); + if (m_bufferSize > 0) { - StreamStackEntry::UpdateStatus(status); - if (m_bufferSize > 0) - { - s32 numAvailableSlots = aznumeric_cast(m_availableBufferSlots.size()); - status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); - status.m_isIdle = status.m_isIdle && m_pendingReads.empty(); - } + s32 numAvailableSlots = aznumeric_cast(m_availableBufferSlots.size()); + status.m_numAvailableSlots = AZStd::min(status.m_numAvailableSlots, numAvailableSlots); + status.m_isIdle = status.m_isIdle && m_pendingReads.empty(); } + } - void ReadSplitter::CollectStatistics(AZStd::vector& statistics) const - { - statistics.push_back(Statistic::CreateFloat(m_name, AvgNumSubReadsName, m_averageNumSubReadsStat.GetAverage())); - statistics.push_back(Statistic::CreatePercentage(m_name, AlignedReadsName, m_alignedReadsStat.GetAverage())); - statistics.push_back(Statistic::CreateInteger(m_name, NumAvailableBufferSlotsName, aznumeric_caster(m_availableBufferSlots.size()))); - statistics.push_back(Statistic::CreateInteger(m_name, NumPendingReadsName, aznumeric_caster(m_pendingReads.size()))); - StreamStackEntry::CollectStatistics(statistics); - } + void ReadSplitter::CollectStatistics(AZStd::vector& statistics) const + { + statistics.push_back(Statistic::CreateFloat(m_name, AvgNumSubReadsName, m_averageNumSubReadsStat.GetAverage())); + statistics.push_back(Statistic::CreatePercentage(m_name, AlignedReadsName, m_alignedReadsStat.GetAverage())); + statistics.push_back(Statistic::CreateInteger(m_name, NumAvailableBufferSlotsName, aznumeric_caster(m_availableBufferSlots.size()))); + statistics.push_back(Statistic::CreateInteger(m_name, NumPendingReadsName, aznumeric_caster(m_pendingReads.size()))); + StreamStackEntry::CollectStatistics(statistics); + } - void ReadSplitter::InitializeBuffer() + void ReadSplitter::InitializeBuffer() + { + // Lazy initialization to avoid allocating memory if it's not needed. + if (m_bufferSize != 0 && m_buffer == nullptr) { - // Lazy initialization to avoid allocating memory if it's not needed. - if (m_bufferSize != 0 && m_buffer == nullptr) - { - m_buffer = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( - m_bufferSize, m_memoryAlignment, 0, "AZ::IO::Streamer ReadSplitter", __FILE__, __LINE__)); - } + m_buffer = reinterpret_cast(AZ::AllocatorInstance::Get().Allocate( + m_bufferSize, m_memoryAlignment, 0, "AZ::IO::Streamer ReadSplitter", __FILE__, __LINE__)); } + } - u8* ReadSplitter::GetBufferSlot(size_t index) - { - AZ_Assert(m_buffer != nullptr, "A buffer slot was requested by the Read Splitter before the buffer was initialized."); - return m_buffer + (index * m_maxReadSize); - } - } // namespace IO -} // namesapce AZ + u8* ReadSplitter::GetBufferSlot(size_t index) + { + AZ_Assert(m_buffer != nullptr, "A buffer slot was requested by the Read Splitter before the buffer was initialized."); + return m_buffer + (index * m_maxReadSize); + } +} // namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IPC/SharedMemory.cpp b/Code/Framework/AzCore/AzCore/IPC/SharedMemory.cpp index af73ab4936..c3fa69a205 100644 --- a/Code/Framework/AzCore/AzCore/IPC/SharedMemory.cpp +++ b/Code/Framework/AzCore/AzCore/IPC/SharedMemory.cpp @@ -13,23 +13,18 @@ #include -namespace AZ +namespace AZ::Internal { - namespace Internal + struct RingData { - struct RingData - { - AZ::u32 m_readOffset; - AZ::u32 m_writeOffset; - AZ::u32 m_startOffset; - AZ::u32 m_endOffset; - AZ::u32 m_dataToRead; - AZ::u8 m_pad[32 - sizeof(AZStd::spin_mutex)]; - }; - } // namespace Internal -} // namespace AZ - - + AZ::u32 m_readOffset; + AZ::u32 m_writeOffset; + AZ::u32 m_startOffset; + AZ::u32 m_endOffset; + AZ::u32 m_dataToRead; + AZ::u8 m_pad[32 - sizeof(AZStd::spin_mutex)]; + }; +} // namespace AZ::Internal using namespace AZ; diff --git a/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.cpp b/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.cpp index c05590ca91..ce8455d9a1 100644 --- a/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.cpp +++ b/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -83,7 +84,7 @@ AZ_THREAD_LOCAL JobManagerWorkStealing::ThreadInfo* JobManagerWorkStealing::m_cu JobManagerWorkStealing::JobManagerWorkStealing(const JobManagerDesc& desc) : m_isAsynchronous(!desc.m_workerThreads.empty()) - , m_workerThreads(AZStd::move(CreateWorkerThreads(desc.m_workerThreads))) + , m_workerThreads(AZStd::move(CreateWorkerThreads(desc))) { //allow workers to begin processing after they have all been created, needed to wait since they may access each others queues m_initSemaphore.release(static_cast(desc.m_workerThreads.size())); @@ -618,8 +619,9 @@ JobManagerWorkStealing::ThreadInfo* JobManagerWorkStealing::FindCurrentThreadInf return info; } -JobManagerWorkStealing::ThreadList JobManagerWorkStealing::CreateWorkerThreads(const JobManagerDesc::DescList& workerDescList) +JobManagerWorkStealing::ThreadList JobManagerWorkStealing::CreateWorkerThreads(const JobManagerDesc& jmDesc) { + const JobManagerDesc::DescList& workerDescList = jmDesc.m_workerThreads; ThreadList workerThreads(workerDescList.size()); m_threads.reserve(workerDescList.size()); @@ -632,8 +634,12 @@ JobManagerWorkStealing::ThreadList JobManagerWorkStealing::CreateWorkerThreads(c info->m_owningManager = this; info->m_workerId = iThread; + AZStd::fixed_string<128> threadName = AZStd::fixed_string<128>::format( + "%s worker thread %d", + jmDesc.m_jobManagerName[0] != '\0' ? jmDesc.m_jobManagerName : "AZ JobManager", + iThread); AZStd::thread_desc threadDesc; - threadDesc.m_name = "AZ JobManager worker thread"; + threadDesc.m_name = threadName.c_str(); threadDesc.m_cpuId = desc.m_cpuId; threadDesc.m_priority = desc.m_priority; if (desc.m_stackSize != 0) diff --git a/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.h b/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.h index 55de872d86..734c166d6a 100644 --- a/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.h +++ b/Code/Framework/AzCore/AzCore/Jobs/Internal/JobManagerWorkStealing.h @@ -115,7 +115,7 @@ namespace AZ void ProcessJobsAssist(ThreadInfo* info, Job* suspendedJob, AZStd::atomic* notifyFlag); void ProcessJobsSynchronous(ThreadInfo* info, Job* suspendedJob, AZStd::atomic* notifyFlag); void ProcessJobsInternal(ThreadInfo* info, Job* suspendedJob, AZStd::atomic* notifyFlag); - ThreadList CreateWorkerThreads(const JobManagerDesc::DescList& workerDescList); + ThreadList CreateWorkerThreads(const JobManagerDesc& jmDesc); #ifndef AZ_MONOLITHIC_BUILD ThreadInfo* CrossModuleFindAndSetWorkerThreadInfo() const; #endif diff --git a/Code/Framework/AzCore/AzCore/Jobs/JobManagerComponent.cpp b/Code/Framework/AzCore/AzCore/Jobs/JobManagerComponent.cpp index 6f5ccc93e4..acf24e8d42 100644 --- a/Code/Framework/AzCore/AzCore/Jobs/JobManagerComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Jobs/JobManagerComponent.cpp @@ -51,16 +51,18 @@ namespace AZ JobManagerBus::Handler::BusConnect(); JobManagerDesc desc; + desc.m_jobManagerName = "Default JobManager"; JobManagerThreadDesc threadDesc; int numberOfWorkerThreads = m_numberOfWorkerThreads; if (numberOfWorkerThreads <= 0) // spawn default number of threads { + #if (AZ_TRAIT_THREAD_NUM_JOB_MANAGER_WORKER_THREADS) + numberOfWorkerThreads = AZ_TRAIT_THREAD_NUM_JOB_MANAGER_WORKER_THREADS; + #else uint32_t scaledHardwareThreads = Threading::CalcNumWorkerThreads(cl_jobThreadsConcurrencyRatio, cl_jobThreadsMinNumber, cl_jobThreadsNumReserved); numberOfWorkerThreads = AZ::GetMin(static_cast(desc.m_workerThreads.capacity()), scaledHardwareThreads); - #if (AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS) - numberOfWorkerThreads = AZ::GetMin(numberOfWorkerThreads, AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS); - #endif // (AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS) + #endif // (AZ_TRAIT_THREAD_NUM_JOB_MANAGER_WORKER_THREADS) } threadDesc.m_cpuId = AFFINITY_MASK_USERTHREADS; diff --git a/Code/Framework/AzCore/AzCore/Jobs/JobManagerDesc.h b/Code/Framework/AzCore/AzCore/Jobs/JobManagerDesc.h index 94f84f27b3..b2156291bc 100644 --- a/Code/Framework/AzCore/AzCore/Jobs/JobManagerDesc.h +++ b/Code/Framework/AzCore/AzCore/Jobs/JobManagerDesc.h @@ -51,6 +51,8 @@ namespace AZ { JobManagerDesc() {} + const char* m_jobManagerName = ""; + using DescList = AZStd::fixed_vector; DescList m_workerThreads; ///< List of worker threads to create }; diff --git a/Code/Framework/AzCore/AzCore/Math/Geometry2DUtils.cpp b/Code/Framework/AzCore/AzCore/Math/Geometry2DUtils.cpp index 47a7e0a2db..912794a518 100644 --- a/Code/Framework/AzCore/AzCore/Math/Geometry2DUtils.cpp +++ b/Code/Framework/AzCore/AzCore/Math/Geometry2DUtils.cpp @@ -8,138 +8,135 @@ #include -namespace AZ +namespace AZ::Geometry2DUtils { - namespace Geometry2DUtils + float ShortestDistanceSqPointSegment(const Vector2& point, const Vector2& segmentStart, const Vector2& segmentEnd, + float epsilon) { - float ShortestDistanceSqPointSegment(const Vector2& point, const Vector2& segmentStart, const Vector2& segmentEnd, - float epsilon) + const AZ::Vector2 segmentVector = segmentEnd - segmentStart; + + // check if the line degenerates to a point + const float segmentLengthSq = segmentVector.GetLengthSq(); + if (segmentLengthSq < epsilon * epsilon) { - const AZ::Vector2 segmentVector = segmentEnd - segmentStart; - - // check if the line degenerates to a point - const float segmentLengthSq = segmentVector.GetLengthSq(); - if (segmentLengthSq < epsilon * epsilon) - { - return (point - segmentStart).GetLengthSq(); - } - - // if the point projects on to the line segment then the shortest distance is the perpendicular - const float projection = (point - segmentStart).Dot(segmentVector); - if (projection >= 0.0f && projection <= segmentLengthSq) - { - const Vector2 perpendicular = (point - segmentStart - projection / segmentLengthSq * segmentVector); - return perpendicular.GetLengthSq(); - } - - // otherwise the point must be closest to one of the end points of the segment - return GetMin( - (point - segmentStart).GetLengthSq(), - (point - segmentEnd).GetLengthSq()); + return (point - segmentStart).GetLengthSq(); } - float Signed2DTriangleArea(const Vector2& a, const Vector2& b, const Vector2& c) + // if the point projects on to the line segment then the shortest distance is the perpendicular + const float projection = (point - segmentStart).Dot(segmentVector); + if (projection >= 0.0f && projection <= segmentLengthSq) { - return 0.5f * ((a.GetX() - c.GetX()) * (b.GetY() - c.GetY()) - (a.GetY() - c.GetY()) * (b.GetX() - c.GetX())); + const Vector2 perpendicular = (point - segmentStart - projection / segmentLengthSq * segmentVector); + return perpendicular.GetLengthSq(); } - float ShortestDistanceSqSegmentSegment( - const Vector2& segment1Start, const Vector2& segment1End, - const Vector2& segment2Start, const Vector2& segment2End) + // otherwise the point must be closest to one of the end points of the segment + return GetMin( + (point - segmentStart).GetLengthSq(), + (point - segmentEnd).GetLengthSq()); + } + + float Signed2DTriangleArea(const Vector2& a, const Vector2& b, const Vector2& c) + { + return 0.5f * ((a.GetX() - c.GetX()) * (b.GetY() - c.GetY()) - (a.GetY() - c.GetY()) * (b.GetX() - c.GetX())); + } + + float ShortestDistanceSqSegmentSegment( + const Vector2& segment1Start, const Vector2& segment1End, + const Vector2& segment2Start, const Vector2& segment2End) + { + // if the segments cross, then the distance is zero + + // if the two ends of segment 2 are on different sides of segment 1, then these two triangles will have + // different winding orders (see Real-Time Collision Detection, Christer Ericson, ISBN 978-1558607323, + // Chapter 5.1.9.1) + const float area1 = Signed2DTriangleArea(segment1Start, segment1End, segment2End); + const float area2 = Signed2DTriangleArea(segment1Start, segment1End, segment2Start); + if (area1 * area2 < 0.0f) { - // if the segments cross, then the distance is zero - - // if the two ends of segment 2 are on different sides of segment 1, then these two triangles will have - // different winding orders (see Real-Time Collision Detection, Christer Ericson, ISBN 978-1558607323, - // Chapter 5.1.9.1) - const float area1 = Signed2DTriangleArea(segment1Start, segment1End, segment2End); - const float area2 = Signed2DTriangleArea(segment1Start, segment1End, segment2Start); - if (area1 * area2 < 0.0f) + // similarly we can check if the two ends of segment 1 are on different sides of segment 2 + const float area3 = Signed2DTriangleArea(segment2Start, segment2End, segment1Start); + const float area4 = area3 + area2 - area1; + if (area3 * area4 < 0.0f) { - // similarly we can check if the two ends of segment 1 are on different sides of segment 2 - const float area3 = Signed2DTriangleArea(segment2Start, segment2End, segment1Start); - const float area4 = area3 + area2 - area1; - if (area3 * area4 < 0.0f) - { - return 0.0f; - } + return 0.0f; } - - // otherwise the shortest distance must be between one of the segment end points and the other segment - return GetMin( - GetMin( - ShortestDistanceSqPointSegment(segment1Start, segment2Start, segment2End), - ShortestDistanceSqPointSegment(segment1End, segment2Start, segment2End)), - GetMin( - ShortestDistanceSqPointSegment(segment2Start, segment1Start, segment1End), - ShortestDistanceSqPointSegment(segment2End, segment1Start, segment1End)) - ); } - bool IsSimplePolygon(const AZStd::vector& vertices, float epsilon) + // otherwise the shortest distance must be between one of the segment end points and the other segment + return GetMin( + GetMin( + ShortestDistanceSqPointSegment(segment1Start, segment2Start, segment2End), + ShortestDistanceSqPointSegment(segment1End, segment2Start, segment2End)), + GetMin( + ShortestDistanceSqPointSegment(segment2Start, segment1Start, segment1End), + ShortestDistanceSqPointSegment(segment2End, segment1Start, segment1End)) + ); + } + + bool IsSimplePolygon(const AZStd::vector& vertices, float epsilon) + { + // note that this implementation is quadratic in the number of vertices + // if it becomes a bottleneck, there are approaches which are O(n log n), e.g. the Bentley-Ottmann algorithm + + const size_t vertexCount = vertices.size(); + + if (vertexCount < 3) { - // note that this implementation is quadratic in the number of vertices - // if it becomes a bottleneck, there are approaches which are O(n log n), e.g. the Bentley-Ottmann algorithm - - const size_t vertexCount = vertices.size(); - - if (vertexCount < 3) - { - return false; - } - - if (vertexCount == 3) - { - return true; - } - - const float epsilonSq = epsilon * epsilon; - - for (size_t i = 0; i < vertexCount; ++i) - { - // make it easy to nicely wrap indices - const size_t safeIndex = i + vertexCount; - - const size_t endIndex = (safeIndex - 1) % vertexCount; - const size_t beginIndex = (safeIndex + 2) % vertexCount; - - for (size_t j = beginIndex; j != endIndex; j = (j + 1) % vertexCount) - { - const float distSq = ShortestDistanceSqSegmentSegment( - vertices[i], - vertices[(i + 1) % vertexCount], - vertices[j], - vertices[(j + 1) % vertexCount] - ); - - if (distSq < epsilonSq) - { - return false; - } - } - } + return false; + } + if (vertexCount == 3) + { return true; } - bool IsConvex(const AZStd::vector& vertices) + const float epsilonSq = epsilon * epsilon; + + for (size_t i = 0; i < vertexCount; ++i) { - const size_t vertexCount = vertices.size(); + // make it easy to nicely wrap indices + const size_t safeIndex = i + vertexCount; - if (vertexCount < 3) - { - return false; - } + const size_t endIndex = (safeIndex - 1) % vertexCount; + const size_t beginIndex = (safeIndex + 2) % vertexCount; - for (size_t i = 0; i < vertexCount; ++i) + for (size_t j = beginIndex; j != endIndex; j = (j + 1) % vertexCount) { - if (Signed2DTriangleArea(vertices[i], vertices[(i + 1) % vertexCount], vertices[(i + 2) % vertexCount]) < 0.0f) + const float distSq = ShortestDistanceSqSegmentSegment( + vertices[i], + vertices[(i + 1) % vertexCount], + vertices[j], + vertices[(j + 1) % vertexCount] + ); + + if (distSq < epsilonSq) { return false; } } - - return true; } - } // namespace Geometry2DUtils -} // namespace AZ + + return true; + } + + bool IsConvex(const AZStd::vector& vertices) + { + const size_t vertexCount = vertices.size(); + + if (vertexCount < 3) + { + return false; + } + + for (size_t i = 0; i < vertexCount; ++i) + { + if (Signed2DTriangleArea(vertices[i], vertices[(i + 1) % vertexCount], vertices[(i + 2) % vertexCount]) < 0.0f) + { + return false; + } + } + + return true; + } +} // namespace AZ::Geometry2DUtils diff --git a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.cpp b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.cpp index 6d79c4c068..78da9e76d2 100644 --- a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.cpp +++ b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.cpp @@ -351,7 +351,8 @@ namespace AZ ->Method("CreateFromMatrix3x3AndTranslation", &Matrix3x4::CreateFromMatrix3x3AndTranslation) ->Method("CreateScale", &Matrix3x4::CreateScale) ->Method("CreateDiagonal", &Matrix3x4::CreateDiagonal) - ->Method("CreateTranslation", &Matrix3x4::CreateTranslation); + ->Method("CreateTranslation", &Matrix3x4::CreateTranslation) + ->Method("UnsafeCreateFromMatrix4x4", &Matrix3x4::UnsafeCreateFromMatrix4x4); } } diff --git a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.h b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.h index 60a88df686..25653627ea 100644 --- a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.h +++ b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.h @@ -90,6 +90,9 @@ namespace AZ //! Constructs from a Matrix3x3 and a translation. static Matrix3x4 CreateFromMatrix3x3AndTranslation(const Matrix3x3& matrix3x3, const Vector3& translation); + //! Constructs from a Matrix4x4. + static Matrix3x4 UnsafeCreateFromMatrix4x4(const Matrix4x4& matrix4x4); + //! Constructs from a Transform. static Matrix3x4 CreateFromTransform(const Transform& transform); @@ -227,7 +230,7 @@ namespace AZ Matrix3x4& operator+=(const Matrix3x4& rhs); //! @} - //! Operator for matrix-matrix substraction. + //! Operator for matrix-matrix subtraction. //! @{ [[nodiscard]] Matrix3x4 operator-(const Matrix3x4& rhs) const; Matrix3x4& operator-=(const Matrix3x4& rhs); @@ -266,6 +269,9 @@ namespace AZ //! Post-multiplies the matrix by a vector, using only the 3x3 part of the matrix. [[nodiscard]] Vector3 TransformVector(const Vector3& rhs) const; + //! Post-multiplies the matrix by a point, using the rotation and translation part of the matrix. + [[nodiscard]] Vector3 TransformPoint(const Vector3& rhs) const; + //! Gets the result of transposing the 3x3 part of the matrix, setting the translation part to zero. [[nodiscard]] Matrix3x4 GetTranspose() const; diff --git a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.inl b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.inl index 2f127221ab..d1367cd26c 100644 --- a/Code/Framework/AzCore/AzCore/Math/Matrix3x4.inl +++ b/Code/Framework/AzCore/AzCore/Math/Matrix3x4.inl @@ -203,6 +203,16 @@ namespace AZ } + AZ_MATH_INLINE Matrix3x4 Matrix3x4::UnsafeCreateFromMatrix4x4(const Matrix4x4& matrix4x4) + { + Matrix3x4 result; + result.SetRow(0, matrix4x4.GetRow(0)); + result.SetRow(1, matrix4x4.GetRow(1)); + result.SetRow(2, matrix4x4.GetRow(2)); + return result; + } + + AZ_MATH_INLINE Matrix3x4 Matrix3x4::CreateScale(const Vector3& scale) { return CreateDiagonal(scale); @@ -609,6 +619,12 @@ namespace AZ } + AZ_MATH_INLINE Vector3 Matrix3x4::TransformPoint(const Vector3& rhs) const + { + return Multiply3x3(rhs) + GetTranslation(); + } + + AZ_MATH_INLINE Matrix3x4 Matrix3x4::GetTranspose() const { Matrix3x4 result; diff --git a/Code/Framework/AzCore/AzCore/Math/Sfmt.cpp b/Code/Framework/AzCore/AzCore/Math/Sfmt.cpp index 66404a9b3f..5f4dc9e5df 100644 --- a/Code/Framework/AzCore/AzCore/Math/Sfmt.cpp +++ b/Code/Framework/AzCore/AzCore/Math/Sfmt.cpp @@ -14,28 +14,26 @@ #include // for memset -namespace AZ +namespace AZ::SfmtInternal { - namespace SfmtInternal - { - static const int N32 = N * 4; - static const int N64 = N * 2; - static const int POS1 = 122; - static const int SL1 = 18; - static const int SR1 = 11; - static const int SL2 = 1; - static const int SR2 = 1; - static const unsigned int MSK1 = 0xdfffffefU; - static const unsigned int MSK2 = 0xddfecb7fU; - static const unsigned int MSK3 = 0xbffaffffU; - static const unsigned int MSK4 = 0xbffffff6U; - static const unsigned int PARITY1 = 0x00000001U; - static const unsigned int PARITY2 = 0x00000000U; - static const unsigned int PARITY3 = 0x00000000U; - static const unsigned int PARITY4 = 0x13c9e684U; + static const int N32 = N * 4; + static const int N64 = N * 2; + static const int POS1 = 122; + static const int SL1 = 18; + static const int SR1 = 11; + static const int SL2 = 1; + static const int SR2 = 1; + static const unsigned int MSK1 = 0xdfffffefU; + static const unsigned int MSK2 = 0xddfecb7fU; + static const unsigned int MSK3 = 0xbffaffffU; + static const unsigned int MSK4 = 0xbffffff6U; + static const unsigned int PARITY1 = 0x00000001U; + static const unsigned int PARITY2 = 0x00000000U; + static const unsigned int PARITY3 = 0x00000000U; + static const unsigned int PARITY4 = 0x13c9e684U; - /** a parity check vector which certificate the period of 2^{MEXP} */ - static unsigned int parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4}; + /** a parity check vector which certificate the period of 2^{MEXP} */ + static unsigned int parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4}; #ifdef ONLY64 # define idxof(_i) (_i ^ 1) @@ -45,259 +43,257 @@ namespace AZ #if AZ_TRAIT_USE_PLATFORM_SIMD_SSE - /** - * This function represents the recursion formula. - * @param a a 128-bit part of the internal state array - * @param b a 128-bit part of the internal state array - * @param c a 128-bit part of the internal state array - * @param d a 128-bit part of the internal state array - * @param mask 128-bit mask - * @return output - */ - AZ_FORCE_INLINE static Simd::Vec4::Int32Type simd_recursion(Simd::Vec4::Int32Type* a, Simd::Vec4::Int32Type* b, Simd::Vec4::Int32Type c, Simd::Vec4::Int32Type d, Simd::Vec4::Int32Type mask) + /** + * This function represents the recursion formula. + * @param a a 128-bit part of the internal state array + * @param b a 128-bit part of the internal state array + * @param c a 128-bit part of the internal state array + * @param d a 128-bit part of the internal state array + * @param mask 128-bit mask + * @return output + */ + AZ_FORCE_INLINE static Simd::Vec4::Int32Type simd_recursion(Simd::Vec4::Int32Type* a, Simd::Vec4::Int32Type* b, Simd::Vec4::Int32Type c, Simd::Vec4::Int32Type d, Simd::Vec4::Int32Type mask) + { + Simd::Vec4::Int32Type v, x, y, z; + x = *a; + y = _mm_srli_epi32(*b, SR1); + z = _mm_srli_si128(c, SR2); + v = _mm_slli_epi32(d, SL1); + z = Simd::Vec4::Xor(z, x); + z = Simd::Vec4::Xor(z, v); + x = _mm_slli_si128(x, SL2); + y = Simd::Vec4::And(y, mask); + z = Simd::Vec4::Xor(z, x); + z = Simd::Vec4::Xor(z, y); + return z; + } + + /** + * This function fills the internal state array with pseudorandom + * integers. + */ + inline void gen_rand_all(Sfmt& g) + { + int i; + Simd::Vec4::Int32Type r, r1, r2, mask; + mask = Simd::Vec4::LoadImmediate((int32_t)MSK4, (int32_t)MSK3, (int32_t)MSK2, (int32_t)MSK1); + + r1 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 2].si); + r2 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { - Simd::Vec4::Int32Type v, x, y, z; - x = *a; - y = _mm_srli_epi32(*b, SR1); - z = _mm_srli_si128(c, SR2); - v = _mm_slli_epi32(d, SL1); - z = Simd::Vec4::Xor(z, x); - z = Simd::Vec4::Xor(z, v); - x = _mm_slli_si128(x, SL2); - y = Simd::Vec4::And(y, mask); - z = Simd::Vec4::Xor(z, x); - z = Simd::Vec4::Xor(z, y); - return z; + r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[i].si, r); + r1 = r2; + r2 = r; } - - /** - * This function fills the internal state array with pseudorandom - * integers. - */ - inline void gen_rand_all(Sfmt& g) + for (; i < N; i++) { - int i; - Simd::Vec4::Int32Type r, r1, r2, mask; - mask = Simd::Vec4::LoadImmediate((int32_t)MSK4, (int32_t)MSK3, (int32_t)MSK2, (int32_t)MSK1); - - r1 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 2].si); - r2 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 1].si); - for (i = 0; i < N - POS1; i++) - { - r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[i].si, r); - r1 = r2; - r2 = r; - } - for (; i < N; i++) - { - r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1 - N].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[i].si, r); - r1 = r2; - r2 = r; - } + r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1 - N].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[i].si, r); + r1 = r2; + r2 = r; } + } - /** - * This function fills the user-specified array with pseudorandom - * integers. - * - * @param array an 128-bit array to be filled by pseudorandom numbers. - * @param size number of 128-bit pesudorandom numbers to be generated. - */ - inline void gen_rand_array(Sfmt& g, w128_t* array, int size) + /** + * This function fills the user-specified array with pseudorandom + * integers. + * + * @param array an 128-bit array to be filled by pseudorandom numbers. + * @param size number of 128-bit pesudorandom numbers to be generated. + */ + inline void gen_rand_array(Sfmt& g, w128_t* array, int size) + { + int i, j; + Simd::Vec4::Int32Type r, r1, r2, mask; + mask = Simd::Vec4::LoadImmediate((int32_t)MSK4, (int32_t)MSK3, (int32_t)MSK2, (int32_t)MSK1); + + r1 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 2].si); + r2 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { - int i, j; - Simd::Vec4::Int32Type r, r1, r2, mask; - mask = Simd::Vec4::LoadImmediate((int32_t)MSK4, (int32_t)MSK3, (int32_t)MSK2, (int32_t)MSK1); - - r1 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 2].si); - r2 = Simd::Vec4::LoadAligned((const int32_t*)&g.m_sfmt[N - 1].si); - for (i = 0; i < N - POS1; i++) - { - r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); - r1 = r2; - r2 = r; - } - for (; i < N; i++) - { - r = simd_recursion(&g.m_sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); - r1 = r2; - r2 = r; - } - /* main loop */ - for (; i < size - N; i++) - { - r = simd_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); - r1 = r2; - r2 = r; - } - for (j = 0; j < 2 * N - size; j++) - { - r = Simd::Vec4::LoadAligned((const int32_t*)&array[j + size - N].si); - Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[j].si, r); - } - for (; i < size; i++) - { - r = simd_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask); - Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); - Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[j++].si, r); - r1 = r2; - r2 = r; - } + r = simd_recursion(&g.m_sfmt[i].si, &g.m_sfmt[i + POS1].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); + r1 = r2; + r2 = r; } + for (; i < N; i++) + { + r = simd_recursion(&g.m_sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); + r1 = r2; + r2 = r; + } + /* main loop */ + for (; i < size - N; i++) + { + r = simd_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); + r1 = r2; + r2 = r; + } + for (j = 0; j < 2 * N - size; j++) + { + r = Simd::Vec4::LoadAligned((const int32_t*)&array[j + size - N].si); + Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[j].si, r); + } + for (; i < size; i++) + { + r = simd_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask); + Simd::Vec4::StoreAligned((int32_t*)&array[i].si, r); + Simd::Vec4::StoreAligned((int32_t*)&g.m_sfmt[j++].si, r); + r1 = r2; + r2 = r; + } + } #else - inline void rshift128(w128_t* out, w128_t const* in, int shift) + inline void rshift128(w128_t* out, w128_t const* in, int shift) + { + AZ::u64 th, tl, oh, ol; + #ifdef ONLY64 + th = ((AZ::u64)in->u[2] << 32) | ((AZ::u64)in->u[3]); + tl = ((AZ::u64)in->u[0] << 32) | ((AZ::u64)in->u[1]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[0] = (AZ::u32)(ol >> 32); + out->u[1] = (AZ::u32)ol; + out->u[2] = (AZ::u32)(oh >> 32); + out->u[3] = (AZ::u32)oh; +#else + th = ((AZ::u64)in->u[3] << 32) | ((AZ::u64)in->u[2]); + tl = ((AZ::u64)in->u[1] << 32) | ((AZ::u64)in->u[0]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[1] = (AZ::u32)(ol >> 32); + out->u[0] = (AZ::u32)ol; + out->u[3] = (AZ::u32)(oh >> 32); + out->u[2] = (AZ::u32)oh; +#endif + } + + inline void lshift128(w128_t* out, w128_t const* in, int shift) + { + AZ::u64 th, tl, oh, ol; +#ifdef ONLY64 + th = ((AZ::u64)in->u[2] << 32) | ((AZ::u64)in->u[3]); + tl = ((AZ::u64)in->u[0] << 32) | ((AZ::u64)in->u[1]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[0] = (AZ::u32)(ol >> 32); + out->u[1] = (AZ::u32)ol; + out->u[2] = (AZ::u32)(oh >> 32); + out->u[3] = (AZ::u32)oh; +#else + th = ((AZ::u64)in->u[3] << 32) | ((AZ::u64)in->u[2]); + tl = ((AZ::u64)in->u[1] << 32) | ((AZ::u64)in->u[0]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[1] = (AZ::u32)(ol >> 32); + out->u[0] = (AZ::u32)ol; + out->u[3] = (AZ::u32)(oh >> 32); + out->u[2] = (AZ::u32)oh; +#endif + } + + inline void do_recursion(w128_t* r, w128_t* a, w128_t* b, w128_t* c, w128_t* d) + { + w128_t x; + w128_t y; + lshift128(&x, a, SL2); + rshift128(&y, c, SR2); +#ifdef ONLY64 + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] ^ (d->u[3] << SL1); +#else + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] ^ (d->u[3] << SL1); +#endif + } + /** + * This function fills the internal state array with pseudorandom + * integers. + */ + inline void gen_rand_all(Sfmt& g) + { + int i; + w128_t* r1, * r2; + + r1 = &g.m_sfmt[N - 2]; + r2 = &g.m_sfmt[N - 1]; + for (i = 0; i < N - POS1; i++) { - AZ::u64 th, tl, oh, ol; - #ifdef ONLY64 - th = ((AZ::u64)in->u[2] << 32) | ((AZ::u64)in->u[3]); - tl = ((AZ::u64)in->u[0] << 32) | ((AZ::u64)in->u[1]); - - oh = th >> (shift * 8); - ol = tl >> (shift * 8); - ol |= th << (64 - shift * 8); - out->u[0] = (AZ::u32)(ol >> 32); - out->u[1] = (AZ::u32)ol; - out->u[2] = (AZ::u32)(oh >> 32); - out->u[3] = (AZ::u32)oh; - #else - th = ((AZ::u64)in->u[3] << 32) | ((AZ::u64)in->u[2]); - tl = ((AZ::u64)in->u[1] << 32) | ((AZ::u64)in->u[0]); - - oh = th >> (shift * 8); - ol = tl >> (shift * 8); - ol |= th << (64 - shift * 8); - out->u[1] = (AZ::u32)(ol >> 32); - out->u[0] = (AZ::u32)ol; - out->u[3] = (AZ::u32)(oh >> 32); - out->u[2] = (AZ::u32)oh; - #endif + do_recursion(&g.m_sfmt[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1], r1, r2); + r1 = r2; + r2 = &g.m_sfmt[i]; } - - inline void lshift128(w128_t* out, w128_t const* in, int shift) + for (; i < N; i++) { - AZ::u64 th, tl, oh, ol; - #ifdef ONLY64 - th = ((AZ::u64)in->u[2] << 32) | ((AZ::u64)in->u[3]); - tl = ((AZ::u64)in->u[0] << 32) | ((AZ::u64)in->u[1]); - - oh = th << (shift * 8); - ol = tl << (shift * 8); - oh |= tl >> (64 - shift * 8); - out->u[0] = (AZ::u32)(ol >> 32); - out->u[1] = (AZ::u32)ol; - out->u[2] = (AZ::u32)(oh >> 32); - out->u[3] = (AZ::u32)oh; - #else - th = ((AZ::u64)in->u[3] << 32) | ((AZ::u64)in->u[2]); - tl = ((AZ::u64)in->u[1] << 32) | ((AZ::u64)in->u[0]); - - oh = th << (shift * 8); - ol = tl << (shift * 8); - oh |= tl >> (64 - shift * 8); - out->u[1] = (AZ::u32)(ol >> 32); - out->u[0] = (AZ::u32)ol; - out->u[3] = (AZ::u32)(oh >> 32); - out->u[2] = (AZ::u32)oh; - #endif + do_recursion(&g.m_sfmt[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &g.m_sfmt[i]; } + } - inline void do_recursion(w128_t* r, w128_t* a, w128_t* b, w128_t* c, w128_t* d) + /** + * This function fills the user-specified array with pseudorandom + * integers. + * + * @param array an 128-bit array to be filled by pseudorandom numbers. + * @param size number of 128-bit pseudorandom numbers to be generated. + */ + inline void gen_rand_array(Sfmt& g, w128_t* array, int size) + { + int i, j; + w128_t* r1, * r2; + + r1 = &g.m_sfmt[N - 2]; + r2 = &g.m_sfmt[N - 1]; + for (i = 0; i < N - POS1; i++) { - w128_t x; - w128_t y; - lshift128(&x, a, SL2); - rshift128(&y, c, SR2); - #ifdef ONLY64 - r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] ^ (d->u[0] << SL1); - r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] ^ (d->u[1] << SL1); - r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] ^ (d->u[2] << SL1); - r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] ^ (d->u[3] << SL1); - #else - r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] ^ (d->u[0] << SL1); - r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] ^ (d->u[1] << SL1); - r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] ^ (d->u[2] << SL1); - r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] ^ (d->u[3] << SL1); - #endif + do_recursion(&array[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1], r1, r2); + r1 = r2; + r2 = &array[i]; } - /** - * This function fills the internal state array with pseudorandom - * integers. - */ - inline void gen_rand_all(Sfmt& g) + for (; i < N; i++) { - int i; - w128_t* r1, * r2; - - r1 = &g.m_sfmt[N - 2]; - r2 = &g.m_sfmt[N - 1]; - for (i = 0; i < N - POS1; i++) - { - do_recursion(&g.m_sfmt[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1], r1, r2); - r1 = r2; - r2 = &g.m_sfmt[i]; - } - for (; i < N; i++) - { - do_recursion(&g.m_sfmt[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1 - N], r1, r2); - r1 = r2; - r2 = &g.m_sfmt[i]; - } + do_recursion(&array[i], &g.m_sfmt[i], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; } - - /** - * This function fills the user-specified array with pseudorandom - * integers. - * - * @param array an 128-bit array to be filled by pseudorandom numbers. - * @param size number of 128-bit pseudorandom numbers to be generated. - */ - inline void gen_rand_array(Sfmt& g, w128_t* array, int size) + for (; i < size - N; i++) { - int i, j; - w128_t* r1, * r2; - - r1 = &g.m_sfmt[N - 2]; - r2 = &g.m_sfmt[N - 1]; - for (i = 0; i < N - POS1; i++) - { - do_recursion(&array[i], &g.m_sfmt[i], &g.m_sfmt[i + POS1], r1, r2); - r1 = r2; - r2 = &array[i]; - } - for (; i < N; i++) - { - do_recursion(&array[i], &g.m_sfmt[i], &array[i + POS1 - N], r1, r2); - r1 = r2; - r2 = &array[i]; - } - for (; i < size - N; i++) - { - do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); - r1 = r2; - r2 = &array[i]; - } - for (j = 0; j < 2 * N - size; j++) - { - g.m_sfmt[j] = array[j + size - N]; - } - for (; i < size; i++, j++) - { - do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); - r1 = r2; - r2 = &array[i]; - g.m_sfmt[j] = array[i]; - } + do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; } + for (j = 0; j < 2 * N - size; j++) + { + g.m_sfmt[j] = array[j + size - N]; + } + for (; i < size; i++, j++) + { + do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; + g.m_sfmt[j] = array[i]; + } + } #endif - } // SmftInternal -} // AZ - +} // namespace AZ::SfmtInternal using namespace AZ; diff --git a/Code/Framework/AzCore/AzCore/Math/Uuid.h b/Code/Framework/AzCore/AzCore/Math/Uuid.h index 6b77e7ec3e..eeacb04877 100644 --- a/Code/Framework/AzCore/AzCore/Math/Uuid.h +++ b/Code/Framework/AzCore/AzCore/Math/Uuid.h @@ -45,7 +45,7 @@ namespace AZ static constexpr int ValidUuidStringLength = 32; /// Number of characters (data only, no extra formatting) in a valid UUID string static const size_t MaxStringBuffer = 39; /// 32 Uuid + 4 dashes + 2 brackets + 1 terminate - Uuid() {} + Uuid() = default; Uuid(const char* string, size_t stringLength = 0) { *this = CreateString(string, stringLength); } static Uuid CreateNull(); diff --git a/Code/Framework/AzCore/AzCore/Math/Vector2.h b/Code/Framework/AzCore/AzCore/Math/Vector2.h index 7c37d74135..91eb61d6c8 100644 --- a/Code/Framework/AzCore/AzCore/Math/Vector2.h +++ b/Code/Framework/AzCore/AzCore/Math/Vector2.h @@ -30,7 +30,7 @@ namespace AZ Vector2() = default; - Vector2(const Vector2& v); + Vector2(const Vector2& v) = default; //! Constructs vector with all components set to the same specified value. explicit Vector2(float x); diff --git a/Code/Framework/AzCore/AzCore/Math/Vector2.inl b/Code/Framework/AzCore/AzCore/Math/Vector2.inl index 086be2bbc3..9691dd3a5c 100644 --- a/Code/Framework/AzCore/AzCore/Math/Vector2.inl +++ b/Code/Framework/AzCore/AzCore/Math/Vector2.inl @@ -8,13 +8,6 @@ namespace AZ { - AZ_MATH_INLINE Vector2::Vector2(const Vector2& v) - : m_value(v.m_value) - { - ; - } - - AZ_MATH_INLINE Vector2::Vector2(float x) : m_value(Simd::Vec2::Splat(x)) { diff --git a/Code/Framework/AzCore/AzCore/Math/Vector3.h b/Code/Framework/AzCore/AzCore/Math/Vector3.h index 821dc8292c..6b7c53266d 100644 --- a/Code/Framework/AzCore/AzCore/Math/Vector3.h +++ b/Code/Framework/AzCore/AzCore/Math/Vector3.h @@ -100,7 +100,7 @@ namespace AZ void Set(float x, float y, float z); //! Sets components from an array of 3 floats in xyz order. - void Set(float values[]); + void Set(const float values[]); //! Indexed access using operator(), just for convenience. float operator()(int32_t index) const; diff --git a/Code/Framework/AzCore/AzCore/Math/Vector3.inl b/Code/Framework/AzCore/AzCore/Math/Vector3.inl index 879ade38cf..6371c688b8 100644 --- a/Code/Framework/AzCore/AzCore/Math/Vector3.inl +++ b/Code/Framework/AzCore/AzCore/Math/Vector3.inl @@ -186,7 +186,7 @@ namespace AZ } - AZ_MATH_INLINE void Vector3::Set(float values[]) + AZ_MATH_INLINE void Vector3::Set(const float values[]) { m_value = Simd::Vec3::LoadImmediate(values[0], values[1], values[2]); } diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp index 3c09b4cae6..44ce08ebd9 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp @@ -20,44 +20,42 @@ #include #include -using namespace AZ; - #if !defined(RELEASE) && !defined(AZCORE_MEMORY_ENABLE_OVERRIDES) # define AZCORE_MEMORY_ENABLE_OVERRIDES #endif -namespace AZ +namespace AZ::Internal { - namespace Internal + struct AMStringHasher { - struct AMStringHasher + using is_transparent = void; + template + size_t operator()(const ConvertibleToStringView& key) { - using is_transparent = void; - template - size_t operator()(const ConvertibleToStringView& key) - { - return AZStd::hash{}(key); - } - }; - using AMString = AZStd::basic_string, AZStdIAllocator>; - using AllocatorNameMap = AZStd::unordered_map, AZStdIAllocator>; - using AllocatorRemappings = AZStd::unordered_map, AZStdIAllocator>; + return AZStd::hash{}(key); + } + }; + using AMString = AZStd::basic_string, AZStdIAllocator>; + using AllocatorNameMap = AZStd::unordered_map, AZStdIAllocator>; + using AllocatorRemappings = AZStd::unordered_map, AZStdIAllocator>; - // For allocators that are created before we have an environment, we keep some module-local data for them so that we can register them - // properly once the environment is attached. - struct PreEnvironmentAttachData - { - static const int MAX_UNREGISTERED_ALLOCATORS = 8; - AZStd::mutex m_mutex; - MallocSchema m_mallocSchema; - IAllocator* m_unregisteredAllocators[MAX_UNREGISTERED_ALLOCATORS]; - int m_unregisteredAllocatorCount = 0; - }; + // For allocators that are created before we have an environment, we keep some module-local data for them so that we can register them + // properly once the environment is attached. + struct PreEnvironmentAttachData + { + static const int MAX_UNREGISTERED_ALLOCATORS = 8; + AZStd::mutex m_mutex; + MallocSchema m_mallocSchema; + IAllocator* m_unregisteredAllocators[MAX_UNREGISTERED_ALLOCATORS]; + int m_unregisteredAllocatorCount = 0; + }; - } } -struct AZ::AllocatorManager::InternalData +namespace AZ +{ + +struct AllocatorManager::InternalData { explicit InternalData(const AZStdIAllocator& alloc) : m_allocatorMap(alloc) @@ -69,13 +67,13 @@ struct AZ::AllocatorManager::InternalData Internal::AllocatorRemappings m_remappingsReverse; }; -static AZ::EnvironmentVariable s_allocManager = nullptr; +static EnvironmentVariable s_allocManager = nullptr; static AllocatorManager* s_allocManagerDebug = nullptr; // For easier viewing in crash dumps /// Returns a module-local instance of data to use for allocators that are created before the environment is attached. -static AZ::Internal::PreEnvironmentAttachData& GetPreEnvironmentAttachData() +static Internal::PreEnvironmentAttachData& GetPreEnvironmentAttachData() { - static AZ::Internal::PreEnvironmentAttachData s_data; + static Internal::PreEnvironmentAttachData s_data; return s_data; } @@ -131,7 +129,7 @@ AllocatorManager& AllocatorManager::Instance() if (!s_allocManager) { AZ_Assert(Environment::IsReady(), "Environment must be ready before calling Instance()"); - s_allocManager = AZ::Environment::CreateVariable(AZ_CRC("AZ::AllocatorManager::s_allocManager", 0x6bdd908c)); + s_allocManager = Environment::CreateVariable(AZ_CRC_CE("AZ::AllocatorManager::s_allocManager")); // Register any allocators that were created in this module before we attached to the environment auto& data = GetPreEnvironmentAttachData(); @@ -156,9 +154,9 @@ AllocatorManager& AllocatorManager::Instance() ////////////////////////////////////////////////////////////////////////// // Create malloc schema using custom AZ_OS_MALLOC allocator. -AZ::MallocSchema* AllocatorManager::CreateMallocSchema() +MallocSchema* AllocatorManager::CreateMallocSchema() { - return static_cast(new(AZ_OS_MALLOC(sizeof(AZ::MallocSchema), alignof(AZ::MallocSchema))) AZ::MallocSchema()); + return static_cast(new(AZ_OS_MALLOC(sizeof(MallocSchema), alignof(MallocSchema))) MallocSchema()); } @@ -168,7 +166,7 @@ AZ::MallocSchema* AllocatorManager::CreateMallocSchema() //========================================================================= AllocatorManager::AllocatorManager() : m_profilingRefcount(0) - , m_mallocSchema(CreateMallocSchema(), [](AZ::MallocSchema* schema) + , m_mallocSchema(CreateMallocSchema(), [](MallocSchema* schema) { if (schema) { @@ -182,7 +180,7 @@ AllocatorManager::AllocatorManager() m_numAllocators = 0; m_isAllocatorLeaking = false; m_configurationFinalized = false; - m_defaultTrackingRecordMode = AZ::Debug::AllocationRecords::RECORD_NO_RECORDS; + m_defaultTrackingRecordMode = Debug::AllocationRecords::RECORD_NO_RECORDS; m_data = new (m_mallocSchema->Allocate(sizeof(InternalData), AZStd::alignment_of::value, 0)) InternalData(AZStdIAllocator(m_mallocSchema.get())); } @@ -411,12 +409,12 @@ AllocatorManager::RemoveOutOfMemoryListener() // [9/16/2011] //========================================================================= void -AllocatorManager::SetTrackingMode(AZ::Debug::AllocationRecords::Mode mode) +AllocatorManager::SetTrackingMode(Debug::AllocationRecords::Mode mode) { AZStd::lock_guard lock(m_allocatorListMutex); for (int i = 0; i < m_numAllocators; ++i) { - AZ::Debug::AllocationRecords* records = m_allocators[i]->GetRecords(); + Debug::AllocationRecords* records = m_allocators[i]->GetRecords(); if (records) { records->SetMode(mode); @@ -595,31 +593,31 @@ void AllocatorManager::GetAllocatorStats(size_t& allocatedBytes, size_t& capacit AZStd::lock_guard lock(m_allocatorListMutex); const int allocatorCount = GetNumAllocators(); - AZStd::unordered_map existingAllocators; - AZStd::unordered_map sourcesToAllocators; + AZStd::unordered_map existingAllocators; + AZStd::unordered_map sourcesToAllocators; // Build a mapping of original allocator sources to their allocators for (int i = 0; i < allocatorCount; ++i) { - AZ::IAllocator* allocator = GetAllocator(i); + IAllocator* allocator = GetAllocator(i); sourcesToAllocators.emplace(allocator->GetOriginalAllocationSource(), allocator); } for (int i = 0; i < allocatorCount; ++i) { - AZ::IAllocator* allocator = GetAllocator(i); - AZ::IAllocatorAllocate* source = allocator->GetAllocationSource(); - AZ::IAllocatorAllocate* originalSource = allocator->GetOriginalAllocationSource(); - AZ::IAllocatorAllocate* schema = allocator->GetSchema(); - AZ::IAllocator* alias = (source != originalSource) ? sourcesToAllocators[source] : nullptr; + IAllocator* allocator = GetAllocator(i); + IAllocatorAllocate* source = allocator->GetAllocationSource(); + IAllocatorAllocate* originalSource = allocator->GetOriginalAllocationSource(); + IAllocatorAllocate* schema = allocator->GetSchema(); + IAllocator* alias = (source != originalSource) ? sourcesToAllocators[source] : nullptr; if (schema && !alias) { // Check to see if this allocator's source maps to another allocator // Need to check both the schema and the allocator itself, as either one might be used as the alias depending on how it's implemented - AZStd::array checkAllocators = { { schema, allocator->GetAllocationSource() } }; + AZStd::array checkAllocators = { { schema, allocator->GetAllocationSource() } }; - for (AZ::IAllocatorAllocate* check : checkAllocators) + for (IAllocatorAllocate* check : checkAllocators) { auto existing = existingAllocators.emplace(check, allocator); @@ -631,7 +629,7 @@ void AllocatorManager::GetAllocatorStats(size_t& allocatedBytes, size_t& capacit } } - static const AZ::IAllocator* OS_ALLOCATOR = &AZ::AllocatorInstance::GetAllocator(); + static const IAllocator* OS_ALLOCATOR = &AllocatorInstance::GetAllocator(); size_t sourceAllocatedBytes = source->NumAllocatedBytes(); size_t sourceCapacityBytes = source->Capacity(); @@ -742,3 +740,5 @@ AllocatorManager::DebugBreak(void* address, const Debug::AllocationInfo& info) } } } + +} // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp index 154d59edd3..e4928e83c5 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp @@ -8,223 +8,220 @@ #include -namespace AZ +namespace AZ::Internal { - namespace Internal + AllocatorOverrideShim* AllocatorOverrideShim::Create(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) { - AllocatorOverrideShim* AllocatorOverrideShim::Create(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) + void* memory = shimAllocationSource->Allocate(sizeof(AllocatorOverrideShim), AZStd::alignment_of::value, 0); + auto result = new (memory) AllocatorOverrideShim(owningAllocator, shimAllocationSource); + return result; + } + + void AllocatorOverrideShim::Destroy(AllocatorOverrideShim* source) + { + auto shimAllocationSource = source->m_shimAllocationSource; + source->~AllocatorOverrideShim(); + shimAllocationSource->DeAllocate(source); + } + + AllocatorOverrideShim::AllocatorOverrideShim(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) + : m_owningAllocator(owningAllocator) + , m_source(owningAllocator->GetOriginalAllocationSource()) + , m_overridingSource(owningAllocator->GetOriginalAllocationSource()) + , m_shimAllocationSource(shimAllocationSource) + , m_records(typename AllocationSet::hasher(), typename AllocationSet::key_eq(), StdAllocationSrc(shimAllocationSource)) + { + } + + void AllocatorOverrideShim::SetOverride(IAllocatorAllocate* source) + { + m_overridingSource = source; + } + + IAllocatorAllocate* AllocatorOverrideShim::GetOverride() const + { + return m_overridingSource; + } + + bool AllocatorOverrideShim::IsOverridden() const + { + return m_source != m_overridingSource; + } + + bool AllocatorOverrideShim::HasOrphanedAllocations() const + { + return !m_records.empty(); + } + + void AllocatorOverrideShim::SetFinalizedConfiguration() + { + m_finalizedConfiguration = true; + } + + typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::Allocate(size_type byteSize, size_type alignment, int flags, const char* name, const char* fileName, int lineNum, unsigned int suppressStackRecord) + { + pointer_type ptr = m_overridingSource->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord); + + if (!IsOverridden()) { - void* memory = shimAllocationSource->Allocate(sizeof(AllocatorOverrideShim), AZStd::alignment_of::value, 0); - auto result = new (memory) AllocatorOverrideShim(owningAllocator, shimAllocationSource); - return result; + lock_type lock(m_mutex); + m_records.insert(ptr); // Record in case we need to orphan this allocation later } - void AllocatorOverrideShim::Destroy(AllocatorOverrideShim* source) + return ptr; + } + + void AllocatorOverrideShim::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment) + { + IAllocatorAllocate* source = m_overridingSource; + bool destroy = false; + { - auto shimAllocationSource = source->m_shimAllocationSource; - source->~AllocatorOverrideShim(); - shimAllocationSource->DeAllocate(source); + lock_type lock(m_mutex); + + // Check to see if this came from a prior allocation source + if (m_records.erase(ptr) && IsOverridden()) + { + source = m_source; + + if (m_records.empty() && m_finalizedConfiguration) + { + // All orphaned records are gone; we are no longer needed + m_owningAllocator->SetAllocationSource(m_overridingSource); + destroy = true; // Must destroy outside the lock + } + } } - AllocatorOverrideShim::AllocatorOverrideShim(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) - : m_owningAllocator(owningAllocator) - , m_source(owningAllocator->GetOriginalAllocationSource()) - , m_overridingSource(owningAllocator->GetOriginalAllocationSource()) - , m_shimAllocationSource(shimAllocationSource) - , m_records(typename AllocationSet::hasher(), typename AllocationSet::key_eq(), StdAllocationSrc(shimAllocationSource)) + source->DeAllocate(ptr, byteSize, alignment); + + if (destroy) { + Destroy(this); + } + } + + typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Resize(pointer_type ptr, size_type newSize) + { + IAllocatorAllocate* source = m_overridingSource; + + if (IsOverridden()) + { + // Determine who owns the allocation + lock_type lock(m_mutex); + + if (m_records.count(ptr)) + { + source = m_source; + } } - void AllocatorOverrideShim::SetOverride(IAllocatorAllocate* source) + size_t result = source->Resize(ptr, newSize); + + return result; + } + + typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) + { + pointer_type newPtr = nullptr; + bool useOverride = true; + bool destroy = false; + + if (IsOverridden()) { - m_overridingSource = source; + lock_type lock(m_mutex); + + if (m_records.erase(ptr)) + { + // An old allocation needs to be transferred to the new, overriding allocator. + useOverride = false; // We'll do the reallocation here + size_t oldSize = m_source->AllocationSize(ptr); + + if (newSize) + { + newPtr = m_overridingSource->Allocate(newSize, newAlignment, 0); + memcpy(newPtr, ptr, AZStd::min(newSize, oldSize)); + } + + m_source->DeAllocate(ptr, oldSize); + + if (m_records.empty() && m_finalizedConfiguration) + { + // All orphaned records are gone; we are no longer needed + m_owningAllocator->SetAllocationSource(m_overridingSource); + destroy = true; // Must destroy outside the lock + } + } } - IAllocatorAllocate* AllocatorOverrideShim::GetOverride() const + if (useOverride) { - return m_overridingSource; - } - - bool AllocatorOverrideShim::IsOverridden() const - { - return m_source != m_overridingSource; - } - - bool AllocatorOverrideShim::HasOrphanedAllocations() const - { - return !m_records.empty(); - } - - void AllocatorOverrideShim::SetFinalizedConfiguration() - { - m_finalizedConfiguration = true; - } - - typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::Allocate(size_type byteSize, size_type alignment, int flags, const char* name, const char* fileName, int lineNum, unsigned int suppressStackRecord) - { - pointer_type ptr = m_overridingSource->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord); + // Default behavior, we weren't deleting an old allocation + newPtr = m_overridingSource->ReAllocate(ptr, newSize, newAlignment); if (!IsOverridden()) { + // Still need to do bookkeeping if we haven't been overridden yet lock_type lock(m_mutex); - m_records.insert(ptr); // Record in case we need to orphan this allocation later - } - - return ptr; - } - - void AllocatorOverrideShim::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment) - { - IAllocatorAllocate* source = m_overridingSource; - bool destroy = false; - - { - lock_type lock(m_mutex); - - // Check to see if this came from a prior allocation source - if (m_records.erase(ptr) && IsOverridden()) - { - source = m_source; - - if (m_records.empty() && m_finalizedConfiguration) - { - // All orphaned records are gone; we are no longer needed - m_owningAllocator->SetAllocationSource(m_overridingSource); - destroy = true; // Must destroy outside the lock - } - } - } - - source->DeAllocate(ptr, byteSize, alignment); - - if (destroy) - { - Destroy(this); + m_records.erase(ptr); + m_records.insert(newPtr); } } - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Resize(pointer_type ptr, size_type newSize) + if (destroy) { - IAllocatorAllocate* source = m_overridingSource; - - if (IsOverridden()) - { - // Determine who owns the allocation - lock_type lock(m_mutex); - - if (m_records.count(ptr)) - { - source = m_source; - } - } - - size_t result = source->Resize(ptr, newSize); - - return result; - } - - typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) - { - pointer_type newPtr = nullptr; - bool useOverride = true; - bool destroy = false; - - if (IsOverridden()) - { - lock_type lock(m_mutex); - - if (m_records.erase(ptr)) - { - // An old allocation needs to be transferred to the new, overriding allocator. - useOverride = false; // We'll do the reallocation here - size_t oldSize = m_source->AllocationSize(ptr); - - if (newSize) - { - newPtr = m_overridingSource->Allocate(newSize, newAlignment, 0); - memcpy(newPtr, ptr, AZStd::min(newSize, oldSize)); - } - - m_source->DeAllocate(ptr, oldSize); - - if (m_records.empty() && m_finalizedConfiguration) - { - // All orphaned records are gone; we are no longer needed - m_owningAllocator->SetAllocationSource(m_overridingSource); - destroy = true; // Must destroy outside the lock - } - } - } - - if (useOverride) - { - // Default behavior, we weren't deleting an old allocation - newPtr = m_overridingSource->ReAllocate(ptr, newSize, newAlignment); - - if (!IsOverridden()) - { - // Still need to do bookkeeping if we haven't been overridden yet - lock_type lock(m_mutex); - m_records.erase(ptr); - m_records.insert(newPtr); - } - } - - if (destroy) - { - Destroy(this); - } - - return newPtr; - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::AllocationSize(pointer_type ptr) - { - IAllocatorAllocate* source = m_overridingSource; - - if (IsOverridden()) - { - // Determine who owns the allocation - lock_type lock(m_mutex); - - if (m_records.count(ptr)) - { - source = m_source; - } - } - - return source->AllocationSize(ptr); - } - - void AllocatorOverrideShim::GarbageCollect() - { - m_source->GarbageCollect(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::NumAllocatedBytes() const - { - return m_source->NumAllocatedBytes(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Capacity() const - { - return m_source->Capacity(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::GetMaxAllocationSize() const - { - return m_source->GetMaxAllocationSize(); - } - - auto AllocatorOverrideShim::GetMaxContiguousAllocationSize() const -> size_type - { - return m_source->GetMaxContiguousAllocationSize(); - } - - IAllocatorAllocate* AllocatorOverrideShim::GetSubAllocator() - { - return m_source->GetSubAllocator(); + Destroy(this); } + return newPtr; } -} + + typename AllocatorOverrideShim::size_type AllocatorOverrideShim::AllocationSize(pointer_type ptr) + { + IAllocatorAllocate* source = m_overridingSource; + + if (IsOverridden()) + { + // Determine who owns the allocation + lock_type lock(m_mutex); + + if (m_records.count(ptr)) + { + source = m_source; + } + } + + return source->AllocationSize(ptr); + } + + void AllocatorOverrideShim::GarbageCollect() + { + m_source->GarbageCollect(); + } + + typename AllocatorOverrideShim::size_type AllocatorOverrideShim::NumAllocatedBytes() const + { + return m_source->NumAllocatedBytes(); + } + + typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Capacity() const + { + return m_source->Capacity(); + } + + typename AllocatorOverrideShim::size_type AllocatorOverrideShim::GetMaxAllocationSize() const + { + return m_source->GetMaxAllocationSize(); + } + + auto AllocatorOverrideShim::GetMaxContiguousAllocationSize() const -> size_type + { + return m_source->GetMaxContiguousAllocationSize(); + } + + IAllocatorAllocate* AllocatorOverrideShim::GetSubAllocator() + { + return m_source->GetSubAllocator(); + } + +} // namespace AZ::Internal diff --git a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp index 6af8f201c2..6e40ccd8cd 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp @@ -140,8 +140,8 @@ namespace AZ { class const_iterator; class iterator { - typedef T& reference; - typedef T* pointer; + using reference = T&; + using pointer = T*; friend class const_iterator; T* mPtr; public: @@ -171,8 +171,8 @@ namespace AZ { class const_iterator { - typedef const T& reference; - typedef const T* pointer; + using reference = const T &; + using pointer = const T *; const T* mPtr; public: const_iterator() @@ -327,7 +327,7 @@ namespace AZ { uint64_t mSizeAndFlags; public: - typedef block_header* block_ptr; + using block_ptr = block_header *; size_t size() const { return mSizeAndFlags & ~BL_FLAG_MASK; } block_ptr next() const {return (block_ptr)((char*)mem() + size()); } block_ptr prev() const {return mPrev; } @@ -415,7 +415,7 @@ namespace AZ { void dec_ref() { HPPA_ASSERT(mUseCount > 0); mUseCount--; } bool check_marker(size_t marker) const { return mMarker == (marker ^ ((size_t)this)); } }; - typedef intrusive_list page_list; + using page_list = intrusive_list; class bucket { page_list mPageList; diff --git a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp index 76a71e0f08..9aa31cd8b6 100644 --- a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp @@ -11,149 +11,168 @@ #include #include +namespace AZ::Internal +{ + struct Header + { + uint32_t offset; + uint32_t size; + }; +} // namespace AZ::Internal + namespace AZ { - namespace Internal + //--------------------------------------------------------------------- + // MallocSchema methods + //--------------------------------------------------------------------- + + MallocSchema::MallocSchema(const Descriptor& desc) + : m_bytesAllocated(0) { - struct Header + if (desc.m_useAZMalloc) { - uint32_t offset; - uint32_t size; - }; + static const int DEFAULT_ALIGNMENT = sizeof(void*) * 2; // Default malloc alignment + + m_mallocFn = [](size_t byteSize) + { + return AZ_OS_MALLOC(byteSize, DEFAULT_ALIGNMENT); + }; + m_freeFn = [](void* ptr) + { + AZ_OS_FREE(ptr); + }; + } + else + { + m_mallocFn = &malloc; + m_freeFn = &free; + } } -} -//--------------------------------------------------------------------- -// MallocSchema methods -//--------------------------------------------------------------------- - -AZ::MallocSchema::MallocSchema(const Descriptor& desc) : - m_bytesAllocated(0) -{ - if (desc.m_useAZMalloc) + MallocSchema::~MallocSchema() { - static const int DEFAULT_ALIGNMENT = sizeof(void*) * 2; // Default malloc alignment - - m_mallocFn = [](size_t byteSize) { return AZ_OS_MALLOC(byteSize, DEFAULT_ALIGNMENT); }; - m_freeFn = [](void* ptr) { AZ_OS_FREE(ptr); }; } - else + + MallocSchema::pointer_type MallocSchema::Allocate( + size_type byteSize, + size_type alignment, + int flags, + const char* name, + const char* fileName, + int lineNum, + unsigned int suppressStackRecord) { - m_mallocFn = &malloc; - m_freeFn = &free; + (void)flags; + (void)name; + (void)fileName; + (void)lineNum; + (void)suppressStackRecord; + + if (!byteSize) + { + return nullptr; + } + + if (alignment == 0) + { + alignment = sizeof(void*) * 2; // Default malloc alignment + } + + AZ_Assert(byteSize < 0x100000000ull, "Malloc allocator only allocates up to 4GB"); + + size_type required = byteSize + sizeof(Internal::Header) + + ((alignment > sizeof(double)) + ? alignment + : 0); // Malloc will align to a minimum boundary for native objects, so we only pad if aligning to a large value + void* data = (*m_mallocFn)(required); + void* result = PointerAlignUp(reinterpret_cast(reinterpret_cast(data) + sizeof(Internal::Header)), alignment); + Internal::Header* header = PointerAlignDown( + (Internal::Header*)(reinterpret_cast(result) - sizeof(Internal::Header)), AZStd::alignment_of::value); + + header->offset = static_cast(reinterpret_cast(result) - reinterpret_cast(data)); + header->size = static_cast(byteSize); + m_bytesAllocated += byteSize; + + return result; } -} -AZ::MallocSchema::~MallocSchema() -{ -} + void MallocSchema::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment) + { + (void)byteSize; + (void)alignment; -AZ::MallocSchema::pointer_type AZ::MallocSchema::Allocate(size_type byteSize, size_type alignment, int flags, const char* name, const char* fileName, int lineNum, unsigned int suppressStackRecord) -{ - (void)flags; - (void)name; - (void)fileName; - (void)lineNum; - (void)suppressStackRecord; + if (!ptr) + { + return; + } - if (!byteSize) + Internal::Header* header = PointerAlignDown( + reinterpret_cast(reinterpret_cast(ptr) - sizeof(Internal::Header)), + AZStd::alignment_of::value); + void* freePtr = reinterpret_cast(reinterpret_cast(ptr) - static_cast(header->offset)); + + m_bytesAllocated -= header->size; + (*m_freeFn)(freePtr); + } + + MallocSchema::pointer_type MallocSchema::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) + { + void* newPtr = Allocate(newSize, newAlignment, 0); + size_t oldSize = AllocationSize(ptr); + + memcpy(newPtr, ptr, AZStd::min(oldSize, newSize)); + DeAllocate(ptr, 0, 0); + + return newPtr; + } + + MallocSchema::size_type MallocSchema::Resize(pointer_type ptr, size_type newSize) + { + (void)ptr; + (void)newSize; + + return 0; + } + + MallocSchema::size_type MallocSchema::AllocationSize(pointer_type ptr) + { + if (!ptr) + { + return 0; + } + Internal::Header* header = PointerAlignDown( + reinterpret_cast(reinterpret_cast(ptr) - sizeof(Internal::Header)), + AZStd::alignment_of::value); + return header->size; + } + + MallocSchema::size_type MallocSchema::NumAllocatedBytes() const + { + return m_bytesAllocated; + } + + MallocSchema::size_type MallocSchema::Capacity() const + { + return 0; + } + + MallocSchema::size_type MallocSchema::GetMaxAllocationSize() const + { + return 0xFFFFFFFFull; + } + + MallocSchema::size_type MallocSchema::GetMaxContiguousAllocationSize() const + { + return AZ_CORE_MAX_ALLOCATOR_SIZE; + } + + IAllocatorAllocate* MallocSchema::GetSubAllocator() { return nullptr; } - if (alignment == 0) + void MallocSchema::GarbageCollect() { - alignment = sizeof(void*) * 2; // Default malloc alignment } - AZ_Assert(byteSize < 0x100000000ull, "Malloc allocator only allocates up to 4GB"); - - size_type required = byteSize + sizeof(Internal::Header) + ((alignment > sizeof(double)) ? alignment : 0); // Malloc will align to a minimum boundary for native objects, so we only pad if aligning to a large value - void* data = (*m_mallocFn)(required); - void* result = PointerAlignUp(reinterpret_cast(reinterpret_cast(data) + sizeof(Internal::Header)), alignment); - Internal::Header* header = PointerAlignDown((Internal::Header*)(reinterpret_cast(result) - sizeof(Internal::Header)), AZStd::alignment_of::value); - - header->offset = static_cast(reinterpret_cast(result) - reinterpret_cast(data)); - header->size = static_cast(byteSize); - m_bytesAllocated += byteSize; - - return result; -} - -void AZ::MallocSchema::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment) -{ - (void)byteSize; - (void)alignment; - - if (!ptr) - { - return; - } - - Internal::Header* header = PointerAlignDown(reinterpret_cast(reinterpret_cast(ptr) - sizeof(Internal::Header)), AZStd::alignment_of::value); - void* freePtr = reinterpret_cast(reinterpret_cast(ptr) - static_cast(header->offset)); - - m_bytesAllocated -= header->size; - (*m_freeFn)(freePtr); -} - -AZ::MallocSchema::pointer_type AZ::MallocSchema::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) -{ - void* newPtr = Allocate(newSize, newAlignment, 0); - size_t oldSize = AllocationSize(ptr); - - memcpy(newPtr, ptr, AZStd::min(oldSize, newSize)); - DeAllocate(ptr, 0, 0); - - return newPtr; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::Resize(pointer_type ptr, size_type newSize) -{ - (void)ptr; - (void)newSize; - - return 0; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::AllocationSize(pointer_type ptr) -{ - size_type result = 0; - - if (ptr) - { - Internal::Header* header = PointerAlignDown(reinterpret_cast(reinterpret_cast(ptr) - sizeof(Internal::Header)), AZStd::alignment_of::value); - result = header->size; - } - - return result; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::NumAllocatedBytes() const -{ - return m_bytesAllocated; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::Capacity() const -{ - return 0; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::GetMaxAllocationSize() const -{ - return 0xFFFFFFFFull; -} - -AZ::MallocSchema::size_type AZ::MallocSchema::GetMaxContiguousAllocationSize() const -{ - return AZ_CORE_MAX_ALLOCATOR_SIZE; -} - -AZ::IAllocatorAllocate* AZ::MallocSchema::GetSubAllocator() -{ - return nullptr; -} - -void AZ::MallocSchema::GarbageCollect() -{ -} +} // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Memory/MemoryDriller.cpp b/Code/Framework/AzCore/AzCore/Memory/MemoryDriller.cpp index 2ea25c3397..dc35c6322b 100644 --- a/Code/Framework/AzCore/AzCore/Memory/MemoryDriller.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/MemoryDriller.cpp @@ -15,280 +15,277 @@ #include #include -namespace AZ +namespace AZ::Debug { - namespace Debug + //========================================================================= + // MemoryDriller + // [2/6/2013] + //========================================================================= + MemoryDriller::MemoryDriller(const Descriptor& desc) { - //========================================================================= - // MemoryDriller - // [2/6/2013] - //========================================================================= - MemoryDriller::MemoryDriller(const Descriptor& desc) + (void)desc; + BusConnect(); + + AllocatorManager::Instance().EnterProfilingMode(); + { - (void)desc; - BusConnect(); - - AllocatorManager::Instance().EnterProfilingMode(); - - { - // Register all allocators that were created before the driller existed - auto allocatorLock = AllocatorManager::Instance().LockAllocators(); - - for (int i = 0; i < AllocatorManager::Instance().GetNumAllocators(); ++i) - { - IAllocator* allocator = AllocatorManager::Instance().GetAllocator(i); - RegisterAllocator(allocator); - } - } - } - - //========================================================================= - // ~MemoryDriller - // [2/6/2013] - //========================================================================= - MemoryDriller::~MemoryDriller() - { - BusDisconnect(); - AllocatorManager::Instance().ExitProfilingMode(); - } - - //========================================================================= - // Start - // [2/6/2013] - //========================================================================= - void MemoryDriller::Start(const Param* params, int numParams) - { - (void)params; - (void)numParams; - - // dump current allocations for all allocators with tracking + // Register all allocators that were created before the driller existed auto allocatorLock = AllocatorManager::Instance().LockAllocators(); + for (int i = 0; i < AllocatorManager::Instance().GetNumAllocators(); ++i) { IAllocator* allocator = AllocatorManager::Instance().GetAllocator(i); - if (auto records = allocator->GetRecords()) - { - RegisterAllocatorOutput(allocator); - const AllocationRecordsType& allocMap = records->GetMap(); - for (AllocationRecordsType::const_iterator allocIt = allocMap.begin(); allocIt != allocMap.end(); ++allocIt) - { - RegisterAllocationOutput(allocator, allocIt->first, &allocIt->second); - } - } + RegisterAllocator(allocator); } } + } - //========================================================================= - // Stop - // [2/6/2013] - //========================================================================= - void MemoryDriller::Stop() + //========================================================================= + // ~MemoryDriller + // [2/6/2013] + //========================================================================= + MemoryDriller::~MemoryDriller() + { + BusDisconnect(); + AllocatorManager::Instance().ExitProfilingMode(); + } + + //========================================================================= + // Start + // [2/6/2013] + //========================================================================= + void MemoryDriller::Start(const Param* params, int numParams) + { + (void)params; + (void)numParams; + + // dump current allocations for all allocators with tracking + auto allocatorLock = AllocatorManager::Instance().LockAllocators(); + for (int i = 0; i < AllocatorManager::Instance().GetNumAllocators(); ++i) { - } - - //========================================================================= - // RegisterAllocator - // [2/6/2013] - //========================================================================= - void MemoryDriller::RegisterAllocator(IAllocator* allocator) - { - // Ignore if our allocator is already registered - if (allocator->GetRecords() != nullptr) + IAllocator* allocator = AllocatorManager::Instance().GetAllocator(i); + if (auto records = allocator->GetRecords()) { - return; - } - - auto debugConfig = allocator->GetDebugConfig(); - - if (!debugConfig.m_excludeFromDebugging) - { - allocator->SetRecords(aznew Debug::AllocationRecords((unsigned char)debugConfig.m_stackRecordLevels, debugConfig.m_usesMemoryGuards, debugConfig.m_marksUnallocatedMemory, allocator->GetName())); - - m_allAllocatorRecords.push_back(allocator->GetRecords()); - - if (m_output == nullptr) - { - return; // we have no active output - } RegisterAllocatorOutput(allocator); - } - } - //========================================================================= - // RegisterAllocatorOutput - // [2/6/2013] - //========================================================================= - void MemoryDriller::RegisterAllocatorOutput(IAllocator* allocator) - { - auto records = allocator->GetRecords(); - m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - m_output->BeginTag(AZ_CRC("RegisterAllocator", 0x19f08114)); - m_output->Write(AZ_CRC("Name", 0x5e237e06), allocator->GetName()); - m_output->Write(AZ_CRC("Id", 0xbf396750), allocator); - m_output->Write(AZ_CRC("Capacity", 0xb5e8b174), allocator->GetAllocationSource()->Capacity()); - m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); - if (records) - { - m_output->Write(AZ_CRC("RecordsMode", 0x764c147a), (char)records->GetMode()); - m_output->Write(AZ_CRC("NumStackLevels", 0xad9cff15), records->GetNumStackLevels()); - } - m_output->EndTag(AZ_CRC("RegisterAllocator", 0x19f08114)); - m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - } - - //========================================================================= - // UnregisterAllocator - // [2/6/2013] - //========================================================================= - void MemoryDriller::UnregisterAllocator(IAllocator* allocator) - { - auto allocatorRecords = allocator->GetRecords(); - AZ_Assert(allocatorRecords, "This allocator is not registered with the memory driller!"); - for (auto records : m_allAllocatorRecords) - { - if (records == allocatorRecords) + const AllocationRecordsType& allocMap = records->GetMap(); + for (AllocationRecordsType::const_iterator allocIt = allocMap.begin(); allocIt != allocMap.end(); ++allocIt) { - m_allAllocatorRecords.remove(records); - break; + RegisterAllocationOutput(allocator, allocIt->first, &allocIt->second); } } - delete allocatorRecords; - allocator->SetRecords(nullptr); + } + } + + //========================================================================= + // Stop + // [2/6/2013] + //========================================================================= + void MemoryDriller::Stop() + { + } + + //========================================================================= + // RegisterAllocator + // [2/6/2013] + //========================================================================= + void MemoryDriller::RegisterAllocator(IAllocator* allocator) + { + // Ignore if our allocator is already registered + if (allocator->GetRecords() != nullptr) + { + return; + } + + auto debugConfig = allocator->GetDebugConfig(); + + if (!debugConfig.m_excludeFromDebugging) + { + allocator->SetRecords(aznew Debug::AllocationRecords((unsigned char)debugConfig.m_stackRecordLevels, debugConfig.m_usesMemoryGuards, debugConfig.m_marksUnallocatedMemory, allocator->GetName())); + + m_allAllocatorRecords.push_back(allocator->GetRecords()); + + if (m_output == nullptr) + { + return; // we have no active output + } + RegisterAllocatorOutput(allocator); + } + } + //========================================================================= + // RegisterAllocatorOutput + // [2/6/2013] + //========================================================================= + void MemoryDriller::RegisterAllocatorOutput(IAllocator* allocator) + { + auto records = allocator->GetRecords(); + m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + m_output->BeginTag(AZ_CRC("RegisterAllocator", 0x19f08114)); + m_output->Write(AZ_CRC("Name", 0x5e237e06), allocator->GetName()); + m_output->Write(AZ_CRC("Id", 0xbf396750), allocator); + m_output->Write(AZ_CRC("Capacity", 0xb5e8b174), allocator->GetAllocationSource()->Capacity()); + m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); + if (records) + { + m_output->Write(AZ_CRC("RecordsMode", 0x764c147a), (char)records->GetMode()); + m_output->Write(AZ_CRC("NumStackLevels", 0xad9cff15), records->GetNumStackLevels()); + } + m_output->EndTag(AZ_CRC("RegisterAllocator", 0x19f08114)); + m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + } + + //========================================================================= + // UnregisterAllocator + // [2/6/2013] + //========================================================================= + void MemoryDriller::UnregisterAllocator(IAllocator* allocator) + { + auto allocatorRecords = allocator->GetRecords(); + AZ_Assert(allocatorRecords, "This allocator is not registered with the memory driller!"); + for (auto records : m_allAllocatorRecords) + { + if (records == allocatorRecords) + { + m_allAllocatorRecords.remove(records); + break; + } + } + delete allocatorRecords; + allocator->SetRecords(nullptr); + + if (m_output == nullptr) + { + return; // we have no active output + } + m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + m_output->Write(AZ_CRC("UnregisterAllocator", 0xb2b54f93), allocator); + m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + } + + //========================================================================= + // RegisterAllocation + // [2/6/2013] + //========================================================================= + void MemoryDriller::RegisterAllocation(IAllocator* allocator, void* address, size_t byteSize, size_t alignment, const char* name, const char* fileName, int lineNum, unsigned int stackSuppressCount) + { + auto records = allocator->GetRecords(); + if (records) + { + const AllocationInfo* info = records->RegisterAllocation(address, byteSize, alignment, name, fileName, lineNum, stackSuppressCount + 1); + if (m_output == nullptr) + { + return; // we have no active output + } + RegisterAllocationOutput(allocator, address, info); + } + } + + //========================================================================= + // RegisterAllocationOutput + // [2/6/2013] + //========================================================================= + void MemoryDriller::RegisterAllocationOutput(IAllocator* allocator, void* address, const AllocationInfo* info) + { + auto records = allocator->GetRecords(); + m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + m_output->BeginTag(AZ_CRC("RegisterAllocation", 0x992a9780)); + m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); + m_output->Write(AZ_CRC("Address", 0x0d4e6f81), address); + if (info) + { + if (info->m_name) + { + m_output->Write(AZ_CRC("Name", 0x5e237e06), info->m_name); + } + m_output->Write(AZ_CRC("Alignment", 0x2cce1e5c), info->m_alignment); + m_output->Write(AZ_CRC("Size", 0xf7c0246a), info->m_byteSize); + if (info->m_fileName) + { + m_output->Write(AZ_CRC("FileName", 0x3c0be965), info->m_fileName); + m_output->Write(AZ_CRC("FileLine", 0xb33c2395), info->m_lineNum); + } + // copy the stack frames directly, resolving the stack should happen later as this is a SLOW procedure. + if (info->m_stackFrames) + { + m_output->Write(AZ_CRC("Stack", 0x41a87b6a), info->m_stackFrames, info->m_stackFrames + records->GetNumStackLevels()); + } + } + m_output->EndTag(AZ_CRC("RegisterAllocation", 0x992a9780)); + m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + } + + //========================================================================= + // UnRegisterAllocation + // [2/6/2013] + //========================================================================= + void MemoryDriller::UnregisterAllocation(IAllocator* allocator, void* address, size_t byteSize, size_t alignment, AllocationInfo* info) + { + auto records = allocator->GetRecords(); + if (records) + { + records->UnregisterAllocation(address, byteSize, alignment, info); if (m_output == nullptr) { return; // we have no active output } m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - m_output->Write(AZ_CRC("UnregisterAllocator", 0xb2b54f93), allocator); - m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - } - - //========================================================================= - // RegisterAllocation - // [2/6/2013] - //========================================================================= - void MemoryDriller::RegisterAllocation(IAllocator* allocator, void* address, size_t byteSize, size_t alignment, const char* name, const char* fileName, int lineNum, unsigned int stackSuppressCount) - { - auto records = allocator->GetRecords(); - if (records) - { - const AllocationInfo* info = records->RegisterAllocation(address, byteSize, alignment, name, fileName, lineNum, stackSuppressCount + 1); - if (m_output == nullptr) - { - return; // we have no active output - } - RegisterAllocationOutput(allocator, address, info); - } - } - - //========================================================================= - // RegisterAllocationOutput - // [2/6/2013] - //========================================================================= - void MemoryDriller::RegisterAllocationOutput(IAllocator* allocator, void* address, const AllocationInfo* info) - { - auto records = allocator->GetRecords(); - m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - m_output->BeginTag(AZ_CRC("RegisterAllocation", 0x992a9780)); + m_output->BeginTag(AZ_CRC("UnRegisterAllocation", 0xea5dc4cd)); m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); m_output->Write(AZ_CRC("Address", 0x0d4e6f81), address); - if (info) - { - if (info->m_name) - { - m_output->Write(AZ_CRC("Name", 0x5e237e06), info->m_name); - } - m_output->Write(AZ_CRC("Alignment", 0x2cce1e5c), info->m_alignment); - m_output->Write(AZ_CRC("Size", 0xf7c0246a), info->m_byteSize); - if (info->m_fileName) - { - m_output->Write(AZ_CRC("FileName", 0x3c0be965), info->m_fileName); - m_output->Write(AZ_CRC("FileLine", 0xb33c2395), info->m_lineNum); - } - // copy the stack frames directly, resolving the stack should happen later as this is a SLOW procedure. - if (info->m_stackFrames) - { - m_output->Write(AZ_CRC("Stack", 0x41a87b6a), info->m_stackFrames, info->m_stackFrames + records->GetNumStackLevels()); - } - } - m_output->EndTag(AZ_CRC("RegisterAllocation", 0x992a9780)); + m_output->EndTag(AZ_CRC("UnRegisterAllocation", 0xea5dc4cd)); m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); } + } - //========================================================================= - // UnRegisterAllocation - // [2/6/2013] - //========================================================================= - void MemoryDriller::UnregisterAllocation(IAllocator* allocator, void* address, size_t byteSize, size_t alignment, AllocationInfo* info) + //========================================================================= + // ReallocateAllocation + // [10/1/2018] + //========================================================================= + void MemoryDriller::ReallocateAllocation(IAllocator* allocator, void* prevAddress, void* newAddress, size_t newByteSize, size_t newAlignment) + { + AllocationInfo info; + UnregisterAllocation(allocator, prevAddress, 0, 0, &info); + RegisterAllocation(allocator, newAddress, newByteSize, newAlignment, info.m_name, info.m_fileName, info.m_lineNum, 0); + } + + //========================================================================= + // ResizeAllocation + // [2/6/2013] + //========================================================================= + void MemoryDriller::ResizeAllocation(IAllocator* allocator, void* address, size_t newSize) + { + auto records = allocator->GetRecords(); + if (records) { - auto records = allocator->GetRecords(); - if (records) - { - records->UnregisterAllocation(address, byteSize, alignment, info); + records->ResizeAllocation(address, newSize); - if (m_output == nullptr) - { - return; // we have no active output - } - m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - m_output->BeginTag(AZ_CRC("UnRegisterAllocation", 0xea5dc4cd)); - m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); - m_output->Write(AZ_CRC("Address", 0x0d4e6f81), address); - m_output->EndTag(AZ_CRC("UnRegisterAllocation", 0xea5dc4cd)); - m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + if (m_output == nullptr) + { + return; // we have no active output + } + m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + m_output->BeginTag(AZ_CRC("ResizeAllocation", 0x8a9c78dc)); + m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); + m_output->Write(AZ_CRC("Address", 0x0d4e6f81), address); + m_output->Write(AZ_CRC("Size", 0xf7c0246a), newSize); + m_output->EndTag(AZ_CRC("ResizeAllocation", 0x8a9c78dc)); + m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); + } + } + + void MemoryDriller::DumpAllAllocations() + { + // Create a copy so allocations done during the printing dont end up affecting the container + const AZStd::list allocationRecords = m_allAllocatorRecords; + + for (auto records : allocationRecords) + { + // Skip if we have had no allocations made + if (records->RequestedAllocs()) + { + records->EnumerateAllocations(AZ::Debug::PrintAllocationsCB(true, true)); } } + } - //========================================================================= - // ReallocateAllocation - // [10/1/2018] - //========================================================================= - void MemoryDriller::ReallocateAllocation(IAllocator* allocator, void* prevAddress, void* newAddress, size_t newByteSize, size_t newAlignment) - { - AllocationInfo info; - UnregisterAllocation(allocator, prevAddress, 0, 0, &info); - RegisterAllocation(allocator, newAddress, newByteSize, newAlignment, info.m_name, info.m_fileName, info.m_lineNum, 0); - } - - //========================================================================= - // ResizeAllocation - // [2/6/2013] - //========================================================================= - void MemoryDriller::ResizeAllocation(IAllocator* allocator, void* address, size_t newSize) - { - auto records = allocator->GetRecords(); - if (records) - { - records->ResizeAllocation(address, newSize); - - if (m_output == nullptr) - { - return; // we have no active output - } - m_output->BeginTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - m_output->BeginTag(AZ_CRC("ResizeAllocation", 0x8a9c78dc)); - m_output->Write(AZ_CRC("RecordsId", 0x7caaca88), records); - m_output->Write(AZ_CRC("Address", 0x0d4e6f81), address); - m_output->Write(AZ_CRC("Size", 0xf7c0246a), newSize); - m_output->EndTag(AZ_CRC("ResizeAllocation", 0x8a9c78dc)); - m_output->EndTag(AZ_CRC("MemoryDriller", 0x1b31269d)); - } - } - - void MemoryDriller::DumpAllAllocations() - { - // Create a copy so allocations done during the printing dont end up affecting the container - const AZStd::list allocationRecords = m_allAllocatorRecords; - - for (auto records : allocationRecords) - { - // Skip if we have had no allocations made - if (records->RequestedAllocs()) - { - records->EnumerateAllocations(AZ::Debug::PrintAllocationsCB(true, true)); - } - } - } - - }// namespace Debug -} // namespace AZ +} // namespace AZ::Debug diff --git a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp index 0bef6b7d28..9167864450 100644 --- a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp @@ -35,8 +35,8 @@ namespace AZ public: AZ_CLASS_ALLOCATOR(PoolAllocation, SystemAllocator, 0) - typedef typename Allocator::Page PageType; - typedef typename Allocator::Bucket BucketType; + using PageType = typename Allocator::Page; + using BucketType = typename Allocator::Bucket; PoolAllocation(Allocator* alloc, size_t pageSize, size_t minAllocationSize, size_t maxAllocationSize); virtual ~PoolAllocation(); @@ -89,7 +89,7 @@ namespace AZ void SetupFreeList(size_t elementSize, size_t pageDataBlockSize); /// We just use a free list of nodes which we cast to the pool type. - typedef AZStd::intrusive_slist > FreeListType; + using FreeListType = AZStd::intrusive_slist>; FreeListType m_freeList; u32 m_bin; @@ -103,7 +103,7 @@ namespace AZ */ struct Bucket { - typedef AZStd::intrusive_list > PageListType; + using PageListType = AZStd::intrusive_list>; PageListType m_pages; }; @@ -162,7 +162,7 @@ namespace AZ return page; } - typedef PoolAllocation AllocatorType; + using AllocatorType = PoolAllocation; IAllocatorAllocate* m_pageAllocator; AllocatorType m_allocator; void* m_staticDataBlock; @@ -199,7 +199,7 @@ namespace AZ void SetupFreeList(size_t elementSize, size_t pageDataBlockSize); /// We just use a free list of nodes which we cast to the pool type. - typedef AZStd::intrusive_slist > FreeListType; + using FreeListType = AZStd::intrusive_slist>; FreeListType m_freeList; AZStd::lock_free_intrusive_stack_node m_lfStack; ///< Lock Free stack node @@ -215,7 +215,7 @@ namespace AZ */ struct Bucket { - typedef AZStd::intrusive_list > PageListType; + using PageListType = AZStd::intrusive_list>; PageListType m_pages; }; @@ -291,7 +291,7 @@ namespace AZ ThreadPoolSchema::SetThreadPoolData m_threadPoolSetter; // Fox X64 we push/pop pages using the m_mutex to sync. Pages are - typedef Bucket::PageListType FreePagesType; + using FreePagesType = Bucket::PageListType; FreePagesType m_freePages; AZStd::vector m_threads; ///< Array with all separate thread data. Used to traverse end free elements. @@ -313,12 +313,12 @@ namespace AZ ThreadPoolData(ThreadPoolSchemaImpl* alloc, size_t pageSize, size_t minAllocationSize, size_t maxAllocationSize); ~ThreadPoolData(); - typedef PoolAllocation AllocatorType; + using AllocatorType = PoolAllocation; /** * Stack with freed elements from other threads. We don't need stamped stack since the ABA problem can not * happen here. We push from many threads and pop from only one (we don't push from it). */ - typedef AZStd::lock_free_intrusive_stack > FreedElementsStack; + using FreedElementsStack = AZStd::lock_free_intrusive_stack>; AllocatorType m_allocator; FreedElementsStack m_freedElements; diff --git a/Code/Framework/AzCore/AzCore/Module/Environment.cpp b/Code/Framework/AzCore/AzCore/Module/Environment.cpp index 71da948a35..790af730bf 100644 --- a/Code/Framework/AzCore/AzCore/Module/Environment.cpp +++ b/Code/Framework/AzCore/AzCore/Module/Environment.cpp @@ -24,10 +24,10 @@ namespace AZ class OSStdAllocator { public: - typedef void* pointer_type; - typedef AZStd::size_t size_type; - typedef AZStd::ptrdiff_t difference_type; - typedef AZStd::false_type allow_memory_leaks; ///< Regular allocators should not leak. + using pointer_type = void *; + using size_type = AZStd::size_t; + using difference_type = AZStd::ptrdiff_t; + using allow_memory_leaks = AZStd::false_type; ///< Regular allocators should not leak. OSStdAllocator(Environment::AllocatorInterface* allocator) : m_name("GlobalEnvironmentAllocator") @@ -122,7 +122,7 @@ namespace AZ : public EnvironmentInterface { public: - typedef AZStd::unordered_map, AZStd::equal_to, OSStdAllocator> MapType; + using MapType = AZStd::unordered_map, AZStd::equal_to, OSStdAllocator>; static EnvironmentInterface* Get(); static void Attach(EnvironmentInstance sourceEnvironment, bool useAsGetFallback); diff --git a/Code/Framework/AzCore/AzCore/Module/Internal/ModuleManagerSearchPathTool.cpp b/Code/Framework/AzCore/AzCore/Module/Internal/ModuleManagerSearchPathTool.cpp index 10aa9904a3..0797fef93f 100644 --- a/Code/Framework/AzCore/AzCore/Module/Internal/ModuleManagerSearchPathTool.cpp +++ b/Code/Framework/AzCore/AzCore/Module/Internal/ModuleManagerSearchPathTool.cpp @@ -10,21 +10,18 @@ #include #include -namespace AZ +namespace AZ::Internal { - namespace Internal + AZ::OSString ModuleManagerSearchPathTool::GetModuleDirectory(const AZ::DynamicModuleDescriptor& moduleDesc) { - AZ::OSString ModuleManagerSearchPathTool::GetModuleDirectory(const AZ::DynamicModuleDescriptor& moduleDesc) + // For each module that is loaded, attempt to set the module's folder as a path for dependent module resolution + AZ::OSString modulePath = moduleDesc.m_dynamicLibraryPath; + AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::ResolveModulePath, modulePath); + auto lastPathSep = modulePath.find_last_of(AZ_TRAIT_OS_PATH_SEPARATOR); + if (lastPathSep != modulePath.npos) { - // For each module that is loaded, attempt to set the module's folder as a path for dependent module resolution - AZ::OSString modulePath = moduleDesc.m_dynamicLibraryPath; - AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::ResolveModulePath, modulePath); - auto lastPathSep = modulePath.find_last_of(AZ_TRAIT_OS_PATH_SEPARATOR); - if (lastPathSep != modulePath.npos) - { - modulePath = modulePath.substr(0, lastPathSep); - } - return modulePath; + modulePath = modulePath.substr(0, lastPathSep); } - } // namespace Internal -} // namespace AZ + return modulePath; + } +} // namespace AZ::Internal diff --git a/Code/Framework/AzCore/AzCore/Name/Internal/NameData.cpp b/Code/Framework/AzCore/AzCore/Name/Internal/NameData.cpp index 574b0bcc7e..69bead11ac 100644 --- a/Code/Framework/AzCore/AzCore/Name/Internal/NameData.cpp +++ b/Code/Framework/AzCore/AzCore/Name/Internal/NameData.cpp @@ -9,45 +9,41 @@ #include #include -namespace AZ +namespace AZ::Internal { - namespace Internal + NameData::NameData(AZStd::string&& name, Hash hash) + : m_name{AZStd::move(name)} + , m_hash{hash} + {} + + AZStd::string_view NameData::GetName() const { - NameData::NameData(AZStd::string&& name, Hash hash) - : m_name{AZStd::move(name)} - , m_hash{hash} - {} + return m_name; + } - AZStd::string_view NameData::GetName() const - { - return m_name; - } + NameData::Hash NameData::GetHash() const + { + return m_hash; + } - NameData::Hash NameData::GetHash() const - { - return m_hash; - } + void NameData::add_ref() + { + AZ_Assert(m_useCount >= 0, "NameData has been deleted"); + ++m_useCount; + } - void NameData::add_ref() + void NameData::release() + { + // this could be released after we decrement the counter, therefore we will + // base the release on the hash which is stable + Hash hash = m_hash; + AZ_Assert(m_useCount > 0, "m_useCount is already 0!"); + if (m_useCount.fetch_sub(1) == 1) { - AZ_Assert(m_useCount >= 0, "NameData has been deleted"); - ++m_useCount; - } - - void NameData::release() - { - // this could be released after we decrement the counter, therefore we will - // base the release on the hash which is stable - Hash hash = m_hash; - AZ_Assert(m_useCount > 0, "m_useCount is already 0!"); - if (m_useCount.fetch_sub(1) == 1) + if (AZ::NameDictionary::IsReady()) { - if (AZ::NameDictionary::IsReady()) - { - AZ::NameDictionary::Instance().TryReleaseName(hash); - } + AZ::NameDictionary::Instance().TryReleaseName(hash); } } } -} - +} // namespace AZ::Internal diff --git a/Code/Framework/AzCore/AzCore/Name/NameDictionary.cpp b/Code/Framework/AzCore/AzCore/Name/NameDictionary.cpp index 3047a2894e..1b39eb81bd 100644 --- a/Code/Framework/AzCore/AzCore/Name/NameDictionary.cpp +++ b/Code/Framework/AzCore/AzCore/Name/NameDictionary.cpp @@ -32,7 +32,12 @@ namespace AZ if (!s_instance) { - s_instance = AZ::Environment::CreateVariable(NameDictionaryInstanceName); + // Because the NameDictionary allocates memory using the AZ::Allocator and it is created + // in the executable memory space, it's ownership cannot be transferred to other module memory spaces + // Otherwise this could cause the the NameDictionary to be destroyed in static de-init + // after the AZ::Allocators have been destroyed + // Therefore we supply the isTransferOwnership value of false using CreateVariableEx + s_instance = AZ::Environment::CreateVariableEx(NameDictionaryInstanceName, true, false); } } @@ -50,7 +55,12 @@ namespace AZ if (!s_instance) { - s_instance = Environment::FindVariable(NameDictionaryInstanceName); + // Because the NameDictionary allocates memory using the AZ::Allocator and it is created + // in the executable memory space, it's ownership cannot be transferred to other module memory spaces + // Otherwise this could cause the the NameDictionary to be destroyed in static de-init + // after the AZ::Allocators have been destroyed + // Therefore we supply the isTransferOwnership value of false using CreateVariableEx + s_instance = AZ::Environment::CreateVariableEx(NameDictionaryInstanceName, true, false); } return s_instance.IsConstructed(); diff --git a/Code/Framework/AzCore/AzCore/Platform.cpp b/Code/Framework/AzCore/AzCore/Platform.cpp index ad345f65e8..0defef826e 100644 --- a/Code/Framework/AzCore/AzCore/Platform.cpp +++ b/Code/Framework/AzCore/AzCore/Platform.cpp @@ -8,19 +8,16 @@ #include -namespace AZ +namespace AZ::Platform { - namespace Platform - { - MachineId s_machineId = MachineId(0); + MachineId s_machineId = MachineId(0); - void SetLocalMachineId(AZ::u32 machineId) + void SetLocalMachineId(AZ::u32 machineId) + { + AZ_Assert(machineId != 0, "0 machine ID is reserved!"); + if (s_machineId != 0) { - AZ_Assert(machineId != 0, "0 machine ID is reserved!"); - if (s_machineId != 0) - { - s_machineId = machineId; - } + s_machineId = machineId; } } -} +} // namespace AZ::Platform diff --git a/Code/Framework/AzCore/AzCore/RTTI/BehaviorContextUtilities.cpp b/Code/Framework/AzCore/AzCore/RTTI/BehaviorContextUtilities.cpp index 6fb03073b0..5632c258c3 100644 --- a/Code/Framework/AzCore/AzCore/RTTI/BehaviorContextUtilities.cpp +++ b/Code/Framework/AzCore/AzCore/RTTI/BehaviorContextUtilities.cpp @@ -117,9 +117,9 @@ namespace AZ if (!explicitOverloads.m_overloads.empty()) { - for (auto methodAndClass : explicitOverloads.m_overloads) + for (const auto& methodAndClass : explicitOverloads.m_overloads) { - overloads.push_back({ methodAndClass.first, methodAndClass.second }); + overloads.emplace_back(methodAndClass.first, methodAndClass.second); } } else @@ -128,7 +128,7 @@ namespace AZ do { - overloads.push_back({ overload, behaviorClass }); + overloads.emplace_back(overload, behaviorClass); overload = overload->m_overload; } while (overload); diff --git a/Code/Framework/AzCore/AzCore/Script/ScriptSystemComponent.cpp b/Code/Framework/AzCore/AzCore/Script/ScriptSystemComponent.cpp index 3b2aebdee6..4b71c57682 100644 --- a/Code/Framework/AzCore/AzCore/Script/ScriptSystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Script/ScriptSystemComponent.cpp @@ -170,61 +170,77 @@ ScriptContext* ScriptSystemComponent::AddContext(ScriptContext* context, int ga ScriptContext* ScriptSystemComponent::AddContextWithId(ScriptContextId id) { AZ_Assert(m_contexts.empty() || id != ScriptContextIds::DefaultScriptContextId, "Default script context ID is reserved! Please provide a Unique context ID for you ScriptContext!"); - if (GetContext(id) == nullptr) + if (GetContext(id) != nullptr) { - m_contexts.emplace_back(); - ContextContainer& cc = m_contexts.back(); - cc.m_context = aznew ScriptContext(id); - cc.m_isOwner = true; - cc.m_garbageCollectorSteps = m_defaultGarbageCollectorSteps; - - cc.m_context->SetRequireHook(AZStd::bind(&ScriptSystemComponent::DefaultRequireHook, this, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3)); - - if (id != ScriptContextIds::CryScriptContextId) + return nullptr; + } + m_contexts.emplace_back(); + ContextContainer& cc = m_contexts.back(); + cc.m_context = aznew ScriptContext(id); + cc.m_isOwner = true; + cc.m_garbageCollectorSteps = m_defaultGarbageCollectorSteps; + cc.m_context->SetRequireHook( + [this](lua_State* lua, ScriptContext* context, const char* module) -> int { - // Reflect script classes - ComponentApplication* app = nullptr; - EBUS_EVENT_RESULT(app, ComponentApplicationBus, GetApplication); - if (app && app->GetDescriptor().m_enableScriptReflection) + return DefaultRequireHook(lua, context, module); + }); + + if (id != ScriptContextIds::CryScriptContextId) + { + // Reflect script classes + ComponentApplication* app = nullptr; + EBUS_EVENT_RESULT(app, ComponentApplicationBus, GetApplication); + if (app && app->GetDescriptor().m_enableScriptReflection) + { + if (app->GetBehaviorContext()) { - if (app->GetBehaviorContext()) - { - cc.m_context->BindTo(app->GetBehaviorContext()); - } - else - { - AZ_Error("Script", false, "We are asked to enabled scripting, but the Applicaion has no BehaviorContext! Scripting relies on BehaviorContext!"); - } + cc.m_context->BindTo(app->GetBehaviorContext()); + } + else + { + AZ_Error("Script", false, "We are asked to enabled scripting, but the Applicaion has no BehaviorContext! Scripting relies on BehaviorContext!"); } } - - return cc.m_context; } - return nullptr; + return cc.m_context; } void ScriptSystemComponent::RestoreDefaultRequireHook(ScriptContextId id) { - if (auto context = GetContext(id)) + auto context = GetContext(id); + if (!context) { - for (auto& inMemoryModule : m_inMemoryModules) - { - ClearAssetReferences(inMemoryModule.second->GetId()); - } - - m_inMemoryModules.clear(); - context->SetRequireHook(AZStd::bind(&ScriptSystemComponent::DefaultRequireHook, this, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3)); + return; } + + for (auto& inMemoryModule : m_inMemoryModules) + { + ClearAssetReferences(inMemoryModule.second->GetId()); + } + + m_inMemoryModules.clear(); + context->SetRequireHook( + [this](lua_State* lua, ScriptContext* context, const char* module) -> int + { + return DefaultRequireHook(lua, context, module); + }); } void ScriptSystemComponent::UseInMemoryRequireHook(const InMemoryScriptModules& modules, ScriptContextId id) { - if (auto context = GetContext(id)) + auto context = GetContext(id); + if (nullptr == context) { - m_inMemoryModules = modules; - context->SetRequireHook(AZStd::bind(&ScriptSystemComponent::InMemoryRequireHook, this, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3)); + return; } + + m_inMemoryModules = modules; + context->SetRequireHook( + [this](lua_State* lua, ScriptContext* context, const char* module) -> int + { + return InMemoryRequireHook(lua, context, module); + }); } //========================================================================= diff --git a/Code/Framework/AzCore/AzCore/ScriptCanvas/ScriptCanvasOnDemandNames.cpp b/Code/Framework/AzCore/AzCore/ScriptCanvas/ScriptCanvasOnDemandNames.cpp index b0d141bba9..d0bea11432 100644 --- a/Code/Framework/AzCore/AzCore/ScriptCanvas/ScriptCanvasOnDemandNames.cpp +++ b/Code/Framework/AzCore/AzCore/ScriptCanvas/ScriptCanvasOnDemandNames.cpp @@ -28,133 +28,130 @@ #include #include -namespace AZ +namespace AZ::ScriptCanvasOnDemandReflection { - namespace ScriptCanvasOnDemandReflection + // the use of this might have to come at the end of on demand reflection...instead of instantly + // basically, it required that dependent classes are reflected first, I'm not sure they are yet. + AZStd::string GetPrettyNameForAZTypeId(AZ::BehaviorContext& context, AZ::Uuid typeId) { - // the use of this might have to come at the end of on demand reflection...instead of instantly - // basically, it required that dependent classes are reflected first, I'm not sure they are yet. - AZStd::string GetPrettyNameForAZTypeId(AZ::BehaviorContext& context, AZ::Uuid typeId) + // return capitalized versions of what we need, otherwise just the regular name + // then strip all the stuff + if (typeId == azrtti_typeid()) { - // return capitalized versions of what we need, otherwise just the regular name - // then strip all the stuff - if (typeId == azrtti_typeid()) + return "AABB"; + } + else if (typeId == azrtti_typeid()) + { + return "Boolean"; + } + else if (typeId == azrtti_typeid()) + { + return "Color"; + } + else if (typeId == azrtti_typeid()) + { + return "CRC"; + } + else if (typeId == azrtti_typeid()) + { + return "EntityId"; + } + else if (typeId == azrtti_typeid()) + { + return "Matrix3x3"; + } + else if (typeId == azrtti_typeid()) + { + return "Matrix4x4"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:s8"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:s16"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:s32"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:s64"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:u8"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:u16"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:u32"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:u64"; + } + else if (typeId == azrtti_typeid()) + { + return "Number:float"; + } + else if (typeId == azrtti_typeid()) + { + return "Number"; + } + else if (typeId == azrtti_typeid()) + { + return "OBB"; + } + else if (typeId == azrtti_typeid()) + { + return "Plane"; + } + else if (typeId == azrtti_typeid()) + { + return "Quaternion"; + } + else if (typeId == azrtti_typeid() || typeId == azrtti_typeid()) + { + return "String"; + } + else if (typeId == azrtti_typeid()) + { + return "Transform"; + } + else if (typeId == azrtti_typeid()) + { + return "Vector2"; + } + else if (typeId == azrtti_typeid()) + { + return "Vector3"; + } + else if (typeId == azrtti_typeid()) + { + return "Vector4"; + } + else + { + auto bcClassIter = context.m_typeToClassMap.find(typeId); + if (bcClassIter != context.m_typeToClassMap.end()) { - return "AABB"; - } - else if (typeId == azrtti_typeid()) - { - return "Boolean"; - } - else if (typeId == azrtti_typeid()) - { - return "Color"; - } - else if (typeId == azrtti_typeid()) - { - return "CRC"; - } - else if (typeId == azrtti_typeid()) - { - return "EntityId"; - } - else if (typeId == azrtti_typeid()) - { - return "Matrix3x3"; - } - else if (typeId == azrtti_typeid()) - { - return "Matrix4x4"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:s8"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:s16"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:s32"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:s64"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:u8"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:u16"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:u32"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:u64"; - } - else if (typeId == azrtti_typeid()) - { - return "Number:float"; - } - else if (typeId == azrtti_typeid()) - { - return "Number"; - } - else if (typeId == azrtti_typeid()) - { - return "OBB"; - } - else if (typeId == azrtti_typeid()) - { - return "Plane"; - } - else if (typeId == azrtti_typeid()) - { - return "Quaternion"; - } - else if (typeId == azrtti_typeid() || typeId == azrtti_typeid()) - { - return "String"; - } - else if (typeId == azrtti_typeid()) - { - return "Transform"; - } - else if (typeId == azrtti_typeid()) - { - return "Vector2"; - } - else if (typeId == azrtti_typeid()) - { - return "Vector3"; - } - else if (typeId == azrtti_typeid()) - { - return "Vector4"; + const AZ::BehaviorClass& bcClass = *(bcClassIter->second); + AZStd::string uglyName = bcClass.m_name; + AZ::StringFunc::Replace(uglyName, "AZStd::", "", true); + AZ::StringFunc::Replace(uglyName, "AZ::", "", true); + AZ::StringFunc::Replace(uglyName, "::", ".", true); + return uglyName; } else { - auto bcClassIter = context.m_typeToClassMap.find(typeId); - if (bcClassIter != context.m_typeToClassMap.end()) - { - const AZ::BehaviorClass& bcClass = *(bcClassIter->second); - AZStd::string uglyName = bcClass.m_name; - AZ::StringFunc::Replace(uglyName, "AZStd::", "", true); - AZ::StringFunc::Replace(uglyName, "AZ::", "", true); - AZ::StringFunc::Replace(uglyName, "::", ".", true); - return uglyName; - } - else - { - return "Invalid"; - } + return "Invalid"; } } - } // namespace ScriptCanvasOnDemandReflection -} // namespace AZ + } +} // namespace AZ::ScriptCanvasOnDemandReflection diff --git a/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl b/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl index bae79a6fe7..a633af22da 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl +++ b/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl @@ -54,12 +54,8 @@ namespace AZStd namespace AZ { - //template - //class ScriptProperty; - namespace Internal { - template void SetupClassElementFromType(SerializeContext::ClassElement& classElement) { @@ -86,15 +82,14 @@ namespace AZ { auto uuid = AzTypeInfo::Uuid(); - using ContainerType = AttributeContainerType; - classElement.m_attributes.emplace_back(AZ_CRC("EnumType", 0xb177e1b5), CreateModuleAttribute(AZStd::move(uuid))); + classElement.m_attributes.emplace_back(AZ_CRC("EnumType", 0xb177e1b5), CreateModuleAttribute(AZStd::move(uuid))); } } template AZStd::enable_if_t::value> InitializeDefaultIfPodType(T& t) { - t = {}; + t = T{}; } template @@ -648,7 +643,6 @@ namespace AZ // Register our key type within an lvalue to rvalue wrapper as an attribute AZ::TypeId uuid = azrtti_typeid(); - using ContainerType = AttributeContainerType; /** * This should technically bind the reference value from the GetCurrentSerializeContextModule() call @@ -658,7 +652,7 @@ namespace AZ */ m_classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return GetCurrentSerializeContextModule().GetAllocator(); })); - m_classElement.m_attributes.emplace_back(AZ_CRC("KeyType", 0x15bc5303), CreateModuleAttribute(AZStd::move(uuid))); + m_classElement.m_attributes.emplace_back(AZ_CRC("KeyType", 0x15bc5303), CreateModuleAttribute(AZStd::move(uuid))); } // Reflect our wrapped key and value types to serializeContext so that may later be used diff --git a/Code/Framework/AzCore/AzCore/Serialization/DataOverlayProviderMsgs.cpp b/Code/Framework/AzCore/AzCore/Serialization/DataOverlayProviderMsgs.cpp index 37f4623301..58f86bf831 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/DataOverlayProviderMsgs.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/DataOverlayProviderMsgs.cpp @@ -19,8 +19,11 @@ namespace AZ nodeStack.push_back(m_dataContainer); SerializeContext::EnumerateInstanceCallContext callContext( - AZStd::bind(&DataOverlayTarget::ElementBegin, this, &nodeStack, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3), - AZStd::bind(&DataOverlayTarget::ElementEnd, this, &nodeStack), + [this, &nodeStack](void* instancePointer, const SerializeContext::ClassData* classData, const SerializeContext::ClassElement* classElement)->bool + { + return ElementBegin(&nodeStack, instancePointer, classData, classElement); + }, + [this, &nodeStack]()->bool { return ElementEnd(&nodeStack); }, m_sc, SerializeContext::ENUM_ACCESS_FOR_READ, m_errorLogger diff --git a/Code/Framework/AzCore/AzCore/Serialization/DataPatch.cpp b/Code/Framework/AzCore/AzCore/Serialization/DataPatch.cpp index 6f1635148c..d27a5005b4 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/DataPatch.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/DataPatch.cpp @@ -36,7 +36,7 @@ namespace AZ class DataNode { public: - typedef AZStd::list ChildDataNodes; + using ChildDataNodes = AZStd::list; DataNode() { @@ -148,25 +148,28 @@ namespace AZ m_root.Reset(); m_currentNode = nullptr; - if (m_context && rootClassPtr) + if (!m_context || !rootClassPtr) { - SerializeContext::EnumerateInstanceCallContext callContext( - AZStd::bind(&DataNodeTree::BeginNode, this, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3), - AZStd::bind(&DataNodeTree::EndNode, this), - m_context, - SerializeContext::ENUM_ACCESS_FOR_READ, - nullptr - ); - - m_context->EnumerateInstanceConst( - &callContext, - rootClassPtr, - rootClassId, - nullptr, - nullptr - ); + return; } + SerializeContext::EnumerateInstanceCallContext callContext( + [this](void* instancePointer, const SerializeContext::ClassData* classData, const SerializeContext::ClassElement* classElement)->bool + { + return BeginNode(instancePointer, classData, classElement); + }, + [this]()->bool { return EndNode(); }, + m_context, + SerializeContext::ENUM_ACCESS_FOR_READ, + nullptr + ); + m_context->EnumerateInstanceConst( + &callContext, + rootClassPtr, + rootClassId, + nullptr, + nullptr + ); m_currentNode = nullptr; } diff --git a/Code/Framework/AzCore/AzCore/Serialization/EditContext.cpp b/Code/Framework/AzCore/AzCore/Serialization/EditContext.cpp index d0c433df16..8a4688d748 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/EditContext.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/EditContext.cpp @@ -34,7 +34,7 @@ namespace AZ } classIt->ClearElements(); } - for (auto enumIt : m_enumData) + for (auto& enumIt : m_enumData) { enumIt.second.ClearAttributes(); } @@ -103,7 +103,7 @@ namespace AZ //========================================================================= void ElementData::ClearAttributes() { - for (auto attrib : m_attributes) + for (auto& attrib : m_attributes) { delete attrib.second; } diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonDeserializer.cpp b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonDeserializer.cpp index a741294544..4e80d9ba69 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonDeserializer.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonDeserializer.cpp @@ -759,6 +759,7 @@ namespace AZ else { typeIdResult.m_determination = JsonDeserializer::TypeIdDetermination::FailedToDetermine; + typeIdResult.m_typeId = Uuid::CreateNull(); } } else if (input.IsString()) @@ -768,6 +769,7 @@ namespace AZ else { typeIdResult.m_determination = JsonDeserializer::TypeIdDetermination::FailedToDetermine; + typeIdResult.m_typeId = Uuid::CreateNull(); } switch (typeIdResult.m_determination) diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSerializationResult.cpp b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSerializationResult.cpp index 822c1c43d5..16bad5a466 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSerializationResult.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSerializationResult.cpp @@ -10,265 +10,264 @@ #include #include -namespace AZ +namespace AZ::JsonSerializationResult::Internal { - namespace JsonSerializationResult + template + void AppendToString(AZ::JsonSerializationResult::ResultCode code, + StringType& target, AZStd::string_view path) { - namespace Internal + if (code.GetTask() == static_cast(0)) { - template - void AppendToString(AZ::JsonSerializationResult::ResultCode code, - StringType& target, AZStd::string_view path) - { - if (code.GetTask() == static_cast(0)) - { - target.append("The result code wasn't initialized"); - return; - } - - target.append("The operation "); - switch (code.GetProcessing()) - { - case Processing::Halted: - target.append("has halted during "); - break; - case Processing::Altered: - target.append("has taken an alternative approach for "); - break; - case Processing::PartialAlter: - target.append("has taken a partially alternative approach for "); - break; - case Processing::Completed: - target.append("has completed "); - break; - default: - target.append("has unknown processing status for "); - break; - } - - switch (code.GetTask()) - { - case Tasks::RetrieveInfo: - target.append("a retrieve info operation "); - break; - case Tasks::CreateDefault: - target.append("a create default operation "); - break; - case Tasks::Convert: - target.append("a convert operation "); - break; - case Tasks::ReadField: - target.append("a read field operation "); - break; - case Tasks::WriteValue: - target.append("a write value operation "); - break; - case Tasks::Merge: - target.append("a merge operation "); - break; - case Tasks::CreatePatch: - target.append("a create patch operation "); - break; - case Tasks::Import: - target.append("an import operation"); - break; - default: - target.append("an unknown operation "); - break; - } - - if (!path.empty()) - { - target.append("for '"); - target.append(path.begin(), path.end()); - target.append("' "); - } - - switch (code.GetOutcome()) - { - case Outcomes::Success: - target.append("which resulted in success"); - break; - case Outcomes::DefaultsUsed: - target.append("by using only default values"); - break; - case Outcomes::PartialDefaults: - target.append("by using one or more default values"); - break; - case Outcomes::Skipped: - target.append("because a field or value was skipped"); - break; - case Outcomes::PartialSkip: - target.append("because one or more fields or values were skipped"); - break; - case Outcomes::Unavailable: - target.append("because the target was unavailable"); - break; - case Outcomes::Unsupported: - target.append("because the action was unsupported"); - break; - case Outcomes::TypeMismatch: - target.append("because the source and target are unrelated types"); - break; - case Outcomes::TestFailed: - target.append("because a test against a value failed"); - break; - case Outcomes::Missing: - target.append("because a required field or value was missing"); - break; - case Outcomes::Invalid: - target.append("because a field or element has an invalid value"); - break; - case Outcomes::Unknown: - target.append("because information was missing"); - break; - case Outcomes::Catastrophic: - target.append("because a catastrophic issue was encountered"); - break; - default: - break; - } - } - } // namespace JsonSerializationResultInternal - - ResultCode::ResultCode(Tasks task) - : m_code(0) - { - m_options.m_task = task; + target.append("The result code wasn't initialized"); + return; } - ResultCode::ResultCode(uint32_t code) - : m_code(code) - {} - - ResultCode::ResultCode(Tasks task, Outcomes outcome) + target.append("The operation "); + switch (code.GetProcessing()) { - m_options.m_task = task; - switch (outcome) - { - case Outcomes::Success: // fall through - case Outcomes::Skipped: // fall through - case Outcomes::PartialSkip: // fall through - case Outcomes::DefaultsUsed: // fall through - case Outcomes::PartialDefaults: - m_options.m_processing = Processing::Completed; - break; - case Outcomes::Unavailable: // fall through - case Outcomes::Unsupported: - m_options.m_processing = Processing::Altered; - break; - case Outcomes::TypeMismatch: // fall through - case Outcomes::TestFailed: // fall through - case Outcomes::Missing: // fall through - case Outcomes::Invalid: // fall through - case Outcomes::Unknown: // fall through - case Outcomes::Catastrophic: // fall through - default: - m_options.m_processing = Processing::Halted; - break; - } - m_options.m_outcome = outcome; + case Processing::Halted: + target.append("has halted during "); + break; + case Processing::Altered: + target.append("has taken an alternative approach for "); + break; + case Processing::PartialAlter: + target.append("has taken a partially alternative approach for "); + break; + case Processing::Completed: + target.append("has completed "); + break; + default: + target.append("has unknown processing status for "); + break; } - bool ResultCode::HasDoneWork() const + switch (code.GetTask()) { - return m_options.m_outcome != static_cast(0); + case Tasks::RetrieveInfo: + target.append("a retrieve info operation "); + break; + case Tasks::CreateDefault: + target.append("a create default operation "); + break; + case Tasks::Convert: + target.append("a convert operation "); + break; + case Tasks::ReadField: + target.append("a read field operation "); + break; + case Tasks::WriteValue: + target.append("a write value operation "); + break; + case Tasks::Merge: + target.append("a merge operation "); + break; + case Tasks::CreatePatch: + target.append("a create patch operation "); + break; + case Tasks::Import: + target.append("an import operation"); + break; + default: + target.append("an unknown operation "); + break; } - ResultCode& ResultCode::Combine(ResultCode other) + if (!path.empty()) { - *this = Combine(*this, other); - return *this; + target.append("for '"); + target.append(path.begin(), path.end()); + target.append("' "); } - ResultCode& ResultCode::Combine(const Result& other) + switch (code.GetOutcome()) { - *this = Combine(*this, other.GetResultCode()); - return *this; + case Outcomes::Success: + target.append("which resulted in success"); + break; + case Outcomes::DefaultsUsed: + target.append("by using only default values"); + break; + case Outcomes::PartialDefaults: + target.append("by using one or more default values"); + break; + case Outcomes::Skipped: + target.append("because a field or value was skipped"); + break; + case Outcomes::PartialSkip: + target.append("because one or more fields or values were skipped"); + break; + case Outcomes::Unavailable: + target.append("because the target was unavailable"); + break; + case Outcomes::Unsupported: + target.append("because the action was unsupported"); + break; + case Outcomes::TypeMismatch: + target.append("because the source and target are unrelated types"); + break; + case Outcomes::TestFailed: + target.append("because a test against a value failed"); + break; + case Outcomes::Missing: + target.append("because a required field or value was missing"); + break; + case Outcomes::Invalid: + target.append("because a field or element has an invalid value"); + break; + case Outcomes::Unknown: + target.append("because information was missing"); + break; + case Outcomes::Catastrophic: + target.append("because a catastrophic issue was encountered"); + break; + default: + break; } + } +} // namespace AZ::JsonSerializationResult::Internal - ResultCode ResultCode::Combine(ResultCode lhs, ResultCode rhs) +namespace AZ::JsonSerializationResult +{ + + ResultCode::ResultCode(Tasks task) + : m_code(0) + { + m_options.m_task = task; + } + + ResultCode::ResultCode(uint32_t code) + : m_code(code) + {} + + ResultCode::ResultCode(Tasks task, Outcomes outcome) + { + m_options.m_task = task; + switch (outcome) { - ResultCode result = ResultCode(AZStd::max(lhs.m_code, rhs.m_code)); - - if ((lhs.m_options.m_outcome == Outcomes::Success && rhs.m_options.m_outcome == Outcomes::DefaultsUsed) || - (lhs.m_options.m_outcome == Outcomes::DefaultsUsed && rhs.m_options.m_outcome == Outcomes::Success)) - { - result.m_options.m_outcome = Outcomes::PartialDefaults; - } - else if ((lhs.m_options.m_outcome == Outcomes::Success && rhs.m_options.m_outcome == Outcomes::Skipped) || - (lhs.m_options.m_outcome == Outcomes::Skipped && rhs.m_options.m_outcome == Outcomes::Success)) - { - result.m_options.m_outcome = Outcomes::PartialSkip; - } - - if ((lhs.m_options.m_processing == Processing::Completed && rhs.m_options.m_processing == Processing::Altered) || - (lhs.m_options.m_processing == Processing::Altered && rhs.m_options.m_processing == Processing::Completed)) - { - result.m_options.m_processing = Processing::PartialAlter; - } - - return result; + case Outcomes::Success: // fall through + case Outcomes::Skipped: // fall through + case Outcomes::PartialSkip: // fall through + case Outcomes::DefaultsUsed: // fall through + case Outcomes::PartialDefaults: + m_options.m_processing = Processing::Completed; + break; + case Outcomes::Unavailable: // fall through + case Outcomes::Unsupported: + m_options.m_processing = Processing::Altered; + break; + case Outcomes::TypeMismatch: // fall through + case Outcomes::TestFailed: // fall through + case Outcomes::Missing: // fall through + case Outcomes::Invalid: // fall through + case Outcomes::Unknown: // fall through + case Outcomes::Catastrophic: // fall through + default: + m_options.m_processing = Processing::Halted; + break; } + m_options.m_outcome = outcome; + } - Tasks ResultCode::GetTask() const + bool ResultCode::HasDoneWork() const + { + return m_options.m_outcome != static_cast(0); + } + + ResultCode& ResultCode::Combine(ResultCode other) + { + *this = Combine(*this, other); + return *this; + } + + ResultCode& ResultCode::Combine(const Result& other) + { + *this = Combine(*this, other.GetResultCode()); + return *this; + } + + ResultCode ResultCode::Combine(ResultCode lhs, ResultCode rhs) + { + ResultCode result = ResultCode(AZStd::max(lhs.m_code, rhs.m_code)); + + if ((lhs.m_options.m_outcome == Outcomes::Success && rhs.m_options.m_outcome == Outcomes::DefaultsUsed) || + (lhs.m_options.m_outcome == Outcomes::DefaultsUsed && rhs.m_options.m_outcome == Outcomes::Success)) { - return m_options.m_task; + result.m_options.m_outcome = Outcomes::PartialDefaults; } - - Processing ResultCode::GetProcessing() const + else if ((lhs.m_options.m_outcome == Outcomes::Success && rhs.m_options.m_outcome == Outcomes::Skipped) || + (lhs.m_options.m_outcome == Outcomes::Skipped && rhs.m_options.m_outcome == Outcomes::Success)) { - return m_options.m_processing == static_cast(0) ? - Processing::Completed : m_options.m_processing; + result.m_options.m_outcome = Outcomes::PartialSkip; } - Outcomes ResultCode::GetOutcome() const + if ((lhs.m_options.m_processing == Processing::Completed && rhs.m_options.m_processing == Processing::Altered) || + (lhs.m_options.m_processing == Processing::Altered && rhs.m_options.m_processing == Processing::Completed)) { - return m_options.m_outcome == static_cast(0) ? - Outcomes::DefaultsUsed : m_options.m_outcome; + result.m_options.m_processing = Processing::PartialAlter; } - void ResultCode::AppendToString(AZ::OSString& target, AZStd::string_view path) const - { - Internal::AppendToString(*this, target, path); - } + return result; + } - void ResultCode::AppendToString(AZStd::string& target, AZStd::string_view path) const - { - Internal::AppendToString(*this, target, path); - } + Tasks ResultCode::GetTask() const + { + return m_options.m_task; + } - AZStd::string ResultCode::ToString(AZStd::string_view path) const - { - AZStd::string result; - AppendToString(result, path); - return result; - } + Processing ResultCode::GetProcessing() const + { + return m_options.m_processing == static_cast(0) ? + Processing::Completed : m_options.m_processing; + } - AZ::OSString ResultCode::ToOSString(AZStd::string_view path) const - { - AZ::OSString result; - AppendToString(result, path); - return result; - } + Outcomes ResultCode::GetOutcome() const + { + return m_options.m_outcome == static_cast(0) ? + Outcomes::DefaultsUsed : m_options.m_outcome; + } + + void ResultCode::AppendToString(AZ::OSString& target, AZStd::string_view path) const + { + Internal::AppendToString(*this, target, path); + } + + void ResultCode::AppendToString(AZStd::string& target, AZStd::string_view path) const + { + Internal::AppendToString(*this, target, path); + } + + AZStd::string ResultCode::ToString(AZStd::string_view path) const + { + AZStd::string result; + AppendToString(result, path); + return result; + } + + AZ::OSString ResultCode::ToOSString(AZStd::string_view path) const + { + AZ::OSString result; + AppendToString(result, path); + return result; + } - Result::Result(const JsonIssueCallback& callback, AZStd::string_view message, ResultCode result, AZStd::string_view path) - : m_result(callback(message, result, path)) - {} + Result::Result(const JsonIssueCallback& callback, AZStd::string_view message, ResultCode result, AZStd::string_view path) + : m_result(callback(message, result, path)) + {} - Result::Result(const JsonIssueCallback& callback, AZStd::string_view message, Tasks task, Outcomes outcome, AZStd::string_view path) - : m_result(callback(message, ResultCode(task, outcome), path)) - {} + Result::Result(const JsonIssueCallback& callback, AZStd::string_view message, Tasks task, Outcomes outcome, AZStd::string_view path) + : m_result(callback(message, ResultCode(task, outcome), path)) + {} - Result::operator ResultCode() const - { - return m_result; - } + Result::operator ResultCode() const + { + return m_result; + } - ResultCode Result::GetResultCode() const - { - return m_result; - } - } // namespace JsonSerializationResult -} // namespace AZ + ResultCode Result::GetResultCode() const + { + return m_result; + } + +} // namespace AZ::JsonSerializationResult diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSystemComponent.cpp b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSystemComponent.cpp index da02e70181..0ba44e4e61 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonSystemComponent.cpp @@ -104,6 +104,8 @@ namespace AZ ->HandlesType(); jsonContext->Serializer() ->HandlesType(); + jsonContext->Serializer() + ->HandlesType(); MathReflect(jsonContext); } diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonUtils.cpp b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonUtils.cpp index 1a4e7b3e80..6d7edb0716 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/JsonUtils.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/JsonUtils.cpp @@ -24,438 +24,435 @@ #include -namespace AZ +namespace AZ::JsonSerializationUtils { - namespace JsonSerializationUtils + static const char* FileTypeTag = "Type"; + static const char* FileType = "JsonSerialization"; + static const char* VersionTag = "Version"; + static const char* ClassNameTag = "ClassName"; + static const char* ClassDataTag = "ClassData"; + + AZ::Outcome WriteJsonString(const rapidjson::Document& document, AZStd::string& jsonText, WriteJsonSettings settings) { - static const char* FileTypeTag = "Type"; - static const char* FileType = "JsonSerialization"; - static const char* VersionTag = "Version"; - static const char* ClassNameTag = "ClassName"; - static const char* ClassDataTag = "ClassData"; + AZ::IO::ByteContainerStream stream{&jsonText}; + return WriteJsonStream(document, stream, settings); + } - AZ::Outcome WriteJsonString(const rapidjson::Document& document, AZStd::string& jsonText, WriteJsonSettings settings) + AZ::Outcome WriteJsonFile(const rapidjson::Document& document, AZStd::string_view filePath, WriteJsonSettings settings) + { + // Write the json into memory first and then write the file, rather than passing a file stream to rapidjson. + // This should avoid creating a large number of micro-writes to the file. + AZStd::string fileContent; + auto outcome = WriteJsonString(document, fileContent, settings); + if (!outcome.IsSuccess()) { - AZ::IO::ByteContainerStream stream{&jsonText}; - return WriteJsonStream(document, stream, settings); + return outcome; } - AZ::Outcome WriteJsonFile(const rapidjson::Document& document, AZStd::string_view filePath, WriteJsonSettings settings) - { - // Write the json into memory first and then write the file, rather than passing a file stream to rapidjson. - // This should avoid creating a large number of micro-writes to the file. - AZStd::string fileContent; - auto outcome = WriteJsonString(document, fileContent, settings); - if (!outcome.IsSuccess()) - { - return outcome; - } + return AZ::Utils::WriteFile(fileContent, filePath); + } - return AZ::Utils::WriteFile(fileContent, filePath); + AZ::Outcome WriteJsonStream(const rapidjson::Document& document, IO::GenericStream& stream, WriteJsonSettings settings) + { + AZ::IO::RapidJSONStreamWriter jsonStreamWriter(&stream); + + rapidjson::PrettyWriter writer(jsonStreamWriter); + + if (settings.m_maxDecimalPlaces >= 0) + { + writer.SetMaxDecimalPlaces(settings.m_maxDecimalPlaces); } - AZ::Outcome WriteJsonStream(const rapidjson::Document& document, IO::GenericStream& stream, WriteJsonSettings settings) + if (document.Accept(writer)) { - AZ::IO::RapidJSONStreamWriter jsonStreamWriter(&stream); + return AZ::Success(); + } + else + { + return AZ::Failure(AZStd::string{"Json Writer failed"}); + } + } - rapidjson::PrettyWriter writer(jsonStreamWriter); - - if (settings.m_maxDecimalPlaces >= 0) - { - writer.SetMaxDecimalPlaces(settings.m_maxDecimalPlaces); - } - - if (document.Accept(writer)) - { - return AZ::Success(); - } - else - { - return AZ::Failure(AZStd::string{"Json Writer failed"}); - } + AZ::Outcome SaveObjectToStreamByType(const void* objectPtr, const Uuid& classId, IO::GenericStream& stream, + const void* defaultObjectPtr, const JsonSerializerSettings* settings) + { + if (!stream.CanWrite()) + { + return AZ::Failure(AZStd::string("The GenericStream can't be written to")); } - AZ::Outcome SaveObjectToStreamByType(const void* objectPtr, const Uuid& classId, IO::GenericStream& stream, - const void* defaultObjectPtr, const JsonSerializerSettings* settings) + JsonSerializerSettings saveSettings; + if (settings) { - if (!stream.CanWrite()) - { - return AZ::Failure(AZStd::string("The GenericStream can't be written to")); - } + saveSettings = *settings; + } - JsonSerializerSettings saveSettings; - if (settings) - { - saveSettings = *settings; - } - - AZ::SerializeContext* serializeContext = saveSettings.m_serializeContext; + AZ::SerializeContext* serializeContext = saveSettings.m_serializeContext; + if (!serializeContext) + { + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); if (!serializeContext) { - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); - if (!serializeContext) - { - return AZ::Failure(AZStd::string::format("Need SerializeContext for saving")); - } - saveSettings.m_serializeContext = serializeContext; + return AZ::Failure(AZStd::string::format("Need SerializeContext for saving")); } - - rapidjson::Document jsonDocument; - jsonDocument.SetObject(); - jsonDocument.AddMember(rapidjson::StringRef(FileTypeTag), rapidjson::StringRef(FileType), jsonDocument.GetAllocator()); - - rapidjson::Value serializedObject; - - JsonSerializationResult::ResultCode jsonResult = JsonSerialization::Store(serializedObject, jsonDocument.GetAllocator(), - objectPtr, defaultObjectPtr, classId, saveSettings); - - if (jsonResult.GetProcessing() != JsonSerializationResult::Processing::Completed) - { - return AZ::Failure(jsonResult.ToString("")); - } - - const SerializeContext::ClassData* classData = serializeContext->FindClassData(classId); - - jsonDocument.AddMember(rapidjson::StringRef(VersionTag), 1, jsonDocument.GetAllocator()); - jsonDocument.AddMember(rapidjson::StringRef(ClassNameTag), rapidjson::StringRef(classData->m_name), jsonDocument.GetAllocator()); - jsonDocument.AddMember(rapidjson::StringRef(ClassDataTag), AZStd::move(serializedObject), jsonDocument.GetAllocator()); - - AZ::IO::RapidJSONStreamWriter jsonStreamWriter(&stream); - rapidjson::PrettyWriter writer(jsonStreamWriter); - bool jsonWriteResult = jsonDocument.Accept(writer); - if (!jsonWriteResult) - { - return AZ::Failure(AZStd::string::format("Unable to write class %s with json serialization format'", - classId.ToString().data())); - } - - return AZ::Success(); + saveSettings.m_serializeContext = serializeContext; } - AZ::Outcome SaveObjectToFileByType(const void* classPtr, const Uuid& classId, const AZStd::string& filePath, - const void* defaultClassPtr, const JsonSerializerSettings* settings) + rapidjson::Document jsonDocument; + jsonDocument.SetObject(); + jsonDocument.AddMember(rapidjson::StringRef(FileTypeTag), rapidjson::StringRef(FileType), jsonDocument.GetAllocator()); + + rapidjson::Value serializedObject; + + JsonSerializationResult::ResultCode jsonResult = JsonSerialization::Store(serializedObject, jsonDocument.GetAllocator(), + objectPtr, defaultObjectPtr, classId, saveSettings); + + if (jsonResult.GetProcessing() != JsonSerializationResult::Processing::Completed) { - AZStd::vector buffer; - buffer.reserve(1024); - AZ::IO::ByteContainerStream > byteStream(&buffer); - auto saveResult = SaveObjectToStreamByType(classPtr, classId, byteStream, defaultClassPtr, settings); - if (saveResult.IsSuccess()) - { - AZ::IO::FileIOStream outputFileStream; - if (!outputFileStream.Open(filePath.c_str(), AZ::IO::OpenMode::ModeWrite | AZ::IO::OpenMode::ModeCreatePath | AZ::IO::OpenMode::ModeText)) - { - return AZ::Failure(AZStd::string::format("Error opening file '%s' for writing", filePath.c_str())); - } - outputFileStream.Write(buffer.size(), buffer.data()); - } - return saveResult; + return AZ::Failure(jsonResult.ToString("")); } - // Helper function to check whether the load outcome was success (for loading json serialization file) - bool WasLoadSuccess(JsonSerializationResult::Outcomes outcome) - { - return (outcome == JsonSerializationResult::Outcomes::Success - || outcome == JsonSerializationResult::Outcomes::DefaultsUsed - || outcome == JsonSerializationResult::Outcomes::PartialDefaults); - } - - AZ::Outcome PrepareDeserializerSettings(const JsonDeserializerSettings* inputSettings, JsonDeserializerSettings& returnSettings - , AZStd::string& deserializeError) - { - if (inputSettings) - { - returnSettings = *inputSettings; - } + const SerializeContext::ClassData* classData = serializeContext->FindClassData(classId); + jsonDocument.AddMember(rapidjson::StringRef(VersionTag), 1, jsonDocument.GetAllocator()); + jsonDocument.AddMember(rapidjson::StringRef(ClassNameTag), rapidjson::StringRef(classData->m_name), jsonDocument.GetAllocator()); + jsonDocument.AddMember(rapidjson::StringRef(ClassDataTag), AZStd::move(serializedObject), jsonDocument.GetAllocator()); + + AZ::IO::RapidJSONStreamWriter jsonStreamWriter(&stream); + rapidjson::PrettyWriter writer(jsonStreamWriter); + bool jsonWriteResult = jsonDocument.Accept(writer); + if (!jsonWriteResult) + { + return AZ::Failure(AZStd::string::format("Unable to write class %s with json serialization format'", + classId.ToString().data())); + } + + return AZ::Success(); + } + + AZ::Outcome SaveObjectToFileByType(const void* classPtr, const Uuid& classId, const AZStd::string& filePath, + const void* defaultClassPtr, const JsonSerializerSettings* settings) + { + AZStd::vector buffer; + buffer.reserve(1024); + AZ::IO::ByteContainerStream > byteStream(&buffer); + auto saveResult = SaveObjectToStreamByType(classPtr, classId, byteStream, defaultClassPtr, settings); + if (saveResult.IsSuccess()) + { + AZ::IO::FileIOStream outputFileStream; + if (!outputFileStream.Open(filePath.c_str(), AZ::IO::OpenMode::ModeWrite | AZ::IO::OpenMode::ModeCreatePath | AZ::IO::OpenMode::ModeText)) + { + return AZ::Failure(AZStd::string::format("Error opening file '%s' for writing", filePath.c_str())); + } + outputFileStream.Write(buffer.size(), buffer.data()); + } + return saveResult; + } + + // Helper function to check whether the load outcome was success (for loading json serialization file) + bool WasLoadSuccess(JsonSerializationResult::Outcomes outcome) + { + return (outcome == JsonSerializationResult::Outcomes::Success + || outcome == JsonSerializationResult::Outcomes::DefaultsUsed + || outcome == JsonSerializationResult::Outcomes::PartialDefaults); + } + + AZ::Outcome PrepareDeserializerSettings(const JsonDeserializerSettings* inputSettings, JsonDeserializerSettings& returnSettings + , AZStd::string& deserializeError) + { + if (inputSettings) + { + returnSettings = *inputSettings; + } + + if (!returnSettings.m_serializeContext) + { + AZ::ComponentApplicationBus::BroadcastResult(returnSettings.m_serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); if (!returnSettings.m_serializeContext) { - AZ::ComponentApplicationBus::BroadcastResult(returnSettings.m_serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); - if (!returnSettings.m_serializeContext) + return AZ::Failure(AZStd::string("Need SerializeContext for loading")); + } + } + + // Report unused data field as error by default + auto reporting = returnSettings.m_reporting; + auto issueReportingCallback = [&deserializeError, reporting](AZStd::string_view message, JsonSerializationResult::ResultCode result, AZStd::string_view target) -> JsonSerializationResult::ResultCode + { + using namespace JsonSerializationResult; + + if (!WasLoadSuccess(result.GetOutcome())) + { + // This if is a hack around fault in the JSON serialization system + // Jira: LY-106587 + if (message != "No part of the string could be interpreted as a uuid.") { - return AZ::Failure(AZStd::string("Need SerializeContext for loading")); + deserializeError.append(message); + deserializeError.append(AZStd::string::format(" '%s' \n", target.data())); } } - // Report unused data field as error by default - auto reporting = returnSettings.m_reporting; - auto issueReportingCallback = [&deserializeError, reporting](AZStd::string_view message, JsonSerializationResult::ResultCode result, AZStd::string_view target) -> JsonSerializationResult::ResultCode + if (reporting) { - using namespace JsonSerializationResult; + result = reporting(message, result, target); + } - if (!WasLoadSuccess(result.GetOutcome())) - { - // This if is a hack around fault in the JSON serialization system - // Jira: LY-106587 - if (message != "No part of the string could be interpreted as a uuid.") - { - deserializeError.append(message); - deserializeError.append(AZStd::string::format(" '%s' \n", target.data())); - } - } + return result; + }; - if (reporting) - { - result = reporting(message, result, target); - } + returnSettings.m_reporting = issueReportingCallback; - return result; - }; + return AZ::Success(); + } - returnSettings.m_reporting = issueReportingCallback; - return AZ::Success(); + AZ::Outcome ReadJsonString(AZStd::string_view jsonText) + { + if (jsonText.empty()) + { + return AZ::Failure(AZStd::string("Failed to parse JSON: input string is empty.")); } - - AZ::Outcome ReadJsonString(AZStd::string_view jsonText) + rapidjson::Document jsonDocument; + jsonDocument.Parse(jsonText.data(), jsonText.size()); + if (jsonDocument.HasParseError()) { - if (jsonText.empty()) + size_t lineNumber = 1; + + const size_t errorOffset = jsonDocument.GetErrorOffset(); + for (size_t searchOffset = jsonText.find('\n'); + searchOffset < errorOffset && searchOffset < AZStd::string::npos; + searchOffset = jsonText.find('\n', searchOffset + 1)) { - return AZ::Failure(AZStd::string("Failed to parse JSON: input string is empty.")); + lineNumber++; } - rapidjson::Document jsonDocument; - jsonDocument.Parse(jsonText.data(), jsonText.size()); - if (jsonDocument.HasParseError()) - { - size_t lineNumber = 1; + return AZ::Failure(AZStd::string::format("JSON parse error at line %zu: %s", lineNumber, rapidjson::GetParseError_En(jsonDocument.GetParseError()))); + } + else + { + return AZ::Success(AZStd::move(jsonDocument)); + } + } - const size_t errorOffset = jsonDocument.GetErrorOffset(); - for (size_t searchOffset = jsonText.find('\n'); - searchOffset < errorOffset && searchOffset < AZStd::string::npos; - searchOffset = jsonText.find('\n', searchOffset + 1)) - { - lineNumber++; - } - - return AZ::Failure(AZStd::string::format("JSON parse error at line %zu: %s", lineNumber, rapidjson::GetParseError_En(jsonDocument.GetParseError()))); - } - else - { - return AZ::Success(AZStd::move(jsonDocument)); - } + AZ::Outcome ReadJsonStream(IO::GenericStream& stream) + { + IO::SizeType length = stream.GetLength(); + + AZStd::vector memoryBuffer; + memoryBuffer.resize_no_construct(static_cast::size_type>(static_cast::size_type>(length) + 1)); + + IO::SizeType bytesRead = stream.Read(length, memoryBuffer.data()); + if (bytesRead != length) + { + return AZ::Failure(AZStd::string{"Cannot to read input stream."}); } - AZ::Outcome ReadJsonStream(IO::GenericStream& stream) + memoryBuffer.back() = 0; + + return ReadJsonString(AZStd::string_view{memoryBuffer.data(), memoryBuffer.size()}); + } + + AZ::Outcome ReadJsonFile(AZStd::string_view filePath, size_t maxFileSize) + { + // Read into memory first and then parse the json, rather than passing a file stream to rapidjson. + // This should avoid creating a large number of micro-reads from the file. + + auto readResult = AZ::Utils::ReadFile(filePath, maxFileSize); + if(!readResult.IsSuccess()) { - IO::SizeType length = stream.GetLength(); - - AZStd::vector memoryBuffer; - memoryBuffer.resize_no_construct(static_cast::size_type>(static_cast::size_type>(length) + 1)); - - IO::SizeType bytesRead = stream.Read(length, memoryBuffer.data()); - if (bytesRead != length) - { - return AZ::Failure(AZStd::string{"Cannot to read input stream."}); - } - - memoryBuffer.back() = 0; - - return ReadJsonString(AZStd::string_view{memoryBuffer.data(), memoryBuffer.size()}); + return AZ::Failure(readResult.GetError()); } - AZ::Outcome ReadJsonFile(AZStd::string_view filePath, size_t maxFileSize) + AZStd::string jsonContent = readResult.TakeValue(); + + auto result = ReadJsonString(jsonContent); + if (!result.IsSuccess()) { - // Read into memory first and then parse the json, rather than passing a file stream to rapidjson. - // This should avoid creating a large number of micro-reads from the file. + return AZ::Failure(AZStd::string::format("Failed to load '%.*s'. %s", AZ_STRING_ARG(filePath), result.GetError().c_str())); + } + else + { + return result; + } + } - auto readResult = AZ::Utils::ReadFile(filePath, maxFileSize); - if(!readResult.IsSuccess()) - { - return AZ::Failure(readResult.GetError()); - } - - AZStd::string jsonContent = readResult.TakeValue(); - - auto result = ReadJsonString(jsonContent); - if (!result.IsSuccess()) - { - return AZ::Failure(AZStd::string::format("Failed to load '%.*s'. %s", AZ_STRING_ARG(filePath), result.GetError().c_str())); - } - else - { - return result; - } + // Helper function to validate the JSON is structured with the standard header for a generic class + AZ::Outcome ValidateJsonClassHeader(const rapidjson::Document& jsonDocument) + { + auto typeItr = jsonDocument.FindMember(FileTypeTag); + if (typeItr == jsonDocument.MemberEnd() || !typeItr->value.IsString() || azstricmp(typeItr->value.GetString(), FileType) != 0) + { + return AZ::Failure(AZStd::string::format("Not a valid JsonSerialization file")); } - // Helper function to validate the JSON is structured with the standard header for a generic class - AZ::Outcome ValidateJsonClassHeader(const rapidjson::Document& jsonDocument) + auto nameItr = jsonDocument.FindMember(ClassNameTag); + if (nameItr == jsonDocument.MemberEnd() || !nameItr->value.IsString()) { - auto typeItr = jsonDocument.FindMember(FileTypeTag); - if (typeItr == jsonDocument.MemberEnd() || !typeItr->value.IsString() || azstricmp(typeItr->value.GetString(), FileType) != 0) - { - return AZ::Failure(AZStd::string::format("Not a valid JsonSerialization file")); - } - - auto nameItr = jsonDocument.FindMember(ClassNameTag); - if (nameItr == jsonDocument.MemberEnd() || !nameItr->value.IsString()) - { - return AZ::Failure(AZStd::string::format("File should contain ClassName")); - } - - auto dataItr = jsonDocument.FindMember(ClassDataTag); - // data can be empty but it should be an object - if (dataItr != jsonDocument.MemberEnd() && !dataItr->value.IsObject()) - { - return AZ::Failure(AZStd::string::format("ClassData should be an object")); - } - - return AZ::Success(); + return AZ::Failure(AZStd::string::format("File should contain ClassName")); } - AZ::Outcome LoadObjectFromStringByType(void* objectToLoad, const Uuid& classId, AZStd::string_view stream, - const JsonDeserializerSettings* settings) + auto dataItr = jsonDocument.FindMember(ClassDataTag); + // data can be empty but it should be an object + if (dataItr != jsonDocument.MemberEnd() && !dataItr->value.IsObject()) { - JsonDeserializerSettings loadSettings; - AZStd::string deserializeErrors; - auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); - if (!prepare.IsSuccess()) - { - return AZ::Failure(prepare.GetError()); - } + return AZ::Failure(AZStd::string::format("ClassData should be an object")); + } - auto parseResult = ReadJsonString(stream); - if (!parseResult.IsSuccess()) - { - return AZ::Failure(parseResult.GetError()); - } + return AZ::Success(); + } - const rapidjson::Document& jsonDocument = parseResult.GetValue(); + AZ::Outcome LoadObjectFromStringByType(void* objectToLoad, const Uuid& classId, AZStd::string_view stream, + const JsonDeserializerSettings* settings) + { + JsonDeserializerSettings loadSettings; + AZStd::string deserializeErrors; + auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); + if (!prepare.IsSuccess()) + { + return AZ::Failure(prepare.GetError()); + } - auto validateResult = ValidateJsonClassHeader(jsonDocument); - if (!validateResult.IsSuccess()) - { - return AZ::Failure(validateResult.GetError()); - } + auto parseResult = ReadJsonString(stream); + if (!parseResult.IsSuccess()) + { + return AZ::Failure(parseResult.GetError()); + } - const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); + const rapidjson::Document& jsonDocument = parseResult.GetValue(); - // validate class name - auto classData = loadSettings.m_serializeContext->FindClassData(classId); - if (!classData) - { - return AZ::Failure(AZStd::string::format("Try to load class from Id %s", classId.ToString().c_str())); - } + auto validateResult = ValidateJsonClassHeader(jsonDocument); + if (!validateResult.IsSuccess()) + { + return AZ::Failure(validateResult.GetError()); + } - if (azstricmp(classData->m_name, className) != 0) - { - return AZ::Failure(AZStd::string::format("Try to load class %s from class %s data", classData->m_name, className)); - } + const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); - JsonSerializationResult::ResultCode result = JsonSerialization::Load(objectToLoad, classId, jsonDocument.FindMember(ClassDataTag)->value, loadSettings); + // validate class name + auto classData = loadSettings.m_serializeContext->FindClassData(classId); + if (!classData) + { + return AZ::Failure(AZStd::string::format("Try to load class from Id %s", classId.ToString().c_str())); + } + + if (azstricmp(classData->m_name, className) != 0) + { + return AZ::Failure(AZStd::string::format("Try to load class %s from class %s data", classData->m_name, className)); + } + + JsonSerializationResult::ResultCode result = JsonSerialization::Load(objectToLoad, classId, jsonDocument.FindMember(ClassDataTag)->value, loadSettings); + + if (!WasLoadSuccess(result.GetOutcome()) || !deserializeErrors.empty()) + { + return AZ::Failure(deserializeErrors); + } + + return AZ::Success(); + } + + AZ::Outcome LoadObjectFromStreamByType(void* objectToLoad, const Uuid& classId, IO::GenericStream& stream, + const JsonDeserializerSettings* settings) + { + JsonDeserializerSettings loadSettings; + AZStd::string deserializeErrors; + auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); + if (!prepare.IsSuccess()) + { + return AZ::Failure(prepare.GetError()); + } + + auto parseResult = ReadJsonStream(stream); + if (!parseResult.IsSuccess()) + { + return AZ::Failure(parseResult.GetError()); + } + + const rapidjson::Document& jsonDocument = parseResult.GetValue(); + + auto validateResult = ValidateJsonClassHeader(jsonDocument); + if (!validateResult.IsSuccess()) + { + return AZ::Failure(validateResult.GetError()); + } + + const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); + + // validate class name + auto classData = loadSettings.m_serializeContext->FindClassData(classId); + if (!classData) + { + return AZ::Failure(AZStd::string::format("Try to load class from Id %s", classId.ToString().c_str())); + } + + if (azstricmp(classData->m_name, className) != 0) + { + return AZ::Failure(AZStd::string::format("Try to load class %s from class %s data", classData->m_name, className)); + } + + JsonSerializationResult::ResultCode result = JsonSerialization::Load(objectToLoad, classId, jsonDocument.FindMember(ClassDataTag)->value, loadSettings); + + if (!WasLoadSuccess(result.GetOutcome()) || !deserializeErrors.empty()) + { + return AZ::Failure(deserializeErrors); + } + + return AZ::Success(); + } + + AZ::Outcome LoadAnyObjectFromStream(IO::GenericStream& stream, const JsonDeserializerSettings* settings) + { + JsonDeserializerSettings loadSettings; + AZStd::string deserializeErrors; + auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); + if (!prepare.IsSuccess()) + { + return AZ::Failure(prepare.GetError()); + } + + auto parseResult = ReadJsonStream(stream); + if (!parseResult.IsSuccess()) + { + return AZ::Failure(parseResult.GetError()); + } + + const rapidjson::Document& jsonDocument = parseResult.GetValue(); + + auto validateResult = ValidateJsonClassHeader(jsonDocument); + if (!parseResult.IsSuccess()) + { + return AZ::Failure(parseResult.GetError()); + } + + const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); + AZStd::vector ids = loadSettings.m_serializeContext->FindClassId(AZ::Crc32(className)); + + // Load with first found class id + if (ids.size() >= 1) + { + auto classId = ids[0]; + AZStd::any anyData = loadSettings.m_serializeContext->CreateAny(classId); + auto& objectData = jsonDocument.FindMember(ClassDataTag)->value; + JsonSerializationResult::ResultCode result = JsonSerialization::Load(AZStd::any_cast(&anyData), classId, objectData, loadSettings); if (!WasLoadSuccess(result.GetOutcome()) || !deserializeErrors.empty()) { return AZ::Failure(deserializeErrors); } - return AZ::Success(); + return AZ::Success(anyData); } - AZ::Outcome LoadObjectFromStreamByType(void* objectToLoad, const Uuid& classId, IO::GenericStream& stream, - const JsonDeserializerSettings* settings) + return AZ::Failure(AZStd::string::format("Can't find serialize context for class %s", className)); + } + + AZ::Outcome LoadAnyObjectFromFile(const AZStd::string& filePath, const JsonDeserializerSettings* settings) + { + AZ::IO::FileIOStream inputFileStream; + if (!inputFileStream.Open(filePath.c_str(), AZ::IO::OpenMode::ModeRead | AZ::IO::OpenMode::ModeText)) { - JsonDeserializerSettings loadSettings; - AZStd::string deserializeErrors; - auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); - if (!prepare.IsSuccess()) - { - return AZ::Failure(prepare.GetError()); - } - - auto parseResult = ReadJsonStream(stream); - if (!parseResult.IsSuccess()) - { - return AZ::Failure(parseResult.GetError()); - } - - const rapidjson::Document& jsonDocument = parseResult.GetValue(); - - auto validateResult = ValidateJsonClassHeader(jsonDocument); - if (!validateResult.IsSuccess()) - { - return AZ::Failure(validateResult.GetError()); - } - - const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); - - // validate class name - auto classData = loadSettings.m_serializeContext->FindClassData(classId); - if (!classData) - { - return AZ::Failure(AZStd::string::format("Try to load class from Id %s", classId.ToString().c_str())); - } - - if (azstricmp(classData->m_name, className) != 0) - { - return AZ::Failure(AZStd::string::format("Try to load class %s from class %s data", classData->m_name, className)); - } - - JsonSerializationResult::ResultCode result = JsonSerialization::Load(objectToLoad, classId, jsonDocument.FindMember(ClassDataTag)->value, loadSettings); - - if (!WasLoadSuccess(result.GetOutcome()) || !deserializeErrors.empty()) - { - return AZ::Failure(deserializeErrors); - } - - return AZ::Success(); - } - - AZ::Outcome LoadAnyObjectFromStream(IO::GenericStream& stream, const JsonDeserializerSettings* settings) - { - JsonDeserializerSettings loadSettings; - AZStd::string deserializeErrors; - auto prepare = PrepareDeserializerSettings(settings, loadSettings, deserializeErrors); - if (!prepare.IsSuccess()) - { - return AZ::Failure(prepare.GetError()); - } - - auto parseResult = ReadJsonStream(stream); - if (!parseResult.IsSuccess()) - { - return AZ::Failure(parseResult.GetError()); - } - - const rapidjson::Document& jsonDocument = parseResult.GetValue(); - - auto validateResult = ValidateJsonClassHeader(jsonDocument); - if (!parseResult.IsSuccess()) - { - return AZ::Failure(parseResult.GetError()); - } - - const char* className = jsonDocument.FindMember(ClassNameTag)->value.GetString(); - AZStd::vector ids = loadSettings.m_serializeContext->FindClassId(AZ::Crc32(className)); - - // Load with first found class id - if (ids.size() >= 1) - { - auto classId = ids[0]; - AZStd::any anyData = loadSettings.m_serializeContext->CreateAny(classId); - auto& objectData = jsonDocument.FindMember(ClassDataTag)->value; - JsonSerializationResult::ResultCode result = JsonSerialization::Load(AZStd::any_cast(&anyData), classId, objectData, loadSettings); - - if (!WasLoadSuccess(result.GetOutcome()) || !deserializeErrors.empty()) - { - return AZ::Failure(deserializeErrors); - } - - return AZ::Success(anyData); - } - - return AZ::Failure(AZStd::string::format("Can't find serialize context for class %s", className)); + return AZ::Failure(AZStd::string::format("Error opening file '%s' for reading", filePath.c_str())); } + return LoadAnyObjectFromStream(inputFileStream, settings); + } - AZ::Outcome LoadAnyObjectFromFile(const AZStd::string& filePath, const JsonDeserializerSettings* settings) - { - AZ::IO::FileIOStream inputFileStream; - if (!inputFileStream.Open(filePath.c_str(), AZ::IO::OpenMode::ModeRead | AZ::IO::OpenMode::ModeText)) - { - return AZ::Failure(AZStd::string::format("Error opening file '%s' for reading", filePath.c_str())); - } - return LoadAnyObjectFromStream(inputFileStream, settings); - } - - } // namespace JsonSerializationUtils -} // namespace AZ +} // namespace AZ::JsonSerializationUtils diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.cpp b/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.cpp index 2392ff435f..ad1839f974 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.cpp @@ -15,6 +15,7 @@ namespace AZ AZ_CLASS_ALLOCATOR_IMPL(JsonAnySerializer, SystemAllocator, 0); AZ_CLASS_ALLOCATOR_IMPL(JsonVariantSerializer, SystemAllocator, 0); AZ_CLASS_ALLOCATOR_IMPL(JsonOptionalSerializer, SystemAllocator, 0); + AZ_CLASS_ALLOCATOR_IMPL(JsonBitsetSerializer, SystemAllocator, 0); JsonSerializationResult::Result JsonUnsupportedTypesSerializer::Load(void*, const Uuid&, const rapidjson::Value&, JsonDeserializerContext& context) @@ -49,4 +50,10 @@ namespace AZ return "The Json Serialization doesn't support AZStd::optional by design. No JSON format has yet been found that wasn't deemed too " "complex or overly verbose."; } + + AZStd::string_view JsonBitsetSerializer::GetMessage() const + { + return "The Json Serialization doesn't support AZStd::bitset by design. No JSON format has yet been found that is content creator " + "friendly i.e., easy to comprehend the intent."; + } } // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.h b/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.h index d913289d3d..fdcac4c761 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.h +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/UnsupportedTypesSerializer.h @@ -65,4 +65,14 @@ namespace AZ protected: AZStd::string_view GetMessage() const override; }; + + class JsonBitsetSerializer : public JsonUnsupportedTypesSerializer + { + public: + AZ_RTTI(JsonBitsetSerializer, "{10CE969D-D69E-4B3F-8593-069736F8F705}", JsonUnsupportedTypesSerializer); + AZ_CLASS_ALLOCATOR_DECL; + + protected: + AZStd::string_view GetMessage() const override; + }; } // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Serialization/ObjectStream.cpp b/Code/Framework/AzCore/AzCore/Serialization/ObjectStream.cpp index 746c80f3ea..ac44ac150c 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/ObjectStream.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/ObjectStream.cpp @@ -1050,7 +1050,7 @@ namespace AZ } m_xmlNode = next; - Uuid specializedId; + Uuid specializedId = Uuid::CreateNull(); // now parse the node rapidxml::xml_attribute* attr = m_xmlNode->first_attribute(); while (attr) @@ -1643,12 +1643,15 @@ namespace AZ m_writeElementResultStack.push_back(WriteElement(ptr, classData, classElement)); return m_writeElementResultStack.back(); }; - auto closeElementCB = [this, classData]() + auto closeElementCB = [this, classTypeId = classData->m_typeId]() { if (m_writeElementResultStack.empty()) { - AZ_UNUSED(classData); // Prevent unused warning in release builds - AZ_Error("Serialize", false, "CloseElement is attempted to be called without a corresponding WriteElement when writing class %s", classData->m_name); + // ClassData could be dangling pointer if it was unreflected by the ObjectStreamWriteOverrideCB + // So use the classTypeId instead + AZ_UNUSED(classTypeId); + AZ_Error("Serialize", false, "CloseElement is attempted to be called without a corresponding WriteElement when writing class %s", + classTypeId.ToString>().c_str()); return true; } if (m_writeElementResultStack.back()) @@ -1665,16 +1668,14 @@ namespace AZ SerializeContext::ENUM_ACCESS_FOR_READ, &m_errorLogger ); - ObjectStreamWriteOverrideCB writeCB; - if (objectStreamWriteOverrideCB.Read(writeCB)) + if (objectStreamWriteOverrideCB.Invoke(callContext, objectPtr, *classData, classElement)) { - writeCB(callContext, objectPtr, *classData, classElement); return false; } else { auto objectStreamError = AZStd::string::format("Unable to invoke ObjectStream Write Element Override for class element %s of class data %s", - classElement->m_name ? classElement->m_name : "", classData->m_name); + classElement && classElement->m_name ? classElement->m_name : "", classData->m_name); m_errorLogger.ReportError(objectStreamError.c_str()); } } diff --git a/Code/Framework/AzCore/AzCore/Serialization/SerializationUtils.cpp b/Code/Framework/AzCore/AzCore/Serialization/SerializationUtils.cpp index 37eacd940e..8439ff8e2c 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/SerializationUtils.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/SerializationUtils.cpp @@ -18,502 +18,499 @@ #include -namespace AZ +namespace AZ::Utils { - namespace Utils + bool LoadObjectFromStreamInPlace(IO::GenericStream& stream, AZ::SerializeContext* context, const SerializeContext::ClassData* objectClassData, void* targetPointer, const FilterDescriptor& filterDesc) { - bool LoadObjectFromStreamInPlace(IO::GenericStream& stream, AZ::SerializeContext* context, const SerializeContext::ClassData* objectClassData, void* targetPointer, const FilterDescriptor& filterDesc) + AZ_PROFILE_FUNCTION(AzCore); + + AZ_Assert(objectClassData, "Class data is required."); + + if (!context) { - AZ_PROFILE_FUNCTION(AzCore); - - AZ_Assert(objectClassData, "Class data is required."); - - if (!context) - { - EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); - AZ_Assert(context, "No serialize context"); - } - - if (!context) - { - return false; - } - - AZ_Assert(targetPointer, "You must provide a target pointer"); - - bool foundSuccess = false; - using CreationCallback = AZStd::function; - auto handler = [&targetPointer, objectClassData, &foundSuccess](void** instance, const SerializeContext::ClassData** classData, const Uuid& classId, SerializeContext* context) - { - void* convertibleInstance{}; - if (objectClassData->ConvertFromType(convertibleInstance, classId, targetPointer, *context)) - { - foundSuccess = true; - if (instance) - { - // The ObjectStream will ask us for the address of the target to load into, so provide it. - *instance = convertibleInstance; - } - if (classData) - { - // The ObjectStream will ask us for the class data of the target being loaded into, so provide it if needed. - // This allows us to load directly into a generic object (templated containers, strings, etc). - *classData = objectClassData; - } - } - }; - bool readSuccess = ObjectStream::LoadBlocking(&stream, *context, ObjectStream::ClassReadyCB(), filterDesc, CreationCallback(handler, AZ::OSStdAllocator())); - - AZ_Warning("Serialization", readSuccess, "LoadObjectFromStreamInPlace: Stream did not deserialize correctly"); - AZ_Warning("Serialization", foundSuccess, "LoadObjectFromStreamInPlace: Did not find the expected type in the stream"); - - return readSuccess && foundSuccess; + EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); + AZ_Assert(context, "No serialize context"); } - bool LoadObjectFromStreamInPlace(IO::GenericStream& stream, AZ::SerializeContext* context, const Uuid& targetClassId, void* targetPointer, const FilterDescriptor& filterDesc) + if (!context) { - AZ_PROFILE_FUNCTION(AzCore); - - if (!context) - { - EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); - AZ_Assert(context, "No serialize context"); - } - - if (!context) - { - return false; - } - - const SerializeContext::ClassData* classData = context->FindClassData(targetClassId); - if (!classData) - { - AZ_Error("Serialization", false, - "Unable to locate class data for uuid \"%s\". This object cannot be serialized as a root element. " - "Make sure the Uuid is valid, or if this is a generic type, use the override that takes a ClassData pointer instead.", - targetClassId.ToString().c_str()); - return false; - } - - return LoadObjectFromStreamInPlace(stream, context, classData, targetPointer, filterDesc); - } - - bool LoadObjectFromFileInPlace(const AZStd::string& filePath, const Uuid& targetClassId, void* destination, AZ::SerializeContext* context /*= nullptr*/, const FilterDescriptor& filterDesc /*= FilterDescriptor()*/) - { - AZ::IO::FileIOStream fileStream; - if (!fileStream.Open(filePath.c_str(), IO::OpenMode::ModeRead | IO::OpenMode::ModeBinary)) - { - return false; - } - - return LoadObjectFromStreamInPlace(fileStream, context, targetClassId, destination, filterDesc); - } - - void* LoadObjectFromStream(IO::GenericStream& stream, AZ::SerializeContext* context, const Uuid* targetClassId, const FilterDescriptor& filterDesc) - { - AZ_PROFILE_FUNCTION(AzCore); - - if (!context) - { - EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); - AZ_Assert(context, "No serialize context"); - } - - if (!context) - { - return nullptr; - } - - void* loadedInstance = nullptr; - bool success = ObjectStream::LoadBlocking(&stream, *context, - [&loadedInstance, targetClassId](void* classPtr, const Uuid& classId, const SerializeContext* serializeContext) - { - if (targetClassId) - { - void* instance = serializeContext->DownCast(classPtr, classId, *targetClassId); - - // Given a valid object - if (instance) - { - AZ_Assert(!loadedInstance, "loadedInstance must be NULL, otherwise we are being invoked with multiple valid objects"); - loadedInstance = instance; - return; - } - } - else - { - if (!loadedInstance) - { - loadedInstance = classPtr; - return; - } - } - - auto classData = serializeContext->FindClassData(classId); - if (classData && classData->m_factory) - { - classData->m_factory->Destroy(classPtr); - } - }, - filterDesc, - ObjectStream::InplaceLoadRootInfoCB() - ); - - if (!success) - { - return nullptr; - } - - return loadedInstance; - } - - void* LoadObjectFromFile(const AZStd::string& filePath, const Uuid& targetClassId, SerializeContext* context, const FilterDescriptor& filterDesc, int /*platformFlags*/) - { - AZ_PROFILE_FUNCTION(AzCore); - - AZ::IO::FileIOStream fileStream; - if (!fileStream.Open(filePath.c_str(), IO::OpenMode::ModeRead | IO::OpenMode::ModeBinary)) - { - return nullptr; - } - - void* loadedObject = LoadObjectFromStream(fileStream, context, &targetClassId, filterDesc); - return loadedObject; - } - - bool SaveObjectToStream(IO::GenericStream& stream, DataStream::StreamType streamType, const void* classPtr, const Uuid& classId, SerializeContext* context, const SerializeContext::ClassData* classData) - { - AZ_PROFILE_FUNCTION(AzCore); - - if (!context) - { - EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); - - if(!context) - { - AZ_Assert(false, "No serialize context"); - return false; - } - } - - if (!classPtr) - { - AZ_Assert(false, "SaveObjectToStream: classPtr is null, object cannot be serialized."); - return false; - } - - AZ::ObjectStream* objectStream = AZ::ObjectStream::Create(&stream, *context, streamType); - if (!objectStream) - { - return false; - } - - if (!objectStream->WriteClass(classPtr, classId, classData)) - { - objectStream->Finalize(); - return false; - } - - if (!objectStream->Finalize()) - { - return false; - } - - return true; - } - - bool SaveStreamToFile(const AZStd::string& filePath, const AZStd::vector& streamData, int platformFlags) - { - AZ::IO::FileIOBase* fileIo = AZ::IO::FileIOBase::GetInstance(); - AZ::IO::FixedMaxPathString resolvedPath; - if (fileIo == nullptr || !fileIo->ResolvePath(filePath.c_str(), resolvedPath.data(), resolvedPath.capacity() + 1)) - { - resolvedPath = filePath; - } - if (AZ::IO::SystemFile fileHandle; fileHandle.Open(resolvedPath.c_str(), - AZ::IO::SystemFile::SF_OPEN_CREATE | AZ::IO::SystemFile::SF_OPEN_CREATE_PATH | AZ::IO::SystemFile::SF_OPEN_WRITE_ONLY, - platformFlags)) - { - AZ::IO::SizeType bytesWritten = fileHandle.Write(streamData.data(), streamData.size()); - return bytesWritten == streamData.size(); - } - return false; } - bool SaveObjectToFile(const AZStd::string& filePath, DataStream::StreamType fileType, const void* classPtr, const Uuid& classId, SerializeContext* context, int platformFlags) - { - AZ_PROFILE_FUNCTION(AzCore); + AZ_Assert(targetPointer, "You must provide a target pointer"); - // \note This is ok for tools, but we should use the streamer to write objects directly (no memory store) - AZStd::vector dstData; - AZ::IO::ByteContainerStream > dstByteStream(&dstData); - - if (!SaveObjectToStream(dstByteStream, fileType, classPtr, classId, context)) + bool foundSuccess = false; + using CreationCallback = AZStd::function; + auto handler = [&targetPointer, objectClassData, &foundSuccess](void** instance, const SerializeContext::ClassData** classData, const Uuid& classId, SerializeContext* context) { - return false; - } - - return SaveStreamToFile(filePath, dstData, platformFlags); - } - - /*! - \brief Finds any descendant DataElementNodes of the @classElement which match each Crc32 values in the supplied elementCrcQueue - \param context SerializeContext used for looking up ClassData - \param classElement Top level DataElementNode to begin comparison each the Crc32 queue - \param elementCrcQueue Container of Crc32 values in the order in which DataElementNodes should be matched as the DataElementNode tree is traversed - \return Vector of valid pointers to DataElementNodes which match the entire element Crc32 queue - */ - AZStd::vector FindDescendantElements(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement, - const AZStd::vector& elementCrcQueue) - { - AZStd::vector dataElementNodes; - FindDescendantElements(context, classElement, dataElementNodes, elementCrcQueue.begin(), elementCrcQueue.end()); - - return dataElementNodes; - } - - /*! - \brief Finds any descendant DataElementNodes of the @classElement which match each Crc32 values in the supplied elementCrcQueue - \param context SerializeContext used for looking up ClassData - \param classElement The current DataElementNode which will be compared against be to current top Crc32 value in the Crc32 queue - \param dataElementNodes[out] Array to populate with a DataElementNode which was found by matching all Crc32 values in the Crc32 queue - \param first The current front of the Crc32 queue - \param last The end of the Crc32 queue - */ - void FindDescendantElements(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement, - AZStd::vector& dataElementNodes, AZStd::vector::const_iterator first, AZStd::vector::const_iterator last) - { - if (first == last) - { - return; - } - - for (int i = 0; i < classElement.GetNumSubElements(); ++i) - { - auto& childElement = classElement.GetSubElement(i); - if (*first == AZ::Crc32(childElement.GetName())) + void* convertibleInstance{}; + if (objectClassData->ConvertFromType(convertibleInstance, classId, targetPointer, *context)) { - if (AZStd::distance(first, last) == 1) + foundSuccess = true; + if (instance) { - dataElementNodes.push_back(&childElement); + // The ObjectStream will ask us for the address of the target to load into, so provide it. + *instance = convertibleInstance; + } + if (classData) + { + // The ObjectStream will ask us for the class data of the target being loaded into, so provide it if needed. + // This allows us to load directly into a generic object (templated containers, strings, etc). + *classData = objectClassData; + } + } + }; + bool readSuccess = ObjectStream::LoadBlocking(&stream, *context, ObjectStream::ClassReadyCB(), filterDesc, CreationCallback(handler, AZ::OSStdAllocator())); + + AZ_Warning("Serialization", readSuccess, "LoadObjectFromStreamInPlace: Stream did not deserialize correctly"); + AZ_Warning("Serialization", foundSuccess, "LoadObjectFromStreamInPlace: Did not find the expected type in the stream"); + + return readSuccess && foundSuccess; + } + + bool LoadObjectFromStreamInPlace(IO::GenericStream& stream, AZ::SerializeContext* context, const Uuid& targetClassId, void* targetPointer, const FilterDescriptor& filterDesc) + { + AZ_PROFILE_FUNCTION(AzCore); + + if (!context) + { + EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); + AZ_Assert(context, "No serialize context"); + } + + if (!context) + { + return false; + } + + const SerializeContext::ClassData* classData = context->FindClassData(targetClassId); + if (!classData) + { + AZ_Error("Serialization", false, + "Unable to locate class data for uuid \"%s\". This object cannot be serialized as a root element. " + "Make sure the Uuid is valid, or if this is a generic type, use the override that takes a ClassData pointer instead.", + targetClassId.ToString().c_str()); + return false; + } + + return LoadObjectFromStreamInPlace(stream, context, classData, targetPointer, filterDesc); + } + + bool LoadObjectFromFileInPlace(const AZStd::string& filePath, const Uuid& targetClassId, void* destination, AZ::SerializeContext* context /*= nullptr*/, const FilterDescriptor& filterDesc /*= FilterDescriptor()*/) + { + AZ::IO::FileIOStream fileStream; + if (!fileStream.Open(filePath.c_str(), IO::OpenMode::ModeRead | IO::OpenMode::ModeBinary)) + { + return false; + } + + return LoadObjectFromStreamInPlace(fileStream, context, targetClassId, destination, filterDesc); + } + + void* LoadObjectFromStream(IO::GenericStream& stream, AZ::SerializeContext* context, const Uuid* targetClassId, const FilterDescriptor& filterDesc) + { + AZ_PROFILE_FUNCTION(AzCore); + + if (!context) + { + EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); + AZ_Assert(context, "No serialize context"); + } + + if (!context) + { + return nullptr; + } + + void* loadedInstance = nullptr; + bool success = ObjectStream::LoadBlocking(&stream, *context, + [&loadedInstance, targetClassId](void* classPtr, const Uuid& classId, const SerializeContext* serializeContext) + { + if (targetClassId) + { + void* instance = serializeContext->DownCast(classPtr, classId, *targetClassId); + + // Given a valid object + if (instance) + { + AZ_Assert(!loadedInstance, "loadedInstance must be NULL, otherwise we are being invoked with multiple valid objects"); + loadedInstance = instance; + return; + } } else { - FindDescendantElements(context, childElement, dataElementNodes, AZStd::next(first), last); - } - } - } - } - - bool IsVectorContainerType(const AZ::Uuid& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - AZ::TypeId containerTypeId = type; - - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - // This type is a container - containerTypeId = classInfo->GetGenericTypeId(); - } - - if (containerTypeId == AZ::GetGenericClassInfoVectorTypeId() - || containerTypeId == AZ::GetGenericClassInfoFixedVectorTypeId() - || containerTypeId == AZ::GetGenericClassInfoArrayTypeId() - ) - { - return true; - } - } - - return false; - } - - bool IsSetContainerType(const AZ::Uuid& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - AZ::TypeId containerTypeId = type; - - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - // This type is a container - containerTypeId = classInfo->GetGenericTypeId(); - } - - if (containerTypeId == AZ::GetGenericClassSetTypeId() - || containerTypeId == AZ::GetGenericClassUnorderedSetTypeId() - ) - { - return true; - } - } - - return false; - } - - - bool IsMapContainerType(const AZ::Uuid& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - AZ::Uuid containerTypeId = type; - - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - containerTypeId = classInfo->GetGenericTypeId(); - } - - if (containerTypeId == AZ::GetGenericClassMapTypeId() - || containerTypeId == AZ::GetGenericClassUnorderedMapTypeId() - ) - { - return true; - } - } - - return false; - } - - bool IsContainerType(const AZ::Uuid& type) - { - return IsVectorContainerType(type) || IsSetContainerType(type) || IsMapContainerType(type); - } - - AZStd::vector GetContainedTypes(const AZ::Uuid& type) - { - AZStd::vector types; - - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - for (int i = 0; i < classInfo->GetNumTemplatedArguments(); ++i) - { - types.push_back(classInfo->GetTemplatedTypeId(i)); - } - } - } - - return types; - } - - bool IsOutcomeType(const AZ::Uuid& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type); - return classInfo && classInfo->GetGenericTypeId() == AZ::GetGenericOutcomeTypeId(); - } - - return false; - } - - bool IsPairContainerType(const AZ::Uuid& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - AZ::Uuid containerTypeId = type; - - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - containerTypeId = classInfo->GetGenericTypeId(); - } - - if (containerTypeId == AZ::GetGenericClassPairTypeId()) - { - return true; - } - } - - return false; - } - - AZ::TypeId GetGenericContainerType(const AZ::TypeId& type) - { - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - // This type is a container - return classInfo->GetGenericTypeId(); - } - } - - return azrtti_typeid(); - } - - bool IsGenericContainerType(const AZ::TypeId& type) - { - return IsContainerType(type) && GetGenericContainerType(type) == azrtti_typeid(); - } - - AZStd::pair GetOutcomeTypes(const AZ::Uuid& type) - { - AZStd::vector types; - - AZ::SerializeContext* serializeContext = nullptr; - AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - - if (serializeContext) - { - if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) - { - AZ_Assert(classInfo->GetNumTemplatedArguments() == 2, "Outcome template arguments must be 2, even if void, void"); - return AZStd::make_pair(classInfo->GetTemplatedTypeId(0), classInfo->GetTemplatedTypeId(1)); - } - } - - return AZStd::make_pair(azrtti_typeid(), azrtti_typeid()); - } - - void* ResolvePointer(void* ptr, const SerializeContext::ClassElement& classElement, const SerializeContext& context) - { - if (classElement.m_flags & SerializeContext::ClassElement::FLG_POINTER) - { - // In the case of pointer-to-pointer, we'll deference. - ptr = *(void**)(ptr); - - // Pointer-to-pointer fields may be base class / polymorphic, so cast pointer to actual type, - // safe for passing as 'this' to member functions. - if (ptr && classElement.m_azRtti) - { - Uuid actualClassId = classElement.m_azRtti->GetActualUuid(ptr); - if (actualClassId != classElement.m_typeId) - { - const SerializeContext::ClassData* classData = context.FindClassData(actualClassId); - if (classData) + if (!loadedInstance) { - ptr = classElement.m_azRtti->Cast(ptr, classData->m_azRtti->GetTypeId()); + loadedInstance = classPtr; + return; } } - } - } - return ptr; + auto classData = serializeContext->FindClassData(classId); + if (classData && classData->m_factory) + { + classData->m_factory->Destroy(classPtr); + } + }, + filterDesc, + ObjectStream::InplaceLoadRootInfoCB() + ); + + if (!success) + { + return nullptr; } - } // namespace Utils -} // namespace AZ + return loadedInstance; + } + + void* LoadObjectFromFile(const AZStd::string& filePath, const Uuid& targetClassId, SerializeContext* context, const FilterDescriptor& filterDesc, int /*platformFlags*/) + { + AZ_PROFILE_FUNCTION(AzCore); + + AZ::IO::FileIOStream fileStream; + if (!fileStream.Open(filePath.c_str(), IO::OpenMode::ModeRead | IO::OpenMode::ModeBinary)) + { + return nullptr; + } + + void* loadedObject = LoadObjectFromStream(fileStream, context, &targetClassId, filterDesc); + return loadedObject; + } + + bool SaveObjectToStream(IO::GenericStream& stream, DataStream::StreamType streamType, const void* classPtr, const Uuid& classId, SerializeContext* context, const SerializeContext::ClassData* classData) + { + AZ_PROFILE_FUNCTION(AzCore); + + if (!context) + { + EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext); + + if(!context) + { + AZ_Assert(false, "No serialize context"); + return false; + } + } + + if (!classPtr) + { + AZ_Assert(false, "SaveObjectToStream: classPtr is null, object cannot be serialized."); + return false; + } + + AZ::ObjectStream* objectStream = AZ::ObjectStream::Create(&stream, *context, streamType); + if (!objectStream) + { + return false; + } + + if (!objectStream->WriteClass(classPtr, classId, classData)) + { + objectStream->Finalize(); + return false; + } + + if (!objectStream->Finalize()) + { + return false; + } + + return true; + } + + bool SaveStreamToFile(const AZStd::string& filePath, const AZStd::vector& streamData, int platformFlags) + { + AZ::IO::FileIOBase* fileIo = AZ::IO::FileIOBase::GetInstance(); + AZ::IO::FixedMaxPathString resolvedPath; + if (fileIo == nullptr || !fileIo->ResolvePath(filePath.c_str(), resolvedPath.data(), resolvedPath.capacity() + 1)) + { + resolvedPath = filePath; + } + if (AZ::IO::SystemFile fileHandle; fileHandle.Open(resolvedPath.c_str(), + AZ::IO::SystemFile::SF_OPEN_CREATE | AZ::IO::SystemFile::SF_OPEN_CREATE_PATH | AZ::IO::SystemFile::SF_OPEN_WRITE_ONLY, + platformFlags)) + { + AZ::IO::SizeType bytesWritten = fileHandle.Write(streamData.data(), streamData.size()); + return bytesWritten == streamData.size(); + } + + return false; + } + + bool SaveObjectToFile(const AZStd::string& filePath, DataStream::StreamType fileType, const void* classPtr, const Uuid& classId, SerializeContext* context, int platformFlags) + { + AZ_PROFILE_FUNCTION(AzCore); + + // \note This is ok for tools, but we should use the streamer to write objects directly (no memory store) + AZStd::vector dstData; + AZ::IO::ByteContainerStream > dstByteStream(&dstData); + + if (!SaveObjectToStream(dstByteStream, fileType, classPtr, classId, context)) + { + return false; + } + + return SaveStreamToFile(filePath, dstData, platformFlags); + } + + /*! + \brief Finds any descendant DataElementNodes of the @classElement which match each Crc32 values in the supplied elementCrcQueue + \param context SerializeContext used for looking up ClassData + \param classElement Top level DataElementNode to begin comparison each the Crc32 queue + \param elementCrcQueue Container of Crc32 values in the order in which DataElementNodes should be matched as the DataElementNode tree is traversed + \return Vector of valid pointers to DataElementNodes which match the entire element Crc32 queue + */ + AZStd::vector FindDescendantElements(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement, + const AZStd::vector& elementCrcQueue) + { + AZStd::vector dataElementNodes; + FindDescendantElements(context, classElement, dataElementNodes, elementCrcQueue.begin(), elementCrcQueue.end()); + + return dataElementNodes; + } + + /*! + \brief Finds any descendant DataElementNodes of the @classElement which match each Crc32 values in the supplied elementCrcQueue + \param context SerializeContext used for looking up ClassData + \param classElement The current DataElementNode which will be compared against be to current top Crc32 value in the Crc32 queue + \param dataElementNodes[out] Array to populate with a DataElementNode which was found by matching all Crc32 values in the Crc32 queue + \param first The current front of the Crc32 queue + \param last The end of the Crc32 queue + */ + void FindDescendantElements(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement, + AZStd::vector& dataElementNodes, AZStd::vector::const_iterator first, AZStd::vector::const_iterator last) + { + if (first == last) + { + return; + } + + for (int i = 0; i < classElement.GetNumSubElements(); ++i) + { + auto& childElement = classElement.GetSubElement(i); + if (*first == AZ::Crc32(childElement.GetName())) + { + if (AZStd::distance(first, last) == 1) + { + dataElementNodes.push_back(&childElement); + } + else + { + FindDescendantElements(context, childElement, dataElementNodes, AZStd::next(first), last); + } + } + } + } + + bool IsVectorContainerType(const AZ::Uuid& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + AZ::TypeId containerTypeId = type; + + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + // This type is a container + containerTypeId = classInfo->GetGenericTypeId(); + } + + if (containerTypeId == AZ::GetGenericClassInfoVectorTypeId() + || containerTypeId == AZ::GetGenericClassInfoFixedVectorTypeId() + || containerTypeId == AZ::GetGenericClassInfoArrayTypeId() + ) + { + return true; + } + } + + return false; + } + + bool IsSetContainerType(const AZ::Uuid& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + AZ::TypeId containerTypeId = type; + + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + // This type is a container + containerTypeId = classInfo->GetGenericTypeId(); + } + + if (containerTypeId == AZ::GetGenericClassSetTypeId() + || containerTypeId == AZ::GetGenericClassUnorderedSetTypeId() + ) + { + return true; + } + } + + return false; + } + + + bool IsMapContainerType(const AZ::Uuid& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + AZ::Uuid containerTypeId = type; + + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + containerTypeId = classInfo->GetGenericTypeId(); + } + + if (containerTypeId == AZ::GetGenericClassMapTypeId() + || containerTypeId == AZ::GetGenericClassUnorderedMapTypeId() + ) + { + return true; + } + } + + return false; + } + + bool IsContainerType(const AZ::Uuid& type) + { + return IsVectorContainerType(type) || IsSetContainerType(type) || IsMapContainerType(type); + } + + AZStd::vector GetContainedTypes(const AZ::Uuid& type) + { + AZStd::vector types; + + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + for (int i = 0; i < classInfo->GetNumTemplatedArguments(); ++i) + { + types.push_back(classInfo->GetTemplatedTypeId(i)); + } + } + } + + return types; + } + + bool IsOutcomeType(const AZ::Uuid& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type); + return classInfo && classInfo->GetGenericTypeId() == AZ::GetGenericOutcomeTypeId(); + } + + return false; + } + + bool IsPairContainerType(const AZ::Uuid& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + AZ::Uuid containerTypeId = type; + + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + containerTypeId = classInfo->GetGenericTypeId(); + } + + if (containerTypeId == AZ::GetGenericClassPairTypeId()) + { + return true; + } + } + + return false; + } + + AZ::TypeId GetGenericContainerType(const AZ::TypeId& type) + { + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + // This type is a container + return classInfo->GetGenericTypeId(); + } + } + + return azrtti_typeid(); + } + + bool IsGenericContainerType(const AZ::TypeId& type) + { + return IsContainerType(type) && GetGenericContainerType(type) == azrtti_typeid(); + } + + AZStd::pair GetOutcomeTypes(const AZ::Uuid& type) + { + AZStd::vector types; + + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); + + if (serializeContext) + { + if (GenericClassInfo* classInfo = serializeContext->FindGenericClassInfo(type)) + { + AZ_Assert(classInfo->GetNumTemplatedArguments() == 2, "Outcome template arguments must be 2, even if void, void"); + return AZStd::make_pair(classInfo->GetTemplatedTypeId(0), classInfo->GetTemplatedTypeId(1)); + } + } + + return AZStd::make_pair(azrtti_typeid(), azrtti_typeid()); + } + + void* ResolvePointer(void* ptr, const SerializeContext::ClassElement& classElement, const SerializeContext& context) + { + if (classElement.m_flags & SerializeContext::ClassElement::FLG_POINTER) + { + // In the case of pointer-to-pointer, we'll deference. + ptr = *(void**)(ptr); + + // Pointer-to-pointer fields may be base class / polymorphic, so cast pointer to actual type, + // safe for passing as 'this' to member functions. + if (ptr && classElement.m_azRtti) + { + Uuid actualClassId = classElement.m_azRtti->GetActualUuid(ptr); + if (actualClassId != classElement.m_typeId) + { + const SerializeContext::ClassData* classData = context.FindClassData(actualClassId); + if (classData) + { + ptr = classElement.m_azRtti->Cast(ptr, classData->m_azRtti->GetTypeId()); + } + } + } + } + + return ptr; + } + +} // namespace AZ::Utils diff --git a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp index 81546f4f28..ff0ad571f7 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp @@ -2065,11 +2065,15 @@ namespace AZ } EnumerateInstanceCallContext callContext( - AZStd::bind(&SerializeContext::BeginCloneElement, this, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3, &cloneData, &m_errorLogger, &scratchBuffer), - AZStd::bind(&SerializeContext::EndCloneElement, this, &cloneData), - this, - SerializeContext::ENUM_ACCESS_FOR_READ, - &m_errorLogger); + [&](void* ptr, const ClassData* classData, const ClassElement* elementData) -> bool + { + return BeginCloneElement(ptr, classData, elementData, &cloneData, &m_errorLogger, &scratchBuffer); + }, + [&]() -> bool + { + return EndCloneElement(&cloneData); + }, + this, SerializeContext::ENUM_ACCESS_FOR_READ, &m_errorLogger); EnumerateInstance( &callContext @@ -2098,19 +2102,17 @@ namespace AZ if (ptr) { EnumerateInstanceCallContext callContext( - AZStd::bind(&SerializeContext::BeginCloneElementInplace, this, dest, AZStd::placeholders::_1, AZStd::placeholders::_2, AZStd::placeholders::_3, &cloneData, &m_errorLogger, &scratchBuffer), - AZStd::bind(&SerializeContext::EndCloneElement, this, &cloneData), - this, - SerializeContext::ENUM_ACCESS_FOR_READ, - &m_errorLogger); + [&](void* ptr, const ClassData* classData, const ClassElement* elementData) -> bool + { + return BeginCloneElementInplace(dest, ptr, classData, elementData, &cloneData, &m_errorLogger, &scratchBuffer); + }, + [&]() -> bool + { + return EndCloneElement(&cloneData); + }, + this, SerializeContext::ENUM_ACCESS_FOR_READ, &m_errorLogger); - EnumerateInstance( - &callContext - , const_cast(ptr) - , classId - , nullptr - , nullptr - ); + EnumerateInstance(&callContext, const_cast(ptr), classId, nullptr, nullptr); } } @@ -2941,14 +2943,10 @@ namespace AZ { m_errorHandler = errorHandler ? errorHandler : &m_defaultErrorHandler; - m_elementCallback = AZStd::bind(static_cast(&SerializeContext::EnumerateInstance) - , m_context - , this - , AZStd::placeholders::_1 - , AZStd::placeholders::_2 - , AZStd::placeholders::_3 - , AZStd::placeholders::_4 - ); + m_elementCallback = [this](void* ptr, const Uuid& classId, const ClassData* classData, const ClassElement* classElement)->bool + { + return m_context->EnumerateInstance(this, ptr, classId, classData, classElement); + }; } //========================================================================= diff --git a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h index a37780029e..bf96bcdb9a 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h +++ b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h @@ -90,7 +90,7 @@ namespace AZ using AttributePtr = AZStd::shared_ptr; using AttributeSharedPair = AZStd::pair; - template + template > AttributePtr CreateModuleAttribute(T&& attrValue); /** @@ -540,6 +540,7 @@ namespace AZ */ struct ClassElement { + AZ_TYPE_INFO(ClassElement, "{7D386902-A1D9-4525-8284-F68435FE1D05}"); enum Flags { FLG_POINTER = (1 << 0), ///< Element is stored as pointer (it's not a value). @@ -563,22 +564,22 @@ namespace AZ void ClearAttributes(); Attribute* FindAttribute(AttributeId attributeId) const; - const char* m_name; ///< Used in XML output and debugging purposes - u32 m_nameCrc; ///< CRC32 of m_name - Uuid m_typeId; - size_t m_dataSize; - size_t m_offset; + const char* m_name{ "" }; ///< Used in XML output and debugging purposes + u32 m_nameCrc{}; ///< CRC32 of m_name + Uuid m_typeId = AZ::TypeId::CreateNull(); + size_t m_dataSize{}; + size_t m_offset{}; - IRttiHelper* m_azRtti; ///< Interface used to support RTTI. + IRttiHelper* m_azRtti{}; ///< Interface used to support RTTI. GenericClassInfo* m_genericClassInfo = nullptr; ///< Valid when the generic class is set. So you don't search for the actual type in the class register. - Edit::ElementData* m_editData; ///< Pointer to edit data (generated by EditContext). + Edit::ElementData* m_editData{}; ///< Pointer to edit data (generated by EditContext). AZStd::vector m_attributes{ AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return AZ::AllocatorInstance::Get(); }) }; ///< Attributes attached to ClassElement. Lambda is required here as AZStdFunctorAllocator expects a function pointer ///< that returns an IAllocatorAllocate& and the AZ::AllocatorInstance::Get returns an AZ::SystemAllocator& /// which while it inherits from IAllocatorAllocate, does not work as function pointers do not support covariant return types AttributeOwnership m_attributeOwnership = AttributeOwnership::Parent; - int m_flags; ///< + int m_flags{}; ///< }; typedef AZStd::vector ClassElementArray; @@ -589,6 +590,8 @@ namespace AZ class ClassData { public: + AZ_TYPE_INFO(ClassData, "{20EB8E2E-D807-4039-84E2-CE37D7647CD4}"); + ClassData(); ~ClassData() { ClearAttributes(); } ClassData(ClassData&&) = default; @@ -1040,6 +1043,7 @@ namespace AZ */ struct EnumerateInstanceCallContext { + AZ_TYPE_INFO(EnumerateInstanceCallContext, "{FCC1DB4B-72BD-4D78-9C23-C84B91589D33}"); EnumerateInstanceCallContext(const BeginElemEnumCB& beginElemCB, const EndElemEnumCB& endElemCB, const SerializeContext* context, unsigned int accessflags, ErrorHandler* errorHandler); BeginElemEnumCB m_beginElemCB; ///< Optional callback when entering an element's hierarchy. @@ -2539,7 +2543,7 @@ namespace AZ /// associated with current module /// @param attrValue value to store within the attribute /// @param ContainerType second parameter which is used for function parameter deduction - template + template AttributePtr CreateModuleAttribute(T&& attrValue) { IAllocatorAllocate& moduleAllocator = GetCurrentSerializeContextModule().GetAllocator(); diff --git a/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl b/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl index 70972bb846..06d4f76c80 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl +++ b/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl @@ -433,9 +433,7 @@ namespace AZ m_classData.m_attributes.set_allocator(AZStd::move(dllAllocator)); // Create the ObjectStreamWriteOverrideCB in the current module - using ContainerType = AttributeData>; - m_classData.m_attributes.emplace_back(AZ_CRC("ObjectStreamWriteElementOverride", 0x35eb659f), CreateModuleAttribute(&ObjectStreamWriter)); + m_classData.m_attributes.emplace_back(AZ_CRC("ObjectStreamWriteElementOverride", 0x35eb659f), CreateModuleAttribute(&ObjectStreamWriter)); } SerializeContext::ClassData* GetClassData() override diff --git a/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryImpl.cpp b/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryImpl.cpp index cbf25c29d4..43c8a64b93 100644 --- a/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryImpl.cpp +++ b/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryImpl.cpp @@ -327,7 +327,7 @@ namespace AZ while (!localNotifierQueue.empty()) { - for (SignalNotifierArgs notifierArgs : localNotifierQueue) + for (const SignalNotifierArgs& notifierArgs : localNotifierQueue) { localNotifierEvent.Signal(notifierArgs.m_jsonPath, notifierArgs.m_type); } diff --git a/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryMergeUtils.cpp b/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryMergeUtils.cpp index 3668ab14fd..975217a131 100644 --- a/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryMergeUtils.cpp +++ b/Code/Framework/AzCore/AzCore/Settings/SettingsRegistryMergeUtils.cpp @@ -266,7 +266,8 @@ namespace AZ::SettingsRegistryMergeUtils // Step 3 locate the project root and attempt to find the engine root using the registered engine // for the project in the project.json file - AZ::IO::FixedMaxPath projectRoot = FindProjectRoot(settingsRegistry); + AZ::IO::FixedMaxPath projectRoot; + settingsRegistry.Get(projectRoot.Native(), FilePathKey_ProjectPath); if (projectRoot.empty()) { return {}; @@ -668,7 +669,7 @@ namespace AZ::SettingsRegistryMergeUtils // NOTE: We make the project-path in the BootstrapSettingsRootKey absolute first AZ::IO::FixedMaxPath projectPath = FindProjectRoot(registry); - if (constexpr auto projectPathKey = FixedValueString(BootstrapSettingsRootKey) + "/project_path"; + if ([[maybe_unused]] constexpr auto projectPathKey = FixedValueString(BootstrapSettingsRootKey) + "/project_path"; !projectPath.empty()) { if (projectPath.IsRelative()) @@ -693,6 +694,7 @@ namespace AZ::SettingsRegistryMergeUtils R"(Project path isn't set in the Settings Registry at "%.*s".)" " Project-related filepaths will be set relative to the executable directory\n", AZ_STRING_ARG(projectPathKey)); + projectPath = exePath; registry.Set(FilePathKey_ProjectPath, exePath.Native()); } diff --git a/Code/Framework/AzCore/AzCore/Slice/SliceComponent.cpp b/Code/Framework/AzCore/AzCore/Slice/SliceComponent.cpp index 0b410369f0..ca89e95162 100644 --- a/Code/Framework/AzCore/AzCore/Slice/SliceComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Slice/SliceComponent.cpp @@ -840,7 +840,7 @@ namespace AZ } SliceComponent::SliceInstance* SliceComponent::SliceReference::CreateInstanceFromExistingEntities(AZStd::vector& entities, - const EntityIdToEntityIdMap assetToLiveIdMap, + const EntityIdToEntityIdMap& assetToLiveIdMap, SliceInstanceId sliceInstanceId) { AZ_PROFILE_FUNCTION(AzCore); diff --git a/Code/Framework/AzCore/AzCore/Slice/SliceComponent.h b/Code/Framework/AzCore/AzCore/Slice/SliceComponent.h index 7a66167a96..ae4ec155c7 100644 --- a/Code/Framework/AzCore/AzCore/Slice/SliceComponent.h +++ b/Code/Framework/AzCore/AzCore/Slice/SliceComponent.h @@ -443,7 +443,7 @@ namespace AZ * @return A pointer to the newly created slice instance. Returns nullptr on error or if the SliceComponent is not instantiated. */ SliceInstance* CreateInstanceFromExistingEntities(AZStd::vector& entities, - const EntityIdToEntityIdMap assetToLiveIdMap, + const EntityIdToEntityIdMap& assetToLiveIdMap, SliceInstanceId sliceInstanceId = SliceInstanceId::CreateRandom()); /** diff --git a/Code/Framework/AzCore/AzCore/Socket/AzSocket.cpp b/Code/Framework/AzCore/AzCore/Socket/AzSocket.cpp index f15c288ad8..71c0aeea6d 100644 --- a/Code/Framework/AzCore/AzCore/Socket/AzSocket.cpp +++ b/Code/Framework/AzCore/AzCore/Socket/AzSocket.cpp @@ -8,77 +8,74 @@ #include -namespace AZ +namespace AZ::AzSock { - namespace AzSock + AzSocketAddress::AzSocketAddress() { - AzSocketAddress::AzSocketAddress() - { - Reset(); - } - - AzSocketAddress& AzSocketAddress::operator=(const AZSOCKADDR& addr) - { - m_sockAddr = *reinterpret_cast(&addr); - return *this; - } - - bool AzSocketAddress::operator==(const AzSocketAddress& rhs) const - { - return m_sockAddr.sin_family == rhs.m_sockAddr.sin_family - && m_sockAddr.sin_addr.s_addr == rhs.m_sockAddr.sin_addr.s_addr - && m_sockAddr.sin_port == rhs.m_sockAddr.sin_port; - } - - const AZSOCKADDR* AzSocketAddress::GetTargetAddress() const - { - return reinterpret_cast(&m_sockAddr); - } - - AZStd::string AzSocketAddress::GetIP() const - { - char ip[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, const_cast(&m_sockAddr.sin_addr), ip, AZ_ARRAY_SIZE(ip)); - return AZStd::string(ip); - } - - AZStd::string AzSocketAddress::GetAddress() const - { - char ip[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, const_cast(&m_sockAddr.sin_addr), ip, AZ_ARRAY_SIZE(ip)); - return AZStd::string::format("%s:%d", ip, AZ::AzSock::NetToHostShort(m_sockAddr.sin_port)); - } - - AZ::u16 AzSocketAddress::GetAddrPort() const - { - return AZ::AzSock::NetToHostShort(m_sockAddr.sin_port); - } - - void AzSocketAddress::SetAddrPort(AZ::u16 port) - { - m_sockAddr.sin_port = AZ::AzSock::HostToNetShort(port); - } - - bool AzSocketAddress::SetAddress(const AZStd::string& ip, AZ::u16 port) - { - AZ_Assert(!ip.empty(), "Invalid address string!"); - Reset(); - return AZ::AzSock::ResolveAddress(ip, port, m_sockAddr); - } - - bool AzSocketAddress::SetAddress(AZ::u32 ip, AZ::u16 port) - { - Reset(); - m_sockAddr.sin_addr.s_addr = AZ::AzSock::HostToNetLong(ip); - m_sockAddr.sin_port = AZ::AzSock::HostToNetShort(port); - return true; - } - - void AzSocketAddress::Reset() - { - memset(&m_sockAddr, 0, sizeof(m_sockAddr)); - m_sockAddr.sin_family = AF_INET; - m_sockAddr.sin_addr.s_addr = INADDR_ANY; - } + Reset(); } -} + + AzSocketAddress& AzSocketAddress::operator=(const AZSOCKADDR& addr) + { + m_sockAddr = *reinterpret_cast(&addr); + return *this; + } + + bool AzSocketAddress::operator==(const AzSocketAddress& rhs) const + { + return m_sockAddr.sin_family == rhs.m_sockAddr.sin_family + && m_sockAddr.sin_addr.s_addr == rhs.m_sockAddr.sin_addr.s_addr + && m_sockAddr.sin_port == rhs.m_sockAddr.sin_port; + } + + const AZSOCKADDR* AzSocketAddress::GetTargetAddress() const + { + return reinterpret_cast(&m_sockAddr); + } + + AZStd::string AzSocketAddress::GetIP() const + { + char ip[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, const_cast(&m_sockAddr.sin_addr), ip, AZ_ARRAY_SIZE(ip)); + return AZStd::string(ip); + } + + AZStd::string AzSocketAddress::GetAddress() const + { + char ip[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, const_cast(&m_sockAddr.sin_addr), ip, AZ_ARRAY_SIZE(ip)); + return AZStd::string::format("%s:%d", ip, AZ::AzSock::NetToHostShort(m_sockAddr.sin_port)); + } + + AZ::u16 AzSocketAddress::GetAddrPort() const + { + return AZ::AzSock::NetToHostShort(m_sockAddr.sin_port); + } + + void AzSocketAddress::SetAddrPort(AZ::u16 port) + { + m_sockAddr.sin_port = AZ::AzSock::HostToNetShort(port); + } + + bool AzSocketAddress::SetAddress(const AZStd::string& ip, AZ::u16 port) + { + AZ_Assert(!ip.empty(), "Invalid address string!"); + Reset(); + return AZ::AzSock::ResolveAddress(ip, port, m_sockAddr); + } + + bool AzSocketAddress::SetAddress(AZ::u32 ip, AZ::u16 port) + { + Reset(); + m_sockAddr.sin_addr.s_addr = AZ::AzSock::HostToNetLong(ip); + m_sockAddr.sin_port = AZ::AzSock::HostToNetShort(port); + return true; + } + + void AzSocketAddress::Reset() + { + memset(&m_sockAddr, 0, sizeof(m_sockAddr)); + m_sockAddr.sin_family = AF_INET; + m_sockAddr.sin_addr.s_addr = INADDR_ANY; + } +} // namespace AZ::AzSock diff --git a/Code/Framework/AzCore/AzCore/Statistics/RunningStatistic.cpp b/Code/Framework/AzCore/AzCore/Statistics/RunningStatistic.cpp index 47f85c8309..3093d38a10 100644 --- a/Code/Framework/AzCore/AzCore/Statistics/RunningStatistic.cpp +++ b/Code/Framework/AzCore/AzCore/Statistics/RunningStatistic.cpp @@ -10,66 +10,63 @@ #include "RunningStatistic.h" -namespace AZ +namespace AZ::Statistics { - namespace Statistics + void RunningStatistic::Reset() { - void RunningStatistic::Reset() + m_numSamples = 0; + m_mostRecentSample = 0.0; + m_minimum = 0.0; + m_maximum = 0.0; + m_sum = 0.0; + m_average = 0.0; + m_varianceTracking = 0.0; + } + + void RunningStatistic::PushSample(double value) + { + m_numSamples++; + m_mostRecentSample = value; + m_sum += value; + + if (m_numSamples == 1) { - m_numSamples = 0; - m_mostRecentSample = 0.0; - m_minimum = 0.0; - m_maximum = 0.0; - m_sum = 0.0; - m_average = 0.0; - m_varianceTracking = 0.0; + m_minimum = value; + m_maximum = value; + m_average = value; + return; } - void RunningStatistic::PushSample(double value) + if (value < m_minimum) { - m_numSamples++; - m_mostRecentSample = value; - m_sum += value; - - if (m_numSamples == 1) - { - m_minimum = value; - m_maximum = value; - m_average = value; - return; - } - - if (value < m_minimum) - { - m_minimum = value; - } - else if (value > m_maximum) - { - m_maximum = value; - } - - //See header notes and references to understand this way of calculating - //running average & variance. - const double newAverage = m_average + (value - m_average) / m_numSamples; - m_varianceTracking = m_varianceTracking + (value - m_average)*(value - newAverage); - - m_average = newAverage; + m_minimum = value; + } + else if (value > m_maximum) + { + m_maximum = value; } - double RunningStatistic::GetVariance(VarianceType varianceType) const - { - if (m_numSamples > 1) - { - const AZ::u64 varianceDivisor = (varianceType == VarianceType::S) ? m_numSamples - 1 : m_numSamples; - return m_varianceTracking / varianceDivisor; - } - return 0.0; - } + //See header notes and references to understand this way of calculating + //running average & variance. + const double newAverage = m_average + (value - m_average) / m_numSamples; + m_varianceTracking = m_varianceTracking + (value - m_average)*(value - newAverage); - double RunningStatistic::GetStdev(VarianceType varianceType) const + m_average = newAverage; + } + + double RunningStatistic::GetVariance(VarianceType varianceType) const + { + if (m_numSamples > 1) { - return sqrt(GetVariance(varianceType)); + const AZ::u64 varianceDivisor = (varianceType == VarianceType::S) ? m_numSamples - 1 : m_numSamples; + return m_varianceTracking / varianceDivisor; } - - }//namespace Statistics -}//namespace AZ + return 0.0; + } + + double RunningStatistic::GetStdev(VarianceType varianceType) const + { + return sqrt(GetVariance(varianceType)); + } + +} // namespace AZ::Statistics diff --git a/Code/Framework/AzCore/AzCore/Statistics/StatisticalProfilerProxySystemComponent.cpp b/Code/Framework/AzCore/AzCore/Statistics/StatisticalProfilerProxySystemComponent.cpp index 00bb97b745..ef87307624 100644 --- a/Code/Framework/AzCore/AzCore/Statistics/StatisticalProfilerProxySystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Statistics/StatisticalProfilerProxySystemComponent.cpp @@ -12,55 +12,52 @@ #include "StatisticalProfilerProxySystemComponent.h" //////////////////////////////////////////////////////////////////////////////////////////////////// -namespace AZ +namespace AZ::Statistics { - namespace Statistics + StatisticalProfilerProxy* StatisticalProfilerProxy::TimedScope::m_profilerProxy = nullptr; + + //////////////////////////////////////////////////////////////////////////////////////////////// + void StatisticalProfilerProxySystemComponent::Reflect(AZ::ReflectContext* context) { - StatisticalProfilerProxy* StatisticalProfilerProxy::TimedScope::m_profilerProxy = nullptr; - - //////////////////////////////////////////////////////////////////////////////////////////////// - void StatisticalProfilerProxySystemComponent::Reflect(AZ::ReflectContext* context) + if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) { - if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) - { - serializeContext->Class() - ->Version(1); - } + serializeContext->Class() + ->Version(1); } + } - //////////////////////////////////////////////////////////////////////////////////////////////// - void StatisticalProfilerProxySystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) - { - provided.push_back(AZ_CRC("StatisticalProfilerService", 0x20066f73)); - } + //////////////////////////////////////////////////////////////////////////////////////////////// + void StatisticalProfilerProxySystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + { + provided.push_back(AZ_CRC("StatisticalProfilerService", 0x20066f73)); + } - //////////////////////////////////////////////////////////////////////////////////////////////// - void StatisticalProfilerProxySystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) - { - incompatible.push_back(AZ_CRC("StatisticalProfilerService", 0x20066f73)); - } + //////////////////////////////////////////////////////////////////////////////////////////////// + void StatisticalProfilerProxySystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) + { + incompatible.push_back(AZ_CRC("StatisticalProfilerService", 0x20066f73)); + } - //////////////////////////////////////////////////////////////////////////////////////////////// - StatisticalProfilerProxySystemComponent::StatisticalProfilerProxySystemComponent() - : m_StatisticalProfilerProxy(nullptr) - { - } + //////////////////////////////////////////////////////////////////////////////////////////////// + StatisticalProfilerProxySystemComponent::StatisticalProfilerProxySystemComponent() + : m_StatisticalProfilerProxy(nullptr) + { + } - //////////////////////////////////////////////////////////////////////////////////////////////// - StatisticalProfilerProxySystemComponent::~StatisticalProfilerProxySystemComponent() - { - } + //////////////////////////////////////////////////////////////////////////////////////////////// + StatisticalProfilerProxySystemComponent::~StatisticalProfilerProxySystemComponent() + { + } - //////////////////////////////////////////////////////////////////////////////////////////////// - void StatisticalProfilerProxySystemComponent::Activate() - { - m_StatisticalProfilerProxy = new StatisticalProfilerProxy; - } + //////////////////////////////////////////////////////////////////////////////////////////////// + void StatisticalProfilerProxySystemComponent::Activate() + { + m_StatisticalProfilerProxy = new StatisticalProfilerProxy; + } - //////////////////////////////////////////////////////////////////////////////////////////////// - void StatisticalProfilerProxySystemComponent::Deactivate() - { - delete m_StatisticalProfilerProxy; - } - } //namespace Statistics -} // namespace AZ + //////////////////////////////////////////////////////////////////////////////////////////////// + void StatisticalProfilerProxySystemComponent::Deactivate() + { + delete m_StatisticalProfilerProxy; + } +} // namespace AZ::Statistics diff --git a/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.cpp b/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.cpp index ff30291a70..8405424f7d 100644 --- a/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.cpp +++ b/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.cpp @@ -416,2147 +416,2155 @@ namespace AZ::StringFunc::Internal } -namespace AZ +namespace AZ::StringFunc { - namespace StringFunc + AZStd::string_view LStrip(AZStd::string_view in, AZStd::string_view stripCharacters) { - AZStd::string_view LStrip(AZStd::string_view in, AZStd::string_view stripCharacters) + if (size_t pos = in.find_first_not_of(stripCharacters); pos != AZStd::string_view::npos) { - if (size_t pos = in.find_first_not_of(stripCharacters); pos != AZStd::string_view::npos) - { - return in.substr(pos); - } + return in.substr(pos); + } - return {}; - }; + return {}; + }; - AZStd::string_view RStrip(AZStd::string_view in, AZStd::string_view stripCharacters) + AZStd::string_view RStrip(AZStd::string_view in, AZStd::string_view stripCharacters) + { + if (size_t pos = in.find_last_not_of(stripCharacters); pos != AZStd::string_view::npos) { - if (size_t pos = in.find_last_not_of(stripCharacters); pos != AZStd::string_view::npos) - { - return in.substr(0, pos < in.size() ? pos + 1 : pos); - } + return in.substr(0, pos < in.size() ? pos + 1 : pos); + } - return {}; - }; + return {}; + }; - AZStd::string_view StripEnds(AZStd::string_view in, AZStd::string_view stripCharacters) + AZStd::string_view StripEnds(AZStd::string_view in, AZStd::string_view stripCharacters) + { + return LStrip(RStrip(in, stripCharacters), stripCharacters); + }; + + bool Equal(const char* inA, const char* inB, bool bCaseSensitive /*= false*/, size_t n /*= 0*/) + { + if (!inA || !inB) { - return LStrip(RStrip(in, stripCharacters), stripCharacters); - }; + return false; + } - bool Equal(const char* inA, const char* inB, bool bCaseSensitive /*= false*/, size_t n /*= 0*/) + if (inA == inB) { - if (!inA || !inB) - { - return false; - } + return true; + } - if (inA == inB) + if (bCaseSensitive) + { + if (n) { - return true; - } - - if (bCaseSensitive) - { - if (n) - { - return !strncmp(inA, inB, n); - } - else - { - return !strcmp(inA, inB); - } + return !strncmp(inA, inB, n); } else { - if (n) - { - return !azstrnicmp(inA, inB, n); - } - else - { - return !azstricmp(inA, inB); - } + return !strcmp(inA, inB); } } - bool Equal(AZStd::string_view inA, AZStd::string_view inB, bool bCaseSensitive) + else { - const size_t maxCharsToCompare = inA.size(); - - return inA.size() == inB.size() && (bCaseSensitive - ? strncmp(inA.data(), inB.data(), maxCharsToCompare) == 0 - : azstrnicmp(inA.data(), inB.data(), maxCharsToCompare) == 0); - } - - bool StartsWith(AZStd::string_view sourceValue, AZStd::string_view prefixValue, bool bCaseSensitive) - { - return sourceValue.size() >= prefixValue.size() - && Equal(sourceValue.data(), prefixValue.data(), bCaseSensitive, prefixValue.size()); - } - - bool EndsWith(AZStd::string_view sourceValue, AZStd::string_view suffixValue, bool bCaseSensitive) - { - return sourceValue.size() >= suffixValue.size() - && Equal(sourceValue.substr(sourceValue.size() - suffixValue.size(), AZStd::string_view::npos).data(), suffixValue.data(), bCaseSensitive, suffixValue.size()); - } - - bool Contains(AZStd::string_view in, char ch, bool bCaseSensitive) - { - return Find(in, ch, 0, false, bCaseSensitive) != AZStd::string_view::npos; - } - bool Contains(AZStd::string_view in, AZStd::string_view sv, bool bCaseSensitive) - { - return Find(in, sv, 0, false, bCaseSensitive) != AZStd::string_view::npos; - } - - size_t Find(AZStd::string_view in, char c, size_t pos /*= 0*/, bool bReverse /*= false*/, bool bCaseSensitive /*= false*/) - { - if (in.empty()) + if (n) { - return AZStd::string::npos; + return !azstrnicmp(inA, inB, n); } - - if (pos == AZStd::string::npos) + else { - pos = 0; + return !azstricmp(inA, inB); } + } + } + bool Equal(AZStd::string_view inA, AZStd::string_view inB, bool bCaseSensitive) + { + const size_t maxCharsToCompare = inA.size(); - size_t inLen = in.size(); - if (inLen < pos) - { - return AZStd::string::npos; - } + return inA.size() == inB.size() && (bCaseSensitive + ? strncmp(inA.data(), inB.data(), maxCharsToCompare) == 0 + : azstrnicmp(inA.data(), inB.data(), maxCharsToCompare) == 0); + } + bool StartsWith(AZStd::string_view sourceValue, AZStd::string_view prefixValue, bool bCaseSensitive) + { + return sourceValue.size() >= prefixValue.size() + && Equal(sourceValue.data(), prefixValue.data(), bCaseSensitive, prefixValue.size()); + } + + bool EndsWith(AZStd::string_view sourceValue, AZStd::string_view suffixValue, bool bCaseSensitive) + { + return sourceValue.size() >= suffixValue.size() + && Equal(sourceValue.substr(sourceValue.size() - suffixValue.size(), AZStd::string_view::npos).data(), suffixValue.data(), bCaseSensitive, suffixValue.size()); + } + + bool Contains(AZStd::string_view in, char ch, bool bCaseSensitive) + { + return Find(in, ch, 0, false, bCaseSensitive) != AZStd::string_view::npos; + } + bool Contains(AZStd::string_view in, AZStd::string_view sv, bool bCaseSensitive) + { + return Find(in, sv, 0, false, bCaseSensitive) != AZStd::string_view::npos; + } + + size_t Find(AZStd::string_view in, char c, size_t pos /*= 0*/, bool bReverse /*= false*/, bool bCaseSensitive /*= false*/) + { + if (in.empty()) + { + return AZStd::string::npos; + } + + if (pos == AZStd::string::npos) + { + pos = 0; + } + + size_t inLen = in.size(); + if (inLen < pos) + { + return AZStd::string::npos; + } + + if (!bCaseSensitive) + { + c = (char)tolower(c); + } + + if (bReverse) + { + pos = inLen - pos - 1; + } + + char character; + + do + { if (!bCaseSensitive) { - c = (char)tolower(c); + character = (char)tolower(in[pos]); + } + else + { + character = in[pos]; + } + + if (character == c) + { + return pos; } if (bReverse) { - pos = inLen - pos - 1; + pos = pos > 0 ? pos-1 : pos; } - - char character; - - do + else { - if (!bCaseSensitive) - { - character = (char)tolower(in[pos]); - } - else - { - character = in[pos]; - } + pos++; + } + } while (bReverse ? pos : character != '\0'); - if (character == c) - { - return pos; - } + return AZStd::string::npos; + } - if (bReverse) - { - pos = pos > 0 ? pos-1 : pos; - } - else - { - pos++; - } - } while (bReverse ? pos : character != '\0'); + size_t Find(AZStd::string_view in, AZStd::string_view s, size_t offset /*= 0*/, bool bReverse /*= false*/, bool bCaseSensitive /*= false*/) + { + // Formally an empty string matches at the offset if it is <= to the size of the input string + if (s.empty() && offset <= in.size()) + { + return offset; + } + if (in.empty()) + { return AZStd::string::npos; } - size_t Find(AZStd::string_view in, AZStd::string_view s, size_t offset /*= 0*/, bool bReverse /*= false*/, bool bCaseSensitive /*= false*/) + const size_t inlen = in.size(); + const size_t slen = s.size(); + + if (offset == AZStd::string::npos) { - // Formally an empty string matches at the offset if it is <= to the size of the input string - if (s.empty() && offset <= in.size()) + offset = 0; + } + + if (offset + slen > inlen) + { + return AZStd::string::npos; + } + + const char* pCur; + + if (bReverse) + { + // Start at the end (- pos) + pCur = in.data() + inlen - slen - offset; + } + else + { + // Start at the beginning (+ pos) + pCur = in.data() + offset; + } + + do + { + if (bCaseSensitive) { - return offset; + if (!strncmp(pCur, s.data(), slen)) + { + return static_cast(pCur - in.data()); + } } - - if (in.empty()) + else { - return AZStd::string::npos; + if (!azstrnicmp(pCur, s.data(), slen)) + { + return static_cast(pCur - in.data()); + } } - const size_t inlen = in.size(); - const size_t slen = s.size(); - - if (offset == AZStd::string::npos) - { - offset = 0; - } - - if (offset + slen > inlen) - { - return AZStd::string::npos; - } - - const char* pCur; - if (bReverse) { - // Start at the end (- pos) - pCur = in.data() + inlen - slen - offset; + pCur--; } else { - // Start at the beginning (+ pos) - pCur = in.data() + offset; + pCur++; + } + } while (bReverse ? pCur >= in.data() : pCur - in.data() <= static_cast(inlen)); + + return AZStd::string::npos; + } + + char FirstCharacter(const char* in) + { + if (!in) + { + return '\0'; + } + if (in[0] == '\n') + { + return '\0'; + } + return in[0]; + } + + char LastCharacter(const char* in) + { + if (!in) + { + return '\0'; + } + size_t len = strlen(in); + if (!len) + { + return '\0'; + } + return in[len - 1]; + } + + AZStd::string& Append(AZStd::string& inout, const char s) + { + return inout.append(1, s); + } + + AZStd::string& Append(AZStd::string& inout, const char* str) + { + if (!str) + { + return inout; + } + return inout.append(str); + } + + AZStd::string& Prepend(AZStd::string& inout, const char s) + { + return inout.insert((size_t)0, 1, s); + } + + AZStd::string& Prepend(AZStd::string& inout, const char* str) + { + if (!str) + { + return inout; + } + return inout.insert(0, str); + } + + AZStd::string& LChop(AZStd::string& inout, size_t num) + { + return Internal::LChop(inout, num); + } + + AZStd::string_view LChop(AZStd::string_view in, size_t num) + { + return Internal::LChop(in, num); + } + + AZStd::string& RChop(AZStd::string& inout, size_t num) + { + return Internal::RChop(inout, num); + } + + AZStd::string_view RChop(AZStd::string_view in, size_t num) + { + return Internal::RChop(in, num); + } + + AZStd::string& LKeep(AZStd::string& inout, size_t pos, bool bKeepPosCharacter) + { + return Internal::LKeep(inout, pos, bKeepPosCharacter); + } + + AZStd::string& RKeep(AZStd::string& inout, size_t pos, bool bKeepPosCharacter) + { + return Internal::RKeep(inout, pos, bKeepPosCharacter); + } + + bool Replace(AZStd::string& inout, const char replaceA, const char withB, bool bCaseSensitive, bool bReplaceFirst, bool bReplaceLast) + { + return Internal::Replace(inout, replaceA, withB, bCaseSensitive, bReplaceFirst, bReplaceLast); + } + + bool Replace(AZStd::string& inout, const char* replaceA, const char* withB, bool bCaseSensitive, bool bReplaceFirst, bool bReplaceLast) + { + return Internal::Replace(inout, replaceA, withB, bCaseSensitive, bReplaceFirst, bReplaceLast); + } + + bool Strip(AZStd::string& inout, const char stripCharacter, bool bCaseSensitive, bool bStripBeginning, bool bStripEnding) + { + return Internal::Strip(inout, stripCharacter, bCaseSensitive, bStripBeginning, bStripEnding); + } + + bool Strip(AZStd::string& inout, const char* stripCharacters, bool bCaseSensitive, bool bStripBeginning, bool bStripEnding) + { + return Internal::Strip(inout, stripCharacters, bCaseSensitive, bStripBeginning, bStripEnding); + } + + AZStd::string& TrimWhiteSpace(AZStd::string& value, bool leading, bool trailing) + { + static const char* trimmable = " \t\r\n"; + if (value.length() > 0) + { + if (leading) + { + value.erase(0, value.find_first_not_of(trimmable)); + } + if (trailing) + { + value.erase(value.find_last_not_of(trimmable) + 1); + } + } + return value; + } + + void Tokenize(AZStd::string_view in, AZStd::vector& tokens, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) + { + return Tokenize(in, tokens, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); + } + + void Tokenize(AZStd::string_view in, AZStd::vector& tokens, AZStd::string_view delimiters, bool keepEmptyStrings, bool keepSpaceStrings) + { + auto insertVisitor = [&tokens](AZStd::string_view token) + { + tokens.push_back(token); + }; + return TokenizeVisitor(in, insertVisitor, delimiters, keepEmptyStrings, keepSpaceStrings); + } + + void TokenizeVisitor(AZStd::string_view in, const TokenVisitor& tokenVisitor, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) + { + return TokenizeVisitor(in, tokenVisitor, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); + } + + void TokenizeVisitor(AZStd::string_view in, const TokenVisitor& tokenVisitor, AZStd::string_view delimiters, + bool keepEmptyStrings, bool keepSpaceStrings) + { + if (delimiters.empty() || in.empty()) + { + return; + } + + while (AZStd::optional nextToken = TokenizeNext(in, delimiters)) + { + bool bIsEmpty = nextToken->empty(); + bool bIsSpaces = false; + if (!bIsEmpty) + { + AZStd::string_view strippedNextToken = StripEnds(*nextToken, " "); + bIsSpaces = strippedNextToken.empty(); } - do + if ((bIsEmpty && keepEmptyStrings) || + (bIsSpaces && keepSpaceStrings) || + (!bIsSpaces && !bIsEmpty)) { - if (bCaseSensitive) + tokenVisitor(*nextToken); + } + } + } + + void TokenizeVisitorReverse(AZStd::string_view in, const TokenVisitor& tokenVisitor, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) + { + return TokenizeVisitorReverse(in, tokenVisitor, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); + } + + void TokenizeVisitorReverse(AZStd::string_view in, const TokenVisitor& tokenVisitor, AZStd::string_view delimiters, + bool keepEmptyStrings, bool keepSpaceStrings) + { + if (delimiters.empty() || in.empty()) + { + return; + } + + while (AZStd::optional nextToken = TokenizeLast(in, delimiters)) + { + bool bIsEmpty = nextToken->empty(); + bool bIsSpaces = false; + if (!bIsEmpty) + { + AZStd::string_view strippedNextToken = StripEnds(*nextToken, " "); + bIsSpaces = strippedNextToken.empty(); + } + + if ((bIsEmpty && keepEmptyStrings) || + (bIsSpaces && keepSpaceStrings) || + (!bIsSpaces && !bIsEmpty)) + { + tokenVisitor(*nextToken); + } + } + } + + AZStd::optional TokenizeNext(AZStd::string_view& inout, const char delimiter) + { + return TokenizeNext(inout, { &delimiter, 1 }); + } + AZStd::optional TokenizeNext(AZStd::string_view& inout, AZStd::string_view delimiters) + { + if (delimiters.empty() || inout.empty()) + { + return AZStd::nullopt; + } + + AZStd::string_view resultToken; + if (size_t pos = inout.find_first_of(delimiters); pos == AZStd::string_view::npos) + { + // The delimiter has not been found, a new view containing the entire + // string will be returned and the input parameter will be set to empty + resultToken.swap(inout); + } + else + { + resultToken = { inout.data(), pos }; + // Strip off all previous characters before the delimiter plus + // the delimiter itself from the input view + inout.remove_prefix(pos + 1); + } + + return resultToken; + } + + AZStd::optional TokenizeLast(AZStd::string_view& inout, const char delimiter) + { + return TokenizeLast(inout, { &delimiter, 1 }); + } + AZStd::optional TokenizeLast(AZStd::string_view& inout, AZStd::string_view delimiters) + { + if (delimiters.empty() || inout.empty()) + { + return AZStd::nullopt; + } + + AZStd::string_view resultToken; + if (size_t pos = inout.find_last_of(delimiters); pos == AZStd::string_view::npos) + { + // The delimiter has not been found, a new view containing the entire + // string will be returned and the input parameter will be set to empty + resultToken.swap(inout); + } + else + { + resultToken = inout.substr(pos + 1); + // Strip off all previous characters before the delimiter plus + // the delimiter itself from the input view + inout = inout.substr(0, pos); + } + + return resultToken; + } + + bool FindFirstOf(AZStd::string_view inString, size_t offset, const AZStd::vector& searchStrings, uint32_t& outIndex, size_t& outOffset) + { + bool found = false; + + outIndex = 0; + outOffset = AZStd::string::npos; + for (int32_t i = 0; i < searchStrings.size(); ++i) + { + const AZStd::string& search = searchStrings[i]; + + size_t entry = inString.find(search, offset); + if (entry != AZStd::string::npos) + { + if (!found || (entry < outOffset)) { - if (!strncmp(pCur, s.data(), slen)) - { - return static_cast(pCur - in.data()); - } - } - else - { - if (!azstrnicmp(pCur, s.data(), slen)) - { - return static_cast(pCur - in.data()); - } - } - - if (bReverse) - { - pCur--; - } - else - { - pCur++; - } - } while (bReverse ? pCur >= in.data() : pCur - in.data() <= static_cast(inlen)); - - return AZStd::string::npos; - } - - char FirstCharacter(const char* in) - { - if (!in) - { - return '\0'; - } - if (in[0] == '\n') - { - return '\0'; - } - return in[0]; - } - - char LastCharacter(const char* in) - { - if (!in) - { - return '\0'; - } - size_t len = strlen(in); - if (!len) - { - return '\0'; - } - return in[len - 1]; - } - - AZStd::string& Append(AZStd::string& inout, const char s) - { - return inout.append(1, s); - } - - AZStd::string& Append(AZStd::string& inout, const char* str) - { - if (!str) - { - return inout; - } - return inout.append(str); - } - - AZStd::string& Prepend(AZStd::string& inout, const char s) - { - return inout.insert((size_t)0, 1, s); - } - - AZStd::string& Prepend(AZStd::string& inout, const char* str) - { - if (!str) - { - return inout; - } - return inout.insert(0, str); - } - - AZStd::string& LChop(AZStd::string& inout, size_t num) - { - return Internal::LChop(inout, num); - } - - AZStd::string_view LChop(AZStd::string_view in, size_t num) - { - return Internal::LChop(in, num); - } - - AZStd::string& RChop(AZStd::string& inout, size_t num) - { - return Internal::RChop(inout, num); - } - - AZStd::string_view RChop(AZStd::string_view in, size_t num) - { - return Internal::RChop(in, num); - } - - AZStd::string& LKeep(AZStd::string& inout, size_t pos, bool bKeepPosCharacter) - { - return Internal::LKeep(inout, pos, bKeepPosCharacter); - } - - AZStd::string& RKeep(AZStd::string& inout, size_t pos, bool bKeepPosCharacter) - { - return Internal::RKeep(inout, pos, bKeepPosCharacter); - } - - bool Replace(AZStd::string& inout, const char replaceA, const char withB, bool bCaseSensitive, bool bReplaceFirst, bool bReplaceLast) - { - return Internal::Replace(inout, replaceA, withB, bCaseSensitive, bReplaceFirst, bReplaceLast); - } - - bool Replace(AZStd::string& inout, const char* replaceA, const char* withB, bool bCaseSensitive, bool bReplaceFirst, bool bReplaceLast) - { - return Internal::Replace(inout, replaceA, withB, bCaseSensitive, bReplaceFirst, bReplaceLast); - } - - bool Strip(AZStd::string& inout, const char stripCharacter, bool bCaseSensitive, bool bStripBeginning, bool bStripEnding) - { - return Internal::Strip(inout, stripCharacter, bCaseSensitive, bStripBeginning, bStripEnding); - } - - bool Strip(AZStd::string& inout, const char* stripCharacters, bool bCaseSensitive, bool bStripBeginning, bool bStripEnding) - { - return Internal::Strip(inout, stripCharacters, bCaseSensitive, bStripBeginning, bStripEnding); - } - - AZStd::string& TrimWhiteSpace(AZStd::string& value, bool leading, bool trailing) - { - static const char* trimmable = " \t\r\n"; - if (value.length() > 0) - { - if (leading) - { - value.erase(0, value.find_first_not_of(trimmable)); - } - if (trailing) - { - value.erase(value.find_last_not_of(trimmable) + 1); - } - } - return value; - } - - void Tokenize(AZStd::string_view in, AZStd::vector& tokens, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) - { - return Tokenize(in, tokens, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); - } - - void Tokenize(AZStd::string_view in, AZStd::vector& tokens, AZStd::string_view delimiters, bool keepEmptyStrings, bool keepSpaceStrings) - { - auto insertVisitor = [&tokens](AZStd::string_view token) - { - tokens.push_back(token); - }; - return TokenizeVisitor(in, insertVisitor, delimiters, keepEmptyStrings, keepSpaceStrings); - } - - void TokenizeVisitor(AZStd::string_view in, const TokenVisitor& tokenVisitor, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) - { - return TokenizeVisitor(in, tokenVisitor, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); - } - - void TokenizeVisitor(AZStd::string_view in, const TokenVisitor& tokenVisitor, AZStd::string_view delimiters, - bool keepEmptyStrings, bool keepSpaceStrings) - { - if (delimiters.empty() || in.empty()) - { - return; - } - - while (AZStd::optional nextToken = TokenizeNext(in, delimiters)) - { - bool bIsEmpty = nextToken->empty(); - bool bIsSpaces = false; - if (!bIsEmpty) - { - AZStd::string_view strippedNextToken = StripEnds(*nextToken, " "); - bIsSpaces = strippedNextToken.empty(); - } - - if ((bIsEmpty && keepEmptyStrings) || - (bIsSpaces && keepSpaceStrings) || - (!bIsSpaces && !bIsEmpty)) - { - tokenVisitor(*nextToken); + found = true; + outIndex = i; + outOffset = entry; } } } - void TokenizeVisitorReverse(AZStd::string_view in, const TokenVisitor& tokenVisitor, const char delimiter, bool keepEmptyStrings, bool keepSpaceStrings) + return found; + } + + void Tokenize(AZStd::string_view input, AZStd::vector& tokens, const AZStd::vector& delimiters, bool keepEmptyStrings /*= false*/, bool keepSpaceStrings /*= false*/) + { + if (input.empty()) { - return TokenizeVisitorReverse(in, tokenVisitor, { &delimiter, 1 }, keepEmptyStrings, keepSpaceStrings); + return; } - void TokenizeVisitorReverse(AZStd::string_view in, const TokenVisitor& tokenVisitor, AZStd::string_view delimiters, - bool keepEmptyStrings, bool keepSpaceStrings) + size_t offset = 0; + for (;;) { - if (delimiters.empty() || in.empty()) + uint32_t nextMatch = 0; + size_t nextOffset = offset; + if (!FindFirstOf(input, offset, delimiters, nextMatch, nextOffset)) { - return; + // No more occurrences of a separator, consume whatever is left and exit + tokens.push_back(input.substr(offset)); + break; } - while (AZStd::optional nextToken = TokenizeLast(in, delimiters)) + // Take the substring, not including the separator, and increment our offset + AZStd::string nextSubstring = input.substr(offset, nextOffset - offset); + if (keepEmptyStrings || keepSpaceStrings || !nextSubstring.empty()) { - bool bIsEmpty = nextToken->empty(); - bool bIsSpaces = false; - if (!bIsEmpty) - { - AZStd::string_view strippedNextToken = StripEnds(*nextToken, " "); - bIsSpaces = strippedNextToken.empty(); - } - - if ((bIsEmpty && keepEmptyStrings) || - (bIsSpaces && keepSpaceStrings) || - (!bIsSpaces && !bIsEmpty)) - { - tokenVisitor(*nextToken); - } + tokens.push_back(nextSubstring); } + + offset = nextOffset + delimiters[nextMatch].size(); } + } - AZStd::optional TokenizeNext(AZStd::string_view& inout, const char delimiter) + int ToInt(const char* in) + { + if (!in) { - return TokenizeNext(inout, { &delimiter, 1 }); + return 0; } - AZStd::optional TokenizeNext(AZStd::string_view& inout, AZStd::string_view delimiters) + return atoi(in); + } + + bool LooksLikeInt(const char* in, int* pInt /*=nullptr*/) + { + if (!in) { - if (delimiters.empty() || inout.empty()) - { - return AZStd::nullopt; - } - - AZStd::string_view resultToken; - if (size_t pos = inout.find_first_of(delimiters); pos == AZStd::string_view::npos) - { - // The delimiter has not been found, a new view containing the entire - // string will be returned and the input parameter will be set to empty - resultToken.swap(inout); - } - else - { - resultToken = { inout.data(), pos }; - // Strip off all previous characters before the delimiter plus - // the delimiter itself from the input view - inout.remove_prefix(pos + 1); - } - - return resultToken; - } - - AZStd::optional TokenizeLast(AZStd::string_view& inout, const char delimiter) - { - return TokenizeLast(inout, { &delimiter, 1 }); - } - AZStd::optional TokenizeLast(AZStd::string_view& inout, AZStd::string_view delimiters) - { - if (delimiters.empty() || inout.empty()) - { - return AZStd::nullopt; - } - - AZStd::string_view resultToken; - if (size_t pos = inout.find_last_of(delimiters); pos == AZStd::string_view::npos) - { - // The delimiter has not been found, a new view containing the entire - // string will be returned and the input parameter will be set to empty - resultToken.swap(inout); - } - else - { - resultToken = inout.substr(pos + 1); - // Strip off all previous characters before the delimiter plus - // the delimiter itself from the input view - inout = inout.substr(0, pos); - } - - return resultToken; - } - - bool FindFirstOf(AZStd::string_view inString, size_t offset, const AZStd::vector& searchStrings, uint32_t& outIndex, size_t& outOffset) - { - bool found = false; - - outIndex = 0; - outOffset = AZStd::string::npos; - for (int32_t i = 0; i < searchStrings.size(); ++i) - { - const AZStd::string& search = searchStrings[i]; - - size_t entry = inString.find(search, offset); - if (entry != AZStd::string::npos) - { - if (!found || (entry < outOffset)) - { - found = true; - outIndex = i; - outOffset = entry; - } - } - } - - return found; - } - - void Tokenize(AZStd::string_view input, AZStd::vector& tokens, const AZStd::vector& delimiters, bool keepEmptyStrings /*= false*/, bool keepSpaceStrings /*= false*/) - { - if (input.empty()) - { - return; - } - - size_t offset = 0; - for (;;) - { - uint32_t nextMatch = 0; - size_t nextOffset = offset; - if (!FindFirstOf(input, offset, delimiters, nextMatch, nextOffset)) - { - // No more occurrences of a separator, consume whatever is left and exit - tokens.push_back(input.substr(offset)); - break; - } - - // Take the substring, not including the separator, and increment our offset - AZStd::string nextSubstring = input.substr(offset, nextOffset - offset); - if (keepEmptyStrings || keepSpaceStrings || !nextSubstring.empty()) - { - tokens.push_back(nextSubstring); - } - - offset = nextOffset + delimiters[nextMatch].size(); - } - } - - int ToInt(const char* in) - { - if (!in) - { - return 0; - } - return atoi(in); - } - - bool LooksLikeInt(const char* in, int* pInt /*=nullptr*/) - { - if (!in) - { - return false; - } - - //if pos is past then end of the string false - size_t len = strlen(in); - if (!len)//must at least 1 characters to work with "1" - { - return false; - } - - const char* pStr = in; - - size_t countNeg = 0; - while (*pStr != '\0' && - (isdigit(*pStr) || - *pStr == '-')) - { - if (*pStr == '-') - { - countNeg++; - } - pStr++; - } - - if (*pStr == '\0' && - countNeg < 2) - { - if (pInt) - { - *pInt = ToInt(in); - } - - return true; - } return false; } - double ToDouble(const char* in) + //if pos is past then end of the string false + size_t len = strlen(in); + if (!len)//must at least 1 characters to work with "1" { - if (!in) - { - return 0.; - } - return atof(in); - } - - bool LooksLikeDouble(const char* in, double* pDouble) - { - if (!in) - { - return false; - } - - size_t len = strlen(in); - if (len < 2)//must have at least 2 characters to work with "1." - { - return false; - } - - const char* pStr = in; - - size_t countDot = 0; - size_t countNeg = 0; - while (*pStr != '\0' && - (isdigit(*pStr) || - (*pStr == '-' || - *pStr == '.'))) - { - if (*pStr == '.') - { - countDot++; - } - if (*pStr == '-') - { - countNeg++; - } - pStr++; - } - - if (*pStr == '\0' && - countDot == 1 && - countNeg < 2) - { - if (pDouble) - { - *pDouble = ToDouble(in); - } - - return true; - } - return false; } - float ToFloat(const char* in) + const char* pStr = in; + + size_t countNeg = 0; + while (*pStr != '\0' && + (isdigit(*pStr) || + *pStr == '-')) { - if (!in) + if (*pStr == '-') { - return 0.f; + countNeg++; } - return (float)atof(in); + pStr++; } - bool LooksLikeFloat(const char* in, float* pFloat /* = nullptr */) + if (*pStr == '\0' && + countNeg < 2) { - bool result = false; - - if (pFloat) + if (pInt) { - double doubleValue = 0.0; - result = LooksLikeDouble(in, &doubleValue); - - (*pFloat) = aznumeric_cast(doubleValue); - } - else - { - result = LooksLikeDouble(in); + *pInt = ToInt(in); } - return result; + return true; } + return false; + } - bool ToBool(const char* in) + double ToDouble(const char* in) + { + if (!in) + { + return 0.; + } + return atof(in); + } + + bool LooksLikeDouble(const char* in, double* pDouble) + { + if (!in) { - bool boolValue = false; - if (LooksLikeBool(in, &boolValue)) - { - return boolValue; - } return false; } - bool LooksLikeBool(const char* in, bool* pBool /* = nullptr */) + size_t len = strlen(in); + if (len < 2)//must have at least 2 characters to work with "1." { - if (!in) - { - return false; - } - - if (!azstricmp(in, "true") || !azstricmp(in, "1")) - { - if (pBool) - { - *pBool = true; - } - return true; - } - - if (!azstricmp(in, "false") || !azstricmp(in, "0")) - { - if (pBool) - { - *pBool = false; - } - return true; - } - return false; } - template - bool LooksLikeVectorHelper(const char* in, VECTOR_TYPE* outVector) - { - AZStd::vector tokens; - Tokenize(in, tokens, ',', false, true); - if (tokens.size() == ELEMENT_COUNT) - { - float vectorValues[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + const char* pStr = in; + size_t countDot = 0; + size_t countNeg = 0; + while (*pStr != '\0' && + (isdigit(*pStr) || + (*pStr == '-' || + *pStr == '.'))) + { + if (*pStr == '.') + { + countDot++; + } + if (*pStr == '-') + { + countNeg++; + } + pStr++; + } + + if (*pStr == '\0' && + countDot == 1 && + countNeg < 2) + { + if (pDouble) + { + *pDouble = ToDouble(in); + } + + return true; + } + + return false; + } + + float ToFloat(const char* in) + { + if (!in) + { + return 0.f; + } + return (float)atof(in); + } + + bool LooksLikeFloat(const char* in, float* pFloat /* = nullptr */) + { + bool result = false; + + if (pFloat) + { + double doubleValue = 0.0; + result = LooksLikeDouble(in, &doubleValue); + + (*pFloat) = aznumeric_cast(doubleValue); + } + else + { + result = LooksLikeDouble(in); + } + + return result; + } + + bool ToBool(const char* in) + { + bool boolValue = false; + if (LooksLikeBool(in, &boolValue)) + { + return boolValue; + } + return false; + } + + bool LooksLikeBool(const char* in, bool* pBool /* = nullptr */) + { + if (!in) + { + return false; + } + + if (!azstricmp(in, "true") || !azstricmp(in, "1")) + { + if (pBool) + { + *pBool = true; + } + return true; + } + + if (!azstricmp(in, "false") || !azstricmp(in, "0")) + { + if (pBool) + { + *pBool = false; + } + return true; + } + + return false; + } + + template + bool LooksLikeVectorHelper(const char* in, VECTOR_TYPE* outVector) + { + AZStd::vector tokens; + Tokenize(in, tokens, ',', false, true); + if (tokens.size() == ELEMENT_COUNT) + { + float vectorValues[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + + for (uint32_t element = 0; element < ELEMENT_COUNT; ++element) + { + if (!LooksLikeFloat(tokens[element].c_str(), outVector ? &vectorValues[element] : nullptr)) + { + return false; + } + } + + if (outVector) + { for (uint32_t element = 0; element < ELEMENT_COUNT; ++element) { - if (!LooksLikeFloat(tokens[element].c_str(), outVector ? &vectorValues[element] : nullptr)) - { - return false; - } + outVector->SetElement(element, vectorValues[element]); } - - if (outVector) - { - for (uint32_t element = 0; element < ELEMENT_COUNT; ++element) - { - outVector->SetElement(element, vectorValues[element]); - } - } - - return true; } - return false; - } - - bool LooksLikeVector2(const char* in, AZ::Vector2* outVector) - { - return LooksLikeVectorHelper(in, outVector); - } - - AZ::Vector2 ToVector2(const char* in) - { - AZ::Vector2 vector; - LooksLikeVector2(in, &vector); - return vector; - } - - bool LooksLikeVector3(const char* in, AZ::Vector3* outVector) - { - return LooksLikeVectorHelper(in, outVector); - } - - AZ::Vector3 ToVector3(const char* in) - { - AZ::Vector3 vector; - LooksLikeVector3(in, &vector); - return vector; - } - - bool LooksLikeVector4(const char* in, AZ::Vector4* outVector) - { - return LooksLikeVectorHelper(in, outVector); - } - - AZ::Vector4 ToVector4(const char* in) - { - AZ::Vector4 vector; - LooksLikeVector4(in, &vector); - return vector; - } - - bool ToHexDump(const char* in, AZStd::string& out) - { - struct TInline - { - static void ByteToHex(char* pszHex, unsigned char bValue) - { - pszHex[0] = bValue / 16; - - if (pszHex[0] < 10) - { - pszHex[0] += '0'; - } - else - { - pszHex[0] -= 10; - pszHex[0] += 'A'; - } - - pszHex[1] = bValue % 16; - - if (pszHex[1] < 10) - { - pszHex[1] += '0'; - } - else - { - pszHex[1] -= 10; - pszHex[1] += 'A'; - } - } - }; - - size_t len = strlen(in); - if (len < 1) //must be at least 1 character to work with - { - return false; - } - - size_t nBytes = len; - - char* pszData = reinterpret_cast(azmalloc((nBytes * 2) + 1)); - - for (size_t ii = 0; ii < nBytes; ++ii) - { - TInline::ByteToHex(&pszData[ii * 2], in[ii]); - } - - pszData[nBytes * 2] = 0x00; - out = pszData; - azfree(pszData); - return true; } - bool FromHexDump(const char* in, AZStd::string& out) + return false; + } + + bool LooksLikeVector2(const char* in, AZ::Vector2* outVector) + { + return LooksLikeVectorHelper(in, outVector); + } + + AZ::Vector2 ToVector2(const char* in) + { + AZ::Vector2 vector; + LooksLikeVector2(in, &vector); + return vector; + } + + bool LooksLikeVector3(const char* in, AZ::Vector3* outVector) + { + return LooksLikeVectorHelper(in, outVector); + } + + AZ::Vector3 ToVector3(const char* in) + { + AZ::Vector3 vector; + LooksLikeVector3(in, &vector); + return vector; + } + + bool LooksLikeVector4(const char* in, AZ::Vector4* outVector) + { + return LooksLikeVectorHelper(in, outVector); + } + + AZ::Vector4 ToVector4(const char* in) + { + AZ::Vector4 vector; + LooksLikeVector4(in, &vector); + return vector; + } + + bool ToHexDump(const char* in, AZStd::string& out) + { + struct TInline { - struct TInline + static void ByteToHex(char* pszHex, unsigned char bValue) { - static unsigned char HexToByte(const char* pszHex) + pszHex[0] = bValue / 16; + + if (pszHex[0] < 10) { - unsigned char bHigh = 0; - unsigned char bLow = 0; - - if ((pszHex[0] >= '0') && (pszHex[0] <= '9')) - { - bHigh = pszHex[0] - '0'; - } - else if ((pszHex[0] >= 'A') && (pszHex[0] <= 'F')) - { - bHigh = (pszHex[0] - 'A') + 10; - } - - bHigh = bHigh << 4; - - if ((pszHex[1] >= '0') && (pszHex[1] <= '9')) - { - bLow = pszHex[1] - '0'; - } - else if ((pszHex[1] >= 'A') && (pszHex[1] <= 'F')) - { - bLow = (pszHex[1] - 'A') + 10; - } - - return bHigh | bLow; - } - }; - - size_t len = strlen(in); - if (len < 2) //must be at least 2 characters to work with - { - return false; - } - - size_t nBytes = len / 2; - char* pszData = reinterpret_cast(azmalloc(nBytes + 1)); - - for (size_t ii = 0; ii < nBytes; ++ii) - { - pszData[ii] = TInline::HexToByte(&in[ii * 2]); - } - - pszData[nBytes] = 0x00; - out = pszData; - azfree(pszData); - - return true; - } - - namespace NumberFormatting - { - int GroupDigits(char* buffer, size_t bufferSize, size_t decimalPosHint, char digitSeparator, char decimalSeparator, int groupingSize, int firstGroupingSize) - { - static const int MAX_SEPARATORS = 16; - - AZ_Assert(buffer, "Null string buffer"); - AZ_Assert(bufferSize > decimalPosHint, "Decimal position %lu cannot be located beyond bufferSize %lu", decimalPosHint, bufferSize); - AZ_Assert(groupingSize > 0, "Grouping size must be a positive integer"); - - int numberEndPos = 0; - int stringEndPos = 0; - - if (decimalPosHint > 0 && decimalPosHint < (bufferSize - 1) && buffer[decimalPosHint] == decimalSeparator) - { - // Assume the number ends at the supplied location - numberEndPos = (int)decimalPosHint; - stringEndPos = numberEndPos + (int)strnlen(buffer + numberEndPos, bufferSize - numberEndPos); + pszHex[0] += '0'; } else { - // Search for the final digit or separator while obtaining the string length - int lastDigitSeenPos = 0; - - while (stringEndPos < bufferSize) - { - char c = buffer[stringEndPos]; - - if (!c) - { - break; - } - else if (c == decimalSeparator) - { - // End the number if there's a decimal - numberEndPos = stringEndPos; - } - else if (numberEndPos <= 0 && c >= '0' && c <= '9') - { - // Otherwise keep track of where the last digit we've seen is - lastDigitSeenPos = stringEndPos; - } - - stringEndPos++; - } - - if (numberEndPos <= 0) - { - if (lastDigitSeenPos > 0) - { - // No decimal, so use the last seen digit as the end of the number - numberEndPos = lastDigitSeenPos + 1; - } - else - { - // No digits, no decimals, therefore no change in the string - return stringEndPos; - } - } + pszHex[0] -= 10; + pszHex[0] += 'A'; } - if (firstGroupingSize <= 0) + pszHex[1] = bValue % 16; + + if (pszHex[1] < 10) { - firstGroupingSize = groupingSize; + pszHex[1] += '0'; + } + else + { + pszHex[1] -= 10; + pszHex[1] += 'A'; + } + } + }; + + size_t len = strlen(in); + if (len < 1) //must be at least 1 character to work with + { + return false; + } + + size_t nBytes = len; + + char* pszData = reinterpret_cast(azmalloc((nBytes * 2) + 1)); + + for (size_t ii = 0; ii < nBytes; ++ii) + { + TInline::ByteToHex(&pszData[ii * 2], in[ii]); + } + + pszData[nBytes * 2] = 0x00; + out = pszData; + azfree(pszData); + + return true; + } + + bool FromHexDump(const char* in, AZStd::string& out) + { + struct TInline + { + static unsigned char HexToByte(const char* pszHex) + { + unsigned char bHigh = 0; + unsigned char bLow = 0; + + if ((pszHex[0] >= '0') && (pszHex[0] <= '9')) + { + bHigh = pszHex[0] - '0'; + } + else if ((pszHex[0] >= 'A') && (pszHex[0] <= 'F')) + { + bHigh = (pszHex[0] - 'A') + 10; } - // Determine where to place the separators - int groupingSizes[] = { firstGroupingSize + 1, groupingSize }; // First group gets +1 since we begin all subsequent groups at the second digit - int groupingOffsetsToNext[] = { 1, 0 }; // We will offset from the first entry to the second, then stay at the second for remaining iterations - const int* currentGroupingSize = groupingSizes; - const int* currentGroupingOffsetToNext = groupingOffsetsToNext; - AZStd::fixed_vector separatorLocations; - int groupCounter = 0; - int digitPosition = numberEndPos - 1; + bHigh = bHigh << 4; - while (digitPosition >= 0) + if ((pszHex[1] >= '0') && (pszHex[1] <= '9')) { - // Walk backwards in the string from the least significant digit to the most significant, demarcating consecutive groups of digits - char c = buffer[digitPosition]; + bLow = pszHex[1] - '0'; + } + else if ((pszHex[1] >= 'A') && (pszHex[1] <= 'F')) + { + bLow = (pszHex[1] - 'A') + 10; + } - if (c >= '0' && c <= '9') - { - if (++groupCounter == *currentGroupingSize) - { - // Demarcate a new group of digits at this location - separatorLocations.push_back(buffer + digitPosition); - currentGroupingSize += *currentGroupingOffsetToNext; - currentGroupingOffsetToNext += *currentGroupingOffsetToNext; - groupCounter = 0; - } + return bHigh | bLow; + } + }; - digitPosition--; - } - else + size_t len = strlen(in); + if (len < 2) //must be at least 2 characters to work with + { + return false; + } + + size_t nBytes = len / 2; + char* pszData = reinterpret_cast(azmalloc(nBytes + 1)); + + for (size_t ii = 0; ii < nBytes; ++ii) + { + pszData[ii] = TInline::HexToByte(&in[ii * 2]); + } + + pszData[nBytes] = 0x00; + out = pszData; + azfree(pszData); + + return true; + } + + namespace NumberFormatting + { + int GroupDigits(char* buffer, size_t bufferSize, size_t decimalPosHint, char digitSeparator, char decimalSeparator, int groupingSize, int firstGroupingSize) + { + static const int MAX_SEPARATORS = 16; + + AZ_Assert(buffer, "Null string buffer"); + AZ_Assert(bufferSize > decimalPosHint, "Decimal position %lu cannot be located beyond bufferSize %lu", decimalPosHint, bufferSize); + AZ_Assert(groupingSize > 0, "Grouping size must be a positive integer"); + + int numberEndPos = 0; + int stringEndPos = 0; + + if (decimalPosHint > 0 && decimalPosHint < (bufferSize - 1) && buffer[decimalPosHint] == decimalSeparator) + { + // Assume the number ends at the supplied location + numberEndPos = (int)decimalPosHint; + stringEndPos = numberEndPos + (int)strnlen(buffer + numberEndPos, bufferSize - numberEndPos); + } + else + { + // Search for the final digit or separator while obtaining the string length + int lastDigitSeenPos = 0; + + while (stringEndPos < bufferSize) + { + char c = buffer[stringEndPos]; + + if (!c) { break; } - } - - if (stringEndPos + separatorLocations.size() >= bufferSize) - { - // Won't fit into buffer, so return unchanged - return stringEndPos; - } - - // Insert the separators by shifting characters forward in the string, starting at the end and working backwards - const char* src = buffer + stringEndPos; - char* dest = buffer + stringEndPos + separatorLocations.size(); - auto separatorItr = separatorLocations.begin(); - - while (separatorItr != separatorLocations.end()) - { - while (src > *separatorItr) + else if (c == decimalSeparator) { - *dest-- = *src--; + // End the number if there's a decimal + numberEndPos = stringEndPos; + } + else if (numberEndPos <= 0 && c >= '0' && c <= '9') + { + // Otherwise keep track of where the last digit we've seen is + lastDigitSeenPos = stringEndPos; } - // Insert the separator and reduce the distance between our destination and source by one - *dest-- = digitSeparator; - ++separatorItr; + stringEndPos++; } - return (int)(stringEndPos + separatorLocations.size()); + if (numberEndPos <= 0) + { + if (lastDigitSeenPos > 0) + { + // No decimal, so use the last seen digit as the end of the number + numberEndPos = lastDigitSeenPos + 1; + } + else + { + // No digits, no decimals, therefore no change in the string + return stringEndPos; + } + } } - } - namespace AssetPath + if (firstGroupingSize <= 0) + { + firstGroupingSize = groupingSize; + } + + // Determine where to place the separators + int groupingSizes[] = { firstGroupingSize + 1, groupingSize }; // First group gets +1 since we begin all subsequent groups at the second digit + int groupingOffsetsToNext[] = { 1, 0 }; // We will offset from the first entry to the second, then stay at the second for remaining iterations + const int* currentGroupingSize = groupingSizes; + const int* currentGroupingOffsetToNext = groupingOffsetsToNext; + AZStd::fixed_vector separatorLocations; + int groupCounter = 0; + int digitPosition = numberEndPos - 1; + + while (digitPosition >= 0) + { + // Walk backwards in the string from the least significant digit to the most significant, demarcating consecutive groups of digits + char c = buffer[digitPosition]; + + if (c >= '0' && c <= '9') + { + if (++groupCounter == *currentGroupingSize) + { + // Demarcate a new group of digits at this location + separatorLocations.push_back(buffer + digitPosition); + currentGroupingSize += *currentGroupingOffsetToNext; + currentGroupingOffsetToNext += *currentGroupingOffsetToNext; + groupCounter = 0; + } + + digitPosition--; + } + else + { + break; + } + } + + if (stringEndPos + separatorLocations.size() >= bufferSize) + { + // Won't fit into buffer, so return unchanged + return stringEndPos; + } + + // Insert the separators by shifting characters forward in the string, starting at the end and working backwards + const char* src = buffer + stringEndPos; + char* dest = buffer + stringEndPos + separatorLocations.size(); + auto separatorItr = separatorLocations.begin(); + + while (separatorItr != separatorLocations.end()) + { + while (src > *separatorItr) + { + *dest-- = *src--; + } + + // Insert the separator and reduce the distance between our destination and source by one + *dest-- = digitSeparator; + ++separatorItr; + } + + return (int)(stringEndPos + separatorLocations.size()); + } + } + + namespace AssetPath + { + namespace Internal { - void CalculateBranchToken(const AZStd::string& appRootPath, AZStd::string& token) + AZ::u32 CalculateBranchTokenHash(AZStd::string_view engineRootPath) { // Normalize the token to prepare for CRC32 calculation - AZStd::string normalized = appRootPath; + auto NormalizeEnginePath = [](const char element) -> char + { + // Substitute path separators with '_' and lower case + return element == AZ::IO::WindowsPathSeparator || element == AZ::IO::PosixPathSeparator + ? '_' + : static_cast(std::tolower(element)); + }; - // Strip out any trailing path separators - AZ::StringFunc::Strip(normalized, AZ_CORRECT_FILESYSTEM_SEPARATOR_STRING AZ_WRONG_FILESYSTEM_SEPARATOR_STRING,false, false, true); - - // Lower case always - AZStd::to_lower(normalized.begin(), normalized.end()); - - // Substitute path separators with '_' - AZStd::replace(normalized.begin(), normalized.end(), '\\', '_'); - AZStd::replace(normalized.begin(), normalized.end(), '/', '_'); + // Trim off trailing path separators + engineRootPath = RStrip(engineRootPath, AZ_CORRECT_AND_WRONG_FILESYSTEM_SEPARATOR); + AZ::IO::FixedMaxPathString enginePath; + AZStd::transform( + engineRootPath.begin(), engineRootPath.end(), AZStd::back_inserter(enginePath), AZStd::move(NormalizeEnginePath)); // Perform the CRC32 calculation - const AZ::Crc32 branchTokenCrc(normalized.c_str(), normalized.size(), true); - char branchToken[12]; - azsnprintf(branchToken, AZ_ARRAY_SIZE(branchToken), "0x%08X", static_cast(branchTokenCrc)); - token = AZStd::string(branchToken); + constexpr bool forceLowercase = true; + return static_cast(AZ::Crc32(enginePath.c_str(), enginePath.size(), forceLowercase)); } + } // namespace Internal + void CalculateBranchToken(AZStd::string_view engineRootPath, AZStd::string& token) + { + token = AZStd::string::format("0x%08X", Internal::CalculateBranchTokenHash(engineRootPath)); + } + void CalculateBranchToken(AZStd::string_view engineRootPath, AZ::IO::FixedMaxPathString& token) + { + token = AZ::IO::FixedMaxPathString::format("0x%08X", Internal::CalculateBranchTokenHash(engineRootPath)); + } + } // namespace AssetPath + + namespace AssetDatabasePath + { + bool Normalize(AZStd::string& inout) + { + // Asset Paths uses the forward slash for the database separator + AZ::IO::Path path(AZStd::move(inout), AZ_CORRECT_DATABASE_SEPARATOR); + bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_DATABASE_SEPARATOR) || path.Native().ends_with(AZ_WRONG_DATABASE_SEPARATOR)) + && path.HasRelativePath(); + inout = AZStd::move(path.LexicallyNormal().Native()); + if (appendTrailingSlash) + { + inout.push_back(AZ_CORRECT_DATABASE_SEPARATOR); + } + return IsValid(inout.c_str()); } - namespace AssetDatabasePath + bool IsValid(const char* in) { - bool Normalize(AZStd::string& inout) + if (!in) { - // Asset Paths uses the forward slash for the database separator - AZ::IO::Path path(AZStd::move(inout), AZ_CORRECT_DATABASE_SEPARATOR); - bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_DATABASE_SEPARATOR) || path.Native().ends_with(AZ_WRONG_DATABASE_SEPARATOR)) - && path.HasRelativePath(); - inout = AZStd::move(path.LexicallyNormal().Native()); - if (appendTrailingSlash) - { - inout.push_back(AZ_CORRECT_DATABASE_SEPARATOR); - } - return IsValid(inout.c_str()); + return false; } - bool IsValid(const char* in) + if (!strlen(in)) { - if (!in) - { - return false; - } + return false; + } - if (!strlen(in)) - { - return false; - } + if (Find(in, AZ_DATABASE_INVALID_CHARACTERS) != AZStd::string::npos) + { + return false; + } - if (Find(in, AZ_DATABASE_INVALID_CHARACTERS) != AZStd::string::npos) - { - return false; - } - - if (Find(in, AZ_WRONG_DATABASE_SEPARATOR) != AZStd::string::npos) - { - return false; - } + if (Find(in, AZ_WRONG_DATABASE_SEPARATOR) != AZStd::string::npos) + { + return false; + } #ifndef AZ_FILENAME_ALLOW_SPACES - if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) - { - return false; - } + if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) + { + return false; + } #endif // AZ_FILENAME_ALLOW_SPACES - if (LastCharacter(in) == AZ_CORRECT_DATABASE_SEPARATOR) - { - return false; - } - - return true; - } - - bool Split(const char* in, [[maybe_unused]] AZStd::string* pDstProjectRootOut, AZStd::string* pDstDatabaseRootOut, - AZStd::string* pDstDatabasePathOut , AZStd::string* pDstFileOut, AZStd::string* pDstFileExtensionOut) + if (LastCharacter(in) == AZ_CORRECT_DATABASE_SEPARATOR) { - AZStd::string_view path{ in }; - if (path.empty()) - { - return false; - } - - AZ::IO::PathView pathView(path, AZ_CORRECT_DATABASE_SEPARATOR); - if (pDstDatabaseRootOut) - { - AZStd::string_view rootNameView = pathView.RootName().Native(); - if (rootNameView.size() > pDstDatabaseRootOut->max_size()) - { - return false; - } - *pDstDatabaseRootOut = rootNameView; - } - if (pDstDatabasePathOut) - { - AZStd::string_view rootPathView = pathView.RootPath().Native(); - AZStd::string_view relPathParentView = pathView.ParentPath().RelativePath().Native(); - if (rootPathView.size() + relPathParentView.size() > pDstDatabasePathOut->max_size()) - { - return false; - } - // Append the root directory if there is one - *pDstDatabasePathOut = rootPathView; - // Append the relative path portion of the split path excluding the filename - *pDstDatabasePathOut += relPathParentView; - } - if (pDstFileOut) - { - AZStd::string_view stemView = pathView.Stem().Native(); - if (stemView.size() > pDstFileOut->max_size()) - { - return false; - } - *pDstFileOut = stemView; - } - if (pDstFileExtensionOut) - { - AZStd::string_view extensionView = pathView.Extension().Native(); - if (extensionView.size() > pDstFileExtensionOut->max_size()) - { - return false; - } - *pDstFileExtensionOut = extensionView; - } - - return true; + return false; } - bool Join(const char* pFirstPart, const char* pSecondPart, AZStd::string& out, [[maybe_unused]] bool bCaseInsensitive /*= true*/, bool bNormalize /*= true*/) - { - // both paths cannot be empty - if (!pFirstPart || !pSecondPart) - { - return false; - } + return true; + } - AZ::IO::PathView secondPath(pSecondPart); - AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," - " this will replace the first part of the path resulting in an output of just the second part", - pSecondPart); - - AZ::IO::Path resultPath(pFirstPart, AZ_CORRECT_DATABASE_SEPARATOR); - resultPath /= secondPath; - out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); - return true; - } - } //namespace AssetDatabasePath - - namespace Root + bool Split(const char* in, [[maybe_unused]] AZStd::string* pDstProjectRootOut, AZStd::string* pDstDatabaseRootOut, + AZStd::string* pDstDatabasePathOut , AZStd::string* pDstFileOut, AZStd::string* pDstFileExtensionOut) { - bool Normalize(AZStd::string& inout) + AZStd::string_view path{ in }; + if (path.empty()) { - AZ::IO::Path path(AZStd::move(inout)); - path = path.LexicallyNormal(); - // After normalization check if the path contains a relative path - bool appendTrailingSlash = path.HasRelativePath(); - inout = AZStd::move(path.Native()); - if (appendTrailingSlash) - { - inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); // Append a trailing separator for Root path normalization - } - return IsValid(inout.c_str()); + return false; } - bool IsValid(const char* in) + AZ::IO::PathView pathView(path, AZ_CORRECT_DATABASE_SEPARATOR); + if (pDstDatabaseRootOut) { - if (!in) + AZStd::string_view rootNameView = pathView.RootName().Native(); + if (rootNameView.size() > pDstDatabaseRootOut->max_size()) { return false; } - - if (!strlen(in)) + *pDstDatabaseRootOut = rootNameView; + } + if (pDstDatabasePathOut) + { + AZStd::string_view rootPathView = pathView.RootPath().Native(); + AZStd::string_view relPathParentView = pathView.ParentPath().RelativePath().Native(); + if (rootPathView.size() + relPathParentView.size() > pDstDatabasePathOut->max_size()) { return false; } - - if (Find(in, AZ_FILESYSTEM_INVALID_CHARACTERS) != AZStd::string::npos) + // Append the root directory if there is one + *pDstDatabasePathOut = rootPathView; + // Append the relative path portion of the split path excluding the filename + *pDstDatabasePathOut += relPathParentView; + } + if (pDstFileOut) + { + AZStd::string_view stemView = pathView.Stem().Native(); + if (stemView.size() > pDstFileOut->max_size()) { return false; } - - if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) + *pDstFileOut = stemView; + } + if (pDstFileExtensionOut) + { + AZStd::string_view extensionView = pathView.Extension().Native(); + if (extensionView.size() > pDstFileExtensionOut->max_size()) { return false; } + *pDstFileExtensionOut = extensionView; + } - #ifndef AZ_FILENAME_ALLOW_SPACES - if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) - { - return false; - } - #endif // AZ_FILENAME_ALLOW_SPACES + return true; + } - AZ::IO::PathView pathView(in); - if (!pathView.HasRootPath()) - { - return false; - } + bool Join(const char* pFirstPart, const char* pSecondPart, AZStd::string& out, [[maybe_unused]] bool bCaseInsensitive /*= true*/, bool bNormalize /*= true*/) + { + // both paths cannot be empty + if (!pFirstPart || !pSecondPart) + { + return false; + } - if (LastCharacter(in) != AZ_CORRECT_FILESYSTEM_SEPARATOR) - { - return false; - } + AZ::IO::PathView secondPath(pSecondPart); + AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," + " this will replace the first part of the path resulting in an output of just the second part", + pSecondPart); + AZ::IO::Path resultPath(pFirstPart, AZ_CORRECT_DATABASE_SEPARATOR); + resultPath /= secondPath; + out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); + return true; + } + } //namespace AssetDatabasePath + + namespace Root + { + bool Normalize(AZStd::string& inout) + { + AZ::IO::Path path(AZStd::move(inout)); + path = path.LexicallyNormal(); + // After normalization check if the path contains a relative path + bool appendTrailingSlash = path.HasRelativePath(); + inout = AZStd::move(path.Native()); + if (appendTrailingSlash) + { + inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); // Append a trailing separator for Root path normalization + } + return IsValid(inout.c_str()); + } + + bool IsValid(const char* in) + { + if (!in) + { + return false; + } + + if (!strlen(in)) + { + return false; + } + + if (Find(in, AZ_FILESYSTEM_INVALID_CHARACTERS) != AZStd::string::npos) + { + return false; + } + + if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) + { + return false; + } + +#ifndef AZ_FILENAME_ALLOW_SPACES + if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) + { + return false; + } +#endif // AZ_FILENAME_ALLOW_SPACES + + AZ::IO::PathView pathView(in); + if (!pathView.HasRootPath()) + { + return false; + } + + if (LastCharacter(in) != AZ_CORRECT_FILESYSTEM_SEPARATOR) + { + return false; + } + + return true; + } + }//namespace Root + + namespace RelativePath + { + bool Normalize(AZStd::string& inout) + { + AZ::IO::Path path(AZStd::move(inout)); + path = path.LexicallyNormal(); + // After normalization check if the path contains a relative path + bool appendTrailingSlash = path.HasRelativePath(); + inout = AZStd::move(path.Native()); + if (appendTrailingSlash) + { + inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); // Append trailing separator for Relative path normalization if it it is not empty + } + return IsValid(inout.c_str()); + } + + bool IsValid(const char* in) + { + if (!in) + { + return false; + } + + if (!strlen(in)) + { return true; } - }//namespace Root - namespace RelativePath + if (Find(in, AZ_FILESYSTEM_INVALID_CHARACTERS) != AZStd::string::npos) + { + return false; + } + + if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) + { + return false; + } + +#ifndef AZ_FILENAME_ALLOW_SPACES + if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) + { + return false; + } +#endif // AZ_FILENAME_ALLOW_SPACES + + if (Path::HasDrive(in)) + { + return false; + } + + if (FirstCharacter(in) == AZ_CORRECT_FILESYSTEM_SEPARATOR) + { + return false; + } + + if (LastCharacter(in) != AZ_CORRECT_FILESYSTEM_SEPARATOR) + { + return false; + } + + return true; + } + }//namespace RelativePath + + namespace Path + { + bool Normalize(AZStd::string& inout) { - bool Normalize(AZStd::string& inout) + AZ::IO::Path path(AZStd::move(inout)); + bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR) || path.Native().ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)); + path = path.LexicallyNormal(); + // After normalization check if the path contains a relative path and addition to ending with a path separator before + appendTrailingSlash = appendTrailingSlash && path.HasRelativePath(); + inout = AZStd::move(path.Native()); + if (appendTrailingSlash) { - AZ::IO::Path path(AZStd::move(inout)); - path = path.LexicallyNormal(); - // After normalization check if the path contains a relative path - bool appendTrailingSlash = path.HasRelativePath(); - inout = AZStd::move(path.Native()); - if (appendTrailingSlash) - { - inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); // Append trailing separator for Relative path normalization if it it is not empty - } - return IsValid(inout.c_str()); + inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); } + return IsValid(inout.c_str()); + } - bool IsValid(const char* in) - { - if (!in) - { - return false; - } - - if (!strlen(in)) - { - return true; - } - - if (Find(in, AZ_FILESYSTEM_INVALID_CHARACTERS) != AZStd::string::npos) - { - return false; - } - - if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) - { - return false; - } - - #ifndef AZ_FILENAME_ALLOW_SPACES - if (Find(in, AZ_SPACE_CHARACTERS) != AZStd::string::npos) - { - return false; - } - #endif // AZ_FILENAME_ALLOW_SPACES - - if (Path::HasDrive(in)) - { - return false; - } - - if (FirstCharacter(in) == AZ_CORRECT_FILESYSTEM_SEPARATOR) - { - return false; - } - - if (LastCharacter(in) != AZ_CORRECT_FILESYSTEM_SEPARATOR) - { - return false; - } - - return true; - } - }//namespace RelativePath - - namespace Path + bool Normalize(FixedString& inout) { - bool Normalize(AZStd::string& inout) + AZ::IO::FixedMaxPath path(AZStd::move(inout)); + bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR) || path.Native().ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)); + path = path.LexicallyNormal(); + // After normalization check if the path contains a relative path and addition to ending with a path separator before + appendTrailingSlash = appendTrailingSlash && path.HasRelativePath(); + inout = AZStd::move(path.Native()); + if (appendTrailingSlash) { - AZ::IO::Path path(AZStd::move(inout)); - bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR) || path.Native().ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)); - path = path.LexicallyNormal(); - // After normalization check if the path contains a relative path and addition to ending with a path separator before - appendTrailingSlash = appendTrailingSlash && path.HasRelativePath(); - inout = AZStd::move(path.Native()); - if (appendTrailingSlash) - { - inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); - } - return IsValid(inout.c_str()); + inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); + } + return IsValid(inout.c_str()); + } + + bool IsValid(const char* in, bool bHasDrive /*= false*/, bool bHasExtension /*= false*/, AZStd::string* errors /*= nullptr*/) + { + //if they gave us a error reporting string empty it. + if (errors) + { + errors->clear(); } - bool Normalize(FixedString& inout) + //empty is not a valid path + if (!in) { - AZ::IO::FixedMaxPath path(AZStd::move(inout)); - bool appendTrailingSlash = (path.Native().ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR) || path.Native().ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)); - path = path.LexicallyNormal(); - // After normalization check if the path contains a relative path and addition to ending with a path separator before - appendTrailingSlash = appendTrailingSlash && path.HasRelativePath(); - inout = AZStd::move(path.Native()); - if (appendTrailingSlash) - { - inout.push_back(AZ_CORRECT_FILESYSTEM_SEPARATOR); - } - return IsValid(inout.c_str()); - } - - bool IsValid(const char* in, bool bHasDrive /*= false*/, bool bHasExtension /*= false*/, AZStd::string* errors /*= nullptr*/) - { - //if they gave us a error reporting string empty it. if (errors) { - errors->clear(); + *errors += "The path is Empty."; } + return false; + } - //empty is not a valid path - if (!in) + //empty is not a valid path + size_t length = strlen(in); + if (!length) + { + if (errors) { - if (errors) - { - *errors += "The path is Empty."; - } - return false; + *errors += "The path is Empty."; } + return false; + } - //empty is not a valid path - size_t length = strlen(in); - if (!length) + //invalid characters + const char* inEnd = in + length; + const char* invalidCharactersBegin = AZ_FILESYSTEM_INVALID_CHARACTERS; + const char* invalidCharactersEnd = invalidCharactersBegin + AZ_ARRAY_SIZE(AZ_FILESYSTEM_INVALID_CHARACTERS); + if (AZStd::find_first_of(in, inEnd, invalidCharactersBegin, invalidCharactersEnd) != inEnd) + { + if (errors) { - if (errors) - { - *errors += "The path is Empty."; - } - return false; + *errors += "The path has invalid characters."; } + return false; + } - //invalid characters - const char* inEnd = in + length; - const char* invalidCharactersBegin = AZ_FILESYSTEM_INVALID_CHARACTERS; - const char* invalidCharactersEnd = invalidCharactersBegin + AZ_ARRAY_SIZE(AZ_FILESYSTEM_INVALID_CHARACTERS); - if (AZStd::find_first_of(in, inEnd, invalidCharactersBegin, invalidCharactersEnd) != inEnd) + //invalid characters + if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) + { + if (errors) { - if (errors) - { - *errors += "The path has invalid characters."; - } - return false; + *errors += "The path has wrong separator."; } + return false; + } - //invalid characters - if (Find(in, AZ_WRONG_FILESYSTEM_SEPARATOR) != AZStd::string::npos) +#ifndef AZ_FILENAME_ALLOW_SPACES + const char* spaceCharactersBegin = AZ_SPACE_CHARACTERS; + const char* spaceCharactersEnd = spaceCharactersBegin + AZ_ARRAY_SIZE(AZ_SPACE_CHARACTERS); + if (AZStd::find_first_of(in, inEnd, spaceCharactersBegin, spaceCharactersEnd) != inEnd) + { + if (errors) { - if (errors) - { - *errors += "The path has wrong separator."; - } - return false; + *errors += "The path has space characters."; } + return false; + } +#endif // AZ_FILENAME_ALLOW_SPACES - #ifndef AZ_FILENAME_ALLOW_SPACES - const char* spaceCharactersBegin = AZ_SPACE_CHARACTERS; - const char* spaceCharactersEnd = spaceCharactersBegin + AZ_ARRAY_SIZE(AZ_SPACE_CHARACTERS); - if (AZStd::find_first_of(in, inEnd, spaceCharactersBegin, spaceCharactersEnd) != inEnd) + //does it have a drive if specified + if (bHasDrive && !HasDrive(in)) + { + if (errors) { - if (errors) - { - *errors += "The path has space characters."; - } - return false; + *errors += "The path should have a drive. The path ["; + *errors += in; + *errors += "] is invalid."; } - #endif // AZ_FILENAME_ALLOW_SPACES + return false; + } - //does it have a drive if specified - if (bHasDrive && !HasDrive(in)) + //does it have and extension if specified + if (bHasExtension && !HasExtension(in)) + { + if (errors) { - if (errors) - { - *errors += "The path should have a drive. The path ["; - *errors += in; - *errors += "] is invalid."; - } - return false; + *errors += "The path should have the a file extension. The path ["; + *errors += in; + *errors += "] is invalid."; } + return false; + } - //does it have and extension if specified - if (bHasExtension && !HasExtension(in)) + //start at the beginning and walk down the characters of the path + const char* elementStart = in; + const char* walk = elementStart; + while (*walk) + { + if (*walk == AZ_CORRECT_FILESYSTEM_SEPARATOR) //is this the correct separator { - if (errors) - { - *errors += "The path should have the a file extension. The path ["; - *errors += in; - *errors += "] is invalid."; - } - return false; + elementStart = walk; } - - //start at the beginning and walk down the characters of the path - const char* elementStart = in; - const char* walk = elementStart; - while (*walk) +#if AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS + else if (*walk == AZ_FILESYSTEM_DRIVE_SEPARATOR) //is this the drive separator { - if (*walk == AZ_CORRECT_FILESYSTEM_SEPARATOR) //is this the correct separator - { - elementStart = walk; - } - #if AZ_TRAIT_OS_USE_WINDOWS_FILE_PATHS - else if (*walk == AZ_FILESYSTEM_DRIVE_SEPARATOR) //is this the drive separator - { - //A AZ_FILESYSTEM_DRIVE_SEPARATOR character con only occur in the first - //component of a valid path. If the elementStart is not GetBufferPtr() - //then we have past the first component - if (elementStart != in) - { - if (errors) - { - *errors += "There is a stray AZ_FILESYSTEM_DRIVE_SEPARATOR = "; - *errors += AZ_FILESYSTEM_DRIVE_SEPARATOR; - *errors += " found after the first component. The path ["; - *errors += in; - *errors += "] is invalid."; - } - return false; - } - } - #endif - #ifndef AZ_FILENAME_ALLOW_SPACES - else if (*walk == ' ') //is this a space + //A AZ_FILESYSTEM_DRIVE_SEPARATOR character con only occur in the first + //component of a valid path. If the elementStart is not GetBufferPtr() + //then we have past the first component + if (elementStart != in) { if (errors) { - *errors += "The component ["; - for (const char* c = elementStart + 1; c != walk; ++c) - { - *errors += *c; - } - *errors += "] has a SPACE character. The path ["; + *errors += "There is a stray AZ_FILESYSTEM_DRIVE_SEPARATOR = "; + *errors += AZ_FILESYSTEM_DRIVE_SEPARATOR; + *errors += " found after the first component. The path ["; *errors += in; *errors += "] is invalid."; } return false; } - #endif - - ++walk; } - - #if !AZ_TRAIT_OS_ALLOW_UNLIMITED_PATH_COMPONENT_LENGTH - //is this full path longer than AZ::IO::MaxPathLength (The longest a path with all components can possibly be)? - if (walk - in > AZ::IO::MaxPathLength) +#endif +#ifndef AZ_FILENAME_ALLOW_SPACES + else if (*walk == ' ') //is this a space { - if (errors != 0) + if (errors) { - *errors += "The path ["; + *errors += "The component ["; + for (const char* c = elementStart + 1; c != walk; ++c) + { + *errors += *c; + } + *errors += "] has a SPACE character. The path ["; *errors += in; - *errors += "] is over the AZ::IO::MaxPathLength = "; - char buf[64]; - _itoa_s(AZ::IO::MaxPathLength, buf, 10); - *errors += buf; - *errors += " characters total length limit."; + *errors += "] is invalid."; } return false; } - #endif +#endif - return true; + ++walk; } - bool ConstructFull(const char* pRootPath, const char* pFileName, AZStd::string& out, bool bNormalize /* = false*/) +#if !AZ_TRAIT_OS_ALLOW_UNLIMITED_PATH_COMPONENT_LENGTH + //is this full path longer than AZ::IO::MaxPathLength (The longest a path with all components can possibly be)? + if (walk - in > AZ::IO::MaxPathLength) { - if (!pRootPath || AZ::IO::PathView(pRootPath).IsRelative() - || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) + if (errors != 0) { - return false; - } - AZ::IO::Path path(pRootPath); - path /= pFileName; - if (bNormalize) - { - out = AZStd::move(path.LexicallyNormal().Native()); - } - else - { - out = AZStd::move(path.Native()); - } - return IsValid(out.c_str()); - } - - bool ConstructFull(const char* pRootPath, const char* pFileName, const char* pFileExtension, AZStd::string& out, bool bNormalize /* = false*/) - { - if (!pRootPath || AZ::IO::PathView(pRootPath).IsRelative() - || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) - { - return false; - } - AZ::IO::Path path(pRootPath); - path /= pFileName; - if (pFileExtension) - { - path.ReplaceExtension(pFileExtension); - } - if (bNormalize) - { - out = AZStd::move(path.LexicallyNormal().Native()); - } - else - { - out = AZStd::move(path.Native()); - } - return IsValid(out.c_str()); - } - - bool ConstructFull(const char* pRoot, const char* pRelativePath, const char* pFileName, const char* pFileExtension, AZStd::string& out, bool bNormalize /* = false*/) - { - if (!pRoot || AZ::IO::PathView(pRoot).IsRelative() - || !pRelativePath || AZ::IO::PathView(pRelativePath).IsAbsolute() - || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) - { - return false; - } - AZ::IO::Path path(pRoot); - path /= pRelativePath; - path /= pFileName; - if (pFileExtension) - { - path.ReplaceExtension(pFileExtension); - } - if (bNormalize) - { - out = AZStd::move(path.LexicallyNormal().Native()); - } - else - { - out = AZStd::move(path.Native()); - } - return IsValid(out.c_str()); - } - - bool Split(const char* in, AZStd::string* pDstDrive, AZStd::string* pDstPath, AZStd::string* pDstName, AZStd::string* pDstExtension) - { - AZStd::string_view path{ in }; - if (path.empty()) - { - return false; - } - - AZ::IO::PathView pathView(path); - if (pDstDrive) - { - AZStd::string_view rootNameView = pathView.RootName().Native(); - if (rootNameView.size() > pDstDrive->max_size()) - { - return false; - } - *pDstDrive = rootNameView; - } - if (pDstPath) - { - AZStd::string_view rootDirectoryView = pathView.RootDirectory().Native(); - AZStd::string_view relPathParentView = pathView.ParentPath().RelativePath().Native(); - if (rootDirectoryView.size() + relPathParentView.size() > pDstPath->max_size()) - { - return false; - } - // Append the root directory if there is one - *pDstPath = rootDirectoryView; - // Append the relative path portion of the split path excluding the filename - *pDstPath += relPathParentView; - } - if (pDstName) - { - AZStd::string_view stemView = pathView.Stem().Native(); - if (stemView.size() > pDstName->max_size()) - { - return false; - } - *pDstName = stemView; - } - if (pDstExtension) - { - AZStd::string_view extensionView = pathView.Extension().Native(); - if (extensionView.size() > pDstExtension->max_size()) - { - return false; - } - *pDstExtension = extensionView; - } - - return true; - } - - bool Join(const char* pFirstPart, const char* pSecondPart, AZStd::string& out, [[maybe_unused]] bool bCaseInsensitive, bool bNormalize) - { - if (!pFirstPart || !pSecondPart) - { - return false; - } - - AZ::IO::PathView secondPath(pSecondPart); - AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," - " this will replace the first part of the path resulting in an output of just the second part", - pSecondPart); - - AZ::IO::Path resultPath(pFirstPart); - resultPath /= secondPath; - out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); - return true; - } - - bool Join(const char* pFirstPart, const char* pSecondPart, FixedString& out, [[maybe_unused]] bool bCaseInsensitive, bool bNormalize) - { - if (!pFirstPart || !pSecondPart) - { - return false; - } - - AZ::IO::PathView secondPath(pSecondPart); - AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," - " this will replace the first part of the path resulting in an output of just the second part", - pSecondPart); - - AZ::IO::FixedMaxPath resultPath(pFirstPart); - resultPath /= secondPath; - out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); - return true; - } - - bool HasDrive(const char* in, bool bCheckAllFileSystemFormats /*= false*/) - { - // no drive if empty - if (!in || in[0] == '\0') - { - return false; - } - AZ::IO::PathView pathView(in); - return pathView.HasRootName() || (bCheckAllFileSystemFormats && pathView.HasRootDirectory()); - } - - bool HasExtension(const char* in) - { - //it doesn't have an extension if it's empty - if (!in || in[0] == '\0') - { - return false; - } - - return AZ::IO::PathView(in).HasExtension(); - } - - bool IsExtension(const char* in, const char* pExtension, bool bCaseInsenitive /*= false*/) - { - //it doesn't have an extension if it's empty - if (!in || in[0] == '\0' || !pExtension || pExtension[0] == '\0') - { - return false; - } - - AZStd::string_view pathExtension = AZ::IO::PathView(in).Extension().Native(); - if (pathExtension.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) - { - pathExtension.remove_prefix(1); - } - AZStd::string_view extensionView(pExtension); - if (extensionView.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) - { - extensionView.remove_prefix(1); - } - - return AZStd::equal(pathExtension.begin(), pathExtension.end(), extensionView.begin(), extensionView.end(), - [bCaseInsenitive](const char lhs, const char rhs) - { - return !bCaseInsenitive ? lhs == rhs : tolower(lhs) == tolower(rhs); - }); - } - - bool IsRelative(const char* in) - { - //not relative if empty - if (!in || in[0] == '\0') - { - return false; - } - - return AZ::IO::PathView(in).IsRelative(); - } - - bool StripDrive(AZStd::string& inout) - { - AZ::IO::PathView pathView(inout); - AZ::IO::PathView rootNameView(pathView.RootName()); - if (!rootNameView.empty()) - { - inout.replace(0, rootNameView.Native().size(), ""); - return true; + *errors += "The path ["; + *errors += in; + *errors += "] is over the AZ::IO::MaxPathLength = "; + char buf[64]; + _itoa_s(AZ::IO::MaxPathLength, buf, 10); + *errors += buf; + *errors += " characters total length limit."; } return false; } +#endif - void StripPath(AZStd::string& inout) + return true; + } + + bool ConstructFull(const char* pRootPath, const char* pFileName, AZStd::string& out, bool bNormalize /* = false*/) + { + if (!pRootPath || AZ::IO::PathView(pRootPath).IsRelative() + || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) { - inout = AZ::IO::PathView(inout).Filename().Native(); + return false; + } + AZ::IO::Path path(pRootPath); + path /= pFileName; + if (bNormalize) + { + out = AZStd::move(path.LexicallyNormal().Native()); + } + else + { + out = AZStd::move(path.Native()); + } + return IsValid(out.c_str()); + } + + bool ConstructFull(const char* pRootPath, const char* pFileName, const char* pFileExtension, AZStd::string& out, bool bNormalize /* = false*/) + { + if (!pRootPath || AZ::IO::PathView(pRootPath).IsRelative() + || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) + { + return false; + } + AZ::IO::Path path(pRootPath); + path /= pFileName; + if (pFileExtension) + { + path.ReplaceExtension(pFileExtension); + } + if (bNormalize) + { + out = AZStd::move(path.LexicallyNormal().Native()); + } + else + { + out = AZStd::move(path.Native()); + } + return IsValid(out.c_str()); + } + + bool ConstructFull(const char* pRoot, const char* pRelativePath, const char* pFileName, const char* pFileExtension, AZStd::string& out, bool bNormalize /* = false*/) + { + if (!pRoot || AZ::IO::PathView(pRoot).IsRelative() + || !pRelativePath || AZ::IO::PathView(pRelativePath).IsAbsolute() + || !pFileName || AZ::IO::PathView(pFileName).IsAbsolute()) + { + return false; + } + AZ::IO::Path path(pRoot); + path /= pRelativePath; + path /= pFileName; + if (pFileExtension) + { + path.ReplaceExtension(pFileExtension); + } + if (bNormalize) + { + out = AZStd::move(path.LexicallyNormal().Native()); + } + else + { + out = AZStd::move(path.Native()); + } + return IsValid(out.c_str()); + } + + bool Split(const char* in, AZStd::string* pDstDrive, AZStd::string* pDstPath, AZStd::string* pDstName, AZStd::string* pDstExtension) + { + AZStd::string_view path{ in }; + if (path.empty()) + { + return false; } - void StripFullName(AZStd::string& inout) + AZ::IO::PathView pathView(path); + if (pDstDrive) { - inout = AZ::IO::Path(AZStd::move(inout)).RemoveFilename().Native(); - } - - void StripExtension(AZStd::string& inout) - { - AZ::IO::Path path(AZStd::move(inout)); - path.ReplaceExtension(); - inout = AZStd::move(path.Native()); - } - - bool StripComponent(AZStd::string& inout, bool bLastComponent /* = false*/) - { - AZ::IO::PathView pathView(inout); - auto pathBeginIter = pathView.begin(); - auto pathEndIter = pathView.end(); - if (pathBeginIter == pathEndIter) + AZStd::string_view rootNameView = pathView.RootName().Native(); + if (rootNameView.size() > pDstDrive->max_size()) { return false; } - AZ::IO::Path resultPath; - if (!bLastComponent) - { - // Removing leading path component - AZStd::advance(pathBeginIter, 1); - } - else - { - // Remove trailing path component - AZStd::advance(pathEndIter, -1); - } - for (; pathBeginIter != pathEndIter; ++pathBeginIter) - { - resultPath /= *pathBeginIter; - } - if (resultPath.empty()) + *pDstDrive = rootNameView; + } + if (pDstPath) + { + AZStd::string_view rootDirectoryView = pathView.RootDirectory().Native(); + AZStd::string_view relPathParentView = pathView.ParentPath().RelativePath().Native(); + if (rootDirectoryView.size() + relPathParentView.size() > pDstPath->max_size()) { return false; } - inout = AZStd::move(resultPath.Native()); + // Append the root directory if there is one + *pDstPath = rootDirectoryView; + // Append the relative path portion of the split path excluding the filename + *pDstPath += relPathParentView; + } + if (pDstName) + { + AZStd::string_view stemView = pathView.Stem().Native(); + if (stemView.size() > pDstName->max_size()) + { + return false; + } + *pDstName = stemView; + } + if (pDstExtension) + { + AZStd::string_view extensionView = pathView.Extension().Native(); + if (extensionView.size() > pDstExtension->max_size()) + { + return false; + } + *pDstExtension = extensionView; + } + + return true; + } + + bool Join(const char* pFirstPart, const char* pSecondPart, AZStd::string& out, [[maybe_unused]] bool bCaseInsensitive, bool bNormalize) + { + if (!pFirstPart || !pSecondPart) + { + return false; + } + + AZ::IO::PathView secondPath(pSecondPart); + AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," + " this will replace the first part of the path resulting in an output of just the second part", + pSecondPart); + + AZ::IO::Path resultPath(pFirstPart); + resultPath /= secondPath; + out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); + return true; + } + + bool Join(const char* pFirstPart, const char* pSecondPart, FixedString& out, [[maybe_unused]] bool bCaseInsensitive, bool bNormalize) + { + if (!pFirstPart || !pSecondPart) + { + return false; + } + + AZ::IO::PathView secondPath(pSecondPart); + AZ_Warning("StringFunc", secondPath.IsRelative(), "The second join parameter %s is an absolute path," + " this will replace the first part of the path resulting in an output of just the second part", + pSecondPart); + + AZ::IO::FixedMaxPath resultPath(pFirstPart); + resultPath /= secondPath; + out = bNormalize ? AZStd::move(resultPath.LexicallyNormal().Native()) : AZStd::move(resultPath.Native()); + return true; + } + + bool HasDrive(const char* in, bool bCheckAllFileSystemFormats /*= false*/) + { + // no drive if empty + if (!in || in[0] == '\0') + { + return false; + } + AZ::IO::PathView pathView(in); + return pathView.HasRootName() || (bCheckAllFileSystemFormats && pathView.HasRootDirectory()); + } + + bool HasExtension(const char* in) + { + //it doesn't have an extension if it's empty + if (!in || in[0] == '\0') + { + return false; + } + + return AZ::IO::PathView(in).HasExtension(); + } + + bool IsExtension(const char* in, const char* pExtension, bool bCaseInsenitive /*= false*/) + { + //it doesn't have an extension if it's empty + if (!in || in[0] == '\0' || !pExtension || pExtension[0] == '\0') + { + return false; + } + + AZStd::string_view pathExtension = AZ::IO::PathView(in).Extension().Native(); + if (pathExtension.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) + { + pathExtension.remove_prefix(1); + } + AZStd::string_view extensionView(pExtension); + if (extensionView.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) + { + extensionView.remove_prefix(1); + } + + return AZStd::equal(pathExtension.begin(), pathExtension.end(), extensionView.begin(), extensionView.end(), + [bCaseInsenitive](const char lhs, const char rhs) + { + return !bCaseInsenitive ? lhs == rhs : tolower(lhs) == tolower(rhs); + }); + } + + bool IsRelative(const char* in) + { + //not relative if empty + if (!in || in[0] == '\0') + { + return false; + } + + return AZ::IO::PathView(in).IsRelative(); + } + + bool StripDrive(AZStd::string& inout) + { + AZ::IO::PathView pathView(inout); + AZ::IO::PathView rootNameView(pathView.RootName()); + if (!rootNameView.empty()) + { + inout.replace(0, rootNameView.Native().size(), ""); return true; } + return false; + } - bool GetDrive(const char* in, AZStd::string& out) + void StripPath(AZStd::string& inout) + { + inout = AZ::IO::PathView(inout).Filename().Native(); + } + + void StripFullName(AZStd::string& inout) + { + inout = AZ::IO::Path(AZStd::move(inout)).RemoveFilename().Native(); + } + + void StripExtension(AZStd::string& inout) + { + AZ::IO::Path path(AZStd::move(inout)); + path.ReplaceExtension(); + inout = AZStd::move(path.Native()); + } + + bool StripComponent(AZStd::string& inout, bool bLastComponent /* = false*/) + { + AZ::IO::PathView pathView(inout); + auto pathBeginIter = pathView.begin(); + auto pathEndIter = pathView.end(); + if (pathBeginIter == pathEndIter) { - if (!in || in[0] == '\0') - { - return false; - } + return false; + } + AZ::IO::Path resultPath; + if (!bLastComponent) + { + // Removing leading path component + AZStd::advance(pathBeginIter, 1); + } + else + { + // Remove trailing path component + AZStd::advance(pathEndIter, -1); + } + for (; pathBeginIter != pathEndIter; ++pathBeginIter) + { + resultPath /= *pathBeginIter; + } + if (resultPath.empty()) + { + return false; + } + inout = AZStd::move(resultPath.Native()); + return true; + } - out = AZ::IO::PathView(in).RootName().Native(); + bool GetDrive(const char* in, AZStd::string& out) + { + if (!in || in[0] == '\0') + { + return false; + } + + out = AZ::IO::PathView(in).RootName().Native(); + return !out.empty(); + } + + AZStd::optional GetParentDir(AZStd::string_view path) + { + if (path.empty()) + { + return {}; + } + + AZStd::string_view parentDir = AZ::IO::PathView(path).ParentPath().Native(); + return !parentDir.empty() ? AZStd::make_optional(parentDir) : AZStd::nullopt; + } + + bool GetFullPath(const char* in, AZStd::string& out) + { + if (!in || in[0] == '\0') + { + return false; + } + + out = AZ::IO::PathView(in).ParentPath().Native(); + return !out.empty(); + } + + bool GetFolderPath(const char* in, AZStd::string& out) + { + return GetFullPath(in, out); + } + + bool GetFolder(const char* in, AZStd::string& out, bool bFirst /* = false*/) + { + if (!in || in[0] == '\0') + { + return false; + } + + if (!bFirst) + { + out = AZ::IO::PathView(in).ParentPath().Filename().Native(); return !out.empty(); } - - AZStd::optional GetParentDir(AZStd::string_view path) + else { - if (path.empty()) - { - return {}; - } - - AZStd::string_view parentDir = AZ::IO::PathView(path).ParentPath().Native(); - return !parentDir.empty() ? AZStd::make_optional(parentDir) : AZStd::nullopt; - } - - bool GetFullPath(const char* in, AZStd::string& out) - { - if (!in || in[0] == '\0') - { - return false; - } - - out = AZ::IO::PathView(in).ParentPath().Native(); + AZStd::string_view relativePath = AZ::IO::PathView(in).RelativePath().Native(); + size_t nextSeparator = relativePath.find_first_of(AZ_CORRECT_FILESYSTEM_SEPARATOR); + out = nextSeparator != AZStd::string_view::npos ? relativePath.substr(0, nextSeparator) : relativePath; return !out.empty(); } + } - bool GetFolderPath(const char* in, AZStd::string& out) + bool GetFullFileName(const char* in, AZStd::string& out) + { + if (!in || in[0] == '\0') { - return GetFullPath(in, out); + return false; } - bool GetFolder(const char* in, AZStd::string& out, bool bFirst /* = false*/) - { - if (!in || in[0] == '\0') - { - return false; - } + out = AZ::IO::PathView(in).Filename().Native(); + return !out.empty(); + } - if (!bFirst) - { - out = AZ::IO::PathView(in).ParentPath().Filename().Native(); - return !out.empty(); - } - else - { - AZStd::string_view relativePath = AZ::IO::PathView(in).RelativePath().Native(); - size_t nextSeparator = relativePath.find_first_of(AZ_CORRECT_FILESYSTEM_SEPARATOR); - out = nextSeparator != AZStd::string_view::npos ? relativePath.substr(0, nextSeparator) : relativePath; - return !out.empty(); - } + bool GetFileName(const char* in, AZStd::string& out) + { + if (!in || in[0] == '\0') + { + return false; } - bool GetFullFileName(const char* in, AZStd::string& out) - { - if (!in || in[0] == '\0') - { - return false; - } + out = AZ::IO::PathView(in).Stem().Native(); + return !out.empty(); + } - out = AZ::IO::PathView(in).Filename().Native(); - return !out.empty(); + bool GetExtension(const char* in, AZStd::string& out, bool includeDot) + { + if (!in || in[0] == '\0') + { + return false; } - bool GetFileName(const char* in, AZStd::string& out) + AZStd::string_view extensionView = AZ::IO::PathView(in).Extension().Native(); + // PathView returns extensions with the character, so remove the + // if it is not included + if (!includeDot && extensionView.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) { - if (!in || in[0] == '\0') + extensionView.remove_prefix(1); + } + out = extensionView; + return !out.empty(); + } + + void ReplaceFullName(AZStd::string& inout, const char* pFileName /* = nullptr*/, const char* pFileExtension /* = nullptr*/) + { + //strip the full file name if it has one + AZ::IO::Path path(AZStd::move(inout)); + path.RemoveFilename(); + if (pFileName) + { + path /= pFileName; + } + if (pFileExtension) + { + path.ReplaceExtension(pFileExtension); + } + inout = AZStd::move(path.Native()); + } + + void ReplaceExtension(AZStd::string& inout, const char* newExtension /* = nullptr*/) + { + //treat this as a strip + if (!newExtension || newExtension[0] == '\0') + { + return; + } + AZ::IO::Path path(AZStd::move(inout)); + path.ReplaceExtension(newExtension); + inout = AZStd::move(path.Native()); + } + + AZStd::string& AppendSeparator(AZStd::string& inout) + { + if (inout.ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)) + { + inout.replace(inout.end() - 1, inout.end(), 1, AZ_CORRECT_FILESYSTEM_SEPARATOR); + } + else if (!inout.ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR)) + { + inout.append(1, AZ_CORRECT_FILESYSTEM_SEPARATOR); + } + return inout; + } + } // namespace Path + + namespace Json + { + /* + According to http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf: + A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All characters may be + placed within the quotation marks except for the characters that must be escaped: quotation mark (U+0022), + reverse solidus (U+005C), and the control characters U+0000 to U+001F. + */ + AZStd::string& ToEscapedString(AZStd::string& inout) + { + size_t strSize = inout.size(); + + for (size_t i = 0; i < strSize; ++i) + { + char character = inout[i]; + + // defaults to 1 if it hits any cases except default + size_t jumpChar = 1; + switch (character) { - return false; + case '"': + inout.insert(i, "\\"); + break; + + case '\\': + inout.insert(i, "\\"); + break; + + case '/': + inout.insert(i, "\\"); + break; + + case '\b': + inout.replace(i, i + 1, "\\b"); + break; + + case '\f': + inout.replace(i, i + 1, "\\f"); + break; + + case '\n': + inout.replace(i, i + 1, "\\n"); + break; + + case '\r': + inout.replace(i, i + 1, "\\r"); + break; + + case '\t': + inout.replace(i, i + 1, "\\t"); + break; + + default: + /* + Control characters U+0000 to U+001F may be represented as a six - character sequence : a reverse solidus, + followed by the lowercase letter u, followed by four hexadecimal digits that encode the code point. + */ + if (character >= '\x0000' && character <= '\x001f') + { + // jumping "\uXXXX" characters + jumpChar = 6; + + AZStd::string hexStr = AZStd::string::format("\\u%04x", static_cast(character)); + inout.replace(i, i + 1, hexStr); + } + else + { + jumpChar = 0; + } } - out = AZ::IO::PathView(in).Stem().Native(); - return !out.empty(); + i += jumpChar; + strSize += jumpChar; } - bool GetExtension(const char* in, AZStd::string& out, bool includeDot) - { - if (!in || in[0] == '\0') - { - return false; - } + return inout; + } + } // namespace Json - AZStd::string_view extensionView = AZ::IO::PathView(in).Extension().Native(); - // PathView returns extensions with the character, so remove the - // if it is not included - if (!includeDot && extensionView.starts_with(AZ_FILESYSTEM_EXTENSION_SEPARATOR)) - { - extensionView.remove_prefix(1); - } - out = extensionView; - return !out.empty(); - } + namespace Base64 + { + static const char base64pad = '='; - void ReplaceFullName(AZStd::string& inout, const char* pFileName /* = nullptr*/, const char* pFileExtension /* = nullptr*/) - { - //strip the full file name if it has one - AZ::IO::Path path(AZStd::move(inout)); - path.RemoveFilename(); - if (pFileName) - { - path /= pFileName; - } - if (pFileExtension) - { - path.ReplaceExtension(pFileExtension); - } - inout = AZStd::move(path.Native()); - } + static const char c_base64Table[] = + { + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/" + }; - void ReplaceExtension(AZStd::string& inout, const char* newExtension /* = nullptr*/) - { - //treat this as a strip - if (!newExtension || newExtension[0] == '\0') - { - return; - } - AZ::IO::Path path(AZStd::move(inout)); - path.ReplaceExtension(newExtension); - inout = AZStd::move(path.Native()); - } + static const AZ::u8 c_inverseBase64Table[] = + { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff + }; - AZStd::string& AppendSeparator(AZStd::string& inout) - { - if (inout.ends_with(AZ_WRONG_FILESYSTEM_SEPARATOR)) - { - inout.replace(inout.end() - 1, inout.end(), 1, AZ_CORRECT_FILESYSTEM_SEPARATOR); - } - else if (!inout.ends_with(AZ_CORRECT_FILESYSTEM_SEPARATOR)) - { - inout.append(1, AZ_CORRECT_FILESYSTEM_SEPARATOR); - } - return inout; - } - } // namespace Path + bool IsValidEncodedChar(const char encodedChar) + { + return c_inverseBase64Table[static_cast(encodedChar)] != 0xff; + } - namespace Json + AZStd::string Encode(const AZ::u8* in, const size_t size) { /* - According to http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf: - A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All characters may be - placed within the quotation marks except for the characters that must be escaped: quotation mark (U+0022), - reverse solidus (U+005C), and the control characters U+0000 to U+001F. + figure retrieved from the Base encoding rfc https://tools.ietf.org/html/rfc4648 + +--first octet--+-second octet--+--third octet--+ + |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| + +-----------+---+-------+-------+---+-----------+ + |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0| + +--1.index--+--2.index--+--3.index--+--4.index--+ */ - AZStd::string& ToEscapedString(AZStd::string& inout) + AZStd::string result; + + const size_t remainder = size % 3; + const size_t alignEndSize = size - remainder; + const AZ::u8* encodeBuf = in; + size_t encodeIndex = 0; + for (; encodeIndex < alignEndSize; encodeIndex += 3) { - size_t strSize = inout.size(); - - for (size_t i = 0; i < strSize; ++i) - { - char character = inout[i]; - - // defaults to 1 if it hits any cases except default - size_t jumpChar = 1; - switch (character) - { - case '"': - inout.insert(i, "\\"); - break; - - case '\\': - inout.insert(i, "\\"); - break; - - case '/': - inout.insert(i, "\\"); - break; - - case '\b': - inout.replace(i, i + 1, "\\b"); - break; - - case '\f': - inout.replace(i, i + 1, "\\f"); - break; - - case '\n': - inout.replace(i, i + 1, "\\n"); - break; - - case '\r': - inout.replace(i, i + 1, "\\r"); - break; - - case '\t': - inout.replace(i, i + 1, "\\t"); - break; - - default: - /* - Control characters U+0000 to U+001F may be represented as a six - character sequence : a reverse solidus, - followed by the lowercase letter u, followed by four hexadecimal digits that encode the code point. - */ - if (character >= '\x0000' && character <= '\x001f') - { - // jumping "\uXXXX" characters - jumpChar = 6; - - AZStd::string hexStr = AZStd::string::format("\\u%04x", static_cast(character)); - inout.replace(i, i + 1, hexStr); - } - else - { - jumpChar = 0; - } - } - - i += jumpChar; - strSize += jumpChar; - } - - return inout; - } - } // namespace Json - - namespace Base64 - { - static const char base64pad = '='; - - static const char c_base64Table[] = - { - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/" - }; - - static const AZ::u8 c_inverseBase64Table[] = - { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, - 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, - 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - }; - - bool IsValidEncodedChar(const char encodedChar) - { - return c_inverseBase64Table[static_cast(encodedChar)] != 0xff; - } - - AZStd::string Encode(const AZ::u8* in, const size_t size) - { - /* - figure retrieved from the Base encoding rfc https://tools.ietf.org/html/rfc4648 - +--first octet--+-second octet--+--third octet--+ - |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| - +-----------+---+-------+-------+---+-----------+ - |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0| - +--1.index--+--2.index--+--3.index--+--4.index--+ - */ - AZStd::string result; - - const size_t remainder = size % 3; - const size_t alignEndSize = size - remainder; - const AZ::u8* encodeBuf = in; - size_t encodeIndex = 0; - for (; encodeIndex < alignEndSize; encodeIndex += 3) - { - encodeBuf = &in[encodeIndex]; - - result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); - result.push_back(c_base64Table[((encodeBuf[0] & 0x03) << 4) | ((encodeBuf[1] & 0xf0) >> 4)]); - result.push_back(c_base64Table[((encodeBuf[1] & 0x0f) << 2) | ((encodeBuf[2] & 0xc0) >> 6)]); - result.push_back(c_base64Table[encodeBuf[2] & 0x3f]); - } - encodeBuf = &in[encodeIndex]; - if (remainder == 2) - { - result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); - result.push_back(c_base64Table[((encodeBuf[0] & 0x03) << 4) | ((encodeBuf[1] & 0xf0) >> 4)]); - result.push_back(c_base64Table[((encodeBuf[1] & 0x0f) << 2)]); - result.push_back(base64pad); - } - else if (remainder == 1) - { - result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); - result.push_back(c_base64Table[(encodeBuf[0] & 0x03) << 4]); - result.push_back(base64pad); - result.push_back(base64pad); - } - return result; + result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); + result.push_back(c_base64Table[((encodeBuf[0] & 0x03) << 4) | ((encodeBuf[1] & 0xf0) >> 4)]); + result.push_back(c_base64Table[((encodeBuf[1] & 0x0f) << 2) | ((encodeBuf[2] & 0xc0) >> 6)]); + result.push_back(c_base64Table[encodeBuf[2] & 0x3f]); } - bool Decode(AZStd::vector& out, const char* in, const size_t size) + encodeBuf = &in[encodeIndex]; + if (remainder == 2) { - if (size % 4 != 0) - { - AZ_Warning("StringFunc", size % 4 == 0, "Base 64 encoded data length must be multiple of 4"); - return false; - } - - AZStd::vector result; - result.reserve(size * 3 / 4); - const char* decodeBuf = in; - size_t decodeIndex = 0; - for (; decodeIndex < size; decodeIndex += 4) - { - decodeBuf = &in[decodeIndex]; - //Check if each character is a valid Base64 encoded character - { - // First Octet - if (!IsValidEncodedChar(decodeBuf[0])) - { - AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[0])); - return false; - } - if (!IsValidEncodedChar(decodeBuf[1])) - { - AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[1])); - return false; - } - - result.push_back((c_inverseBase64Table[static_cast(decodeBuf[0])] << 2) | ((c_inverseBase64Table[static_cast(decodeBuf[1])] & 0x30) >> 4)); - } - - { - // Second Octet - if (decodeBuf[2] == base64pad) - { - break; - } - - if (!IsValidEncodedChar(decodeBuf[2])) - { - AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[2])); - return false; - } - - result.push_back(((c_inverseBase64Table[static_cast(decodeBuf[1])] & 0x0f) << 4) | ((c_inverseBase64Table[static_cast(decodeBuf[2])] & 0x3c) >> 2)); - } - - { - // Third Octet - if (decodeBuf[3] == base64pad) - { - break; - } - - if (!IsValidEncodedChar(decodeBuf[3])) - { - AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[3])); - return false; - } - - result.push_back(((c_inverseBase64Table[static_cast(decodeBuf[2])] & 0x03) << 6) | (c_inverseBase64Table[static_cast(decodeBuf[3])] & 0x3f)); - } - } - - out = AZStd::move(result); - return true; + result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); + result.push_back(c_base64Table[((encodeBuf[0] & 0x03) << 4) | ((encodeBuf[1] & 0xf0) >> 4)]); + result.push_back(c_base64Table[((encodeBuf[1] & 0x0f) << 2)]); + result.push_back(base64pad); } + else if (remainder == 1) + { + result.push_back(c_base64Table[(encodeBuf[0] & 0xfc) >> 2]); + result.push_back(c_base64Table[(encodeBuf[0] & 0x03) << 4]); + result.push_back(base64pad); + result.push_back(base64pad); + } + + return result; } - namespace Utf8 + bool Decode(AZStd::vector& out, const char* in, const size_t size) { - bool CheckNonAsciiChar(const AZStd::string& in) + if (size % 4 != 0) { - for (int i = 0; i < in.length(); ++i) - { - char byte = in[i]; - if (byte & 0x80) - { - return true; - } - } + AZ_Warning("StringFunc", size % 4 == 0, "Base 64 encoded data length must be multiple of 4"); return false; } + + AZStd::vector result; + result.reserve(size * 3 / 4); + const char* decodeBuf = in; + size_t decodeIndex = 0; + for (; decodeIndex < size; decodeIndex += 4) + { + decodeBuf = &in[decodeIndex]; + //Check if each character is a valid Base64 encoded character + { + // First Octet + if (!IsValidEncodedChar(decodeBuf[0])) + { + AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[0])); + return false; + } + if (!IsValidEncodedChar(decodeBuf[1])) + { + AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[1])); + return false; + } + + result.push_back((c_inverseBase64Table[static_cast(decodeBuf[0])] << 2) | ((c_inverseBase64Table[static_cast(decodeBuf[1])] & 0x30) >> 4)); + } + + { + // Second Octet + if (decodeBuf[2] == base64pad) + { + break; + } + + if (!IsValidEncodedChar(decodeBuf[2])) + { + AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[2])); + return false; + } + + result.push_back(((c_inverseBase64Table[static_cast(decodeBuf[1])] & 0x0f) << 4) | ((c_inverseBase64Table[static_cast(decodeBuf[2])] & 0x3c) >> 2)); + } + + { + // Third Octet + if (decodeBuf[3] == base64pad) + { + break; + } + + if (!IsValidEncodedChar(decodeBuf[3])) + { + AZ_Warning("StringFunc", false, "Invalid Base64 encoded text at offset %tu", AZStd::distance(in, &decodeBuf[3])); + return false; + } + + result.push_back(((c_inverseBase64Table[static_cast(decodeBuf[2])] & 0x03) << 6) | (c_inverseBase64Table[static_cast(decodeBuf[3])] & 0x3f)); + } + } + + out = AZStd::move(result); + return true; } - } // namespace StringFunc -} // namespace AZ + } + + namespace Utf8 + { + bool CheckNonAsciiChar(const AZStd::string& in) + { + for (int i = 0; i < in.length(); ++i) + { + char byte = in[i]; + if (byte & 0x80) + { + return true; + } + } + return false; + } + } +} // namespace AZ::StringFunc diff --git a/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.h b/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.h index 1e651afc93..55236a0fff 100644 --- a/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.h +++ b/Code/Framework/AzCore/AzCore/StringFunc/StringFunc.h @@ -485,10 +485,11 @@ namespace AZ //! CalculateBranchToken /*! Calculate the branch token that is used for asset processor connection negotiations * - * \param appRootPath - The absolute path of the app root to base the token calculation on + * \param engineRootPath - The absolute path to the engine root to base the token calculation on * \param token - The result of the branch token calculation */ - void CalculateBranchToken(const AZStd::string& appRootPath, AZStd::string& token); + void CalculateBranchToken(AZStd::string_view engineRootPath, AZStd::string& token); + void CalculateBranchToken(AZStd::string_view engineRootPath, AZ::IO::FixedMaxPathString& token); } ////////////////////////////////////////////////////////////////////////// diff --git a/Code/Framework/AzCore/AzCore/Task/TaskGraphSystemComponent.cpp b/Code/Framework/AzCore/AzCore/Task/TaskGraphSystemComponent.cpp index 56b56e96a1..1cacef7ff3 100644 --- a/Code/Framework/AzCore/AzCore/Task/TaskGraphSystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Task/TaskGraphSystemComponent.cpp @@ -30,8 +30,13 @@ namespace AZ if (Interface::Get() == nullptr) { + #if (AZ_TRAIT_THREAD_NUM_TASK_GRAPH_WORKER_THREADS) + const uint32_t numberOfWorkerThreads = AZ_TRAIT_THREAD_NUM_TASK_GRAPH_WORKER_THREADS; + #else + const uint32_t numberOfWorkerThreads = Threading::CalcNumWorkerThreads(cl_taskGraphThreadsConcurrencyRatio, cl_taskGraphThreadsMinNumber, cl_taskGraphThreadsNumReserved); + #endif // (AZ_TRAIT_THREAD_NUM_TASK_GRAPH_WORKER_THREADS) Interface::Register(this); // small window that another thread can try to use taskgraph between this line and the set instance. - m_taskExecutor = aznew TaskExecutor(Threading::CalcNumWorkerThreads(cl_taskGraphThreadsConcurrencyRatio, cl_taskGraphThreadsMinNumber, cl_taskGraphThreadsNumReserved)); + m_taskExecutor = aznew TaskExecutor(numberOfWorkerThreads); TaskExecutor::SetInstance(m_taskExecutor); } } diff --git a/Code/Framework/AzCore/AzCore/Time/ITime.h b/Code/Framework/AzCore/AzCore/Time/ITime.h index a97ba2319a..a61b629f35 100644 --- a/Code/Framework/AzCore/AzCore/Time/ITime.h +++ b/Code/Framework/AzCore/AzCore/Time/ITime.h @@ -12,8 +12,8 @@ #include #include #include -#include #include +#include namespace AZ { @@ -24,15 +24,21 @@ namespace AZ //! Using int64_t as the underlying type, this is good to represent approximately 292,471 years AZ_TYPE_SAFE_INTEGRAL(TimeUs, int64_t); + namespace Time + { + static const AZ::TimeMs ZeroTimeMs = AZ::TimeMs{ 0 }; + static const AZ::TimeUs ZeroTimeUs = AZ::TimeUs{ 0 }; + } + //! @class ITime //! @brief This is an AZ::Interface<> for managing time related operations. //! AZ::ITime and associated types may not operate in realtime. These abstractions are to allow our application //! simulation to operate both slower and faster than realtime in a well defined and user controllable manner - //! The rate at which time passes for AZ::ITime is controlled by the cvar t_scale - //! t_scale == 0 means simulation time should halt - //! 0 < t_scale < 1 will cause time to pass slower than realtime, with t_scale 0.1 being roughly 1/10th realtime - //! t_scale == 1 will cause time to pass at roughly realtime - //! t_scale > 1 will cause time to pass faster than normal, with t_scale 10 being roughly 10x realtime + //! The rate at which time passes for AZ::ITime is controlled by the cvar t_simulationTickScale + //! t_simulationTickScale == 0 means simulation time should halt + //! 0 < t_simulationTickScale < 1 will cause time to pass slower than realtime, with t_simulationTickScale 0.1 being roughly 1/10th realtime + //! t_simulationTickScale == 1 will cause time to pass at roughly realtime + //! t_simulationTickScale > 1 will cause time to pass faster than normal, with t_simulationTickScale 10 being roughly 10x realtime class ITime { public: @@ -41,15 +47,72 @@ namespace AZ ITime() = default; virtual ~ITime() = default; - //! Returns the number of milliseconds since application start. - //! @return the number of milliseconds that have elapsed since application start + //! Returns the number of milliseconds since application start scaled by t_simulationTickScale. + //! @return The number of milliseconds that have elapsed since application start. virtual TimeMs GetElapsedTimeMs() const = 0; - //! Returns the number of microseconds since application start. + //! Returns the number of microseconds since application start scaled by t_simulationTickScale. //! @return the number of microseconds that have elapsed since application start virtual TimeUs GetElapsedTimeUs() const = 0; + + //! Returns the number of milliseconds since application start. + //! This value is not affected by the t_simulationTickScale cvar. + //! @return The number of milliseconds that have elapsed since application start. + virtual TimeMs GetRealElapsedTimeMs() const = 0; + + //! Returns the number of microseconds since application start. + //! This value is not affected by the t_simulationTickScale cvar. + //! @return The number of microseconds that have elapsed since application start. + virtual TimeUs GetRealElapsedTimeUs() const = 0; + + //! Returns the current simulation tick delta time. + //! This is affected by the cvars t_simulationTickScale, t_simulationTickDeltaOverride, and t_maxGameTickDelta. + //! @return The number of microseconds elapsed since the last game tick. + virtual TimeUs GetSimulationTickDeltaTimeUs() const = 0; + + //! Returns the non-manipulated tick time. + //! @return The number of microseconds elapsed since the last game tick. + virtual TimeUs GetRealTickDeltaTimeUs() const = 0; + + //! Returns the time since application start of when the last simulation tick was updated. + virtual TimeUs GetLastSimulationTickTime() const = 0; + + //! If > 0 this will override the simulation tick delta time with the provided value. + //! When enabled this will ignore any set simulation tick scale. + //! Setting to 0 disables the override. + //! @param timeMs The time in milliseconds to use for the tick delta. + virtual void SetSimulationTickDeltaOverride(TimeMs timeMs) = 0; + + //! Returns the current simulation tick override. + //! 0 means disabled. + //! @returns The current simulation tick override in milliseconds. + virtual TimeMs GetSimulationTickDeltaOverride() const = 0; + + //! A scalar amount to adjust the passage of time by, 1.0 == realtime, 0.5 == half realtime, 2.0 == doubletime. + //! @param scale The scalar value to apply to the simulation time. + virtual void SetSimulationTickScale(float scale) = 0; + + //! Returns the current simulation tick scale. + //! 1.0 == realtime, 0.5 == half realtime, 2.0 == doubletime. + //! @returns The simulation tick scale value. + virtual float GetSimulationTickScale() const = 0; + + //! The minimum rate to force the simulation tick to run. + //! 0 for as fast as possible. 30 = ~33ms, 60 = ~16ms. + //! Setting to 0 will disable rate limiting. + //! @note It is not guaranteed to hit the requested tick rate exactly. + //! @param rate The rate in frames per second. + virtual void SetSimulationTickRate(int rate) = 0; + + //! Return the current simulation tick rate. + //! 0 means disabled. + //! @return The rate in frames per second. + virtual int32_t GetSimulationTickRate() const = 0; AZ_DISABLE_COPY_MOVE(ITime); + + static const AZ::TimeMs ZeroTimeMs = AZ::TimeMs{ 0 }; + static const AZ::TimeUs ZeroTimeUs = AZ::TimeUs{ 0 }; }; // EBus wrapper for ScriptCanvas @@ -74,6 +137,36 @@ namespace AZ return AZ::Interface::Get()->GetElapsedTimeUs(); } + //! This is a simple convenience wrapper + inline TimeMs GetRealElapsedTimeMs() + { + return AZ::Interface::Get()->GetRealElapsedTimeMs(); + } + + //! This is a simple convenience wrapper + inline TimeUs GetRealElapsedTimeUs() + { + return AZ::Interface::Get()->GetRealElapsedTimeUs(); + } + + //! This is a simple convenience wrapper + inline TimeUs GetSimulationTickDeltaTimeUs() + { + return AZ::Interface::Get()->GetSimulationTickDeltaTimeUs(); + } + + //! This is a simple convenience wrapper + inline TimeUs GetRealTickDeltaTimeUs() + { + return AZ::Interface::Get()->GetRealTickDeltaTimeUs(); + } + + //! This is a simple convenience wrapper + inline TimeUs GetLastSimulationTickTime() + { + return AZ::Interface::Get()->GetLastSimulationTickTime(); + } + //! Converts from milliseconds to microseconds inline TimeUs TimeMsToUs(TimeMs value) { @@ -92,12 +185,24 @@ namespace AZ return static_cast(value) / 1000.0f; } + //! Converts from milliseconds to seconds + inline double TimeMsToSecondsDouble(TimeMs value) + { + return static_cast(value) / 1000.0; + } + //! Converts from microseconds to seconds inline float TimeUsToSeconds(TimeUs value) { return static_cast(value) / 1000000.0f; } + //! Converts from microseconds to seconds + inline double TimeUsToSecondsDouble(TimeUs value) + { + return static_cast(value) / 1000000.0; + } + //! Converts from milliseconds to AZStd::chrono::time_point inline auto TimeMsToChrono(TimeMs value) { @@ -113,6 +218,20 @@ namespace AZ auto chronoValue = AZStd::chrono::microseconds(aznumeric_cast(value)); return epoch + chronoValue; } + + //! A utility function to convert from seconds to TimeMs + inline TimeMs SecondsToTimeMs(const double value) + { + const double valueMs = value * 1000.0; + return static_cast(static_cast(valueMs)); + } + + //! A utility function to convert from seconds to TimeUs + inline TimeUs SecondsToTimeUs(const double value) + { + const double valueMs = value * 1000000.0; + return static_cast(static_cast(valueMs)); + } } // namespace AZ AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(AZ::TimeMs); diff --git a/Code/Framework/AzCore/AzCore/Time/TimeSystem.cpp b/Code/Framework/AzCore/AzCore/Time/TimeSystem.cpp new file mode 100644 index 0000000000..a5e0908ac4 --- /dev/null +++ b/Code/Framework/AzCore/AzCore/Time/TimeSystem.cpp @@ -0,0 +1,213 @@ +/* + * 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 +#include +#include +#include + +namespace AZ +{ + namespace + { + void cvar_t_simulationTickScale_Changed(const float& value) + { + if (auto* timeSystem = AZ::Interface::Get()) + { + timeSystem->SetSimulationTickScale(value); + } + } + + void cvar_t_simulationTickDeltaOverride_Changed(const float& value) + { + if (auto* timeSystem = AZ::Interface::Get()) + { + timeSystem->SetSimulationTickDeltaOverride(AZ::SecondsToTimeMs(value)); + } + } + + void cvar_t_simulationTickRate_Changed(const int& rate) + { + AZ_Warning("tick", false, "Simulation tick rate limiting is currently disabled. Setting will not be applied."); + if (auto* timeSystem = AZ::Interface::Get()) + { + timeSystem->SetSimulationTickRate(rate); + } + } + } // namespace + + AZ_CVAR(float, t_simulationTickScale, 1.0f, cvar_t_simulationTickScale_Changed, AZ::ConsoleFunctorFlags::Null, + "A scalar amount to adjust time passage by, 1.0 == realtime, 0.5 == half realtime, 2.0 == doubletime"); + + AZ_CVAR(float, t_simulationTickDeltaOverride, 0.0f, cvar_t_simulationTickDeltaOverride_Changed, AZ::ConsoleFunctorFlags::Null, + "If > 0, overrides the simulation tick delta time with the provided value (Seconds) and ignores any t_simulationTickScale value."); + + AZ_CVAR(int, t_simulationTickRate, 0, cvar_t_simulationTickRate_Changed, AZ::ConsoleFunctorFlags::Null, + "The minimum rate to force the game simulation tick to run. 0 for as fast as possible. 30 = ~33ms, 60 = ~16ms"); + + void TimeSystem::Reflect(AZ::ReflectContext* context) + { + if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) + { + serializeContext->Class() + ->Version(1); + } + } + + TimeSystem::TimeSystem() + { + m_lastInvokedTimeUs = static_cast(AZStd::GetTimeNowMicroSecond()); + AZ::Interface::Register(this); + ITimeRequestBus::Handler::BusConnect(); + } + + TimeSystem::~TimeSystem() + { + AZ::Interface::Unregister(this); + ITimeRequestBus::Handler::BusDisconnect(); + } + + TimeMs TimeSystem::GetElapsedTimeMs() const + { + return AZ::TimeUsToMs(GetElapsedTimeUs()); + } + + TimeUs TimeSystem::GetElapsedTimeUs() const + { + TimeUs currentTime = static_cast(AZStd::GetTimeNowMicroSecond()); + TimeUs deltaTime = currentTime - m_lastInvokedTimeUs; + + if (t_simulationTickScale != 1.0f) + { + const float floatDelta = AZStd::GetMax(static_cast(deltaTime) * t_simulationTickScale, 1.0f); + deltaTime = static_cast(static_cast(floatDelta)); + } + + m_accumulatedTimeUs += deltaTime; + m_lastInvokedTimeUs = currentTime; + + return m_accumulatedTimeUs; + } + + TimeMs TimeSystem::GetRealElapsedTimeMs() const + { + return AZ::TimeUsToMs(GetRealElapsedTimeUs()); + } + + TimeUs TimeSystem::GetRealElapsedTimeUs() const + { + return static_cast(AZStd::GetTimeNowMicroSecond()); + } + + TimeUs TimeSystem::GetSimulationTickDeltaTimeUs() const + { + return m_simulationTickDeltaTimeUs; + } + + TimeUs TimeSystem::GetRealTickDeltaTimeUs() const + { + return m_realTickDeltaTimeUs; + } + + TimeUs TimeSystem::GetLastSimulationTickTime() const + { + return m_lastSimulationTickTimeUs; + } + + TimeUs TimeSystem::AdvanceTickDeltaTimes() + { + const TimeUs currentTimeUs = static_cast(AZStd::GetTimeNowMicroSecond()); + + //real time + m_realTickDeltaTimeUs = currentTimeUs - m_lastSimulationTickTimeUs; + + //game time + if (m_simulationTickDeltaOverride > AZ::Time::ZeroTimeUs) + { + m_simulationTickDeltaTimeUs = m_simulationTickDeltaOverride; + m_lastSimulationTickTimeUs = currentTimeUs; + return m_simulationTickDeltaTimeUs; + } + + m_simulationTickDeltaTimeUs = currentTimeUs - m_lastSimulationTickTimeUs; + + if (!AZ::IsClose(t_simulationTickScale, 1.0f)) + { + const double floatDelta = AZStd::GetMax(static_cast(m_simulationTickDeltaTimeUs) * static_cast(t_simulationTickScale), 1.0); + m_simulationTickDeltaTimeUs = static_cast(static_cast(floatDelta)); + } + m_lastSimulationTickTimeUs = currentTimeUs; + + return m_simulationTickDeltaTimeUs; + } + + void TimeSystem::ApplyTickRateLimiterIfNeeded() + { + // Currently disabling the Tick rate limiter as there are some reported issues when using it. + #ifdef ENABLE_TICK_RATE_LIMITER + // If tick rate limiting is on, ensure (1 / t_simulationTickRate) ms has elapsed since the last frame, + // sleeping if there's still time remaining. + if (t_simulationTickRate > 0) + { + const TimeUs currentTimeUs = AZ::GetRealElapsedTimeUs(); + const TimeUs timeUntilNextTick = (m_lastSimulationTickTimeUs + m_simulationTickLimitTimeUs) - currentTimeUs; + if (timeUntilNextTick > AZ::Time::ZeroTimeUs) + { + AZ_TracePrintf("tick", "Sleeping for %.2f", AZ::TimeUsToSecondsDouble(timeUntilNextTick)); + AZStd::this_thread::sleep_for(AZStd::chrono::microseconds(static_cast(timeUntilNextTick))); + } + } + #endif // #ifdef ENABLE_TICK_RATE_LIMITER + } + + void TimeSystem::SetSimulationTickDeltaOverride(TimeMs timeMs) + { + const TimeUs timeUs = AZ::TimeMsToUs(timeMs); + if (timeUs != m_simulationTickDeltaOverride) + { + m_simulationTickDeltaOverride = timeUs; + t_simulationTickDeltaOverride = AZ::TimeUsToSeconds(timeUs); //update the cvar + } + } + + TimeMs TimeSystem::GetSimulationTickDeltaOverride() const + { + return AZ::TimeUsToMs(m_simulationTickDeltaOverride); + } + + void TimeSystem::SetSimulationTickScale(float scale) + { + if (!AZ::IsClose(scale, t_simulationTickScale)) + { + t_simulationTickScale = scale; + } + } + + float TimeSystem::GetSimulationTickScale() const + { + return t_simulationTickScale; + } + + void TimeSystem::SetSimulationTickRate(int rate) + { + m_simulationTickLimitRate = AZStd::abs(rate); + if (m_simulationTickLimitRate != 0) + { + m_simulationTickLimitTimeUs = AZ::SecondsToTimeUs(1.0f / m_simulationTickLimitRate); + } + else + { + m_simulationTickLimitTimeUs = AZ::Time::ZeroTimeUs; + } + } + + int32_t TimeSystem::GetSimulationTickRate() const + { + return m_simulationTickLimitRate; + } +} // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Time/TimeSystem.h b/Code/Framework/AzCore/AzCore/Time/TimeSystem.h new file mode 100644 index 0000000000..ded70c9be5 --- /dev/null +++ b/Code/Framework/AzCore/AzCore/Time/TimeSystem.h @@ -0,0 +1,83 @@ +/* + * 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 + * + */ + +#pragma once + +#include +#include + +namespace AZ +{ + class ReflectContext; + + //! Implementation of the ITime system interface. + class TimeSystem + : public ITimeRequestBus::Handler + { + public: + AZ_RTTI(AZ::TimeSystem, "{CE1C5E4F-7DC1-4248-B10C-AC55E8924A48}", AZ::ITime); + + static void Reflect(AZ::ReflectContext* context); + + TimeSystem(); + virtual ~TimeSystem(); + + //! ITime overrides. + //! @{ + TimeMs GetElapsedTimeMs() const override; + TimeUs GetElapsedTimeUs() const override; + TimeMs GetRealElapsedTimeMs() const override; + TimeUs GetRealElapsedTimeUs() const override; + TimeUs GetSimulationTickDeltaTimeUs() const override; + TimeUs GetRealTickDeltaTimeUs() const override; + TimeUs GetLastSimulationTickTime() const override; + void SetSimulationTickDeltaOverride(TimeMs timeMs) override; + TimeMs GetSimulationTickDeltaOverride() const override; + void SetSimulationTickScale(float scale) override; + float GetSimulationTickScale() const override; + void SetSimulationTickRate(int rate) override; + int32_t GetSimulationTickRate() const override; + //! @} + + //! Advances the Simulation and Real tick delta time counters. + //! This is called from the owner of the TimeSystem, ComponentApplication in Tick(). + //! @return The delta in microseconds from the last call to AdvanceTickDeltaTimes(). Value will be the same as GetSimulationTickDeltaTimeUs(). + TimeUs AdvanceTickDeltaTimes(); + + //! If t_simulationTickRate is >0 this will try to have the game delta time run at a maximum of the rate set. + //! This is called from the owner of the TimeSystem, ComponentApplication in Tick(). + //! example. If t_simulationTickRate is set to 60Fps, and the game tick delta is <17ms(60fps), this will add a sleep for the remaining time. + //! example. If t_simulationTickRate is set to 60Fps, and the game tick delta is >=17ms(60fps), this will not sleep at all. + //! @note It is not guaranteed to hit the requested tick rate exactly. + void ApplyTickRateLimiterIfNeeded(); + private: + //! Used to calculate the delta time between calls to GetElapsedTimeMs/TimeUs(). + //! Mutable to allow GetElapsedTimeMs/TimeUs() to be a const functions. + mutable TimeUs m_lastInvokedTimeUs = AZ::Time::ZeroTimeUs; + + //! Accumulates the delta time of GetElapsedTimeMs/TimeUs() calls. + //! Mutable to allow GetElapsedTimeMs/TimeUs() to be a const functions. + mutable TimeUs m_accumulatedTimeUs = AZ::Time::ZeroTimeUs; + + //! The current game tick delta time. + //! Can be affected by time system cvars. + //! Updated in AdvanceTickDeltaTimes(). + TimeUs m_simulationTickDeltaTimeUs = AZ::Time::ZeroTimeUs; + + //! The current real tick delta time. + //! Will not be affected by time system cvars. + //! Updated in AdvanceTickDeltaTimes(). + TimeUs m_realTickDeltaTimeUs = AZ::Time::ZeroTimeUs; + + TimeUs m_lastSimulationTickTimeUs = AZ::Time::ZeroTimeUs; //!< Used to determine the game tick delta time. + + TimeUs m_simulationTickDeltaOverride = AZ::Time::ZeroTimeUs; // -#include -#include - -namespace AZ -{ - AZ_CVAR(float, t_scale, 1.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "A scalar amount to adjust time passage by, 1.0 == realtime, 0.5 == half realtime, 2.0 == doubletime"); - - void TimeSystemComponent::Reflect(AZ::ReflectContext* context) - { - if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) - { - serializeContext->Class() - ->Version(1); - } - } - - void TimeSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) - { - provided.push_back(AZ_CRC_CE("TimeService")); - } - - void TimeSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) - { - incompatible.push_back(AZ_CRC_CE("TimeService")); - } - - TimeSystemComponent::TimeSystemComponent() - { - m_lastInvokedTimeUs = static_cast(AZStd::GetTimeNowMicroSecond()); - AZ::Interface::Register(this); - ITimeRequestBus::Handler::BusConnect(); - } - - TimeSystemComponent::~TimeSystemComponent() - { - ITimeRequestBus::Handler::BusDisconnect(); - AZ::Interface::Unregister(this); - } - - void TimeSystemComponent::Activate() - { - ; - } - - void TimeSystemComponent::Deactivate() - { - ; - } - - TimeMs TimeSystemComponent::GetElapsedTimeMs() const - { - return TimeUsToMs(GetElapsedTimeUs()); - } - - TimeUs TimeSystemComponent::GetElapsedTimeUs() const - { - TimeUs currentTime = static_cast(AZStd::GetTimeNowMicroSecond()); - TimeUs deltaTime = currentTime - m_lastInvokedTimeUs; - - if (t_scale != 1.0f) - { - float floatDelta = static_cast(deltaTime) * t_scale; - deltaTime = static_cast(static_cast(floatDelta)); - } - - m_accumulatedTimeUs += deltaTime; - m_lastInvokedTimeUs = currentTime; - - return m_accumulatedTimeUs; - } -} diff --git a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h b/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h deleted file mode 100644 index 3ab3dbc234..0000000000 --- a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 - * - */ - -#pragma once - -#include -#include -#include - -namespace AZ -{ - //! Implementation of the ITime system interface. - class TimeSystemComponent - : public AZ::Component - , public ITimeRequestBus::Handler - { - public: - - AZ_COMPONENT(TimeSystemComponent, "{CE1C5E4F-7DC1-4248-B10C-AC55E8924A48}"); - - static void Reflect(AZ::ReflectContext* context); - static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); - static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible); - - TimeSystemComponent(); - virtual ~TimeSystemComponent(); - - //! AZ::Component overrides. - //! @{ - void Activate() override; - void Deactivate() override; - //! @} - - //! ITime overrides. - //! @{ - TimeMs GetElapsedTimeMs() const override; - TimeUs GetElapsedTimeUs() const override; - //! @} - - private: - - mutable TimeUs m_lastInvokedTimeUs = TimeUs{0}; - mutable TimeUs m_accumulatedTimeUs = TimeUs{0}; - }; -} diff --git a/Code/Framework/AzCore/AzCore/UnitTest/MockComponentApplication.h b/Code/Framework/AzCore/AzCore/UnitTest/MockComponentApplication.h index 8f069da9dd..190fa09cb7 100644 --- a/Code/Framework/AzCore/AzCore/UnitTest/MockComponentApplication.h +++ b/Code/Framework/AzCore/AzCore/UnitTest/MockComponentApplication.h @@ -41,7 +41,6 @@ namespace UnitTest MOCK_METHOD0(GetSerializeContext, AZ::SerializeContext* ()); MOCK_METHOD0(GetJsonRegistrationContext, AZ::JsonRegistrationContext* ()); MOCK_METHOD0(GetBehaviorContext, AZ::BehaviorContext* ()); - MOCK_CONST_METHOD0(GetAppRoot, const char* ()); MOCK_CONST_METHOD0(GetEngineRoot, const char* ()); MOCK_CONST_METHOD0(GetExecutableFolder, const char* ()); MOCK_CONST_METHOD1(QueryApplicationType, void(AZ::ApplicationTypeQuery&)); diff --git a/Code/Framework/AzCore/AzCore/UnitTest/Mocks/MockITime.h b/Code/Framework/AzCore/AzCore/UnitTest/Mocks/MockITime.h new file mode 100644 index 0000000000..3d31056e27 --- /dev/null +++ b/Code/Framework/AzCore/AzCore/UnitTest/Mocks/MockITime.h @@ -0,0 +1,119 @@ +/* + * 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 + * + */ +#pragma once + +#include +#include + +namespace AZ +{ + class MockTimeSystem; + using NiceTimeSystemMock =::testing::NiceMock; + + //used if you wish to mock any of the Get time functions. + class MockTimeSystem + : public ITimeRequestBus::Handler + { + public: + MockTimeSystem() + { + AZ::Interface::Register(this); + ITimeRequestBus::Handler::BusConnect(); + } + virtual ~MockTimeSystem() + { + AZ::Interface::Unregister(this); + ITimeRequestBus::Handler::BusDisconnect(); + } + + MOCK_CONST_METHOD0(GetElapsedTimeMs, TimeMs()); + MOCK_CONST_METHOD0(GetElapsedTimeUs, TimeUs()); + MOCK_CONST_METHOD0(GetRealElapsedTimeMs, TimeMs()); + MOCK_CONST_METHOD0(GetRealElapsedTimeUs, TimeUs()); + MOCK_CONST_METHOD0(GetSimulationTickDeltaTimeUs, TimeUs()); + MOCK_CONST_METHOD0(GetRealTickDeltaTimeUs, TimeUs()); + MOCK_CONST_METHOD0(GetLastSimulationTickTime, TimeUs()); + MOCK_METHOD1(SetSimulationTickDeltaOverride, void(TimeMs)); + MOCK_CONST_METHOD0(GetSimulationTickDeltaOverride, TimeMs()); + MOCK_METHOD1(SetSimulationTickScale, void(float)); + MOCK_CONST_METHOD0(GetSimulationTickScale, float()); + MOCK_METHOD1(SetSimulationTickRate, void(int)); + MOCK_CONST_METHOD0(GetSimulationTickRate, int32_t()); + }; + + //used if you wish to override any of the Get time functions with specific functionality. + class StubTimeSystem + : public AZ::TimeSystem + { + public: + AZ_RTTI(AZ::StubTimeSystem, "{DD5D5A6A-345F-49FD-A61E-A40E63C49CFA}", AZ::TimeSystem); + + virtual AZ::TimeMs GetElapsedTimeMs() const override + { + return AZ::Time::ZeroTimeMs; + } + + virtual AZ::TimeUs GetElapsedTimeUs() const override + { + return AZ::Time::ZeroTimeUs; + } + + virtual AZ::TimeMs GetRealElapsedTimeMs() const override + { + return AZ::Time::ZeroTimeMs; + } + + virtual AZ::TimeUs GetRealElapsedTimeUs() const override + { + return AZ::Time::ZeroTimeUs; + } + + virtual AZ::TimeUs GetSimulationTickDeltaTimeUs() const override + { + return AZ::Time::ZeroTimeUs; + } + + virtual AZ::TimeUs GetRealTickDeltaTimeUs() const override + { + return AZ::Time::ZeroTimeUs; + } + + virtual AZ::TimeUs GetLastSimulationTickTime() const override + { + return AZ::Time::ZeroTimeUs; + } + + virtual void SetSimulationTickDeltaOverride([[maybe_unused]]TimeMs timeMs) override + { + } + + virtual TimeMs GetSimulationTickDeltaOverride() const override + { + return AZ::Time::ZeroTimeMs; + } + + virtual void SetSimulationTickScale([[maybe_unused]] float scale) override + { + } + + virtual float GetSimulationTickScale() const override + { + return 1.0f; + } + + virtual void SetSimulationTickRate([[maybe_unused]] int rate) override + { + } + + virtual int32_t GetSimulationTickRate() const override + { + return 0; + } + }; + +} // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/UnitTest/UnitTest.h b/Code/Framework/AzCore/AzCore/UnitTest/UnitTest.h index 5244493c78..be9199b6c4 100644 --- a/Code/Framework/AzCore/AzCore/UnitTest/UnitTest.h +++ b/Code/Framework/AzCore/AzCore/UnitTest/UnitTest.h @@ -60,6 +60,7 @@ namespace UnitTest m_isAssertTest = true; m_numAssertsFailed = 0; } + int StopAssertTests() { m_isAssertTest = false; @@ -69,6 +70,11 @@ namespace UnitTest } bool m_isAssertTest; + bool m_suppressErrors = true; + bool m_suppressWarnings = true; + bool m_suppressAsserts = true; + bool m_suppressOutput = true; + bool m_suppressPrintf = true; int m_numAssertsFailed; }; @@ -114,7 +120,7 @@ namespace UnitTest // utility classes that you can derive from or contain, which suppress AZ_Asserts // and AZ_Errors to the below macros (processAssert, etc) - // If TraceBusHook or TraceBusRedirector have been started in your unit tests, + // If TraceBusHook or TraceBusRedirector have been started in your unit tests, // use AZ_TEST_START_TRACE_SUPPRESSION and AZ_TEST_STOP_TRACE_SUPPRESSION(numExpectedAsserts) macros to perform AZ_Assert and AZ_Error suppression class TraceBusRedirector : public AZ::Debug::TraceMessageBus::Handler @@ -124,16 +130,19 @@ namespace UnitTest if (UnitTest::TestRunner::Instance().m_isAssertTest) { UnitTest::TestRunner::Instance().ProcessAssert(message, file, line, false); + return true; } - else + else if (UnitTest::TestRunner::Instance().m_suppressAsserts) { GTEST_MESSAGE_AT_(file, line, message, ::testing::TestPartResult::kNonFatalFailure); + return true; } - return true; + + return false; } bool OnAssert(const char* /*message*/) override { - return true; // stop processing + return UnitTest::TestRunner::Instance().m_suppressAsserts; // stop processing } bool OnPreError(const char* /*window*/, const char* file, int line, const char* /*func*/, const char* message) override { @@ -142,6 +151,7 @@ namespace UnitTest UnitTest::TestRunner::Instance().ProcessAssert(message, file, line, false); return true; } + return false; } bool OnError(const char* /*window*/, const char* message) override @@ -149,12 +159,15 @@ namespace UnitTest if (UnitTest::TestRunner::Instance().m_isAssertTest) { UnitTest::TestRunner::Instance().ProcessAssert(message, __FILE__, __LINE__, UnitTest::AssertionExpr(false)); + return true; } - else + else if (UnitTest::TestRunner::Instance().m_suppressErrors) { GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure); + return true; } - return true; // stop processing + + return false; } bool OnPreWarning(const char* /*window*/, const char* /*fileName*/, int /*line*/, const char* /*func*/, const char* /*message*/) override { @@ -163,21 +176,21 @@ namespace UnitTest } bool OnWarning(const char* /*window*/, const char* /*message*/) override { - return true; + return UnitTest::TestRunner::Instance().m_suppressWarnings; } bool OnOutput(const char* /*window*/, const char* /*message*/) override { - return true; + return UnitTest::TestRunner::Instance().m_suppressOutput; } bool OnPrintf(const char* window, const char* message) override { if (AZStd::string_view(window) == "Memory") // We want to print out the memory leak's stack traces { - ColoredPrintf(COLOR_RED, "[ MEMORY ] %s", message); + ColoredPrintf(COLOR_RED, "[ MEMORY ] %s", message); } - return true; + return UnitTest::TestRunner::Instance().m_suppressPrintf; } }; @@ -259,7 +272,6 @@ namespace UnitTest bool m_environmentSetup = false; bool m_createdAllocator = false; }; - } diff --git a/Code/Framework/AzCore/AzCore/azcore_files.cmake b/Code/Framework/AzCore/AzCore/azcore_files.cmake index abb16d61a0..aa5d49dccc 100644 --- a/Code/Framework/AzCore/AzCore/azcore_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcore_files.cmake @@ -663,8 +663,8 @@ set(FILES Threading/ThreadUtils.h Threading/ThreadUtils.cpp Time/ITime.h - Time/TimeSystemComponent.cpp - Time/TimeSystemComponent.h + Time/TimeSystem.cpp + Time/TimeSystem.h ) # Prevent the following files from being grouped in UNITY builds diff --git a/Code/Framework/AzCore/AzCore/azcoretestcommon_files.cmake b/Code/Framework/AzCore/AzCore/azcoretestcommon_files.cmake index 6c25641f9f..e31dc803b9 100644 --- a/Code/Framework/AzCore/AzCore/azcoretestcommon_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcoretestcommon_files.cmake @@ -12,5 +12,6 @@ set(FILES UnitTest/UnitTest.h UnitTest/TestTypes.h UnitTest/Mocks/MockFileIOBase.h + UnitTest/Mocks/MockITime.h UnitTest/Mocks/MockSettingsRegistry.h ) diff --git a/Code/Framework/AzCore/AzCore/std/containers/deque.h b/Code/Framework/AzCore/AzCore/std/containers/deque.h index db585e0ec9..9eed66aeb3 100644 --- a/Code/Framework/AzCore/AzCore/std/containers/deque.h +++ b/Code/Framework/AzCore/AzCore/std/containers/deque.h @@ -1233,6 +1233,14 @@ namespace AZStd right.swap(AZStd::forward(left)); } + template + decltype(auto) erase(deque& container, const U& value) + { + auto iter = AZStd::remove(container.begin(), container.end(), value); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } template decltype(auto) erase_if(deque& container, Predicate predicate) { diff --git a/Code/Framework/AzCore/AzCore/std/containers/fixed_vector.h b/Code/Framework/AzCore/AzCore/std/containers/fixed_vector.h index 7c0cca0308..82faaab2d8 100644 --- a/Code/Framework/AzCore/AzCore/std/containers/fixed_vector.h +++ b/Code/Framework/AzCore/AzCore/std/containers/fixed_vector.h @@ -130,10 +130,11 @@ namespace AZStd::Internal //! Constructors - constexpr fixed_trivial_storage() = default; + fixed_trivial_storage() = default; template >> - constexpr fixed_trivial_storage(AZStd::initializer_list ilist) noexcept + fixed_trivial_storage(AZStd::initializer_list ilist) noexcept + : m_size(aznumeric_caster(ilist.size())) { AZSTD_CONTAINER_ASSERT(ilist.size() <= capacity(), "Initializer list cannot be larger than storage capacity"); size_t index{}; @@ -141,20 +142,19 @@ namespace AZStd::Internal { m_data[index++] = element; } - resize_no_construct(ilist.size()); } - constexpr pointer data() noexcept + pointer data() noexcept { return m_data; } - constexpr const_pointer data() const noexcept + const_pointer data() const noexcept { return m_data; } //! Number of elements currently stored. - constexpr size_type size() const noexcept + size_type size() const noexcept { return m_size; } @@ -164,12 +164,12 @@ namespace AZStd::Internal return Capacity; } //! Is the storage empty? - constexpr bool empty() const noexcept + bool empty() const noexcept { return size() == 0; } //! Is the storage full? - constexpr bool full() const noexcept + bool full() const noexcept { return size() == capacity(); } @@ -186,7 +186,7 @@ namespace AZStd::Internal //! Increases size of the storage by one. //! Always fails for empty storage. template >> - constexpr reference emplace_back(Args&&... args) noexcept + reference emplace_back(Args&&... args) noexcept { AZSTD_CONTAINER_ASSERT(!full(), "emplace_back cannot be invoked on full storage"); reference new_element = *(data() + size()); @@ -196,7 +196,7 @@ namespace AZStd::Internal } //! Removes the last element of the storage. //! Precondition: size is not empty - constexpr void pop_back() noexcept + void pop_back() noexcept { AZSTD_CONTAINER_ASSERT(!empty(), "pop_back cannot be invoked on empty storage"); resize_no_construct(size() - 1); @@ -205,7 +205,7 @@ namespace AZStd::Internal //! removing elements (unsafe). //! //! Updates the size of the container while checking that the new size is less than capacity - constexpr void resize_no_construct(size_t new_size) noexcept + void resize_no_construct(size_t new_size) noexcept { AZSTD_CONTAINER_ASSERT(new_size <= capacity(), "New size cannot be larger than capacity"); m_size = aznumeric_cast(new_size); @@ -215,19 +215,19 @@ namespace AZStd::Internal //! This does not modify the size of the storage //! This is a no-op for trivial types template >> - constexpr void unsafe_destroy(InputIt, InputIt) noexcept + void unsafe_destroy(InputIt, InputIt) noexcept { } //! Destructs all elements of the storage. //! This does not modify the size of the storage //! This is a no-op for trivial types - constexpr void unsafe_destroy_all() noexcept + void unsafe_destroy_all() noexcept { } private: - T m_data[Capacity]{}; + T m_data[Capacity]; size_type m_size{}; }; @@ -245,7 +245,7 @@ namespace AZStd::Internal using reference = T&; using const_reference = const T&; - constexpr fixed_non_trivial_storage() = default; + fixed_non_trivial_storage() = default; ~fixed_non_trivial_storage() noexcept { @@ -253,7 +253,7 @@ namespace AZStd::Internal } template >> - constexpr fixed_non_trivial_storage(AZStd::initializer_list ilist) noexcept(noexcept(emplace_back(AZStd::declval()))) + fixed_non_trivial_storage(AZStd::initializer_list ilist) noexcept(noexcept(emplace_back(AZStd::declval()))) { AZSTD_CONTAINER_ASSERT(ilist.size() <= capacity(), "Initializer list cannot be larger than storage capacity"); for (const U& element : ilist) @@ -272,7 +272,7 @@ namespace AZStd::Internal } //! Number of elements currently stored. - constexpr size_type size() const noexcept + size_type size() const noexcept { return m_size; } @@ -282,12 +282,12 @@ namespace AZStd::Internal return Capacity; } //! Is the storage empty? - constexpr bool empty() const noexcept + bool empty() const noexcept { return size() == 0; } //! Is the storage full? - constexpr bool full() const noexcept + bool full() const noexcept { return size() == capacity(); } @@ -325,7 +325,7 @@ namespace AZStd::Internal //! removing elements (unsafe). //! //! Updates the size of the container while checking that the new size is less than capacity - constexpr void resize_no_construct(size_t new_size) noexcept + void resize_no_construct(size_t new_size) noexcept { AZSTD_CONTAINER_ASSERT(new_size <= capacity(), "New size cannot be larger than capacity"); m_size = aznumeric_cast(new_size); @@ -402,23 +402,23 @@ namespace AZStd ////////////////////////////////////////////////////////////////////////// // 23.2.4.1 construct/copy/destroy - constexpr fixed_vector() = default; + fixed_vector() = default; - constexpr explicit fixed_vector(size_type numElements, const_reference value = value_type()) + explicit fixed_vector(size_type numElements, const_reference value = value_type()) { resize_no_construct(numElements); AZStd::uninitialized_fill_n(data(), numElements, value); } template >> - constexpr fixed_vector(InputIt first, InputIt last) + fixed_vector(InputIt first, InputIt last) { resize_no_construct(AZStd::distance(first, last)); AZStd::uninitialized_copy(first, last, data()); } - constexpr fixed_vector(const fixed_vector& rhs) + fixed_vector(const fixed_vector& rhs) { resize_no_construct(rhs.size()); AZStd::uninitialized_copy(rhs.data(), rhs.data() + rhs.size(), data()); @@ -428,7 +428,7 @@ namespace AZStd // It performs an AZStd::move on each of the fixed_vector elements instead // of swapping pointers to the allocted memory address // as it is unable to perform that operations due to the storage being baked into the container - constexpr fixed_vector(fixed_vector&& rhs) + fixed_vector(fixed_vector&& rhs) { resize_no_construct(rhs.size()); AZStd::uninitialized_move(rhs.data(), rhs.data() + rhs.size(), data()); @@ -440,7 +440,7 @@ namespace AZStd // into a fixed_vector given that the type in question isn't the same type as this fixed_vector type template && !AZStd::is_convertible_v>> - constexpr fixed_vector(VectorContainer&& rhs) + fixed_vector(VectorContainer&& rhs) { constexpr bool is_const_or_lvalue_reference = AZStd::is_lvalue_reference_v || AZStd::is_const_v; @@ -459,12 +459,12 @@ namespace AZStd } } - constexpr fixed_vector(AZStd::initializer_list ilist) + fixed_vector(AZStd::initializer_list ilist) : base_type(ilist) { } - constexpr fixed_vector& operator=(const fixed_vector& rhs) + fixed_vector& operator=(const fixed_vector& rhs) { if (this == &rhs) { @@ -475,7 +475,7 @@ namespace AZStd return assign_helper(rhs); } - constexpr fixed_vector& operator=(fixed_vector&& rhs) + fixed_vector& operator=(fixed_vector&& rhs) { if (this == &rhs) { @@ -487,23 +487,23 @@ namespace AZStd } template - constexpr AZStd::enable_if_t, fixed_vector>, fixed_vector>& operator=(VectorContainer&& rhs) + AZStd::enable_if_t, fixed_vector>, fixed_vector>& operator=(VectorContainer&& rhs) { return assign_helper(AZStd::forward(rhs)); } - constexpr iterator begin() { return iterator(data()); } - constexpr const_iterator begin() const { return const_iterator(data()); } - constexpr const_iterator cbegin() const { return const_iterator(data()); } - constexpr iterator end() { return iterator(data() + size()); } - constexpr const_iterator end() const { return const_iterator(data() + size()); } - constexpr const_iterator cend() const { return const_iterator(data() + size()); } - constexpr reverse_iterator rbegin() { return reverse_iterator(end()); } - constexpr const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - constexpr const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } - constexpr reverse_iterator rend() { return reverse_iterator(begin()); } - constexpr const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } - constexpr const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } + iterator begin() { return iterator(data()); } + const_iterator begin() const { return const_iterator(data()); } + const_iterator cbegin() const { return const_iterator(data()); } + iterator end() { return iterator(data() + size()); } + const_iterator end() const { return const_iterator(data() + size()); } + const_iterator cend() const { return const_iterator(data() + size()); } + reverse_iterator rbegin() { return reverse_iterator(end()); } + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } + reverse_iterator rend() { return reverse_iterator(begin()); } + const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } // bring in fixed_vector_storage functions into scope using base_type::data; @@ -514,7 +514,7 @@ namespace AZStd // extension method using base_type::resize_no_construct; - constexpr size_type size() const noexcept + size_type size() const noexcept { return base_type::size(); } @@ -527,12 +527,12 @@ namespace AZStd return base_type::max_size(); } - constexpr void resize(size_type newSize) + void resize(size_type newSize) { return resize(newSize, value_type{}); } - constexpr void resize(size_type newSize, const_reference value) + void resize(size_type newSize, const_reference value) { size_type dataSize = size(); if (dataSize < newSize) @@ -547,7 +547,7 @@ namespace AZStd // Removes unused capacity - For fixed_vector this only asserts // that the supplied capacity is not longer than the fixed_vector capacity - constexpr void reserve(size_type newCapacity) + void reserve(size_type newCapacity) { // No-op - Implemented to provide consistent std::vector AZSTD_CONTAINER_ASSERT(newCapacity <= capacity(), @@ -556,79 +556,79 @@ namespace AZStd } // Removes unused capacity - For fixed_vector this does nothing - constexpr void shrink_to_fit() + void shrink_to_fit() { // No-op - Implemented to provide consistent std::vector } - constexpr reference at(size_type position) + reference at(size_type position) { AZSTD_CONTAINER_ASSERT(position < size(), "AZStd::fixed_vector<>::at - position is out of range"); return *(data() + position); } - constexpr const_reference at(size_type position) const + const_reference at(size_type position) const { AZSTD_CONTAINER_ASSERT(position < size(), "AZStd::fixed_vector<>::at - position is out of range"); return *(data() + position); } - constexpr reference operator[](size_type position) + reference operator[](size_type position) { AZSTD_CONTAINER_ASSERT(position < size(), "AZStd::fixed_vector<>::at - position is out of range"); return *(data() + position); } - constexpr const_reference operator[](size_type position) const + const_reference operator[](size_type position) const { AZSTD_CONTAINER_ASSERT(position < size(), "AZStd::fixed_vector<>::at - position is out of range"); return *(data() + position); } - constexpr reference front() + reference front() { AZSTD_CONTAINER_ASSERT(!empty(), "AZStd::fixed_vector<>::front - container is empty!"); return *data(); } - constexpr const_reference front() const + const_reference front() const { AZSTD_CONTAINER_ASSERT(!empty(), "AZStd::fixed_vector<>::front - container is empty!"); return *data(); } - constexpr reference back() + reference back() { AZSTD_CONTAINER_ASSERT(!empty(), "AZStd::fixed_vector<>::back - container is empty!"); return *(data() + size() - 1); } - constexpr const_reference back() const + const_reference back() const { AZSTD_CONTAINER_ASSERT(!empty(), "AZStd::fixed_vector<>::back - container is empty!"); return *(data() + size() - 1); } - constexpr void push_back(const_reference value) + void push_back(const_reference value) { emplace_back(value); } - constexpr void assign(size_type numElements, const_reference value) + void assign(size_type numElements, const_reference value) { clear(); insert(end(), numElements, value); } template >> - constexpr void assign(InputIt first, InputIt last) + void assign(InputIt first, InputIt last) { clear(); insert(end(), first, last); } - constexpr void assign(AZStd::initializer_list ilist) + void assign(AZStd::initializer_list ilist) { assign(ilist.begin(), ilist.end()); } template >> - constexpr iterator emplace(const_iterator insertPos, Args&&... args) + iterator emplace(const_iterator insertPos, Args&&... args) { AZSTD_CONTAINER_ASSERT(!full(), "Cannot emplace on a full fixed_vector"); AZSTD_CONTAINER_ASSERT(insertPos >= cbegin() && insertPos <= cend(), "insert position must be in range of container"); @@ -645,18 +645,18 @@ namespace AZStd AZStd::construct_at(insertPosPtr, AZStd::forward(args)...); return iterator(insertPosPtr); } - constexpr iterator insert(const_iterator insertPos, const_reference value) + iterator insert(const_iterator insertPos, const_reference value) { AZSTD_CONTAINER_ASSERT(insertPos >= cbegin() && insertPos <= cend(), "insert position must be in range of container"); return emplace(insertPos, value); } - constexpr iterator insert(const_iterator insertPos, value_type&& value) + iterator insert(const_iterator insertPos, value_type&& value) { AZSTD_CONTAINER_ASSERT(insertPos >= cbegin() && insertPos <= cend(), "insert position must be in range of container"); return emplace(insertPos, AZStd::move(value)); } - constexpr void insert(const_iterator insertPos, size_type numElements, const_reference value) + void insert(const_iterator insertPos, size_type numElements, const_reference value) { if (numElements == 0) { @@ -708,24 +708,24 @@ namespace AZStd } template>> - constexpr void insert(const_iterator insertPos, InputIt first, InputIt last) + void insert(const_iterator insertPos, InputIt first, InputIt last) { // specialize for iterator categories. AZSTD_CONTAINER_ASSERT(insertPos >= cbegin() && insertPos <= cend(), "insert position must be in range of container"); insert_iter(insertPos, first, last, typename iterator_traits::iterator_category()); }; - constexpr void insert(const_iterator insertPos, AZStd::initializer_list ilist) + void insert(const_iterator insertPos, AZStd::initializer_list ilist) { insert(insertPos, ilist.begin(), ilist.end()); } - constexpr iterator erase(const_iterator elementIter) + iterator erase(const_iterator elementIter) { return erase(elementIter, elementIter + 1); } - constexpr iterator erase(const_iterator first, const_iterator last) + iterator erase(const_iterator first, const_iterator last) { AZSTD_CONTAINER_ASSERT(first >= cbegin() && last <= cend(), "erase iterator must be inside the range of fixed_vector container"); iterator dataStart = begin(); @@ -741,12 +741,12 @@ namespace AZStd return dataStart + offset; } - constexpr void clear() + void clear() { base_type::unsafe_destroy_all(); resize_no_construct(0); } - constexpr void swap(fixed_vector& rhs) + void swap(fixed_vector& rhs) { // Fixed containers cannot swap pointers, they need to do full copies. // The strategy is to extend the smaller fixed_vector to be the size @@ -776,12 +776,12 @@ namespace AZStd } // Validate container status. - constexpr bool validate() const + bool validate() const { return size() <= max_size(); } // Validate iterator. - constexpr int validate_iterator(const_iterator iter) const + int validate_iterator(const_iterator iter) const { const_pointer start = data(); const_pointer end = data() + size(); @@ -799,19 +799,19 @@ namespace AZStd } // pushes back an empty without a provided instance. - constexpr void push_back() + void push_back() { emplace_back(); } - constexpr void leak_and_reset() + void leak_and_reset() { resize_no_construct(0); } private: template - constexpr fixed_vector& assign_helper(VectorContainer&& rhs) + fixed_vector& assign_helper(VectorContainer&& rhs) { constexpr bool is_const_or_lvalue_reference = AZStd::is_lvalue_reference_v || AZStd::is_const_v; @@ -872,7 +872,7 @@ namespace AZStd } template - constexpr void insert_iter(const_iterator insertPos, Iterator first, Iterator last, const forward_iterator_tag&) + void insert_iter(const_iterator insertPos, Iterator first, Iterator last, const forward_iterator_tag&) { size_type numElements = AZStd::distance(first, last); if (numElements == 0) @@ -923,7 +923,7 @@ namespace AZStd } template - constexpr void insert_iter(const_iterator insertPos, Iterator first, Iterator last, const input_iterator_tag&) + void insert_iter(const_iterator insertPos, Iterator first, Iterator last, const input_iterator_tag&) { iterator dataStart = data(); size_type offset = AZStd::distance(dataStart, insertPos); @@ -974,4 +974,22 @@ namespace AZStd { return !operator<(a, b); } + + // C++20 erase free functions + template + constexpr decltype(auto) erase(fixed_vector& container, const U& value) + { + auto iter = AZStd::remove(container.begin(), container.end(), value); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } + template + constexpr decltype(auto) erase_if(fixed_vector& container, Predicate predicate) + { + auto iter = AZStd::remove_if(container.begin(), container.end(), predicate); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } } diff --git a/Code/Framework/AzCore/AzCore/std/containers/forward_list.h b/Code/Framework/AzCore/AzCore/std/containers/forward_list.h index 407d65ffa9..9d7eebbe0e 100644 --- a/Code/Framework/AzCore/AzCore/std/containers/forward_list.h +++ b/Code/Framework/AzCore/AzCore/std/containers/forward_list.h @@ -1275,6 +1275,11 @@ namespace AZStd return !(left == right); } + template + decltype(auto) erase(forward_list& container, const U& value) + { + return container.remove(value); + } template decltype(auto) erase_if(forward_list& container, Predicate predicate) { diff --git a/Code/Framework/AzCore/AzCore/std/containers/list.h b/Code/Framework/AzCore/AzCore/std/containers/list.h index 60d2977749..51bc62f444 100644 --- a/Code/Framework/AzCore/AzCore/std/containers/list.h +++ b/Code/Framework/AzCore/AzCore/std/containers/list.h @@ -1340,6 +1340,11 @@ namespace AZStd return !(left == right); } + template + decltype(auto) erase(list& container, const U& value) + { + return container.remove(value); + } template decltype(auto) erase_if(list& container, Predicate predicate) { diff --git a/Code/Framework/AzCore/AzCore/std/containers/vector.h b/Code/Framework/AzCore/AzCore/std/containers/vector.h index 48de12a5b4..255e1c4de4 100644 --- a/Code/Framework/AzCore/AzCore/std/containers/vector.h +++ b/Code/Framework/AzCore/AzCore/std/containers/vector.h @@ -1387,6 +1387,14 @@ namespace AZStd } //#pragma endregion + template + decltype(auto) erase(vector& container, const U& value) + { + auto iter = AZStd::remove(container.begin(), container.end(), value); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } template decltype(auto) erase_if(vector& container, Predicate predicate) { diff --git a/Code/Framework/AzCore/AzCore/std/string/fixed_string.h b/Code/Framework/AzCore/AzCore/std/string/fixed_string.h index b68f21784b..a8e0f96988 100644 --- a/Code/Framework/AzCore/AzCore/std/string/fixed_string.h +++ b/Code/Framework/AzCore/AzCore/std/string/fixed_string.h @@ -465,6 +465,15 @@ namespace AZStd template constexpr bool operator>=(const Element* lhs, const basic_fixed_string& rhs); + // C++20 erase helpers + template + constexpr auto erase(basic_fixed_string& container, const U& element) + -> typename basic_fixed_string::size_type; + + template + constexpr auto erase_if(basic_fixed_string& container, Predicate predicate) + -> typename basic_fixed_string::size_type; + template struct hash; diff --git a/Code/Framework/AzCore/AzCore/std/string/fixed_string.inl b/Code/Framework/AzCore/AzCore/std/string/fixed_string.inl index 9a7e38aca0..8b973d3b2c 100644 --- a/Code/Framework/AzCore/AzCore/std/string/fixed_string.inl +++ b/Code/Framework/AzCore/AzCore/std/string/fixed_string.inl @@ -11,7 +11,7 @@ #include #include -#include +#include #include @@ -1680,6 +1680,26 @@ namespace AZStd return !operator<(lhs, rhs); } + template + inline constexpr auto erase(basic_fixed_string& container, const U& element) + -> typename basic_fixed_string::size_type + { + auto iter = AZStd::remove(container.begin(), container.end(), element); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } + + template + inline constexpr auto erase_if(basic_fixed_string& container, Predicate predicate) + -> typename basic_fixed_string::size_type + { + auto iter = AZStd::remove_if(container.begin(), container.end(), predicate); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } + template struct hash> { diff --git a/Code/Framework/AzCore/AzCore/std/string/memorytoascii.cpp b/Code/Framework/AzCore/AzCore/std/string/memorytoascii.cpp index 8f2d5ddf25..21f3a1c5f4 100644 --- a/Code/Framework/AzCore/AzCore/std/string/memorytoascii.cpp +++ b/Code/Framework/AzCore/AzCore/std/string/memorytoascii.cpp @@ -8,179 +8,176 @@ #include -namespace AZStd +namespace AZStd::MemoryToASCII { - namespace MemoryToASCII + AZStd::string ToString(const void* memoryAddrs, AZStd::size_t dataSize, AZStd::size_t maxShowSize, AZStd::size_t dataWidth/*=16*/, Options format/*=Options::Default*/) { - AZStd::string ToString(const void* memoryAddrs, AZStd::size_t dataSize, AZStd::size_t maxShowSize, AZStd::size_t dataWidth/*=16*/, Options format/*=Options::Default*/) + AZStd::string output; + + if ((memoryAddrs != nullptr) && (dataSize > 0)) { - AZStd::string output; + const AZ::u8 *data = reinterpret_cast(memoryAddrs); - if ((memoryAddrs != nullptr) && (dataSize > 0)) + if (static_cast(format) != 0) { - const AZ::u8 *data = reinterpret_cast(memoryAddrs); + output.reserve(8162); - if (static_cast(format) != 0) + bool showHeader = static_cast(format) & static_cast(Options::Header) ? true : false; + bool showOffset = static_cast(format) & static_cast(Options::Offset) ? true : false; + bool showBinary = static_cast(format) & static_cast(Options::Binary) ? true : false; + bool showASCII = static_cast(format) & static_cast(Options::ASCII) ? true : false; + bool showInfo = static_cast(format) & static_cast(Options::Info) ? true : false; + + // Because of the auto formatting for the headers, the min width is 3 + if (dataWidth < 3) { - output.reserve(8162); + dataWidth = 3; + } - bool showHeader = static_cast(format) & static_cast(Options::Header) ? true : false; - bool showOffset = static_cast(format) & static_cast(Options::Offset) ? true : false; - bool showBinary = static_cast(format) & static_cast(Options::Binary) ? true : false; - bool showASCII = static_cast(format) & static_cast(Options::ASCII) ? true : false; - bool showInfo = static_cast(format) & static_cast(Options::Info) ? true : false; + if (showHeader) + { + AZStd::string line1; + AZStd::string line2; + line1.reserve(1024); + line2.reserve(1024); - // Because of the auto formatting for the headers, the min width is 3 - if (dataWidth < 3) + if (showOffset) { - dataWidth = 3; + line1 += "Offset"; + line2 += "------"; + + if (showBinary || showASCII) + { + line1 += " "; + line2 += " "; + } } - if (showHeader) + if (showBinary) { - AZStd::string line1; - AZStd::string line2; - line1.reserve(1024); - line2.reserve(1024); + static const char *kHeaderName = "Data"; + static AZStd::size_t kHeaderNameSize = 4; - if (showOffset) + AZStd::size_t lineLength = (dataWidth * 3) - 1; + AZStd::size_t numPreSpaces = (lineLength - kHeaderNameSize) / 2; + AZStd::size_t numPostSpaces = lineLength - numPreSpaces - kHeaderNameSize; + + line1 += AZStd::string(numPreSpaces, ' ') + kHeaderName + AZStd::string(numPostSpaces, ' '); + //line2 += AZStd::string(lineLength, '-'); + for(size_t i=0; i 0) { - line1 += " "; - line2 += " "; - } - } - - if (showBinary) - { - static const char *kHeaderName = "Data"; - static AZStd::size_t kHeaderNameSize = 4; - - AZStd::size_t lineLength = (dataWidth * 3) - 1; - AZStd::size_t numPreSpaces = (lineLength - kHeaderNameSize) / 2; - AZStd::size_t numPostSpaces = lineLength - numPreSpaces - kHeaderNameSize; - - line1 += AZStd::string(numPreSpaces, ' ') + kHeaderName + AZStd::string(numPostSpaces, ' '); - //line2 += AZStd::string(lineLength, '-'); - for(size_t i=0; i 0) - { - line2 += "-"; - } - - line2 += AZStd::string::format("%02zx", i); + line2 += "-"; } - if (showASCII) - { - line1 += " "; - line2 += " "; - } + line2 += AZStd::string::format("%02zx", i); } if (showASCII) { - static const char *kHeaderName = "ASCII"; - static AZStd::size_t kHeaderNameSize = 5; - - AZStd::size_t numPreSpaces = (dataWidth - kHeaderNameSize) / 2; - AZStd::size_t numPostSpaces = dataWidth - numPreSpaces - kHeaderNameSize; - - line1 += AZStd::string(numPreSpaces, ' ') + kHeaderName + AZStd::string(numPostSpaces, ' '); - line2 += AZStd::string(dataWidth, '-'); + line1 += " "; + line2 += " "; } - - if (showInfo) - { - output += AZStd::string::format("Address: 0x%p Data Size:%zu Max Size:%zu\n", data, dataSize, maxShowSize); - } - - output += line1 + "\n"; - output += line2 + "\n"; } - AZStd::size_t offset = 0; - AZStd::size_t maxSize = dataSize > maxShowSize ? maxShowSize : dataSize; - - while (offset < maxSize) + if (showASCII) { - if (showOffset) - { - output += AZStd::string::format("%06zx", offset); + static const char *kHeaderName = "ASCII"; + static AZStd::size_t kHeaderNameSize = 5; - if (showBinary || showASCII) + AZStd::size_t numPreSpaces = (dataWidth - kHeaderNameSize) / 2; + AZStd::size_t numPostSpaces = dataWidth - numPreSpaces - kHeaderNameSize; + + line1 += AZStd::string(numPreSpaces, ' ') + kHeaderName + AZStd::string(numPostSpaces, ' '); + line2 += AZStd::string(dataWidth, '-'); + } + + if (showInfo) + { + output += AZStd::string::format("Address: 0x%p Data Size:%zu Max Size:%zu\n", data, dataSize, maxShowSize); + } + + output += line1 + "\n"; + output += line2 + "\n"; + } + + AZStd::size_t offset = 0; + AZStd::size_t maxSize = dataSize > maxShowSize ? maxShowSize : dataSize; + + while (offset < maxSize) + { + if (showOffset) + { + output += AZStd::string::format("%06zx", offset); + + if (showBinary || showASCII) + { + output += " "; + } + } + + if (showBinary) + { + AZStd::string binLine; + binLine.reserve((dataWidth * 3) * 2); + + for (AZStd::size_t index = 0; index < dataWidth; index++) + { + if (!binLine.empty()) { - output += " "; + binLine += " "; + } + + if ((offset + index) < maxSize) + { + binLine += AZStd::string::format("%02x", data[offset + index]); + } + else + { + binLine += " "; } } - if (showBinary) - { - AZStd::string binLine; - binLine.reserve((dataWidth * 3) * 2); - - for (AZStd::size_t index = 0; index < dataWidth; index++) - { - if (!binLine.empty()) - { - binLine += " "; - } - - if ((offset + index) < maxSize) - { - binLine += AZStd::string::format("%02x", data[offset + index]); - } - else - { - binLine += " "; - } - } - - output += binLine; - - if (showASCII) - { - output += " "; - } - } + output += binLine; if (showASCII) { - AZStd::string asciiLine; - asciiLine.reserve(dataWidth * 2); + output += " "; + } + } - for (AZStd::size_t index = 0; index < dataWidth; index++) + if (showASCII) + { + AZStd::string asciiLine; + asciiLine.reserve(dataWidth * 2); + + for (AZStd::size_t index = 0; index < dataWidth; index++) + { + if ((offset + index) > maxSize) { - if ((offset + index) > maxSize) - { - break; - } - else - { - char value = static_cast(data[offset + index]); - - if ((value < 32) || (value > 127)) - value = ' '; - - asciiLine += value; - } + break; } + else + { + char value = static_cast(data[offset + index]); - output += asciiLine; + if ((value < 32) || (value > 127)) + value = ' '; + + asciiLine += value; + } } - output += "\n"; - offset += dataWidth; + output += asciiLine; } + + output += "\n"; + offset += dataWidth; } } - - return output; } - } // namespace MemoryToASCII -} // namespace AZStd + + return output; + } +} // namespace AZStd::MemoryToASCII diff --git a/Code/Framework/AzCore/AzCore/std/string/string.h b/Code/Framework/AzCore/AzCore/std/string/string.h index 9ef7ea3086..e107c4657d 100644 --- a/Code/Framework/AzCore/AzCore/std/string/string.h +++ b/Code/Framework/AzCore/AzCore/std/string/string.h @@ -1955,6 +1955,16 @@ namespace AZStd { return basic_string(lhs).compare(rhs) >= 0; } + + template + decltype(auto) erase(basic_string& container, const U& element) + { + auto iter = AZStd::remove(container.begin(), container.end(), element); + auto removedCount = AZStd::distance(iter, container.end()); + container.erase(iter, container.end()); + return removedCount; + } + template decltype(auto) erase_if(basic_string& container, Predicate predicate) { diff --git a/Code/Framework/AzCore/Platform/Android/AzCore/AzCore_Traits_Android.h b/Code/Framework/AzCore/Platform/Android/AzCore/AzCore_Traits_Android.h index 495c8d5f2c..e05e000a4e 100644 --- a/Code/Framework/AzCore/Platform/Android/AzCore/AzCore_Traits_Android.h +++ b/Code/Framework/AzCore/Platform/Android/AzCore/AzCore_Traits_Android.h @@ -75,7 +75,6 @@ #define AZ_TRAIT_HEAPSCHEMA_COMPILE_MALLINFO 0 #define AZ_TRAIT_IS_ABS_PATH_IF_COLON_FOUND_ANYWHERE 0 #define AZ_TRAIT_JSON_CLANG_IGNORE_UNKNOWN_WARNING 0 -#define AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS 0 #define AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING_INTERVAL_MS 0 diff --git a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Debug/Trace_UnixLike.cpp b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Debug/Trace_UnixLike.cpp index f4d7db802b..92a80b0d9a 100644 --- a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Debug/Trace_UnixLike.cpp +++ b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Debug/Trace_UnixLike.cpp @@ -13,73 +13,67 @@ #include #include -namespace AZ +namespace AZ::Debug::Platform { - namespace Debug - { - namespace Platform - { #if defined(AZ_ENABLE_DEBUG_TOOLS) - bool performDebuggerDetection() + bool performDebuggerDetection() + { + AZ::IO::SystemFile processStatusFile; + if (!processStatusFile.Open("/proc/self/status", AZ::IO::SystemFile::SF_OPEN_READ_ONLY)) + { + return false; + } + + char buffer[4096]; + AZ::IO::SystemFile::SizeType numRead = processStatusFile.Read(sizeof(buffer), buffer); + + const AZStd::string_view processStatusView(buffer, buffer + numRead); + constexpr AZStd::string_view tracerPidString = "TracerPid:"; + const size_t tracerPidOffset = processStatusView.find(tracerPidString); + if (tracerPidOffset == AZStd::string_view::npos) + { + return false; + } + for (size_t i = tracerPidOffset + tracerPidString.length(); i < numRead; ++i) + { + if (!::isspace(processStatusView[i])) { - AZ::IO::SystemFile processStatusFile; - if (!processStatusFile.Open("/proc/self/status", AZ::IO::SystemFile::SF_OPEN_READ_ONLY)) - { - return false; - } - - char buffer[4096]; - AZ::IO::SystemFile::SizeType numRead = processStatusFile.Read(sizeof(buffer), buffer); - - const AZStd::string_view processStatusView(buffer, buffer + numRead); - constexpr AZStd::string_view tracerPidString = "TracerPid:"; - const size_t tracerPidOffset = processStatusView.find(tracerPidString); - if (tracerPidOffset == AZStd::string_view::npos) - { - return false; - } - for (size_t i = tracerPidOffset + tracerPidString.length(); i < numRead; ++i) - { - if (!::isspace(processStatusView[i])) - { - return processStatusView[i] != '0'; - } - } - return false; - } - - bool IsDebuggerPresent() - { - static bool s_detectionPerformed = false; - static bool s_debuggerDetected = false; - if (!s_detectionPerformed) - { - s_debuggerDetected = performDebuggerDetection(); - s_detectionPerformed = true; - } - return s_debuggerDetected; - } - - bool AttachDebugger() - { - // Not supported yet - AZ_Assert(false, "AttachDebugger() is not supported for Unix platform yet"); - return false; - } - - void HandleExceptions(bool) - {} - - void DebugBreak() - { - raise(SIGINT); - } -#endif // AZ_ENABLE_DEBUG_TOOLS - - void Terminate(int exitCode) - { - _exit(exitCode); + return processStatusView[i] != '0'; } } + return false; } -} + + bool IsDebuggerPresent() + { + static bool s_detectionPerformed = false; + static bool s_debuggerDetected = false; + if (!s_detectionPerformed) + { + s_debuggerDetected = performDebuggerDetection(); + s_detectionPerformed = true; + } + return s_debuggerDetected; + } + + bool AttachDebugger() + { + // Not supported yet + AZ_Assert(false, "AttachDebugger() is not supported for Unix platform yet"); + return false; + } + + void HandleExceptions(bool) + {} + + void DebugBreak() + { + raise(SIGINT); + } +#endif // AZ_ENABLE_DEBUG_TOOLS + + void Terminate(int exitCode) + { + _exit(exitCode); + } +} // namespace AZ::Debug::Platform diff --git a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/IO/Internal/SystemFileUtils_UnixLike.cpp b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/IO/Internal/SystemFileUtils_UnixLike.cpp index 07614ad56b..4c605f6154 100644 --- a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/IO/Internal/SystemFileUtils_UnixLike.cpp +++ b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/IO/Internal/SystemFileUtils_UnixLike.cpp @@ -8,82 +8,76 @@ #include "SystemFileUtils_UnixLike.h" -namespace AZ +namespace AZ::IO::Internal { - namespace IO + bool FormatAndPeelOffWildCardExtension(const char* sourcePath, char* filePath, size_t filePathSize, char* extensionPath, size_t extensionSize, bool keepWildcard) { - namespace Internal + if (sourcePath == nullptr || filePath == nullptr || extensionPath == nullptr || filePathSize == 0 || extensionSize == 0) { - bool FormatAndPeelOffWildCardExtension(const char* sourcePath, char* filePath, size_t filePathSize, char* extensionPath, size_t extensionSize, bool keepWildcard) + AZ_Error("AZ::IO::Internal", false, "FormatAndPeelOffWildCardExtension: One or more parameters was invalid."); + return false; + } + const char* pSrcPath = sourcePath; + char* pDestPath = filePath; + size_t destinationSize = filePathSize; + unsigned numFileChars = 0; + unsigned numExtensionChars = 0; + unsigned* pNumDestChars = &numFileChars; + bool bIsWildcardExtension = false; + while (*pSrcPath) + { + char srcChar = *pSrcPath++; + + // Skip '*' and '.' + if ((!bIsWildcardExtension && srcChar != '*') || (bIsWildcardExtension && srcChar != '.' && (keepWildcard || srcChar != '*'))) { - if (sourcePath == nullptr || filePath == nullptr || extensionPath == nullptr || filePathSize == 0 || extensionSize == 0) + unsigned numChars = *pNumDestChars; + pDestPath[numChars++] = srcChar; + *pNumDestChars = numChars; + + --destinationSize; + if (destinationSize == 0) { - AZ_Error("AZ::IO::Internal", false, "FormatAndPeelOffWildCardExtension: One or more parameters was invalid."); + AZ_Error( + "AZ::IO::Internal", + false, + "Error splitting sourcePath '%s' into filePath and extension, %s length is larger than storage size %d.", + sourcePath, + bIsWildcardExtension ? "extensionPath" : "filePath", + bIsWildcardExtension ? extensionSize : filePathSize); return false; } - const char* pSrcPath = sourcePath; - char* pDestPath = filePath; - size_t destinationSize = filePathSize; - unsigned numFileChars = 0; - unsigned numExtensionChars = 0; - unsigned* pNumDestChars = &numFileChars; - bool bIsWildcardExtension = false; - while (*pSrcPath) + } + // Wild-card extension is separate + if (srcChar == '*') + { + bIsWildcardExtension = true; + pDestPath = extensionPath; + destinationSize = extensionSize; + pNumDestChars = &numExtensionChars; + if (keepWildcard) { - char srcChar = *pSrcPath++; + unsigned numChars = *pNumDestChars; + pDestPath[numChars++] = srcChar; + *pNumDestChars = numChars; - // Skip '*' and '.' - if ((!bIsWildcardExtension && srcChar != '*') || (bIsWildcardExtension && srcChar != '.' && (keepWildcard || srcChar != '*'))) + --destinationSize; + if (destinationSize == 0) { - unsigned numChars = *pNumDestChars; - pDestPath[numChars++] = srcChar; - *pNumDestChars = numChars; - - --destinationSize; - if (destinationSize == 0) - { - AZ_Error( - "AZ::IO::Internal", - false, - "Error splitting sourcePath '%s' into filePath and extension, %s length is larger than storage size %d.", - sourcePath, - bIsWildcardExtension ? "extensionPath" : "filePath", - bIsWildcardExtension ? extensionSize : filePathSize); - return false; - } - } - // Wild-card extension is separate - if (srcChar == '*') - { - bIsWildcardExtension = true; - pDestPath = extensionPath; - destinationSize = extensionSize; - pNumDestChars = &numExtensionChars; - if (keepWildcard) - { - unsigned numChars = *pNumDestChars; - pDestPath[numChars++] = srcChar; - *pNumDestChars = numChars; - - --destinationSize; - if (destinationSize == 0) - { - AZ_Error( - "AZ::IO::Internal", - false, - "Error splitting sourcePath '%s' into filePath and extension, extensionPath length is larger than storage size %d.", - sourcePath, - extensionSize); - return false; - } - } + AZ_Error( + "AZ::IO::Internal", + false, + "Error splitting sourcePath '%s' into filePath and extension, extensionPath length is larger than storage size %d.", + sourcePath, + extensionSize); + return false; } } - // Close strings - filePath[numFileChars] = 0; - extensionPath[numExtensionChars] = 0; - return true; } } + // Close strings + filePath[numFileChars] = 0; + extensionPath[numExtensionChars] = 0; + return true; } -} +} // namespace AZ::IO::Internal diff --git a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Platform_UnixLike.cpp b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Platform_UnixLike.cpp index bdc2e753be..4a316bcde6 100644 --- a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Platform_UnixLike.cpp +++ b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Platform_UnixLike.cpp @@ -14,31 +14,28 @@ #include #include -namespace AZ +namespace AZ::Platform { - namespace Platform + ProcessId GetCurrentProcessId() { - ProcessId GetCurrentProcessId() - { - return static_cast(::getpid()); - } + return static_cast(::getpid()); + } - MachineId GetLocalMachineId() + MachineId GetLocalMachineId() + { + if (s_machineId == 0) { + // In specialized server situations, SetLocalMachineId() should be used, with whatever criteria works best in that environment + // A proper implementation for each supported system will be needed instead of this temporary measure to avoid collision in the small scale. + // On a larger scale, the odds of two people getting in here at the same millisecond will go up drastically, and we'll have the same issue again, + // though far less reproducible, for duplicated EntityId's across a network. + s_machineId = static_cast(AZStd::GetTimeUTCMilliSecond() & 0xffffffff); if (s_machineId == 0) { - // In specialized server situations, SetLocalMachineId() should be used, with whatever criteria works best in that environment - // A proper implementation for each supported system will be needed instead of this temporary measure to avoid collision in the small scale. - // On a larger scale, the odds of two people getting in here at the same millisecond will go up drastically, and we'll have the same issue again, - // though far less reproducible, for duplicated EntityId's across a network. - s_machineId = static_cast(AZStd::GetTimeUTCMilliSecond() & 0xffffffff); - if (s_machineId == 0) - { - s_machineId = 1; - AZ_Warning("System", false, "0 machine ID is reserved!"); - } + s_machineId = 1; + AZ_Warning("System", false, "0 machine ID is reserved!"); } - return s_machineId; } - } // namespace Platform -} + return s_machineId; + } +} // namespace AZ::Platform diff --git a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Socket/AzSocket_UnixLike.cpp b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Socket/AzSocket_UnixLike.cpp index 5b35b13d8b..555d70f1e6 100644 --- a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Socket/AzSocket_UnixLike.cpp +++ b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Socket/AzSocket_UnixLike.cpp @@ -18,365 +18,362 @@ #define INVALID_SOCKET (-1) #define closesocket(_s) close(_s) #define GetInternalSocketError errno -typedef int SOCKET; -typedef AZ::u32 AZSOCKLEN; +using SOCKET = int; +using AZSOCKLEN = AZ::u32; -namespace AZ +namespace AZ::AzSock { - namespace AzSock + AZ::s32 TranslateOSError(AZ::s32 oserror) { - AZ::s32 TranslateOSError(AZ::s32 oserror) - { - AZ::s32 error; + AZ::s32 error; #define TRANSLATE(_from, _to) case (_from): error = static_cast(_to); break; - switch (oserror) - { - TRANSLATE(0, AzSockError::eASE_NO_ERROR); - TRANSLATE(EACCES, AzSockError::eASE_EACCES); - TRANSLATE(EADDRINUSE, AzSockError::eASE_EADDRINUSE); - TRANSLATE(EADDRNOTAVAIL, AzSockError::eASE_EADDRNOTAVAIL); - TRANSLATE(EAFNOSUPPORT, AzSockError::eASE_EAFNOSUPPORT); - TRANSLATE(EALREADY, AzSockError::eASE_EALREADY); - TRANSLATE(EBADF, AzSockError::eASE_EBADF); - TRANSLATE(ECONNABORTED, AzSockError::eASE_ECONNABORTED); - TRANSLATE(ECONNREFUSED, AzSockError::eASE_ECONNREFUSED); - TRANSLATE(ECONNRESET, AzSockError::eASE_ECONNRESET); - TRANSLATE(EFAULT, AzSockError::eASE_EFAULT); - TRANSLATE(EHOSTDOWN, AzSockError::eASE_EHOSTDOWN); - TRANSLATE(EINPROGRESS, AzSockError::eASE_EINPROGRESS); - TRANSLATE(EINTR, AzSockError::eASE_EINTR); - TRANSLATE(EINVAL, AzSockError::eASE_EINVAL); - TRANSLATE(EISCONN, AzSockError::eASE_EISCONN); - TRANSLATE(EMFILE, AzSockError::eASE_EMFILE); - TRANSLATE(EMSGSIZE, AzSockError::eASE_EMSGSIZE); - TRANSLATE(ENETUNREACH, AzSockError::eASE_ENETUNREACH); - TRANSLATE(ENOBUFS, AzSockError::eASE_ENOBUFS); - TRANSLATE(ENOPROTOOPT, AzSockError::eASE_ENOPROTOOPT); - TRANSLATE(ENOTCONN, AzSockError::eASE_ENOTCONN); - TRANSLATE(EOPNOTSUPP, AzSockError::eASE_EOPNOTSUPP); - TRANSLATE(EPROTONOSUPPORT, AzSockError::eASE_EAFNOSUPPORT); - TRANSLATE(ETIMEDOUT, AzSockError::eASE_ETIMEDOUT); - TRANSLATE(ETOOMANYREFS, AzSockError::eASE_ETOOMANYREFS); - TRANSLATE(EWOULDBLOCK, AzSockError::eASE_EWOULDBLOCK); + switch (oserror) + { + TRANSLATE(0, AzSockError::eASE_NO_ERROR); + TRANSLATE(EACCES, AzSockError::eASE_EACCES); + TRANSLATE(EADDRINUSE, AzSockError::eASE_EADDRINUSE); + TRANSLATE(EADDRNOTAVAIL, AzSockError::eASE_EADDRNOTAVAIL); + TRANSLATE(EAFNOSUPPORT, AzSockError::eASE_EAFNOSUPPORT); + TRANSLATE(EALREADY, AzSockError::eASE_EALREADY); + TRANSLATE(EBADF, AzSockError::eASE_EBADF); + TRANSLATE(ECONNABORTED, AzSockError::eASE_ECONNABORTED); + TRANSLATE(ECONNREFUSED, AzSockError::eASE_ECONNREFUSED); + TRANSLATE(ECONNRESET, AzSockError::eASE_ECONNRESET); + TRANSLATE(EFAULT, AzSockError::eASE_EFAULT); + TRANSLATE(EHOSTDOWN, AzSockError::eASE_EHOSTDOWN); + TRANSLATE(EINPROGRESS, AzSockError::eASE_EINPROGRESS); + TRANSLATE(EINTR, AzSockError::eASE_EINTR); + TRANSLATE(EINVAL, AzSockError::eASE_EINVAL); + TRANSLATE(EISCONN, AzSockError::eASE_EISCONN); + TRANSLATE(EMFILE, AzSockError::eASE_EMFILE); + TRANSLATE(EMSGSIZE, AzSockError::eASE_EMSGSIZE); + TRANSLATE(ENETUNREACH, AzSockError::eASE_ENETUNREACH); + TRANSLATE(ENOBUFS, AzSockError::eASE_ENOBUFS); + TRANSLATE(ENOPROTOOPT, AzSockError::eASE_ENOPROTOOPT); + TRANSLATE(ENOTCONN, AzSockError::eASE_ENOTCONN); + TRANSLATE(EOPNOTSUPP, AzSockError::eASE_EOPNOTSUPP); + TRANSLATE(EPROTONOSUPPORT, AzSockError::eASE_EAFNOSUPPORT); + TRANSLATE(ETIMEDOUT, AzSockError::eASE_ETIMEDOUT); + TRANSLATE(ETOOMANYREFS, AzSockError::eASE_ETOOMANYREFS); + TRANSLATE(EWOULDBLOCK, AzSockError::eASE_EWOULDBLOCK); - default: - AZ_TracePrintf("AzSock", "AzSocket could not translate OS error code %x, treating as miscellaneous.\n", oserror); - error = static_cast(AzSockError::eASE_MISC_ERROR); - break; - } + default: + AZ_TracePrintf("AzSock", "AzSocket could not translate OS error code %x, treating as miscellaneous.\n", oserror); + error = static_cast(AzSockError::eASE_MISC_ERROR); + break; + } #undef TRANSLATE - return error; - } + return error; + } - AZ::s32 TranslateSocketOption(AzSocketOption opt) - { - AZ::s32 value; + AZ::s32 TranslateSocketOption(AzSocketOption opt) + { + AZ::s32 value; #define TRANSLATE(_from, _to) case (_from): value = (_to); break; - switch (opt) - { - TRANSLATE(AzSocketOption::REUSEADDR, SO_REUSEADDR); - TRANSLATE(AzSocketOption::KEEPALIVE, SO_KEEPALIVE); - TRANSLATE(AzSocketOption::LINGER, SO_LINGER); + switch (opt) + { + TRANSLATE(AzSocketOption::REUSEADDR, SO_REUSEADDR); + TRANSLATE(AzSocketOption::KEEPALIVE, SO_KEEPALIVE); + TRANSLATE(AzSocketOption::LINGER, SO_LINGER); - default: - AZ_TracePrintf("AzSock", "AzSocket option %x not yet supported", opt); - value = 0; - break; - } + default: + AZ_TracePrintf("AzSock", "AzSocket option %x not yet supported", opt); + value = 0; + break; + } #undef TRANSLATE - return value; - } + return value; + } - AZSOCKET HandleInvalidSocket(SOCKET sock) + AZSOCKET HandleInvalidSocket(SOCKET sock) + { + AZSOCKET azsock = static_cast(sock); + if (sock == INVALID_SOCKET) { - AZSOCKET azsock = static_cast(sock); - if (sock == INVALID_SOCKET) - { - azsock = TranslateOSError(GetInternalSocketError); - } - return azsock; + azsock = TranslateOSError(GetInternalSocketError); } + return azsock; + } - AZ::s32 HandleSocketError(AZ::s32 socketError) + AZ::s32 HandleSocketError(AZ::s32 socketError) + { + if (socketError == SOCKET_ERROR) { - if (socketError == SOCKET_ERROR) - { - socketError = TranslateOSError(GetInternalSocketError); - } - return socketError; + socketError = TranslateOSError(GetInternalSocketError); } + return socketError; + } - const char* GetStringForError(AZ::s32 errorNumber) - { - AzSockError errorCode = AzSockError(errorNumber); + const char* GetStringForError(AZ::s32 errorNumber) + { + AzSockError errorCode = AzSockError(errorNumber); #define CASE_RETSTRING(errorEnum) case errorEnum: { return #errorEnum; } - switch (errorCode) - { - CASE_RETSTRING(AzSockError::eASE_NO_ERROR); - CASE_RETSTRING(AzSockError::eASE_SOCKET_INVALID); - CASE_RETSTRING(AzSockError::eASE_EACCES); - CASE_RETSTRING(AzSockError::eASE_EADDRINUSE); - CASE_RETSTRING(AzSockError::eASE_EADDRNOTAVAIL); - CASE_RETSTRING(AzSockError::eASE_EAFNOSUPPORT); - CASE_RETSTRING(AzSockError::eASE_EALREADY); - CASE_RETSTRING(AzSockError::eASE_EBADF); - CASE_RETSTRING(AzSockError::eASE_ECONNABORTED); - CASE_RETSTRING(AzSockError::eASE_ECONNREFUSED); - CASE_RETSTRING(AzSockError::eASE_ECONNRESET); - CASE_RETSTRING(AzSockError::eASE_EFAULT); - CASE_RETSTRING(AzSockError::eASE_EHOSTDOWN); - CASE_RETSTRING(AzSockError::eASE_EINPROGRESS); - CASE_RETSTRING(AzSockError::eASE_EINTR); - CASE_RETSTRING(AzSockError::eASE_EINVAL); - CASE_RETSTRING(AzSockError::eASE_EISCONN); - CASE_RETSTRING(AzSockError::eASE_EMFILE); - CASE_RETSTRING(AzSockError::eASE_EMSGSIZE); - CASE_RETSTRING(AzSockError::eASE_ENETUNREACH); - CASE_RETSTRING(AzSockError::eASE_ENOBUFS); - CASE_RETSTRING(AzSockError::eASE_ENOPROTOOPT); - CASE_RETSTRING(AzSockError::eASE_ENOTCONN); - CASE_RETSTRING(AzSockError::eASE_ENOTINITIALISED); - CASE_RETSTRING(AzSockError::eASE_EOPNOTSUPP); - CASE_RETSTRING(AzSockError::eASE_EPIPE); - CASE_RETSTRING(AzSockError::eASE_EPROTONOSUPPORT); - CASE_RETSTRING(AzSockError::eASE_ETIMEDOUT); - CASE_RETSTRING(AzSockError::eASE_ETOOMANYREFS); - CASE_RETSTRING(AzSockError::eASE_EWOULDBLOCK); - CASE_RETSTRING(AzSockError::eASE_EWOULDBLOCK_CONN); - CASE_RETSTRING(AzSockError::eASE_MISC_ERROR); - } + switch (errorCode) + { + CASE_RETSTRING(AzSockError::eASE_NO_ERROR); + CASE_RETSTRING(AzSockError::eASE_SOCKET_INVALID); + CASE_RETSTRING(AzSockError::eASE_EACCES); + CASE_RETSTRING(AzSockError::eASE_EADDRINUSE); + CASE_RETSTRING(AzSockError::eASE_EADDRNOTAVAIL); + CASE_RETSTRING(AzSockError::eASE_EAFNOSUPPORT); + CASE_RETSTRING(AzSockError::eASE_EALREADY); + CASE_RETSTRING(AzSockError::eASE_EBADF); + CASE_RETSTRING(AzSockError::eASE_ECONNABORTED); + CASE_RETSTRING(AzSockError::eASE_ECONNREFUSED); + CASE_RETSTRING(AzSockError::eASE_ECONNRESET); + CASE_RETSTRING(AzSockError::eASE_EFAULT); + CASE_RETSTRING(AzSockError::eASE_EHOSTDOWN); + CASE_RETSTRING(AzSockError::eASE_EINPROGRESS); + CASE_RETSTRING(AzSockError::eASE_EINTR); + CASE_RETSTRING(AzSockError::eASE_EINVAL); + CASE_RETSTRING(AzSockError::eASE_EISCONN); + CASE_RETSTRING(AzSockError::eASE_EMFILE); + CASE_RETSTRING(AzSockError::eASE_EMSGSIZE); + CASE_RETSTRING(AzSockError::eASE_ENETUNREACH); + CASE_RETSTRING(AzSockError::eASE_ENOBUFS); + CASE_RETSTRING(AzSockError::eASE_ENOPROTOOPT); + CASE_RETSTRING(AzSockError::eASE_ENOTCONN); + CASE_RETSTRING(AzSockError::eASE_ENOTINITIALISED); + CASE_RETSTRING(AzSockError::eASE_EOPNOTSUPP); + CASE_RETSTRING(AzSockError::eASE_EPIPE); + CASE_RETSTRING(AzSockError::eASE_EPROTONOSUPPORT); + CASE_RETSTRING(AzSockError::eASE_ETIMEDOUT); + CASE_RETSTRING(AzSockError::eASE_ETOOMANYREFS); + CASE_RETSTRING(AzSockError::eASE_EWOULDBLOCK); + CASE_RETSTRING(AzSockError::eASE_EWOULDBLOCK_CONN); + CASE_RETSTRING(AzSockError::eASE_MISC_ERROR); + } #undef CASE_RETSTRING - return "(invalid)"; - } - - AZ::u32 HostToNetLong(AZ::u32 hstLong) - { - return htonl(hstLong); - } - - AZ::u32 NetToHostLong(AZ::u32 netLong) - { - return ntohl(netLong); - } - - AZ::u16 HostToNetShort(AZ::u16 hstShort) - { - return htons(hstShort); - } - - AZ::u16 NetToHostShort(AZ::u16 netShort) - { - return ntohs(netShort); - } - - AZ::s32 GetHostName(AZStd::string& hostname) - { - AZ::s32 result = 0; - hostname.clear(); - char name[256]; - result = HandleSocketError(gethostname(name, AZ_ARRAY_SIZE(name))); - if (result == static_cast(AzSockError::eASE_NO_ERROR)) - { - hostname = name; - } - return result; - } - - AZSOCKET Socket() - { - return Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - } - - AZSOCKET Socket(AZ::s32 af, AZ::s32 type, AZ::s32 protocol) - { - return HandleInvalidSocket(socket(af, type, protocol)); - } - - AZ::s32 SetSockOpt(AZSOCKET sock, AZ::s32 level, AZ::s32 optname, const char* optval, AZ::s32 optlen) - { - AZSOCKLEN length(optlen); - return HandleSocketError(setsockopt(sock, level, optname, optval, length)); - } - - AZ::s32 SetSocketOption(AZSOCKET sock, AzSocketOption opt, bool enable) - { - AZ::u32 val = enable ? 1 : 0; - return SetSockOpt(sock, SOL_SOCKET, TranslateSocketOption(opt), reinterpret_cast(&val), sizeof(val)); - } - - AZ::s32 EnableTCPNoDelay(AZSOCKET sock, bool enable) - { - AZ::u32 val = enable ? 1 : 0; - return SetSockOpt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&val), sizeof(val)); - } - - AZ::s32 SetSocketBlockingMode(AZSOCKET sock, bool blocking) - { - AZ::s32 flags = ::fcntl(sock, F_GETFL); - flags &= ~O_NONBLOCK; - flags |= (blocking ? 0 : O_NONBLOCK); - return ::fcntl(sock, F_SETFL, flags); - } - - AZ::s32 CloseSocket(AZSOCKET sock) - { - return HandleSocketError(closesocket(sock)); - } - - AZ::s32 Shutdown(AZSOCKET sock, AZ::s32 how) - { - return HandleSocketError(shutdown(sock, how)); - } - - AZ::s32 GetSockName(AZSOCKET sock, AzSocketAddress& addr) - { - AZSOCKADDR sAddr; - AZSOCKLEN sAddrLen = sizeof(AZSOCKADDR); - memset(&sAddr, 0, sAddrLen); - AZ::s32 result = HandleSocketError(getsockname(sock, &sAddr, &sAddrLen)); - addr = sAddr; - return result; - } - - AZ::s32 Connect(AZSOCKET sock, const AzSocketAddress& addr) - { - AZ::s32 err = HandleSocketError(connect(sock, addr.GetTargetAddress(), sizeof(AZSOCKADDR_IN))); - if (err == static_cast(AzSockError::eASE_EINPROGRESS)) - { - err = static_cast(AzSockError::eASE_EWOULDBLOCK_CONN); - } - return err; - } - - AZ::s32 Listen(AZSOCKET sock, AZ::s32 backlog) - { - return HandleSocketError(listen(sock, backlog)); - } - - AZSOCKET Accept(AZSOCKET sock, AzSocketAddress& addr) - { - AZSOCKADDR sAddr; - AZSOCKLEN sAddrLen = sizeof(AZSOCKADDR); - memset(&sAddr, 0, sAddrLen); - AZSOCKET outSock = HandleInvalidSocket(accept(sock, &sAddr, &sAddrLen)); - addr = sAddr; - return outSock; - } - - AZ::s32 Send(AZSOCKET sock, const char* buf, AZ::s32 len, AZ::s32 flags) - { - AZ::s32 msgNoSignal = MSG_NOSIGNAL; - return HandleSocketError(send(sock, buf, len, flags | msgNoSignal)); - } - - AZ::s32 Recv(AZSOCKET sock, char* buf, AZ::s32 len, AZ::s32 flags) - { - return HandleSocketError(recv(sock, buf, len, flags)); - } - - AZ::s32 Bind(AZSOCKET sock, const AzSocketAddress& addr) - { - return HandleSocketError(bind(sock, addr.GetTargetAddress(), sizeof(AZSOCKADDR_IN))); - } - - AZ::s32 Select(AZSOCKET sock, AZFD_SET* readfdsock, AZFD_SET* writefdsock, AZFD_SET* exceptfdsock, AZTIMEVAL* timeout) - { - return HandleSocketError(::select(sock + 1, readfdsock, writefdsock, exceptfdsock, timeout)); - } - - AZ::s32 IsRecvPending(AZSOCKET sock, AZTIMEVAL* timeout) - { - AZFD_SET readSet; - FD_ZERO(&readSet); - FD_SET(sock, &readSet); - - AZ::s32 ret = Select(sock, &readSet, nullptr, nullptr, timeout); - if (ret >= 0) - { - ret = FD_ISSET(sock, &readSet); - if (ret != 0) - { - ret = 1; - } - } - - return ret; - } - - AZ::s32 WaitForWritableSocket(AZSOCKET sock, AZTIMEVAL* timeout) - { - AZFD_SET writeSet; - FD_ZERO(&writeSet); - FD_SET(sock, &writeSet); - - AZ::s32 ret = Select(sock, nullptr, &writeSet, nullptr, timeout); - if (ret >= 0) - { - ret = FD_ISSET(sock, &writeSet); - if (ret != 0) - { - ret = 1; - } - } - - return ret; - } - - AZ::s32 Startup() - { - return static_cast(AzSockError::eASE_NO_ERROR); - } - - AZ::s32 Cleanup() - { - return static_cast(AzSockError::eASE_NO_ERROR); - } - - bool ResolveAddress(const AZStd::string& ip, AZ::u16 port, AZSOCKADDR_IN& socketAddress) - { - bool foundAddr = false; - addrinfo hints; - memset(&hints, 0, sizeof(addrinfo)); - addrinfo* addrInfo; - hints.ai_family = AF_INET; - hints.ai_flags = AI_CANONNAME; - char strPort[8]; - azsnprintf(strPort, AZ_ARRAY_SIZE(strPort), "%d", port); - - const char* address = ip.c_str(); - if (address && strlen(address) == 0) // getaddrinfo doesn't accept empty string - { - address = nullptr; - } - - AZ::s32 err = HandleSocketError(getaddrinfo(address, strPort, &hints, &addrInfo)); - if (err == 0) // eASE_NO_ERROR - { - if (addrInfo->ai_family == AF_INET) - { - socketAddress = *reinterpret_cast(addrInfo->ai_addr); - foundAddr = true; - } - - freeaddrinfo(addrInfo); - } - else - { - AZ_Assert(false, "AzSocketAddress could not resolve address %s with port %d. (reason - %s)", ip.c_str(), port, GetStringForError(err)); - } - return foundAddr; - } + return "(invalid)"; } -} + + AZ::u32 HostToNetLong(AZ::u32 hstLong) + { + return htonl(hstLong); + } + + AZ::u32 NetToHostLong(AZ::u32 netLong) + { + return ntohl(netLong); + } + + AZ::u16 HostToNetShort(AZ::u16 hstShort) + { + return htons(hstShort); + } + + AZ::u16 NetToHostShort(AZ::u16 netShort) + { + return ntohs(netShort); + } + + AZ::s32 GetHostName(AZStd::string& hostname) + { + AZ::s32 result = 0; + hostname.clear(); + char name[256]; + result = HandleSocketError(gethostname(name, AZ_ARRAY_SIZE(name))); + if (result == static_cast(AzSockError::eASE_NO_ERROR)) + { + hostname = name; + } + return result; + } + + AZSOCKET Socket() + { + return Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + } + + AZSOCKET Socket(AZ::s32 af, AZ::s32 type, AZ::s32 protocol) + { + return HandleInvalidSocket(socket(af, type, protocol)); + } + + AZ::s32 SetSockOpt(AZSOCKET sock, AZ::s32 level, AZ::s32 optname, const char* optval, AZ::s32 optlen) + { + AZSOCKLEN length(optlen); + return HandleSocketError(setsockopt(sock, level, optname, optval, length)); + } + + AZ::s32 SetSocketOption(AZSOCKET sock, AzSocketOption opt, bool enable) + { + AZ::u32 val = enable ? 1 : 0; + return SetSockOpt(sock, SOL_SOCKET, TranslateSocketOption(opt), reinterpret_cast(&val), sizeof(val)); + } + + AZ::s32 EnableTCPNoDelay(AZSOCKET sock, bool enable) + { + AZ::u32 val = enable ? 1 : 0; + return SetSockOpt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&val), sizeof(val)); + } + + AZ::s32 SetSocketBlockingMode(AZSOCKET sock, bool blocking) + { + AZ::s32 flags = ::fcntl(sock, F_GETFL); + flags &= ~O_NONBLOCK; + flags |= (blocking ? 0 : O_NONBLOCK); + return ::fcntl(sock, F_SETFL, flags); + } + + AZ::s32 CloseSocket(AZSOCKET sock) + { + return HandleSocketError(closesocket(sock)); + } + + AZ::s32 Shutdown(AZSOCKET sock, AZ::s32 how) + { + return HandleSocketError(shutdown(sock, how)); + } + + AZ::s32 GetSockName(AZSOCKET sock, AzSocketAddress& addr) + { + AZSOCKADDR sAddr; + AZSOCKLEN sAddrLen = sizeof(AZSOCKADDR); + memset(&sAddr, 0, sAddrLen); + AZ::s32 result = HandleSocketError(getsockname(sock, &sAddr, &sAddrLen)); + addr = sAddr; + return result; + } + + AZ::s32 Connect(AZSOCKET sock, const AzSocketAddress& addr) + { + AZ::s32 err = HandleSocketError(connect(sock, addr.GetTargetAddress(), sizeof(AZSOCKADDR_IN))); + if (err == static_cast(AzSockError::eASE_EINPROGRESS)) + { + err = static_cast(AzSockError::eASE_EWOULDBLOCK_CONN); + } + return err; + } + + AZ::s32 Listen(AZSOCKET sock, AZ::s32 backlog) + { + return HandleSocketError(listen(sock, backlog)); + } + + AZSOCKET Accept(AZSOCKET sock, AzSocketAddress& addr) + { + AZSOCKADDR sAddr; + AZSOCKLEN sAddrLen = sizeof(AZSOCKADDR); + memset(&sAddr, 0, sAddrLen); + AZSOCKET outSock = HandleInvalidSocket(accept(sock, &sAddr, &sAddrLen)); + addr = sAddr; + return outSock; + } + + AZ::s32 Send(AZSOCKET sock, const char* buf, AZ::s32 len, AZ::s32 flags) + { + AZ::s32 msgNoSignal = MSG_NOSIGNAL; + return HandleSocketError(send(sock, buf, len, flags | msgNoSignal)); + } + + AZ::s32 Recv(AZSOCKET sock, char* buf, AZ::s32 len, AZ::s32 flags) + { + return HandleSocketError(recv(sock, buf, len, flags)); + } + + AZ::s32 Bind(AZSOCKET sock, const AzSocketAddress& addr) + { + return HandleSocketError(bind(sock, addr.GetTargetAddress(), sizeof(AZSOCKADDR_IN))); + } + + AZ::s32 Select(AZSOCKET sock, AZFD_SET* readfdsock, AZFD_SET* writefdsock, AZFD_SET* exceptfdsock, AZTIMEVAL* timeout) + { + return HandleSocketError(::select(sock + 1, readfdsock, writefdsock, exceptfdsock, timeout)); + } + + AZ::s32 IsRecvPending(AZSOCKET sock, AZTIMEVAL* timeout) + { + AZFD_SET readSet; + FD_ZERO(&readSet); + FD_SET(sock, &readSet); + + AZ::s32 ret = Select(sock, &readSet, nullptr, nullptr, timeout); + if (ret >= 0) + { + ret = FD_ISSET(sock, &readSet); + if (ret != 0) + { + ret = 1; + } + } + + return ret; + } + + AZ::s32 WaitForWritableSocket(AZSOCKET sock, AZTIMEVAL* timeout) + { + AZFD_SET writeSet; + FD_ZERO(&writeSet); + FD_SET(sock, &writeSet); + + AZ::s32 ret = Select(sock, nullptr, &writeSet, nullptr, timeout); + if (ret >= 0) + { + ret = FD_ISSET(sock, &writeSet); + if (ret != 0) + { + ret = 1; + } + } + + return ret; + } + + AZ::s32 Startup() + { + return static_cast(AzSockError::eASE_NO_ERROR); + } + + AZ::s32 Cleanup() + { + return static_cast(AzSockError::eASE_NO_ERROR); + } + + bool ResolveAddress(const AZStd::string& ip, AZ::u16 port, AZSOCKADDR_IN& socketAddress) + { + bool foundAddr = false; + addrinfo hints; + memset(&hints, 0, sizeof(addrinfo)); + addrinfo* addrInfo; + hints.ai_family = AF_INET; + hints.ai_flags = AI_CANONNAME; + char strPort[8]; + azsnprintf(strPort, AZ_ARRAY_SIZE(strPort), "%d", port); + + const char* address = ip.c_str(); + if (address && strlen(address) == 0) // getaddrinfo doesn't accept empty string + { + address = nullptr; + } + + AZ::s32 err = HandleSocketError(getaddrinfo(address, strPort, &hints, &addrInfo)); + if (err == 0) // eASE_NO_ERROR + { + if (addrInfo->ai_family == AF_INET) + { + socketAddress = *reinterpret_cast(addrInfo->ai_addr); + foundAddr = true; + } + + freeaddrinfo(addrInfo); + } + else + { + AZ_Assert(false, "AzSocketAddress could not resolve address %s with port %d. (reason - %s)", ip.c_str(), port, GetStringForError(err)); + } + return foundAddr; + } +} // namespace AZ::AzSock diff --git a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Utils/Utils_UnixLike.cpp b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Utils/Utils_UnixLike.cpp index 7327c8f152..8a93f88ac2 100644 --- a/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Utils/Utils_UnixLike.cpp +++ b/Code/Framework/AzCore/Platform/Common/UnixLike/AzCore/Utils/Utils_UnixLike.cpp @@ -11,68 +11,65 @@ #include #include -namespace AZ +namespace AZ::Utils { - namespace Utils + void RequestAbnormalTermination() { - void RequestAbnormalTermination() + abort(); + } + + void NativeErrorMessageBox(const char*, const char*) {} + + AZ::IO::FixedMaxPathString GetHomeDirectory() + { + constexpr AZStd::string_view overrideHomeDirKey = "/Amazon/Settings/override_home_dir"; + AZ::IO::FixedMaxPathString overrideHomeDir; + if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr) { - abort(); - } - - void NativeErrorMessageBox(const char*, const char*) {} - - AZ::IO::FixedMaxPathString GetHomeDirectory() - { - constexpr AZStd::string_view overrideHomeDirKey = "/Amazon/Settings/override_home_dir"; - AZ::IO::FixedMaxPathString overrideHomeDir; - if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr) + if (settingsRegistry->Get(overrideHomeDir, overrideHomeDirKey)) { - if (settingsRegistry->Get(overrideHomeDir, overrideHomeDirKey)) - { - AZ::IO::FixedMaxPath path{overrideHomeDir}; - return path.Native(); - } - } - - if (const char* homePath = std::getenv("HOME"); homePath != nullptr) - { - AZ::IO::FixedMaxPath path{homePath}; + AZ::IO::FixedMaxPath path{overrideHomeDir}; return path.Native(); } - - struct passwd* pass = getpwuid(getuid()); - if (pass) - { - AZ::IO::FixedMaxPath path{pass->pw_dir}; - return path.Native(); - } - - return {}; } - bool ConvertToAbsolutePath(const char* path, char* absolutePath, AZ::u64 maxLength) + if (const char* homePath = std::getenv("HOME"); homePath != nullptr) { + AZ::IO::FixedMaxPath path{homePath}; + return path.Native(); + } + + struct passwd* pass = getpwuid(getuid()); + if (pass) + { + AZ::IO::FixedMaxPath path{pass->pw_dir}; + return path.Native(); + } + + return {}; + } + + bool ConvertToAbsolutePath(const char* path, char* absolutePath, AZ::u64 maxLength) + { #ifdef PATH_MAX - static constexpr size_t UnixMaxPathLength = PATH_MAX; + static constexpr size_t UnixMaxPathLength = PATH_MAX; #else - // Fallback to 4096 if the PATH_MAX macro isn't defined on the Unix System - static constexpr size_t UnixMaxPathLength = 4096; + // Fallback to 4096 if the PATH_MAX macro isn't defined on the Unix System + static constexpr size_t UnixMaxPathLength = 4096; #endif - if (!AZ::IO::PathView(path).IsAbsolute()) + if (!AZ::IO::PathView(path).IsAbsolute()) + { + // note that realpath fails if the path does not exist and actually changes the return value + // to be the actual place that FAILED, which we don't want. + // if we fail, we'd prefer to fall through and at least use the original path. + char absolutePathBuffer[UnixMaxPathLength]; + if (const char* result = realpath(path, absolutePathBuffer); result != nullptr) { - // note that realpath fails if the path does not exist and actually changes the return value - // to be the actual place that FAILED, which we don't want. - // if we fail, we'd prefer to fall through and at least use the original path. - char absolutePathBuffer[UnixMaxPathLength]; - if (const char* result = realpath(path, absolutePathBuffer); result != nullptr) - { - azstrcpy(absolutePath, maxLength, absolutePathBuffer); - return true; - } + azstrcpy(absolutePath, maxLength, absolutePathBuffer); + return true; } - azstrcpy(absolutePath, maxLength, path); - return AZ::IO::PathView(absolutePath).IsAbsolute(); } - } // namespace Utils -} // namespace AZ + azstrcpy(absolutePath, maxLength, path); + return AZ::IO::PathView(absolutePath).IsAbsolute(); + } +} // namespace AZ::Utils diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/AzCore_Traits_Linux.h b/Code/Framework/AzCore/Platform/Linux/AzCore/AzCore_Traits_Linux.h index 6ba369e86d..2c0d554078 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/AzCore_Traits_Linux.h +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/AzCore_Traits_Linux.h @@ -75,7 +75,6 @@ #define AZ_TRAIT_HEAPSCHEMA_COMPILE_MALLINFO 1 #define AZ_TRAIT_IS_ABS_PATH_IF_COLON_FOUND_ANYWHERE 0 #define AZ_TRAIT_JSON_CLANG_IGNORE_UNKNOWN_WARNING 0 -#define AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS 0 #define AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING_INTERVAL_MS 0 diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp index 1ca06fcf7f..e4cd0d0db4 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp @@ -9,16 +9,10 @@ #include #include -namespace AZ +namespace AZ::Debug::Platform { - namespace Debug + void OutputToDebugger([[maybe_unused]] const char* title, [[maybe_unused]] const char* message) { - namespace Platform - { - void OutputToDebugger([[maybe_unused]] const char* title, [[maybe_unused]] const char* message) - { - // std::cout << title << ": " << message; - } - } + // std::cout << title << ": " << message; } -} +} // namespace AZ::Debug::Platform diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Memory/HeapSchema_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Memory/HeapSchema_Linux.cpp index e28832cf61..d9defd5acc 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Memory/HeapSchema_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Memory/HeapSchema_Linux.cpp @@ -8,13 +8,10 @@ #include -namespace AZ +namespace AZ::Platform { - namespace Platform + size_t GetHeapCapacity() { - size_t GetHeapCapacity() - { - return 0; - } + return 0; } -} +} // namespace AZ::Platform diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Module/DynamicModuleHandle_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Module/DynamicModuleHandle_Linux.cpp index 62718c2d46..aee2bdb622 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Module/DynamicModuleHandle_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Module/DynamicModuleHandle_Linux.cpp @@ -10,28 +10,25 @@ #include #include -namespace AZ +namespace AZ::Platform { - namespace Platform + AZ::IO::FixedMaxPath GetModulePath() { - AZ::IO::FixedMaxPath GetModulePath() - { - return AZ::Utils::GetExecutableDirectory(); - } - - void* OpenModule(const AZ::OSString& fileName, bool& alreadyOpen) - { - void* handle = dlopen(fileName.c_str(), RTLD_NOLOAD); - alreadyOpen = (handle != nullptr); - if (!alreadyOpen) - { - handle = dlopen(fileName.c_str(), RTLD_NOW); - } - return handle; - } - - void ConstructModuleFullFileName(AZ::IO::FixedMaxPath&) - { - } + return AZ::Utils::GetExecutableDirectory(); } -} + + void* OpenModule(const AZ::OSString& fileName, bool& alreadyOpen) + { + void* handle = dlopen(fileName.c_str(), RTLD_NOLOAD); + alreadyOpen = (handle != nullptr); + if (!alreadyOpen) + { + handle = dlopen(fileName.c_str(), RTLD_NOW); + } + return handle; + } + + void ConstructModuleFullFileName(AZ::IO::FixedMaxPath&) + { + } +} // namespace AZ::Platform diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Module/Internal/ModuleManagerSearchPathTool_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Module/Internal/ModuleManagerSearchPathTool_Linux.cpp index 39b4cbd4ea..9dfa08e554 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Module/Internal/ModuleManagerSearchPathTool_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Module/Internal/ModuleManagerSearchPathTool_Linux.cpp @@ -8,20 +8,17 @@ #include -namespace AZ +namespace AZ::Internal { - namespace Internal + ModuleManagerSearchPathTool::ModuleManagerSearchPathTool() { - ModuleManagerSearchPathTool::ModuleManagerSearchPathTool() - { - } + } - ModuleManagerSearchPathTool::~ModuleManagerSearchPathTool() - { - } + ModuleManagerSearchPathTool::~ModuleManagerSearchPathTool() + { + } - void ModuleManagerSearchPathTool::SetModuleSearchPath(const AZ::DynamicModuleDescriptor&) - { - } - } // namespace Internal -} // namespace AZ + void ModuleManagerSearchPathTool::SetModuleSearchPath(const AZ::DynamicModuleDescriptor&) + { + } +} // namespace AZ::Internal diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Utils/Utils_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Utils/Utils_Linux.cpp index d75eab0139..ed4e2674c5 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Utils/Utils_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Utils/Utils_Linux.cpp @@ -13,42 +13,39 @@ #include -namespace AZ +namespace AZ::Utils { - namespace Utils + GetExecutablePathReturnType GetExecutablePath(char* exeStorageBuffer, size_t exeStorageSize) { - GetExecutablePathReturnType GetExecutablePath(char* exeStorageBuffer, size_t exeStorageSize) + GetExecutablePathReturnType result; + result.m_pathIncludesFilename = true; + + // http://man7.org/linux/man-pages/man5/proc.5.html + const ssize_t bytesWritten = readlink("/proc/self/exe", exeStorageBuffer, exeStorageSize); + if (bytesWritten == -1) { - GetExecutablePathReturnType result; - result.m_pathIncludesFilename = true; - - // http://man7.org/linux/man-pages/man5/proc.5.html - const ssize_t bytesWritten = readlink("/proc/self/exe", exeStorageBuffer, exeStorageSize); - if (bytesWritten == -1) - { - result.m_pathStored = ExecutablePathResult::GeneralError; - } - else if (bytesWritten == exeStorageSize) - { - result.m_pathStored = ExecutablePathResult::BufferSizeNotLargeEnough; - } - else - { - // readlink doesn't null terminate - exeStorageBuffer[bytesWritten] = '\0'; - } - - return result; + result.m_pathStored = ExecutablePathResult::GeneralError; + } + else if (bytesWritten == exeStorageSize) + { + result.m_pathStored = ExecutablePathResult::BufferSizeNotLargeEnough; + } + else + { + // readlink doesn't null terminate + exeStorageBuffer[bytesWritten] = '\0'; } - AZStd::optional GetDefaultAppRootPath() - { - return AZStd::nullopt; - } - - AZStd::optional GetDevWriteStoragePath() - { - return AZStd::nullopt; - } + return result; } -} + + AZStd::optional GetDefaultAppRootPath() + { + return AZStd::nullopt; + } + + AZStd::optional GetDevWriteStoragePath() + { + return AZStd::nullopt; + } +} // namespace AZ::Utils diff --git a/Code/Framework/AzCore/Platform/Mac/AzCore/AzCore_Traits_Mac.h b/Code/Framework/AzCore/Platform/Mac/AzCore/AzCore_Traits_Mac.h index a41b5c6baa..816a28b728 100644 --- a/Code/Framework/AzCore/Platform/Mac/AzCore/AzCore_Traits_Mac.h +++ b/Code/Framework/AzCore/Platform/Mac/AzCore/AzCore_Traits_Mac.h @@ -75,7 +75,6 @@ #define AZ_TRAIT_HEAPSCHEMA_COMPILE_MALLINFO 1 #define AZ_TRAIT_IS_ABS_PATH_IF_COLON_FOUND_ANYWHERE 0 #define AZ_TRAIT_JSON_CLANG_IGNORE_UNKNOWN_WARNING 0 -#define AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS 0 #define AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING_INTERVAL_MS 0 diff --git a/Code/Framework/AzCore/Platform/Windows/AzCore/AzCore_Traits_Windows.h b/Code/Framework/AzCore/Platform/Windows/AzCore/AzCore_Traits_Windows.h index 2f9fcefdbd..83cff9b54f 100644 --- a/Code/Framework/AzCore/Platform/Windows/AzCore/AzCore_Traits_Windows.h +++ b/Code/Framework/AzCore/Platform/Windows/AzCore/AzCore_Traits_Windows.h @@ -75,7 +75,6 @@ #define AZ_TRAIT_HEAPSCHEMA_COMPILE_MALLINFO 1 #define AZ_TRAIT_IS_ABS_PATH_IF_COLON_FOUND_ANYWHERE 0 #define AZ_TRAIT_JSON_CLANG_IGNORE_UNKNOWN_WARNING 1 -#define AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS 0 #define AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE 1 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING_INTERVAL_MS 0 diff --git a/Code/Framework/AzCore/Platform/iOS/AzCore/AzCore_Traits_iOS.h b/Code/Framework/AzCore/Platform/iOS/AzCore/AzCore_Traits_iOS.h index d53f4b057e..b0832c54f6 100644 --- a/Code/Framework/AzCore/Platform/iOS/AzCore/AzCore_Traits_iOS.h +++ b/Code/Framework/AzCore/Platform/iOS/AzCore/AzCore_Traits_iOS.h @@ -76,7 +76,6 @@ #define AZ_TRAIT_HEAPSCHEMA_COMPILE_MALLINFO 1 #define AZ_TRAIT_IS_ABS_PATH_IF_COLON_FOUND_ANYWHERE 0 #define AZ_TRAIT_JSON_CLANG_IGNORE_UNKNOWN_WARNING 0 -#define AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS 0 #define AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING 0 #define AZ_TRAIT_PUMP_SYSTEM_EVENTS_WHILE_LOADING_INTERVAL_MS 0 diff --git a/Code/Framework/AzCore/Tests/AZStd/String.cpp b/Code/Framework/AzCore/Tests/AZStd/String.cpp index 68bdd311d5..356310a94d 100644 --- a/Code/Framework/AzCore/Tests/AZStd/String.cpp +++ b/Code/Framework/AzCore/Tests/AZStd/String.cpp @@ -2376,17 +2376,52 @@ namespace UnitTest static_assert(AZStd::wildcard_match_case(filter1, blahValue)); } - TEST_F(String, StringEraseIf_Succeeds) + TEST_F(String, StringCXX20Erase_Succeeds) { - AZStd::string eraseIfTest = "ABC CBA"; - auto eraseCount = AZStd::erase_if(eraseIfTest, [](AZStd::string::value_type ch) - { - return ch == 'C'; - }); + auto erasePredicate = [](AZStd::string::value_type ch) + { + return ch == 'C'; + }; + auto eraseCount = AZStd::erase_if(eraseIfTest, erasePredicate); EXPECT_EQ(2, eraseCount); EXPECT_EQ(5, eraseIfTest.size()); EXPECT_STREQ("AB BA", eraseIfTest.c_str()); + + // Now erase the letter 'A'; + eraseCount = AZStd::erase(eraseIfTest, 'A'); + EXPECT_EQ(2, eraseCount); + EXPECT_EQ(3, eraseIfTest.size()); + EXPECT_EQ("B B", eraseIfTest); + } + + TEST_F(String, FixedStringCXX20Erase_Succeeds) + { + // Erase 'l' from the phrase "Hello" World" + constexpr auto eraseTest = [](const char* testString) constexpr + { + AZStd::fixed_string<16> testResult{ testString }; + AZStd::erase(testResult, 'l'); + return testResult; + }("HelloWorld"); + + static_assert(eraseTest == "HeoWord"); + EXPECT_EQ("HeoWord", eraseTest); + + // Use erase_if to erase both 'H' and 'e' from the remaining eraseTest string + constexpr auto eraseIfTest = [](AZStd::string_view testString) constexpr + { + AZStd::fixed_string<16> testResult{ testString }; + auto erasePredicate = [](char ch) + { + return ch == 'H' || ch == 'e'; + }; + AZStd::erase_if(testResult, erasePredicate); + return testResult; + }(eraseTest); + + static_assert(eraseIfTest == "oWord"); + EXPECT_EQ("oWord", eraseIfTest); } template diff --git a/Code/Framework/AzCore/Tests/AZStd/VectorAndArray.cpp b/Code/Framework/AzCore/Tests/AZStd/VectorAndArray.cpp index 47c3630091..851a7a8609 100644 --- a/Code/Framework/AzCore/Tests/AZStd/VectorAndArray.cpp +++ b/Code/Framework/AzCore/Tests/AZStd/VectorAndArray.cpp @@ -753,7 +753,7 @@ namespace UnitTest TEST_F(Arrays, FixedVectorCanCopyAndMoveWithDifferentCapacity) { - constexpr AZStd::fixed_vector sourceVector{ 1,2,3,4,5 }; + AZStd::fixed_vector sourceVector{ 1,2,3,4,5 }; AZStd::fixed_vector copyConstructVector{ sourceVector }; EXPECT_EQ(sourceVector, copyConstructVector); @@ -768,32 +768,63 @@ namespace UnitTest AZStd::fixed_vector moveAssignVector = AZStd::move(moveConstructVector); - constexpr AZStd::fixed_vector expectedVector{ 1,2,3,4,5,6 }; + AZStd::fixed_vector expectedVector{ 1,2,3,4,5,6 }; EXPECT_EQ(expectedVector, moveAssignVector); } TEST_F(Arrays, FixedVectorComparisonOperatorsSucceedAsExpected) { - constexpr AZStd::fixed_vector testVector{ 1,2,3,4,5 }; - constexpr AZStd::fixed_vector equalVector{ 1,2,3,4,5 }; - constexpr AZStd::fixed_vector notEqualVectorDifferentSize{ 1,2,3,4,5,6 }; - constexpr AZStd::fixed_vector lessVector{ 1,2,3,4,4 }; - constexpr AZStd::fixed_vector greaterVectorDifferentSize{ 1,2,3,4,5, 1 }; + AZStd::fixed_vector testVector{ 1,2,3,4,5 }; + AZStd::fixed_vector equalVector{ 1,2,3,4,5 }; + AZStd::fixed_vector notEqualVectorDifferentSize{ 1,2,3,4,5,6 }; + AZStd::fixed_vector lessVector{ 1,2,3,4,4 }; + AZStd::fixed_vector greaterVectorDifferentSize{ 1,2,3,4,5, 1 }; - static_assert(testVector == equalVector); - static_assert(testVector != notEqualVectorDifferentSize); - static_assert(testVector != lessVector); - static_assert(lessVector < testVector); - static_assert(lessVector < greaterVectorDifferentSize); - static_assert(lessVector <= lessVector); - static_assert(lessVector <= testVector); - static_assert(lessVector <= greaterVectorDifferentSize); - static_assert(testVector > lessVector); - static_assert(testVector > lessVector); - static_assert(notEqualVectorDifferentSize > testVector); - static_assert(testVector >= testVector); - static_assert(testVector >= lessVector); - static_assert(greaterVectorDifferentSize > lessVector); + EXPECT_EQ(testVector, equalVector); + EXPECT_NE(testVector, notEqualVectorDifferentSize); + EXPECT_NE(testVector, lessVector); + EXPECT_LT(lessVector, testVector); + EXPECT_LT(lessVector, greaterVectorDifferentSize); + EXPECT_LE(lessVector, lessVector); + EXPECT_LE(lessVector, testVector); + EXPECT_LE(lessVector, greaterVectorDifferentSize); + EXPECT_GT(testVector, lessVector); + EXPECT_GT(testVector, lessVector); + EXPECT_GT(notEqualVectorDifferentSize, testVector); + EXPECT_GE(testVector, testVector); + EXPECT_GE(testVector, lessVector); + EXPECT_GT(greaterVectorDifferentSize, lessVector); + } + + TEST_F(Arrays, FixedVectorCXX20Erase_Succeeds) + { + // Erase 'l' from the phrase "Hello" World" + auto eraseTest = [](AZStd::initializer_list testInit) + { + AZStd::fixed_vector testResult{ testInit }; + AZStd::erase(testResult, 'l'); + return testResult; + }({ 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }); + + constexpr AZStd::string_view expectedEraseString = "HeoWord"; + AZStd::string_view testEraseString{ eraseTest.begin(), eraseTest.end() }; + EXPECT_EQ(expectedEraseString, testEraseString); + + // Use erase_if to erase both 'H' and 'e' from the remaining eraseTest string + auto eraseIfTest = [](const AZStd::fixed_vector& testVector) + { + AZStd::fixed_vector testResult{ testVector }; + auto erasePredicate = [](char ch) + { + return ch == 'H' || ch == 'e'; + }; + AZStd::erase_if(testResult, erasePredicate); + return testResult; + }(testEraseString); + + constexpr AZStd::string_view expectedEraseIfString = "oWord"; + AZStd::string_view testEraseIfString{ eraseIfTest.begin(), eraseIfTest.end() }; + EXPECT_EQ(expectedEraseIfString, testEraseIfString); } TEST_F(Arrays, VectorSwap) diff --git a/Code/Framework/AzCore/Tests/Asset/AssetManagerLoadingTests.cpp b/Code/Framework/AzCore/Tests/Asset/AssetManagerLoadingTests.cpp index e33dbce9c1..fe38451101 100644 --- a/Code/Framework/AzCore/Tests/Asset/AssetManagerLoadingTests.cpp +++ b/Code/Framework/AzCore/Tests/Asset/AssetManagerLoadingTests.cpp @@ -608,24 +608,9 @@ namespace UnitTest AZ::Data::AssetData::AssetStatus expected_base_status = AZ::Data::AssetData::AssetStatus::Ready; EXPECT_EQ(baseStatus, expected_base_status); } - - struct DebugListener : AZ::Interface::Registrar - { - void AssetStatusUpdate(AZ::Data::AssetId id, AZ::Data::AssetData::AssetStatus status) override - { - AZ::Debug::Trace::Output( - "", AZStd::string::format("Status %s - %d\n", id.ToString().c_str(), static_cast(status)).c_str()); - } - void ReleaseAsset(AZ::Data::AssetId id) override - { - AZ::Debug::Trace::Output( - "", AZStd::string::format("Release %s\n", id.ToString().c_str()).c_str()); - } - }; - + TEST_F(AssetJobsFloodTest, RapidAcquireAndRelease) { - DebugListener listener; auto assetUuids = { MyAsset1Id, MyAsset2Id, @@ -652,7 +637,7 @@ namespace UnitTest threads.emplace_back([this, &threadCount, &cv, assetUuid]() { bool checkLoaded = true; - for (int i = 0; i < 5000; i++) + for (int i = 0; i < 1000; i++) { Asset asset1 = m_testAssetManager->GetAsset(assetUuid, azrtti_typeid(), AZ::Data::AssetLoadBehavior::PreLoad); @@ -678,7 +663,7 @@ namespace UnitTest while (threadCount > 0 && !timedOut) { AZStd::unique_lock lock(mutex); - timedOut = (AZStd::cv_status::timeout == cv.wait_until(lock, AZStd::chrono::system_clock::now() + DefaultTimeoutSeconds * 20000)); + timedOut = (AZStd::cv_status::timeout == cv.wait_until(lock, AZStd::chrono::system_clock::now() + DefaultTimeoutSeconds)); } ASSERT_EQ(threadCount, 0) << "Thread count is non-zero, a thread has likely deadlocked. Test will not shut down cleanly."; @@ -1190,7 +1175,7 @@ namespace UnitTest #if AZ_TRAIT_DISABLE_FAILED_ASSET_MANAGER_TESTS TEST_F(AssetJobsFloodTest, DISABLED_ContainerFilterTest_ContainersWithAndWithoutFiltering_Success) #else - TEST_F(AssetJobsFloodTest, DISABLED_ContainerFilterTest_ContainersWithAndWithoutFiltering_Success) + TEST_F(AssetJobsFloodTest, ContainerFilterTest_ContainersWithAndWithoutFiltering_Success) #endif // !AZ_TRAIT_DISABLE_FAILED_ASSET_MANAGER_TESTS { m_assetHandlerAndCatalog->AssetCatalogRequestBus::Handler::BusConnect(); @@ -2297,6 +2282,45 @@ namespace UnitTest AssetManager::Destroy(); } + struct MockAssetContainer : AssetContainer + { + MockAssetContainer(Asset assetData, const AssetLoadParameters& loadParams) + { + // Copying the code in the original constructor, we can't call that constructor because it will not invoke our virtual method + m_rootAsset = AssetInternal::WeakAsset(assetData); + m_containerAssetId = m_rootAsset.GetId(); + + AddDependentAssets(assetData, loadParams); + } + + protected: + AZStd::vector>> CreateAndQueueDependentAssets( + const AZStd::vector& dependencyInfoList, const AssetLoadParameters& loadParamsCopyWithNoLoadingFilter) override + { + auto result = AssetContainer::CreateAndQueueDependentAssets(dependencyInfoList, loadParamsCopyWithNoLoadingFilter); + + // Sleep for a long enough time to allow asset loads to complete and start triggering AssetReady events + // This forces the race condition to occur + AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(500)); + + return result; + } + }; + + struct MockAssetManager : AssetManager + { + explicit MockAssetManager(const Descriptor& desc) + : AssetManager(desc) + { + } + + protected: + AZStd::shared_ptr CreateAssetContainer(Asset asset, const AssetLoadParameters& loadParams) const override + { + return AZStd::shared_ptr(aznew MockAssetContainer(asset, loadParams)); + } + }; + void ParallelDeepAssetReferences() { SerializeContext context; @@ -2304,7 +2328,7 @@ namespace UnitTest AssetWithAssetReference::Reflect(context); AssetManager::Descriptor desc; - AssetManager::Create(desc); + AssetManager::SetInstance(aznew MockAssetManager(desc)); auto& db = AssetManager::Instance(); @@ -2327,17 +2351,17 @@ namespace UnitTest // AssetC is MYASSETC AssetWithAssetReference c; - c.m_asset = AssetManager::Instance().CreateAsset(AssetId(MyAssetDId)); // point at D + c.m_asset = db.CreateAsset(AssetId(MyAssetDId)); // point at D EXPECT_TRUE(AZ::Utils::SaveObjectToFile(GetTestFolderPath() + "TestAsset3.txt", AZ::DataStream::ST_XML, &c, &context)); // AssetB is MYASSETB AssetWithAssetReference b; - b.m_asset = AssetManager::Instance().CreateAsset(AssetId(MyAssetCId)); // point at C + b.m_asset = db.CreateAsset(AssetId(MyAssetCId)); // point at C EXPECT_TRUE(AZ::Utils::SaveObjectToFile(GetTestFolderPath() + "TestAsset2.txt", AZ::DataStream::ST_XML, &b, &context)); // AssetA will be written to disk as MYASSETA AssetWithAssetReference a; - a.m_asset = AssetManager::Instance().CreateAsset(AssetId(MyAssetBId)); // point at B + a.m_asset = db.CreateAsset(AssetId(MyAssetBId)); // point at B EXPECT_TRUE(AZ::Utils::SaveObjectToFile(GetTestFolderPath() + "TestAsset1.txt", AZ::DataStream::ST_XML, &a, &context)); } @@ -2546,7 +2570,7 @@ namespace UnitTest TEST_F(AssetJobsMultithreadedTest, DISABLED_ParallelDeepAssetReferences) #else // temporarily disabled until sporadic failures can be root caused - TEST_F(AssetJobsMultithreadedTest, DISABLED_ParallelDeepAssetReferences) + TEST_F(AssetJobsMultithreadedTest, ParallelDeepAssetReferences) #endif // AZ_TRAIT_DISABLE_FAILED_ASSET_MANAGER_TESTS { ParallelDeepAssetReferences(); diff --git a/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.cpp b/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.cpp index 4dbd3c0e1e..c6fa296cbc 100644 --- a/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.cpp +++ b/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.cpp @@ -67,7 +67,10 @@ namespace UnitTest { SerializeContextFixture::SetUp(); + SuppressTraceOutput(false); + AZ::JobManagerDesc jobDesc; + AZ::JobManagerThreadDesc threadDesc; for (size_t threadCount = 0; threadCount < GetNumJobManagerThreads(); threadCount++) { @@ -111,9 +114,21 @@ namespace UnitTest delete m_jobContext; delete m_jobManager; + // Reset back to default suppression settings to avoid affecting other tests + SuppressTraceOutput(true); + SerializeContextFixture::TearDown(); } + void BaseAssetManagerTest::SuppressTraceOutput(bool suppress) + { + UnitTest::TestRunner::Instance().m_suppressAsserts = suppress; + UnitTest::TestRunner::Instance().m_suppressErrors = suppress; + UnitTest::TestRunner::Instance().m_suppressWarnings = suppress; + UnitTest::TestRunner::Instance().m_suppressPrintf = suppress; + UnitTest::TestRunner::Instance().m_suppressOutput = suppress; + } + void BaseAssetManagerTest::WriteAssetToDisk(const AZStd::string& assetName, [[maybe_unused]] const AZStd::string& assetIdGuid) { AZStd::string assetFileName = GetTestFolderPath() + assetName; diff --git a/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.h b/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.h index 757edb3713..29c2c124cd 100644 --- a/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.h +++ b/Code/Framework/AzCore/Tests/Asset/BaseAssetManagerTest.h @@ -63,6 +63,8 @@ namespace UnitTest void SetUp() override; void TearDown() override; + static void SuppressTraceOutput(bool suppress); + // Helper methods to create and destroy actual assets on the disk for true end-to-end asset loading. void WriteAssetToDisk(const AZStd::string& assetName, const AZStd::string& assetIdGuid); void DeleteAssetFromDisk(const AZStd::string& assetName); diff --git a/Code/Framework/AzCore/Tests/BehaviorContextFixture.h b/Code/Framework/AzCore/Tests/BehaviorContextFixture.h index 1895f2e35b..3c6d48add7 100644 --- a/Code/Framework/AzCore/Tests/BehaviorContextFixture.h +++ b/Code/Framework/AzCore/Tests/BehaviorContextFixture.h @@ -59,7 +59,6 @@ namespace UnitTest AZ::SerializeContext* GetSerializeContext() override { return nullptr; } AZ::BehaviorContext* GetBehaviorContext() override { return m_behaviorContext; } AZ::JsonRegistrationContext* GetJsonRegistrationContext() override { return nullptr; } - const char* GetAppRoot() const override { return nullptr; } const char* GetEngineRoot() const override { return nullptr; } const char* GetExecutableFolder() const override { return nullptr; } void EnumerateEntities(const EntityCallback& /*callback*/) override {} diff --git a/Code/Framework/AzCore/Tests/Components.cpp b/Code/Framework/AzCore/Tests/Components.cpp index 55b1c193b3..013d998c52 100644 --- a/Code/Framework/AzCore/Tests/Components.cpp +++ b/Code/Framework/AzCore/Tests/Components.cpp @@ -1060,26 +1060,21 @@ namespace UnitTest /** * UserSettingsComponent test */ - class UserSettingsTestApp - : public ComponentApplication - , public UserSettingsFileLocatorBus::Handler - { - public: - void SetExecutableFolder(const char* path) - { - m_exeDirectory = path; - } - + class UserSettingsTestApp + : public ComponentApplication + , public UserSettingsFileLocatorBus::Handler + { + public: AZStd::string ResolveFilePath(u32 providerId) override { AZStd::string filePath; if (providerId == UserSettings::CT_GLOBAL) { - filePath = (m_exeDirectory / "GlobalUserSettings.xml").String(); + filePath = (AZ::IO::Path(GetTestFolderPath()) / "GlobalUserSettings.xml").Native(); } else if (providerId == UserSettings::CT_LOCAL) { - filePath = (m_exeDirectory / "LocalUserSettings.xml").String(); + filePath = (AZ::IO::Path(GetTestFolderPath()) / "LocalUserSettings.xml").Native(); } return filePath; } @@ -1117,7 +1112,6 @@ namespace UnitTest ComponentApplication::Descriptor appDesc; appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024; Entity* systemEntity = app.Create(appDesc); - app.SetExecutableFolder(GetTestFolderPath().c_str()); app.UserSettingsFileLocatorBus::Handler::BusConnect(); // Make sure user settings file does not exist at this point diff --git a/Code/Framework/AzCore/Tests/Debug.cpp b/Code/Framework/AzCore/Tests/Debug.cpp index 0d6e1a51e0..bd90611af3 100644 --- a/Code/Framework/AzCore/Tests/Debug.cpp +++ b/Code/Framework/AzCore/Tests/Debug.cpp @@ -6,7 +6,6 @@ * */ -#include #include #include #include diff --git a/Code/Framework/AzCore/Tests/EBus/ScheduledEventTests.cpp b/Code/Framework/AzCore/Tests/EBus/ScheduledEventTests.cpp index ec9a09fb09..b9c690edcc 100644 --- a/Code/Framework/AzCore/Tests/EBus/ScheduledEventTests.cpp +++ b/Code/Framework/AzCore/Tests/EBus/ScheduledEventTests.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -26,22 +26,22 @@ namespace UnitTest SetupAllocator(); AZ::NameDictionary::Create(); - m_loggerComponent = new AZ::LoggerSystemComponent; - m_timeComponent = new AZ::TimeSystemComponent; - m_eventSchedulerComponent = new AZ::EventSchedulerSystemComponent; + m_loggerComponent = AZStd::make_unique(); + m_timeSystem = AZStd::make_unique(); + m_eventSchedulerComponent = AZStd::make_unique(); - m_testEvent = new AZ::ScheduledEvent([this] { TestBasicEvent(); }, AZ::Name("UnitTestEvent fire once event")); - m_testRequeue = new AZ::ScheduledEvent([this] { TestAutoRequeuedEvent(); }, AZ::Name("UnitTestEvent auto Requeue")); + m_testEvent = AZStd::make_unique([this] { TestBasicEvent(); }, AZ::Name("UnitTestEvent fire once event")); + m_testRequeue = AZStd::make_unique([this] { TestAutoRequeuedEvent(); }, AZ::Name("UnitTestEvent auto Requeue")); } void TearDown() override { - delete m_testEvent; - delete m_testRequeue; + m_testEvent.reset(); + m_testRequeue.reset(); - delete m_eventSchedulerComponent; - delete m_timeComponent; - delete m_loggerComponent; + m_eventSchedulerComponent.reset(); + m_timeSystem.reset(); + m_loggerComponent.reset(); AZ::NameDictionary::Destroy(); TeardownAllocator(); @@ -60,12 +60,12 @@ namespace UnitTest uint32_t m_basicEventTriggerCount = 0; uint32_t m_requeuedEventTriggerCount = 0; - AZ::ScheduledEvent* m_testEvent = nullptr; - AZ::ScheduledEvent* m_testRequeue = nullptr; + AZStd::unique_ptr m_testEvent; + AZStd::unique_ptr m_testRequeue; - AZ::LoggerSystemComponent* m_loggerComponent = nullptr; - AZ::TimeSystemComponent* m_timeComponent = nullptr; - AZ::EventSchedulerSystemComponent* m_eventSchedulerComponent = nullptr; + AZStd::unique_ptr m_loggerComponent; + AZStd::unique_ptr m_timeSystem; + AZStd::unique_ptr m_eventSchedulerComponent; }; TEST_F(ScheduledEventTests, TestFireOnce) diff --git a/Code/Framework/AzCore/Tests/Math/FrustumTests.cpp b/Code/Framework/AzCore/Tests/Math/FrustumTests.cpp index 294d622a92..31fa6b626f 100644 --- a/Code/Framework/AzCore/Tests/Math/FrustumTests.cpp +++ b/Code/Framework/AzCore/Tests/Math/FrustumTests.cpp @@ -552,7 +552,6 @@ namespace UnitTest box.testCaseName = "BoxShaped"; frustums.push_back(box); - // Default values in a CCamera from Cry_Camera.h FrustumTestCase defaultCameraFrustum; defaultCameraFrustum.nearTopLeft = AZ::Vector3(-0.204621f, 0.200000f, 0.153465f); defaultCameraFrustum.nearTopRight = AZ::Vector3(0.204621f, 0.200000f, 0.153465f); diff --git a/Code/Framework/AzCore/Tests/Math/MathTestData.h b/Code/Framework/AzCore/Tests/Math/MathTestData.h index f87ab8f448..72a98e835d 100644 --- a/Code/Framework/AzCore/Tests/Math/MathTestData.h +++ b/Code/Framework/AzCore/Tests/Math/MathTestData.h @@ -33,6 +33,15 @@ namespace MathTestData AZ::Matrix3x3::CreateScale(AZ::Vector3(0.7f, 1.3f, 0.9f)) }; + static const AZ::Matrix4x4 Matrix4x4s[] = { + AZ::Matrix4x4::CreateIdentity(), + AZ::Matrix4x4::CreateFromQuaternionAndTranslation(AZ::Quaternion(-0.46f, 0.26f, -0.22f, 0.82f), AZ::Vector3(1.0f, 5.0f, 10.0f)), + AZ::Matrix4x4::CreateFromTransform(AZ::Transform::CreateFromMatrix3x3AndTranslation( + AZ::Matrix3x3::CreateScale(AZ::Vector3(1.0f, 2.0f, 3.0f)), AZ::Vector3(2.0f, 4.0f, 6.0f))), + AZ::Matrix4x4::CreateScale(AZ::Vector3(5.0f, 10.0f, 15.0f)), + AZ::Matrix4x4::CreateRotationZ(AZ::DegToRad(45.0f)) + }; + using AxisPair = AZStd::pair; static const AxisPair Axes[] = { { AZ::Constants::Axis::XPositive, AZ::Vector3::CreateAxisX(1.0f) }, diff --git a/Code/Framework/AzCore/Tests/Math/Matrix3x4Tests.cpp b/Code/Framework/AzCore/Tests/Math/Matrix3x4Tests.cpp index 8f8f8ca1e9..20d1107757 100644 --- a/Code/Framework/AzCore/Tests/Math/Matrix3x4Tests.cpp +++ b/Code/Framework/AzCore/Tests/Math/Matrix3x4Tests.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "MathTestData.h" @@ -392,6 +393,32 @@ namespace UnitTest INSTANTIATE_TEST_CASE_P(MATH_Matrix3x4, Matrix3x4CreateFromMatrix3x3Fixture, ::testing::ValuesIn(MathTestData::Matrix3x3s)); + using Matrix3x4CreateFromMatrix4x4Fixture = ::testing::TestWithParam; + + TEST_P(Matrix3x4CreateFromMatrix4x4Fixture, UnsafeCreateFromMatrix4x4) + { + const AZ::Matrix4x4 matrix4x4 = GetParam(); + const AZ::Matrix3x4 matrix3x4 = AZ::Matrix3x4::UnsafeCreateFromMatrix4x4(matrix4x4); + EXPECT_THAT(matrix3x4.GetTranslation(), IsClose(matrix4x4.GetTranslation())); + const AZ::Vector3 vector(2.3f, -0.6, 1.8f); + EXPECT_THAT(matrix3x4.TransformVector(vector), IsClose((matrix4x4 * AZ::Vector3ToVector4(vector, 0.0f)).GetAsVector3())); + const AZ::Vector3 point(12.3f, -5.6, 7.3f); + EXPECT_THAT(matrix3x4.TransformPoint(point), IsClose((matrix4x4 * AZ::Vector3ToVector4(point, 1.0f)).GetAsVector3())); + } + + INSTANTIATE_TEST_CASE_P(MATH_Matrix3x4, Matrix3x4CreateFromMatrix4x4Fixture, ::testing::ValuesIn(MathTestData::Matrix4x4s)); + + TEST(MATH_Matrix3x4, TransformPoint) + { + const AZ::Matrix3x4 matrix3x4 = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation( + AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(90.0f)), AZ::Vector3(5.0f, 0.0f, 0.0f)); + + const AZ::Vector3 result = matrix3x4.TransformPoint(AZ::Vector3(1.0f, 0.0f, 0.0f)); + const AZ::Vector3 expected = AZ::Vector3(5.0f, 0.0f, -1.0f); + + EXPECT_THAT(result, expected); + } + TEST(MATH_Matrix3x4, CreateScale) { const AZ::Vector3 scale(1.7f, 0.3f, 2.4f); diff --git a/Code/Framework/AzCore/Tests/Serialization.cpp b/Code/Framework/AzCore/Tests/Serialization.cpp index f1d5edc490..219744a480 100644 --- a/Code/Framework/AzCore/Tests/Serialization.cpp +++ b/Code/Framework/AzCore/Tests/Serialization.cpp @@ -1240,7 +1240,6 @@ namespace UnitTest SerializeContext* GetSerializeContext() override { return m_serializeContext.get(); } BehaviorContext* GetBehaviorContext() override { return nullptr; } JsonRegistrationContext* GetJsonRegistrationContext() override { return nullptr; } - const char* GetAppRoot() const override { return nullptr; } const char* GetEngineRoot() const override { return nullptr; } const char* GetExecutableFolder() const override { return nullptr; } void EnumerateEntities(const EntityCallback& /*callback*/) override {} diff --git a/Code/Framework/AzCore/Tests/Time/TimeTests.cpp b/Code/Framework/AzCore/Tests/Time/TimeTests.cpp index 6727ef1501..7ba01ec0e0 100644 --- a/Code/Framework/AzCore/Tests/Time/TimeTests.cpp +++ b/Code/Framework/AzCore/Tests/Time/TimeTests.cpp @@ -6,7 +6,7 @@ * */ -#include +#include #include namespace UnitTest @@ -18,16 +18,16 @@ namespace UnitTest void SetUp() override { SetupAllocator(); - m_timeComponent = new AZ::TimeSystemComponent; + m_timeSystem = AZStd::make_unique(); } void TearDown() override { - delete m_timeComponent; + m_timeSystem.reset(); TeardownAllocator(); } - AZ::TimeSystemComponent* m_timeComponent = nullptr; + AZStd::unique_ptr m_timeSystem; }; TEST_F(TimeTests, TestConversionUsToMs) @@ -44,6 +44,30 @@ namespace UnitTest EXPECT_EQ(timeUs, AZ::TimeUs{ 1000000 }); } + TEST_F(TimeTests, TestConversionTimeMsToSeconds) + { + AZ::TimeMs timeMs = AZ::TimeMs{ 1000 }; + float timeSecondsFloat = AZ::TimeMsToSeconds(timeMs); + EXPECT_TRUE(AZ::IsClose(timeSecondsFloat, 1.0f)); + + double timeSecondsDouble = AZ::TimeMsToSecondsDouble(timeMs); + EXPECT_TRUE(AZ::IsClose(timeSecondsDouble, 1.0)); + } + + TEST_F(TimeTests, TestConversionSecondsToTimeUs) + { + double seconds = 1.0; + AZ::TimeUs timeUs = AZ::SecondsToTimeUs(seconds); + EXPECT_EQ(timeUs, AZ::TimeUs{ 1000000 }); + } + + TEST_F(TimeTests, TestConversionSecondsToTimeMs) + { + double seconds = 1.0; + AZ::TimeMs timeMs = AZ::SecondsToTimeMs(seconds); + EXPECT_EQ(timeMs, AZ::TimeMs{ 1000 }); + } + TEST_F(TimeTests, TestClocks) { AZ::TimeUs timeUs = AZ::GetElapsedTimeUs(); diff --git a/Code/Framework/AzFramework/AzFramework/API/ApplicationAPI.h b/Code/Framework/AzFramework/AzFramework/API/ApplicationAPI.h index 1c5db0a82b..e536082d61 100644 --- a/Code/Framework/AzFramework/AzFramework/API/ApplicationAPI.h +++ b/Code/Framework/AzFramework/AzFramework/API/ApplicationAPI.h @@ -67,12 +67,6 @@ namespace AzFramework /// Make path relative to the provided root. virtual void MakePathRelative(AZStd::string& /*fullPath*/, const char* /*rootPath*/) {} - /// Gets the engine root path where the modules for the current engine are located. - virtual const char* GetEngineRoot() const { return nullptr; } - - /// Retrieves the app root path for the application. - virtual const char* GetAppRoot() const { return nullptr; } - /// Get the Command Line arguments passed in. virtual const CommandLine* GetCommandLine() { return nullptr; } diff --git a/Code/Framework/AzFramework/AzFramework/Application/Application.cpp b/Code/Framework/AzFramework/AzFramework/Application/Application.cpp index 323e834413..b3521a877f 100644 --- a/Code/Framework/AzFramework/AzFramework/Application/Application.cpp +++ b/Code/Framework/AzFramework/AzFramework/Application/Application.cpp @@ -69,6 +69,7 @@ #include #include #include +#include #include "Application.h" #include @@ -224,13 +225,6 @@ namespace AzFramework } } - void Application::PreModuleLoad() - { - SetRootPath(RootPathType::EngineRoot, m_engineRoot.c_str()); - AZ_TracePrintf(s_azFrameworkWarningWindow, "Engine Path: %s\n", m_engineRoot.c_str()); - } - - void Application::Stop() { if (m_isStarted) @@ -318,6 +312,8 @@ namespace AzFramework AzFramework::SurfaceData::SurfaceTagWeight::Reflect(context); AzFramework::SurfaceData::SurfacePoint::Reflect(context); AzFramework::Terrain::TerrainDataRequests::Reflect(context); + Physics::HeightfieldProviderRequests::Reflect(context); + Physics::HeightMaterialPoint::Reflect(context); if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) { @@ -394,11 +390,6 @@ namespace AzFramework outModules.emplace_back(aznew AzFrameworkModule()); } - const char* Application::GetAppRoot() const - { - return m_appRoot.c_str(); - } - const char* Application::GetCurrentConfigurationName() const { #if defined(_RELEASE) @@ -434,19 +425,19 @@ namespace AzFramework void Application::ResolveEnginePath(AZStd::string& engineRelativePath) const { - AZ::IO::FixedMaxPath fullPath = m_engineRoot / engineRelativePath; + auto fullPath = AZ::IO::FixedMaxPath(GetEngineRoot()) / engineRelativePath; engineRelativePath = fullPath.String(); } void Application::CalculateBranchTokenForEngineRoot(AZStd::string& token) const { - AzFramework::StringFunc::AssetPath::CalculateBranchToken(m_engineRoot.String(), token); + AZ::StringFunc::AssetPath::CalculateBranchToken(GetEngineRoot(), token); } //////////////////////////////////////////////////////////////////////////// void Application::MakePathRootRelative(AZStd::string& fullPath) { - MakePathRelative(fullPath, m_engineRoot.c_str()); + MakePathRelative(fullPath, GetEngineRoot()); } //////////////////////////////////////////////////////////////////////////// @@ -562,11 +553,9 @@ namespace AzFramework //////////////////////////////////////////////////////////////////////////// - AZ_CVAR(float, t_frameTimeOverride, 0.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "If > 0, overrides the application delta frame-time with the provided value"); - - void Application::Tick(float deltaOverride /*= -1.f*/) + void Application::Tick() { - ComponentApplication::Tick((t_frameTimeOverride > 0.0f) ? t_frameTimeOverride : deltaOverride); + ComponentApplication::Tick(); } //////////////////////////////////////////////////////////////////////////// @@ -582,30 +571,6 @@ namespace AzFramework } } - void Application::SetRootPath(RootPathType type, const char* source) - { - [[maybe_unused]] const size_t sourceLen = strlen(source); - - // Copy the source path to the intended root path and correct the path separators as well - switch (type) - { - case RootPathType::AppRoot: - { - AZ_Assert(sourceLen < m_appRoot.Native().max_size(), "String overflow for App Root: %s", source); - m_appRoot = AZ::IO::PathView(source).LexicallyNormal(); - } - break; - case RootPathType::EngineRoot: - { - AZ_Assert(sourceLen < m_engineRoot.Native().max_size(), "String overflow for Engine Root: %s", source); - m_engineRoot = AZ::IO::PathView(source).LexicallyNormal(); - } - break; - default: - AZ_Assert(false, "Invalid RootPathType (%d)", static_cast(type)); - } - } - struct DeprecatedAliasesKeyVisitor : AZ::SettingsRegistryInterface::Visitor { diff --git a/Code/Framework/AzFramework/AzFramework/Application/Application.h b/Code/Framework/AzFramework/AzFramework/Application/Application.h index c6b1dfeaae..27144e4376 100644 --- a/Code/Framework/AzFramework/AzFramework/Application/Application.h +++ b/Code/Framework/AzFramework/AzFramework/Application/Application.h @@ -87,7 +87,7 @@ namespace AzFramework */ virtual void Stop(); - void Tick(float deltaOverride = -1.f) override; + void Tick() override; AZ::ComponentTypeList GetRequiredSystemComponents() const override; @@ -95,8 +95,6 @@ namespace AzFramework ////////////////////////////////////////////////////////////////////////// //! ApplicationRequests::Bus::Handler - const char* GetEngineRoot() const override { return m_engineRoot.c_str(); } - const char* GetAppRoot() const override; void ResolveEnginePath(AZStd::string& engineRelativePath) const override; void CalculateBranchTokenForEngineRoot(AZStd::string& token) const override; bool IsPrefabSystemEnabled() const override; @@ -146,8 +144,6 @@ namespace AzFramework */ void SetFileIOAliases(); - void PreModuleLoad() override; - ////////////////////////////////////////////////////////////////////////// //! AZ::ComponentApplication void RegisterCoreComponents() override; @@ -181,13 +177,7 @@ namespace AzFramework bool m_ownsConsole = false; bool m_exitMainLoopRequested = false; - - enum class RootPathType - { - AppRoot, - EngineRoot - }; - void SetRootPath(RootPathType type, const char* source); + }; } // namespace AzFramework diff --git a/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp b/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp index c1c5775958..a60a657d87 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -43,8 +42,10 @@ namespace AZ::IO { - AZ_CVAR(int, sys_PakPriority, aznumeric_cast(ArchiveVars{}.nPriority), nullptr, AZ::ConsoleFunctorFlags::Null, - "If set to 1, tells Archive to try to open the file in pak first, then go to file system"); + AZ_CVAR(int, sys_PakPriority, aznumeric_cast(ArchiveVars{}.m_fileSearchPriority), nullptr, AZ::ConsoleFunctorFlags::Null, + "If set to 0, tells Archive to try to open the file on the file system first othewise check mounted paks.\n" + "If set to 1, tells Archive to try to open the file in pak first, then go to file system.\n" + "If set to 2, tells the Archive to only open files from the pak"); AZ_CVAR(int, sys_PakMessageInvalidFileAccess, ArchiveVars{}.nMessageInvalidFileAccess, nullptr, AZ::ConsoleFunctorFlags::Null, "Message Box synchronous file access when in game"); @@ -437,9 +438,9 @@ namespace AZ::IO ////////////////////////////////////////////////////////////////////////// - bool Archive::IsFileExist(AZStd::string_view sFilename, EFileSearchLocation fileLocation) + bool Archive::IsFileExist(AZStd::string_view sFilename, FileSearchLocation fileLocation) { - const AZ::IO::ArchiveLocationPriority nVarPakPriority = GetPakPriority(); + const AZ::IO::FileSearchPriority nVarPakPriority = GetPakPriority(); auto szFullPath = AZ::IO::FileIOBase::GetDirectInstance()->ResolvePath(sFilename); if (!szFullPath) @@ -450,25 +451,25 @@ namespace AZ::IO switch(fileLocation) { - case IArchive::eFileLocation_Any: + case FileSearchLocation::Any: // Search for file based on pak priority switch (nVarPakPriority) { - case ArchiveLocationPriority::ePakPriorityFileFirst: + case FileSearchPriority::FileFirst: return FileIOBase::GetDirectInstance()->Exists(szFullPath->c_str()) || FindPakFileEntry(szFullPath->Native()); - case ArchiveLocationPriority::ePakPriorityPakFirst: + case FileSearchPriority::PakFirst: return FindPakFileEntry(szFullPath->Native()) || IO::FileIOBase::GetDirectInstance()->Exists(szFullPath->c_str()); - case ArchiveLocationPriority::ePakPriorityPakOnly: + case FileSearchPriority::PakOnly: return FindPakFileEntry(szFullPath->Native()); default: - AZ_Assert(false, "PakPriority %d doesn't match a value in the ArchiveLocationPriority enum", + AZ_Assert(false, "PakPriority %d doesn't match a value in the FileSearchPriority enum", aznumeric_cast(nVarPakPriority)); } break; - case IArchive::eFileLocation_InPak: + case FileSearchLocation::InPak: return FindPakFileEntry(szFullPath->Native()); - case IArchive::eFileLocation_OnDisk: - if (nVarPakPriority != ArchiveLocationPriority::ePakPriorityPakOnly) + case FileSearchLocation::OnDisk: + if (nVarPakPriority != FileSearchPriority::PakOnly) { return FileIOBase::GetDirectInstance()->Exists(szFullPath->c_str()); } @@ -485,7 +486,7 @@ namespace AZ::IO ////////////////////////////////////////////////////////////////////////// bool Archive::IsFolder(AZStd::string_view sPath) { - AZStd::fixed_string filePath{ sPath }; + AZ::IO::FixedMaxPath filePath{ sPath }; return AZ::IO::FileIOBase::GetDirectInstance()->IsDirectory(filePath.c_str()); } @@ -515,7 +516,7 @@ namespace AZ::IO // get the priority into local variable to avoid it changing in the course of // this function execution (?) - const ArchiveLocationPriority nVarPakPriority = GetPakPriority(); + const FileSearchPriority nVarPakPriority = GetPakPriority(); AZ::IO::OpenMode nOSFlags = AZ::IO::GetOpenModeFromStringMode(szMode); @@ -628,17 +629,17 @@ namespace AZ::IO switch (nVarPakPriority) { - case ArchiveLocationPriority::ePakPriorityFileFirst: + case FileSearchPriority::FileFirst: { AZ::IO::HandleType fileHandle = OpenFromFileSystem(); return fileHandle != AZ::IO::InvalidHandle ? fileHandle : OpenFromArchive(); } - case ArchiveLocationPriority::ePakPriorityPakFirst: + case FileSearchPriority::PakFirst: { AZ::IO::HandleType fileHandle = OpenFromArchive(); return fileHandle != AZ::IO::InvalidHandle ? fileHandle : OpenFromFileSystem(); } - case ArchiveLocationPriority::ePakPriorityPakOnly: + case FileSearchPriority::PakOnly: { return OpenFromArchive(); } @@ -810,7 +811,7 @@ namespace AZ::IO return 0; } - if (GetPakPriority() == ArchiveLocationPriority::ePakPriorityFileFirst) // if the file system files have priority now.. + if (GetPakPriority() == FileSearchPriority::FileFirst) // if the file system files have priority now.. { IArchive::SignedFileSize nFileSize = GetFileSizeOnDisk(fullPath->Native()); if (nFileSize != IArchive::FILE_NOT_PRESENT) @@ -825,7 +826,7 @@ namespace AZ::IO return pFileEntry->desc.lSizeUncompressed; } - if (bAllowUseFileSystem || GetPakPriority() == ArchiveLocationPriority::ePakPriorityPakFirst) // if the archive files had more priority, we didn't attempt fopen before- try it now + if (bAllowUseFileSystem || GetPakPriority() == FileSearchPriority::PakFirst) // if the archive files had more priority, we didn't attempt fopen before- try it now { IArchive::SignedFileSize nFileSize = GetFileSizeOnDisk(fullPath->Native()); if (nFileSize != IArchive::FILE_NOT_PRESENT) @@ -1023,7 +1024,7 @@ namespace AZ::IO ////////////////////////////////////////////////////////////////////////// - AZ::IO::ArchiveFileIterator Archive::FindFirst(AZStd::string_view pDir, EFileSearchType searchType) + AZ::IO::ArchiveFileIterator Archive::FindFirst(AZStd::string_view pDir, FileSearchLocation searchType) { auto szFullPath = AZ::IO::FileIOBase::GetDirectInstance()->ResolvePath(pDir); if (!szFullPath) @@ -1036,18 +1037,21 @@ namespace AZ::IO bool bAllowUseFileSystem{}; switch (searchType) { - case IArchive::eFileSearchType_AllowInZipsOnly: - bAllowUseFileSystem = false; - bScanZips = true; - break; - case IArchive::eFileSearchType_AllowOnDiskAndInZips: - bAllowUseFileSystem = true; - bScanZips = true; - break; - case IArchive::eFileSearchType_AllowOnDiskOnly: - bAllowUseFileSystem = true; - bScanZips = false; - break; + case FileSearchLocation::InPak: + bAllowUseFileSystem = false; + bScanZips = true; + break; + case FileSearchLocation::Any: + bAllowUseFileSystem = true; + bScanZips = true; + break; + case FileSearchLocation::OnDisk: + bAllowUseFileSystem = true; + bScanZips = false; + break; + default: + AZ_Assert(false, "Invalid search location value supplied"); + break; } AZStd::intrusive_ptr pFindData = aznew AZ::IO::FindData(); @@ -1218,7 +1222,7 @@ namespace AZ::IO else { // [LYN-2376] Remove once legacy slice support is removed - AZStd::vector levelDirs; + AZStd::vector levelDirs; if (addLevels) { @@ -1241,6 +1245,10 @@ namespace AZ::IO m_arrZips.insert(revItZip.base(), desc); + // This lock is for m_arrZips. + // Unlock it now because the modification is complete, and events responding to this signal + // will attempt to lock the same mutex, causing the application to lock up. + lock.unlock(); m_levelOpenEvent.Signal(levelDirs); } @@ -1376,7 +1384,7 @@ namespace AZ::IO return true; } - if (AZ::IO::ArchiveFileIterator fileIterator = FindFirst(pWildcardIn, IArchive::eFileSearchType_AllowOnDiskOnly); fileIterator) + if (AZ::IO::ArchiveFileIterator fileIterator = FindFirst(pWildcardIn, FileSearchLocation::OnDisk); fileIterator) { AZStd::vector files; do @@ -1951,15 +1959,15 @@ namespace AZ::IO } // gets the current archive priority - ArchiveLocationPriority Archive::GetPakPriority() const + FileSearchPriority Archive::GetPakPriority() const { - int pakPriority = aznumeric_cast(ArchiveVars{}.nPriority); + FileSearchPriority pakPriority = ArchiveVars{}.m_fileSearchPriority; if (auto console = AZ::Interface::Get(); console != nullptr) { - [[maybe_unused]] AZ::GetValueResult getCvarResult = console->GetCvarValue("sys_PakPriority", pakPriority); + [[maybe_unused]] AZ::GetValueResult getCvarResult = console->GetCvarValue("sys_PakPriority", reinterpret_cast(pakPriority)); AZ_Error("Archive", getCvarResult == AZ::GetValueResult::Success, "Lookup of 'sys_PakPriority console variable failed with error %s", AZ::GetEnumString(getCvarResult)); } - return static_cast(pakPriority); + return pakPriority; } ////////////////////////////////////////////////////////////////////////// @@ -2026,13 +2034,13 @@ namespace AZ::IO switch (GetPakPriority()) { - case ArchiveLocationPriority::ePakPriorityFileFirst: + case FileSearchPriority::FileFirst: info.m_conflictResolution = AZ::IO::ConflictResolution::PreferFile; break; - case ArchiveLocationPriority::ePakPriorityPakFirst: + case FileSearchPriority::PakFirst: info.m_conflictResolution = AZ::IO::ConflictResolution::PreferArchive; break; - case ArchiveLocationPriority::ePakPriorityPakOnly: + case FileSearchPriority::PakOnly: info.m_conflictResolution = AZ::IO::ConflictResolution::UseArchiveOnly; break; } @@ -2143,13 +2151,13 @@ namespace AZ::IO return manifestInfo; } - AZStd::vector Archive::ScanForLevels(ZipDir::CachePtr pZip) + AZStd::vector Archive::ScanForLevels(ZipDir::CachePtr pZip) { - AZStd::queue scanDirs; - AZStd::vector levelDirs; - AZStd::string currentDir = "levels"; - AZStd::string currentDirPattern; - AZStd::string currentFilePattern; + AZStd::queue scanDirs; + AZStd::vector levelDirs; + AZ::IO::Path currentDir = "levels"; + AZ::IO::Path currentDirPattern; + AZ::IO::Path currentFilePattern; ZipDir::FindDir findDir(pZip); findDir.FindFirst(currentDir.c_str()); @@ -2167,11 +2175,10 @@ namespace AZ::IO scanDirs.pop(); } - currentDirPattern = currentDir + AZ_FILESYSTEM_SEPARATOR_WILDCARD; - currentFilePattern = currentDir + AZ_CORRECT_FILESYSTEM_SEPARATOR_STRING + "level.pak"; + currentDirPattern = currentDir / "*"; + currentFilePattern = currentDir / "level.pak"; - ZipDir::FileEntry* fileEntry = findFile.FindExact(currentFilePattern.c_str()); - if (fileEntry) + if (ZipDir::FileEntry* fileEntry = findFile.FindExact(currentFilePattern); fileEntry) { levelDirs.emplace_back(currentDir); continue; @@ -2179,9 +2186,7 @@ namespace AZ::IO for (findDir.FindFirst(currentDirPattern.c_str()); findDir.GetDirEntry(); findDir.FindNext()) { - AZStd::string_view dirName = findDir.GetDirName(); - AZStd::string dirToAdd = AZStd::string::format("%s/%.*s", currentDir.data(), aznumeric_cast(dirName.size()), dirName.data()); - scanDirs.push(dirToAdd); + scanDirs.push(currentDir / findDir.GetDirName()); } } while (!scanDirs.empty()); diff --git a/Code/Framework/AzFramework/AzFramework/Archive/Archive.h b/Code/Framework/AzFramework/AzFramework/Archive/Archive.h index 279702b433..d429aa8f17 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/Archive.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/Archive.h @@ -207,7 +207,7 @@ namespace AZ::IO uint64_t FTell(AZ::IO::HandleType handle) override; int FFlush(AZ::IO::HandleType handle) override; int FClose(AZ::IO::HandleType handle) override; - AZ::IO::ArchiveFileIterator FindFirst(AZStd::string_view pDir, EFileSearchType searchType = eFileSearchType_AllowInZipsOnly) override; + AZ::IO::ArchiveFileIterator FindFirst(AZStd::string_view pDir, FileSearchLocation searchType = FileSearchLocation::InPak) override; AZ::IO::ArchiveFileIterator FindNext(AZ::IO::ArchiveFileIterator fileIterator) override; bool FindClose(AZ::IO::ArchiveFileIterator fileIterator) override; int FEof(AZ::IO::HandleType handle) override; @@ -219,7 +219,7 @@ namespace AZ::IO bool RemoveDir(AZStd::string_view pName) override; // remove directory from FS (if supported) bool IsAbsPath(AZStd::string_view pPath) override; - bool IsFileExist(AZStd::string_view sFilename, EFileSearchLocation fileLocation = eFileLocation_Any) override; + bool IsFileExist(AZStd::string_view sFilename, FileSearchLocation fileLocation = FileSearchLocation::Any) override; bool IsFolder(AZStd::string_view sPath) override; IArchive::SignedFileSize GetFileSizeOnDisk(AZStd::string_view filename) override; @@ -255,7 +255,7 @@ namespace AZ::IO bool DisableRuntimeFileAccess(bool status, AZStd::thread_id threadId) override; // gets the current archive priority - ArchiveLocationPriority GetPakPriority() const override; + FileSearchPriority GetPakPriority() const override; uint64_t GetFileOffsetOnMedia(AZStd::string_view szName) const override; @@ -305,7 +305,7 @@ namespace AZ::IO AZStd::shared_ptr GetBundleCatalog(ZipDir::CachePtr pZip, const AZStd::string& catalogName); // [LYN-2376] Remove once legacy slice support is removed - AZStd::vector ScanForLevels(ZipDir::CachePtr pZip); + AZStd::vector ScanForLevels(ZipDir::CachePtr pZip); mutable AZStd::shared_mutex m_csOpenFiles; ZipPseudoFileArray m_arrOpenFiles; diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFileIO.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFileIO.cpp index 9e6e1034ea..8cc2b9dfb4 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFileIO.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFileIO.cpp @@ -169,7 +169,7 @@ namespace AZ::IO size = m_archive->FGetSize(filePath, true); if (!size) { - return m_archive->IsFileExist(filePath, IArchive::eFileLocation_Any) ? IO::ResultCode::Success : IO::ResultCode::Error; + return m_archive->IsFileExist(filePath, FileSearchLocation::Any) ? IO::ResultCode::Success : IO::ResultCode::Error; } return IO::ResultCode::Success; diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFindData.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFindData.cpp index 7b483dc5de..9e4290131e 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFindData.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveFindData.cpp @@ -78,9 +78,9 @@ namespace AZ::IO { // get the priority into local variable to avoid it changing in the course of // this function execution - ArchiveLocationPriority nVarPakPriority = archive->GetPakPriority(); + FileSearchPriority nVarPakPriority = archive->GetPakPriority(); - if (nVarPakPriority == ArchiveLocationPriority::ePakPriorityFileFirst) + if (nVarPakPriority == FileSearchPriority::FileFirst) { // first, find the file system files ScanFS(archive, szDir); @@ -96,7 +96,7 @@ namespace AZ::IO { ScanZips(archive, szDir); } - if (bAllowUseFS || nVarPakPriority != ArchiveLocationPriority::ePakPriorityPakOnly) + if (bAllowUseFS || nVarPakPriority != FileSearchPriority::PakOnly) { ScanFS(archive, szDir); } diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.cpp new file mode 100644 index 0000000000..0098d97b8d --- /dev/null +++ b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.cpp @@ -0,0 +1,24 @@ +/* + * 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 + +namespace AZ::IO +{ + FileSearchPriority GetDefaultFileSearchPriority() + { +#if defined(LY_ARCHIVE_FILE_SEARCH_MODE) + return FileSearchPriority{ LY_ARCHIVE_FILE_SEARCH_MODE }; +#else + return FileSearchPriority{ !ArchiveVars::IsReleaseConfig + ? FileSearchPriority::FileFirst + : FileSearchPriority::PakOnly }; +#endif + } +} diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.h b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.h index 931b07fa71..253e3b0c5b 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/ArchiveVars.h @@ -13,13 +13,24 @@ namespace AZ::IO { - enum class ArchiveLocationPriority + enum class FileSearchPriority { - ePakPriorityFileFirst = 0, - ePakPriorityPakFirst = 1, - ePakPriorityPakOnly = 2 + FileFirst, + PakFirst, + PakOnly }; + + //file location enum used in isFileExist to control where the archive system looks for the file. + enum class FileSearchLocation + { + Any, + OnDisk, + InPak + }; + + FileSearchPriority GetDefaultFileSearchPriority(); + // variables that control behavior of the Archive subsystem struct ArchiveVars { @@ -28,8 +39,6 @@ namespace AZ::IO #else inline static constexpr bool IsReleaseConfig{}; #endif - - public: int nReadSlice{}; int nSaveTotalResourceList{}; int nSaveFastloadResourceList{}; @@ -42,9 +51,7 @@ namespace AZ::IO int nLoadCache{}; int nLoadModePaks{}; int nStreamCache{ STREAM_CACHE_DEFAULT }; - ArchiveLocationPriority nPriority{ IsReleaseConfig - ? ArchiveLocationPriority::ePakPriorityPakOnly - : ArchiveLocationPriority::ePakPriorityFileFirst }; // Which file location to favor (loose vs. pak files) + FileSearchPriority m_fileSearchPriority{ GetDefaultFileSearchPriority()}; int nMessageInvalidFileAccess{}; int nLogInvalidFileAccess{ IsReleaseConfig ? 0 : 1 }; int nDisableNonLevelRelatedPaks{ 1 }; diff --git a/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h b/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h index bd9615110a..d7eaee6e24 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h @@ -18,13 +18,13 @@ #include #include - +#include enum EStreamSourceMediaType : int32_t; namespace AZ::IO { - enum class ArchiveLocationPriority; + enum class FileSearchPriority; struct IResourceList; struct INestedArchive; struct IArchive; @@ -114,14 +114,6 @@ namespace AZ::IO RFOM_NextLevel // used for level2level loading }; - //file location enum used in isFileExist to control where the archive system looks for the file. - enum EFileSearchLocation - { - eFileLocation_Any = 0, - eFileLocation_OnDisk, - eFileLocation_InPak, - }; - enum EInMemoryArchiveLocation { eInMemoryPakLocale_Unload = 0, @@ -130,12 +122,6 @@ namespace AZ::IO eInMemoryPakLocale_PAK, }; - enum EFileSearchType - { - eFileSearchType_AllowInZipsOnly = 0, - eFileSearchType_AllowOnDiskAndInZips, - eFileSearchType_AllowOnDiskOnly - }; using SignedFileSize = int64_t; @@ -213,7 +199,7 @@ namespace AZ::IO virtual AZStd::intrusive_ptr PoolAllocMemoryBlock(size_t nSize, const char* sUsage, size_t nAlign = 1) = 0; // Arguments: - virtual ArchiveFileIterator FindFirst(AZStd::string_view pDir, EFileSearchType searchType = eFileSearchType_AllowInZipsOnly) = 0; + virtual ArchiveFileIterator FindFirst(AZStd::string_view pDir, FileSearchLocation searchType = FileSearchLocation::InPak) = 0; virtual ArchiveFileIterator FindNext(AZ::IO::ArchiveFileIterator handle) = 0; virtual bool FindClose(AZ::IO::ArchiveFileIterator handle) = 0; //returns file modification time @@ -221,7 +207,7 @@ namespace AZ::IO // Description: // Checks if specified file exist in filesystem. - virtual bool IsFileExist(AZStd::string_view sFilename, EFileSearchLocation = eFileLocation_Any) = 0; + virtual bool IsFileExist(AZStd::string_view sFilename, FileSearchLocation = FileSearchLocation::Any) = 0; // Checks if path is a folder virtual bool IsFolder(AZStd::string_view sPath) = 0; @@ -283,7 +269,7 @@ namespace AZ::IO virtual bool DisableRuntimeFileAccess(bool status, AZStd::thread_id threadId) = 0; // gets the current pak priority - virtual ArchiveLocationPriority GetPakPriority() const = 0; + virtual FileSearchPriority GetPakPriority() const = 0; // Summary: // Return offset in archive file (ideally has to return offset on DVD) for streaming requests sorting @@ -295,7 +281,7 @@ namespace AZ::IO // Event sent when a archive file is opened that contains a level.pak // @param const AZStd::vector& - Array of directories containing level.pak files - using LevelPackOpenEvent = AZ::Event&>; + using LevelPackOpenEvent = AZ::Event&>; virtual auto GetLevelPackOpenEvent()->LevelPackOpenEvent* = 0; // Event sent when a archive contains a level.pak is closed // @param const AZStd::string_view - Name of the pak file that was closed diff --git a/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.cpp b/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.cpp index 43a5e8fdb3..b4da5745be 100644 --- a/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.cpp +++ b/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.cpp @@ -7,23 +7,153 @@ */ #include +#include +#include #include namespace AzFramework { - - const int AssetBundleManifest::CurrentBundleVersion = 2; + // Redirects writing of the AssetBundleManifest to an older version if the bundle version + // is not set to the current version + static void OldBundleManifestWriter(AZ::SerializeContext::EnumerateInstanceCallContext& callContext, const void* bundleManifestPointer, + const AZ::SerializeContext::ClassData&, const AZ::SerializeContext::ClassElement* assetBundleManifestClassElement); + + static bool BundleManifestVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement); + + const int AssetBundleManifest::CurrentBundleVersion = 3; const char AssetBundleManifest::s_manifestFileName[] = "manifest.xml"; + + AssetBundleManifest::AssetBundleManifest() = default; + AssetBundleManifest::~AssetBundleManifest() = default; + void AssetBundleManifest::ReflectSerialize(AZ::SerializeContext* serializeContext) { if (serializeContext) { serializeContext->Class() - ->Version(2) + ->Version(CurrentBundleVersion, &BundleManifestVersionConverter) + ->Attribute(AZ::SerializeContextAttributes::ObjectStreamWriteElementOverride, &OldBundleManifestWriter) ->Field("BundleVersion", &AssetBundleManifest::m_bundleVersion) ->Field("CatalogName", &AssetBundleManifest::m_catalogName) - ->Field("DependentBundleNames", &AssetBundleManifest::m_depedendentBundleNames) + ->Field("DependentBundleNames", &AssetBundleManifest::m_dependentBundleNames) ->Field("LevelNames", &AssetBundleManifest::m_levelDirs); + + // Make sure the AZStd::vector type is reflected so that it can be read + // using DataElement::GetChildData + serializeContext->RegisterGenericType>(); } } + + bool BundleManifestVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement) + { + if (rootElement.GetVersion() < 3) + { + static constexpr AZ::u32 levelNamesCrc = AZ_CRC_CE("LevelNames"); + AZStd::vector newLevelDirs; + if (AZStd::vector oldLevelNames; rootElement.GetChildData(levelNamesCrc, oldLevelNames)) + { + newLevelDirs.insert(newLevelDirs.end(), + AZStd::make_move_iterator(oldLevelNames.begin()), AZStd::make_move_iterator(oldLevelNames.end())); + } + else + { + AZ_Error("AssetBundleManifest", false, R"(Unable to read "levelNames" from AssetBundleManifest version %u )", + rootElement.GetVersion()); + } + + rootElement.RemoveElementByName(levelNamesCrc); + rootElement.AddElementWithData(context, "LevelNames", newLevelDirs); + } + return true; + } + + void OldBundleManifestWriter(AZ::SerializeContext::EnumerateInstanceCallContext& callContext, const void* bundleManifestPointer, + const AZ::SerializeContext::ClassData&, const AZ::SerializeContext::ClassElement* assetBundleManifestClassElement) + { + // Copy the AssetBundleManifest current version instance to the AssetBundleManifest V2 instance + auto assetBundleManifestCurrent = reinterpret_cast(bundleManifestPointer); + if (assetBundleManifestCurrent->GetBundleVersion() <= 2) + { + auto serializeContext = const_cast(callContext.m_context); + + struct AssetBundleManifestV2 + { + // Use the same ClassName and typeid as the AssetBundleManifest + AZ_TYPE_INFO(AssetBundleManifest, azrtti_typeid()); + AZStd::string m_catalogName; + AZStd::vector m_dependentBundleNames; + AZStd::vector m_levelDirs; + int m_bundleVersion{}; + }; + auto ReflectAssetBundleManifestV2 = [](AZ::SerializeContext* serializeContext) + { + serializeContext->Class() + ->Version(2) + ->Field("BundleVersion", &AssetBundleManifestV2::m_bundleVersion) + ->Field("CatalogName", &AssetBundleManifestV2::m_catalogName) + ->Field("DependentBundleNames", &AssetBundleManifestV2::m_dependentBundleNames) + ->Field("LevelNames", &AssetBundleManifestV2::m_levelDirs); + }; + + // Unreflect the AssetBundleManifest class at the version since it shares the same typeid + // as the older version and Reflect the V2 AssetBundlerManifest + serializeContext->EnableRemoveReflection(); + AssetBundleManifest::ReflectSerialize(serializeContext); + serializeContext->DisableRemoveReflection(); + ReflectAssetBundleManifestV2(serializeContext); + + // Use the Current AssetBundleManifest instance to make a Version 2 AssetBundleManifest + AssetBundleManifestV2 assetBundleManifestV2; + assetBundleManifestV2.m_catalogName = assetBundleManifestCurrent->GetCatalogName(); + assetBundleManifestV2.m_dependentBundleNames = assetBundleManifestCurrent->GetDependentBundleNames(); + assetBundleManifestV2.m_bundleVersion = assetBundleManifestCurrent->GetBundleVersion(); + for (const AZ::IO::Path& levelDir : assetBundleManifestCurrent->GetLevelDirectories()) + { + assetBundleManifestV2.m_levelDirs.emplace_back(levelDir.Native()); + } + + const AZ::TypeId& assetBundlerManifestTypeId = azrtti_typeid(); + const auto assetBundleManifestV2ClassData = serializeContext->FindClassData(assetBundlerManifestTypeId); + + // Create an AssetBundleManifest Version 2 Class Eleemnt + // It will copy over the name and nameCrc values of the current AssetBundleManifestelemnt + auto CreateAssetBundleManifestV2ClassElement = [&assetBundlerManifestTypeId]( + const AZ::SerializeContext::ClassElement* currentVersionElement) -> AZ::SerializeContext::ClassElement + { + AZ::SerializeContext::ClassElement v2ClassElement; + // Copy over the name of he current + if (currentVersionElement) + { + v2ClassElement.m_name = currentVersionElement->m_name; + v2ClassElement.m_nameCrc = currentVersionElement->m_nameCrc; + } + v2ClassElement.m_dataSize = sizeof(AssetBundleManifest); + v2ClassElement.m_azRtti = AZ::GetRttiHelper(); + v2ClassElement.m_genericClassInfo = nullptr; + v2ClassElement.m_typeId = assetBundlerManifestTypeId; + v2ClassElement.m_editData = nullptr; + v2ClassElement.m_attributeOwnership = AZ::SerializeContext::ClassElement::AttributeOwnership::Self; + return v2ClassElement; + }; + const auto assetBundleManifestV2ClassElement = CreateAssetBundleManifestV2ClassElement(assetBundleManifestClassElement); + + serializeContext->EnumerateInstanceConst(&callContext, &assetBundleManifestV2, assetBundlerManifestTypeId, + assetBundleManifestV2ClassData, assetBundleManifestClassElement ? &assetBundleManifestV2ClassElement : nullptr); + + // Unreflect the V2 AssetBundleManifest and Re-reflect the AssetBundleManifest class at the current version + serializeContext->EnableRemoveReflection(); + ReflectAssetBundleManifestV2(serializeContext); + serializeContext->DisableRemoveReflection(); + AssetBundleManifest::ReflectSerialize(serializeContext); + } + } + + const AZStd::vector& AssetBundleManifest::GetLevelDirectories() const + { + return m_levelDirs; + } + void AssetBundleManifest::SetLevelsDirectory(const AZStd::vector& levelDirs) + { + m_levelDirs = levelDirs; + } } // namespace AzFramework diff --git a/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.h b/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.h index 9760482231..eb0390a8c0 100644 --- a/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.h +++ b/Code/Framework/AzFramework/AzFramework/Asset/AssetBundleManifest.h @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -27,7 +28,8 @@ namespace AzFramework AZ_TYPE_INFO(AssetBundleManifest, "{8628A669-7B19-4C48-A7CB-F670CC9586FD}"); AZ_CLASS_ALLOCATOR(AssetBundleManifest, AZ::SystemAllocator, 0); - AssetBundleManifest() = default; + AssetBundleManifest(); + ~AssetBundleManifest(); static void ReflectSerialize(AZ::SerializeContext* serializeContext); @@ -35,21 +37,21 @@ namespace AzFramework // of files within the AssetBundle in order to update the Asset Registry at runtime when // loading the bundle const AZStd::string& GetCatalogName() const { return m_catalogName; } - AZStd::vector GetDependentBundleNames() const { return m_depedendentBundleNames; } - AZStd::vector GetLevelDirectories() const { return m_levelDirs; } + AZStd::vector GetDependentBundleNames() const { return m_dependentBundleNames; } + const AZStd::vector& GetLevelDirectories() const; int GetBundleVersion() const { return m_bundleVersion; } void SetCatalogName(const AZStd::string& catalogName) { m_catalogName = catalogName; } void SetBundleVersion(int bundleVersion) { m_bundleVersion = bundleVersion; } - void SetDependentBundleNames(const AZStd::vector& dependentBundleNames) { m_depedendentBundleNames = dependentBundleNames; } - void SetLevelsDirectory(const AZStd::vector& levelDirs) { m_levelDirs = levelDirs; } + void SetDependentBundleNames(const AZStd::vector& dependentBundleNames) { m_dependentBundleNames = dependentBundleNames; } + void SetLevelsDirectory(const AZStd::vector& levelDirs); static const char s_manifestFileName[]; static const int CurrentBundleVersion; - private: + private: AZStd::string m_catalogName; - AZStd::vector m_depedendentBundleNames; - AZStd::vector m_levelDirs; + AZStd::vector m_dependentBundleNames; + AZStd::vector m_levelDirs; int m_bundleVersion = CurrentBundleVersion; }; diff --git a/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.cpp b/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.cpp index 3d76bcefc4..0c6b195434 100644 --- a/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.cpp +++ b/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.cpp @@ -1186,7 +1186,7 @@ namespace AzFramework } - bool AssetCatalog::CreateBundleManifest(const AZStd::string& deltaCatalogPath, const AZStd::vector& dependentBundleNames, const AZStd::string& fileDirectory, int bundleVersion, const AZStd::vector& levelDirs) + bool AssetCatalog::CreateBundleManifest(const AZStd::string& deltaCatalogPath, const AZStd::vector& dependentBundleNames, const AZStd::string& fileDirectory, int bundleVersion, const AZStd::vector& levelDirs) { if (bundleVersion > AzFramework::AssetBundleManifest::CurrentBundleVersion || bundleVersion < 0) { diff --git a/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.h b/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.h index 64f3c2e3d2..20f1355e7f 100644 --- a/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.h +++ b/Code/Framework/AzFramework/AzFramework/Asset/AssetCatalog.h @@ -67,7 +67,7 @@ namespace AzFramework bool InsertDeltaCatalogBefore(AZStd::shared_ptr deltaCatalog, AZStd::shared_ptr afterDeltaCatalog) override; bool RemoveDeltaCatalog(AZStd::shared_ptr deltaCatalog) override; static bool SaveAssetBundleManifest(const char* assetBundleManifestFile, AzFramework::AssetBundleManifest* bundleManifest); - bool CreateBundleManifest(const AZStd::string& deltaCatalogPath, const AZStd::vector& dependentBundleNames, const AZStd::string& fileDirectory, int bundleVersion, const AZStd::vector& levelDirs) override; + bool CreateBundleManifest(const AZStd::string& deltaCatalogPath, const AZStd::vector& dependentBundleNames, const AZStd::string& fileDirectory, int bundleVersion, const AZStd::vector& levelDirs) override; bool CreateDeltaCatalog(const AZStd::vector& files, const AZStd::string& filePath) override; void AddExtension(const char* extension) override; diff --git a/Code/Framework/AzFramework/AzFramework/Components/TransformComponent.cpp b/Code/Framework/AzFramework/AzFramework/Components/TransformComponent.cpp index bc109b73c2..809f664a10 100644 --- a/Code/Framework/AzFramework/AzFramework/Components/TransformComponent.cpp +++ b/Code/Framework/AzFramework/AzFramework/Components/TransformComponent.cpp @@ -534,7 +534,7 @@ namespace AzFramework void TransformComponent::SetParentImpl(AZ::EntityId parentId, bool isKeepWorldTM) { - if (parentId == GetEntityId()) + if (GetEntity() && parentId == GetEntityId()) { AZ_Warning("TransformComponent", false, "An entity can not be set as its own parent."); return; diff --git a/Code/Framework/AzFramework/AzFramework/Entity/EntityDebugDisplayBus.h b/Code/Framework/AzFramework/AzFramework/Entity/EntityDebugDisplayBus.h index 49506364f4..b81a0ab7f0 100644 --- a/Code/Framework/AzFramework/AzFramework/Entity/EntityDebugDisplayBus.h +++ b/Code/Framework/AzFramework/AzFramework/Entity/EntityDebugDisplayBus.h @@ -76,9 +76,12 @@ namespace AzFramework virtual void DrawSolidCone(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, float height, bool drawShaded = true) { (void)pos; (void)dir; (void)radius; (void)height; (void)drawShaded; } virtual void DrawWireCylinder(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height) { (void)center; (void)axis; (void)radius; (void)height; } virtual void DrawSolidCylinder(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height, bool drawShaded = true) { (void)center; (void)axis; (void)radius; (void)height; (void)drawShaded; } + virtual void DrawWireCylinderNoEnds(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height) { (void)center; (void)axis; (void)radius; (void)height; } + virtual void DrawSolidCylinderNoEnds(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height, bool drawShaded = true) { (void)center; (void)axis; (void)radius; (void)height; (void)drawShaded; } virtual void DrawWireCapsule(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float heightStraightSection) { (void)center; (void)axis; (void)radius; (void)heightStraightSection; } virtual void DrawWireSphere(const AZ::Vector3& pos, float radius) { (void)pos; (void)radius; } virtual void DrawWireSphere(const AZ::Vector3& pos, const AZ::Vector3 radius) { (void)pos; (void)radius; } + virtual void DrawWireHemisphere(const AZ::Vector3& pos, const AZ::Vector3& axis, float radius) { (void)pos; (void)axis; (void)radius; } virtual void DrawWireDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius) { (void)pos; (void)dir; (void)radius; } virtual void DrawBall(const AZ::Vector3& pos, float radius, bool drawShaded = true) { (void)pos; (void)radius; (void)drawShaded; } virtual void DrawDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius) { (void)pos; (void)dir; (void)radius; } diff --git a/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.cpp b/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.cpp new file mode 100644 index 0000000000..38fac9655c --- /dev/null +++ b/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.cpp @@ -0,0 +1,46 @@ +/* + * 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 "HeightfieldProviderBus.h" +#include +#include +#include + +namespace Physics +{ + void HeightfieldProviderRequests::Reflect(AZ::ReflectContext* context) + { + if (AZ::BehaviorContext* behaviorContext = azrtti_cast(context)) + { + behaviorContext->EBus("HeightfieldProviderRequestsBus") + ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) + ->Attribute(AZ::Script::Attributes::Module, "physics") + ->Attribute(AZ::Script::Attributes::Category, "PhysX") + ->Event("GetHeightfieldGridSpacing", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldGridSpacing) + ->Event("GetHeightfieldAabb", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldAabb) + ->Event("GetHeightfieldTransform", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldTransform) + ->Event("GetMaterialList", &Physics::HeightfieldProviderRequestsBus::Events::GetMaterialList) + ->Event("GetHeights", &Physics::HeightfieldProviderRequestsBus::Events::GetHeights) + ->Event("GetHeightsAndMaterials", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightsAndMaterials) + ->Event("GetHeightfieldMinHeight", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldMinHeight) + ->Event("GetHeightfieldMaxHeight", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldMaxHeight) + ->Event("GetHeightfieldGridColumns", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldGridColumns) + ->Event("GetHeightfieldGridRows", &Physics::HeightfieldProviderRequestsBus::Events::GetHeightfieldGridRows) + ; + } + } + + void HeightMaterialPoint::Reflect(AZ::ReflectContext* context) + { + if (AZ::BehaviorContext* behaviorContext = azrtti_cast(context)) + { + behaviorContext->Class()->Attribute(AZ::Script::Attributes::Category, "Physics"); + } + } + +} // namespace Physics diff --git a/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.h b/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.h index 73523ee1ba..da361f0a2b 100644 --- a/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.h +++ b/Code/Framework/AzFramework/AzFramework/Physics/HeightfieldProviderBus.h @@ -26,10 +26,25 @@ namespace Physics struct HeightMaterialPoint { + HeightMaterialPoint( + float height = 0.0f, QuadMeshType type = QuadMeshType::SubdivideUpperLeftToBottomRight, uint8_t index = 0) + : m_height(height) + , m_quadMeshType(type) + , m_materialIndex(index) + , m_padding(0) + { + } + + virtual ~HeightMaterialPoint() = default; + + static void Reflect(AZ::ReflectContext* context); + + AZ_RTTI(HeightMaterialPoint, "{DF167ED4-24E6-4F7B-8AB7-42622F7DBAD3}"); float m_height{ 0.0f }; //!< Holds the height of this point in the heightfield relative to the heightfield entity location. QuadMeshType m_quadMeshType{ QuadMeshType::SubdivideUpperLeftToBottomRight }; //!< By default, create two triangles like this |\|, where this point is in the upper left corner. uint8_t m_materialIndex{ 0 }; //!< The surface material index for the upper left corner of this quad. uint16_t m_padding{ 0 }; //!< available for future use. + }; //! An interface to provide heightfield values. @@ -37,6 +52,8 @@ namespace Physics : public AZ::ComponentBus { public: + static void Reflect(AZ::ReflectContext* context); + //! Returns the distance between each height in the map. //! @return Vector containing Column Spacing, Rows Spacing. virtual AZ::Vector2 GetHeightfieldGridSpacing() const = 0; @@ -46,11 +63,27 @@ namespace Physics //! @param numRows contains the size of the grid in the y direction. virtual void GetHeightfieldGridSize(int32_t& numColumns, int32_t& numRows) const = 0; + //! Returns the height field gridsize columns. + //! @return the size of the grid in the x direction. + virtual int32_t GetHeightfieldGridColumns() const = 0; + + //! Returns the height field gridsize rows. + //! @return the size of the grid in the y direction. + virtual int32_t GetHeightfieldGridRows() const = 0; + //! Returns the height field min and max height bounds. //! @param minHeightBounds contains the minimum height that the heightfield can contain. //! @param maxHeightBounds contains the maximum height that the heightfield can contain. virtual void GetHeightfieldHeightBounds(float& minHeightBounds, float& maxHeightBounds) const = 0; + //! Returns the height field min height bounds. + //! @return the minimum height that the heightfield can contain. + virtual float GetHeightfieldMinHeight() const = 0; + + //! Returns the height field max height bounds. + //! @return the maximum height that the heightfield can contain. + virtual float GetHeightfieldMaxHeight() const = 0; + //! Returns the AABB of the heightfield. //! This is provided separately from the shape AABB because the heightfield might choose to modify the AABB bounds. //! @return AABB of the heightfield. diff --git a/Code/Framework/AzFramework/AzFramework/Physics/Material.cpp b/Code/Framework/AzFramework/AzFramework/Physics/Material.cpp index 222bc48dda..d78d4e0943 100644 --- a/Code/Framework/AzFramework/AzFramework/Physics/Material.cpp +++ b/Code/Framework/AzFramework/AzFramework/Physics/Material.cpp @@ -360,6 +360,11 @@ namespace Physics ->Field("MaterialId", &Physics::MaterialId::m_id) ; } + + if (AZ::BehaviorContext* behaviorContext = azrtti_cast(context)) + { + behaviorContext->Class()->Attribute(AZ::Script::Attributes::Category, "Physics"); + } } MaterialId MaterialId::Create() diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.cpp b/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.cpp index 26c9db64aa..771884ac6a 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.cpp +++ b/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.cpp @@ -94,27 +94,27 @@ namespace AzFramework float y; float z; - // 2.4 Factor as RzRyRx - if (orientation.GetElement(2, 0) < 1.0f) + // 2.5 Factor as RzRxRy + if (orientation.GetElement(2, 1) < 1.0f) { - if (orientation.GetElement(2, 0) > -1.0f) + if (orientation.GetElement(2, 1) > -1.0f) { - x = AZStd::atan2(orientation.GetElement(2, 1), orientation.GetElement(2, 2)); - y = AZStd::asin(-orientation.GetElement(2, 0)); - z = AZStd::atan2(orientation.GetElement(1, 0), orientation.GetElement(0, 0)); + x = AZStd::asin(orientation.GetElement(2, 1)); + y = AZStd::atan2(-orientation.GetElement(2, 0), orientation.GetElement(2, 2)); + z = AZStd::atan2(-orientation.GetElement(0, 1), orientation.GetElement(1, 1)); } else { - x = 0.0f; - y = AZ::Constants::Pi * 0.5f; - z = -AZStd::atan2(-orientation.GetElement(2, 1), orientation.GetElement(1, 1)); + x = -AZ::Constants::Pi * 0.5f; + y = 0.0f; + z = -AZStd::atan2(orientation.GetElement(0, 2), orientation.GetElement(0, 0)); } } else { - x = 0.0f; - y = -AZ::Constants::Pi * 0.5f; - z = AZStd::atan2(-orientation.GetElement(1, 2), orientation.GetElement(1, 1)); + x = AZ::Constants::Pi * 0.5f; + y = 0.0f; + z = AZStd::atan2(orientation.GetElement(0, 2), orientation.GetElement(0, 0)); } return { x, y, z }; @@ -122,14 +122,36 @@ namespace AzFramework void UpdateCameraFromTransform(Camera& camera, const AZ::Transform& transform) { - const auto eulerAngles = AzFramework::EulerAngles(AZ::Matrix3x3::CreateFromTransform(transform)); + UpdateCameraFromTranslationAndRotation( + camera, transform.GetTranslation(), AzFramework::EulerAngles(AZ::Matrix3x3::CreateFromTransform(transform))); + } + void UpdateCameraFromTranslationAndRotation(Camera& camera, const AZ::Vector3& translation, const AZ::Vector3& eulerAngles) + { camera.m_pitch = eulerAngles.GetX(); camera.m_yaw = eulerAngles.GetZ(); - camera.m_pivot = transform.GetTranslation(); + camera.m_pivot = translation; camera.m_offset = AZ::Vector3::CreateZero(); } + float SmoothValueTime(const float smoothness, float deltaTime) + { + // note: the math for the lerp smoothing implementation for camera rotation and translation was inspired by this excellent + // article by Scott Lembcke: https://www.gamasutra.com/blogs/ScottLembcke/20180404/316046/Improved_Lerp_Smoothing.php + const float rate = AZStd::exp2(smoothness); + return AZStd::exp2(-rate * deltaTime); + } + + float SmoothValue(const float target, const float current, const float time) + { + return AZ::Lerp(target, current, time); + } + + float SmoothValue(const float target, const float current, const float smoothness, const float deltaTime) + { + return SmoothValue(target, current, SmoothValueTime(smoothness, deltaTime)); + } + bool CameraSystem::HandleEvents(const InputEvent& event) { if (const auto& cursor = AZStd::get_if(&event)) @@ -291,6 +313,11 @@ namespace AzFramework { return false; }; + + m_constrainPitch = []() constexpr + { + return true; + }; } bool RotateCameraInput::HandleEvents(const InputEvent& event, const ScreenVector& cursorDelta, [[maybe_unused]] const float scrollDelta) @@ -312,7 +339,10 @@ namespace AzFramework nextCamera.m_yaw -= float(cursorDelta.m_x) * rotateSpeed * Invert(m_invertYawFn()); nextCamera.m_yaw = WrapYawRotation(nextCamera.m_yaw); - nextCamera.m_pitch = ClampPitchRotation(nextCamera.m_pitch); + if (m_constrainPitch()) + { + nextCamera.m_pitch = ClampPitchRotation(nextCamera.m_pitch); + } return nextCamera; } @@ -726,14 +756,14 @@ namespace AzFramework Camera SmoothCamera(const Camera& currentCamera, const Camera& targetCamera, const CameraProps& cameraProps, const float deltaTime) { - const auto clamp_rotation = [](const float angle) + const auto clampRotation = [](const float angle) { return AZStd::fmod(angle + AZ::Constants::TwoPi, AZ::Constants::TwoPi); }; // keep yaw in 0 - 360 range - float targetYaw = clamp_rotation(targetCamera.m_yaw); - const float currentYaw = clamp_rotation(currentCamera.m_yaw); + float targetYaw = clampRotation(targetCamera.m_yaw); + const float currentYaw = clampRotation(currentCamera.m_yaw); // return the sign of the float input (-1, 0, 1) const auto sign = [](const float value) @@ -742,21 +772,17 @@ namespace AzFramework }; // ensure smooth transition when moving across 0 - 360 boundary - const float yawDelta = targetYaw - currentYaw; - if (AZStd::abs(yawDelta) >= AZ::Constants::Pi) + if (const float yawDelta = targetYaw - currentYaw; AZStd::abs(yawDelta) >= AZ::Constants::Pi) { targetYaw -= AZ::Constants::TwoPi * sign(yawDelta); } Camera camera; - // note: the math for the lerp smoothing implementation for camera rotation and translation was inspired by this excellent - // article by Scott Lembcke: https://www.gamasutra.com/blogs/ScottLembcke/20180404/316046/Improved_Lerp_Smoothing.php if (cameraProps.m_rotateSmoothingEnabledFn()) { - const float lookRate = AZStd::exp2(cameraProps.m_rotateSmoothnessFn()); - const float lookTime = AZStd::exp2(-lookRate * deltaTime); - camera.m_pitch = AZ::Lerp(targetCamera.m_pitch, currentCamera.m_pitch, lookTime); - camera.m_yaw = AZ::Lerp(targetYaw, currentYaw, lookTime); + const float lookTime = SmoothValueTime(cameraProps.m_rotateSmoothnessFn(), deltaTime); + camera.m_pitch = SmoothValue(targetCamera.m_pitch, currentCamera.m_pitch, lookTime); + camera.m_yaw = SmoothValue(targetYaw, currentYaw, lookTime); } else { @@ -766,8 +792,7 @@ namespace AzFramework if (cameraProps.m_translateSmoothingEnabledFn()) { - const float moveRate = AZStd::exp2(cameraProps.m_translateSmoothnessFn()); - const float moveTime = AZStd::exp2(-moveRate * deltaTime); + const float moveTime = SmoothValueTime(cameraProps.m_rotateSmoothnessFn(), deltaTime); camera.m_pivot = targetCamera.m_pivot.Lerp(currentCamera.m_pivot, moveTime); camera.m_offset = targetCamera.m_offset.Lerp(currentCamera.m_offset, moveTime); } diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.h b/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.h index 2d02bb0b6d..4eea94cbe7 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/CameraInput.h @@ -85,6 +85,19 @@ namespace AzFramework //! Extracts Euler angles (orientation) and translation from the transform and writes the values to the camera. void UpdateCameraFromTransform(Camera& camera, const AZ::Transform& transform); + //! Writes the translation value and Euler angles to the camera. + void UpdateCameraFromTranslationAndRotation(Camera& camera, const AZ::Vector3& translation, const AZ::Vector3& eulerAngles); + + //! Returns the time ('t') input value to use with SmoothValue. + //! Useful if it is to be reused for multiple calls to SmoothValue. + float SmoothValueTime(float smoothness, float deltaTime); + + // Smoothly interpolate a value from current to target according to a smoothing parameter. + float SmoothValue(float target, float current, float smoothness, float deltaTime); + + // Overload of SmoothValue that takes time ('t') value directly. + float SmoothValue(float target, float current, float time); + //! Generic motion type. template struct MotionEvent @@ -334,6 +347,7 @@ namespace AzFramework AZStd::function m_rotateSpeedFn; AZStd::function m_invertPitchFn; AZStd::function m_invertYawFn; + AZStd::function m_constrainPitch; private: InputChannelId m_rotateChannelId; //!< Input channel to begin the rotate camera input. diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.cpp b/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.cpp index 00cbda7b34..40fb82ca97 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.cpp +++ b/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.cpp @@ -8,18 +8,18 @@ #include "CameraState.h" -#include #include #include +#include namespace AzFramework { void SetCameraClippingVolume( - AzFramework::CameraState& cameraState, const float nearPlane, const float farPlane, const float fovRad) + AzFramework::CameraState& cameraState, const float nearPlane, const float farPlane, const float verticalFovRad) { cameraState.m_nearClip = nearPlane; cameraState.m_farClip = farPlane; - cameraState.m_fovOrZoom = fovRad; + cameraState.m_fovOrZoom = verticalFovRad; } void SetCameraTransform(CameraState& cameraState, const AZ::Transform& transform) @@ -35,20 +35,34 @@ namespace AzFramework SetCameraClippingVolume(cameraState, 0.1f, 1000.0f, AZ::DegToRad(60.0f)); } - AzFramework::CameraState CreateDefaultCamera( - const AZ::Transform& transform, const AZ::Vector2& viewportSize) + CameraState CreateCamera( + const AZ::Transform& transform, + const float nearPlane, + const float farPlane, + const float verticalFovRad, + const AZ::Vector2& viewportSize) { AzFramework::CameraState cameraState; - SetDefaultCameraClippingVolume(cameraState); SetCameraTransform(cameraState, transform); + SetCameraClippingVolume(cameraState, nearPlane, farPlane, verticalFovRad); cameraState.m_viewportSize = viewportSize; return cameraState; } - AzFramework::CameraState CreateIdentityDefaultCamera( - const AZ::Vector3& position, const AZ::Vector2& viewportSize) + AzFramework::CameraState CreateDefaultCamera(const AZ::Transform& transform, const AZ::Vector2& viewportSize) + { + AzFramework::CameraState cameraState; + + SetCameraTransform(cameraState, transform); + SetDefaultCameraClippingVolume(cameraState); + cameraState.m_viewportSize = viewportSize; + + return cameraState; + } + + AzFramework::CameraState CreateIdentityDefaultCamera(const AZ::Vector3& position, const AZ::Vector2& viewportSize) { return CreateDefaultCamera(AZ::Transform::CreateTranslation(position), viewportSize); } @@ -89,15 +103,15 @@ namespace AzFramework void CameraState::Reflect(AZ::SerializeContext& serializeContext) { - serializeContext.Class()-> - Field("Position", &CameraState::m_position)-> - Field("Forward", &CameraState::m_forward)-> - Field("Side", &CameraState::m_side)-> - Field("Up", &CameraState::m_up)-> - Field("ViewportSize", &CameraState::m_viewportSize)-> - Field("NearClip", &CameraState::m_nearClip)-> - Field("FarClip", &CameraState::m_farClip)-> - Field("FovZoom", &CameraState::m_fovOrZoom)-> - Field("Ortho", &CameraState::m_orthographic); + serializeContext.Class() + ->Field("Position", &CameraState::m_position) + ->Field("Forward", &CameraState::m_forward) + ->Field("Side", &CameraState::m_side) + ->Field("Up", &CameraState::m_up) + ->Field("ViewportSize", &CameraState::m_viewportSize) + ->Field("NearClip", &CameraState::m_nearClip) + ->Field("FarClip", &CameraState::m_farClip) + ->Field("FovZoom", &CameraState::m_fovOrZoom) + ->Field("Ortho", &CameraState::m_orthographic); } } // namespace AzFramework diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.h b/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.h index e174771c3b..cefb144ec1 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/CameraState.h @@ -40,10 +40,14 @@ namespace AzFramework AZ::Vector2 m_viewportSize = AZ::Vector2::CreateZero(); //!< Dimensions of the viewport. float m_nearClip = 0.01f; //!< Near clip plane of the camera. float m_farClip = 100.0f; //!< Far clip plane of the camera. - float m_fovOrZoom = 0.0f; //!< Fov or zoom of camera depending on if it is using orthographic projection or not. + float m_fovOrZoom = 0.0f; //!< Vertical fov or zoom of camera depending on if it is using orthographic projection or not. bool m_orthographic = false; //!< Is the camera using orthographic projection or not. }; + //! Create a camera at the given transform, specifying the near and far clip planes as well as the fov with a specific viewport size. + CameraState CreateCamera( + const AZ::Transform& transform, float nearPlane, float farPlane, float verticalFovRad, const AZ::Vector2& viewportSize); + //! Create a camera at the given transform with a specific viewport size. //! @note The near/far clip planes and fov are sensible default values - please //! use SetCameraClippingVolume to override them. @@ -60,7 +64,7 @@ namespace AzFramework CameraState CreateCameraFromWorldFromViewMatrix(const AZ::Matrix4x4& worldFromView, const AZ::Vector2& viewportSize); //! Override the default near/far clipping planes and fov of the camera. - void SetCameraClippingVolume(CameraState& cameraState, float nearPlane, float farPlane, float fovRad); + void SetCameraClippingVolume(CameraState& cameraState, float nearPlane, float farPlane, float verticalFovRad); //! Override the default near/far clipping planes and fov of the camera by inferring them the specified right handed transform into clip space. void SetCameraClippingVolumeFromPerspectiveFovMatrixRH(CameraState& cameraState, const AZ::Matrix4x4& clipFromView); diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.cpp b/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.cpp index 1b3645630e..294ff71974 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.cpp +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.cpp @@ -24,11 +24,12 @@ namespace AzFramework ClickDetector::ClickOutcome ClickDetector::DetectClick(const ClickEvent clickEvent, const ScreenVector& cursorDelta) { + m_moveAccumulator += ScreenVectorLength(cursorDelta); + const auto previousDetectionState = m_detectionState; if (previousDetectionState == DetectionState::WaitingForMove) { // only allow the action to begin if the mouse has been moved a small amount - m_moveAccumulator += ScreenVectorLength(cursorDelta); if (m_moveAccumulator > m_deadZone) { m_detectionState = DetectionState::Moved; @@ -43,7 +44,7 @@ namespace AzFramework using FloatingPointSeconds = AZStd::chrono::duration; const auto diff = now - m_tryBeginTime.value(); - if (FloatingPointSeconds(diff).count() < m_doubleClickInterval) + if (FloatingPointSeconds(diff).count() < m_doubleClickInterval && m_moveAccumulator < m_deadZone) { return ClickOutcome::Nil; } diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.h b/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.h index 70bdeb4619..544afe6d69 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ClickDetector.h @@ -15,6 +15,10 @@ namespace AzFramework { + //! Default value to use for detecting if the mouse has moved far enough after a mouse down to no longer + //! register a click when a mouse up occurs. + inline constexpr float DefaultMouseMoveDeadZone = 2.0f; + struct ScreenVector; //! Utility class to help detect different types of mouse click (mouse down and up with @@ -66,7 +70,7 @@ namespace AzFramework }; float m_moveAccumulator = 0.0f; //!< How far the mouse has moved after mouse down. - float m_deadZone = 2.0f; //!< How far to move before a click is cancelled (when Move will fire). + float m_deadZone = DefaultMouseMoveDeadZone; //!< How far to move before a click is cancelled (when Move will fire). float m_doubleClickInterval = 0.4f; //!< Default double click interval, can be overridden. DetectionState m_detectionState; //!< Internal state of ClickDetector. //! Mouse down time (happens each mouse down, helps with double click handling). diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.cpp b/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.cpp index e26d7cfa9b..f1c21230c7 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.cpp +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.cpp @@ -24,6 +24,10 @@ namespace AzFramework serializeContext->Class()-> Field("X", &ScreenVector::m_x)-> Field("Y", &ScreenVector::m_y); + + serializeContext->Class()-> + Field("Width", &ScreenSize::m_width)-> + Field("Height", &ScreenSize::m_height); } } } // namespace AzFramework diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.h b/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.h index 1529d760e5..7a5d7fdc62 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ScreenGeometry.h @@ -26,7 +26,7 @@ namespace AzFramework AZ_TYPE_INFO(ScreenPoint, "{8472B6C2-527F-44FC-87F8-C226B1A57A97}"); ScreenPoint() = default; - ScreenPoint(int x, int y) + constexpr ScreenPoint(int x, int y) : m_x(x) , m_y(y) { @@ -45,7 +45,7 @@ namespace AzFramework AZ_TYPE_INFO(ScreenVector, "{1EAA2C62-8FDB-4A28-9FE3-1FA4F1418894}"); ScreenVector() = default; - ScreenVector(int x, int y) + constexpr ScreenVector(int x, int y) : m_x(x) , m_y(y) { @@ -55,6 +55,22 @@ namespace AzFramework int m_y; //!< Y screen delta. }; + //! A wrapper around a screen width and height. + struct ScreenSize + { + AZ_TYPE_INFO(ScreenSize, "{26D28916-6E8E-44B8-83F9-C44BCDA370E2}"); + ScreenSize() = default; + + constexpr ScreenSize(int width, int height) + : m_width(width) + , m_height(height) + { + } + + int m_width; //!< Screen size width. + int m_height; //!< Screen size height. + }; + void ScreenGeometryReflect(AZ::ReflectContext* context); inline const ScreenVector operator-(const ScreenPoint& lhs, const ScreenPoint& rhs) @@ -138,6 +154,16 @@ namespace AzFramework return !operator==(lhs, rhs); } + inline const bool operator==(const ScreenSize& lhs, const ScreenSize& rhs) + { + return lhs.m_width == rhs.m_width && lhs.m_height == rhs.m_height; + } + + inline const bool operator!=(const ScreenSize& lhs, const ScreenSize& rhs) + { + return !operator==(lhs, rhs); + } + inline ScreenVector& operator*=(ScreenVector& lhs, const float rhs) { lhs.m_x = aznumeric_cast(AZStd::lround(aznumeric_cast(lhs.m_x) * rhs)); @@ -152,6 +178,20 @@ namespace AzFramework return result; } + inline ScreenSize& operator*=(ScreenSize& lhs, const float rhs) + { + lhs.m_width = aznumeric_cast(AZStd::lround(aznumeric_cast(lhs.m_width) * rhs)); + lhs.m_height = aznumeric_cast(AZStd::lround(aznumeric_cast(lhs.m_height) * rhs)); + return lhs; + } + + inline const ScreenSize operator*(const ScreenSize& lhs, const float rhs) + { + ScreenSize result{ lhs }; + result *= rhs; + return result; + } + inline float ScreenVectorLength(const ScreenVector& screenVector) { return aznumeric_cast(AZStd::sqrt(screenVector.m_x * screenVector.m_x + screenVector.m_y * screenVector.m_y)); @@ -168,4 +208,28 @@ namespace AzFramework { return AZ::Vector2(aznumeric_cast(screenVector.m_x), aznumeric_cast(screenVector.m_y)); } + + //! Return an AZ::Vector2 from a ScreenSize. + inline AZ::Vector2 Vector2FromScreenSize(const ScreenSize& screenSize) + { + return AZ::Vector2(aznumeric_cast(screenSize.m_width), aznumeric_cast(screenSize.m_height)); + } + + //! Return a ScreenPoint from an AZ::Vector2. + inline ScreenPoint ScreenPointFromVector2(const AZ::Vector2& vector2) + { + return ScreenPoint(aznumeric_cast(AZStd::lround(vector2.GetX())), aznumeric_cast(AZStd::lround(vector2.GetY()))); + } + + //! Return a ScreenVector from an AZ::Vector2. + inline ScreenVector ScreenVectorFromVector2(const AZ::Vector2& vector2) + { + return ScreenVector(aznumeric_cast(AZStd::lround(vector2.GetX())), aznumeric_cast(AZStd::lround(vector2.GetY()))); + } + + //! Return a ScreenSize from an AZ::Vector2. + inline ScreenSize ScreenSizeFromVector2(const AZ::Vector2& vector2) + { + return ScreenSize(aznumeric_cast(AZStd::lround(vector2.GetX())), aznumeric_cast(AZStd::lround(vector2.GetY()))); + } } // namespace AzFramework diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportBus.h b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportBus.h index 131ecc0c57..f53b84a63f 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportBus.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportBus.h @@ -15,6 +15,7 @@ namespace AZ { class Matrix4x4; + class Matrix3x4; class Transform; class ReflectContext; } // namespace AZ @@ -32,6 +33,8 @@ namespace AzFramework //! Gets the current camera's world to view matrix. virtual const AZ::Matrix4x4& GetCameraViewMatrix() const = 0; + //! Gets the current camera's world to view matrix as a Matrix3x4. + virtual AZ::Matrix3x4 GetCameraViewMatrixAsMatrix3x4() const = 0; //! Sets the current camera's world to view matrix. virtual void SetCameraViewMatrix(const AZ::Matrix4x4& matrix) = 0; //! Gets the current camera's projection (view to clip) matrix. diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.cpp b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.cpp index afd16a6c23..a23b306546 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.cpp +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -30,34 +31,31 @@ namespace AzFramework // multiplication which must be used (see CameraTransformFromCameraView and CameraViewFromCameraTransform) // note: coordinate system convention is right handed // see Matrix4x4::CreateProjection for more details - static AZ::Matrix4x4 ZYCoordinateSystemConversion() + static AZ::Matrix3x4 ZYCoordinateSystemConversion() { // note: the below matrix is the result of these combined transformations // pitch = AZ::Matrix4x4::CreateRotationX(AZ::DegToRad(-90.0f)); // yaw = AZ::Matrix4x4::CreateRotationZ(AZ::DegToRad(180.0f)); // conversion = pitch * yaw - return AZ::Matrix4x4::CreateFromColumns( - AZ::Vector4(-1.0f, 0.0f, 0.0f, 0.0f), AZ::Vector4(0.0f, 0.0f, 1.0f, 0.0f), AZ::Vector4(0.0f, 1.0f, 0.0f, 0.0f), - AZ::Vector4(0.0f, 0.0f, 0.0f, 1.0f)); + return AZ::Matrix3x4::CreateFromColumns( + AZ::Vector3(-1.0f, 0.0f, 0.0f), AZ::Vector3(0.0f, 0.0f, 1.0f), AZ::Vector3(0.0f, 1.0f, 0.0f), AZ::Vector3(0.0f, 0.0f, 0.0f)); } - AZ::Matrix4x4 CameraTransform(const CameraState& cameraState) + AZ::Matrix3x4 CameraTransform(const CameraState& cameraState) { - return AZ::Matrix4x4::CreateFromColumns( - AZ::Vector3ToVector4(cameraState.m_side), AZ::Vector3ToVector4(cameraState.m_forward), AZ::Vector3ToVector4(cameraState.m_up), - AZ::Vector3ToVector4(cameraState.m_position, 1.0f)); + return AZ::Matrix3x4::CreateFromColumns(cameraState.m_side, cameraState.m_forward, cameraState.m_up, cameraState.m_position); } - AZ::Matrix4x4 CameraView(const CameraState& cameraState) + AZ::Matrix3x4 CameraView(const CameraState& cameraState) { // ensure the camera is looking down positive z with the x axis pointing left - return ZYCoordinateSystemConversion() * CameraTransform(cameraState).GetInverseTransform(); + return ZYCoordinateSystemConversion() * CameraTransform(cameraState).GetInverseFast(); } - AZ::Matrix4x4 InverseCameraView(const CameraState& cameraState) + AZ::Matrix3x4 InverseCameraView(const CameraState& cameraState) { // ensure the camera is looking down positive z with the x axis pointing left - return CameraView(cameraState).GetInverseTransform(); + return CameraView(cameraState).GetInverseFast(); } AZ::Matrix4x4 CameraProjection(const CameraState& cameraState) @@ -71,14 +69,14 @@ namespace AzFramework return CameraProjection(cameraState).GetInverseFull(); } - AZ::Matrix4x4 CameraTransformFromCameraView(const AZ::Matrix4x4& cameraView) + AZ::Matrix3x4 CameraTransformFromCameraView(const AZ::Matrix3x4& cameraView) { - return (ZYCoordinateSystemConversion() * cameraView).GetInverseTransform(); + return (ZYCoordinateSystemConversion() * cameraView).GetInverseFast(); } - AZ::Matrix4x4 CameraViewFromCameraTransform(const AZ::Matrix4x4& cameraTransform) + AZ::Matrix3x4 CameraViewFromCameraTransform(const AZ::Matrix3x4& cameraTransform) { - return ZYCoordinateSystemConversion() * cameraTransform.GetInverseTransform(); + return ZYCoordinateSystemConversion() * cameraTransform.GetInverseFast(); } AZ::Frustum FrustumFromCameraState(const CameraState& cameraState) @@ -90,16 +88,17 @@ namespace AzFramework { const auto worldFromView = AzFramework::CameraTransform(cameraState); const auto cameraWorldTransform = AZ::Transform::CreateFromMatrix3x3AndTranslation( - AZ::Matrix3x3::CreateFromMatrix4x4(worldFromView), worldFromView.GetTranslation()); + AZ::Matrix3x3::CreateFromMatrix3x4(worldFromView), worldFromView.GetTranslation()); return AZ::ViewFrustumAttributes( cameraWorldTransform, AspectRatio(cameraState.m_viewportSize), cameraState.m_fovOrZoom, cameraState.m_nearClip, cameraState.m_farClip); } - AZ::Vector3 WorldToScreenNdc(const AZ::Vector3& worldPosition, const AZ::Matrix4x4& cameraView, const AZ::Matrix4x4& cameraProjection) + AZ::Vector3 WorldToScreenNdc(const AZ::Vector3& worldPosition, const AZ::Matrix3x4& cameraView, const AZ::Matrix4x4& cameraProjection) { // transform the world space position to clip space - const auto clipSpacePosition = cameraProjection * cameraView * AZ::Vector3ToVector4(worldPosition, 1.0f); + const auto clipSpacePosition = + cameraProjection * AZ::Vector3ToVector4(cameraView.TransformPoint(worldPosition), 1.0f); // transform the clip space position to ndc space (perspective divide) const auto ndcPosition = clipSpacePosition / clipSpacePosition.GetW(); // transform ndc space from <-1,1> to <0, 1> range @@ -108,13 +107,12 @@ namespace AzFramework ScreenPoint WorldToScreen( const AZ::Vector3& worldPosition, - const AZ::Matrix4x4& cameraView, + const AZ::Matrix3x4& cameraView, const AZ::Matrix4x4& cameraProjection, const AZ::Vector2& viewportSize) { - const auto ndcNormalizedPosition = WorldToScreenNdc(worldPosition, cameraView, cameraProjection); // scale ndc position by screen dimensions to return screen position - return ScreenPointFromNdc(AZ::Vector3ToVector2(ndcNormalizedPosition), viewportSize); + return ScreenPointFromNdc(AZ::Vector3ToVector2(WorldToScreenNdc(worldPosition, cameraView, cameraProjection)), viewportSize); } ScreenPoint WorldToScreen(const AZ::Vector3& worldPosition, const CameraState& cameraState) @@ -123,7 +121,7 @@ namespace AzFramework } AZ::Vector3 ScreenNdcToWorld( - const AZ::Vector2& normalizedScreenPosition, const AZ::Matrix4x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection) + const AZ::Vector2& normalizedScreenPosition, const AZ::Matrix3x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection) { // convert screen space coordinates from <0, 1> to <-1,1> range const auto ndcPosition = normalizedScreenPosition * 2.0f - AZ::Vector2::CreateOne(); @@ -140,13 +138,11 @@ namespace AzFramework AZ::Vector3 ScreenToWorld( const ScreenPoint& screenPosition, - const AZ::Matrix4x4& inverseCameraView, + const AZ::Matrix3x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection, const AZ::Vector2& viewportSize) { - const auto normalizedScreenPosition = NdcFromScreenPoint(screenPosition, viewportSize); - - return ScreenNdcToWorld(normalizedScreenPosition, inverseCameraView, inverseCameraProjection); + return ScreenNdcToWorld(NdcFromScreenPoint(screenPosition, viewportSize), inverseCameraView, inverseCameraProjection); } AZ::Vector3 ScreenToWorld(const ScreenPoint& screenPosition, const CameraState& cameraState) diff --git a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.h b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.h index 8a6c0249b2..9cd88e1c0c 100644 --- a/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.h +++ b/Code/Framework/AzFramework/AzFramework/Viewport/ViewportScreen.h @@ -16,6 +16,7 @@ namespace AZ { class Frustum; + class Matrix3x4; class Matrix4x4; struct ViewFrustumAttributes; } // namespace AZ @@ -43,7 +44,7 @@ namespace AzFramework } //! Projects a position in world space to screen space normalized device coordinates for the given camera. - AZ::Vector3 WorldToScreenNdc(const AZ::Vector3& worldPosition, const AZ::Matrix4x4& cameraView, const AZ::Matrix4x4& cameraProjection); + AZ::Vector3 WorldToScreenNdc(const AZ::Vector3& worldPosition, const AZ::Matrix3x4& cameraView, const AZ::Matrix4x4& cameraProjection); //! Projects a position in world space to screen space for the given camera. ScreenPoint WorldToScreen(const AZ::Vector3& worldPosition, const CameraState& cameraState); @@ -52,7 +53,7 @@ namespace AzFramework //! is called many times in a loop. ScreenPoint WorldToScreen( const AZ::Vector3& worldPosition, - const AZ::Matrix4x4& cameraView, + const AZ::Matrix3x4& cameraView, const AZ::Matrix4x4& cameraProjection, const AZ::Vector2& viewportSize); @@ -64,14 +65,14 @@ namespace AzFramework //! is called many times in a loop. AZ::Vector3 ScreenToWorld( const ScreenPoint& screenPosition, - const AZ::Matrix4x4& inverseCameraView, + const AZ::Matrix3x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection, const AZ::Vector2& viewportSize); //! Unprojects a position in screen space normalized device coordinates to world space. //! Note: The position returned will be on the near clip plane of the camera in world space. AZ::Vector3 ScreenNdcToWorld( - const AZ::Vector2& ndcPosition, const AZ::Matrix4x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection); + const AZ::Vector2& ndcPosition, const AZ::Matrix3x4& inverseCameraView, const AZ::Matrix4x4& inverseCameraProjection); //! Returns the camera projection for the current camera state. AZ::Matrix4x4 CameraProjection(const CameraState& cameraState); @@ -81,27 +82,27 @@ namespace AzFramework //! Returns the camera view for the current camera state. //! @note This is the 'v' in the MVP transform going from world space to view space (viewFromWorld). - AZ::Matrix4x4 CameraView(const CameraState& cameraState); + AZ::Matrix3x4 CameraView(const CameraState& cameraState); //! Returns the inverse of the camera view for the current camera state. //! @note This is the same as the CameraTransform but corrected for Z up. - AZ::Matrix4x4 InverseCameraView(const CameraState& cameraState); + AZ::Matrix3x4 InverseCameraView(const CameraState& cameraState); //! Returns the camera transform for the current camera state. //! @note This is the inverse of 'v' in the MVP transform going from view space to world space (worldFromView). - AZ::Matrix4x4 CameraTransform(const CameraState& cameraState); + AZ::Matrix3x4 CameraTransform(const CameraState& cameraState); //! Takes a camera view (the world to camera space transform) and returns the //! corresponding camera transform (the world position and orientation of the camera). //! @note The parameter is the viewFromWorld transform (the 'v' in MVP) going from world space //! to view space. The return value is worldFromView transform going from view space to world space. - AZ::Matrix4x4 CameraTransformFromCameraView(const AZ::Matrix4x4& cameraView); + AZ::Matrix3x4 CameraTransformFromCameraView(const AZ::Matrix3x4& cameraView); //! Takes a camera transform (the world position and orientation of the camera) and //! returns the corresponding camera view (to be used to transform from world to camera space). //! @note The parameter is the worldFromView transform going from view space to world space. The //! return value is viewFromWorld transform (the 'v' in MVP) going from view space to world space. - AZ::Matrix4x4 CameraViewFromCameraTransform(const AZ::Matrix4x4& cameraTransform); + AZ::Matrix3x4 CameraViewFromCameraTransform(const AZ::Matrix3x4& cameraTransform); //! Returns a frustum representing the camera transform and view volume in world space. AZ::Frustum FrustumFromCameraState(const CameraState& cameraState); diff --git a/Code/Framework/AzFramework/AzFramework/azframework_files.cmake b/Code/Framework/AzFramework/AzFramework/azframework_files.cmake index 22fbddb39a..b8d428ab82 100644 --- a/Code/Framework/AzFramework/AzFramework/azframework_files.cmake +++ b/Code/Framework/AzFramework/AzFramework/azframework_files.cmake @@ -21,6 +21,7 @@ set(FILES Archive/ArchiveFindData.cpp Archive/ArchiveFindData.h Archive/ArchiveVars.h + Archive/ArchiveVars.cpp Archive/Codec.h Archive/IArchive.h Archive/INestedArchive.h @@ -78,6 +79,7 @@ set(FILES CommandLine/CommandLine.h CommandLine/CommandRegistrationBus.h Debug/DebugCameraBus.h + feature_options.cmake Viewport/ViewportBus.h Viewport/ViewportBus.cpp Viewport/ViewportColors.h @@ -229,6 +231,7 @@ set(FILES Physics/Configuration/SystemConfiguration.h Physics/Configuration/SystemConfiguration.cpp Physics/HeightfieldProviderBus.h + Physics/HeightfieldProviderBus.cpp Physics/SimulatedBodies/RigidBody.h Physics/SimulatedBodies/RigidBody.cpp Physics/SimulatedBodies/StaticRigidBody.h diff --git a/Code/Framework/AzFramework/AzFramework/feature_options.cmake b/Code/Framework/AzFramework/AzFramework/feature_options.cmake new file mode 100644 index 0000000000..e10ddf6ba6 --- /dev/null +++ b/Code/Framework/AzFramework/AzFramework/feature_options.cmake @@ -0,0 +1,13 @@ +# +# 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 +# +# + +set(LY_ARCHIVE_FILE_SEARCH_MODE "" CACHE STRING "Set the default file search mode to locate non-Pak files within the Archive System\n\ + Valid values are:\n\ + 0 = Search FileSystem first, before searching within mounted Paks (default in debug/profile)\n\ + 1 = Search mounted Paks first, before searching FileSystem\n\ + 2 = Search only mounted Paks (default in release)\n") diff --git a/Code/Framework/AzFramework/CMakeLists.txt b/Code/Framework/AzFramework/CMakeLists.txt index c8eeac5c2d..9f30d23a19 100644 --- a/Code/Framework/AzFramework/CMakeLists.txt +++ b/Code/Framework/AzFramework/CMakeLists.txt @@ -6,6 +6,7 @@ # # +include(AzFramework/feature_options.cmake) ly_get_list_relative_pal_filename(pal_dir ${CMAKE_CURRENT_LIST_DIR}/Platform/${PAL_PLATFORM_NAME}) ly_get_list_relative_pal_filename(common_dir ${CMAKE_CURRENT_LIST_DIR}/Platform/Common) @@ -33,6 +34,14 @@ ly_add_target( 3rdParty::lz4 ) +set(LY_SEARCH_MODE_DEFINE $<$:LY_ARCHIVE_FILE_SEARCH_MODE=${LY_ARCHIVE_FILE_SEARCH_MODE}>) + +ly_add_source_properties( + SOURCES + AzFramework/Archive/ArchiveVars.cpp + PROPERTY COMPILE_DEFINITIONS + VALUES ${LY_SEARCH_MODE_DEFINE}) + if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) ly_get_list_relative_pal_filename(pal_dir ${CMAKE_CURRENT_LIST_DIR}/Tests/Platform/${PAL_PLATFORM_NAME}) @@ -43,6 +52,7 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) FILES_CMAKE Tests/framework_shared_tests_files.cmake AzFramework/Physics/physics_mock_files.cmake + Tests/terrain_mock_files.cmake INCLUDE_DIRECTORIES PUBLIC Tests diff --git a/Code/Framework/AzFramework/Platform/Common/Xcb/AzFramework/XcbInputDeviceMouse.h b/Code/Framework/AzFramework/Platform/Common/Xcb/AzFramework/XcbInputDeviceMouse.h index a69a8a9ec5..f5b805a7a5 100644 --- a/Code/Framework/AzFramework/Platform/Common/Xcb/AzFramework/XcbInputDeviceMouse.h +++ b/Code/Framework/AzFramework/Platform/Common/Xcb/AzFramework/XcbInputDeviceMouse.h @@ -6,6 +6,8 @@ * */ +#pragma once + #include #include #include diff --git a/Code/Framework/AzFramework/Tests/ArchiveTests.cpp b/Code/Framework/AzFramework/Tests/ArchiveTests.cpp index ff0e3ab724..33924d7e0c 100644 --- a/Code/Framework/AzFramework/Tests/ArchiveTests.cpp +++ b/Code/Framework/AzFramework/Tests/ArchiveTests.cpp @@ -273,7 +273,7 @@ namespace UnitTest // Also enable extra verbosity in the AZ::IO::Archive code CVarIntValueScope previousLocationPriority{ *console, "sys_pakPriority" }; CVarIntValueScope oldArchiveVerbosity{ *console, "az_archive_verbosity" }; - console->PerformCommand("sys_PakPriority", { AZ::CVarFixedString::format("%d", aznumeric_cast(AZ::IO::ArchiveLocationPriority::ePakPriorityPakOnly)) }); + console->PerformCommand("sys_PakPriority", { AZ::CVarFixedString::format("%d", aznumeric_cast(AZ::IO::FileSearchPriority::PakOnly)) }); console->PerformCommand("az_archive_verbosity", { "1" }); // ---- Archive FGetCachedFileDataTests (these leverage Archive CachedFile mechanism for caching data --- @@ -459,7 +459,7 @@ namespace UnitTest // Once the archive has been deleted it should no longer be searched CVarIntValueScope previousLocationPriority{ *console, "sys_pakPriority" }; - console->PerformCommand("sys_PakPriority", { AZ::CVarFixedString::format("%d", aznumeric_cast(AZ::IO::ArchiveLocationPriority::ePakPriorityPakOnly)) }); + console->PerformCommand("sys_PakPriority", { AZ::CVarFixedString::format("%d", aznumeric_cast(AZ::IO::FileSearchPriority::PakOnly)) }); handle = archive->FindFirst("levels\\*"); EXPECT_FALSE(static_cast(handle)); @@ -785,7 +785,7 @@ namespace UnitTest EXPECT_TRUE(archive->OpenPack("@usercache@", realNameBuf)); EXPECT_TRUE(archive->IsFileExist("@usercache@/foundit.dat")); - EXPECT_FALSE(archive->IsFileExist("@usercache@/foundit.dat", AZ::IO::IArchive::eFileLocation_OnDisk)); + EXPECT_FALSE(archive->IsFileExist("@usercache@/foundit.dat", AZ::IO::FileSearchLocation::OnDisk)); EXPECT_FALSE(archive->IsFileExist("@usercache@/notfoundit.dat")); EXPECT_TRUE(archive->ClosePack(realNameBuf)); @@ -793,7 +793,7 @@ namespace UnitTest EXPECT_TRUE(archive->OpenPack("@products@", realNameBuf)); EXPECT_TRUE(archive->IsFileExist("@products@/foundit.dat")); EXPECT_FALSE(archive->IsFileExist("@usercache@/foundit.dat")); // do not find it in the previous location! - EXPECT_FALSE(archive->IsFileExist("@products@/foundit.dat", AZ::IO::IArchive::eFileLocation_OnDisk)); + EXPECT_FALSE(archive->IsFileExist("@products@/foundit.dat", AZ::IO::FileSearchLocation::OnDisk)); EXPECT_FALSE(archive->IsFileExist("@products@/notfoundit.dat")); EXPECT_TRUE(archive->ClosePack(realNameBuf)); @@ -802,8 +802,8 @@ namespace UnitTest EXPECT_TRUE(archive->IsFileExist("@products@/mystuff/foundit.dat")); EXPECT_FALSE(archive->IsFileExist("@products@/foundit.dat")); // do not find it in the previous locations! EXPECT_FALSE(archive->IsFileExist("@usercache@/foundit.dat")); // do not find it in the previous locations! - EXPECT_FALSE(archive->IsFileExist("@products@/foundit.dat", AZ::IO::IArchive::eFileLocation_OnDisk)); - EXPECT_FALSE(archive->IsFileExist("@products@/mystuff/foundit.dat", AZ::IO::IArchive::eFileLocation_OnDisk)); + EXPECT_FALSE(archive->IsFileExist("@products@/foundit.dat", AZ::IO::FileSearchLocation::OnDisk)); + EXPECT_FALSE(archive->IsFileExist("@products@/mystuff/foundit.dat", AZ::IO::FileSearchLocation::OnDisk)); EXPECT_FALSE(archive->IsFileExist("@products@/notfoundit.dat")); // non-existent file EXPECT_FALSE(archive->IsFileExist("@products@/mystuff/notfoundit.dat")); // non-existent file EXPECT_TRUE(archive->ClosePack(realNameBuf)); diff --git a/Code/Framework/AzFramework/Tests/CameraInputTests.cpp b/Code/Framework/AzFramework/Tests/CameraInputTests.cpp index a89fb0bd84..005623677c 100644 --- a/Code/Framework/AzFramework/Tests/CameraInputTests.cpp +++ b/Code/Framework/AzFramework/Tests/CameraInputTests.cpp @@ -104,9 +104,10 @@ namespace UnitTest AZStd::shared_ptr m_orbitCamera; AZ::Vector3 m_pivot = AZ::Vector3::CreateZero(); - //! This is approximately Pi/2 * 1000 - this can be used to rotate the camera 90 degrees (pitch or yaw based - //! on vertical or horizontal motion) as the rotate speed function is set to be 1/1000. - inline static const int PixelMotionDelta = 1570; + // this is approximately Pi/2 * 1000 - this can be used to rotate the camera 90 degrees (pitch or yaw based + // on vertical or horizontal motion) as the rotate speed function is set to be 1/1000. + inline static const int PixelMotionDelta90Degrees = 1570; + inline static const int PixelMotionDelta135Degrees = 2356; }; TEST_F(CameraInputFixture, BeginAndEndOrbitCameraInputConsumesCorrectEvents) @@ -292,7 +293,7 @@ namespace UnitTest HandleEventAndUpdate( AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began }); - HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ PixelMotionDelta }); + HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ PixelMotionDelta90Degrees }); const float expectedYaw = AzFramework::WrapYawRotation(-AZ::Constants::HalfPi); @@ -310,7 +311,7 @@ namespace UnitTest HandleEventAndUpdate( AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began }); - HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta }); + HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta90Degrees }); const float expectedPitch = AzFramework::ClampPitchRotation(-AZ::Constants::HalfPi); @@ -331,7 +332,7 @@ namespace UnitTest HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_orbitChannelId, AzFramework::InputChannel::State::Began }); HandleEventAndUpdate( AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Began }); - HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta }); + HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ PixelMotionDelta90Degrees }); const auto expectedCameraEndingPosition = AZ::Vector3(0.0f, -10.0f, 10.0f); const float expectedPitch = AzFramework::ClampPitchRotation(-AZ::Constants::HalfPi); @@ -354,7 +355,7 @@ namespace UnitTest HandleEventAndUpdate(AzFramework::DiscreteInputEvent{ m_orbitChannelId, AzFramework::InputChannel::State::Began }); HandleEventAndUpdate( AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Left, AzFramework::InputChannel::State::Began }); - HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ -PixelMotionDelta }); + HandleEventAndUpdate(AzFramework::HorizontalMotionEvent{ -PixelMotionDelta90Degrees }); const auto expectedCameraEndingPosition = AZ::Vector3(20.0f, -5.0f, 0.0f); const float expectedYaw = AzFramework::WrapYawRotation(AZ::Constants::HalfPi); @@ -366,4 +367,42 @@ namespace UnitTest EXPECT_THAT(m_camera.m_offset, IsClose(AZ::Vector3(5.0f, -10.0f, 0.0f))); EXPECT_THAT(m_camera.Translation(), IsCloseTolerance(expectedCameraEndingPosition, 0.01f)); } + + TEST_F(CameraInputFixture, CameraPitchCanNotBeMovedPastNinetyDegreesWhenConstrained) + { + const auto cameraStartingPosition = AZ::Vector3(15.0f, -20.0f, 0.0f); + m_targetCamera.m_pivot = cameraStartingPosition; + + HandleEventAndUpdate( + AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began }); + // pitch by 135.0 degrees + HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ -PixelMotionDelta135Degrees }); + + // clamped to 90.0 degrees + const float expectedPitch = AZ::DegToRad(90.0f); + + using ::testing::FloatNear; + EXPECT_THAT(m_camera.m_pitch, FloatNear(expectedPitch, 0.001f)); + } + + TEST_F(CameraInputFixture, CameraPitchCanBeMovedPastNinetyDegreesWhenUnconstrained) + { + m_firstPersonRotateCamera->m_constrainPitch = [] + { + return false; + }; + + const auto cameraStartingPosition = AZ::Vector3(15.0f, -20.0f, 0.0f); + m_targetCamera.m_pivot = cameraStartingPosition; + + HandleEventAndUpdate( + AzFramework::DiscreteInputEvent{ AzFramework::InputDeviceMouse::Button::Right, AzFramework::InputChannel::State::Began }); + // pitch by 135.0 degrees + HandleEventAndUpdate(AzFramework::VerticalMotionEvent{ -PixelMotionDelta135Degrees }); + + const float expectedPitch = AZ::DegToRad(135.0f); + + using ::testing::FloatNear; + EXPECT_THAT(m_camera.m_pitch, FloatNear(expectedPitch, 0.001f)); + } } // namespace UnitTest diff --git a/Code/Framework/AzFramework/Tests/CameraState.cpp b/Code/Framework/AzFramework/Tests/CameraState.cpp index 70fa8be89a..1f35d5a06c 100644 --- a/Code/Framework/AzFramework/Tests/CameraState.cpp +++ b/Code/Framework/AzFramework/Tests/CameraState.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace UnitTest @@ -51,22 +52,6 @@ namespace UnitTest { }; - // Taken from Atom::MatrixUtils for testing purposes, this can be removed if MakePerspectiveFovMatrixRH makes it into AZ - static AZ::Matrix4x4 MakePerspectiveMatrixRH(float fovY, float aspectRatio, float nearClip, float farClip) - { - float sinFov, cosFov; - AZ::SinCos(0.5f * fovY, sinFov, cosFov); - float yScale = cosFov / sinFov; //cot(fovY/2) - float xScale = yScale / aspectRatio; - - AZ::Matrix4x4 out; - out.SetRow(0, xScale, 0.f, 0.f, 0.f ); - out.SetRow(1, 0.f, yScale, 0.f, 0.f ); - out.SetRow(2, 0.f, 0.f, farClip / (nearClip - farClip), nearClip*farClip / (nearClip - farClip) ); - out.SetRow(3, 0.f, 0.f, -1.f, 0.f ); - return out; - } - TEST_P(Translation, Permutation) { // Given a position @@ -176,7 +161,8 @@ namespace UnitTest { auto [fovY, aspectRatio, nearClip, farClip] = GetParam(); - AZ::Matrix4x4 clipFromView = MakePerspectiveMatrixRH(fovY, aspectRatio, nearClip, farClip); + AZ::Matrix4x4 clipFromView; + MakePerspectiveFovMatrixRH(clipFromView, fovY, aspectRatio, nearClip, farClip); AzFramework::SetCameraClippingVolumeFromPerspectiveFovMatrixRH(m_cameraState, clipFromView); diff --git a/Code/Framework/AzFramework/Tests/ClickDetectorTests.cpp b/Code/Framework/AzFramework/Tests/ClickDetectorTests.cpp index 45bac2770e..62852c9b87 100644 --- a/Code/Framework/AzFramework/Tests/ClickDetectorTests.cpp +++ b/Code/Framework/AzFramework/Tests/ClickDetectorTests.cpp @@ -144,12 +144,45 @@ namespace UnitTest { using ::testing::Eq; - const ClickDetector::ClickOutcome downOutcome = - m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(0, 0)); - const ClickDetector::ClickOutcome upOutcome = - m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(50, 50)); + const ClickDetector::ClickOutcome downOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome upOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(50, 50)); EXPECT_THAT(downOutcome, Eq(ClickDetector::ClickOutcome::Nil)); EXPECT_THAT(upOutcome, Eq(ClickDetector::ClickOutcome::Release)); } + + //! note: ClickDetector does not explicitly return double clicks but if one occurs the ClickOutcome will be Nil + TEST_F(ClickDetectorFixture, DoubleClickIsRegisteredIfMouseDeltaHasMovedLessThanDeadzoneInClickInterval) + { + using ::testing::Eq; + + const ClickDetector::ClickOutcome firstDownOutcome = + m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome firstUpOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome secondDownOutcome = + m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome secondUpOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(0, 0)); + + EXPECT_THAT(firstDownOutcome, Eq(ClickDetector::ClickOutcome::Nil)); + EXPECT_THAT(firstUpOutcome, Eq(ClickDetector::ClickOutcome::Click)); + EXPECT_THAT(secondDownOutcome, Eq(ClickDetector::ClickOutcome::Nil)); + EXPECT_THAT(secondUpOutcome, Eq(ClickDetector::ClickOutcome::Nil)); + } + + TEST_F(ClickDetectorFixture, DoubleClickIsNotRegisteredIfMouseDeltaHasMovedMoreThanDeadzoneInClickInterval) + { + using ::testing::Eq; + + const ClickDetector::ClickOutcome firstDownOutcome = + m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome firstUpOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(0, 0)); + const ClickDetector::ClickOutcome secondDownOutcome = + m_clickDetector.DetectClick(ClickDetector::ClickEvent::Down, ScreenVector(10, 10)); + const ClickDetector::ClickOutcome secondUpOutcome = m_clickDetector.DetectClick(ClickDetector::ClickEvent::Up, ScreenVector(0, 0)); + + EXPECT_THAT(firstDownOutcome, Eq(ClickDetector::ClickOutcome::Nil)); + EXPECT_THAT(firstUpOutcome, Eq(ClickDetector::ClickOutcome::Click)); + EXPECT_THAT(secondDownOutcome, Eq(ClickDetector::ClickOutcome::Nil)); + EXPECT_THAT(secondUpOutcome, Eq(ClickDetector::ClickOutcome::Click)); + } } // namespace UnitTest diff --git a/Code/Framework/AzFramework/Tests/CursorStateTests.cpp b/Code/Framework/AzFramework/Tests/CursorStateTests.cpp index 923cd02a10..fe15580dad 100644 --- a/Code/Framework/AzFramework/Tests/CursorStateTests.cpp +++ b/Code/Framework/AzFramework/Tests/CursorStateTests.cpp @@ -8,12 +8,13 @@ #include #include +#include namespace UnitTest { using AzFramework::CursorState; - using AzFramework::ScreenVector; using AzFramework::ScreenPoint; + using AzFramework::ScreenVector; class CursorStateFixture : public ::testing::Test { diff --git a/Code/Framework/AzFramework/Tests/Mocks/Terrain/MockTerrainDataRequestBus.h b/Code/Framework/AzFramework/Tests/Mocks/Terrain/MockTerrainDataRequestBus.h new file mode 100644 index 0000000000..dbce11d639 --- /dev/null +++ b/Code/Framework/AzFramework/Tests/Mocks/Terrain/MockTerrainDataRequestBus.h @@ -0,0 +1,80 @@ +/* + * 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 + * + */ +#pragma once + +#include + +#include +#include +#include +#include + +namespace UnitTest +{ + class MockTerrainDataNotificationListener : public AzFramework::Terrain::TerrainDataNotificationBus::Handler + { + public: + MockTerrainDataNotificationListener() + { + AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusConnect(); + } + + ~MockTerrainDataNotificationListener() + { + AzFramework::Terrain::TerrainDataNotificationBus::Handler::BusDisconnect(); + } + + MOCK_METHOD0(OnTerrainDataCreateBegin, void()); + MOCK_METHOD0(OnTerrainDataCreateEnd, void()); + MOCK_METHOD0(OnTerrainDataDestroyBegin, void()); + MOCK_METHOD0(OnTerrainDataDestroyEnd, void()); + MOCK_METHOD2(OnTerrainDataChanged, void(const AZ::Aabb& dirtyRegion, TerrainDataChangedMask dataChangedMask)); + }; + + class MockTerrainDataRequests : public AzFramework::Terrain::TerrainDataRequestBus::Handler + { + public: + MockTerrainDataRequests() + { + AzFramework::Terrain::TerrainDataRequestBus::Handler::BusConnect(); + } + + ~MockTerrainDataRequests() + { + AzFramework::Terrain::TerrainDataRequestBus::Handler::BusDisconnect(); + } + + MOCK_CONST_METHOD0(GetTerrainHeightQueryResolution, AZ::Vector2()); + MOCK_METHOD1(SetTerrainHeightQueryResolution, void(AZ::Vector2)); + MOCK_CONST_METHOD0(GetTerrainAabb, AZ::Aabb()); + MOCK_METHOD1(SetTerrainAabb, void(const AZ::Aabb&)); + MOCK_CONST_METHOD3(GetHeight, float(const AZ::Vector3&, Sampler, bool*)); + MOCK_CONST_METHOD3(GetHeightFromVector2, float(const AZ::Vector2&, Sampler, bool*)); + MOCK_CONST_METHOD4(GetHeightFromFloats, float(float, float, Sampler, bool*)); + MOCK_CONST_METHOD2(GetIsHole, bool(const AZ::Vector3&, Sampler)); + MOCK_CONST_METHOD2(GetIsHoleFromVector2, bool(const AZ::Vector2&, Sampler)); + MOCK_CONST_METHOD3(GetIsHoleFromFloats, bool(float, float, Sampler)); + MOCK_CONST_METHOD3(GetNormal, AZ::Vector3(const AZ::Vector3&, Sampler, bool*)); + MOCK_CONST_METHOD3(GetNormalFromVector2, AZ::Vector3(const AZ::Vector2&, Sampler, bool*)); + MOCK_CONST_METHOD4(GetNormalFromFloats, AZ::Vector3(float, float, Sampler, bool*)); + MOCK_CONST_METHOD3(GetMaxSurfaceWeight, AzFramework::SurfaceData::SurfaceTagWeight(const AZ::Vector3&, Sampler, bool*)); + MOCK_CONST_METHOD3(GetMaxSurfaceWeightFromVector2, AzFramework::SurfaceData::SurfaceTagWeight(const AZ::Vector2&, Sampler, bool*)); + MOCK_CONST_METHOD4(GetMaxSurfaceWeightFromFloats, AzFramework::SurfaceData::SurfaceTagWeight(float, float, Sampler, bool*)); + MOCK_CONST_METHOD4(GetSurfaceWeights, void(const AZ::Vector3&, AzFramework::SurfaceData::SurfaceTagWeightList&, Sampler, bool*)); + MOCK_CONST_METHOD4( + GetSurfaceWeightsFromVector2, void(const AZ::Vector2&, AzFramework::SurfaceData::SurfaceTagWeightList&, Sampler, bool*)); + MOCK_CONST_METHOD5( + GetSurfaceWeightsFromFloats, void(float, float, AzFramework::SurfaceData::SurfaceTagWeightList&, Sampler, bool*)); + MOCK_CONST_METHOD3(GetMaxSurfaceName, const char*(const AZ::Vector3&, Sampler, bool*)); + MOCK_CONST_METHOD4(GetSurfacePoint, void(const AZ::Vector3&, AzFramework::SurfaceData::SurfacePoint&, Sampler, bool*)); + MOCK_CONST_METHOD4( + GetSurfacePointFromVector2, void(const AZ::Vector2&, AzFramework::SurfaceData::SurfacePoint&, Sampler, bool*)); + MOCK_CONST_METHOD5( + GetSurfacePointFromFloats, void(float, float, AzFramework::SurfaceData::SurfacePoint&, Sampler, bool*)); + }; +} // namespace UnitTest diff --git a/Code/Framework/AzFramework/Tests/Utils/Printers.cpp b/Code/Framework/AzFramework/Tests/Utils/Printers.cpp new file mode 100644 index 0000000000..91c9558be7 --- /dev/null +++ b/Code/Framework/AzFramework/Tests/Utils/Printers.cpp @@ -0,0 +1,32 @@ +/* + * 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 "Printers.h" + +#include + +#include +#include + +namespace AzFramework +{ + void PrintTo(const ScreenPoint& screenPoint, std::ostream* os) + { + *os << "(x: " << screenPoint.m_x << ", y: " << screenPoint.m_y << ")"; + } + + void PrintTo(const ScreenVector& screenVector, std::ostream* os) + { + *os << "(x: " << screenVector.m_x << ", y: " << screenVector.m_y << ")"; + } + + void PrintTo(const ScreenSize& screenSize, std::ostream* os) + { + *os << "(width: " << screenSize.m_width << ", height: " << screenSize.m_height << ")"; + } +} // namespace AzFramework diff --git a/Code/Framework/AzFramework/Tests/Utils/Printers.h b/Code/Framework/AzFramework/Tests/Utils/Printers.h new file mode 100644 index 0000000000..fc967eabe9 --- /dev/null +++ b/Code/Framework/AzFramework/Tests/Utils/Printers.h @@ -0,0 +1,20 @@ +/* + * 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 + +namespace AzFramework +{ + struct ScreenPoint; + struct ScreenVector; + struct ScreenSize; + + void PrintTo(const ScreenPoint& screenPoint, std::ostream* os); + void PrintTo(const ScreenVector& screenVector, std::ostream* os); + void PrintTo(const ScreenSize& screenSize, std::ostream* os); +} // namespace AzFramework diff --git a/Code/Framework/AzFramework/Tests/framework_shared_tests_files.cmake b/Code/Framework/AzFramework/Tests/framework_shared_tests_files.cmake index 85c00a2e8a..9427738e67 100644 --- a/Code/Framework/AzFramework/Tests/framework_shared_tests_files.cmake +++ b/Code/Framework/AzFramework/Tests/framework_shared_tests_files.cmake @@ -11,5 +11,7 @@ set(FILES Mocks/MockWindowRequests.h Utils/Utils.h Utils/Utils.cpp + Utils/Printers.h + Utils/Printers.cpp FrameworkApplicationFixture.h ) diff --git a/Code/Framework/AzFramework/Tests/terrain_mock_files.cmake b/Code/Framework/AzFramework/Tests/terrain_mock_files.cmake new file mode 100644 index 0000000000..590c64d88b --- /dev/null +++ b/Code/Framework/AzFramework/Tests/terrain_mock_files.cmake @@ -0,0 +1,11 @@ +# +# 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 +# +# + +set(FILES + Mocks/Terrain/MockTerrainDataRequestBus.h +) \ No newline at end of file diff --git a/Code/Framework/AzManipulatorTestFramework/Include/AzManipulatorTestFramework/ViewportInteraction.h b/Code/Framework/AzManipulatorTestFramework/Include/AzManipulatorTestFramework/ViewportInteraction.h index a8ba63500c..3c41b28851 100644 --- a/Code/Framework/AzManipulatorTestFramework/Include/AzManipulatorTestFramework/ViewportInteraction.h +++ b/Code/Framework/AzManipulatorTestFramework/Include/AzManipulatorTestFramework/ViewportInteraction.h @@ -41,8 +41,8 @@ namespace AzManipulatorTestFramework // ViewportInteractionRequestBus overrides ... AzFramework::CameraState GetCameraState() override; AzFramework::ScreenPoint ViewportWorldToScreen(const AZ::Vector3& worldPosition) override; - AZStd::optional ViewportScreenToWorld(const AzFramework::ScreenPoint& screenPosition, float depth) override; - AZStd::optional ViewportScreenToWorldRay( + AZ::Vector3 ViewportScreenToWorld(const AzFramework::ScreenPoint& screenPosition) override; + AzToolsFramework::ViewportInteraction::ProjectedViewportRay ViewportScreenToWorldRay( const AzFramework::ScreenPoint& screenPosition) override; float DeviceScalingFactor() override; diff --git a/Code/Framework/AzManipulatorTestFramework/Source/AzManipulatorTestFrameworkUtils.cpp b/Code/Framework/AzManipulatorTestFramework/Source/AzManipulatorTestFrameworkUtils.cpp index 4f1e108a14..6fbf2cb219 100644 --- a/Code/Framework/AzManipulatorTestFramework/Source/AzManipulatorTestFrameworkUtils.cpp +++ b/Code/Framework/AzManipulatorTestFramework/Source/AzManipulatorTestFrameworkUtils.cpp @@ -95,7 +95,7 @@ namespace AzManipulatorTestFramework AzToolsFramework::ViewportInteraction::MousePick mousePick; mousePick.m_screenCoordinates = screenPoint; - mousePick.m_rayOrigin = cameraState.m_position; + mousePick.m_rayOrigin = nearPlaneWorldPosition; mousePick.m_rayDirection = (nearPlaneWorldPosition - cameraState.m_position).GetNormalized(); return mousePick; diff --git a/Code/Framework/AzManipulatorTestFramework/Source/ImmediateModeActionDispatcher.cpp b/Code/Framework/AzManipulatorTestFramework/Source/ImmediateModeActionDispatcher.cpp index c122a941c4..5bfe63bd99 100644 --- a/Code/Framework/AzManipulatorTestFramework/Source/ImmediateModeActionDispatcher.cpp +++ b/Code/Framework/AzManipulatorTestFramework/Source/ImmediateModeActionDispatcher.cpp @@ -69,8 +69,6 @@ namespace AzManipulatorTestFramework void ImmediateModeActionDispatcher::CameraStateImpl(const AzFramework::CameraState& cameraState) { m_viewportManipulatorInteraction.GetViewportInteraction().SetCameraState(cameraState); - GetMouseInteractionEvent()->m_mouseInteraction.m_mousePick.m_rayOrigin = cameraState.m_position; - GetMouseInteractionEvent()->m_mouseInteraction.m_mousePick.m_rayDirection = cameraState.m_forward; } void ImmediateModeActionDispatcher::MouseLButtonDownImpl() diff --git a/Code/Framework/AzManipulatorTestFramework/Source/IndirectManipulatorViewportInteraction.cpp b/Code/Framework/AzManipulatorTestFramework/Source/IndirectManipulatorViewportInteraction.cpp index 730c106301..9bcb8986d5 100644 --- a/Code/Framework/AzManipulatorTestFramework/Source/IndirectManipulatorViewportInteraction.cpp +++ b/Code/Framework/AzManipulatorTestFramework/Source/IndirectManipulatorViewportInteraction.cpp @@ -20,7 +20,8 @@ namespace AzManipulatorTestFramework { public: IndirectCallManipulatorManager(ViewportInteractionInterface& viewportInteraction); - // ManipulatorManagerInterface ... + + // ManipulatorManagerInterface overrides ... void ConsumeMouseInteractionEvent(const MouseInteractionEvent& event) override; AzToolsFramework::ManipulatorManagerId GetId() const override; bool ManipulatorBeingInteracted() const override; diff --git a/Code/Framework/AzManipulatorTestFramework/Source/ViewportInteraction.cpp b/Code/Framework/AzManipulatorTestFramework/Source/ViewportInteraction.cpp index 269baa703d..08cae0c738 100644 --- a/Code/Framework/AzManipulatorTestFramework/Source/ViewportInteraction.cpp +++ b/Code/Framework/AzManipulatorTestFramework/Source/ViewportInteraction.cpp @@ -140,13 +140,12 @@ namespace AzManipulatorTestFramework return m_viewportId; } - AZStd::optional ViewportInteraction::ViewportScreenToWorld( - [[maybe_unused]] const AzFramework::ScreenPoint& screenPosition, [[maybe_unused]] float depth) + AZ::Vector3 ViewportInteraction::ViewportScreenToWorld([[maybe_unused]] const AzFramework::ScreenPoint& screenPosition) { - return {}; + return AZ::Vector3::CreateZero(); } - AZStd::optional ViewportInteraction::ViewportScreenToWorldRay( + AzToolsFramework::ViewportInteraction::ProjectedViewportRay ViewportInteraction::ViewportScreenToWorldRay( [[maybe_unused]] const AzFramework::ScreenPoint& screenPosition) { return {}; diff --git a/Code/Framework/AzManipulatorTestFramework/Tests/WorldSpaceBuilderTest.cpp b/Code/Framework/AzManipulatorTestFramework/Tests/WorldSpaceBuilderTest.cpp index 211c8d64ef..376c18072e 100644 --- a/Code/Framework/AzManipulatorTestFramework/Tests/WorldSpaceBuilderTest.cpp +++ b/Code/Framework/AzManipulatorTestFramework/Tests/WorldSpaceBuilderTest.cpp @@ -140,8 +140,8 @@ namespace UnitTest // given a left mouse down ray in world space // consume the mouse move event state.m_actionDispatcher->CameraState(m_cameraState) - ->MouseLButtonDown() ->MousePosition(AzManipulatorTestFramework::GetCameraStateViewportCenter(m_cameraState)) + ->MouseLButtonDown() ->ExpectTrue(state.m_linearManipulator->PerformingAction()) ->ExpectManipulatorBeingInteracted() ->MouseLButtonUp() diff --git a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.cpp b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.cpp index 3a3ab06d02..6705ec8b36 100644 --- a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.cpp @@ -42,7 +42,7 @@ namespace AzNetworking { const uint32_t sampleAtom = 1 - m_activeAtom; - if (m_atoms[sampleAtom].m_timeAccumulatorMs == AZ::TimeMs{0}) + if (m_atoms[sampleAtom].m_timeAccumulatorMs == AZ::Time::ZeroTimeMs) { return 0.0f; } diff --git a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.h b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.h index b576c64e86..19db52b979 100644 --- a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.h +++ b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.h @@ -19,7 +19,7 @@ namespace AzNetworking { DatarateAtom() = default; - AZ::TimeMs m_timeAccumulatorMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_timeAccumulatorMs = AZ::Time::ZeroTimeMs; uint32_t m_bytesTransmitted = 0; uint32_t m_packetsSent = 0; uint32_t m_packetsLost = 0; @@ -78,7 +78,7 @@ namespace AzNetworking ConnectionPacketEntry(PacketId packetId, AZ::TimeMs sendTimeMs); PacketId m_packetId = InvalidPacketId; - AZ::TimeMs m_sendTimeMs = AZ::TimeMs{0}; + AZ::TimeMs m_sendTimeMs = AZ::Time::ZeroTimeMs; }; //! @class ConnectionComputeRtt diff --git a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnection.h b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnection.h index 363fd1d37b..7afbbaee7c 100644 --- a/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnection.h +++ b/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/IConnection.h @@ -28,8 +28,8 @@ namespace AzNetworking ConnectionQuality(int32_t lossPercentage, AZ::TimeMs latencyMs, AZ::TimeMs varianceMs); int32_t m_lossPercentage = 0; - AZ::TimeMs m_latencyMs = AZ::TimeMs{ 0 }; - AZ::TimeMs m_varianceMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_latencyMs = AZ::Time::ZeroTimeMs; + AZ::TimeMs m_varianceMs = AZ::Time::ZeroTimeMs; }; enum class TrustZone diff --git a/Code/Framework/AzNetworking/AzNetworking/DataStructures/TimeoutQueue.h b/Code/Framework/AzNetworking/AzNetworking/DataStructures/TimeoutQueue.h index 097e45c960..1a4423144f 100644 --- a/Code/Framework/AzNetworking/AzNetworking/DataStructures/TimeoutQueue.h +++ b/Code/Framework/AzNetworking/AzNetworking/DataStructures/TimeoutQueue.h @@ -37,8 +37,8 @@ namespace AzNetworking void UpdateTimeoutTime(AZ::TimeMs currentTimeMs); uint64_t m_userData = 0; - AZ::TimeMs m_timeoutMs = AZ::TimeMs{0}; - AZ::TimeMs m_nextTimeoutTimeMs = AZ::TimeMs{0}; + AZ::TimeMs m_timeoutMs = AZ::Time::ZeroTimeMs; + AZ::TimeMs m_nextTimeoutTimeMs = AZ::Time::ZeroTimeMs; }; TimeoutQueue() = default; diff --git a/Code/Framework/AzNetworking/AzNetworking/Framework/NetworkInterfaceMetrics.h b/Code/Framework/AzNetworking/AzNetworking/Framework/NetworkInterfaceMetrics.h index 8cad6e5537..d4bfabf54a 100644 --- a/Code/Framework/AzNetworking/AzNetworking/Framework/NetworkInterfaceMetrics.h +++ b/Code/Framework/AzNetworking/AzNetworking/Framework/NetworkInterfaceMetrics.h @@ -15,11 +15,11 @@ namespace AzNetworking struct NetworkInterfaceMetrics { //! Returns the total number of milliseconds spent updating this network interface. - AZ::TimeMs m_updateTimeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_updateTimeMs = AZ::Time::ZeroTimeMs; //! Returns the total number of connections bound to this network interface. uint64_t m_connectionCount = 0; //! Returns the total number of milliseconds spent sending data on this network interface. - AZ::TimeMs m_sendTimeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_sendTimeMs = AZ::Time::ZeroTimeMs; //! Returns the total number of packets sent on this socket. uint64_t m_sendPackets = 0; //! Returns the total number of encrypted packets sent on this socket. @@ -37,7 +37,7 @@ namespace AzNetworking //! Returns the total number of packets that had to be resent on this network interface due to packet loss. uint64_t m_resentPackets = 0; //! Returns the total number of milliseconds spent processing received data on this network interface. - AZ::TimeMs m_recvTimeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_recvTimeMs = AZ::Time::ZeroTimeMs; //! Returns the total number of packets received on this socket. uint64_t m_recvPackets = 0; //! Returns the total number of bytes received on this socket after compression. diff --git a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpListenThread.h b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpListenThread.h index 0859e1d0f7..f2dc88cab4 100644 --- a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpListenThread.h +++ b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpListenThread.h @@ -67,6 +67,6 @@ namespace AzNetworking uint32_t m_listenPortCount = 0; TcpSocketManager m_tcpSocketManager; AZ::ThreadSafeDeque m_listenPorts; - AZ::TimeMs m_updateTimeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_updateTimeMs = AZ::Time::ZeroTimeMs; }; } diff --git a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.cpp b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.cpp index 0278856ce9..18ce25c4dd 100644 --- a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.cpp @@ -107,7 +107,7 @@ namespace AzNetworking auto readCallback = [this, startTimeMs](SocketFd socketFd) { HandleConnectionRecv(socketFd, startTimeMs); }; auto writeCallback = [this](SocketFd socketFd) { HandleConnectionSend(socketFd); }; - m_tcpSocketManager.ProcessEvents(AZ::TimeMs{ 0 }, readCallback, writeCallback); + m_tcpSocketManager.ProcessEvents(AZ::Time::ZeroTimeMs, readCallback, writeCallback); FlushQueuedRemoves(); diff --git a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.h b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.h index 8d45e847a8..d483a89cf3 100644 --- a/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.h +++ b/Code/Framework/AzNetworking/AzNetworking/TcpTransport/TcpNetworkInterface.h @@ -146,7 +146,7 @@ namespace AzNetworking AZ::Name m_name; TrustZone m_trustZone; uint16_t m_port = 0; - AZ::TimeMs m_timeoutMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_timeoutMs = AZ::Time::ZeroTimeMs; IConnectionListener& m_connectionListener; TcpConnectionSet m_connectionSet; TcpSocketManager m_tcpSocketManager; diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.cpp b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.cpp index b810c7a347..1ca5922753 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.cpp @@ -736,7 +736,7 @@ namespace AzNetworking udpConnection->SendUnreliablePacket(CorePackets::HeartbeatPacket(true)); ++udpConnection->m_timeoutCounter; } - else if (net_UdpTimeoutConnections && (GetTimeoutMs() > AZ::TimeMs{ 0 })) + else if (net_UdpTimeoutConnections && (GetTimeoutMs() > AZ::Time::ZeroTimeMs)) { udpConnection->Disconnect(DisconnectReason::Timeout, TerminationEndpoint::Local); return TimeoutResult::Delete; diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.h b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.h index e6abeded0d..a640a6e3b8 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.h +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpNetworkInterface.h @@ -171,7 +171,7 @@ namespace AzNetworking TrustZone m_trustZone; uint16_t m_port = 0; bool m_allowIncomingConnections = false; - AZ::TimeMs m_timeoutMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_timeoutMs = AZ::Time::ZeroTimeMs; IConnectionListener& m_connectionListener; UdpConnectionSet m_connectionSet; TimeoutQueue m_connectionTimeoutQueue; diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpReaderThread.h b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpReaderThread.h index e0c97f157f..1d25a9e6e1 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpReaderThread.h +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpReaderThread.h @@ -94,6 +94,6 @@ namespace AzNetworking int32_t m_backIndex = 0; AZStd::array m_readerBuffers; AZStd::vector m_pendingAdds; - AZ::TimeMs m_updateTimeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_updateTimeMs = AZ::Time::ZeroTimeMs; }; } diff --git a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpSocket.cpp b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpSocket.cpp index 300b3527fa..85cc3a38f8 100644 --- a/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpSocket.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/UdpTransport/UdpSocket.cpp @@ -135,7 +135,7 @@ namespace AzNetworking int32_t sentBytes = size; #ifdef ENABLE_LATENCY_DEBUG - if (connectionQuality.m_latencyMs <= AZ::TimeMs{ 0 }) + if (connectionQuality.m_latencyMs <= AZ::Time::ZeroTimeMs) #endif { sentBytes = SendInternal(address, data, size, encrypt, dtlsEndpoint); @@ -153,9 +153,9 @@ namespace AzNetworking } } #ifdef ENABLE_LATENCY_DEBUG - else if ((connectionQuality.m_latencyMs > AZ::TimeMs{ 0 }) || (connectionQuality.m_varianceMs > AZ::TimeMs{ 0 })) + else if ((connectionQuality.m_latencyMs > AZ::Time::ZeroTimeMs) || (connectionQuality.m_varianceMs > AZ::Time::ZeroTimeMs)) { - const AZ::TimeMs jitterMs = aznumeric_cast(m_random.GetRandom()) % (connectionQuality.m_varianceMs > AZ::TimeMs{ 0 } + const AZ::TimeMs jitterMs = aznumeric_cast(m_random.GetRandom()) % (connectionQuality.m_varianceMs > AZ::Time::ZeroTimeMs ? connectionQuality.m_varianceMs : AZ::TimeMs{ 1 }); const AZ::TimeMs deferTimeMs = (connectionQuality.m_latencyMs) + jitterMs; diff --git a/Code/Framework/AzNetworking/AzNetworking/Utilities/EncryptionCommon.cpp b/Code/Framework/AzNetworking/AzNetworking/Utilities/EncryptionCommon.cpp index 9f496c3e0c..0903205eb8 100644 --- a/Code/Framework/AzNetworking/AzNetworking/Utilities/EncryptionCommon.cpp +++ b/Code/Framework/AzNetworking/AzNetworking/Utilities/EncryptionCommon.cpp @@ -94,7 +94,7 @@ namespace AzNetworking static const uint32_t MaxCookieHistory = 8; static bool g_encryptionInitialized = false; static int32_t g_azNetworkingTrustDataIndex = 0; - static AZ::TimeMs g_lastCookieTimestamp = AZ::TimeMs{0}; + static AZ::TimeMs g_lastCookieTimestamp = AZ::Time::ZeroTimeMs; static uint64_t g_validCookieArray[MaxCookieHistory]; static uint32_t g_cookieReplaceIndex = 0; diff --git a/Code/Framework/AzNetworking/Tests/Serialization/DeltaSerializerTests.cpp b/Code/Framework/AzNetworking/Tests/Serialization/DeltaSerializerTests.cpp index 32562d9940..4ad9412849 100644 --- a/Code/Framework/AzNetworking/Tests/Serialization/DeltaSerializerTests.cpp +++ b/Code/Framework/AzNetworking/Tests/Serialization/DeltaSerializerTests.cpp @@ -15,7 +15,7 @@ namespace UnitTest { AzNetworking::PacketId m_packetId = AzNetworking::InvalidPacketId; uint32_t m_id = 0; - AZ::TimeMs m_timeMs = AZ::TimeMs{ 0 }; + AZ::TimeMs m_timeMs = AZ::Time::ZeroTimeMs; float m_blendFactor = 0.f; AZStd::vector m_growVector, m_shrinkVector; diff --git a/Code/Framework/AzNetworking/Tests/TcpTransport/TcpTransportTests.cpp b/Code/Framework/AzNetworking/Tests/TcpTransport/TcpTransportTests.cpp index 77632da572..d3a956bef3 100644 --- a/Code/Framework/AzNetworking/Tests/TcpTransport/TcpTransportTests.cpp +++ b/Code/Framework/AzNetworking/Tests/TcpTransport/TcpTransportTests.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -102,24 +102,24 @@ namespace UnitTest SetupAllocator(); AZ::NameDictionary::Create(); - m_loggerComponent = new AZ::LoggerSystemComponent; - m_timeComponent = new AZ::TimeSystemComponent; - m_networkingSystemComponent = new AzNetworking::NetworkingSystemComponent; + m_loggerComponent = AZStd::make_unique(); + m_timeSystem = AZStd::make_unique(); + m_networkingSystemComponent = AZStd::make_unique(); } void TearDown() override { - delete m_networkingSystemComponent; - delete m_timeComponent; - delete m_loggerComponent; + m_networkingSystemComponent.reset(); + m_timeSystem.reset(); + m_loggerComponent.reset(); AZ::NameDictionary::Destroy(); TeardownAllocator(); } - AZ::LoggerSystemComponent* m_loggerComponent; - AZ::TimeSystemComponent* m_timeComponent; - AzNetworking::NetworkingSystemComponent* m_networkingSystemComponent; + AZStd::unique_ptr m_loggerComponent; + AZStd::unique_ptr m_timeSystem; + AZStd::unique_ptr m_networkingSystemComponent; }; #if AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS diff --git a/Code/Framework/AzNetworking/Tests/UdpTransport/UdpTransportTests.cpp b/Code/Framework/AzNetworking/Tests/UdpTransport/UdpTransportTests.cpp index 65c2cfa2b5..821c261fab 100644 --- a/Code/Framework/AzNetworking/Tests/UdpTransport/UdpTransportTests.cpp +++ b/Code/Framework/AzNetworking/Tests/UdpTransport/UdpTransportTests.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include @@ -105,24 +105,24 @@ namespace UnitTest SetupAllocator(); AZ::NameDictionary::Create(); - m_loggerComponent = new AZ::LoggerSystemComponent; - m_timeComponent = new AZ::TimeSystemComponent; - m_networkingSystemComponent = new AzNetworking::NetworkingSystemComponent; + m_loggerComponent = AZStd::make_unique(); + m_timeSystem = AZStd::make_unique(); + m_networkingSystemComponent = AZStd::make_unique(); } void TearDown() override { - delete m_networkingSystemComponent; - delete m_timeComponent; - delete m_loggerComponent; + m_networkingSystemComponent.reset(); + m_timeSystem.reset(); + m_loggerComponent.reset(); AZ::NameDictionary::Destroy(); TeardownAllocator(); } - AZ::LoggerSystemComponent* m_loggerComponent; - AZ::TimeSystemComponent* m_timeComponent; - AzNetworking::NetworkingSystemComponent* m_networkingSystemComponent; + AZStd::unique_ptr m_loggerComponent; + AZStd::unique_ptr m_timeSystem; + AZStd::unique_ptr m_networkingSystemComponent; }; TEST_F(UdpTransportTests, PacketIdWrap) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp index b6acd14a83..af902f97a0 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp @@ -53,14 +53,14 @@ static void OptimizedSetParent(QWidget* widget, QWidget* parent) namespace AzQtComponents { - static FancyDockingDropZoneConstants g_FancyDockingConstants; + static const FancyDockingDropZoneConstants g_FancyDockingConstants; // Constant for the threshold in pixels for snapping to edges while dragging for docking static const int g_snapThresholdInPixels = 15; - static QString g_minimizeButtonObjectName = "minimizeButton"; - static QString g_maximizeButtonObjectName = "maximizeButton"; - static QString g_closeButtonObjectName = "closeButton"; + static const QString MinimizeButtonObjectName = QStringLiteral("minimizeButton"); + static const QString MaximizeButtonObjectName = QStringLiteral("maximizeButton"); + static const QString CloseButtonObjectName = QStringLiteral("closeButton"); static Qt::Orientation orientation(Qt::DockWidgetArea area) { @@ -460,7 +460,7 @@ namespace AzQtComponents // Minimize Icon QAction* minimizeAction = new QAction(tr("Minimize")); - minimizeAction->setObjectName(g_minimizeButtonObjectName); + minimizeAction->setObjectName(MinimizeButtonObjectName); connect(minimizeAction, &QAction::triggered, this, [titleBar]() { titleBar->handleMinimize(); @@ -470,7 +470,7 @@ namespace AzQtComponents // Maximize Icon QAction* maximizeAction = new QAction(tr("Maximize")); - maximizeAction->setObjectName(g_maximizeButtonObjectName); + maximizeAction->setObjectName(MaximizeButtonObjectName); connect(maximizeAction, &QAction::triggered, this, [titleBar]() { titleBar->handleMaximize(); @@ -480,7 +480,7 @@ namespace AzQtComponents // Close Icon QAction* closeAction = new QAction(tr("Close")); - closeAction->setObjectName(g_closeButtonObjectName); + closeAction->setObjectName(CloseButtonObjectName); connect(closeAction, &QAction::triggered, this, [titleBar]() { titleBar->handleClose(); diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDockingDropZoneWidget.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDockingDropZoneWidget.cpp index d729929d7b..1451094ea1 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDockingDropZoneWidget.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDockingDropZoneWidget.cpp @@ -19,7 +19,7 @@ namespace AzQtComponents { - static FancyDockingDropZoneConstants g_Constants; + static const FancyDockingDropZoneConstants g_Constants; FancyDockingDropZoneConstants::FancyDockingDropZoneConstants() { diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BrowseEdit.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BrowseEdit.cpp index 03d2c13f64..7e18843d65 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BrowseEdit.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BrowseEdit.cpp @@ -27,7 +27,7 @@ AZ_POP_DISABLE_WARNING namespace AzQtComponents { - static const char clearButtonActionNameC[] = "_q_qlineeditclearaction"; + static const QString ClearButtonActionNameC = QStringLiteral("_q_qlineeditclearaction"); struct BrowseEdit::InternalData { @@ -263,7 +263,7 @@ namespace AzQtComponents auto lineEdit = browseEdit->m_data->m_lineEdit; LineEdit::polish(style, lineEdit, lineEditConfig); - QAction* action = lineEdit->findChild(clearButtonActionNameC); + QAction* action = lineEdit->findChild(ClearButtonActionNameC); if (action) { QStyleOptionFrame option; @@ -284,7 +284,7 @@ namespace AzQtComponents auto lineEdit = browseEdit->m_data->m_lineEdit; LineEdit::unpolish(style, lineEdit, lineEditConfig); - QAction* action = lineEdit->findChild(clearButtonActionNameC); + QAction* action = lineEdit->findChild(ClearButtonActionNameC); if (action) { QStyleOptionFrame option; diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.cpp index f6417d3470..21f2500822 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.cpp @@ -32,7 +32,7 @@ namespace AzQtComponents namespace Internal { - const char* OverlayWidgetLayer::s_layerStyle = "background-color:rgba(0, 0, 0, 179)"; + static const QString LayerStyle = QStringLiteral("background-color:rgba(0, 0, 0, 179)"); OverlayWidgetLayer::OverlayWidgetLayer(OverlayWidget* parent, QWidget* centerWidget, QWidget* breakoutWidget, const char* title, const OverlayWidgetButtonList& buttons) @@ -66,7 +66,7 @@ namespace AzQtComponents if (breakoutWidget) { - setStyleSheet(s_layerStyle); + setStyleSheet(LayerStyle); setLayout(new QHBoxLayout()); // close the overlay if either dependent widget is destroyed @@ -100,7 +100,7 @@ namespace AzQtComponents } else { - setStyleSheet(s_layerStyle); + setStyleSheet(LayerStyle); } AddButtons(*m_ui.data(), buttons, parent == nullptr); } diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.h b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.h index 1ad3cb5071..cba61cb67d 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.h +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/Internal/OverlayWidgetLayer.h @@ -59,8 +59,6 @@ namespace AzQtComponents bool eventFilter(QObject* object, QEvent* event) override; - static const char* s_layerStyle; - QVector