|
4 | 4 | using System.IO; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Net; |
| 7 | +using System.Net.Http; |
7 | 8 | using System.Reflection; |
8 | 9 | using System.Threading; |
9 | 10 | using System.Threading.Tasks; |
|
12 | 13 | using Unity.VectorGraphics.Editor; |
13 | 14 | using UnityEditor; |
14 | 15 | using UnityEngine; |
15 | | -using UnityEngine.Networking; |
16 | 16 | using Object = UnityEngine.Object; |
17 | 17 |
|
18 | 18 | namespace Figma.Inspectors |
19 | 19 | { |
20 | 20 | using global; |
21 | | - using number = Double; |
22 | 21 |
|
23 | 22 | class FigmaUpdater : FigmaApi |
24 | 23 | { |
@@ -194,26 +193,81 @@ async Task SaveRemapsAsync() |
194 | 193 |
|
195 | 194 | async Task DownloadImagesAsync(int progress, CancellationToken token) |
196 | 195 | { |
197 | | - async Task GetImageAsync(string nodeID, string url, string extension) |
| 196 | + async Task WriteInvalidSvgAsync(string assetPath) |
198 | 197 | { |
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 | + } |
200 | 214 |
|
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); |
202 | 233 |
|
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}"); |
206 | 235 |
|
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); |
208 | 240 |
|
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('"'); |
211 | 243 |
|
212 | 244 | (bool _, string assetPath) = GetAssetPath(nodeID, extension); |
213 | 245 | string relativePath = Path.Combine(relativeFolder, assetPath).Replace('\\', '/'); |
214 | 246 |
|
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 | + } |
217 | 271 | } |
218 | 272 | async Task WriteGradientsAsync(int progress, CancellationToken token) |
219 | 273 | { |
|
0 commit comments