ulong and grid list wrapper test

- wrapped lists in serialized dicts do work, maybe wrap anything.
- note: maybe convert long fields to string fields to support ulong?
This commit is contained in:
max 2023-07-03 23:35:29 +02:00
parent 6f600c38ab
commit 80c90d884a
7 changed files with 79 additions and 52 deletions

View File

@ -47,9 +47,9 @@ private void DrawSceneDataCache()
{ {
EditorGUILayout.LabelField($"scenePartitions"); EditorGUILayout.LabelField($"scenePartitions");
foreach (KeyValuePair<long, ScenePartition> scenePartition in sceneData.Value.ScenePartitions) foreach (KeyValuePair<ulong, ScenePartition> scenePartition in sceneData.Value.ScenePartitions)
{ {
EditorGUILayout.LongField(scenePartition.Key); EditorGUILayout.LongField((long)scenePartition.Key);
} }
} }
@ -59,9 +59,9 @@ private void DrawSceneDataCache()
{ {
EditorGUILayout.LabelField($"loadedScenePartitions"); EditorGUILayout.LabelField($"loadedScenePartitions");
foreach (KeyValuePair<long, ScenePartition> scenePartition in sceneData.Value.LoadedScenePartitions) foreach (KeyValuePair<ulong, ScenePartition> scenePartition in sceneData.Value.LoadedScenePartitions)
{ {
EditorGUILayout.LongField(scenePartition.Value.id); EditorGUILayout.LongField((long)scenePartition.Value.id);
} }
} }
@ -69,15 +69,15 @@ private void DrawSceneDataCache()
{ {
EditorGUILayout.LabelField($"generatedSceneGrid"); EditorGUILayout.LabelField($"generatedSceneGrid");
foreach (KeyValuePair<int, List<long>> item in sceneData.Value.SceneGrid.Grid) foreach (KeyValuePair<int, GridList> item in sceneData.Value.SceneGrid.Grid)
{ {
EditorGUILayout.IntField("gridId", item.Key); EditorGUILayout.IntField("gridId", item.Key);
EditorGUI.indentLevel++; EditorGUI.indentLevel++;
foreach (long id in item.Value) foreach (ulong id in item.Value.list)
{ {
EditorGUILayout.LongField(id); EditorGUILayout.LongField((long)id);
} }
EditorGUI.indentLevel--; EditorGUI.indentLevel--;

View File

@ -15,7 +15,7 @@ public class ScenePartitionSO : ScriptableObject
public SceneAsset SceneAsset { get; private set; } = null; public SceneAsset SceneAsset { get; private set; } = null;
public string SceneName => SceneAsset == null ? name : SceneAsset.name; public string SceneName => SceneAsset == null ? name : SceneAsset.name;
public List<long> alwaysLoadIds = new List<long> { 0, 1, 2, 3, 4 }; public List<ulong> alwaysLoadIds = new List<ulong> { 0, 1, 2, 3, 4 };
public ScenePartitionData Data public ScenePartitionData Data
{ {
@ -53,7 +53,7 @@ public void CreateScene()
public void LoadAll() public void LoadAll()
{ {
CreateScenePartitions(); CreateScenePartitions();
SortedSet<long> ids = new SortedSet<long>(Data.ScenePartitions.Keys); SortedSet<ulong> ids = new SortedSet<ulong>(Data.ScenePartitions.Keys);
LoadScenePartitions(ids); LoadScenePartitions(ids);
} }
@ -63,7 +63,7 @@ public void LoadAll()
public void Reload() public void Reload()
{ {
if (!Data.HasLoadedPartitions) return; if (!Data.HasLoadedPartitions) return;
LoadScenePartitions(new SortedSet<long>(Data.LoadedScenePartitions.Keys)); LoadScenePartitions(new SortedSet<ulong>(Data.LoadedScenePartitions.Keys));
} }
private void CreateScenePartitions() private void CreateScenePartitions()
@ -80,7 +80,7 @@ private void CreateScenePartitions()
} }
} }
private void LoadScenePartitions(SortedSet<long> partitionIds) private void LoadScenePartitions(SortedSet<ulong> partitionIds)
{ {
if (!Data.HasCreatedPartitions) return; if (!Data.HasCreatedPartitions) return;
@ -91,14 +91,14 @@ private void LoadScenePartitions(SortedSet<long> partitionIds)
Data.LoadedScenePartitions.Clear(); Data.LoadedScenePartitions.Clear();
// Add always load ids. // Add always load ids.
SortedSet<long> baseIds = GetAlwaysLoadIds(); SortedSet<ulong> baseIds = GetAlwaysLoadIds();
foreach (var id in baseIds) foreach (var id in baseIds)
{ {
partitionIds.Add(id); partitionIds.Add(id);
} }
// Create scene data. // Create scene data.
foreach (long id in partitionIds) foreach (ulong id in partitionIds)
{ {
ScenePartition p = Data.ScenePartitions[id]; ScenePartition p = Data.ScenePartitions[id];
sceneData.AddRange(File.ReadAllLines(p.filePath)); sceneData.AddRange(File.ReadAllLines(p.filePath));
@ -178,7 +178,7 @@ private void DeleteLoadedPartitions()
{ {
if (!Data.HasLoadedPartitions) return; if (!Data.HasLoadedPartitions) return;
foreach (KeyValuePair<long, ScenePartition> scenePartition in Data.LoadedScenePartitions) foreach (KeyValuePair<ulong, ScenePartition> scenePartition in Data.LoadedScenePartitions)
{ {
if (!File.Exists(scenePartition.Value.filePath)) continue; if (!File.Exists(scenePartition.Value.filePath)) continue;
@ -186,14 +186,14 @@ private void DeleteLoadedPartitions()
} }
} }
public void LoadPartitions(long[] ids) public void LoadPartitions(ulong[] ids)
{ {
SortedSet<long> partitionIds = new SortedSet<long>(); SortedSet<ulong> partitionIds = new SortedSet<ulong>();
for (int i = 0; i < ids.Length; i++) for (int i = 0; i < ids.Length; i++)
{ {
SortedSet<long> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]); SortedSet<ulong> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (long c in connections) foreach (ulong c in connections)
{ {
partitionIds.Add(c); partitionIds.Add(c);
} }
@ -202,14 +202,14 @@ public void LoadPartitions(long[] ids)
LoadScenePartitions(partitionIds); LoadScenePartitions(partitionIds);
} }
private SortedSet<long> GetAlwaysLoadIds() private SortedSet<ulong> GetAlwaysLoadIds()
{ {
SortedSet<long> partitionIds = new SortedSet<long>(); SortedSet<ulong> partitionIds = new SortedSet<ulong>();
foreach (long id in alwaysLoadIds) foreach (ulong id in alwaysLoadIds)
{ {
SortedSet<long> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, id); SortedSet<ulong> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, id);
foreach (long c in connections) foreach (ulong c in connections)
{ {
partitionIds.Add(c); partitionIds.Add(c);
} }
@ -281,11 +281,11 @@ public void GenerateSceneGridData()
if (ids[i].targetPrefabId == 0) // 0 = no prefab. if (ids[i].targetPrefabId == 0) // 0 = no prefab.
{ {
Data.SceneGrid.Insert((long)ids[i].targetObjectId, rootGameObjects[i].transform.position); Data.SceneGrid.Insert(ids[i].targetObjectId, rootGameObjects[i].transform.position);
} }
else else
{ {
Data.SceneGrid.Insert((long)ids[i].targetPrefabId, rootGameObjects[i].transform.position); Data.SceneGrid.Insert(ids[i].targetPrefabId, rootGameObjects[i].transform.position);
} }
} }
@ -294,9 +294,9 @@ public void GenerateSceneGridData()
public void LoadCell(int gridId) public void LoadCell(int gridId)
{ {
if (Data.SceneGrid.Grid.TryGetValue(gridId, out List<long> ids)) if (Data.SceneGrid.Grid.TryGetValue(gridId, out GridList ids))
{ {
LoadPartitions(ids.ToArray()); LoadPartitions(ids.list.ToArray());
} }
} }

View File

@ -7,7 +7,7 @@ namespace VertexColor.ScenePartition.Editor
[CustomEditor(typeof(ScenePartitionSO))] [CustomEditor(typeof(ScenePartitionSO))]
public class ScenePartitionSOEditor : UnityEditor.Editor public class ScenePartitionSOEditor : UnityEditor.Editor
{ {
private long id = 0; private ulong id = 0;
private int gridId = 0; private int gridId = 0;
public override void OnInspectorGUI() public override void OnInspectorGUI()
@ -60,11 +60,11 @@ public override void OnInspectorGUI()
EditorGUILayout.Space(); EditorGUILayout.Space();
id = EditorGUILayout.LongField("id", id); id = (ulong)EditorGUILayout.LongField("id", (long)id);
if (GUILayout.Button("Load Section")) if (GUILayout.Button("Load Section"))
{ {
scenePartitionSO.LoadPartitions(new long[1] { (long)id }); scenePartitionSO.LoadPartitions(new ulong[1] { id });
} }
if (GUILayout.Button("GenerateSceneGrid")) if (GUILayout.Button("GenerateSceneGrid"))
@ -83,15 +83,15 @@ public override void OnInspectorGUI()
{ {
EditorGUILayout.LabelField($"generatedSceneGrid"); EditorGUILayout.LabelField($"generatedSceneGrid");
foreach (KeyValuePair<int, List<long>> item in scenePartitionSO.Data.SceneGrid.Grid) foreach (KeyValuePair<int, GridList> item in scenePartitionSO.Data.SceneGrid.Grid)
{ {
EditorGUILayout.LongField("gridId", item.Key); EditorGUILayout.LongField("gridId", item.Key);
EditorGUI.indentLevel++; EditorGUI.indentLevel++;
foreach (long id in item.Value) foreach (ulong id in item.Value.list)
{ {
EditorGUILayout.LongField(id); EditorGUILayout.LongField((long)id);
} }
EditorGUI.indentLevel--; EditorGUI.indentLevel--;

View File

@ -9,7 +9,7 @@ public static class ScenePartitionUtils
{ {
public static string GetDataPath(ScenePartitionSO scenePartitionSO) public static string GetDataPath(ScenePartitionSO scenePartitionSO)
{ {
string dataPath = Path.Combine(Application.dataPath, $"../Data/Scenes/{scenePartitionSO.name}"); string dataPath = Path.Combine(Application.dataPath, GetRelativeFilePath(scenePartitionSO));
if (!Directory.Exists(dataPath)) if (!Directory.Exists(dataPath))
{ {
@ -19,6 +19,11 @@ public static string GetDataPath(ScenePartitionSO scenePartitionSO)
return dataPath; return dataPath;
} }
public static string GetRelativeFilePath(ScenePartitionSO scenePartitionSO)
{
return $"../Data/ScenePartition/{scenePartitionSO.name}";
}
public static string GetScenePath(ScenePartitionSO scenePartitionSO) public static string GetScenePath(ScenePartitionSO scenePartitionSO)
{ {
string scenePath = AssetDatabase.GetAssetOrScenePath(scenePartitionSO.SceneAsset); string scenePath = AssetDatabase.GetAssetOrScenePath(scenePartitionSO.SceneAsset);
@ -26,26 +31,26 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
return scenePath; return scenePath;
} }
public static SortedSet<long> FindDeeplyLinkedObjects(SortedList<long, ScenePartition> scenePartitions, long partitionId) public static SortedSet<ulong> FindDeeplyLinkedObjects(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId)
{ {
SortedSet<long> linkedObjects = new SortedSet<long>(); SortedSet<ulong> linkedObjects = new SortedSet<ulong>();
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects); FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
return linkedObjects; return linkedObjects;
} }
private static void FindDeeplyLinkedObjectsRecursive(SortedList<long, ScenePartition> scenePartitions, long partitionId, SortedSet<long> linkedObjects) private static void FindDeeplyLinkedObjectsRecursive(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId, SortedSet<ulong> linkedObjects)
{ {
if (linkedObjects.Contains(partitionId)) return; if (linkedObjects.Contains(partitionId)) return;
if (!scenePartitions.TryGetValue(partitionId, out ScenePartition partition)) return; if (!scenePartitions.TryGetValue(partitionId, out ScenePartition partition)) return;
linkedObjects.Add(partitionId); linkedObjects.Add(partitionId);
foreach (long reference in partition.references) foreach (ulong reference in partition.references)
{ {
FindDeeplyLinkedObjectsRecursive(scenePartitions, reference, linkedObjects); FindDeeplyLinkedObjectsRecursive(scenePartitions, reference, linkedObjects);
if (scenePartitions.TryGetValue(reference, out ScenePartition referencedPartition)) if (scenePartitions.TryGetValue(reference, out ScenePartition referencedPartition))
{ {
foreach (long subReference in referencedPartition.references) foreach (ulong subReference in referencedPartition.references)
{ {
FindDeeplyLinkedObjectsRecursive(scenePartitions, subReference, linkedObjects); FindDeeplyLinkedObjectsRecursive(scenePartitions, subReference, linkedObjects);
} }

View File

@ -1,4 +1,3 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace VertexColor.ScenePartition namespace VertexColor.ScenePartition
@ -14,16 +13,18 @@ public class SceneGrid
public SceneGridDictionary Grid => grid; public SceneGridDictionary Grid => grid;
public void Insert(long scenePartitionId, Vector3 point) public void Insert(ulong scenePartitionId, Vector3 point)
{ {
int gridId = CalculateGridPosition(point, cellSize); int gridId = CalculateGridPosition(point, cellSize);
if (grid.TryGetValue(gridId, out List<long> ids)) if (grid.TryGetValue(gridId, out GridList ids))
{ {
ids.Add(scenePartitionId); ids.list.Add(scenePartitionId);
} }
else else
{ {
grid.Add(gridId, new List<long> { scenePartitionId }); var l = new GridList();
l.list.Add(scenePartitionId);
grid.Add(gridId, l);
} }
} }
@ -64,5 +65,20 @@ public static (long, long) LongToLongPair(long value)
return (x, y); return (x, y);
} }
public static ulong ULongPairToULong(ulong x, ulong y)
{
// Combine x and y components into a single ulong
return (x << 32) | (uint)y;
}
public static (ulong, ulong) ULongToULongPair(ulong value)
{
// Extract x and y components from the combined ulong
ulong x = value >> 32;
ulong y = (uint)value;
return (x, y);
}
} }
} }

View File

@ -24,9 +24,9 @@ public int Compare(ScenePartition x, ScenePartition y)
[System.Serializable] [System.Serializable]
public class ScenePartition public class ScenePartition
{ {
public long id = 0; public ulong id = 0;
//public long classId = 0; //public ulong classId = 0;
public string filePath = null; public string filePath = null; // TODO: Only store relative path.
//public string[] data = null; //public string[] data = null;
public LongSortedSet references = new LongSortedSet(); public LongSortedSet references = new LongSortedSet();
@ -45,7 +45,7 @@ public ScenePartition(string filePath)
Match match = Regex.Match(data[0], pattern); Match match = Regex.Match(data[0], pattern);
if (!match.Success) return; if (!match.Success) return;
if (!long.TryParse(match.Groups[1].Value, out id)) return; if (!ulong.TryParse(match.Groups[1].Value, out id)) return;
} }
//{ // Get class id. //{ // Get class id.
@ -68,7 +68,7 @@ public ScenePartition(string filePath)
if (!match.Success) continue; if (!match.Success) continue;
if (long.TryParse(match.Groups[1].Value, out long fileNumber)) if (ulong.TryParse(match.Groups[1].Value, out ulong fileNumber))
{ {
if (fileNumber == 0) continue; // 0 == nothing. if (fileNumber == 0) continue; // 0 == nothing.

View File

@ -3,11 +3,17 @@
namespace VertexColor.ScenePartition namespace VertexColor.ScenePartition
{ {
[System.Serializable] [System.Serializable]
public class ScenePartitionSortedList : SerializableSortedList<long, ScenePartition> { } public class ScenePartitionSortedList : SerializableSortedList<ulong, ScenePartition> { }
[System.Serializable] [System.Serializable]
public class LongSortedSet : SerializableSortedSet<long> { } public class LongSortedSet : SerializableSortedSet<ulong> { }
[System.Serializable] [System.Serializable]
public class SceneGridDictionary : SerializableDictionary<int, List<long>> { } public class SceneGridDictionary : SerializableDictionary<int, GridList> { }
[System.Serializable]
public class GridList
{
public List<ulong> list = new List<ulong>();
}
} }