Repository navigation
Expand file tree
/
Copy pathFigmaDownloader.cs
More file actions
308 lines (247 loc) · 13.2 KB
/
Copy pathFigmaDownloader.cs
File metadata and controls
308 lines (247 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;
namespace Figma
{
using Core;
using Core.Assets;
using Internals;
using static Internals.Const;
using static Internals.PathExtensions;
internal class FigmaDownloader : Api
{
#region Consts
const int maxConcurrentRequests = 5;
const int maxComponentsIdsInOneRequest = 400;
#endregion
#region Fields
readonly AssetsInfo assetsInfo;
string componentsDirectoryPath;
string elementsDirectoryPath;
string imagesDirectoryPath;
string framesDirectoryPath;
FigmaWriter figmaWriter;
NodeMetadata nodeMetadata;
NodesRegistry nodesRegistry;
StylesPreprocessor stylesPreprocessor;
#endregion
#region Constructors
internal FigmaDownloader(string personalAccessToken, string fileKey, AssetsInfo assetsInfo) : base(personalAccessToken, fileKey) => this.assetsInfo = assetsInfo;
#endregion
#region Methods
internal async Task Run(bool downloadImages, string uxmlName, IReadOnlyCollection<Type> frames, bool prune, bool filter, bool systemCopyBuffer, int progress, CancellationToken token)
{
Directory.CreateDirectory(framesDirectoryPath = assetsInfo.GetAbsolutePath(framesDirectoryName));
Directory.CreateDirectory(imagesDirectoryPath = assetsInfo.GetAbsolutePath(imagesDirectoryName));
Directory.CreateDirectory(elementsDirectoryPath = assetsInfo.GetAbsolutePath(elementsDirectoryName));
Directory.CreateDirectory(componentsDirectoryPath = assetsInfo.GetAbsolutePath(componentsDirectoryName));
int steps = downloadImages ? 5 : 4;
await assetsInfo.cachedAssets.LoadAsync(token);
Progress.Report(progress, 1, steps, "Downloading file");
List<string> visibleSceneNodes = new(32);
if (filter)
{
Progress.SetDescription(progress, "Filtering nodes");
Data shallowData = await GetAsync<Data>($"files/{fileKey}?depth=2", token);
shallowData.document.SetParent();
NodeMetadata shallowMetadata = new(shallowData.document, frames, true, false, true);
visibleSceneNodes.AddRange(shallowData.document.children.SelectMany(x => x.children).Where(shallowMetadata.EnabledInHierarchy).Select(node => node.id));
Progress.SetDescription(progress, string.Empty);
}
string idsString = string.Empty;
if (visibleSceneNodes.Any())
idsString = $"?ids={string.Join(",", visibleSceneNodes)}";
Progress.SetDescription(progress, "Resolving Figma file");
string json = await GetJsonAsync($"files/{fileKey}{idsString}", token);
if (systemCopyBuffer)
GUIUtility.systemCopyBuffer = json;
Progress.Report(progress, 2, steps, "Parsing Figma file");
Data data = await ConvertOnBackgroundAsync<Data>(json, token);
data.document.SetParent();
Progress.SetDescription(progress, "Creating entities");
nodeMetadata = new NodeMetadata(data.document, frames, filter);
nodesRegistry = new NodesRegistry(data, nodeMetadata);
stylesPreprocessor = new StylesPreprocessor(data, assetsInfo);
figmaWriter = new FigmaWriter(assetsInfo.directory, uxmlName, data, stylesPreprocessor, nodeMetadata, assetsInfo);
Progress.Report(progress, 3, steps, "Downloading missing components");
await DownloadDocumentsAsync(token);
if (downloadImages)
{
Progress.Report(progress, 4, steps, "Downloading images");
Progress.SetDescription(progress, "Writing Gradients");
await WriteGradientsAsync(token);
Progress.SetDescription(progress, "Downloading image fills");
await GetImageFillsAsync(progress, nodesRegistry.ImageFills, token);
Progress.SetDescription(progress, $"Downloading {KnownFormats.png} files");
await GetImageNodesAsync(progress, nodesRegistry.Pngs, UxmlDownloadImages.RenderAsPng, KnownFormats.png, token);
Progress.SetDescription(progress, $"Downloading {KnownFormats.svg} files");
await GetImageNodesAsync(progress, nodesRegistry.Svgs, UxmlDownloadImages.RenderAsSvg, KnownFormats.svg, token);
await assetsInfo.cachedAssets.SaveAsync();
}
Progress.SetStepLabel(progress, string.Empty);
Progress.Report(progress, steps, steps, "Updating *.uss/*.uxml files");
await figmaWriter.WriteAsync(prune);
}
internal void CleanUp(bool cleanImages = false)
{
void CleanDirectory(string directory, string[] filters)
{
IEnumerable<string> target = filters.SelectMany(filter => GetFiles(directory, filter, SearchOption.AllDirectories))
.Where(fileName => !assetsInfo.modifiedContent.Contains(fileName));
foreach (string file in target)
{
FileUtil.DeleteFileOrDirectory(file);
string meta = $"{file}.{KnownFormats.meta}";
if (File.Exists(meta))
FileUtil.DeleteFileOrDirectory(meta);
}
}
string[] textExtensions = { $"*.{KnownFormats.uxml}", $"*.{KnownFormats.uss}" };
CleanDirectory(componentsDirectoryPath, textExtensions);
CleanDirectory(elementsDirectoryPath, textExtensions);
CleanDirectory(framesDirectoryPath, textExtensions);
if (!cleanImages)
return;
string[] imagesExtensions = { $"*.{KnownFormats.svg}", $"*.{KnownFormats.png}" };
CleanDirectory(imagesDirectoryPath, imagesExtensions);
}
public void RemoveEmptyDirectories()
{
void RemoveDirectoryWithMeta(string path)
{
FileUtil.DeleteFileOrDirectory(path);
string meta = $"{path}.{KnownFormats.meta}";
if (File.Exists(meta))
FileUtil.DeleteFileOrDirectory(meta);
}
void RemoveEmptyDirectory(string path, bool recursive)
{
if (!Directory.Exists(path))
return;
if (recursive)
{
Stack<string> directories = new();
directories.Push(path);
for (int depth = 0; depth < Const.maximumAllowedDepthLimit; depth++)
{
if (directories.Count == 0)
return;
path = directories.Pop();
if (!Directory.EnumerateFileSystemEntries(path).Any())
RemoveDirectoryWithMeta(path);
else
foreach (string subDirectory in Directory.EnumerateDirectories(path))
directories.Push(subDirectory);
}
throw new InvalidOperationException(Const.maximumDepthLimitReachedExceptionMessage);
}
if (Directory.EnumerateFileSystemEntries(path).Any())
return;
RemoveDirectoryWithMeta(path);
}
RemoveEmptyDirectory(componentsDirectoryPath, false);
RemoveEmptyDirectory(elementsDirectoryPath, false);
RemoveEmptyDirectory(framesDirectoryPath, true);
RemoveEmptyDirectory(imagesDirectoryPath, false);
}
#endregion
#region Support Methods
async Task DownloadDocumentsAsync(CancellationToken token)
{
async Task<IEnumerable<Nodes.Document>> GetMissingComponentsAsync(IEnumerable<string> components) =>
(await Task.WhenAll(components.Chunk(maxComponentsIdsInOneRequest)
.Select(chunk => GetAsync<Nodes>($"files/{fileKey}/nodes?ids={string.Join(",", chunk.Distinct())}", token))))
.SelectMany(node => node.nodes.Values.Where(value => value != null));
if (nodesRegistry.MissingComponents.Count > 0)
foreach (Nodes.Document value in await GetMissingComponentsAsync(nodesRegistry.MissingComponents))
stylesPreprocessor.AddMissingComponent(value.document, value.styles);
}
async Task GetImageFillsAsync(int progress, List<IBaseNodeMixin> imageFills, CancellationToken token)
{
IBaseNodeMixin[] nodes = imageFills.Where(x => nodeMetadata.ShouldDownload(x, UxmlDownloadImages.ImageFills)).ToArray();
if (!nodes.Any())
return;
Data.Images filesImages = await GetAsync<Data.Images>($"files/{fileKey}/images", token);
IEnumerable<string> imageRefs = nodes.OfType<IGeometryMixin>().Select(y => y.fills.OfType<ImagePaint>().First().imageRef);
IEnumerable<KeyValuePair<string, string>> urls = filesImages.meta.images.Where(item => imageRefs.Contains(item.Key));
await urls.ForEachParallelAsync(maxConcurrentRequests, x => GetImageAsync(x.Key, x.Value, KnownFormats.png, progress, token), token);
}
async Task GetImageNodesAsync(int progress, IEnumerable<IBaseNodeMixin> targetNodes, UxmlDownloadImages downloadImages, string extension, CancellationToken token)
{
IBaseNodeMixin[] nodes = targetNodes.Where(x => nodeMetadata.ShouldDownload(x, downloadImages)).ToArray();
if (!nodes.Any())
return;
int i = 0;
IEnumerable<IGrouping<int, string>> groups = nodes.Select(x => x.id).GroupBy(_ => i++ / 100);
Task<Images>[] tasks = groups.Select(x => GetAsync<Images>($"images/{fileKey}?ids={string.Join(",", x)}&format={extension}", token)).ToArray();
await Task.WhenAll(tasks);
IEnumerable<KeyValuePair<string, string>> urls = tasks.SelectMany(x => x.Result.images);
await urls.ForEachParallelAsync(maxConcurrentRequests, x => GetImageAsync(x.Key, x.Value, extension, progress, token), token);
}
async Task GetImageAsync(string nodeID, string url, string extension, int progress, CancellationToken token)
{
if (string.IsNullOrEmpty(url))
{
Debug.LogWarning($"Node {nodeID} does not have any URL for image. Most likely, this node uses a mask.");
return;
}
bool fileExists = assetsInfo.GetAssetPath(nodeID, extension, out string assetPath);
if (assetsInfo.modifiedContent.Contains(assetsInfo.GetAbsolutePath(assetPath)))
return;
Progress.SetStepLabel(progress, url);
using HttpRequestMessage request = new(HttpMethod.Get, url);
if (fileExists && assetsInfo.cachedAssets.Map.TryGetValue(nodeID, out string etag))
request.Headers.Add("If-None-Match", $"\"{etag}\"");
HttpResponseMessage response = await httpClient.SendAsync(request, token);
bool isResolved = assetsInfo.cachedAssets.Map.ContainsValue(nodeID);
if (response.Headers.TryGetValues("ETag", out IEnumerable<string> values))
assetsInfo.cachedAssets[nodeID] = values.First().Trim('"');
assetsInfo.GetAssetPath(nodeID, extension, out assetPath);
string path = assetsInfo.GetAbsolutePath(assetPath);
if (assetsInfo.modifiedContent.Contains(path))
return;
assetsInfo.modifiedContent.Add(path);
if (response.IsSuccessStatusCode && !isResolved)
{
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
if (bytes.Length == 0)
{
Debug.LogWarning($"Response is empty for node={nodeID}, url={url}");
if (extension == KnownFormats.svg)
await File.WriteAllTextAsync(path, InvalidSvg, token);
else
await File.WriteAllBytesAsync(path, InvalidPng, token);
}
else
{
try
{
await File.WriteAllBytesAsync(path, bytes, token);
}
catch (Exception exception)
{
Debug.LogException(exception);
}
}
}
}
async Task WriteGradientsAsync(CancellationToken token)
{
foreach ((string key, GradientPaint gradient) in nodesRegistry.Gradients)
{
assetsInfo.GetAssetPath(key, KnownFormats.svg, out string relativePath);
string xmlPath = assetsInfo.GetAbsolutePath(relativePath);
using GradientWriter writer = new(xmlPath);
await writer.WriteAsync(gradient, token);
assetsInfo.modifiedContent.Add(xmlPath);
}
}
#endregion
}
}