Skip to content

Commit 2cf50b4

Browse files
author
Stewart Miles
committed
Filtered manifests from obsolete file list in VersionHandler.
VersionHandler no longer prompts to delete obsolete manifests in order to smooth the experience of upgrading packages. In addition, removed reimport of assets and flush of metadata modified by the VersionHandler as both operations are *really* slow. Asset renaming (AssetDatabase.ImportAsset()) is now the slowest operation in the plugin. Bug: 35141238, 35587604 Change-Id: I6b5923500b834d27253f00a22fcf482234208f3d
1 parent 309552c commit 2cf50b4

File tree

1 file changed

+65
-22
lines changed

1 file changed

+65
-22
lines changed

source/VersionHandler/src/VersionHandler.cs

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ public bool RenameAsset(string newFilename) {
402402
}
403403
}
404404
try {
405+
// b/35587604 this is *really* slow.
405406
string error = AssetDatabase.RenameAsset(
406407
filename, filenameComponents.basenameNoExtension);
407408
if (!String.IsNullOrEmpty(error)) {
@@ -410,7 +411,6 @@ public bool RenameAsset(string newFilename) {
410411
newFilename + " (" + error + ")");
411412
return false;
412413
}
413-
AssetDatabase.ImportAsset(newFilename);
414414
} catch (Exception) {
415415
// Unity 5.3 and below can end up throw all sorts of
416416
// exceptions here when attempting to reload renamed
@@ -523,6 +523,20 @@ public FileMetadata this[long version] {
523523
}
524524
}
525525

