Skip to content

Commit 2562288

Browse files
committed
assets matching line ids are now preferred.
I think we do need to come up with a better long term system for better association. Fixes YarnSpinnerTool/IssuesDiscussion#97
1 parent 687d462 commit 2562288

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2323
- Each assembly now gets it's own YSLS file when generating them.
2424
- renamed the test asmdef files from `YarnSpinnerTests.x` to `YarnSpinner.Unity.Tests.X` so that they are matched by the existing filters.
2525
- Generated YSLS file now matches the newer schema, see the YS core repo for details of this
26+
- When matching assets to line IDs the importer now prefers exact matches to the line ID
2627

2728
### Removed
2829

Editor/Utility/YarnProjectUtility.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -238,38 +238,41 @@ internal static Dictionary<string, string> FindAssetPathsForLineIDs(IEnumerable<
238238
var allFiles = Directory.EnumerateFiles(assetsFolderPath, "*", SearchOption.AllDirectories)
239239
.Where(path => path.EndsWith(".meta") == false)
240240
.Where(path => assetType.IsAssignableFrom(AssetDatabase.GetMainAssetTypeAtPath(path)));
241-
241+
242242
// Match files with those whose filenames contain a line ID
243-
var matchedFilesAndPaths = lineIDs.GroupJoin(
244-
// the elements we're matching lineIDs to
245-
allFiles,
246-
// the key for lineIDs (being strings, it's just the line ID
247-
// itself)
248-
lineID => lineID,
249-
// the key for assets (the filename without the path)
250-
assetPath => Path.GetFileName(assetPath),
251-
// the way we produce the result (a key-value pair)
252-
(lineID, assetPaths) =>
243+
// If a direct file match is found prefer that
244+
Dictionary<string, string> assets = new();
245+
foreach (var lineID in lineIDs)
246+
{
247+
var lineIDWithoutPrefix = lineID.Replace("line:", "").ToLowerInvariant();
248+
var candidates = new List<string>();
249+
foreach (var asset in allFiles)
253250
{
254-
if (assetPaths.Count() > 1)
251+
var file = Path.GetFileNameWithoutExtension(asset).ToLowerInvariant();
252+
253+
if (file == lineIDWithoutPrefix)
254+
{
255+
assets[lineIDWithoutPrefix] = asset;
256+
break;
257+
}
258+
259+
if (file.Contains(lineIDWithoutPrefix))
255260
{
256-
Debug.LogWarning($"Line {lineID} has {assetPaths.Count()} possible assets.\n{string.Join(", ", assetPaths)}");
261+
candidates.Add(asset);
257262
}
258-
return new { lineID, assetPaths };
259-
},
260-
// the way we test to see if two elements should be joined (does
261-
// the filename contain the line ID?)
262-
Compare.By<string>((fileName, lineID) =>
263+
}
264+
265+
var count = candidates.Count();
266+
if (count > 0)
263267
{
264-
var lineIDWithoutPrefix = lineID.Replace("line:", "");
265-
return Path.GetFileNameWithoutExtension(fileName).Contains(lineIDWithoutPrefix);
266-
})
267-
)
268-
// Discard any pair where no asset was found
269-
.Where(pair => pair.assetPaths.Count() > 0)
270-
.ToDictionary(entry => entry.lineID, entry => entry.assetPaths.FirstOrDefault());
271-
272-
return matchedFilesAndPaths;
268+
assets[lineID] = candidates.FirstOrDefault();
269+
if (count > 1)
270+
{
271+
Debug.LogWarning($"Discovered {count} candidates for {lineID}. Selecting one.");
272+
}
273+
}
274+
}
275+
return assets;
273276
}
274277

275278
/// <summary>

0 commit comments

Comments
 (0)