Skip to content

Commit 9910bb5

Browse files
committed
Minor stability updates for importing process
1 parent bd03d84 commit 9910bb5

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

Editor/Inspector/FigmaInspector.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,10 @@ static async Task UpdateTitleAsync(Figma figma, int progress, string title, stri
238238
string name = figma.name;
239239
FigmaUpdater updater = new(PersonalAccessToken, title, folder, relativeFolder, fontDirs);
240240
await updater.DownloadAssetsAsync(name, downloadImages, elements, figma.Filter, systemCopyBuffer, progress, token);
241-
updater.ImportTextures();
241+
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); // Force import all images
242242
updater.WriteUssUxml(name, progress);
243-
updater.ImportElements(name);
244-
updater.ImportFinal(name);
245243
updater.Cleanup(name);
244+
AssetDatabase.Refresh();
246245
}
247246
#endregion
248247
}

Editor/Inspector/FigmaUpdater.cs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,43 +133,13 @@ async Task DownloadImagesAsync()
133133
await DownloadMissingComponentsAsync();
134134
await DownloadImagesAsync();
135135
}
136-
internal void ImportTextures()
137-
{
138-
foreach (string path in Directory.GetFiles(Path.Combine(folder, elements), "*.png"))
139-
{
140-
string filename = Path.GetFileName(path);
141-
string relativePath = Path.Combine(relativeFolder, elements, filename);
142-
AssetDatabase.ImportAsset(relativePath);
143-
}
144-
foreach (string path in Directory.GetFiles(Path.Combine(folder, elements), "*.svg"))
145-
{
146-
string filename = Path.GetFileName(path);
147-
string relativePath = Path.Combine(relativeFolder, elements, filename);
148-
AssetDatabase.ImportAsset(relativePath);
149-
}
150-
}
151136
internal void WriteUssUxml(string name, int progress)
152137
{
153138
Progress.Report(progress, 5, 5, "Updating uss/uxml files");
154139

155140
parser.Run(GetAssetPath, GetAssetSize);
156141
parser.Write(folder, name, nodeMetadata.EnabledInHierarchy, nodeMetadata.GetTemplate, nodeMetadata.GetElementType);
157142
}
158-
internal void ImportElements(string name)
159-
{
160-
string uxmlContents = File.ReadAllText(Path.Combine(folder, $"{name}.uxml"));
161-
foreach (string path in Directory.GetFiles(Path.Combine(folder, elements), "*.uxml"))
162-
{
163-
string filename = Path.GetFileName(path);
164-
string relativePath = Path.Combine(relativeFolder, elements, filename);
165-
if (uxmlContents.Contains(filename)) AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceUpdate);
166-
}
167-
}
168-
internal void ImportFinal(string name)
169-
{
170-
if (File.Exists(Path.GetFileName(Path.Combine(folder, $"{name}.uxml")))) AssetDatabase.ImportAsset(Path.Combine(relativeFolder, $"{name}.uxml"), ImportAssetOptions.ForceUpdate);
171-
if (File.Exists(Path.GetFileName(Path.Combine(folder, $"{name}.uss")))) AssetDatabase.ImportAsset(Path.Combine(relativeFolder, $"{name}.uss"), ImportAssetOptions.ForceUpdate);
172-
}
173143
internal void Cleanup(string name)
174144
{
175145
string uxmlContents = File.ReadAllText(Path.Combine(folder, $"{name}.uxml"));
@@ -224,18 +194,49 @@ async Task SaveRemapsAsync()
224194

225195
async Task DownloadImagesAsync(int progress, CancellationToken token)
226196
{
227-
HttpClient client = new();
228-
foreach (KeyValuePair<string, string> header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value);
197+
async Task WriteInvalidSvgAsync(string assetPath)
198+
{
199+
XmlWriter writer = XmlWriter.Create(Path.Combine(folder, assetPath), new XmlWriterSettings
200+
{
201+
Indent = true,
202+
NewLineOnAttributes = true,
203+
IndentChars = " ",
204+
Async = true
205+
});
206+
writer.WriteStartElement("svg");
207+
{
208+
writer.WriteStartElement("rect");
209+
writer.WriteAttributeString("width", "100");
210+
writer.WriteAttributeString("height", "100");
211+
writer.WriteAttributeString("fill", "magenta");
212+
await writer.WriteEndElementAsync();
213+
await Task.Delay(0, token);
214+
}
229215

216+
await writer.WriteEndElementAsync();
217+
await Task.Delay(0, token);
218+
219+
writer.Close();
220+
}
221+
async Task WriteInvalidPngAsync(string assetPath)
222+
{
223+
Texture2D magenta = new(2, 2);
224+
magenta.SetPixel(0, 0, Color.magenta);
225+
magenta.SetPixel(1, 0, Color.magenta);
226+
magenta.SetPixel(0, 1, Color.magenta);
227+
magenta.SetPixel(1, 1, Color.magenta);
228+
magenta.Apply();
229+
await File.WriteAllBytesAsync(Path.Combine(folder, assetPath), magenta.EncodeToPNG(), token);
230+
}
230231
async Task GetImageAsync(string nodeID, string url, string extension)
231232
{
232-
(bool fileExists, string _) = GetAssetPath(nodeID, extension);
233+
(bool fileExists, string test) = GetAssetPath(nodeID, extension);
233234

234235
Progress.SetStepLabel(progress, $"{url}");
235236

237+
HttpClient client = new();
238+
foreach (KeyValuePair<string, string> header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value);
236239
if (fileExists && remaps.TryGetValue(nodeID, out string etag)) client.DefaultRequestHeaders.Add("If-None-Match", $"\"{etag}\"");
237-
else client.DefaultRequestHeaders.Remove("If-None-Match");
238-
239240
HttpResponseMessage response = await client.GetAsync(url, token);
240241

241242
if (response.Headers.TryGetValues("ETag", out IEnumerable<string> values))
@@ -245,7 +246,29 @@ async Task GetImageAsync(string nodeID, string url, string extension)
245246
string relativePath = Path.Combine(relativeFolder, assetPath).Replace('\\', '/');
246247

247248
if (response.StatusCode == HttpStatusCode.OK)
248-
await File.WriteAllBytesAsync(relativePath, await response.Content.ReadAsByteArrayAsync(), token);
249+
{
250+
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
251+
switch (bytes.Length)
252+
{
253+
case 0:
254+
Debug.LogWarning($"Response is empty for node={nodeID}, url={url}");
255+
switch (extension)
256+
{
257+
case "svg":
258+
await WriteInvalidSvgAsync(assetPath);
259+
break;
260+
261+
default:
262+
await WriteInvalidPngAsync(assetPath);
263+
break;
264+
}
265+
break;
266+
267+
default:
268+
await File.WriteAllBytesAsync(relativePath, bytes, token);
269+
break;
270+
}
271+
}
249272
}
250273
async Task WriteGradientsAsync(int progress, CancellationToken token)
251274
{

0 commit comments

Comments
 (0)