private async void HandleActionButtonClick(object sender, EventArgs e) { try { Window.ActionEnabled = false; Window.PathEnabled = false; Window.StatusText = "Converting..."; string pathCopy = Window.Path; PbrImageFormat formatCopy = Window.ImageFormat; bool removeUniformCopy = Window.RemoveUniform; await Task.Run(() => Convert(pathCopy, formatCopy, removeUniformCopy)); Window.StatusText = "Successfully converted resource pack!"; Window.ActionEnabled = true; Window.PathEnabled = true; } catch (Exception ex) { MessageWindow.Caption = "Error during conversion."; MessageWindow.Message = ex.Message; MessageWindow.ShowDialog(); Window.StatusText = "Failed to convert."; Window.ActionEnabled = true; Window.PathEnabled = true; } }
private void Convert(string path, PbrImageFormat format, bool removeSolids) { List <string> paths = ResourcePack.GetColorFiles($"{path}\\textures\\blocks"); Parallel.ForEach(paths, (colorfile, loopState) => { try { string colorFileDir = Path.GetDirectoryName(colorfile) + Path.DirectorySeparatorChar; string colorfilename = Path.GetFileNameWithoutExtension(colorfile); string merfile = ResourcePack.GetPbrFile(colorfile, PbrType.MER); string normalfile = ResourcePack.GetPbrFile(colorfile, PbrType.Normal); string heightmapfile = ResourcePack.GetPbrFile(colorfile, PbrType.Heightmap); Texture colorTexture = new Texture(colorfile); Texture merTexture = merfile != null ? new Texture(merfile) : new Texture(); Texture normalTexture = normalfile != null ? new Texture(normalfile) : new Texture(); Texture heightmapTexture = heightmapfile != null ? new Texture(heightmapfile) : new Texture(); Dictionary <Texture, object> textures = new Dictionary <Texture, object> { { colorTexture, colorfile }, { merTexture, merfile }, { normalTexture, normalfile }, { heightmapTexture, heightmapfile } }; List <Texture> texCopy = new List <Texture>(textures.Keys); if (removeSolids) { Parallel.ForEach(texCopy, (texture) => { if (textures[texture] as string != normalfile && textures[texture] as string != heightmapfile && texture.Bitmap != null && texture.Uniform) { File.Delete(textures[texture] as string); textures[texture] = textures[texture] as string == colorfile ? texture.FullColorArray : texture.SolidColorArray; } }); } if (format != null) { Parallel.ForEach(texCopy, (texture) => { if (texture.Bitmap != null && textures[texture] is string) { if (File.Exists(textures[texture] as string)) { File.Delete(textures[texture] as string); } string newFile = Path.GetDirectoryName( textures[texture] as string) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension( textures[texture] as string) + "." + format.Value; texture.Save(newFile, format); textures[texture] = newFile; } }); } Parallel.ForEach(texCopy, (texture) => { if (textures[texture] != null) { if (textures[texture] is string) { textures[texture] = Path.GetFileName(textures[texture] as string); } } }); using FileStream fs = File.Create($"{Path.GetDirectoryName(colorfile)}\\" + $"{colorfilename}.texture_set.json"); byte[] content = new UTF8Encoding(true).GetBytes( JsonConvert.SerializeObject(new TextureSetModel { FormatVersion = Configuration.AppData.TextureSetVersion, MinecraftTextureSet = new TextureSetModel.TextureSetInfo { Color = textures[colorTexture], MER = textures[merTexture], Normal = textures[normalTexture] as string, Heightmap = textures[heightmapTexture] as string } }, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); fs.Write(content); } catch (AggregateException e) { loopState.Stop(); throw new FormatException("Error occurred while processing: " + $"'{Path.GetFileName(colorfile)}'\n{e.InnerExceptions[0].Message}"); } }); }