Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc fixes for GUID regeneration issues #719

Merged
merged 4 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions XRTK-Core/Packages/com.xrtk.core/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
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'));
}
}
}
1 change: 1 addition & 0 deletions articles/00-GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Once the build completes, you can install and run it on the device.

* [Downloading the XRTK](01-DownloadingTheXRTK.md)
* [Configuring your project](02-Configuration.md)
* [Known Issues](04-KnownIssues.md)

---

Expand Down
8 changes: 8 additions & 0 deletions articles/04-KnownIssues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Known issues

> Current known issues up to and including the 0.2 release

Issue | Severity | Description | Mitigation
|---|---|---|---|
| 1 | **Low** | Currently XRTK assets are copied from the individual packages to the Unity project for developers to use. Existing assets may potentially be overwritten, corrupted, or conflict if a copy already exists in the project. We're working on updating the import process to provide developers a clean copy they can modify safely without fear of this. | Developers are advised not to replace assets in the XRTK.Generated folder. When profiles / assets are cloned, place them outside this folder to prevent overwriting following an upgrade.
||||
2 changes: 2 additions & 0 deletions articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
href: 02-Configuration.md
- name: Platform Template Generator
href: 03-Template-Generator.md
- name: Known Issues
href: 04-KnownIssues.md
- name: Test Plans
items:
- name: Overview
Expand Down