Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 64a2fd7

Browse files
committed
feat: save and load to file instead of playerprefs
1 parent 15cc981 commit 64a2fd7

File tree

4 files changed

+71
-26
lines changed

4 files changed

+71
-26
lines changed

Packages/com.nuclearband.sodatabase/Editor/ScriptableObjectDatabaseEditorWindow.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#if UNITY_EDITOR
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
7+
using System.Reflection;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Serialization;
610
using Sirenix.OdinInspector.Editor;
711
using Sirenix.Utilities;
812
using Sirenix.Utilities.Editor;
@@ -27,6 +31,35 @@ private static void Open()
2731
};
2832
}
2933

34+
[MenuItem("Tools/NuclearBand/ScriptableObjectDatabase-ClearSave")]
35+
private static async void ClearSave()
36+
{
37+
File.Delete(SODatabase.SavePath);
38+
await SODatabase.InitAsync(null, null);
39+
await SODatabase.LoadAsync();
40+
var models = SODatabase.GetModels<DataNode>("", true);
41+
foreach (var model in models)
42+
{
43+
var typeInfo = model.GetType().GetTypeInfo();
44+
var fields = typeInfo.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
45+
foreach (var field in fields)
46+
{
47+
var attributes = field.GetCustomAttributes(typeof(JsonPropertyAttribute), false);
48+
if (attributes.Length > 0)
49+
field.SetValue(model, default);
50+
}
51+
52+
EditorUtility.SetDirty(model);
53+
}
54+
AssetDatabase.SaveAssets();
55+
}
56+
57+
[MenuItem("Tools/NuclearBand/ScriptableObjectDatabase-OpenSaveFolder")]
58+
private static void OpenSaveFolder()
59+
{
60+
EditorUtility.RevealInFinder(Application.persistentDataPath);
61+
}
62+
3063
protected override OdinMenuTree BuildMenuTree()
3164
{
3265
var tree = new OdinMenuTree(true)

Packages/com.nuclearband.sodatabase/Runtime/SODatabase.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable enable
22
using System;
33
using System.Collections.Generic;
4+
using System.IO;
45
using System.Linq;
56
using System.Threading.Tasks;
67
using Newtonsoft.Json;
@@ -12,12 +13,15 @@ namespace NuclearBand
1213
{
1314
public static class SODatabase
1415
{
16+
public static string SavePath => Application.persistentDataPath + @"/save.txt";
17+
1518
private static FolderHolder root = null!;
1619

1720
public static async void Init(Action<float>? onProgress, Action? onComplete)
1821
{
1922
await InitAsync(onProgress, onComplete);
2023
}
24+
2125
public static async Task InitAsync(Action<float>? onProgress, Action? onComplete)
2226
{
2327
var loadHandler = Addressables.LoadResourceLocationsAsync(SODatabaseSettings.Label);
@@ -26,16 +30,11 @@ public static async Task InitAsync(Action<float>? onProgress, Action? onComplete
2630
{
2731
while (!loadHandler.IsDone)
2832
{
29-
CallAction(() =>
30-
{
31-
onProgress?.Invoke(loadHandler.PercentComplete);
32-
});
33+
CallAction(() => { onProgress?.Invoke(loadHandler.PercentComplete); });
3334
await Task.Delay(50);
3435
}
35-
CallAction(() =>
36-
{
37-
onProgress?.Invoke(loadHandler.PercentComplete);
38-
});
36+
37+
CallAction(() => { onProgress?.Invoke(loadHandler.PercentComplete); });
3938
});
4039
#pragma warning restore 4014
4140
var resourceLocations = await loadHandler.Task;
@@ -67,6 +66,7 @@ public static async Task InitAsync(Action<float>? onProgress, Action? onComplete
6766
static async void CallAction(Action? action)
6867
{
6968
action?.Invoke();
69+
await Task.CompletedTask;
7070
}
7171

7272
public static T GetModel<T>(string path) where T : DataNode
@@ -87,14 +87,10 @@ public static List<T> GetModels<T>(string path, bool includeSubFolders = false)
8787
if (path != string.Empty)
8888
for (var i = 0; i < pathElements.Length; i++)
8989
{
90-
try
91-
{
90+
if (curFolder.FolderHolders.ContainsKey(pathElements[i]))
9291
curFolder = curFolder.FolderHolders[pathElements[i]];
93-
}
94-
catch (Exception e)
95-
{
96-
97-
}
92+
else
93+
break;
9894
}
9995

10096
var res = curFolder.DataNodes.Values.OfType<T>().ToList();
@@ -111,12 +107,16 @@ public static List<T> GetModels<T>(string path, bool includeSubFolders = false)
111107
return res;
112108
}
113109

114-
public static void Save()
110+
public static async void Save()
111+
{
112+
await SaveAsync();
113+
}
114+
115+
public static async Task SaveAsync()
115116
{
116117
var res = SaveFolderHolder(root, string.Empty);
117-
foreach (var pair in res)
118-
PlayerPrefs.SetString(pair.Key, pair.Value);
119-
PlayerPrefs.Save();
118+
using var fileStream = new StreamWriter(SavePath);
119+
await fileStream.WriteAsync(JsonConvert.SerializeObject(res));
120120
}
121121

122122
static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, string path)
@@ -143,19 +143,31 @@ static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, st
143143
return res;
144144
}
145145

146-
public static void Load()
146+
public static async void Load()
147147
{
148-
LoadFolderHolder(root, string.Empty);
148+
await LoadAsync();
149149
}
150150

151-
static void LoadFolderHolder(FolderHolder folderHolder, string path)
151+
public static async Task LoadAsync()
152+
{
153+
if (!File.Exists(SavePath))
154+
return;
155+
156+
using var fileStream = new StreamReader(SavePath);
157+
var serializedDictionary = await fileStream.ReadToEndAsync();
158+
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(serializedDictionary);
159+
LoadFolderHolder(root, string.Empty, dict);
160+
}
161+
162+
163+
static void LoadFolderHolder(FolderHolder folderHolder, string path, Dictionary<string, string> data)
152164
{
153165
foreach (var dataNodePair in folderHolder.DataNodes)
154166
{
155167
var fullPath = dataNodePair.Key;
156168
if (!string.IsNullOrEmpty(path))
157169
fullPath = path + '/' + fullPath;
158-
var json = PlayerPrefs.GetString(fullPath);
170+
var json = data.ContainsKey(fullPath) ? data[fullPath] : string.Empty;
159171
if (string.IsNullOrEmpty(json))
160172
continue;
161173
JsonConvert.PopulateObject(json, dataNodePair.Value);
@@ -166,7 +178,7 @@ static void LoadFolderHolder(FolderHolder folderHolder, string path)
166178
var fullPath = folderHolderPair.Key;
167179
if (!string.IsNullOrEmpty(path))
168180
fullPath = path + '/' + fullPath;
169-
LoadFolderHolder(folderHolderPair.Value, fullPath);
181+
LoadFolderHolder(folderHolderPair.Value, fullPath, data);
170182
}
171183
}
172184
}

Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"dependencies": {
33
"com.unity.2d.sprite": "1.0.0",
44
"com.unity.addressables": "1.11.2",
5-
"com.unity.ide.rider": "2.0.5",
5+
"com.unity.ide.rider": "2.0.7",
66
"com.unity.ide.visualstudio": "2.0.2",
77
"com.unity.ide.vscode": "1.2.1",
88
"com.unity.nuget.newtonsoft-json": "2.0.0",

Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"url": "https://packages.unity.com"
3636
},
3737
"com.unity.ide.rider": {
38-
"version": "2.0.5",
38+
"version": "2.0.7",
3939
"depth": 0,
4040
"source": "registry",
4141
"dependencies": {

0 commit comments

Comments
 (0)