Skip to content

Commit 569cab9

Browse files
author
Stewart Miles
committed
Added exportpath metadata support to Version Handler.
* Added exportpath which allows a file to be mapped to the initial location of the file. This will make it possible for a manifest to reference a file that has been moved from it's initial location. * Added gvhp_ prefix since older versions of Version Handler will strip all gvh_ prefixed labels they don't understand. Bug: 150886091 Change-Id: I241cfe4a08faf2fd919a74c1c19f8bbc03e8ab5e
1 parent 3da2838 commit 569cab9

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ Unity plugins can be managed by the Version Handler using the following steps:
438438
to manage.
439439
1. Add the `gvh_version-VERSION` label to each asset where `VERSION` is the
440440
version of the plugin you're releasing (e.g 1.2.3).
441+
1. Add the `gvhp_exportpath-PATH` label to each asset where `PATH` is the
442+
export path of the file when the `.unitypackage` is created. This is
443+
used to track files if they're moved around in a project by developers.
441444
1. Optional: Add `gvh_targets-editor` label to each editor DLL in your
442445
plugin and disable `editor` as a target platform for the DLL.
443446
The Version Handler will enable the most recent version of this DLL when

source/VersionHandlerImpl/src/VersionHandlerImpl.cs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public FilenameComponents(string filename) {
6464
}
6565
}
6666

