private static async Task GenerateAssets(string[] files, DeviceType mode, string destinationDirectory, int quality, string postfix) { // It fails to create the first file var firstItem = files[0]; var initalLoad = new SKSvg(); initalLoad.Load(firstItem); await PngHelper.GeneratePng((int)initalLoad.CanvasSize.Width, (int)initalLoad.CanvasSize.Height, firstItem, "temp", 1); File.Delete("temp"); foreach (var filepath in files.OrderBy(s => s).ToList()) { Console.WriteLine($"Creating assets from {filepath}"); var filename = Path.GetFileNameWithoutExtension(filepath); if (mode == DeviceType.iOS) { var generator = new IOSAssetGenerator(); await generator.CreateAsset(filepath, filename, destinationDirectory, quality, string.Empty); } else { var generator = new AndroidAssetGenerator(); await generator.CreateAsset(filepath, filename, destinationDirectory, quality, postfix); } } }
public async Task CreateAsset(string filepath, string filename, string destinationDirectory, int quality, string postfix = default) { for (var i = 1; i < 4; i++) { try { var svg = new SKSvg(); try { svg.Load(filepath); } catch (Exception e) { Console.WriteLine($"Unexpected error when parsing asset: {filepath}"); Console.WriteLine("Error: " + e.Message); Console.WriteLine("Exiting with error 1"); Environment.Exit(1); } string newFilename; if (i == 1) { newFilename = filename + ".png"; } else { newFilename = filename + $"@{i}x.png"; } var width = (int)(svg.CanvasSize.Width * i); var height = (int)(svg.CanvasSize.Height * i); await PngHelper.GeneratePng(width, height, filepath, Path.Combine(destinationDirectory, newFilename), quality); Console.WriteLine($"Successfully created asset: {Path.GetFileName(newFilename)}"); } catch (Exception e) { Console.WriteLine($"Failed to generate asset: {filename}"); Console.WriteLine("Error: " + e.Message); Console.WriteLine("Exiting with error 1"); Environment.Exit(1); } } }
public async Task CreateAsset(string filepath, string filename, string destinationDirectory, int quality, string postfix) { var resourceTypes = new Dictionary <AndroidResourceType, float> { // Scale factors { AndroidResourceType.LDPI, 0.75f }, { AndroidResourceType.MDPI, 1 }, { AndroidResourceType.HDPI, 1.5f }, { AndroidResourceType.XHDPI, 2f }, { AndroidResourceType.XXHDPI, 3f }, { AndroidResourceType.XXXHDPI, 4f } }; foreach (var resourceType in resourceTypes) { try { var name = Enum.GetName(typeof(AndroidResourceType), resourceType.Key); var resourceDirectoryName = string.IsNullOrEmpty(postfix) ? $"drawable-{name.ToLowerInvariant()}" : $"drawable-{name.ToLowerInvariant()}-{postfix}"; var svg = new SKSvg(); try { svg.Load(filepath); } catch (Exception e) { Console.WriteLine($"Unexpected error when parsing asset: {filepath}"); Console.WriteLine("Error: " + e.Message); Console.WriteLine("Exiting with error 1"); Environment.Exit(1); } var width = (int)(svg.CanvasSize.Width * resourceType.Value); var height = (int)(svg.CanvasSize.Height * resourceType.Value); // Cheap clamp if (width < 1) { width = 1; } if (height < 1) { height = 1; } var filenameWithExtension = $"{filename}.png"; var resourceDir = Path.Combine(destinationDirectory, resourceDirectoryName); if (!Directory.Exists(resourceDir)) { Directory.CreateDirectory(resourceDir); } var finalPath = Path.Combine(resourceDir, filenameWithExtension); await PngHelper.GeneratePng(width, height, filepath, finalPath, quality); Console.WriteLine($"Successfully created asset: {finalPath}"); } catch (Exception e) { Console.WriteLine($"Failed to generate asset: {filename}"); Console.WriteLine("Error: " + e.Message); Console.WriteLine("Exiting with error 1"); Environment.Exit(1); } } }