Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Misc fixes for GUID regeneration issues (#719)
Browse files Browse the repository at this point in the history
* Initial check-in for agreed changes for asset referencing issues:

- Set GUID regeneration to FALSE by default - GUID regeneration has to be specifically called

* Reverted changes to include GUID registry

* Updated docs to refer to known potential issues

* Updated formatting and added mitigation for known issues
  • Loading branch information
SimonDarksideJ authored Dec 16, 2020
1 parent 0b4fd77 commit 47ec073
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 89 deletions.
4 changes: 2 additions & 2 deletions Editor/PackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class PackageInstaller
/// <param name="destinationPath">The destination path, typically inside the projects "Assets" directory.</param>
/// <param name="regenerateGuids">Should the guids for the copied assets be regenerated?</param>
/// <returns>Returns true if the profiles were successfully copies, installed, and added to the <see cref="MixedRealityToolkitRootProfile"/>.</returns>
public static bool TryInstallAssets(string sourcePath, string destinationPath, bool regenerateGuids = true)
public static bool TryInstallAssets(string sourcePath, string destinationPath, bool regenerateGuids = false)
{
return TryInstallAssets(new Dictionary<string, string> { { sourcePath, destinationPath } }, regenerateGuids);
}
Expand All @@ -38,7 +38,7 @@ public static bool TryInstallAssets(string sourcePath, string destinationPath, b
/// <param name="installationPaths">The assets paths to be installed. Key is the source path of the assets to be installed. This should typically be from a hidden upm package folder marked with a "~". Value is the destination.</param>
/// <param name="regenerateGuids">Should the guids for the copied assets be regenerated?</param>
/// <returns>Returns true if the profiles were successfully copies, installed, and added to the <see cref="MixedRealityToolkitRootProfile"/>.</returns>
public static bool TryInstallAssets(Dictionary<string, string> installationPaths, bool regenerateGuids = true)
public static bool TryInstallAssets(Dictionary<string, string> installationPaths, bool regenerateGuids = false)
{
var anyFail = false;
var newInstall = true;
Expand Down
98 changes: 11 additions & 87 deletions Editor/Utilities/GuidRegenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,6 @@ namespace XRTK.Editor.Utilities
/// </summary>
public static class GuidRegenerator
{
[Serializable]
private struct GuidPair
{
public string Key;
public string Value;
}

[Serializable]
private struct GuidMap
{
public List<GuidPair> Map;

public bool TryGetKey(string inGuid, out string outGuid)
{
for (int i = 0; i < Map.Count; i++)
{
if (Map[i].Value == inGuid)
{
outGuid = Map[i].Key;
return true;
}
}

outGuid = string.Empty;
return false;
}

public bool TryGetValue(string inGuid, out string outGuid)
{
for (int i = 0; i < Map.Count; i++)
{
if (Map[i].Key == inGuid)
{
outGuid = Map[i].Value;
return true;
}
}

outGuid = string.Empty;
return false;
}
}

private static readonly string GuidMapFilePath = $"{Application.dataPath}/{MixedRealityPreferences.ProfileGenerationPath}xrtk.guid.map.json";

/// <summary>
/// Regenerate the guids for assets located in the <see cref="assetsRootPath"/>.
/// </summary>
Expand Down Expand Up @@ -109,17 +64,13 @@ private static void RegenerateGuidsInternal(List<string> assetsRootPath)
filesPaths.AddRange(UnityFileHelper.GetUnityAssetsAtPath(assetsRootPath[i]));
}

var guidMap = File.Exists(GuidMapFilePath)
? JsonUtility.FromJson<GuidMap>(File.ReadAllText(GuidMapFilePath))
: new GuidMap { Map = new List<GuidPair>(0) };

// Create dictionary to hold old-to-new GUID map
var guidOldToNewMap = new Dictionary<string, string>();
var guidsInFileMap = new Dictionary<string, List<string>>();

// We must only replace GUIDs for Resources present in the path.
// Otherwise built-in resources (shader, meshes etc) get overwritten.
var guidsToReplace = new HashSet<string>();
var ownGuids = new HashSet<string>();

// Traverse all files, remember which GUIDs are in which files and generate new GUIDs
var counter = 0;
Expand All @@ -129,49 +80,32 @@ private static void RegenerateGuidsInternal(List<string> assetsRootPath)
EditorUtility.DisplayProgressBar("Gathering asset info...", filePath, counter / (float)filesPaths.Count);

var isFirstGuid = true;
var oldGuids = GetGuids(File.ReadAllText(filePath));
var guids = GetGuids(File.ReadAllText(filePath));

foreach (var oldGuid in oldGuids)
foreach (var oldGuid in guids)
{
// If we've previously regenerated the guids for this asset
// then replace the original guid key
var guid = guidMap.TryGetKey(oldGuid, out var originalGuid)
? originalGuid
: oldGuid;

// First GUID in .meta file is always the GUID of the asset itself
if (isFirstGuid && Path.GetExtension(filePath) == ".meta")
{
guidsToReplace.Add(guid);
ownGuids.Add(oldGuid);
isFirstGuid = false;
}

// Generate and save new GUID if we haven't added it before
if (!guidOldToNewMap.ContainsKey(guid))
if (!guidOldToNewMap.ContainsKey(oldGuid))
{
// If we've previously replaced this guid then
// use the generated guid from before
if (!guidMap.TryGetValue(guid, out var newGuid))
{
newGuid = Guid.NewGuid().ToString("N");
guidMap.Map.Add(new GuidPair
{
Key = guid,
Value = newGuid
});
}

guidOldToNewMap.Add(guid, newGuid);
var newGuid = Guid.NewGuid().ToString("N");
guidOldToNewMap.Add(oldGuid, newGuid);
}

if (!guidsInFileMap.ContainsKey(filePath))
{
guidsInFileMap[filePath] = new List<string>();
}

if (!guidsInFileMap[filePath].Contains(guid))
if (!guidsInFileMap[filePath].Contains(oldGuid))
{
guidsInFileMap[filePath].Add(guid);
guidsInFileMap[filePath].Add(oldGuid);
}
}

Expand All @@ -191,7 +125,7 @@ private static void RegenerateGuidsInternal(List<string> assetsRootPath)

foreach (var oldGuid in guidsInFileMap[filePath])
{
if (!guidsToReplace.Contains(oldGuid)) { continue; }
if (!ownGuids.Contains(oldGuid)) { continue; }

var newGuid = guidOldToNewMap[oldGuid];

Expand Down Expand Up @@ -221,16 +155,6 @@ private static void RegenerateGuidsInternal(List<string> assetsRootPath)
}
}

// Write guid map to file.
try
{
File.WriteAllText(GuidMapFilePath, JsonUtility.ToJson(guidMap));
}
catch (Exception e)
{
Debug.LogError(e);
}

EditorUtility.ClearProgressBar();
}

Expand Down Expand Up @@ -267,4 +191,4 @@ private static IEnumerable<string> GetGuids(string text)

private static bool IsGuid(string text) => text.All(c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'));
}
}
}

0 comments on commit 47ec073

Please sign in to comment.