Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/rework/persistent-property'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Assets/C's Utils/Runtime/Systems/Data Saving/GameData.cs
  • Loading branch information
R-C137 committed Nov 27, 2024
2 parents b3ec422 + 53d4cb6 commit c012a13
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
33 changes: 32 additions & 1 deletion Assets/C's Utils/Runtime/Systems/Data Saving/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@
*
* [24/11/2024] - Fixed typo in a field name (C137)
* [27/11/2024] - Improved logging of clashing sections (C137)
* - Support for loading classes & structs (C137)
* - Fixed saving lists & arrays (C137)
*
*/

using CsUtils.Systems.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json.Linq;
using Unity.Plastic.Newtonsoft.Json;
using UnityEngine;

namespace CsUtils.Systems.DataSaving
{

[UnityEngine.DefaultExecutionOrder(-20), UnityEngine.ExecuteAlways]
public class GameData : MonoBehaviour
{
Expand Down Expand Up @@ -335,8 +338,36 @@ public static bool FixTypeCasting<T>(object value, out T destination)
destination = (T)Convert.ChangeType(scriptableObject, typeof(T));
return true;
}

if(value is JObject jObject)
{
destination = jObject.ToObject<T>();
return true;
}

if(value is JArray jArray)
{
T array = jArray.ToObject<T>();

// if(array is IEnumerable enumerable)
// {
// List<object> values = enumerable.Cast<object>().ToList();
//
// for(int i = 0; i < values.Count; i++)
// {
// object obj = values[i];
// FixTypeCasting(obj, out obj);
//
// values[i] = obj;
// }
// }

destination = array;
return true;
}

destination = (T)value;

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
* [23/11/2024] - Added support for data obfuscation (C137)
* [24/11/2024] - Added methods for getting & settings raw file data (C137)
* [27/11/2024] - Removed data change checks (C137)
*
*/
using Newtonsoft.Json;
using System.Collections.Generic;
Expand Down Expand Up @@ -134,10 +136,6 @@ public bool TryGet<T>(string id, out T value)
/// <returns>The data that was saved</returns>
public T Set<T>(string id, T value)
{
//Prevents serializing all of the data unnecessarily
if (data.TryGetValue(id, out object previousValue) && previousValue == (object)value)
return value;

data[id] = value;

SaveData();
Expand Down
21 changes: 19 additions & 2 deletions Assets/C's Utils/Runtime/Systems/Data Saving/PersistentProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* Changes:
* [04/01/2024] - Initial implementation (C137)
* [05/01/2024] - Added missing namespace (C137)
* [27/11/2024] - Save data can now be manually loaded & saved (C137)
*
*/
namespace CsUtils.Systems.DataSaving
{
Expand Down Expand Up @@ -60,7 +62,7 @@ public PersistentProperty(string dataID, T defaultValue = default, string sectio
this.sectionID = sectionID;
this.dataID = dataID;

UpdateData(defaultValue);
UpdateFromDisk(defaultValue);
GameData.persistentDataSections[sectionID].onDataUpdated += DataUpdated;
}

Expand All @@ -80,10 +82,25 @@ private void DataUpdated(string id, object data)
_data = (T)data;
}

/// <summary>
/// Updates the data on disk with the provided one
/// </summary>
/// <param name="value">The data to update the disk with</param>
public void UpdateToDisk(T value) => GameData.Set(dataID, value, sectionID);

/// <summary>
/// Updates the current memory value to disk<br></br>
/// Useful when the property is a class.
/// </summary>
public void UpdateToDisk()
{
UpdateToDisk(Value);
}

/// <summary>
/// Manually updates the cached value of the data
/// </summary>
public void UpdateData(T defaultValue = default)
public void UpdateFromDisk(T defaultValue = default)
{
_data = GameData.Get(dataID, sectionID, defaultValue);
}
Expand Down
17 changes: 13 additions & 4 deletions Assets/C's Utils/Testing/SoSavingTest.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
using System;
using System.Collections.Generic;
using CsUtils;
using CsUtils.Systems.DataSaving;
using UnityEngine;

public class testing
{
public string data = "none";
}
public class SoSavingTest : MonoBehaviour
{
public PersistentProperty<ItemBaseTest> so;
public PersistentProperty<List<List<testing>>> so;

public ItemBaseTest item;

public ItemBaseTest savedItem;

private void Start()
{
so = new("testing.so.item", savedItem);
so = new("testing.so.item", new());
}

private void Update()
{
if(InputQuery.GetKeyDown(KeyCode.K))
so.Value = savedItem;
{
so.Value.Add(new List<testing>(){ new (){data = "testing data"}});
so.UpdateToDisk();
}
if(InputQuery.GetKeyDown(KeyCode.L))
savedItem = so.Value;
{
}
}
}

0 comments on commit c012a13

Please sign in to comment.