Skip to content

Commit

Permalink
[SDK-454] feature/settings-handler-refactor (#124)
Browse files Browse the repository at this point in the history
- refactored settings handler and separated classes
  • Loading branch information
HarrisonHough authored Sep 21, 2023
1 parent eef533e commit 25f78e0
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 59 deletions.
10 changes: 5 additions & 5 deletions Editor/Analytics/AnalyticsEditorLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ReadyPlayerMe.Core.Analytics
using ReadyPlayerMe.Core.Editor;

namespace ReadyPlayerMe.Core.Analytics
{
public static class AnalyticsEditorLogger
{
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Editor/BuildPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/Module Management/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static ModuleInstaller()
private static void DelayCreateCoreSettings()
{
EditorApplication.delayCall -= DelayCreateCoreSettings;
CoreSettingsHandler.EnsureSettingsExist();
CoreSettingsLoader.EnsureSettingsExist();
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Editor/UI/EditorWindows/SettingsWindow/SettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private void OnLoggingToggle(ChangeEvent<bool> evt)
{
AnalyticsEditorLogger.EventLogger.LogSetLoggingEnabled(evt.newValue);
SDKLogger.EnableLogging(evt.newValue);
CoreSettingsSetter.SetEnableLogging(evt.newValue);
}

private void OnPrivacyPolicyClicked(MouseUpEvent evt)
Expand Down
2 changes: 1 addition & 1 deletion Editor/UI/EditorWindows/Templates/AppIdTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void SaveAppId()
AnalyticsEditorLogger.EventLogger.LogUpdatePartnerURL(id, appIdField.value);
}

CoreSettingsHandler.SaveAppId(appIdField.value);
CoreSettingsSetter.SaveAppId(appIdField.value);
}
}
}
2 changes: 1 addition & 1 deletion Editor/UI/EditorWindows/Templates/SubdomainTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Editor/UI/Obsolete/Components/SubdomainField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
35 changes: 35 additions & 0 deletions Editor/Utils/CoreSettingsLoader.cs
Original file line number Diff line number Diff line change
@@ -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<CoreSettings>();
AssetDatabase.CreateAsset(newSettings, PROJECT_RELATIVE_ASSET_PATH);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
11 changes: 11 additions & 0 deletions Editor/Utils/CoreSettingsLoader.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Editor/Utils/CoreSettingsSetter.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
11 changes: 11 additions & 0 deletions Editor/Utils/CoreSettingsSetter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 4 additions & 45 deletions Runtime/CoreSettingsHandler.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,30 @@
using ReadyPlayerMe.Core.Data;
using UnityEditor;
using UnityEngine;

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<CoreSettings>(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<CoreSettings>(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<CoreSettings>();
AssetDatabase.CreateAsset(newSettings, PROJECT_RELATIVE_ASSET_PATH);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return newSettings;
return Resources.Load<CoreSettings>(RESOURCE_PATH);
}
#endif
}
}
4 changes: 0 additions & 4 deletions Runtime/Logger/SDKLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 25f78e0

Please sign in to comment.