Skip to content

Commit 4accade

Browse files
committed
Improve stability
1 parent bbbe9d0 commit 4accade

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Editor/Inspector/FigmaInspector.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using UnityEngine;
2020
using UnityEngine.UIElements;
2121
using Trackman;
22+
using Object = UnityEngine.Object;
2223
using Debug = UnityEngine.Debug;
2324

2425
// ReSharper disable MemberCanBeMadeStatic.Local
@@ -263,15 +264,12 @@ static async Task UpdateTitleAsync(UIDocument document, Figma figma, int progres
263264
{
264265
async Task AddMissingComponentsAsync(FigmaParser parser, Dictionary<string, string> headers, DocumentNode documentNode)
265266
{
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))
267269
{
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);
275273
}
276274
}
277275
void InitializeMetadata(DocumentNode documentNode)
@@ -458,11 +456,13 @@ bool CleanupAfter()
458456
if (parser.PngNodes.Any(x => x.ShouldDownload(UxmlDownloadImages.RenderAsPng)))
459457
{
460458
Progress.SetDescription(progress, "Downloading png images");
459+
461460
int i = 0;
462461
IEnumerable<IGrouping<int, string>> items = parser.PngNodes.Where(x => x.ShouldDownload(UxmlDownloadImages.RenderAsPng)).Select(y => y.id).GroupBy(_ => i++ / 100);
463462
Task<byte[]>[] tasks = items.Select((group) => $"{api}/images/{title}?ids={string.Join(",", group)}&format=png".HttpGetAsync(defaultRequestHeaders, cancellationToken: token)).ToArray();
464463
await Task.WhenAll(tasks);
465464
IEnumerable<KeyValuePair<string, string>> images = tasks.SelectMany(t => JsonUtility.FromJson<Images>(t.Result).images);
465+
466466
svgToPngSyncTask = images.ForEachParallelAsync(maxConcurrentRequests, DownloadMethodFor("png", AddPngImport), token);
467467
}
468468
if (parser.SvgNodes.Any(x => x.ShouldDownload(UxmlDownloadImages.RenderAsSvg)))
@@ -593,9 +593,9 @@ string GetFontPath(string name, string extension)
593593
if (valid)
594594
{
595595
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));
597597

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)
599599
{
600600
Vector2 size = (Vector2)fieldInfo.GetValue(vectorImage);
601601
return (true, Mathf.CeilToInt(size.x), Mathf.CeilToInt(size.y));
@@ -625,7 +625,7 @@ string GetFontPath(string name, string extension)
625625
FigmaParser parser = new(files.document, files.styles, GetAssetPath, GetAssetSize);
626626

627627
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);
629629

630630
Progress.Report(progress, 4, 5, "Downloading images");
631631
Func<bool> cleanupImages = default;

0 commit comments

Comments
 (0)