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

Commit c599e17

Browse files
committed
feat: runtime-only models
1 parent 60fcaff commit c599e17

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ public static class SODatabase
1717

1818
private static FolderHolder root = null!;
1919

20+
private static Dictionary<string, object> runtimeModels = new Dictionary<string, object>();
21+
2022
private static JsonSerializerSettings jsonSerializerSettings => new JsonSerializerSettings
2123
{
22-
Formatting = Formatting.None,
24+
Formatting = Formatting.Indented,
25+
ReferenceResolverProvider = () => new DataNodeReferenceResolver()
26+
};
27+
28+
private static JsonSerializerSettings jsonRuntimeSerializerSettings => new JsonSerializerSettings
29+
{
30+
Formatting = Formatting.Indented,
31+
TypeNameHandling = TypeNameHandling.All,
2332
ReferenceResolverProvider = () => new DataNodeReferenceResolver()
2433
};
2534

@@ -120,9 +129,15 @@ public static async void Save()
120129

121130
public static async Task SaveAsync()
122131
{
123-
var res = SaveFolderHolder(root, string.Empty);
132+
var save = new SODatabaseSaveFormat
133+
{
134+
StaticNodes = SaveFolderHolder(root, string.Empty)
135+
};
136+
foreach (var runtimeModelPair in runtimeModels)
137+
save.RuntimeNodes.Add(runtimeModelPair.Key, JsonConvert.SerializeObject(runtimeModelPair.Value, jsonRuntimeSerializerSettings));
138+
124139
using var fileStream = new StreamWriter(SavePath);
125-
await fileStream.WriteAsync(JsonConvert.SerializeObject(res));
140+
await fileStream.WriteAsync(JsonConvert.SerializeObject(save));
126141
}
127142

128143
static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, string path)
@@ -163,8 +178,14 @@ public static async Task LoadAsync()
163178

164179
using var fileStream = new StreamReader(SavePath);
165180
var serializedDictionary = await fileStream.ReadToEndAsync();
166-
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(serializedDictionary);
167-
LoadFolderHolder(root, string.Empty, dict);
181+
var save = JsonConvert.DeserializeObject<SODatabaseSaveFormat>(serializedDictionary);
182+
LoadFolderHolder(root, string.Empty, save.StaticNodes);
183+
runtimeModels.Clear();
184+
foreach (var runtimeNodePair in save.RuntimeNodes)
185+
{
186+
var x = JsonConvert.DeserializeObject(runtimeNodePair.Value, jsonRuntimeSerializerSettings)!;
187+
runtimeModels.Add(runtimeNodePair.Key, x);
188+
}
168189
}
169190

170191
static void LoadFolderHolder(FolderHolder folderHolder, string path, Dictionary<string, string> data)
@@ -189,5 +210,18 @@ static void LoadFolderHolder(FolderHolder folderHolder, string path, Dictionary<
189210
LoadFolderHolder(folderHolderPair.Value, fullPath, data);
190211
}
191212
}
213+
214+
public static T GetRuntimeModel<T>(string path, Func<T>? allocator = null) where T : class
215+
{
216+
T model;
217+
if (runtimeModels.ContainsKey(path))
218+
model = (T) runtimeModels[path];
219+
else
220+
{
221+
model = allocator!();
222+
runtimeModels.Add(path, model);
223+
}
224+
return model;
225+
}
192226
}
193227
}
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
#nullable enable
22
using System;
33
using Newtonsoft.Json.Serialization;
4-
using NuclearBand;
54

6-
public class DataNodeReferenceResolver : IReferenceResolver
5+
namespace NuclearBand
76
{
8-
public static DataNode CurrentDataNode = null!;
9-
10-
public object ResolveReference(object context, string reference)
7+
public class DataNodeReferenceResolver : IReferenceResolver
118
{
12-
try
9+
public static DataNode CurrentDataNode = null!;
10+
11+
public object ResolveReference(object context, string reference)
1312
{
14-
return SODatabase.GetModel<DataNode>(reference);
13+
try
14+
{
15+
return SODatabase.GetModel<DataNode>(reference);
16+
}
17+
catch (Exception)
18+
{
19+
return null!;
20+
}
1521
}
16-
catch (Exception)
22+
23+
public string GetReference(object context, object value)
1724
{
18-
return null!;
25+
var dataNode = (DataNode) value;
26+
return dataNode.FullPath;
1927
}
20-
}
21-
22-
public string GetReference(object context, object value)
23-
{
24-
var dataNode = (DataNode) value;
25-
return dataNode.FullPath;
26-
}
2728

28-
public bool IsReferenced(object context, object value) => !ReferenceEquals(value, CurrentDataNode);
29+
public bool IsReferenced(object context, object value) => !ReferenceEquals(value, CurrentDataNode);
2930

30-
public void AddReference(object context, string reference, object value)
31-
{
31+
public void AddReference(object context, string reference, object value)
32+
{
33+
}
3234
}
3335
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
3+
#nullable enable
4+
5+
namespace NuclearBand
6+
{
7+
public class SODatabaseSaveFormat
8+
{
9+
public Dictionary<string, string> StaticNodes = new Dictionary<string, string>();
10+
public Dictionary<string, string> RuntimeNodes = new Dictionary<string, string>();
11+
}
12+
}

Packages/com.nuclearband.sodatabase/Runtime/SerializationUtilities/SODatabaseSaveFormat.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)