ScenePartitionSS Saving

And added reload function to reload previously/currently loaded partitions.
This commit is contained in:
max 2023-06-27 01:09:28 +02:00
parent 7e8d46623a
commit afd241655c
3 changed files with 52 additions and 27 deletions

View File

@ -17,15 +17,16 @@ public class ScenePartitionSO : ScriptableObject
public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 };
public ScenePartitionData ScenePartitionData
public ScenePartitionData Data
{
get
{
scenePartitionData ??= ScenePartitionSS.instance.GetScenePartitionData(this);
return scenePartitionData;
// Load data from the ScriptableSingleton.
data ??= ScenePartitionSS.instance.GetScenePartitionData(this);
return data;
}
}
private ScenePartitionData scenePartitionData = null;
private ScenePartitionData data = null;
public void CreateScene()
{
@ -52,38 +53,47 @@ public void CreateScene()
public void LoadAll()
{
CreateScenePartitions();
SortedSet<uint> ids = new SortedSet<uint>(ScenePartitionData.ScenePartitions.Keys);
SortedSet<uint> ids = new SortedSet<uint>(Data.ScenePartitions.Keys);
LoadScenePartitions(ids);
}
/// <summary>
/// Load already/previously loaded partitions.
/// </summary>
public void Reload()
{
if (!Data.HasLoadedPartitions) return;
LoadScenePartitions(new SortedSet<uint>(Data.LoadedScenePartitions.Keys));
}
private void CreateScenePartitions()
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string[] files = Directory.GetFiles(dataPath);
ScenePartitionData.ScenePartitions = new ScenePartitionSortedList();
Data.ScenePartitions = new ScenePartitionSortedList();
for (int i = 0; i < files.Length; i++)
{
ScenePartition scenePartition = new ScenePartition(files[i]);
ScenePartitionData.ScenePartitions.Add(scenePartition.id, scenePartition);
Data.ScenePartitions.Add(scenePartition.id, scenePartition);
}
}
private void LoadScenePartitions(SortedSet<uint> partitionIds)
{
if (!ScenePartitionData.HasCreatedPartitions) return;
if (!Data.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> data = new List<string>();
List<string> sceneData = new List<string>();
ScenePartitionData.LoadedScenePartitions.Clear();
Data.LoadedScenePartitions.Clear();
// Add default ids.
for (int i = 0; i < alwaysLoadIds.Count; i++)
{
if (ScenePartitionData.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
if (Data.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
{
partitionIds.Add(alwaysLoadIds[i]);
}
@ -92,13 +102,13 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
// Create scene data.
foreach (uint id in partitionIds)
{
ScenePartition p = ScenePartitionData.ScenePartitions[id];
data.AddRange(File.ReadAllLines(p.filePath));
ScenePartitionData.LoadedScenePartitions.Add(p.id, p);
ScenePartition p = Data.ScenePartitions[id];
sceneData.AddRange(File.ReadAllLines(p.filePath));
Data.LoadedScenePartitions.Add(p.id, p);
}
// Create scene.
File.WriteAllLines(scenePath, data);
File.WriteAllLines(scenePath, sceneData);
AssetDatabase.Refresh();
}
@ -115,15 +125,15 @@ public void Save()
string scenePath = ScenePartitionUtils.GetScenePath(this);
// Read the data from the scene file.
string[] data = File.ReadAllLines(scenePath);
string[] sceneData = File.ReadAllLines(scenePath);
// Split it into blocks.
int lastIndex = data.Length;
for (int i = data.Length - 1; i >= 0; i--)
int lastIndex = sceneData.Length;
for (int i = sceneData.Length - 1; i >= 0; i--)
{
if (data[i].StartsWith("---")) // --- is the start of a new yaml document.
if (sceneData[i].StartsWith("---")) // --- is the start of a new yaml document.
{
Match match = Regex.Match(data[i], pattern);
Match match = Regex.Match(sceneData[i], pattern);
if (match.Success)
{
@ -131,7 +141,7 @@ public void Save()
string id = match.Groups[1].Value;
// Write data to disk.
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", data[i..lastIndex]);
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", sceneData[i..lastIndex]);
}
lastIndex = i;
@ -139,7 +149,7 @@ public void Save()
}
// Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", data[0..lastIndex]);
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", sceneData[0..lastIndex]);
}
/// <summary>
@ -161,16 +171,16 @@ public void Unload()
EditorSceneManager.SaveScene(scene);
ScenePartitionData.LoadedScenePartitions.Clear();
Data.LoadedScenePartitions.Clear();
AssetDatabase.Refresh();
}
private void DeleteLoadedPartitions()
{
if (!ScenePartitionData.HasLoadedPartitions) return;
if (!Data.HasLoadedPartitions) return;
foreach (KeyValuePair<uint, ScenePartition> scenePartition in ScenePartitionData.LoadedScenePartitions)
foreach (KeyValuePair<uint, ScenePartition> scenePartition in Data.LoadedScenePartitions)
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
@ -184,7 +194,7 @@ public void LoadPartitions(uint[] ids)
for (int i = 0; i < ids.Length; i++)
{
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(ScenePartitionData.ScenePartitions, ids[i]);
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (uint c in connections)
{

View File

@ -34,6 +34,11 @@ public override void OnInspectorGUI()
scenePartitionSO.LoadAll();
}
if (GUILayout.Button("Reload"))
{
scenePartitionSO.Reload();
}
if (GUILayout.Button("Save"))
{
scenePartitionSO.Save();

View File

@ -3,7 +3,12 @@
namespace VertexColor.ScenePartition.Editor
{
[FilePath("ScenePartition/SceneDataCache.asset", FilePathAttribute.Location.ProjectFolder)]
/// <summary>
/// Local cache/data container.
/// Hold loaded data, calculated data, cache data, state data.
/// This data is persistent though editor sessions and recompiles, is stored in the project folder.
/// </summary>
[FilePath("Library/ScenePartition/SceneDataCache.asset", FilePathAttribute.Location.ProjectFolder)]
public class ScenePartitionSS : ScriptableSingleton<ScenePartitionSS>
{
public SceneDataCache SceneDataCache => sceneDataCache;
@ -21,6 +26,11 @@ public ScenePartitionData GetScenePartitionData(ScenePartitionSO scenePartitionS
return scenePartitionData;
}
private void OnDisable()
{
Save(true);
}
}
[System.Serializable]