ME3Explorer.Texplorer2.InstallTexture C# (CSharp) Method

InstallTexture() public method

public InstallTexture ( string texname, List pccs, List IDs, byte imgdata ) : bool
texname string
pccs List
IDs List
imgdata byte
return bool
        public bool InstallTexture(string texname, List<string> pccs, List<int> IDs, byte[] imgdata)
        {
            if (pccs.Count == 0)
            {
                DebugOutput.PrintLn("No PCC's found for " + texname + ", skipping.");
                return false;
            }
            string fulpath = pccs[0];
            //string temppath = (WhichGame == 1) ? Path.GetDirectoryName(pathBIOGame) : pathBIOGame;
            // Heff: Again, is the removal of the last dir for ME1 intended, and if so for what purpose?
            string temppath = pathBIOGame;
            if (!fulpath.Contains(temppath))
                fulpath = Path.Combine(temppath, fulpath);


            // KFreon: Skip files that don't exist
            if (!File.Exists(fulpath))
                return false;

            PCCObjects.IPCCObject pcc = PCCObjects.Creation.CreatePCCObject(fulpath, WhichGame);
            
            if ((pcc.Exports[IDs[0]].ClassName != "Texture2D" && pcc.Exports[IDs[0]].ClassName != "LightMapTexture2D" && pcc.Exports[IDs[0]].ClassName != "TextureFlipBook") || string.Compare(pcc.Exports[IDs[0]].ObjectName, texname, true) != 0)
                throw new InvalidDataException("Export is not correct class or name!");

            //Load the texture from the pcc
            Textures.ITexture2D tex2D = pcc.CreateTexture2D(IDs[0], pathBIOGame);
            {
                    
                tex2D.allPccs = pccs;
                tex2D.expIDs = IDs;
                int noImg = tex2D.imgList.Count;

                DebugOutput.PrintLn("Now replacing textures in texture: " + tex2D.texName, true);
                Debug.WriteLine("Now replacing textures in texture: " + tex2D.texName + "  ID: " + IDs[0]);
                WriteDebug("Now replacing textures in texture: " + tex2D.texName + "  ID: " + IDs[0]);

                ImageFile im = null;
                try
                {
                    im = new DDS("", imgdata);
                }
                catch
                {
                    Console.WriteLine("Error: Unable to detect input DDS format, skipping.");
                    return false;
                }



                // KFreon: TESTING
                Debug.WriteLine("First pcc: " + fulpath + "    ArcName: " + tex2D.arcName);
                WriteDebug("First pcc: " + fulpath + "    ArcName: " + tex2D.arcName);



                //The texture is a single image, therefore use replace function
                if (noImg == 1)
                {
                    string imgSize = tex2D.imgList[0].imgSize.width + "x" + tex2D.imgList[0].imgSize.height;
                    try
                    {
                        tex2D.replaceImage(imgSize, im, pathBIOGame);
                    }
                    catch
                    {
                        // KFreon:  If replace fails, it's single image thus use the singleimageupscale function
                        tex2D.singleImageUpscale(im, pathBIOGame);
                    }
                }

                //If the texture has multiple images, then check the input texture for MIPMAPS
                else
                {
                    bool hasMips = true;
                    ImageFile imgFile = im;
                    /*try { ImageMipMapHandler imgMipMap = new ImageMipMapHandler("", imgdata); }
                    catch (Exception e)
                    {
                        hasMips = false;
                    }*/
                    using (ImageEngineImage img = new ImageEngineImage(imgdata))
                        hasMips = img.NumMipMaps > 1;


                    if (!hasMips)
                    {
                        string imgSize = imgFile.imgSize.width + "x" + imgFile.imgSize.height;
                        try
                        {
                            //Try replacing the image. If it doesn't exist then it'll throw and error and you'll need to upscale the image
                            tex2D.replaceImage(imgSize, imgFile, pathBIOGame);
                        }
                        catch (Exception e)
                        {
                            tex2D.addBiggerImage(imgFile, pathBIOGame);
                        }
                    }
                    else
                    {
                        try
                        {
                            tex2D.OneImageToRuleThemAll(imgFile, pathBIOGame, imgdata);
                        }
                        catch (Exception e)
                        {
                            if (e.Message.Contains("Format"))
                            {
                                MessageBox.Show(texname + " is in the wrong format." + Environment.NewLine + Environment.NewLine + e.Message);
                                return false;
                            }
                        }
                    }
                }

                Debug.WriteLine("After replace: " + tex2D.arcName);
                WriteDebug("After replace: " + tex2D.arcName);


                DebugOutput.PrintLn("Replacement complete. Now saving pcc: " + pcc.pccFileName, true);

                PCCObjects.IExportEntry expEntry = pcc.Exports[IDs[0]];
                var tt = tex2D.ToArray(expEntry.DataOffset, pcc);
                expEntry.SetData(tt);
                expEntry.hasChanged = true;
                pcc.Exports[IDs[0]] = expEntry;

                pcc.saveToFile(pcc.pccFileName);

                int modCount = tex2D.allPccs.Count;

                // KFreon: Elapsed time stuff
                int start = Environment.TickCount;

                // KFreon: Refresh objects after saving
                pcc = KFreonLib.PCCObjects.Creation.CreatePCCObject(pcc.pccFileName, WhichGame);
                tex2D = pcc.CreateTexture2D(IDs[0], pathBIOGame);


                if (modCount > 1)
                    for (int item = 1; item < modCount; item++)
                    {
                        Debug.WriteLine(pccs[item] + "   " + IDs[item]);
                        WriteDebug(pccs[item] + "   " + IDs[item]);
                        if (!SaveFile(pccs, IDs, tex2D, item))
                            break;
                    }
                Debug.WriteLine("");
                WriteDebug("");

                // KFreon: More timer stuff
                TimeSpan ts = TimeSpan.FromMilliseconds(Environment.TickCount - start);
                Console.WriteLine(ts.Duration());
                DebugOutput.Print("All PCC updates finished. ");
                return true;
            }

            pcc = null;
        }

