public static void OptimizePNG() { // f for format file size Func <ulong, string> f = (ulong size) => { double dsize = size; string[] suffixes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; while (size >= 1024 && order < suffixes.Length - 1) { order++; size /= 1024; dsize /= 1024.0; } return($"{dsize:0.##} {suffixes[order]}"); }; ulong totalLengthAfter = 0; ulong totalLengthBefore = 0; using (new DirectoryChanger(The.Workspace.AssetsDirectory)) { foreach (var fi in Lime.AssetBundle.Current.EnumerateFileInfos(null, ".png")) { ulong lengthBefore = (ulong)(new System.IO.FileInfo(fi.Path)).Length; totalLengthBefore += lengthBefore; TextureConverter.OptimizePNG(fi.Path); ulong lengthAfter = (ulong)(new System.IO.FileInfo(fi.Path)).Length; totalLengthAfter += lengthAfter; Console.WriteLine($"{fi.Path} : {f(lengthBefore)} => {f(lengthAfter)}, diff: {f(lengthBefore - lengthAfter)}"); } } Console.WriteLine($"Totals: {f(totalLengthBefore)} => {f(totalLengthAfter)}, diff: {f(totalLengthBefore - totalLengthAfter)}"); }
public static void ImportTexture(string path, Bitmap texture, ICookingRules rules, byte[] CookingRulesSHA1) { var textureParamsPath = Path.ChangeExtension(path, ".texture"); if (!AreTextureParamsDefault(rules)) { UpscaleTextureIfNeeded(ref texture, rules, false); var textureParams = new TextureParams { WrapMode = rules.WrapMode, MinFilter = rules.MinFilter, MagFilter = rules.MagFilter, }; Serialization.WriteObjectToBundle(AssetBundle, textureParamsPath, textureParams, Serialization.Format.Binary, ".texture", AssetAttributes.None, null); } else { if (AssetBundle.FileExists(textureParamsPath)) { DeleteFileFromBundle(textureParamsPath); } } if (ShouldGenerateOpacityMasks()) { var maskPath = Path.ChangeExtension(path, ".mask"); OpacityMaskCreator.CreateMask(AssetBundle, texture, maskPath); } var attributes = AssetAttributes.ZippedDeflate; if (!TextureConverterUtils.IsPowerOf2(texture.Width) || !TextureConverterUtils.IsPowerOf2(texture.Height)) { attributes |= AssetAttributes.NonPowerOf2Texture; } switch (Platform) { case TargetPlatform.Android: var f = rules.PVRFormat; if (f == PVRFormat.ARGB8 || f == PVRFormat.RGB565 || f == PVRFormat.RGBA4) { TextureConverter.RunPVRTexTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, rules.PVRFormat, CookingRulesSHA1); } else { TextureConverter.RunEtcTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, CookingRulesSHA1); } break; case TargetPlatform.iOS: TextureConverter.RunPVRTexTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, rules.PVRFormat, CookingRulesSHA1); break; case TargetPlatform.Win: case TargetPlatform.Mac: TextureConverter.RunNVCompress(texture, AssetBundle, path, attributes, rules.DDSFormat, rules.MipMaps, CookingRulesSHA1); break; default: throw new Lime.Exception(); } }
public void ImportTexture(string path, Bitmap texture, ICookingRules rules, DateTime time, byte[] CookingRulesSHA1) { var textureParamsPath = Path.ChangeExtension(path, ".texture"); var textureParams = new TextureParams { WrapMode = rules.WrapMode, MinFilter = rules.MinFilter, MagFilter = rules.MagFilter, }; if (!AreTextureParamsDefault(rules)) { TextureTools.UpscaleTextureIfNeeded(ref texture, rules, false); var isNeedToRewriteTexParams = true; if (AssetBundle.FileExists(textureParamsPath)) { var oldTexParams = InternalPersistence.Instance.ReadObject <TextureParams>(textureParamsPath, AssetBundle.OpenFile(textureParamsPath)); isNeedToRewriteTexParams = !oldTexParams.Equals(textureParams); } if (isNeedToRewriteTexParams) { InternalPersistence.Instance.WriteObjectToBundle(AssetBundle, textureParamsPath, textureParams, Persistence.Format.Binary, ".texture", File.GetLastWriteTime(textureParamsPath), AssetAttributes.None, null); } } else { if (AssetBundle.FileExists(textureParamsPath)) { DeleteFileFromBundle(textureParamsPath); } } if (rules.GenerateOpacityMask) { var maskPath = Path.ChangeExtension(path, ".mask"); OpacityMaskCreator.CreateMask(AssetBundle, texture, maskPath); } var attributes = AssetAttributes.ZippedDeflate; if (!TextureConverterUtils.IsPowerOf2(texture.Width) || !TextureConverterUtils.IsPowerOf2(texture.Height)) { attributes |= AssetAttributes.NonPowerOf2Texture; } switch (Target.Platform) { case TargetPlatform.Android: //case TargetPlatform.iOS: var f = rules.PVRFormat; if (f == PVRFormat.ARGB8 || f == PVRFormat.RGB565 || f == PVRFormat.RGBA4) { TextureConverter.RunPVRTexTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, rules.PVRFormat, CookingRulesSHA1, time); } else { TextureConverter.RunEtcTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, CookingRulesSHA1, time); } break; case TargetPlatform.iOS: TextureConverter.RunPVRTexTool(texture, AssetBundle, path, attributes, rules.MipMaps, rules.HighQualityCompression, rules.PVRFormat, CookingRulesSHA1, time); break; case TargetPlatform.Win: case TargetPlatform.Mac: TextureConverter.RunNVCompress(texture, AssetBundle, path, attributes, rules.DDSFormat, rules.MipMaps, CookingRulesSHA1, time); break; default: throw new Lime.Exception(); } }