-
Notifications
You must be signed in to change notification settings - Fork 27
/
ProjectCurator.cs
183 lines (157 loc) · 6.78 KB
/
ProjectCurator.cs
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
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System;
using System.Linq;
using System.IO;
namespace Ogxd.ProjectCurator
{
public static class ProjectCurator
{
[NonSerialized]
private static Dictionary<string, AssetInfo> guidToAssetInfo;
static ProjectCurator()
{
guidToAssetInfo = new Dictionary<string, AssetInfo>();
try {
var assetInfos = ProjectCuratorData.AssetInfos;
for (int i = 0; i < assetInfos.Length; i++) {
guidToAssetInfo.Add(assetInfos[i].guid, assetInfos[i]);
}
} catch (Exception e) {
Debug.LogError($"An error occurred while loading ProjectCurator database: {e}");
}
}
public static AssetInfo GetAsset(string guid)
{
guidToAssetInfo.TryGetValue(guid, out AssetInfo assetInfo);
return assetInfo;
}
public static AssetInfo AddAssetToDatabase(string guid, HashSet<string> referencers = null)
{
AssertGuidValid(guid);
AssetInfo assetInfo;
if (!guidToAssetInfo.TryGetValue(guid, out assetInfo)) {
guidToAssetInfo.Add(guid, assetInfo = new AssetInfo(guid));
}
var path = AssetDatabase.GUIDToAssetPath(guid);
var dependencyPaths = AssetDatabase.GetDependencies(path, recursive: false);
foreach (string dependencyPath in dependencyPaths) {
var dependencyGuid = AssetDatabase.AssetPathToGUID(dependencyPath);
if (
dependencyGuid != assetInfo.guid &&
guidToAssetInfo.TryGetValue(dependencyGuid, out AssetInfo depInfo)
) {
assetInfo.dependencies.Add(dependencyGuid);
depInfo.referencers.Add(assetInfo.guid);
// Included status may have changed and need to be recomputed
depInfo.ClearIncludedStatus();
}
}
if (referencers != null)
assetInfo.referencers = referencers;
return assetInfo;
}
public static AssetInfo RemoveAssetFromDatabase(string guid)
{
AssertGuidValid(guid);
if (guidToAssetInfo.TryGetValue(guid, out AssetInfo assetInfo)) {
foreach (string referencer in assetInfo.referencers) {
if (guidToAssetInfo.TryGetValue(referencer, out AssetInfo referencerAssetInfo)) {
if (referencerAssetInfo.dependencies.Remove(guid)) {
referencerAssetInfo.ClearIncludedStatus();
} else {
// Non-Reciprocity Error
Warn($"Asset '{FormatGuid(referencer)}' that depends on '{FormatGuid(guid)}' doesn't have it as a dependency");
}
} else {
Warn($"Asset '{FormatGuid(referencer)}' that depends on '{FormatGuid(guid)}' is not present in the database");
}
}
foreach (string dependency in assetInfo.dependencies) {
if (guidToAssetInfo.TryGetValue(dependency, out AssetInfo dependencyAssetInfo)) {
if (dependencyAssetInfo.referencers.Remove(guid)) {
dependencyAssetInfo.ClearIncludedStatus();
} else {
// Non-Reciprocity Error
Warn($"Asset '{FormatGuid(dependency)}' that is referenced by '{FormatGuid(guid)}' doesn't have it as a referencer");
}
} else {
Warn($"Asset '{FormatGuid(dependency)}' that is referenced by '{FormatGuid(guid)}' is not present in the database");
}
}
guidToAssetInfo.Remove(guid);
} else {
Warn($"Asset '{FormatGuid(guid)}' is not present in the database");
}
return assetInfo;
}
public static void ClearDatabase()
{
guidToAssetInfo.Clear();
}
public static void RebuildDatabase()
{
guidToAssetInfo = new Dictionary<string, AssetInfo>();
var allAssetPaths = AssetDatabase.GetAllAssetPaths();
// Ignore non-assets (package folder for instance) and directories
allAssetPaths = allAssetPaths
.Where(path => path.StartsWith("Assets/") && !Directory.Exists(path))
.ToArray();
EditorUtility.DisplayProgressBar("Building Dependency Database", "Gathering All Assets...", 0f);
// Gather all assets
for (int p = 0; p < allAssetPaths.Length; p++) {
string path = allAssetPaths[p];
string guid = AssetDatabase.AssetPathToGUID(path);
AssetInfo assetInfo = new AssetInfo(guid);
guidToAssetInfo.Add(assetInfo.guid, assetInfo);
}
// Find links between assets
for (int p = 0; p < allAssetPaths.Length; p++) {
var path = allAssetPaths[p];
if (p % 10 == 0) {
var cancel = EditorUtility.DisplayCancelableProgressBar("Building Dependency Database", path, (float)p / allAssetPaths.Length);
if (cancel) {
guidToAssetInfo = null;
break;
}
}
string guid = AssetDatabase.AssetPathToGUID(path);
AddAssetToDatabase(guid);
}
EditorUtility.ClearProgressBar();
ProjectCuratorData.IsUpToDate = true;
SaveDatabase();
}
public static void SaveDatabase()
{
if (guidToAssetInfo == null)
return;
var assetInfos = new AssetInfo[guidToAssetInfo.Count];
int i = 0;
foreach (var pair in guidToAssetInfo) {
assetInfos[i] = pair.Value;
i++;
}
ProjectCuratorData.AssetInfos = assetInfos;
ProjectCuratorData.Save();
}
static void Warn(string message)
{
Debug.LogWarning("ProjectCurator: " + message);
}
static string FormatGuid(string guid)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
return string.IsNullOrEmpty(path)
? $"(Missing asset with GUID={guid})"
: path;
}
static void AssertGuidValid(string guid)
{
if (string.IsNullOrEmpty(guid)) {
throw new ArgumentException("GUID required", nameof(guid));
}
}
}
}