Esempio n. 1
0
 private void StartProcess1(object o)
 {
     try
     {
         ProcessorArgs     processorArgs = (ProcessorArgs)o;
         ConfigurationMain mainCfg       = processorArgs.ConfigurationMain;
         //
         if (mainCfg.IsShowResults)
         {
             Logger.Log("Preparing futur results data");
             var infoTextures = new DirectoryInfo(mainCfg.PathSource);
             //
             foreach (var directoryInfo in infoTextures.GetDirectories())
             {
                 processorArgs.Folders.Add(directoryInfo.Name,
                                           new InformationDirectory {
                     Size = FileUtils.CalculateDirectorySizeMo(directoryInfo, mainCfg.PassBsa.Enabled)
                 });
             }
             processorArgs.Folders.Add(infoTextures.Name,
                                       new InformationDirectory {
                 Size = FileUtils.CalculateDirectorySizeMo(infoTextures, mainCfg.PassBsa.Enabled)
             });
         }
         //
         if (mainCfg.PassBsa.Enabled)
         {
             FileInformation.GetFileInfos(mainCfg.PathSource, mainCfg.PathBackup, processorArgs.FilesBsa, true, mainCfg);
             FilePreparation.PrepareTreat(processorArgs.FilesBsa, mainCfg, processorArgs.BsaOrders, null, null, true);
             FileExecution.ExecuteTreat(processorArgs.BsaOrders, mainCfg, true);
             if (mainCfg.PassBsa.MustUnpack())
             {
                 FileExecution.UnpackBsa(processorArgs.BsaOrders, mainCfg);
             }
             if (mainCfg.PassBsa.IsCopyAsLoose)
             {
                 if (mainCfg.PassBsa.IsCopyAsLooseIfDummy)
                 {
                     List <InformationOrder> confirmedBsaUnpack = new List <InformationOrder>();
                     List <InformationCopy>  confirmedDeletes   = new List <InformationCopy>();
                     foreach (InformationOrder bsaOrder in processorArgs.BsaOrders)
                     {
                         var      bsaName = Path.GetFileNameWithoutExtension(bsaOrder.FileSource.FullName);
                         var      espName = bsaName.ToUpper().Replace(" - TEXTURES", "").Replace("- TEXTURES", "");
                         var      espFile = Path.Combine(bsaOrder.FileSource.Directory.FullName, string.Format("{0}.esp", espName));
                         FileInfo dummy   = new FileInfo("dummy.esp");
                         if (!dummy.Exists)
                         {
                             throw new Exception("No dummy esp to compare from. A dummy.esp file must be present in Octagon directory.");
                         }
                         FileInfo esp = new FileInfo(espFile);
                         if (esp.Exists && dummy.Length == esp.Length)
                         {
                             confirmedBsaUnpack.Add(bsaOrder);
                             if (!confirmedDeletes.Any(e => string.Equals(e.Target, espFile, StringComparison.OrdinalIgnoreCase)))
                             {
                                 confirmedDeletes.Add(new InformationCopy(null, espFile, false, false));
                             }
                         }
                     }
                     FileExecution.CopyBsaAsLooseSimplified(confirmedBsaUnpack, mainCfg);
                     foreach (InformationCopy confirmedDelete in confirmedDeletes)
                     {
                         FileUtils.ExecuteCopyOrDelete(confirmedDelete);
                     }
                 }
                 else
                 {
                     FileExecution.CopyBsaAsLooseSimplified(processorArgs.BsaOrders, mainCfg);
                 }
             }
         }
         //
         if (mainCfg.HasTextureOperation() || (mainCfg.IsBackupActivated && mainCfg.IsRecopyOriginal))
         {
             FileInformation.GetFileInfos(mainCfg.PathSource, mainCfg.PathBackup, processorArgs.FilesDds, false, mainCfg);
         }
         OnProcessorEnded(2);
     }
     catch (AggregateException e)
     {
         Logger.Log(e);
         foreach (Exception innerException in e.InnerExceptions)
         {
             Logger.Log(innerException);
         }
         OnProcessorEnded(-1);
     }
     catch (Exception e)
     {
         Logger.Log(e);
         OnProcessorEnded(-1);
     }
 }
