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

Commit 20b617d

Browse files
author
serg.morozov
committed
feat: backup save
1 parent 21bea34 commit 20b617d

File tree

7 files changed

+92
-43
lines changed

7 files changed

+92
-43
lines changed

Assets/com.nuclearband.sodatabase/Runtime/Holders/FolderHolder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace NuclearBand
1010
public class FolderHolder : Holder
1111
{
1212
[HideInInspector]
13-
public Dictionary<string, DataNode> DataNodes = new Dictionary<string, DataNode>();
13+
public Dictionary<string, DataNode> DataNodes = new();
1414

1515
[HideInInspector]
16-
public Dictionary<string, FolderHolder> FolderHolders = new Dictionary<string, FolderHolder>();
16+
public Dictionary<string, FolderHolder> FolderHolders = new();
1717

1818
public FolderHolder() : base()
1919
{

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

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@ namespace NuclearBand
1313
public static class SODatabase
1414
{
1515
public static string SavePath => Application.persistentDataPath + @"/save.txt";
16+
public static string SaveBakPath => Application.persistentDataPath + @"/save.bak";
1617

1718
private static FolderHolder root = null!;
1819

19-
private static Dictionary<string, object> runtimeModels = new Dictionary<string, object>();
20+
private static readonly Dictionary<string, object> runtimeModels = new();
2021

21-
private static JsonSerializerSettings jsonSerializerSettings => new JsonSerializerSettings
22+
private static bool saving = false;
23+
24+
private static JsonSerializerSettings jsonSerializerSettings => new()
2225
{
2326
Formatting = Formatting.Indented,
2427
TypeNameHandling = TypeNameHandling.All,
2528
ReferenceResolverProvider = () => new DataNodeReferenceResolver(),
2629
ObjectCreationHandling = ObjectCreationHandling.Replace,
2730
};
2831

29-
private static JsonSerializerSettings jsonRuntimeSerializerSettings => new JsonSerializerSettings
32+
private static JsonSerializerSettings jsonRuntimeSerializerSettings => new()
3033
{
3134
Formatting = Formatting.Indented,
3235
TypeNameHandling = TypeNameHandling.All,
@@ -137,6 +140,13 @@ public static async void Save()
137140

138141
public static async Task SaveAsync()
139142
{
143+
if (saving)
144+
return;
145+
146+
saving = true;
147+
if (File.Exists(SavePath))
148+
File.Copy(SavePath, SaveBakPath, true);
149+
140150
var staticNodes = new Dictionary<string, string>();
141151
foreach (var dataNode in DataNodes(root))
142152
{
@@ -155,6 +165,7 @@ public static async Task SaveAsync()
155165

156166
using var fileStream = new StreamWriter(SavePath);
157167
await fileStream.WriteAsync(JsonConvert.SerializeObject(save));
168+
saving = false;
158169
}
159170

160171
public static async void Load()
@@ -169,27 +180,50 @@ public static async Task LoadAsync()
169180
dataNode.AfterLoad();
170181
return;
171182
}
172-
173-
using var fileStream = new StreamReader(SavePath);
174-
var serializedDictionary = await fileStream.ReadToEndAsync();
175-
var save = JsonConvert.DeserializeObject<SODatabaseSaveFormat>(serializedDictionary);
176-
177-
foreach (var dataNode in DataNodes(root))
178-
{
179-
DataNodeReferenceResolver.CurrentDataNode = dataNode;
180-
var json = save.StaticNodes.ContainsKey(dataNode.FullPath) ? save.StaticNodes[dataNode.FullPath] : string.Empty;
181-
if (string.IsNullOrEmpty(json))
182-
continue;
183-
DataNodeReferenceResolver.CurrentDataNode = dataNode;
184-
JsonConvert.PopulateObject(json, dataNode, jsonSerializerSettings);
185-
}
186-
187-
runtimeModels.Clear();
188-
foreach (var runtimeNodePair in save.RuntimeNodes)
183+
184+
do
189185
{
190-
var x = JsonConvert.DeserializeObject(runtimeNodePair.Value, jsonRuntimeSerializerSettings)!;
191-
runtimeModels.Add(runtimeNodePair.Key, x);
192-
}
186+
try
187+
{
188+
using var fileStream = new StreamReader(SavePath);
189+
var serializedDictionary = await fileStream.ReadToEndAsync();
190+
var save = JsonConvert.DeserializeObject<SODatabaseSaveFormat>(serializedDictionary);
191+
if (save == null)
192+
throw new Exception();
193+
194+
foreach (var dataNode in DataNodes(root))
195+
{
196+
DataNodeReferenceResolver.CurrentDataNode = dataNode;
197+
var json = save.StaticNodes.ContainsKey(dataNode.FullPath)
198+
? save.StaticNodes[dataNode.FullPath]
199+
: string.Empty;
200+
if (string.IsNullOrEmpty(json))
201+
continue;
202+
DataNodeReferenceResolver.CurrentDataNode = dataNode;
203+
JsonConvert.PopulateObject(json, dataNode, jsonSerializerSettings);
204+
}
205+
206+
runtimeModels.Clear();
207+
foreach (var runtimeNodePair in save.RuntimeNodes)
208+
{
209+
var x = JsonConvert.DeserializeObject(runtimeNodePair.Value, jsonRuntimeSerializerSettings)!;
210+
runtimeModels.Add(runtimeNodePair.Key, x);
211+
}
212+
213+
break;
214+
}
215+
catch (Exception)
216+
{
217+
if (File.Exists(SaveBakPath))
218+
{
219+
File.Copy(SaveBakPath, SavePath, true);
220+
File.Delete(SaveBakPath);
221+
continue;
222+
}
223+
224+
break;
225+
}
226+
} while (true);
193227

194228
foreach (var dataNode in DataNodes(root))
195229
dataNode.AfterLoad();

Assets/com.nuclearband.sodatabase/Runtime/SerializationUtilities/SODatabaseSaveFormat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace NuclearBand
66
{
77
public class SODatabaseSaveFormat
88
{
9-
public Dictionary<string, string> StaticNodes = new Dictionary<string, string>();
10-
public Dictionary<string, string> RuntimeNodes = new Dictionary<string, string>();
9+
public Dictionary<string, string> StaticNodes = new();
10+
public Dictionary<string, string> RuntimeNodes = new();
1111
}
1212
}

Packages/manifest.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dependencies": {
3-
"com.unity.addressables": "1.19.19",
4-
"com.unity.ide.rider": "3.0.13",
5-
"com.unity.ide.visualstudio": "2.0.14",
3+
"com.unity.addressables": "1.20.5",
4+
"com.unity.ide.rider": "3.0.15",
5+
"com.unity.ide.visualstudio": "2.0.16",
66
"com.unity.ide.vscode": "1.2.5",
7-
"com.unity.nuget.newtonsoft-json": "2.0.2",
8-
"com.unity.test-framework": "1.1.31",
7+
"com.unity.nuget.newtonsoft-json": "3.0.2",
8+
"com.unity.test-framework": "1.1.33",
99
"com.unity.textmeshpro": "3.0.6",
10-
"com.unity.timeline": "1.6.4",
10+
"com.unity.timeline": "1.7.1",
1111
"com.unity.ugui": "1.0.0",
1212
"com.unity.modules.ai": "1.0.0",
1313
"com.unity.modules.androidjni": "1.0.0",

Packages/packages-lock.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"dependencies": {
33
"com.unity.addressables": {
4-
"version": "1.19.19",
4+
"version": "1.20.5",
55
"depth": 0,
66
"source": "registry",
77
"dependencies": {
8-
"com.unity.scriptablebuildpipeline": "1.19.6",
8+
"com.unity.scriptablebuildpipeline": "1.20.2",
99
"com.unity.modules.assetbundle": "1.0.0",
1010
"com.unity.modules.imageconversion": "1.0.0",
1111
"com.unity.modules.jsonserialize": "1.0.0",
@@ -22,7 +22,7 @@
2222
"url": "https://packages.unity.com"
2323
},
2424
"com.unity.ide.rider": {
25-
"version": "3.0.13",
25+
"version": "3.0.15",
2626
"depth": 0,
2727
"source": "registry",
2828
"dependencies": {
@@ -31,7 +31,7 @@
3131
"url": "https://packages.unity.com"
3232
},
3333
"com.unity.ide.visualstudio": {
34-
"version": "2.0.14",
34+
"version": "2.0.16",
3535
"depth": 0,
3636
"source": "registry",
3737
"dependencies": {
@@ -47,21 +47,21 @@
4747
"url": "https://packages.unity.com"
4848
},
4949
"com.unity.nuget.newtonsoft-json": {
50-
"version": "2.0.2",
50+
"version": "3.0.2",
5151
"depth": 0,
5252
"source": "registry",
5353
"dependencies": {},
5454
"url": "https://packages.unity.com"
5555
},
5656
"com.unity.scriptablebuildpipeline": {
57-
"version": "1.19.6",
57+
"version": "1.20.2",
5858
"depth": 1,
5959
"source": "registry",
6060
"dependencies": {},
6161
"url": "https://packages.unity.com"
6262
},
6363
"com.unity.test-framework": {
64-
"version": "1.1.31",
64+
"version": "1.1.33",
6565
"depth": 0,
6666
"source": "registry",
6767
"dependencies": {
@@ -81,7 +81,7 @@
8181
"url": "https://packages.unity.com"
8282
},
8383
"com.unity.timeline": {
84-
"version": "1.6.4",
84+
"version": "1.7.1",
8585
"depth": 0,
8686
"source": "registry",
8787
"dependencies": {

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
m_EditorVersion: 2021.2.19f1
2-
m_EditorVersionWithRevision: 2021.2.19f1 (602ecdbb2fb0)
1+
m_EditorVersion: 2022.1.13f1
2+
m_EditorVersionWithRevision: 2022.1.13f1 (22856944e6d2)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &1
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 61
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 0}
13+
m_Name:
14+
m_EditorClassIdentifier: Unity.Rider.Editor:Packages.Rider.Editor:RiderScriptEditorPersistedState
15+
lastWriteTicks: -8585494112810372608

0 commit comments

Comments
 (0)