static void Operate(string root, string backup, string operation) { var backupDI = Directory.CreateDirectory(Path.Combine(backup, DateTime.Now.ToBinary() + @"\")); using (var recovery = new Recovery(backupDI.FullName)) { var scanner = new Scanner(root, recovery); foreach (var kvp in scanner.RemovePattern("thumbs.db", "pspbrwse.jbf", "*.tmp")) { Console.WriteLine("Removing flotsam " + kvp.Key); kvp.Value(); } using (var org = new Organizer(scanner.ScanPattern("*.dds"), recovery)) { org.Completed.Wait(); } Console.WriteLine("DXT Processing complete. Press any key to proceed to duplicate removal."); Console.ReadKey(); foreach (var kvp in scanner.RemoveDuplicates("*.dds")) { Console.WriteLine("Removing duplicates of " + kvp.Key); kvp.Value(); } } Console.WriteLine("Good."); }
//backup cannot be a subdirectory of rootpath public Scanner(string root, Recovery recovery) { if (!Directory.Exists(root)) throw new ArgumentException("Root directory does not exist", "root"); _recovery = recovery; _rootPath = root; }
public async Task<JobResult> Process(Job job, Recovery recovery) { _count++; var result = /* _process(file, recovery); */ await Task.Factory.StartNew<JobResult>(() => _process(job, recovery)); Trace.TraceInformation("Count hit: " + --_count); return result; }
//bmp > tga > png > dds //public byte[] ReadFile(string file) //{ // try // { // byte[] fBytes = File.ReadAllBytes(file); // if (fBytes.Length == 0) // { // switch (Path.GetExtension(file).ToLowerInvariant()) // { // case "bmp": // return ReadFile(Path.ChangeExtension(file, "tga")); // case "tga": // return ReadFile(Path.ChangeExtension(file, "png")); // case "png": // return ReadFile(Path.ChangeExtension(file, "dds")); // //case "dds": // //default: // } // } // } // catch (IOException e) // { // Trace.TraceError(e.ToString()); // } // return null; //} private JobResult _process(Job job, Recovery recovery, bool asMappable = true) { string file = job.Key; byte[] fBytes = job.Value; int fLen = fBytes.Length; Result code = Result.NoAction; byte[] output = null; using (var ms = new MemoryStream(fBytes)) try { var txFlags = asMappable ? SharpDX.Toolkit.Graphics.TextureFlags.RenderTarget : SharpDX.Toolkit.Graphics.TextureFlags.ShaderResource; bool cube = file.Contains("\\cubemaps\\"); Texture tex; if (cube) tex = SharpDX.Toolkit.Graphics.TextureCube.Load(_device, ms, txFlags, ResourceUsage.Default); else tex = SharpDX.Toolkit.Graphics.Texture2D.Load(_device, ms, txFlags, ResourceUsage.Default); using (tex) { if (tex.Description.BindFlags.HasFlag(BindFlags.RenderTarget | BindFlags.ShaderResource) && tex.Description.MipLevels > 1) { Trace.TraceInformation("Generating mipmaps..."); tex.GenerateMipMaps(); code |= Result.GeneratedMipmaps; } if (!tex.IsBlockCompressed) { Console.WriteLine(tex.Description.Format); //var loadOpts = new ImageLoadInformation() //{ // //Format = SharpDX.DXGI.Format.bc3, //}; var desc = tex.Description; var loadOpts = new ImageLoadInformation { BindFlags = desc.BindFlags, CpuAccessFlags = desc.CpuAccessFlags, Depth = desc.Depth, Filter = FilterFlags.None, //FirstMipLevel = desc.MipLevels,// 0, Format = desc.Format,//SharpDX.DXGI.Format.BC3_UNorm_SRgb, Height = tex.Height, Width = tex.Width, MipFilter = FilterFlags.Box, MipLevels = desc.MipLevels, OptionFlags = desc.OptionFlags, //PSrcInfo = new IntPtr(&imginf), Usage = desc.Usage, }; ms.Seek(0, SeekOrigin.Begin); System.Diagnostics.Trace.Assert(tex.GetType() == typeof(SharpDX.Toolkit.Graphics.Texture2D)); System.Diagnostics.Trace.Assert(!cube); //using (var newTex = SharpDX.Direct3D11.Texture2D.FromStream(_device, ms, fLen))//, loadOpts)) using (var newTex = SharpDX.Toolkit.Graphics.Texture2D.Load(_device, ms, txFlags, ResourceUsage.Default))//(_device, ms, fLen))//, loadOpts)) { using (var msNew = new MemoryStream()) { SharpDX.Direct3D11.Texture2D.ToStream(_device, newTex, ImageFileFormat.Dds, msNew); File.Copy(file, recovery.GetBackupPath(Path.GetFileNameWithoutExtension(file) + "_orig.dds")); //recovery.Backup(file); output = msNew.ToArray(); } Trace.TraceInformation(Path.GetFileNameWithoutExtension(file) + " converted to BC3"); code |= Result.CompressedBC3; } } } } catch (SharpDXException sdx) { if (asMappable) return _process(job, recovery, false); Trace.TraceError(sdx.ToString()); code = Result.Failed; } catch (NullReferenceException nre) { Trace.TraceError(nre.ToString()); code = Result.Failed; } return new JobResult(recovery.GetBackupPath(file), code, output); }