private bool resize(ddsfileinfo ddsinfo, string file, FileInfo fileinf) { int size = 0; if (ddsinfo.height > ddsinfo.width) { size = ddsinfo.height; } else { size = ddsinfo.width; } int ifgreater = 8192; if (texturesize.Contains("256")) { ifgreater = 256; } else if (texturesize.Contains("512")) { ifgreater = 512; } else if (texturesize.Contains("1024")) { ifgreater = 1024; } else if (texturesize.Contains("2048")) { ifgreater = 2048; } else if (texturesize.Contains("4096")) { ifgreater = 4096; } else if (texturesize.Contains("All")) { ifgreater = 4; } if (size > ifgreater) { texconv("\"" + file + "\" -y -o \"" + fileinf.DirectoryName + "\" -w " + ddsinfo.width / 2 + " -h " + ddsinfo.height / 2 + " -m 1"); //gen mipmaps texconv("\"" + file + "\" -y -o \"" + fileinf.DirectoryName + "\"" + " -m 0"); return(true); } else { return(false); } }
private ddsfileinfo checkdds(string file) { Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); //startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.FileName = Application.StartupPath + "\\bin\\texdiag.exe"; startInfo.Arguments = "info \"" + file + "\""; process.StartInfo = startInfo; process.Start(); ddsfileinfo ddsinfo = new ddsfileinfo(); while (!process.StandardOutput.EndOfStream) { string line = process.StandardOutput.ReadLine(); if (line.Contains("height")) { //workaround for rare int.parse errors int temp; if (int.TryParse(line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2), out temp)) { ddsinfo.height = temp; } else { try { ddsinfo.height = int.Parse(line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2), CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show("Couldn't parse dds height. Save current message and next ones for providing more info about error. Error log: " + ex.ToString()); MessageBox.Show("Height line unedited: " + line); MessageBox.Show("Height line edited: " + line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2)); } } } if (line.Contains("width")) { //workaround for rare int.parse errors int temp; if (int.TryParse(line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2), out temp)) { ddsinfo.width = temp; } else { try { ddsinfo.width = int.Parse(line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2), CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show("Couldn't parse dds width. Save current message and next ones for providing more info about error. Error log: " + ex.ToString()); MessageBox.Show("Width line unedited: " + line); MessageBox.Show("Width line edited: " + line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2)); } } } if (line.Contains("format")) { ddsinfo.format = line.Substring(line.IndexOf("=") + 2, line.Length - line.IndexOf("=") - 2); } if (line.Contains("FAILED")) { ddsinfo.format = "Unsupported format"; ddsinfo.width = 0; ddsinfo.height = 0; } } //process.WaitForExit(); // ddsinfo.alpha = false; if (!Directory.Exists(Application.StartupPath + "\\temp")) { Directory.CreateDirectory(Application.StartupPath + "\\temp"); } FileInfo fileinf = new FileInfo(file); if (ddsinfo.format.Contains("BC3")) { File.Copy(file, Application.StartupPath + "\\temp\\" + fileinf.Name, true); } else { if (ddsinfo.format.Contains("SRGB")) { texconv("\"" + file + "\" -y -f BC3_UNORM_SRGB -o \"" + Application.StartupPath + "\\temp" + "\""); } else { texconv("\"" + file + "\" -y -f BC3_UNORM -o \"" + Application.StartupPath + "\\temp" + "\""); } } startInfo.Arguments = "analyze \"" + Application.StartupPath + "\\temp\\" + fileinf.Name + "\""; process.StartInfo = startInfo; process.Start(); bool finished6 = false; bool finished8 = false; int blocks6 = 0; int blocks8 = 0; while (!process.StandardOutput.EndOfStream && !(finished6 && finished8)) { string line = process.StandardOutput.ReadLine(); if (line.Contains("8 alpha blocks") && !finished8) { finished8 = true; //workaround for possible int.parse errors int temp; if (int.TryParse(line.Replace("8 alpha blocks - ", ""), out temp)) { blocks8 = temp; } else { try { blocks8 = int.Parse(line.Replace("8 alpha blocks - ", ""), CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show("Couldn't parse dds alpha8 channel info. Save current message and next ones for providing more info about error. Error log: " + ex.ToString()); MessageBox.Show("Unedited line: " + line); MessageBox.Show("Edited line: " + line.Replace("8 alpha blocks - ", "")); } } } if (line.Contains("6 alpha blocks") && !finished6) { finished6 = true; //workaround for possible int.parse errors int temp; if (int.TryParse(line.Replace("6 alpha blocks - ", ""), out temp)) { blocks6 = temp; } else { try { blocks6 = int.Parse(line.Replace("6 alpha blocks - ", ""), CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show("Couldn't parse dds alpha6 channel info. Save current message and next ones for providing more info about error. Error log: " + ex.ToString()); MessageBox.Show("Unedited line: " + line); MessageBox.Show("Edited line: " + line.Replace("8 alpha blocks - ", "")); } } } //process.WaitForExit(); if (blocks6 > 0 && blocks8 > 0) { ddsinfo.alpha = true; } if (File.Exists(Application.StartupPath + "\\temp\\" + fileinf.Name + "\"")) { File.Delete(Application.StartupPath + "\\temp\\" + fileinf.Name + "\""); } } return(ddsinfo); }
private string compress(ddsfileinfo ddsinfo, string file, FileInfo fileinf) { string newformat = "Already compressed"; int ddslvl = 10; if (ddsinfo.format.Contains("BC1")) { ddslvl = 1; } else if (ddsinfo.format.Contains("BC2")) { ddslvl = 3; } else if (ddsinfo.format.Contains("BC3")) { ddslvl = 3; } else if (ddsinfo.format.Contains("BC4")) { ddslvl = 5; } else if (ddsinfo.format.Contains("BC5")) { ddslvl = 5; } else if (ddsinfo.format.Contains("BC7")) { ddslvl = 7; } if (ddslvl == 1) { return(newformat); } if (ddsinfo.alpha == true) { if (ddslvl > compressalphalvl) { if (ddsinfo.format.Contains("SRGB")) { if (compresswithalpha == "BC5") { texconv("\"" + file + "\" -y -f BC3_UNORM_SRGB -o \"" + fileinf.DirectoryName + "\" -w " + ddsinfo.width / 2 + " -h " + ddsinfo.height / 2 + " -m 1"); newformat = "BC3_UNORM_SRGB"; } else { texconv("\"" + file + "\" -y -f " + compresswithalpha + "_UNORM_SRGB -o \"" + fileinf.DirectoryName + "\""); newformat = compresswithalpha + "_UNORM_SRGB"; } } else { texconv("\"" + file + "\" -y -f " + compresswithalpha + "_UNORM -o \"" + fileinf.DirectoryName + "\""); newformat = compresswithalpha + "_UNORM"; } } } else { if (ddslvl > compresslvl) { if (ddsinfo.format.Contains("SRGB")) { if (compressnoalpha == "BC5") { texconv("\"" + file + "\" -y -f BC3_UNORM_SRGB -o \"" + fileinf.DirectoryName + "\" -w " + ddsinfo.width / 2 + " -h " + ddsinfo.height / 2 + " -m 1"); newformat = "BC3_UNORM_SRGB"; } else { texconv("\"" + file + "\" -y -f " + compressnoalpha + "_UNORM_SRGB -o \"" + fileinf.DirectoryName + "\""); newformat = compressnoalpha + "_UNORM_SRGB"; } } else { texconv("\"" + file + "\" -y -f " + compressnoalpha + "_UNORM -o \"" + fileinf.DirectoryName + "\""); newformat = compressnoalpha + "_UNORM"; } } } return(newformat); }
private void compressmaster(string file, bool ignoresn, bool ignoreface, bool ignorediffuse, int num, bool isthreaded) { List <string> log = new List <string>(); FileInfo fileinf = new FileInfo(file); log.Add(num + ": " + file); if (ignoresn && (fileinf.Name.Contains("_s.dds") || fileinf.Name.Contains("_n.dds") || fileinf.Name.Contains("_g.dds") || fileinf.Name.Contains("_S.dds") || fileinf.Name.Contains("_N.dds") || fileinf.Name.Contains("_G.dds"))) { log.Add("Specular, normal and glowmaps are ignored, skipping"); } else if (ignoreface && (fileinf.Name.Contains("femalehead") || fileinf.Name.Contains("malehead"))) { log.Add("Face textures are ignored, skipping"); } else if (ignorediffuse && !(fileinf.Name.Contains("_s.dds") || fileinf.Name.Contains("_n.dds") || fileinf.Name.Contains("_g.dds") || fileinf.Name.Contains("_S.dds") || fileinf.Name.Contains("_N.dds") || fileinf.Name.Contains("_G.dds"))) { log.Add("Diffuse textures are ignored, skipping"); } else { ddsfileinfo ddsinfo = checkdds(file); log.Add("height = " + ddsinfo.height); log.Add("width = " + ddsinfo.width); log.Add("format = " + ddsinfo.format); log.Add("alpha = " + ddsinfo.alpha.ToString()); if (ddsinfo.format.Contains("Unsupported format")) { log.Add("Unsupported format, skipping"); } else { double filesize = Math.Round((Double) new FileInfo(file).Length / 1024, 1); originalfilessize += filesize; log.Add("file size = " + filesize + " kb"); //compress if (compress_check.Checked) { string compressed = compress(ddsinfo, file, fileinf); if (!compressed.Contains("Already compressed")) { log.Add("new format = " + compressed); } } //resize if (resize_check.Checked) { bool resized = resize(ddsinfo, file, fileinf); if (resized) { log.Add("new width = " + (ddsinfo.width / 2)); log.Add("new height = " + (ddsinfo.height / 2)); } } // double newfilesize = Math.Round((Double) new FileInfo(file).Length / 1024, 1); compressedfilessize += newfilesize; if (newfilesize < filesize) { log.Add("new file size = " + newfilesize + " kb"); } } } if (!isthreaded) { foreach (string entry in log) { listBox1.Items.Add(entry); } } else { foreach (string entry in log) { logqueue.Enqueue(entry); } if (isthreaded) { currentthreads--; } } }