67-
6867
// Separator for metadata tokens in the supplied filename.
6968
private static char[] FILENAME_TOKEN_SEPARATOR = new char[] { '_' };
7069
// Separator for fields in each metadata token in the supplied
@@ -84,6 +83,8 @@ public FilenameComponents(string filename) {
8483
public static string[] TOKEN_MANIFEST = new [] { "manifest" };
8584
// Prefix which identifies the canonical name of this Linux library.
8685
public static string[] TOKEN_LINUX_LIBRARY_BASENAME = new [] { "linuxlibname-" };
86+
// Prefix which identifies the original path of a file when the package was exported.
87+
public static string[] TOKEN_EXPORT_PATH = new [] { "exportpath-" };
8788

8889
// Delimiter for version numbers.
8990
private static char[] VERSION_DELIMITER = new char[] { '.' };
@@ -94,6 +95,9 @@ public FilenameComponents(string filename) {
9495
private static long VERSION_COMPONENT_MULTIPLIER = 1000;
9596
// Prefix for labels which encode metadata of an asset.
9697
private static string LABEL_PREFIX = "gvh_";
98+
// Prefix for labels which encode metadata of the asset for 1.2.138 and above.
99+
// These labels are never removed by the version handler.
100+
private static string LABEL_PREFIX_PRESERVE = "gvhp_";
97101
// Initialized depending on the version of unity we are running against
98102
private static HashSet<BuildTarget> targetBlackList = null;
99103
// Initialized by parsing BuildTarget enumeration values from
@@ -298,15 +302,34 @@ public static string ScriptingRuntimeDotNetVersion {
298302
/// </summary>
299303
public string linuxLibraryBasename = null;
300304

305+
/// <summary>
306+
/// Path of the file when it was originally exported as a package.
307+
/// </summary>
308+
public string exportPath = "";
309+
301310
/// <summary>
302311
/// Parse metadata from filename and store in this class.
303312
/// </summary>
304313
/// <param name="filename">Name of the file to parse.</param>
305314
public FileMetadata(string filename) {
306315
this.filename = FileUtils.NormalizePathSeparators(filename);
307-
filenameCanonical = this.filename;
316+
filenameCanonical = ParseMetadataFromFilename(this.filename);
317+
ParseMetadataFromAssetLabels();
318+
319+
// If the export path was specified, override the canonical filename.
320+
if (!String.IsNullOrEmpty(exportPath)) {
321+
filenameCanonical = ParseMetadataFromFilename(exportPath);
322+
}
323+
UpdateAssetLabels();
324+
}
308325

309-
var filenameComponents = new FilenameComponents(filename);
326+
/// <summary>
327+
/// Parse metadata from the specified filename and store in this class.
328+
/// </summary>
329+
/// <param name="filenameToParse">Parse metadata from the specified filename.</param>
330+
/// <retuns>Filename with metadata removed.</returns>
331+
private string ParseMetadataFromFilename(string filenameToParse) {
332+
var filenameComponents = new FilenameComponents(filenameToParse);
310333
// Parse metadata from the filename.
311334
string[] tokens =
312335
filenameComponents.basenameNoExtension.Split(
@@ -320,28 +343,31 @@ public FileMetadata(string filename) {
320343
}
321344
filenameComponents.basenameNoExtension = basenameNoExtension;
322345
}
323-
// Parse metadata from asset labels if it hasn't been specified in
324-
// the filename.
325-
AssetImporter importer = GetAssetImporter();
326-
if (importer != null) {
327-
foreach (string label in AssetDatabase.GetLabels(importer)) {
328-
ParseLabel(label);
329-
}
330-
331-
isHandledByPluginImporter = typeof(PluginImporter).IsInstanceOfType(importer);
332-
}
333-
334346
// On Windows the AssetDatabase converts native path separators
335347
// used by the .NET framework '\' to *nix style '/' such that
336348
// System.IO.Path generated paths will not match those looked up
337349
// in the asset database. So we convert the output of Path.Combine
338350
// here to use *nix style paths so that it's possible to perform
339351
// simple string comparisons to check for path equality.
340-
filenameCanonical = FileUtils.NormalizePathSeparators(Path.Combine(
352+
return FileUtils.NormalizePathSeparators(Path.Combine(
341353
filenameComponents.directory,
342354
filenameComponents.basenameNoExtension +
343355
filenameComponents.extension));
344-
UpdateAssetLabels();
356+
}
357+
358+
/// <summary>
359+
/// Parse metadata from asset labels.
360+
/// </summary>
361+
public void ParseMetadataFromAssetLabels() {
362+
// Parse metadata from asset labels if it hasn't been specified in
363+
// the filename.
364+
AssetImporter importer = GetAssetImporter();
365+
if (importer != null) {
366+
foreach (string label in AssetDatabase.GetLabels(importer)) {
367+
ParseLabel(label);
368+
}
369+
isHandledByPluginImporter = typeof(PluginImporter).IsInstanceOfType(importer);
370+
}
345371
}
346372

347373
/// <summary>
@@ -423,6 +449,12 @@ private bool ParseToken(string token, string prefix = null) {
423449
linuxLibraryBasename = String.Join(FIELD_SEPARATOR[0].ToString(), values);
424450
return true;
425451
}
452+
values = MatchPrefixesGetValues(token, TOKEN_EXPORT_PATH, prefix);
453+
if (values != null) {
454+
exportPath = FileUtils.NormalizePathSeparators(
455+
String.Join(FIELD_SEPARATOR[0].ToString(), values));
456+
return true;
457+
}
426458
return false;
427459
}
428460

@@ -432,7 +464,8 @@ private bool ParseToken(string token, string prefix = null) {
432464
/// <param name="label">Asset label to parse.</param>
433465
/// <returns>true if the token is parsed, false otherwise.</returns>
434466
private bool ParseLabel(string label) {
435-
return ParseToken(label, prefix: LABEL_PREFIX);
467+
return ParseToken(label, prefix: LABEL_PREFIX_PRESERVE) ||
468+
ParseToken(label, prefix: LABEL_PREFIX);
436469
}
437470

438471
/// <summary>
@@ -453,11 +486,13 @@ private string CreateToken(string[] fieldPrefixes, string[] values) {
453486
/// <param name="fieldPrefixes">The first item of this list is used as the prefix.
454487
/// </param>
455488
/// <param name="values">Set of values to store with the field.</param>
456-
private static string[] CreateLabels(string[] fieldPrefixes, IEnumerable<string> values) {
489+
/// <param name="preserve">Always preserve these labels.</param>
490+
private static string[] CreateLabels(string[] fieldPrefixes, IEnumerable<string> values,
491+
bool preserve = false) {
457492
string prefix = fieldPrefixes[0];
458493
List<string> labels = new List<string>();
459494
foreach (var value in values) {
460-
labels.Add(CreateLabel(prefix, value));
495+
labels.Add(CreateLabel(prefix, value, preserve: preserve));
461496
}
462497

463498
return labels.ToArray();
@@ -469,8 +504,9 @@ private static string[] CreateLabels(string[] fieldPrefixes, IEnumerable<string>
469504
/// <param name="prefix"> The field prefix to be applied to the label.
470505
/// </param>
471506
/// <param name="value">The value to store in the field</param>
472-
public static string CreateLabel(string prefix, string value) {
473-
return LABEL_PREFIX + prefix + value;
507+
/// <param name="preserve">Whether the label should be preserved.</param>
508+
public static string CreateLabel(string prefix, string value, bool preserve = false) {
509+
return (preserve ? LABEL_PREFIX_PRESERVE : LABEL_PREFIX) + prefix + value;
474510
}
475511

476512
/// <summary>
@@ -580,6 +616,9 @@ public void UpdateAssetLabels() {
580616
if (!String.IsNullOrEmpty(linuxLibraryBasename)) {
581617
labels.Add(CreateLabel(TOKEN_LINUX_LIBRARY_BASENAME[0], linuxLibraryBasename));
582618
}
619+
if (!String.IsNullOrEmpty(exportPath)) {
620+
labels.Add(CreateLabel(TOKEN_EXPORT_PATH[0], exportPath, preserve: true));
621+
}
583622
if (isManifest) {
584623
labels.Add(CreateLabel(TOKEN_MANIFEST[0], null));
585624
}

0 commit comments

Comments
 (0)