Esempio n. 2
0
        private void PrepareFileOrder(List <InformationFile> files, ConfigurationMain mainCfg, bool isBsa, InformationFile file,
                                      List <InformationOrder> orders, List <InformationOrder> discardedOrders, List <InformationOrder> searchResult, string eventName)
        {
            if (mainCfg.IsVerbose)
            {
                Logger.Log(string.Format("Analyzing {0}", file.FileSource.FullName));
            }
            //
            Progresser.ChangeProgress(eventName, files.Count);
            //
            var confirmOrder            = false;
            var isBsaMustBeDecompressed = false;
            var order = new InformationOrder(file.FileSource, file.FileTarget)
            {
                IsUseBackup = mainCfg.IsUseBackup && mainCfg.IsBackupActivated
            };

            //
            if (isBsa)
            {
                if (mainCfg.PassBsa.Enabled && mainCfg.PassBsa.Selection.GetValidation(Path.GetFileNameWithoutExtension(file.FileSource.FullName)))
                {
                    order.IsBsaFormatCompressed = ExternalTools.CallBsarchProperty(file.FileSource.FullName, "*COMPRESSED");
                    //
                    if (mainCfg.PassBsa.IsCheckFormatIsGameFormat)
                    {
                        string searchForFile = "sse";
                        switch (mainCfg.PassBsa.GameParameter)
                        {
                        case "fo4":
                            searchForFile = "Fallout 4 General";
                            break;

                        case "fo4dds":
                            searchForFile = "Fallout 4 DDS";
                            order.IsBsaFormatCompressed = true;
                            break;
                        }
                        isBsaMustBeDecompressed = ExternalTools.CallBsarchProperty(file.FileSource.FullName, searchForFile);
                    }
                    else if (mainCfg.PassBsa.IsCopyAsLoose)
                    {
                        if (mainCfg.PassBsa.IsTreatNonTextureArchives)
                        {
                            isBsaMustBeDecompressed = true;
                        }
                        else
                        {
                            foreach (var texExtensionName in TextureExtensionList.List)
                            {
                                isBsaMustBeDecompressed = ExternalTools.CallBsarchList(file.FileSource.FullName, texExtensionName);
                                if (isBsaMustBeDecompressed)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    //
                    confirmOrder |= isBsaMustBeDecompressed;
                }
            }
            else if (mainCfg.HasTextureOperation() && GetCurrentImageSize(order, mainCfg.IsVerbose))
            {
                //
                foreach (var scalePass in mainCfg.Passes)
                {
                    // Stop here if validation failed
                    if (!scalePass.Selection.GetValidation(Path.GetFileNameWithoutExtension(file.FileSource.FullName)))
                    {
                        continue;
                    }
                    //
                    if (scalePass.TypePass == TypePass.ApplyGmic)
                    {
                        order.IsGmicPass = true;
                        order.GmicCommands.Add(scalePass.Command);
                        confirmOrder = true;
                        continue;
                    }
                    //
                    if (scalePass.TypePass == TypePass.ApplyCustom)
                    {
                        order.IsCustomPass = true;
                        order.IsApplyOnPng = scalePass.IsApplyOnPng;
                        order.CustomCommands.Add(scalePass.Command);
                        confirmOrder = true;
                        continue;
                    }
                    //
                    if (scalePass.TypePass == TypePass.Force)
                    {
                        order.IsDoTexconv = true;
                        order.TargetSize.TypeTexCompression = scalePass.TypeTexCompression;
                        confirmOrder = true;
                        continue;
                    }
                    //
                    string f = order.TargetSize.Format;
                    if (!string.IsNullOrEmpty(scalePass.ForceFormat) && !string.Equals(scalePass.ForceFormat, f, StringComparison.OrdinalIgnoreCase))
                    {
                        order.IsDoTexconv                   = true;
                        order.TargetSize.Format             = scalePass.ForceFormat;
                        order.TargetSize.TypeTexCompression = scalePass.TypeTexCompression;
                        confirmOrder = true;
                        continue;
                    }
                    //

                    //
                    int w = order.TargetSize.Width;
                    int h = order.TargetSize.Height;
                    int m = order.TargetSize.Mipmaps;
                    //
                    int  div       = 1;
                    int  mult      = 1;
                    bool applyPass = false;
                    //
                    if (scalePass.TypePass == TypePass.CorrectSize)
                    {
                        //correct to nearby power of 2 value
                        if (!TablePowerOfTwo.ContainsKey(w) || !TablePowerOfTwo.ContainsKey(h))
                        {
                            w         = order.TargetSize.Width = FindNearPowerOf2Value(w);
                            h         = order.TargetSize.Height = FindNearPowerOf2Value(h);
                            applyPass = true;
                            //
                            if (mainCfg.IsVerbose)
                            {
                                Logger.Log(
                                    "{0} size is not a power of 2 and will be resized to width=>{1} height=>{2}",
                                    file.FileSource.Name, w, h, TypeLog.Warning);
                            }
                        }
                    }
                    // find new size and new mipmaps needed
                    if (scalePass.TypePass == TypePass.DownscaleFactor)
                    {
                        // division method
                        if ((w / scalePass.WantedFactor >= scalePass.WantedMinSize && h / scalePass.WantedFactor >= scalePass.WantedMinSize))
                        {
                            div       = scalePass.WantedFactor;
                            applyPass = true;
                        }
                    }
                    //
                    if (scalePass.TypePass == TypePass.DownscaleFixed)
                    {
                        // fixed size method
                        while ((w / div > scalePass.WantedSize || h / div > scalePass.WantedSize) &&
                               (w / div > scalePass.WantedMinSize && h / div > scalePass.WantedMinSize))
                        {
                            div      *= 2;
                            applyPass = true;
                        }
                    }
                    // find new size and new mipmaps needed
                    if (scalePass.TypePass == TypePass.UpscaleFactor)
                    {
                        // division method
                        if ((w * scalePass.WantedFactor < scalePass.WantedMaxSize && h * scalePass.WantedFactor < scalePass.WantedMaxSize))
                        {
                            mult      = scalePass.WantedFactor;
                            applyPass = true;
                        }
                    }
                    //
                    if (scalePass.TypePass == TypePass.UpscaleFixed)
                    {
                        // fixed size method
                        while ((w * mult < scalePass.WantedSize || h * mult < scalePass.WantedSize) &&
                               (w * mult < scalePass.WantedMaxSize && h * mult < scalePass.WantedMaxSize))
                        {
                            mult     *= 2;
                            applyPass = true;
                        }
                    }
                    //
                    if (scalePass.TypePass == TypePass.CorrectMipmaps && m > 1)
                    {
                        var newSizeToSonsiderFormips = Math.Max(w, h);
                        if (TablePowerOfTwo.ContainsKey(newSizeToSonsiderFormips))
                        {
                            var goodMipmap = TablePowerOfTwo[newSizeToSonsiderFormips];
                            if (m != goodMipmap)
                            {
                                m         = goodMipmap;
                                applyPass = true;
                            }
                        }
                    }
                    //
                    if (scalePass.TypePass == TypePass.ForceMipmaps)
                    {
                        m         = 2;
                        applyPass = true;
                    }
                    //
                    if (applyPass)
                    {
                        //
                        var newWidth  = (w / div) * mult;
                        var newHeight = (h / div) * mult;
                        //
                        if (!TablePowerOfTwo.ContainsKey(newWidth) || !TablePowerOfTwo.ContainsKey(newHeight))
                        {
                            Logger.Log("Texture size is not a power of 2 for {0} (current size: {1}/{2})", file.FileSource.FullName, newWidth, newHeight, TypeLog.Warning);
                            //continue;
                        }
                        //
                        int newMipmap = 1;
                        //
                        if (m > 1)
                        {
                            var newSizeToSonsiderFormips = Math.Max(newWidth, newHeight);
                            if (TablePowerOfTwo.ContainsKey(newSizeToSonsiderFormips))
                            {
                                newMipmap = TablePowerOfTwo[newSizeToSonsiderFormips];
                            }
                            else
                            {
                                newMipmap = 0;
                            }
                        }
                        //
                        order.IsDoTexconv        = true;
                        order.TargetSize.Width   = newWidth;
                        order.TargetSize.Height  = newHeight;
                        order.TargetSize.Mipmaps = newMipmap;
                        confirmOrder             = true;
                    }
                }
            }
            //
            if (mainCfg.IsRefreshBackup && mainCfg.IsBackupActivated)
            {
                if (((isBsa && isBsaMustBeDecompressed) || !isBsa) && (!order.FileTarget.Exists || order.FileSource.Length > order.FileTarget.Length))
                {
                    order.IsRefreshBackup = true;
                    confirmOrder          = true;
                }
            }
            //
            if (mainCfg.IsRecopyOriginal && mainCfg.IsBackupActivated)
            {
                if (((isBsa && isBsaMustBeDecompressed) || !isBsa) && (!order.FileSource.Exists || order.FileTarget.Length > order.FileSource.Length))
                {
                    order.IsRecopyOriginal = true;
                    confirmOrder           = true;
                }
            }
            //
            if (order != null)
            {
                bool searchValidation = true;
                //
                if (mainCfg.Search.IsSearchEnabled && !isBsa)
                {
                    searchValidation = Search(searchResult, order, mainCfg.Search);
                }
                //
                if (confirmOrder && (searchValidation || !mainCfg.Search.IsApplySearchToProcess))
                {
                    lock (orders)
                    {
                        orders.Add(order);
                    }
                }
                else
                {
                    if (discardedOrders != null)
                    {
                        lock (discardedOrders)
                        {
                            discardedOrders.Add(order);
                        }
                    }
                }
            }
        }