ME3Explorer.KFreonTPFTools3.AutofixInternal C# (CSharp) Method

AutofixInternal() private method

private AutofixInternal ( TPFTexInfo tex ) : bool
tex KFreonLib.Textures.TPFTexInfo
return bool
        private bool AutofixInternal(TPFTexInfo tex)
        {
            bool retval = false;

            TPFTexInfo backup = tex.Clone();

            string path = tex.Autofixedpath(TemporaryPath);
            Directory.CreateDirectory(Path.GetDirectoryName(path));

            byte[] imgData = tex.Extract(Path.GetDirectoryName(path), true);

            using (ImageEngineImage img = new ImageEngineImage(imgData))
            {
                var destFormat = tex.ExpectedFormat;
                img.Resize(UsefulThings.General.RoundToNearestPowerOfTwo(img.Width), false);
                retval = img.Save(path, destFormat, tex.ExpectedMips > 1 ? MipHandling.Default : MipHandling.KeepTopOnly);
            }

            if (!retval)
            {
                tex.AutofixSuccess = false;
                return false;
            }

            tex.FilePath = Path.GetDirectoryName(tex.Autofixedpath(TemporaryPath));

            tex.FileName = Path.GetFileName(path);

            // Heff: Cancellation check
            if (cts.IsCancellationRequested)
                return false;
            tex.EnumerateDetails();

            // Heff: if fix was successfull, but the number of mips are still wrong,
            // force it and let texplorer skip the lowest resolutions
            // Heff: this should no longer happen, but keeping this as it might help in some real odd case.
            if (tex.ExpectedMips > 1 && (tex.NumMips < tex.ExpectedMips || tex.NumMips < TPFTexInfo.CalculateMipCount(tex.Width, tex.Height)))
                tex.NumMips = Math.Max(tex.ExpectedMips, TPFTexInfo.CalculateMipCount(tex.Width, tex.Height));

            if (!tex.Valid)
            {
                tex = backup;
                tex.AutofixSuccess = false;
            }
            else
                tex.AutofixSuccess = true;

            return tex.AutofixSuccess;
        }
KFreonTPFTools3