Test code to add merge detection on Settings Registry keys

Signed-off-by: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com>
This commit is contained in:
Danilo Aimini
2021-08-10 14:48:33 -07:00
parent 29c2ee9b0e
commit a70a106fd2
7 changed files with 247 additions and 0 deletions
@@ -0,0 +1,103 @@
/*
* 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 <AzToolsFramework/Editor/Settings/EditorSettingsOriginTracker.h>
#include <AzCore/IO/Path/Path.h>
#include <AzCore/Utils/Utils.h>
namespace AzToolsFramework
{
EditorPreferencesSettingsOriginTracker::SettingsNotificationHandler::SettingsNotificationHandler(
AZ::SettingsRegistryInterface& registry)
: m_settingsRegistry(registry)
{
AZ::JsonApplyPatchSettings applyPatchSettings;
m_settingsRegistry.GetApplyPatchSettings(applyPatchSettings);
// Wrap any existing callbacks into the reporting callback, so that both this struct
// reporting callbacks and the existing callbacks can be invoked
m_prevReportingCallback = applyPatchSettings.m_reporting;
applyPatchSettings.m_reporting = [this](
AZStd::string_view message, AZ::JsonSerializationResult::ResultCode result,
AZStd::string_view path) -> AZ::JsonSerializationResult::ResultCode
{
m_prevReportingCallback(message, result, path);
(*this)(message, result, path);
return result;
};
m_settingsRegistry.SetApplyPatchSettings(applyPatchSettings);
}
EditorPreferencesSettingsOriginTracker::SettingsNotificationHandler::~SettingsNotificationHandler()
{
// Restore previous reporting callback
AZ::JsonApplyPatchSettings applyPatchSettings;
m_settingsRegistry.GetApplyPatchSettings(applyPatchSettings);
applyPatchSettings.m_reporting = m_prevReportingCallback;
m_settingsRegistry.SetApplyPatchSettings(applyPatchSettings);
}
// Use the Json Serialization Issue Callback system
// to determine when a merge option modifies a value
AZ::JsonSerializationResult::ResultCode EditorPreferencesSettingsOriginTracker::SettingsNotificationHandler::operator()(
AZStd::string_view /*message*/, AZ::JsonSerializationResult::ResultCode result, AZStd::string_view path)
{
using FixedValueString = AZ::SettingsRegistryInterface::FixedValueString;
AZ::IO::PathView preferencesRootKey{ "/Amazon/Preferences", AZ::IO::PosixPathSeparator };
AZ::IO::PathView inputKey{ path, AZ::IO::PosixPathSeparator };
// Delegate to the Notifier Handler callable below
if (result.GetTask() == AZ::JsonSerializationResult::Tasks::Merge &&
result.GetProcessing() == AZ::JsonSerializationResult::Processing::Completed && inputKey.IsRelativeTo(preferencesRootKey))
{
if (auto type = m_settingsRegistry.GetType(path); type != AZ::SettingsRegistryInterface::Type::NoType)
{
operator()(path, type);
}
}
return result;
}
void EditorPreferencesSettingsOriginTracker::SettingsNotificationHandler::operator()(
AZStd::string_view path, AZ::SettingsRegistryInterface::Type /*type*/)
{
constexpr AZ::IO::PathView preferencesRootKey{ "/Amazon/Preferences", AZ::IO::PosixPathSeparator };
AZ::IO::PathView inputKey{ path, AZ::IO::PosixPathSeparator };
if (inputKey.IsRelativeTo(preferencesRootKey))
{
// Do stuff with key
}
}
EditorPreferencesSettingsOriginTracker::EditorPreferencesSettingsOriginTracker(AZ::SettingsRegistryInterface& registry)
: m_settingsRegistry(registry)
{
auto PreMergeEvent = [this](AZStd::string_view filePath, AZStd::string_view /*rootKey*/)
{
AZ::IO::FixedMaxPath editorPreferencesPath = AZ::Utils::GetProjectPath();
editorPreferencesPath = editorPreferencesPath / "user" / "Registry" / "editorpreferences.setreg";
if (AZ::IO::PathView(filePath) == editorPreferencesPath)
{
m_notifyHandler = m_settingsRegistry.RegisterNotifier(SettingsNotificationHandler(m_settingsRegistry));
}
};
auto PostMergeEvent = [this](AZStd::string_view /*filePath*/, AZStd::string_view /*rootKey*/)
{
// Clear the notification handler so that it goes out of scope
// and this tracker instance no handles settings updates
m_notifyHandler = {};
};
m_preMergeEventHandler = m_settingsRegistry.RegisterPreMergeEvent(PreMergeEvent);
m_postMergeEventHandler = m_settingsRegistry.RegisterPostMergeEvent(PostMergeEvent);
}
}
@@ -0,0 +1,39 @@
/*
* 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 <AzCore/Serialization/Json/JsonSerializationSettings.h>
#include <AzCore/Settings/SettingsRegistry.h>
#include <AzCore/Settings/SettingsRegistryMergeUtils.h>
namespace AzToolsFramework
{
struct EditorPreferencesSettingsOriginTracker
{
explicit EditorPreferencesSettingsOriginTracker(AZ::SettingsRegistryInterface& registry);
struct SettingsNotificationHandler
{
SettingsNotificationHandler(AZ::SettingsRegistryInterface& registry);
~SettingsNotificationHandler();
AZ::JsonSerializationResult::ResultCode operator()(
AZStd::string_view message, AZ::JsonSerializationResult::ResultCode result, AZStd::string_view path);
void operator()(AZStd::string_view path, AZ::SettingsRegistryInterface::Type type);
private:
AZ::SettingsRegistryInterface& m_settingsRegistry;
AZ::JsonSerializationResult::JsonIssueCallback m_prevReportingCallback;
};
private:
AZ::SettingsRegistryInterface& m_settingsRegistry;
AZ::SettingsRegistryInterface::PreMergeEventHandler m_preMergeEventHandler;
AZ::SettingsRegistryInterface::PostMergeEventHandler m_postMergeEventHandler;
AZ::SettingsRegistryInterface::NotifyEventHandler m_notifyHandler;
};
} // namespace AZ
@@ -113,6 +113,8 @@ set(FILES
Component/EditorLevelComponentAPIComponent.h
Editor/EditorContextMenuBus.h
Editor/EditorSettingsAPIBus.h
Editor/Settings/EditorSettingsOriginTracker.cpp
Editor/Settings/EditorSettingsOriginTracker.h
Entity/EditorEntityStartStatus.h
Entity/EditorEntityAPIBus.h
Entity/EditorEntityContextComponent.cpp