Skip to content

Commit 33cc082

Browse files
committed
string -> ushort
1 parent 93a31ab commit 33cc082

File tree

2 files changed

+106
-130
lines changed

2 files changed

+106
-130
lines changed

Runtime/AssetFormatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public class AssetFormatter<T> : IMessagePackFormatter<T> where T : Object
88
{
99
public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
1010
{
11-
DataSerializer.Container.TryResolveId(value, out var id);
11+
DataSerializer.Container.TryGetId(value, out var id);
1212

13-
writer.Write(id);
13+
writer.WriteUInt16(id);
1414
}
1515

1616
public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
1717
{
18-
DataSerializer.Container.TryResolveReference(reader.ReadString(), out var value);
18+
DataSerializer.Container.TryGetObject(reader.ReadUInt16(), out var value);
1919

2020
return (T)value;
2121
}

Runtime/AssetsContainer.cs

Lines changed: 103 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
#if UNITY_EDITOR
@@ -9,132 +8,109 @@
98

109
namespace ToolBox.Serialization
1110
{
12-
internal sealed class AssetsContainer : ScriptableObject
13-
{
14-
[SerializeField] private AssetEntry[] _savedAssets;
15-
[SerializeField] private string[] _paths;
16-
17-
public bool TryResolveId(Object value, out string id)
18-
{
19-
id = null;
20-
21-
if (!TryGetByObject(value, out var entry))
22-
{
23-
return false;
24-
}
25-
26-
id = entry.Guid;
27-
return true;
28-
}
29-
30-
public bool TryResolveReference(string id, out Object value)
31-
{
32-
value = null;
33-
34-
if (!TryGetById(id, out var entry))
35-
{
36-
return false;
37-
}
38-
39-
value = entry.Asset;
40-
return true;
41-
}
11+
public sealed class AssetsContainer : ScriptableObject
12+
{
13+
[SerializeField] private Object[] _savedAssets;
14+
[SerializeField] private string[] _paths;
15+
16+
public bool TryGetObject(ushort id, out Object entry)
17+
{
18+
entry = null;
19+
20+
if (id == 0 || id >= _savedAssets.Length)
21+
{
22+
return false;
23+
}
24+
25+
entry = _savedAssets[id];
26+
return true;
27+
}
28+
29+
public bool TryGetId(Object value, out ushort id)
30+
{
31+
id = 0;
32+
33+
for (ushort i = 1; i < _savedAssets.Length; i++)
34+
{
35+
if (_savedAssets[i] != value)
36+
{
37+
continue;
38+
}
39+
40+
id = i;
41+
return true;
42+
}
43+
44+
return false;
45+
}
4246

4347
#if UNITY_EDITOR
44-
public void LoadAssets()
45-
{
46-
if (_paths == null)
47-
{
48-
return;
49-
}
50-
51-
_paths = _paths.Where(x => !string.IsNullOrEmpty(x) && AssetDatabase.IsValidFolder(x)).ToArray();
52-
53-
if (_paths.Length == 0)
54-
{
55-
return;
56-
}
57-
58-
// ReSharper disable once UseArrayEmptyMethod
59-
_savedAssets ??= new AssetEntry[0];
60-
61-
var assets = AssetDatabase
62-
.FindAssets("t:Object", _paths)
63-
.Select(AssetDatabase.GUIDToAssetPath)
64-
.Select(AssetDatabase.LoadAssetAtPath<Object>)
65-
.Where(x =>
66-
{
67-
var fileNamespace = x.GetType().Namespace;
68-
69-
return x != null && (fileNamespace == null || !fileNamespace.Contains("UnityEditor"));
70-
})
71-
.ToList();
72-
73-
var newEntries = new List<AssetEntry>();
74-
75-
foreach (var asset in assets)
76-
{
77-
if (!TryGetByObject(asset, out _))
78-
{
79-
newEntries.Add(new AssetEntry(Guid.NewGuid().ToString(), asset));
80-
}
81-
82-
var children = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(asset));
83-
84-
foreach (var child in children)
85-
{
86-
if (TryGetByObject(child, out _))
87-
{
88-
continue;
89-
}
90-
91-
newEntries.Add(new AssetEntry(Guid.NewGuid().ToString(), child));
92-
}
93-
}
94-
95-
ArrayUtility.AddRange(ref _savedAssets, newEntries.ToArray());
96-
EditorUtility.SetDirty(this);
97-
}
98-
99-
public void Clear()
100-
{
101-
_savedAssets = null;
102-
EditorUtility.SetDirty(this);
103-
}
48+
public void LoadAssets()
49+
{
50+
if (_paths == null)
51+
{
52+
return;
53+
}
54+
55+
_paths = _paths.Where(x => !string.IsNullOrEmpty(x) && AssetDatabase.IsValidFolder(x)).ToArray();
56+
57+
if (_paths.Length == 0)
58+
{
59+
return;
60+
}
61+
62+
// ReSharper disable once UseArrayEmptyMethod
63+
_savedAssets ??= new Object[0];
64+
65+
var assets = AssetDatabase
66+
.FindAssets("t:Object", _paths)
67+
.Select(AssetDatabase.GUIDToAssetPath)
68+
.Select(AssetDatabase.LoadAssetAtPath<Object>)
69+
.Where(x =>
70+
{
71+
var fileNamespace = x.GetType().Namespace;
72+
73+
return x != null && (fileNamespace == null || !fileNamespace.Contains("UnityEditor"));
74+
})
75+
.ToList();
76+
77+
var newEntries = new List<Object>();
78+
79+
foreach (var asset in assets)
80+
{
81+
if (!TryGetId(asset, out _))
82+
{
83+
newEntries.Add(asset);
84+
}
85+
86+
var children = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(asset));
87+
88+
foreach (var child in children)
89+
{
90+
if (TryGetId(child, out _))
91+
{
92+
continue;
93+
}
94+
95+
newEntries.Add(child);
96+
}
97+
}
98+
99+
ArrayUtility.AddRange(ref _savedAssets, newEntries.ToArray());
100+
101+
if (_savedAssets.Length == 0 || _savedAssets[0] != null)
102+
{
103+
ArrayUtility.Insert(ref _savedAssets, 0, null);
104+
}
105+
106+
EditorUtility.SetDirty(this);
107+
}
108+
109+
public void Clear()
110+
{
111+
_savedAssets = null;
112+
EditorUtility.SetDirty(this);
113+
}
104114
#endif
105-
106-
private bool TryGetById(string guid, out AssetEntry entry)
107-
{
108-
foreach (var asset in _savedAssets)
109-
{
110-
if (asset.Guid != guid)
111-
{
112-
continue;
113-
}
114-
115-
entry = asset;
116-
return true;
117-
}
118-
119-
entry = null;
120-
return false;
121-
}
122-
123-
private bool TryGetByObject(Object value, out AssetEntry entry)
124-
{
125-
foreach (var asset in _savedAssets)
126-
{
127-
if (asset.Asset != value)
128-
{
129-
continue;
130-
}
131-
132-
entry = asset;
133-
return true;
134-
}
135-
136-
entry = null;
137-
return false;
138-
}
139-
}
140-
}
115+
}
116+
}

0 commit comments

Comments
 (0)