Skip to content

Commit 06c9af9

Browse files
Using HttpClient instead of UnityWebRequest
1 parent b464293 commit 06c9af9

File tree

1 file changed

+67
-13
lines changed

1 file changed

+67
-13
lines changed

Editor/Inspector/FigmaUpdater.cs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Net;
7+
using System.Net.Http;
78
using System.Reflection;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -12,13 +13,11 @@
1213
using Unity.VectorGraphics.Editor;
1314
using UnityEditor;
1415
using UnityEngine;
15-
using UnityEngine.Networking;
1616
using Object = UnityEngine.Object;
1717

1818
namespace Figma.Inspectors
1919
{
2020
using global;
21-
using number = Double;
2221

2322
class FigmaUpdater : FigmaApi
2423
{
@@ -194,26 +193,81 @@ async Task SaveRemapsAsync()
194193

195194
async Task DownloadImagesAsync(int progress, CancellationToken token)
196195
{
197-
async Task GetImageAsync(string nodeID, string url, string extension)
196+
async Task WriteInvalidSvgAsync(string assetPath)
198197
{
199-
(bool fileExists, string _) = GetAssetPath(nodeID, extension);
198+
XmlWriter writer = XmlWriter.Create(Path.Combine(folder, assetPath), new XmlWriterSettings
199+
{
200+
Indent = true,
201+
NewLineOnAttributes = true,
202+
IndentChars = " ",
203+
Async = true
204+
});
205+
writer.WriteStartElement("svg");
206+
{
207+
writer.WriteStartElement("rect");
208+
writer.WriteAttributeString("width", "100");
209+
writer.WriteAttributeString("height", "100");
210+
writer.WriteAttributeString("fill", "magenta");
211+
await writer.WriteEndElementAsync();
212+
await Task.Delay(0, token);
213+
}
200214

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

203-
Dictionary<string, string> responseHeaders = new();
204-
Dictionary<string, string> requestHeaders = headers.ToDictionary(header => header.Key, header => header.Value);
205-
if (fileExists && remaps.TryGetValue(nodeID, out string etag)) requestHeaders.Add("If-None-Match", $"\"{etag}\"");
234+
Progress.SetStepLabel(progress, $"{url}");
206235

207-
UnityWebRequest request = await url.HttpGetRequestAsync(requestHeaders, responseHeaders, cancellationToken: token);
236+
HttpClient client = new();
237+
foreach (KeyValuePair<string, string> header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value);
238+
if (fileExists && remaps.TryGetValue(nodeID, out string etag)) client.DefaultRequestHeaders.Add("If-None-Match", $"\"{etag}\"");
239+
HttpResponseMessage response = await client.GetAsync(url, token);
208240

209-
if (responseHeaders.TryGetValue("ETag", out etag))
210-
remaps[nodeID] = etag.Trim('"');
241+
if (response.Headers.TryGetValues("ETag", out IEnumerable<string> values))
242+
remaps[nodeID] = values.First().Trim('"');
211243

212244
(bool _, string assetPath) = GetAssetPath(nodeID, extension);
213245
string relativePath = Path.Combine(relativeFolder, assetPath).Replace('\\', '/');
214246

215-
if (request.result == UnityWebRequest.Result.Success && request.responseCode == (long)HttpStatusCode.OK &&
216-
request.downloadHandler.data is { Length: > 0 } data) await File.WriteAllBytesAsync(relativePath, data, token);
247+
if (response.StatusCode == HttpStatusCode.OK)
248+
{
249+
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
250+
switch (bytes.Length)
251+
{
252+
case 0:
253+
Debug.LogWarning($"Response is empty for node={nodeID}, url={url}");
254+
switch (extension)
255+
{
256+
case "svg":
257+
await WriteInvalidSvgAsync(assetPath);
258+
break;
259+
260+
default:
261+
await WriteInvalidPngAsync(assetPath);
262+
break;
263+
}
264+
break;
265+
266+
default:
267+
await File.WriteAllBytesAsync(relativePath, bytes, token);
268+
break;
269+
}
270+
}
217271
}
218272
async Task WriteGradientsAsync(int progress, CancellationToken token)
219273
{

0 commit comments

Comments
 (0)