Skip to content

Commit

Permalink
Handle resource Name Collision (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
NavneetThekkumpat authored Sep 28, 2022
1 parent 7fb6c53 commit 2fae9da
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/PAModel/CanvasDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)
}
}

// Keeps track of all resource paths stabilized
var resourceStabilizer = new HashSet<string>(StringComparer.Ordinal);

// Update AssetFile paths
foreach (var resource in _resourcesJson.Resources.OrderBy(resource => resource.Name, StringComparer.Ordinal))
Expand All @@ -552,9 +554,10 @@ internal void StabilizeAssetFilePaths(ErrorContainer errors)

var originalName = resource.Name;
var assetFilePath = GetAssetFilePathWithoutPrefix(resource.Path);
if (!_assetFiles.TryGetValue(assetFilePath, out var fileEntry))
continue;
var pathToStabilize = resource.Path;

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

var withoutPrefix = GetAssetFilePathWithoutPrefix(resource.Path);
fileEntry.Name = withoutPrefix;
_assetFiles.Remove(assetFilePath);
var updatedPathToStabilize = resource.Path;

// Add or Update the assetFile path entry
if (_assetFiles.ContainsKey(withoutPrefix))
if (resourceStabilizer.Contains(pathToStabilize) || resourceStabilizer.Contains(updatedPathToStabilize))
{
_assetFiles.Remove(withoutPrefix);
_assetFiles.Add(withoutPrefix, fileEntry);
fileEntry.Name = withoutPrefix;
_assetFiles.Remove(assetFilePath);
_assetFiles[withoutPrefix] = fileEntry;
}
else
{
_assetFiles.Add(withoutPrefix, fileEntry);
// 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);
}

// For every duplicate asset file an additional <filename>.json file is created which contains information like - originalName, newFileName.
Expand Down
42 changes: 42 additions & 0 deletions src/PAModelTests/NameCollisionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,47 @@ public void TestDataSourceNameCollision(string appName)

Assert.IsFalse(errors.HasErrors);
}

[TestMethod]
public void TestAssetFileCollision()
{
var doc = new CanvasDocument();
var resource1 = new ResourceJson()
{
Name = "0012",
Path = "Assets\\Images\\0002.png",
FileName = "0002.png",
ResourceKind = ResourceKind.LocalFile,
Content = ContentKind.Image,
};

FileEntry f1 = new FileEntry();
f1.Name = new FilePath("Images", "0002.png");

// First Asset file
doc._assetFiles.Add(new FilePath("Images", "0002.png"), f1);

var resource2 = new ResourceJson()
{
Name = "0038",
Path = "Assets\\Images\\0012.png",
FileName = "0012.png",
ResourceKind = ResourceKind.LocalFile,
Content = ContentKind.Image,
};

FileEntry f2 = new FileEntry();
f2.Name = new FilePath("Images", "0012.png");

// Second Asset file
doc._assetFiles.Add(new FilePath("Images", "0012.png"), f2);

doc._resourcesJson = new ResourcesJson() { Resources = new ResourceJson[] { resource1, resource2 } };

var errorContainer = new ErrorContainer();
doc.StabilizeAssetFilePaths(errorContainer);

Assert.AreEqual(doc._assetFiles.Count(), 2);
}
}
}

0 comments on commit 2fae9da

Please sign in to comment.