Skip to content

Commit

Permalink
Updating asset collision handling (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjschneider authored Dec 15, 2022
1 parent 62a0429 commit e0e09e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/PAModel/CanvasDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,9 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)
}
}

// Keeps track of all resource paths stabilized
var resourceStabilizer = new HashSet<string>(StringComparer.Ordinal);
// Keep the newly renamed asset files separate so they do not overwrite any existing asset files
// due to collisions between the resource and file names.
Dictionary<FilePath, FileEntry> newAssetFiles = new Dictionary<FilePath, FileEntry>();

// Update AssetFile paths
foreach (var resource in _resourcesJson.Resources.Where(resource => resource?.Name != null && resource.ResourceKind == ResourceKind.LocalFile)
Expand All @@ -552,7 +553,7 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)
var pathToStabilize = resource.Path;

if (!_assetFiles.TryGetValue(assetFilePath, out var fileEntry))
continue;
continue;
if (!caseSensitiveNames.Contains(resource.Name) && caseInsensitiveNames.Contains(resource.Name))
{
int i = 1;
Expand Down Expand Up @@ -582,31 +583,11 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)
resource.FileName = newFileName;

var withoutPrefix = GetAssetFilePathWithoutPrefix(resource.Path);
var updatedPathToStabilize = resource.Path;
var updatedPathToStabilize = resource.Path;

if (resourceStabilizer.Contains(pathToStabilize) || resourceStabilizer.Contains(updatedPathToStabilize))
{
fileEntry.Name = withoutPrefix;
_assetFiles.Remove(assetFilePath);
_assetFiles[withoutPrefix] = fileEntry;
}
else
{
// This makes sure that the fileentry is updated to the updated path without prefix
fileEntry.Name = withoutPrefix;

// If the updated path key(withoutPrefix) already exists in assetFiles do not overwrite that file entry (avoid discarding the fileentry of the key that already exists)
// Else remove the old filepath and update the assetFiles with the new entry
if (!_assetFiles.ContainsKey(withoutPrefix))
{
_assetFiles.Remove(assetFilePath);
_assetFiles[withoutPrefix] = fileEntry;
}

// resourceStabilizer is updated with the old and new paths
resourceStabilizer.Add(pathToStabilize);
resourceStabilizer.Add(updatedPathToStabilize);
}
fileEntry.Name = withoutPrefix;
_assetFiles.Remove(assetFilePath);
newAssetFiles[withoutPrefix] = fileEntry;

// For every duplicate asset file an additional <filename>.json file is created which contains information like - originalName, newFileName.
if (resource.Name != originalName && !_localAssetInfoJson.ContainsKey(newFileName))
Expand All @@ -615,6 +596,12 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)
_localAssetInfoJson.Add(resource.FileName, new LocalAssetInfoJson() { OriginalName = originalName, NewFileName = resource.FileName, Path = assetFileInfoPath.ToPlatformPath() });
}
}

// Once we have all the new asset files, add them to the existing
foreach (FilePath newAssetKey in newAssetFiles.Keys)
{
_assetFiles[newAssetKey] = newAssetFiles[newAssetKey];
}
}

private int FindMaxEntropyFileName()
Expand Down Expand Up @@ -644,6 +631,10 @@ private void RestoreAssetFilePaths(ErrorContainer errors)

var maxFileNumber = FindMaxEntropyFileName();

// Keep the newly renamed asset files separate so they do not overwrite any existing asset files
// due to collisions between the resource and file names.
Dictionary<FilePath, FileEntry> newAssetFiles = new Dictionary<FilePath, FileEntry>();

foreach (var resource in _resourcesJson.Resources)
{
if (resource == null)
Expand Down Expand Up @@ -677,12 +668,18 @@ private void RestoreAssetFilePaths(ErrorContainer errors)
var withoutPrefix = GetAssetFilePathWithoutPrefix(resource.Path);
fileEntry.Name = withoutPrefix;
_assetFiles.Remove(assetFilePath);
_assetFiles[withoutPrefix] = fileEntry;
newAssetFiles[withoutPrefix] = fileEntry;
}

// Once we have all the new asset files, add them to the existing
foreach (FilePath newAssetKey in newAssetFiles.Keys)
{
_assetFiles[newAssetKey] = newAssetFiles[newAssetKey];
}
}
catch (NullReferenceException nullReferenceException)
{
errors.InternalError(nullReferenceException);
errors.InternalError(nullReferenceException);
return;
}
}
Expand Down
Binary file added src/PAModelTests/Apps/AssetPackingCollision.msapp
Binary file not shown.

0 comments on commit e0e09e6

Please sign in to comment.