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
@@ -728,6 +728,7 @@ namespace AZ
DestroyReflectionManager();
static_cast<SettingsRegistryImpl*>(m_settingsRegistry.get())->ClearNotifiers();
static_cast<SettingsRegistryImpl*>(m_settingsRegistry.get())->ClearMergeEvents();
// Uninit and unload any dynamic modules.
m_moduleManager->UnloadModules();
@@ -123,6 +123,36 @@ namespace AZ
using NotifyEvent = AZ::Event<AZStd::string_view, Type>;
using NotifyEventHandler = typename NotifyEvent::Handler;
using PreMergeEventCallback = AZStd::function<void(AZStd::string_view path, AZStd::string_view rootKey)>;
using PostMergeEventCallback = AZStd::function<void(AZStd::string_view path, AZStd::string_view rootKey)>;
using PreMergeEvent = AZ::Event<AZStd::string_view, AZStd::string_view>;
using PostMergeEvent = AZ::Event<AZStd::string_view, AZStd::string_view>;
using PreMergeEventHandler = typename PreMergeEvent::Handler;
using PostMergeEventHandler = typename PostMergeEvent::Handler;
struct ScopedMergeEvent
{
ScopedMergeEvent(
PreMergeEvent& preMergeEvent, PostMergeEvent& postMergeEvent, AZStd::string_view filePath, AZStd::string_view rootKey)
: m_preMergeEvent{ preMergeEvent }
, m_postMergeEvent{ postMergeEvent }
, m_filePath{ filePath }
, m_rootKey{ rootKey }
{
preMergeEvent.Signal(m_filePath, m_rootKey);
}
~ScopedMergeEvent()
{
m_postMergeEvent.Signal(m_filePath, m_rootKey);
}
PreMergeEvent& m_preMergeEvent;
PostMergeEvent& m_postMergeEvent;
AZStd::string_view m_filePath;
AZStd::string_view m_rootKey;
};
using VisitorCallback =
AZStd::function<VisitResponse(AZStd::string_view path, AZStd::string_view valueName, VisitAction action, Type type)>;
//! Base class for the visitor class during traversal over the Settings Registry. The type-agnostic function is always
@@ -169,6 +199,20 @@ namespace AZ
//! @callback The function to call when an entry gets a new/updated value.
[[nodiscard]] virtual NotifyEventHandler RegisterNotifier(NotifyCallback&& callback) = 0;
//! Register a callback that will be called before an entry is merged.
//! @callback The function to call when an entry gets a new/updated value.
[[nodiscard]] virtual PreMergeEventHandler RegisterPreMergeEvent(const PreMergeEventCallback& callback) = 0;
//! Register a callback that will be called before an entry is merged.
//! @callback The function to call when an entry gets a new/updated value.
[[nodiscard]] virtual PreMergeEventHandler RegisterPreMergeEvent (PreMergeEventCallback&& callback) = 0;
//! Register a callback that will be called after an entry is merged.
//! @callback The function to call when an entry gets a new/updated value.
[[nodiscard]] virtual PostMergeEventHandler RegisterPostMergeEvent(const PostMergeEventCallback& callback) = 0;
//! Register a callback that will be called after an entry is merged.
//! @callback The function to call when an entry gets a new/updated value.
[[nodiscard]] virtual PostMergeEventHandler RegisterPostMergeEvent (PostMergeEventCallback&& callback) = 0;
//! Gets the boolean value at the provided path.
//! @param result The target to write the result to.
//! @param path The path to the value.
@@ -229,6 +229,53 @@ namespace AZ
m_notifiers.DisconnectAllHandlers();
}
auto SettingsRegistryImpl::RegisterPreMergeEvent(const PreMergeEventCallback& callback) -> PreMergeEventHandler
{
PreMergeEventHandler preMergeHandler{ callback };
{
AZStd::scoped_lock lock(m_settingMutex);
preMergeHandler.Connect(m_preMergeEvent);
}
return preMergeHandler;
}
auto SettingsRegistryImpl::RegisterPreMergeEvent(PreMergeEventCallback&& callback) -> PreMergeEventHandler
{
PreMergeEventHandler preMergeHandler{ AZStd::move(callback) };
{
AZStd::scoped_lock lock(m_settingMutex);
preMergeHandler.Connect(m_preMergeEvent);
}
return preMergeHandler;
}
auto SettingsRegistryImpl::RegisterPostMergeEvent(const PostMergeEventCallback& callback) -> PostMergeEventHandler
{
PostMergeEventHandler postMergeHandler{ callback };
{
AZStd::scoped_lock lock(m_settingMutex);
postMergeHandler.Connect(m_postMergeEvent);
}
return postMergeHandler;
}
auto SettingsRegistryImpl::RegisterPostMergeEvent(PostMergeEventCallback&& callback) -> PostMergeEventHandler
{
PostMergeEventHandler postMergeHandler{ AZStd::move(callback) };
{
AZStd::scoped_lock lock(m_settingMutex);
postMergeHandler.Connect(m_postMergeEvent);
}
return postMergeHandler;
}
void SettingsRegistryImpl::ClearMergeEvents()
{
AZStd::scoped_lock lock(m_settingMutex);
m_preMergeEvent.DisconnectAllHandlers();
m_postMergeEvent.DisconnectAllHandlers();
}
SettingsRegistryInterface::Type SettingsRegistryImpl::GetType(AZStd::string_view path) const
{
if (path.empty())
@@ -1115,6 +1162,8 @@ namespace AZ
return false;
}
ScopedMergeEvent(m_preMergeEvent, m_postMergeEvent, path, rootKey);
JsonSerializationResult::ResultCode mergeResult(JsonSerializationResult::Tasks::Merge);
if (rootKey.empty())
{
@@ -48,6 +48,12 @@ namespace AZ
[[nodiscard]] NotifyEventHandler RegisterNotifier(NotifyCallback&& callback) override;
void ClearNotifiers();
[[nodiscard]] PreMergeEventHandler RegisterPreMergeEvent(const PreMergeEventCallback& callback) override;
[[nodiscard]] PreMergeEventHandler RegisterPreMergeEvent(PreMergeEventCallback&& callback) override;
[[nodiscard]] PostMergeEventHandler RegisterPostMergeEvent(const PostMergeEventCallback& callback) override;
[[nodiscard]] PostMergeEventHandler RegisterPostMergeEvent(PostMergeEventCallback&& callback) override;
void ClearMergeEvents();
bool Get(bool& result, AZStd::string_view path) const override;
bool Get(s64& result, AZStd::string_view path) const override;
bool Get(u64& result, AZStd::string_view path) const override;
@@ -103,6 +109,9 @@ namespace AZ
mutable AZStd::recursive_mutex m_settingMutex;
NotifyEvent m_notifiers;
PreMergeEvent m_preMergeEvent;
PostMergeEvent m_postMergeEvent;
rapidjson::Document m_settings;
JsonSerializerSettings m_serializationSettings;
JsonDeserializerSettings m_deserializationSettings;
@@ -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