From 25f78e0adce8f05a8aaa5583fdd9202b2dc4d58d Mon Sep 17 00:00:00 2001 From: Harrison Hough Date: Thu, 21 Sep 2023 08:52:30 +0300 Subject: [PATCH] [SDK-454] feature/settings-handler-refactor (#124) - refactored settings handler and separated classes --- Editor/Analytics/AnalyticsEditorLogger.cs | 10 ++-- Editor/BuildPostProcessor.cs | 2 +- Editor/Module Management/ModuleInstaller.cs | 2 +- .../SettingsWindow/SettingsEditor.cs | 1 + .../EditorWindows/Templates/AppIdTemplate.cs | 2 +- .../Templates/SubdomainTemplate.cs | 2 +- .../UI/Obsolete/Components/SubdomainField.cs | 2 +- Editor/Utils/CoreSettingsLoader.cs | 35 +++++++++++++ Editor/Utils/CoreSettingsLoader.cs.meta | 11 +++++ Editor/Utils/CoreSettingsSetter.cs | 38 ++++++++++++++ Editor/Utils/CoreSettingsSetter.cs.meta | 11 +++++ Runtime/CoreSettingsHandler.cs | 49 ++----------------- Runtime/Logger/SDKLogger.cs | 4 -- 13 files changed, 110 insertions(+), 59 deletions(-) create mode 100644 Editor/Utils/CoreSettingsLoader.cs create mode 100644 Editor/Utils/CoreSettingsLoader.cs.meta create mode 100644 Editor/Utils/CoreSettingsSetter.cs create mode 100644 Editor/Utils/CoreSettingsSetter.cs.meta diff --git a/Editor/Analytics/AnalyticsEditorLogger.cs b/Editor/Analytics/AnalyticsEditorLogger.cs index 4b262401..8db355d9 100644 --- a/Editor/Analytics/AnalyticsEditorLogger.cs +++ b/Editor/Analytics/AnalyticsEditorLogger.cs @@ -1,4 +1,6 @@ -namespace ReadyPlayerMe.Core.Analytics +using ReadyPlayerMe.Core.Editor; + +namespace ReadyPlayerMe.Core.Analytics { public static class AnalyticsEditorLogger { @@ -16,16 +18,14 @@ public static void Enable() { IsEnabled = true; EventLogger.Enable(); - CoreSettingsHandler.CoreSettings.EnableAnalytics = true; - CoreSettingsHandler.Save(); + CoreSettingsSetter.SetEnableAnalytics(true); } public static void Disable() { EventLogger.Disable(); IsEnabled = false; - CoreSettingsHandler.CoreSettings.EnableAnalytics = false; - CoreSettingsHandler.Save(); + CoreSettingsSetter.SetEnableAnalytics(false); } } } diff --git a/Editor/BuildPostProcessor.cs b/Editor/BuildPostProcessor.cs index ea9f95e1..ed5d3e6e 100644 --- a/Editor/BuildPostProcessor.cs +++ b/Editor/BuildPostProcessor.cs @@ -11,7 +11,7 @@ public static class BuildPostProcessor public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { // create asset if it has been deleted - CoreSettingsHandler.EnsureSettingsExist(); + CoreSettingsLoader.EnsureSettingsExist(); AppData appData = ApplicationData.GetData(); AnalyticsEditorLogger.EventLogger.LogBuildApplication(appData.BuildTarget, PlayerSettings.productName, !Debug.isDebugBuild); } diff --git a/Editor/Module Management/ModuleInstaller.cs b/Editor/Module Management/ModuleInstaller.cs index 10b2d9bf..efceb167 100644 --- a/Editor/Module Management/ModuleInstaller.cs +++ b/Editor/Module Management/ModuleInstaller.cs @@ -62,7 +62,7 @@ static ModuleInstaller() private static void DelayCreateCoreSettings() { EditorApplication.delayCall -= DelayCreateCoreSettings; - CoreSettingsHandler.EnsureSettingsExist(); + CoreSettingsLoader.EnsureSettingsExist(); } /// diff --git a/Editor/UI/EditorWindows/SettingsWindow/SettingsEditor.cs b/Editor/UI/EditorWindows/SettingsWindow/SettingsEditor.cs index c1f5fe87..7fc855ed 100644 --- a/Editor/UI/EditorWindows/SettingsWindow/SettingsEditor.cs +++ b/Editor/UI/EditorWindows/SettingsWindow/SettingsEditor.cs @@ -113,6 +113,7 @@ private void OnLoggingToggle(ChangeEvent evt) { AnalyticsEditorLogger.EventLogger.LogSetLoggingEnabled(evt.newValue); SDKLogger.EnableLogging(evt.newValue); + CoreSettingsSetter.SetEnableLogging(evt.newValue); } private void OnPrivacyPolicyClicked(MouseUpEvent evt) diff --git a/Editor/UI/EditorWindows/Templates/AppIdTemplate.cs b/Editor/UI/EditorWindows/Templates/AppIdTemplate.cs index 62e05e96..768ae134 100644 --- a/Editor/UI/EditorWindows/Templates/AppIdTemplate.cs +++ b/Editor/UI/EditorWindows/Templates/AppIdTemplate.cs @@ -60,7 +60,7 @@ private void SaveAppId() AnalyticsEditorLogger.EventLogger.LogUpdatePartnerURL(id, appIdField.value); } - CoreSettingsHandler.SaveAppId(appIdField.value); + CoreSettingsSetter.SaveAppId(appIdField.value); } } } diff --git a/Editor/UI/EditorWindows/Templates/SubdomainTemplate.cs b/Editor/UI/EditorWindows/Templates/SubdomainTemplate.cs index 45df7448..16c0c2a5 100644 --- a/Editor/UI/EditorWindows/Templates/SubdomainTemplate.cs +++ b/Editor/UI/EditorWindows/Templates/SubdomainTemplate.cs @@ -116,7 +116,7 @@ private void SaveSubdomain() var subDomain = CoreSettingsHandler.CoreSettings.Subdomain; if (subDomain == partnerSubdomain || !IsValidSubdomain()) return; AnalyticsEditorLogger.EventLogger.LogUpdatePartnerURL(subDomain, partnerSubdomain); - CoreSettingsHandler.SaveSubDomain(partnerSubdomain); + CoreSettingsSetter.SaveSubDomain(partnerSubdomain); } } } diff --git a/Editor/UI/Obsolete/Components/SubdomainField.cs b/Editor/UI/Obsolete/Components/SubdomainField.cs index 677ec47d..62e1aedf 100644 --- a/Editor/UI/Obsolete/Components/SubdomainField.cs +++ b/Editor/UI/Obsolete/Components/SubdomainField.cs @@ -141,7 +141,7 @@ public void SaveSubdomain() var subDomain = CoreSettingsHandler.CoreSettings.Subdomain; if (subDomain == partnerSubdomain || !IsValidSubdomain()) return; AnalyticsEditorLogger.EventLogger.LogUpdatePartnerURL(subDomain, partnerSubdomain); - CoreSettingsHandler.SaveSubDomain(partnerSubdomain); + CoreSettingsSetter.SaveSubDomain(partnerSubdomain); } } } diff --git a/Editor/Utils/CoreSettingsLoader.cs b/Editor/Utils/CoreSettingsLoader.cs new file mode 100644 index 00000000..088350e2 --- /dev/null +++ b/Editor/Utils/CoreSettingsLoader.cs @@ -0,0 +1,35 @@ +using ReadyPlayerMe.Core.Data; +using UnityEditor; +using UnityEngine; + +namespace ReadyPlayerMe.Core.Editor +{ + [InitializeOnLoad] + public static class CoreSettingsLoader + { + private const string PROJECT_RELATIVE_ASSET_PATH = "Assets/Ready Player Me/Resources/Settings/CoreSettings.asset"; + private const string SETTINGS_SAVE_FOLDER = "Ready Player Me/Resources/Settings"; + + static CoreSettingsLoader() + { + EnsureSettingsExist(); + } + + public static void EnsureSettingsExist() + { + if (CoreSettingsHandler.CoreSettings == null) + { + CreateSettings(); + } + } + + private static void CreateSettings() + { + DirectoryUtility.ValidateDirectory($"{Application.dataPath}/{SETTINGS_SAVE_FOLDER}"); + var newSettings = ScriptableObject.CreateInstance(); + AssetDatabase.CreateAsset(newSettings, PROJECT_RELATIVE_ASSET_PATH); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + } +} \ No newline at end of file diff --git a/Editor/Utils/CoreSettingsLoader.cs.meta b/Editor/Utils/CoreSettingsLoader.cs.meta new file mode 100644 index 00000000..5c715adc --- /dev/null +++ b/Editor/Utils/CoreSettingsLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: da717eefac74935459a3f995092c5463 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Utils/CoreSettingsSetter.cs b/Editor/Utils/CoreSettingsSetter.cs new file mode 100644 index 00000000..7d596408 --- /dev/null +++ b/Editor/Utils/CoreSettingsSetter.cs @@ -0,0 +1,38 @@ +using UnityEditor; + +namespace ReadyPlayerMe.Core.Editor +{ + public static class CoreSettingsSetter + { + public static void SetEnableAnalytics(bool isEnabled) + { + CoreSettingsHandler.CoreSettings.EnableAnalytics = isEnabled; + Save(); + } + + public static void SetEnableLogging(bool isEnabled) + { + CoreSettingsHandler.CoreSettings.EnableLogging = isEnabled; + Save(); + } + + public static void SaveSubDomain(string subDomain) + { + if (string.IsNullOrEmpty(subDomain) || CoreSettingsHandler.CoreSettings.Subdomain == subDomain) return; + CoreSettingsHandler.CoreSettings.Subdomain = subDomain; + Save(); + } + + public static void SaveAppId(string appId) + { + CoreSettingsHandler.CoreSettings.AppId = appId; + Save(); + } + + public static void Save() + { + EditorUtility.SetDirty(CoreSettingsHandler.CoreSettings); + AssetDatabase.SaveAssets(); + } + } +} diff --git a/Editor/Utils/CoreSettingsSetter.cs.meta b/Editor/Utils/CoreSettingsSetter.cs.meta new file mode 100644 index 00000000..61c5ac5e --- /dev/null +++ b/Editor/Utils/CoreSettingsSetter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 79be5f9e434a00b46bacd5a47871b69a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/CoreSettingsHandler.cs b/Runtime/CoreSettingsHandler.cs index c923b537..ba0b578d 100644 --- a/Runtime/CoreSettingsHandler.cs +++ b/Runtime/CoreSettingsHandler.cs @@ -1,5 +1,4 @@ using ReadyPlayerMe.Core.Data; -using UnityEditor; using UnityEngine; namespace ReadyPlayerMe.Core @@ -7,65 +6,25 @@ namespace ReadyPlayerMe.Core public static class CoreSettingsHandler { private const string RESOURCE_PATH = "Settings/CoreSettings"; - public const string PROJECT_RELATIVE_ASSET_PATH = "Assets/Ready Player Me/Resources/Settings/CoreSettings.asset"; - private const string SETTINGS_SAVE_FOLDER = "Ready Player Me/Resources/Settings"; - public static CoreSettings CoreSettings { get { if (coreSettings != null) return coreSettings; - coreSettings = Resources.Load(RESOURCE_PATH); -#if UNITY_EDITOR + coreSettings = Load(); if (coreSettings == null) { - coreSettings = CreateSettings(); + Debug.LogError("CoreSettings could not be loaded."); } -#endif return coreSettings; } } private static CoreSettings coreSettings; -#if UNITY_EDITOR - public static void SaveSubDomain(string subDomain) - { - if (string.IsNullOrEmpty(subDomain) || coreSettings.Subdomain == subDomain) return; - coreSettings.Subdomain = subDomain; - Save(); - } - - public static void SaveAppId(string appId) - { - coreSettings.AppId = appId; - Save(); - } - - public static void Save() - { - EditorUtility.SetDirty(coreSettings); - AssetDatabase.SaveAssets(); - } - - public static void EnsureSettingsExist() - { - coreSettings = Resources.Load(RESOURCE_PATH); - if (coreSettings == null) - { - coreSettings = CreateSettings(); - } - } - - private static CoreSettings CreateSettings() + public static CoreSettings Load() { - DirectoryUtility.ValidateDirectory($"{Application.dataPath}/{SETTINGS_SAVE_FOLDER}"); - var newSettings = ScriptableObject.CreateInstance(); - AssetDatabase.CreateAsset(newSettings, PROJECT_RELATIVE_ASSET_PATH); - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - return newSettings; + return Resources.Load(RESOURCE_PATH); } -#endif } } diff --git a/Runtime/Logger/SDKLogger.cs b/Runtime/Logger/SDKLogger.cs index 12f09cd4..a5b3faee 100644 --- a/Runtime/Logger/SDKLogger.cs +++ b/Runtime/Logger/SDKLogger.cs @@ -25,14 +25,10 @@ public static bool IsLoggingEnabled() return CoreSettingsHandler.CoreSettings.EnableLogging; } -#if UNITY_EDITOR public static void EnableLogging(bool enabled) { loggingEnabled = enabled; - CoreSettingsHandler.CoreSettings.EnableLogging = loggingEnabled; - CoreSettingsHandler.Save(); } -#endif public static void Log(string tag, object message) {