Skip to content

Commit b39da08

Browse files
author
Stewart Miles
committed
Fixed unneccesary label changes and duplication.
I241cfe4a08faf2fd919a74c1c19f8bbc03e8ab5e introduce a new label prefix which preserves labels that would have been stripped out by old versions of the Version Handler. This change had the side effect of causing duplicate labels to be written in some cases. For example, an asset is labeled with gvhp_target-Editor (valid) but the label is rewritten as gvh_target-Editor (legacy compatible format) which causes two labels to be applied to the asset and an unnecessary update of the asset database. This change searches the current set of labels applied to an asset and applies the appropriate prefix based upon the currently applied labels to minimize the set of labels that are modified. Bug: 150886091 Change-Id: I43c98064135c8242f283725a62c23257a645c895
1 parent 8826651 commit b39da08

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

source/VersionHandlerImpl/src/VersionHandlerImpl.cs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,14 @@ private string CreateToken(string[] fieldPrefixes, string[] values) {
559559
/// <param name="fieldPrefixes">The first item of this list is used as the prefix.
560560
/// </param>
561561
/// <param name="values">Set of values to store with the field.</param>
562+
/// <param name="currentLabels">Labels to search for the suitable prefix.</param>
562563
/// <param name="preserve">Always preserve these labels.</param>
563564
private static string[] CreateLabels(string[] fieldPrefixes, IEnumerable<string> values,
564-
bool preserve = false) {
565+
HashSet<string> currentLabels, bool preserve = false) {
565566
string prefix = fieldPrefixes[0];
566567
List<string> labels = new List<string>();
567568
foreach (var value in values) {
568-
labels.Add(CreateLabel(prefix, value, preserve: preserve));
569+
labels.Add(CreateLabel(prefix, value, currentLabels, preserve: preserve));
569570
}
570571

571572
return labels.ToArray();
@@ -582,6 +583,23 @@ public static string CreateLabel(string prefix, string value, bool preserve = fa
582583
return (preserve ? LABEL_PREFIX_PRESERVE : LABEL_PREFIX) + prefix + value;
583584
}
584585

586+
/// <summary>
587+
/// Create an asset label keeping the preservation in the supplied set if it already exists.
588+
/// </summary>
589+
/// <param name="prefix"> The field prefix to be applied to the label.
590+
/// </param>
591+
/// <param name="value">The value to store in the field</param>
592+
/// <param name="currentLabels">Labels to search for the suitable prefix.</param>
593+
/// <param name="preserve">Whether the label should be preserved.</param>
594+
public static string CreateLabel(string prefix, string value, HashSet<string> currentLabels,
595+
bool preserve = false) {
596+
var legacyLabel = CreateLabel(prefix, value, false);
597+
var preservedLabel = CreateLabel(prefix, value, true);
598+
if (currentLabels.Contains(legacyLabel)) return legacyLabel;
599+
if (currentLabels.Contains(preservedLabel)) return preservedLabel;
600+
return preserve ? preservedLabel : legacyLabel;
601+
}
602+
585603
/// <summary>
586604
/// Determine whether this file is compatible with the editor.
587605
/// This is a special case as the editor isn't a "platform" covered
@@ -661,8 +679,8 @@ public void UpdateAssetLabels() {
661679
return;
662680
}
663681
AssetImporter importer = AssetImporter.GetAtPath(filename);
664-
var labels = new List<string>();
665-
var currentLabels = new List<string>();
682+
var labels = new HashSet<string>();
683+
var currentLabels = new HashSet<string>();
666684
// Strip labels we're currently managing.
667685
foreach (string label in AssetDatabase.GetLabels(importer)) {
668686
currentLabels.Add(label);
@@ -676,52 +694,56 @@ public void UpdateAssetLabels() {
676694
labels.Add(ASSET_LABEL);
677695
// Add labels for the metadata in this class.
678696
if (!String.IsNullOrEmpty(versionString)) {
679-
labels.Add(CreateLabel(TOKEN_VERSION[0], versionString));
697+
labels.Add(CreateLabel(TOKEN_VERSION[0], versionString, currentLabels));
680698
}
681699
if (targets != null && targets.Count > 0) {
682-
labels.AddRange(CreateLabels(TOKEN_TARGETS, targets));
700+
labels.UnionWith(CreateLabels(TOKEN_TARGETS, targets, currentLabels));
683701

684702
if (!isHandledByPluginImporter) {
685703
labels.Add(ASSET_LABEL_RENAME_TO_DISABLE);
686704
}
687705
}
688706
if (dotNetTargets != null && dotNetTargets.Count > 0) {
689-
labels.AddRange(CreateLabels(TOKEN_DOTNET_TARGETS, dotNetTargets));
707+
labels.UnionWith(CreateLabels(TOKEN_DOTNET_TARGETS, dotNetTargets, currentLabels));
690708
}
691709
if (!String.IsNullOrEmpty(linuxLibraryBasename)) {
692-
labels.Add(CreateLabel(TOKEN_LINUX_LIBRARY_BASENAME[0], linuxLibraryBasename));
710+
labels.Add(CreateLabel(TOKEN_LINUX_LIBRARY_BASENAME[0], linuxLibraryBasename,
711+
currentLabels));
693712
}
694713
if (!String.IsNullOrEmpty(exportPath)) {
695-
labels.Add(CreateLabel(TOKEN_EXPORT_PATH[0], exportPath, preserve: true));
714+
labels.Add(CreateLabel(TOKEN_EXPORT_PATH[0], exportPath, currentLabels,
715+
preserve: true));
696716
}
697717
if (isManifest) {
698-
labels.Add(CreateLabel(TOKEN_MANIFEST[0], null));
718+
labels.Add(CreateLabel(TOKEN_MANIFEST[0], null, currentLabels));
699719
}
700720
if (customManifestNames != null && customManifestNames.Count > 0) {
701721
foreach (var indexAndName in customManifestNames) {
702722
int order = indexAndName.Key + CUSTOM_MANIFEST_NAMES_FIRST_INDEX_OFFSET;
703723
var name = indexAndName.Value;
704724
if (order < CUSTOM_MANIFEST_NAMES_FIRST_INDEX_OFFSET) {
705725
labels.Add(CreateLabel(TOKEN_MANIFEST_NAME[0], order.ToString() + name,
706-
preserve: true));
726+
currentLabels, preserve: true));
707727
} else {
708-
labels.Add(CreateLabel(TOKEN_MANIFEST_NAME[0], name, preserve: true));
728+
labels.Add(CreateLabel(TOKEN_MANIFEST_NAME[0], name, currentLabels,
729+
preserve: true));
709730
}
710731
}
711732
}
712-
var uniqueLabels = new HashSet<string>(labels);
713-
labels = new List<string>(uniqueLabels);
714-
if (!uniqueLabels.SetEquals(new HashSet<string>(currentLabels))) {
715-
currentLabels.Sort();
716-
labels.Sort();
733+
if (!labels.SetEquals(currentLabels)) {
734+
var sortedLabels = new List<string>(labels);
735+
var sortedCurrentLabels = new List<string>(currentLabels);
736+
sortedCurrentLabels.Sort();
737+
sortedLabels.Sort();
738+
var labelsArray = sortedLabels.ToArray();
717739
Log(String.Format("Changing labels of {0}\n" +
718740
"from: {1}\n" +
719-
"to: {2}\n",
741+
"to: {2}\n",
720742
filename,
721-
String.Join(", ", currentLabels.ToArray()),
722-
String.Join(", ", labels.ToArray())),
743+
String.Join(", ", sortedCurrentLabels.ToArray()),
744+
String.Join(", ", labelsArray)),
723745
verbose: true);
724-
AssetDatabase.SetLabels(importer, labels.ToArray());
746+
AssetDatabase.SetLabels(importer, labelsArray);
725747
}
726748
}
727749

0 commit comments

Comments
 (0)