-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDependencyDatabase.cs
More file actions
186 lines (163 loc) · 4.82 KB
/
DependencyDatabase.cs
File metadata and controls
186 lines (163 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#if !USE_SEARCH_DEPENDENCY_VIEWER || USE_SEARCH_MODULE
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEditor.Search
{
enum DependencyType : uint
{
File = 0x01,
Folder,
Asset,
Object,
BuiltIn,
External = 0xF000,
Unknown = 0xF0000000
};
enum DependencyLink
{
Direct = 1,
Weak
};
class DependencyItem
{
public readonly int id;
public readonly string path;
System.Type m_Type;
Texture m_Preview;
int? m_InstanceID;
public System.Type type
{
get
{
if (m_Type == null)
m_Type = AssetDatabase.GetMainAssetTypeAtPath(path);
return m_Type;
}
}
public Texture preview
{
get
{
if (!m_Preview)
m_Preview = AssetDatabase.GetCachedIcon(path);
return m_Preview;
}
}
public int instanceID
{
get
{
if (!m_InstanceID.HasValue)
m_InstanceID = DependencyUtils.GetMainAssetInstanceID(path);
return m_InstanceID.Value;
}
}
public DependencyItem(int id, string path)
{
this.id = id;
this.path = path;
}
}
interface IDependencyDatabase
{
public string GetResourceName(int id);
public DependencyType GetResourceType(int id);
public string GetResourceTypeName(int id);
public int[] GetResourceDependencies(int id);
public int[] GetResourceReferences(int id);
public int[] GetWeakDependencies(int id);
public Texture GetResourcePreview(int id);
public int FindResourceByName(in string path);
}
class DependencyDatabase : IDependencyDatabase
{
public DependencyDatabase()
{
m_NextId = 1;
m_IdByPath = new Dictionary<string, int>();
m_Items = new Dictionary<int, DependencyItem>();
}
volatile int m_NextId;
Dictionary<string, int> m_IdByPath;
Dictionary<int, DependencyItem> m_Items;
// Resource APIs
//
public int GetResourceID(int index)
{
Debug.Log($"GetResourceID({index})");
return 0;
}
public string GetResourceName(int id)
{
if (!m_Items.TryGetValue(id, out var di))
return null;
return di.path;
}
public DependencyType GetResourceType(int id)
{
Debug.Log($"GetResourceType({id})");
return DependencyType.Asset;
}
public string GetResourceTypeName(int id)
{
if (!m_Items.TryGetValue(id, out var di))
return null;
return di.type?.Name;
}
public int[] GetResourceDependencies(int id)
{
if (!m_Items.TryGetValue(id, out var di))
return new int[0];
return SearchItemIds("from", di.path).ToArray();
}
public int[] GetResourceReferences(int id)
{
if (!m_Items.TryGetValue(id, out var di))
return new int[0];
return SearchItemIds("ref", di.path).ToArray();
}
ISet<int> SearchItemIds(in string op, in string assetPath)
{
return new HashSet<int>(SearchGUIDs(op, assetPath)
.Select(guid => FindResourceByName(AssetDatabase.GUIDToAssetPath(guid))));
}
IEnumerable<string> SearchGUIDs(string op, string assetPath)
{
var depProvider = SearchService.GetProvider("dep");
using (var context = SearchService.CreateContext(depProvider, $"{op}=\"{assetPath}\""))
using (var request = SearchService.Request(context, SearchFlags.Synchronous))
{
foreach (var r in request)
{
if (r == null)
continue;
yield return r.id;
}
}
}
public int[] GetWeakDependencies(int id)
{
return new int[0];
}
public Texture GetResourcePreview(int id)
{
if (!m_Items.TryGetValue(id, out var di))
return null;
return di.preview;
}
// Find APIs
//
public int FindResourceByName(in string path)
{
if (m_IdByPath.TryGetValue(path, out var id))
return id;
var nextId = m_NextId++;
m_IdByPath[path] = nextId;
var di = new DependencyItem(nextId, path);
m_Items[di.id] = di;
return di.id;
}
}
}
#endif