Skip to content

Commit 7d8ff29

Browse files
authored
Merge pull request #12 from IntoTheDev/vlad/odin_version_optimization
Vlad/odin version optimization
2 parents e042fd8 + 792d98c commit 7d8ff29

File tree

279 files changed

+4577
-629
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

279 files changed

+4577
-629
lines changed

DataSerializer/ApplicationStateObserver.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ namespace ToolBox.Serialization
55
{
66
internal sealed class ApplicationStateObserver : MonoBehaviour
77
{
8-
public event Action OnQuit = null;
8+
public event Action OnQuit;
99

1010
#if !UNITY_IOS && !UNITY_ANDROID
11-
private void OnApplicationQuit() =>
11+
private void OnApplicationQuit()
12+
{
1213
OnQuit?.Invoke();
14+
}
1315
#else
1416
private void OnApplicationPause(bool pause)
1517
{

DataSerializer/AssemblyGenerator.cs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public void OnPreprocessBuild(BuildReport report)
3030
&& AOTSupportUtilities.ScanProjectForSerializedTypes(out var types))
3131
{
3232
types.Add(typeof(byte[]));
33-
types.Add(typeof(Item));
3433

3534
var providers = AppDomain
3635
.CurrentDomain

DataSerializer/AssetsContainer.cs

+104-125
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,116 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using ToolBox.Serialization.OdinSerializer;
43
#if UNITY_EDITOR
54
using UnityEditor;
65
#endif
76
using UnityEngine;
7+
using Object = UnityEngine.Object;
88

99
namespace ToolBox.Serialization
1010
{
11-
internal sealed class AssetsContainer : ScriptableObject, IExternalStringReferenceResolver
12-
{
13-
[SerializeField] private AssetEntry[] _savedAssets = null;
14-
[SerializeField] private string[] _paths = null;
15-
16-
public IExternalStringReferenceResolver NextResolver { get; set; }
17-
18-
public bool CanReference(object value, out string id)
19-
{
20-
id = null;
21-
22-
if (!(value is Object obj) || !TryGetValue(obj, out var entry))
23-
return false;
24-
25-
id = entry.Guid;
26-
return true;
27-
}
28-
29-
public bool TryResolveReference(string id, out object value)
30-
{
31-
value = null;
32-
33-
if (id == null)
34-
return false;
35-
36-
bool contains = TryGetValue(id, out var entry);
37-
value = entry.Asset;
38-
39-
return contains;
40-
}
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+
}
4146

4247
#if UNITY_EDITOR
43-
// TODO: Make everything with loops and lists instead of LINQ
44-
public void LoadAssets()
45-
{
46-
if (_paths == null)
47-
return;
48-
49-
_paths = _paths.Where(x => !string.IsNullOrEmpty(x) && AssetDatabase.IsValidFolder(x)).ToArray();
50-
51-
if (_paths.Length == 0)
52-
return;
53-
54-
List<Object> assets;
55-
56-
assets = AssetDatabase
57-
.FindAssets("t:Object", _paths)
58-
.Select(AssetDatabase.GUIDToAssetPath)
59-
.Select(AssetDatabase.LoadAssetAtPath<Object>)
60-
.Where(x =>
61-
{
62-
string fileNamespace = x.GetType().Namespace;
63-
64-
return x != null && (fileNamespace == null || !fileNamespace.Contains("UnityEditor"));
65-
}) // Change UnityEditor to Editor?
66-
.ToList();
67-
68-
var newEntries = new List<AssetEntry>();
69-
70-
foreach (var asset in assets)
71-
{
72-
string path = AssetDatabase.GetAssetPath(asset);
73-
string guid = AssetDatabase.AssetPathToGUID(path);
74-
75-
if (!TryGetValue(asset, out _))
76-
newEntries.Add(new AssetEntry(guid, asset));
77-
78-
var childAssets = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(asset));
79-
80-
for (int i = 0; i < childAssets.Length; i++)
81-
{
82-
var child = childAssets[i];
83-
84-
if (!TryGetValue(child, out _))
85-
{
86-
string childGuid = System.Guid.NewGuid().ToString();
87-
newEntries.Add(new AssetEntry(childGuid, child));
88-
}
89-
}
90-
}
91-
92-
ArrayUtility.AddRange(ref _savedAssets, newEntries.ToArray());
93-
EditorUtility.SetDirty(this);
94-
}
95-
96-
public void Clear()
97-
{
98-
_savedAssets = new AssetEntry[0];
99-
EditorUtility.SetDirty(this);
100-
}
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+
}
101114
#endif
102-
103-
private bool TryGetValue(string guid, out AssetEntry entry)
104-
{
105-
for (int i = 0; i < _savedAssets.Length; i++)
106-
{
107-
var asset = _savedAssets[i];
108-
109-
if (asset.Guid == guid)
110-
{
111-
entry = asset;
112-
return true;
113-
}
114-
}
115-
116-
entry = null;
117-
return false;
118-
}
119-
120-
private bool TryGetValue(Object obj, out AssetEntry entry)
121-
{
122-
for (int i = 0; i < _savedAssets.Length; i++)
123-
{
124-
var asset = _savedAssets[i];
125-
126-
if (asset.Asset == obj)
127-
{
128-
entry = asset;
129-
return true;
130-
}
131-
}
132-
133-
entry = null;
134-
return false;
135-
}
136-
}
137-
}
115+
}
116+
}

0 commit comments

Comments
 (0)