|
19 | 19 | using UnityEngine; |
20 | 20 | using UnityEngine.UIElements; |
21 | 21 | using Trackman; |
| 22 | +using Object = UnityEngine.Object; |
22 | 23 | using Debug = UnityEngine.Debug; |
23 | 24 |
|
24 | 25 | // ReSharper disable MemberCanBeMadeStatic.Local |
@@ -263,15 +264,12 @@ static async Task UpdateTitleAsync(UIDocument document, Figma figma, int progres |
263 | 264 | { |
264 | 265 | async Task AddMissingComponentsAsync(FigmaParser parser, Dictionary<string, string> headers, DocumentNode documentNode) |
265 | 266 | { |
266 | | - if (parser.MissingComponents.Count > 0) |
| 267 | + Nodes nodes = JsonUtility.FromJson<Nodes>(await $"{api}/files/{title}/nodes?ids={string.Join(",", parser.MissingComponents.Distinct())}".HttpGetAsync(headers, cancellationToken: token)); |
| 268 | + foreach (Nodes.Document value in nodes.nodes.Values.Where(value => value is not null)) |
267 | 269 | { |
268 | | - Nodes nodes = JsonUtility.FromJson<Nodes>(await $"{api}/files/{title}/nodes?ids={string.Join(",", parser.MissingComponents.Distinct())}".HttpGetAsync(headers, cancellationToken: token)); |
269 | | - foreach (Nodes.Document value in nodes.nodes.Values.Where(value => value is not null)) |
270 | | - { |
271 | | - value.document.parent = documentNode; |
272 | | - value.document.SetParentRecursively(); |
273 | | - parser.AddMissingComponent(value.document, value.styles); |
274 | | - } |
| 270 | + value.document.parent = documentNode; |
| 271 | + value.document.SetParentRecursively(); |
| 272 | + parser.AddMissingComponent(value.document, value.styles); |
275 | 273 | } |
276 | 274 | } |
277 | 275 | void InitializeMetadata(DocumentNode documentNode) |
@@ -458,11 +456,13 @@ bool CleanupAfter() |
458 | 456 | if (parser.PngNodes.Any(x => x.ShouldDownload(UxmlDownloadImages.RenderAsPng))) |
459 | 457 | { |
460 | 458 | Progress.SetDescription(progress, "Downloading png images"); |
| 459 | + |
461 | 460 | int i = 0; |
462 | 461 | IEnumerable<IGrouping<int, string>> items = parser.PngNodes.Where(x => x.ShouldDownload(UxmlDownloadImages.RenderAsPng)).Select(y => y.id).GroupBy(_ => i++ / 100); |
463 | 462 | Task<byte[]>[] tasks = items.Select((group) => $"{api}/images/{title}?ids={string.Join(",", group)}&format=png".HttpGetAsync(defaultRequestHeaders, cancellationToken: token)).ToArray(); |
464 | 463 | await Task.WhenAll(tasks); |
465 | 464 | IEnumerable<KeyValuePair<string, string>> images = tasks.SelectMany(t => JsonUtility.FromJson<Images>(t.Result).images); |
| 465 | + |
466 | 466 | svgToPngSyncTask = images.ForEachParallelAsync(maxConcurrentRequests, DownloadMethodFor("png", AddPngImport), token); |
467 | 467 | } |
468 | 468 | if (parser.SvgNodes.Any(x => x.ShouldDownload(UxmlDownloadImages.RenderAsSvg))) |
@@ -593,9 +593,9 @@ string GetFontPath(string name, string extension) |
593 | 593 | if (valid) |
594 | 594 | { |
595 | 595 | SVGImporter importer = (SVGImporter)AssetImporter.GetAtPath(Path.Combine(relativeFolder, path)); |
596 | | - UnityEngine.Object vectorImage = AssetDatabase.LoadMainAssetAtPath(Path.Combine(relativeFolder, path)); |
| 596 | + Object vectorImage = AssetDatabase.LoadMainAssetAtPath(Path.Combine(relativeFolder, path)); |
597 | 597 |
|
598 | | - if (vectorImage.GetType().GetField("size", BindingFlags.NonPublic | BindingFlags.Instance) is { } fieldInfo) |
| 598 | + if (vectorImage && vectorImage.GetType().GetField("size", BindingFlags.NonPublic | BindingFlags.Instance) is { } fieldInfo) |
599 | 599 | { |
600 | 600 | Vector2 size = (Vector2)fieldInfo.GetValue(vectorImage); |
601 | 601 | return (true, Mathf.CeilToInt(size.x), Mathf.CeilToInt(size.y)); |
@@ -625,7 +625,7 @@ string GetFontPath(string name, string extension) |
625 | 625 | FigmaParser parser = new(files.document, files.styles, GetAssetPath, GetAssetSize); |
626 | 626 |
|
627 | 627 | Progress.Report(progress, 3, 5, "Downloading missing nodes"); |
628 | | - await AddMissingComponentsAsync(parser, headers, files.document); |
| 628 | + if (parser.MissingComponents.Count > 0) await AddMissingComponentsAsync(parser, headers, files.document); |
629 | 629 |
|
630 | 630 | Progress.Report(progress, 4, 5, "Downloading images"); |
631 | 631 | Func<bool> cleanupImages = default; |
|
0 commit comments