Usage Example

コード例 #1
0
        private bool InstallTextures(List<TPFTexInfo> textures, bool result)
        {
            // KFreon: Cancel if analysis failed
            if (!result)
            {
                this.Invoke(new Action(() =>
                {
                    EnableSecondProgressBar(false);
                    OverallStatusLabel.Text = "Ready.";
                    OverallProgressBar.Value = 0;
                }));
                return false;
            }

            // KFreon: Get valids only
            List<TPFTexInfo> validtexes = new List<TPFTexInfo>();
            validtexes = new List<TPFTexInfo>(textures.Where(tex => tex.Valid));

            int valids = validtexes.Count;
            if (valids == 0)
                return true;

            // KFreon: Setup modified DLC list
            List<string> modifiedDLC = new List<string>();

            // Heff: Cancellation check
            if (cts.IsCancellationRequested)
                return false;
            OverallProg.ChangeProgressBar(0, validtexes.Count + 1);
            int count = 0;
            DebugOutput.PrintLn("Textures loaded = " + validtexes.Count);
            DebugOutput.PrintLn("Num valid: " + valids);

            // KFreon: Install textures
            Texplorer2 texplorer = new Texplorer2(true, WhichGame);
            var numInstalled = 0;
            foreach (TPFTexInfo tex in validtexes)
            {
                // Heff: Cancellation check
                if (cts.IsCancellationRequested)
                    return false;
                
                // Heff: match against known problematic hashes, prompt the user.
                // (particularly textures animateed by distortion)
                if (DistortionHashList.Contains(tex.Hash))
                {
                    bool install = false;
                    this.Invoke(new Action(() =>
                    {
                        var dialogResult = MessageBox.Show("This texture is known to cause problems when resolution is higher than normal. \nDo you still want to try to install it? (Not recommended for normal users)", "Warning!", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        install = dialogResult == System.Windows.Forms.DialogResult.Yes ? true : false;
                    }));
                    if (!install)
                    {
                        this.Invoke(new Action(() =>
                        {
                            Overall.UpdateText("Skipping mod:  " + tex.TexName + " | " + count + "/" + valids + " mods completed.");
                            DebugOutput.PrintLn("Skipping mod:  " + tex.TexName + " | " + count++ + "/" + valids + " mods completed.");
                            OverallProg.IncrementBar();
                        }));
                        continue;
                    }
                }

                this.Invoke(new Action(() =>
                {
                    Overall.UpdateText("Installing mod:  " + tex.TexName + " | " + count + "/" + valids + " mods completed.");
                    DebugOutput.PrintLn("Installing mod:  " + tex.TexName + " | " + count++ + "/" + valids + " mods completed.");
                    OverallProg.IncrementBar();
                }));

                try
                {
                    if (texplorer.InstallTexture(tex.TexName, tex.Files, tex.ExpIDs, tex.Extract(null, true)))
                        numInstalled++;
                } catch (Exception e)
                {
                    if (e is System.UnauthorizedAccessException)
                    {
                        this.Invoke(new Action(() =>
                        {
                            MessageBox.Show("Could not install " + tex.TexName + " due to problems with the pcc file, \nplease check that the relevant .pcc files are not set as read-only, \nand try running me3explorer as admin.", "Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }));
                    }
                    else
                    {
                        DebugOutput.PrintLn("Unknown error with mod:  " + tex.TexName + ", skipping.");
                        DebugOutput.PrintLn("(Error:  " + e.Message + ")");
                    }
                    continue;
                }

                // KFreon: Add modified DLC to list
                if (WhichGame == 3)
                {
                    foreach (string file in tex.Files)
                    {
                        string dlcname = KFreonLib.Misc.Methods.GetDLCNameFromPath(file);
                        if (dlcname != "" && dlcname != null && !modifiedDLC.Contains(dlcname))
                            modifiedDLC.Add(dlcname);
                    }
                }
            }

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

            if (numInstalled > 0)
            {
                // KFreon: Update TOC's
                this.Invoke(new Action(() =>
                {
                    OverallProgressBar.Value = OverallProgressBar.Maximum - 1;
                    OverallStatusLabel.Text = "Checking TOC.bin...";
                }));
                Texplorer2.UpdateTOCs(WhichGame);

                // Heff: Cancellation check
                if (cts.IsCancellationRequested)
                    return false;
                this.Invoke(new Action(() =>
                {
                    OverallStatusLabel.Text = "Installed " + numInstalled + "/" + valids + " valid mods.";
                    OverallProgressBar.Value = OverallProgressBar.Maximum;
                }));
            }
            else
            {
                DebugOutput.PrintLn("All mods were skipped.");
                this.Invoke(new Action(() =>
                {
                    OverallStatusLabel.Text = "No mods installed!";
                    OverallProgressBar.Value = OverallProgressBar.Maximum;
                }));
            }

            return true;
        }
All Usage Examples Of ME3Explorer.Texplorer2::InstallTexture
Texplorer2