Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force save settings #41

Merged
merged 3 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ MonoBehaviour:
m_state: 0
m_list: []
m_list2: []
m_vector3: {x: 9.78, y: 0.54, z: 1.43}
m_vector3: {x: 33, y: 0.54, z: 1.43}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override bool Exists()
return File.Exists(AssetPath);
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
if (data == null) throw new ArgumentNullException(nameof(data));

Expand All @@ -67,7 +67,10 @@ protected override void OnSaveSettings(TData data)
AssetDatabase.ImportAsset(AssetPath);
}

AssetDatabase.SaveAssets();
if (force)
{
AssetDatabase.SaveAssets();
}
}
}

Expand All @@ -77,7 +80,7 @@ protected override TData OnLoadSettings()
{
var data = ScriptableObject.CreateInstance<TData>();

OnSaveSettings(data);
OnSaveSettings(data, true);
}

return HasExternalPath ? EditorYamlUtility.FromYamlAtPath<TData>(AssetPath) : AssetDatabase.LoadAssetAtPath<TData>(AssetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override bool Exists()
return EditorPrefs.HasKey(Key);
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
if (data == null) throw new ArgumentNullException(nameof(data));

Expand Down
4 changes: 3 additions & 1 deletion Packages/UGF.CustomSettings/Editor/CustomSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public override void OnDeactivate()
ClearEditor();

base.OnDeactivate();

Settings.SaveSettings();
}

public override void OnGUI(string searchContext)
Expand All @@ -57,7 +59,7 @@ public override void OnGUI(string searchContext)

if (EditorGUI.EndChangeCheck())
{
Settings.SaveSettings();
Settings.SaveSettings(false);
}
}

Expand Down
10 changes: 10 additions & 0 deletions Packages/UGF.CustomSettings/Runtime/CustomSettings.Deprecated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ public abstract partial class CustomSettings<TData>
{
[Obsolete("Property Data has been deprecated. Use GetData() method instead.")]
public virtual TData Data { get { return GetData(); } }

/// <summary>
/// Override this method to implement saving of the data.
/// </summary>
/// <param name="data">The data to save.</param>
[Obsolete("Method OnSaveSettings(T data) has been deprecated. Use OnSaveSettings(T data, bool force) instead.")]
protected virtual void OnSaveSettings(TData data)
{
OnSaveSettings(data, true);
}
}
}

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

8 changes: 5 additions & 3 deletions Packages/UGF.CustomSettings/Runtime/CustomSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public virtual bool Exists()
/// <remarks>
/// <see cref="CanSave"/> determines whether settings can be saved.
/// </remarks>
public void SaveSettings()
/// <param name="force">The value that determines whether to force data serialization.</param>
public void SaveSettings(bool force = true)
{
if (CanSave())
{
if (m_data == null) throw new ArgumentException($"Data of '{GetType()}' not specified.");

OnSaveSettings(m_data);
OnSaveSettings(m_data, force);

Saved?.Invoke(m_data);
}
Expand Down Expand Up @@ -131,7 +132,8 @@ protected virtual TData OnGetData()
/// Override this method to implement saving of the data.
/// </summary>
/// <param name="data">The data to save.</param>
protected virtual void OnSaveSettings(TData data)
/// <param name="force">The value that determines whether to force data serialization.</param>
protected virtual void OnSaveSettings(TData data, bool force)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Packages/UGF.CustomSettings/Runtime/CustomSettingsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override bool Exists()
return File.Exists(FilePath);
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
if (data == null) throw new ArgumentNullException(nameof(data));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override bool Exists()
return File.Exists(AssetPath);
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
if (data == null) throw new ArgumentNullException(nameof(data));

Expand All @@ -30,7 +30,10 @@ protected override void OnSaveSettings(TData data)
AssetDatabase.ImportAsset(AssetPath);
}

AssetDatabase.SaveAssets();
if (force)
{
AssetDatabase.SaveAssets();
}
}

protected override TData OnLoadSettings()
Expand All @@ -39,7 +42,7 @@ protected override TData OnLoadSettings()
{
var data = ScriptableObject.CreateInstance<TData>();

OnSaveSettings(data);
OnSaveSettings(data, true);
}

return base.OnLoadSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace UGF.CustomSettings.Runtime
{
public partial class CustomSettingsPrefs<TData>
{
/// <summary>
/// Gets the value that determines whether to force player prefs saving each time when settings saving performed.
/// </summary>
[Obsolete("Property ForceSave has been deprecated.")]
public bool ForceSave { get; }

/// <summary>
/// Creates settings with the specified player prefs key.
/// </summary>
/// <param name="key">The key of the data in player prefs.</param>
/// <param name="forceSave">The value that determines whether to save player prefs when settings saving performed.</param>
[Obsolete("Constructor CustomSettingsPrefs(string key, bool forceSave) has been deprecated.")]
public CustomSettingsPrefs(string key, bool forceSave)
{
if (string.IsNullOrEmpty(key)) throw new ArgumentException("The prefs key cannot be null or empty.", nameof(key));

Key = key;
ForceSave = forceSave;
}
}
}

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

19 changes: 4 additions & 15 deletions Packages/UGF.CustomSettings/Runtime/CustomSettingsPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,37 @@ namespace UGF.CustomSettings.Runtime
/// <summary>
/// Represents custom settings which stores data at specified key in player prefs as Json representation.
/// </summary>
public class CustomSettingsPrefs<TData> : CustomSettingsPlayMode<TData> where TData : ScriptableObject
public partial class CustomSettingsPrefs<TData> : CustomSettingsPlayMode<TData> where TData : ScriptableObject
{
/// <summary>
/// Gets the key which used to store data in player prefs.
/// </summary>
public string Key { get; }

/// <summary>
/// Gets the value that determines whether to force player prefs saving each time when settings saving performed.
/// </summary>
public bool ForceSave { get; }

/// <summary>
/// Creates settings with the specified player prefs key.
/// </summary>
/// <param name="key">The key of the data in player prefs.</param>
/// <param name="forceSave">The value that determines whether to save player prefs when settings saving performed.</param>
public CustomSettingsPrefs(string key, bool forceSave = false)
public CustomSettingsPrefs(string key)
{
if (string.IsNullOrEmpty(key)) throw new ArgumentException("The prefs key cannot be null or empty.", nameof(key));

Key = key;
ForceSave = forceSave;
}

public override bool Exists()
{
return PlayerPrefs.HasKey(Key);
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
if (data == null) throw new ArgumentNullException(nameof(data));

string text = JsonUtility.ToJson(data);

PlayerPrefs.SetString(Key, text);

if (ForceSave)
{
PlayerPrefs.Save();
}
PlayerPrefs.Save();
}

protected override TData OnLoadSettings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override bool Exists()
return Resources.Load<TData>(ResourcesPath) != null;
}

protected override void OnSaveSettings(TData data)
protected override void OnSaveSettings(TData data, bool force)
{
}

Expand Down