526+
/// <summary>
527+
/// Get the most referenced FileMetadata from this object.
528+
/// </summary>
529+
/// <returns>FileMetadata instance if this object contains at least one version, null
530+
/// otherwise.</returns>
531+
public FileMetadata MostRecentVersion {
532+
get {
533+
var numberOfVersions = metadataByVersion.Count;
534+
return numberOfVersions > 0 ?
535+
(FileMetadata)(
536+
(new ArrayList(metadataByVersion.Values))[numberOfVersions - 1]) : null;
537+
}
538+
}
539+
526540
/// <summary>
527541
/// Determine whether the PluginImporter class is available in
528542
/// UnityEditor. Unity 4 does not have the PluginImporter class so
@@ -569,9 +583,7 @@ public bool EnableMostRecentPlugins() {
569583

570584
// If the canonical file is out of date, update it.
571585
if (numberOfVersions > 0) {
572-
FileMetadata mostRecentVersion =
573-
(FileMetadata)((new ArrayList(metadataByVersion.Values))
574-
[numberOfVersions - 1]);
586+
FileMetadata mostRecentVersion = MostRecentVersion;
575587
if (mostRecentVersion.filename != filenameCanonical) {
576588
FileMetadata canonicalMetadata = null;
577589
foreach (var metadata in metadataByVersion.Values) {
@@ -643,9 +655,6 @@ public bool EnableMostRecentPlugins() {
643655
"Unexpected error enumerating targets: " + e.Message);
644656
}
645657
}
646-
if (modifiedThisVersion) {
647-
pluginImporter.SaveAndReimport();
648-
}
649658
// If the version was modified and it's obsolete keep track of
650659
// it to log it later.
651660
if (obsoleteVersion && modifiedThisVersion) {
@@ -832,6 +841,11 @@ public class ManifestReferences {
832841
/// </summary>
833842
public FileMetadata currentMetadata = null;
834843

844+
/// <summary>
845+
/// Metadata for each version of this manifest.
846+
/// </summary>
847+
public FileMetadataByVersion metadataByVersion = null;
848+
835849
/// <summary>
836850
/// Set of current files in this package.
837851
/// </summary>
@@ -870,6 +884,7 @@ public bool ParseManifests(FileMetadataByVersion metadataByVersion,
870884
foreach (FileMetadata metadata in metadataByVersion.Values) {
871885
versionIndex++;
872886
if (!metadata.isManifest) return false;
887+
this.metadataByVersion = metadataByVersion;
873888
bool manifestNeedsUpdate = false;
874889
HashSet<string> filesInManifest =
875890
versionIndex < numberOfVersions ?
@@ -949,12 +964,22 @@ public class ObsoleteFiles {
949964
/// </summary>
950965
public HashSet<string> unreferenced;
951966

967+
/// <summary>
968+
/// Same as the "unreferenced" member exluding manifest files.
969+
/// </summary>
970+
public HashSet<string> unreferencedExcludingManifests;
971+
952972
/// <summary>
953973
/// Obsolete files that are referenced by manifests. Each item in
954974
/// the dictionary contains a list of manifests referencing the file.
955975
/// </summary>
956976
public Dictionary<string, List<string>> referenced;
957977

978+
/// <summary>
979+
/// Same as the "referenced" member exluding manifest files.
980+
/// </summary>
981+
public Dictionary<string, List<string>> referencedExcludingManifests;
982+
958983
/// <summary>
959984
/// Build an ObsoleteFiles instance searching a set of
960985
/// ManifestReferences and a FileMetadataSet for old files.
@@ -975,22 +1000,31 @@ public ObsoleteFiles(
9751000
// global sets.
9761001
var currentFiles = new HashSet<string>();
9771002
var obsoleteFiles = new HashSet<string>();
1003+
var manifestFilenames = new HashSet<string>();
9781004
foreach (var manifestReferences in manifestReferencesList) {
9791005
currentFiles.UnionWith(manifestReferences.currentFiles);
9801006
obsoleteFiles.UnionWith(manifestReferences.obsoleteFiles);
1007+
foreach (var manifestMetadata in manifestReferences.metadataByVersion.Values) {
1008+
manifestFilenames.Add(manifestMetadata.filename);
1009+
}
9811010
}
9821011
// Fold in obsolete files that are not referenced by manifests.
9831012
foreach (var metadataByVersion in metadataSet.Values) {
984-
obsoleteFiles.UnionWith(
985-
metadataByVersion.FindObsoleteVersions());
1013+
var obsoleteVersions = metadataByVersion.FindObsoleteVersions();
1014+
obsoleteFiles.UnionWith(obsoleteVersions);
1015+
if (metadataByVersion.MostRecentVersion.isManifest) {
1016+
manifestFilenames.UnionWith(obsoleteVersions);
1017+
}
9861018
}
9871019
// Filter the obsoleteFiles set for all obsolete files currently
9881020
// in use and add to a dictionary indexed by filename
9891021
// which contains a list of manifest filenames which reference
9901022
// each file.
9911023
var referencedObsoleteFiles =
9921024
new Dictionary<string, List<string>>();
1025+
var referencedObsoleteFilesExcludingManifests = new Dictionary<string, List<string>>();
9931026
var obsoleteFilesToDelete = new HashSet<string>();
1027+
var obsoleteFilesToDeleteExcludingManifests = new HashSet<string>();
9941028
foreach (var obsoleteFile in obsoleteFiles) {
9951029
var manifestsReferencingFile = new List<string>();
9961030
foreach (var manifestReferences in manifestReferencesList) {
@@ -1004,15 +1038,25 @@ public ObsoleteFiles(
10041038
if (!File.Exists(obsoleteFile)) {
10051039
continue;
10061040
}
1041+
bool isManifest = manifestFilenames.Contains(obsoleteFile);
10071042
if (manifestsReferencingFile.Count > 0) {
10081043
referencedObsoleteFiles[obsoleteFile] =
10091044
manifestsReferencingFile;
1045+
if (!isManifest) {
1046+
referencedObsoleteFilesExcludingManifests[obsoleteFile] =
1047+
manifestsReferencingFile;
1048+
}
10101049
} else {
10111050
obsoleteFilesToDelete.Add(obsoleteFile);
1051+
if (!isManifest) {
1052+
obsoleteFilesToDeleteExcludingManifests.Add(obsoleteFile);
1053+
}
10121054
}
10131055
}
10141056
unreferenced = obsoleteFilesToDelete;
1057+
unreferencedExcludingManifests = obsoleteFilesToDeleteExcludingManifests;
10151058
referenced = referencedObsoleteFiles;
1059+
referencedExcludingManifests = referencedObsoleteFilesExcludingManifests;
10161060
}
10171061
}
10181062

@@ -1153,7 +1197,6 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
11531197

11541198
var metadataSet = FileMetadataSet.FindWithPendingUpdates(
11551199
FileMetadataSet.ParseFromFilenames(FindAllAssets()));
1156-
11571200
if (metadataSet.EnableMostRecentPlugins()) {
11581201
AssetDatabase.Refresh();
11591202
}
@@ -1166,13 +1209,14 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
11661209
// enabled.
11671210
bool deleteFiles = true;
11681211
if (obsoleteFiles.unreferenced.Count > 0) {
1169-
if (CleanUpPromptEnabled && deleteFiles) {
1212+
if (CleanUpPromptEnabled && deleteFiles &&
1213+
obsoleteFiles.unreferencedExcludingManifests.Count > 0) {
11701214
deleteFiles = EditorUtility.DisplayDialog(
11711215
PLUGIN_NAME,
11721216
"Would you like to delete the following obsolete files " +
11731217
"in your project?\n\n" +
11741218
String.Join("\n", new List<string>(
1175-
obsoleteFiles.unreferenced).ToArray()),
1219+
obsoleteFiles.unreferencedExcludingManifests).ToArray()),
11761220
"Yes", cancel: "No");
11771221
}
11781222
foreach (var filename in obsoleteFiles.unreferenced) {
@@ -1189,21 +1233,20 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
11891233
// confirmation of deletion.
11901234
if (obsoleteFiles.referenced.Count > 0) {
11911235
List<string> referencesString = new List<string>();
1192-
foreach (var item in obsoleteFiles.referenced) {
1236+
foreach (var item in obsoleteFiles.referencedExcludingManifests) {
11931237
List<string> lines = new List<string>();
1194-
lines.Add(item.Key);
11951238
foreach (var reference in item.Value) {
1196-
lines.Add(" " + reference);
1239+
lines.Add(String.Format("{0}: {1}", reference, item.Key));
11971240
}
11981241
referencesString.Add(String.Join("\n", lines.ToArray()));
11991242
}
1200-
deleteFiles = EditorUtility.DisplayDialog(
1201-
PLUGIN_NAME,
1202-
"The following obsolete files are referenced by packages in " +
1203-
"your project, would you like to delete them?\n\n" +
1204-
String.Join("\n", referencesString.ToArray()),
1205-
"Yes", cancel: "No");
1206-
1243+
deleteFiles = obsoleteFiles.referencedExcludingManifests.Values.Count == 0 ||
1244+
EditorUtility.DisplayDialog(
1245+
PLUGIN_NAME,
1246+
"The following obsolete files are referenced by packages in " +
1247+
"your project, would you like to delete them?\n\n" +
1248+
String.Join("\n", referencesString.ToArray()),
1249+
"Yes", cancel: "No");
12071250
foreach (var item in obsoleteFiles.referenced) {
12081251
if (deleteFiles) {
12091252
MoveAssetToTrash(item.Key);

0 commit comments

Comments
 (0)