Exemplo n.º 1
0
        public static bool PackGARC(string folderPath, string garcPath, int version, int bytesPadding, ProgressBar pBar1 = null, Label label = null, bool supress = false)
        {
            Progress = pBar1;
            Label    = label;
            GARC.FileCountDetermined += GARC_FileCountDetermined;
            GARC.PackProgressed      += GARC_PackProgressed;
            try
            {
                var filectr = GARC.PackGARC(folderPath, garcPath, version, bytesPadding);
                if (filectr > 0)
                {
                    // We're done.
                    SystemSounds.Exclamation.Play();
                    if (!supress)
                    {
                        WinFormsUtil.Alert("Pack Successful!", filectr + " files packed to the GARC!");
                    }
                }
                if (label != null)
                {
                    if (label.InvokeRequired)
                    {
                        label.Invoke((MethodInvoker)(() => label.Visible = false));
                    }
                    else
                    {
                        label.Visible = false;
                    }
                }

                return(true);
            }
            catch (DirectoryNotFoundException)
            {
                if (!supress)
                {
                    WinFormsUtil.Error("Folder does not exist.");
                }
            }
            catch (Exception e)
            {
                WinFormsUtil.Error("Packing failed", e.ToString());
            }
            finally
            {
                GARC.FileCountDetermined -= GARC_FileCountDetermined;
                GARC.PackProgressed      -= GARC_PackProgressed;
            }
            return(false);
        }
Exemplo n.º 2
0
        public static bool UnpackGARC(string garcPath, string outPath, bool skipDecompression, ProgressBar pBar1 = null, Label label = null, bool supress = false)
        {
            Progress = pBar1;
            Label    = label;
            GARC.FileCountDetermined += GARC_FileCountDetermined;
            GARC.UnpackProgressed    += GARC_UnpackProgressed;
            try
            {
                var fileCount = GARC.GarcUnpack(garcPath, outPath, skipDecompression);
                if (fileCount > 0)
                {
                    SystemSounds.Exclamation.Play();
                    if (!supress)
                    {
                        WinFormsUtil.Alert("Unpack Successful!", fileCount + " files unpacked from the GARC!");
                    }
                }

                if (label == null)
                {
                    return(true);
                }
                if (label.InvokeRequired)
                {
                    label.Invoke((MethodInvoker) delegate { label.Visible = false; });
                }
                else
                {
                    label.Visible = false;
                }
                return(true);
            }
            catch (FileNotFoundException)
            {
                WinFormsUtil.Alert("File does not exist");
            }
            finally
            {
                GARC.FileCountDetermined -= GARC_FileCountDetermined;
                GARC.UnpackProgressed    -= GARC_UnpackProgressed;
            }
            return(false);
        }
Exemplo n.º 3
0
        public static file load(Stream data)
        {
            //Too small
            if (data.Length < 0x10)
            {
                data.Close();
                return(new file {
                    type = formatType.unsupported
                });
            }

            BinaryReader input = new BinaryReader(data);
            uint         magic, length;

            switch (peek(input))
            {
            case 0x00010000: return(new file {
                    data = GfModel.load(data), type = formatType.model
                });

            case 0x00060000: return(new file {
                    data = GfMotion.loadAnim(input), type = formatType.anims
                });

            case 0x15041213: return(new file {
                    data = GfTexture.load(data), type = formatType.image
                });

            case 0x15122117:
                RenderBase.OModelGroup mdls = new RenderBase.OModelGroup();
                mdls.model.Add(GfModel.loadModel(data));
                return(new file {
                    data = mdls, type = formatType.model
                });
            }

            switch (getMagic(input, 5))
            {
            case "MODEL": return(new file {
                    data = DQVIIPack.load(data), type = formatType.container
                });
            }

            switch (getMagic(input, 4))
            {
            case "CGFX": return(new file {
                    data = CGFX.load(data), type = formatType.model
                });

            case "CRAG": return(new file {
                    data = GARC.load(data), type = formatType.container
                });

            case "darc": return(new file {
                    data = DARC.load(data), type = formatType.container
                });

            case "FPT0": return(new file {
                    data = FPT0.load(data), type = formatType.container
                });

            case "IECP":
                magic  = input.ReadUInt32();
                length = input.ReadUInt32();
                return(load(new MemoryStream(LZSS.decompress(data, length))));

            case "NLK2":
                data.Seek(0x80, SeekOrigin.Begin);
                return(new file
                {
                    data = CGFX.load(data),
                    type = formatType.model
                });

            case "SARC": return(new file {
                    data = SARC.load(data), type = formatType.container
                });

            case "SMES": return(new file {
                    data = NLP.loadMesh(data), type = formatType.model
                });

            case "Yaz0":
                magic  = input.ReadUInt32();
                length = IOUtils.endianSwap(input.ReadUInt32());
                data.Seek(8, SeekOrigin.Current);
                return(load(new MemoryStream(Yaz0.decompress(data, length))));

            case "zmdl": return(new file {
                    data = ZMDL.load(data), type = formatType.model
                });

            case "ztex": return(new file {
                    data = ZTEX.load(data), type = formatType.texture
                });
            }

            //Check if is a BCLIM or BFLIM file (header on the end)
            if (data.Length > 0x28)
            {
                data.Seek(-0x28, SeekOrigin.End);
                string clim = IOUtils.readStringWithLength(input, 4);
                if (clim == "CLIM" || clim == "FLIM")
                {
                    return new file {
                               data = BCLIM.load(data), type = formatType.image
                    }
                }
                ;
            }

            switch (getMagic(input, 3))
            {
            case "BCH":
                byte[] buffer = new byte[data.Length];
                input.Read(buffer, 0, buffer.Length);
                data.Close();
                return(new file
                {
                    data = BCH.load(new MemoryStream(buffer)),
                    type = formatType.model
                });

            case "DMP": return(new file {
                    data = DMP.load(data), type = formatType.image
                });
            }

            string magic2b = getMagic(input, 2);

            switch (magic2b)
            {
            case "AD": return(new file {
                    data = AD.load(data), type = formatType.model
                });

            case "BM": return(new file {
                    data = MM.load(data), type = formatType.model
                });

            case "BS": return(new file {
                    data = BS.load(data), type = formatType.anims
                });

            case "CM": return(new file {
                    data = CM.load(data), type = formatType.model
                });

            case "CP": return(new file {
                    data = CP.load(data), type = formatType.model
                });

            case "GR": return(new file {
                    data = GR.load(data), type = formatType.model
                });

            case "MM": return(new file {
                    data = MM.load(data), type = formatType.model
                });

            case "PC": return(new file {
                    data = PC.load(data), type = formatType.model
                });

            case "PT": return(new file {
                    data = PT.load(data), type = formatType.texture
                });
            }

            if (magic2b.Length == 2)
            {
                if ((magic2b[0] >= 'A' && magic2b[0] <= 'Z') &&
                    (magic2b[1] >= 'A' && magic2b[1] <= 'Z'))
                {
                    return(new file {
                        data = PkmnContainer.load(data), type = formatType.container
                    });
                }
            }

            //Compressions
            data.Seek(0, SeekOrigin.Begin);
            uint cmp = input.ReadUInt32();

            if ((cmp & 0xff) == 0x13)
            {
                cmp = input.ReadUInt32();
            }
            switch (cmp & 0xff)
            {
            case 0x11: return(load(new MemoryStream(LZSS_Ninty.decompress(data, cmp >> 8))));

            case 0x90:
                byte[] buffer  = BLZ.decompress(data);
                byte[] newData = new byte[buffer.Length - 1];
                Buffer.BlockCopy(buffer, 1, newData, 0, newData.Length);
                return(load(new MemoryStream(newData)));
            }

            data.Close();
            return(new file {
                type = formatType.unsupported
            });
        }
Exemplo n.º 4
0
        private void B_Shuffle_Click(object sender, EventArgs e)
        {
            if (garc == null)
            {
                return;
            }

            string garcID = L_File.Text.Split(':')[1].Replace("\\", "").Replace(" ", "");

            if (banlist.Contains(garcID))
            {
                WinFormsUtil.Alert("GARC is prevented from being shuffled."); return;
            }

            var g = GARC.unpackGARC(garc);

            // Build a list of all the files we can relocate.
            int[] randFiles = new int[g.fatb.FileCount];
            int   ctr       = 0;

            for (int i = 0; i < randFiles.Length; i++)
            {
                if (!g.fatb.Entries[i].IsFolder)
                {
                    randFiles[ctr++] = i;
                }
            }

            Array.Resize(ref randFiles, ctr);

            if (ctr == 0)
            {
                WinFormsUtil.Alert("No files to shuffle...?"); return;
            }

            // Create backup
            string dest = "backup" + Path.DirectorySeparatorChar + $"PreShuffle {garcID}";

            if (!File.Exists(dest))
            {
                File.Copy(garc, dest);
            }

            var g2 = GARC.unpackGARC(garc);

            int[] newFileOffset = (int[])randFiles.Clone();
            Util.Shuffle(newFileOffset);

            for (int i = 0; i < randFiles.Length; i++)
            {
                g.fatb.Entries[randFiles[i]] = g2.fatb.Entries[newFileOffset[i]];
            }

            #region Re-write GARC Header information!
            using (var newGARC = File.OpenWrite(garc))
                using (BinaryWriter gw = new BinaryWriter(newGARC))
                {
                    gw.Seek(7 * 4, SeekOrigin.Begin); // Skip GARC Header
                    // Write GARC
                    // gw.Write((uint)0x47415243); // GARC
                    // gw.Write((uint)0x0000001C); // Header Length
                    // gw.Write((ushort)0xFEFF);   // Endianness BOM
                    // gw.Write((ushort)0x0400);   // Const (4)
                    // gw.Write((uint)0x00000004); // Section Count (4)
                    // gw.Write((uint)0x00000000); // Data Offset (temp)
                    // gw.Write((uint)0x00000000); // File Length (temp)
                    // gw.Write((uint)0x00000000); // Largest File Size (temp)

                    // Write FATO
                    gw.Write((uint)0x4641544F);  // FATO
                    gw.Write(g.fato.HeaderSize); // Header Size
                    gw.Write(g.fato.EntryCount); // Entry Count
                    gw.Write(g.fato.Padding);    // Padding
                    for (int i = 0; i < g.fato.Entries.Length; i++)
                    {
                        gw.Write((uint)g.fato.Entries[i].Offset);
                    }

                    // Write FATB
                    gw.Write((uint)0x46415442);  // FATB
                    gw.Write(g.fatb.HeaderSize); // Header Size
                    gw.Write(g.fatb.FileCount);  // File Count
                    foreach (var fe in g.fatb.Entries)
                    {
                        gw.Write(fe.Vector);
                        foreach (var s in fe.SubEntries.Where(s => s.Exists))
                        {
                            gw.Write((uint)s.Start);
                            gw.Write((uint)s.End);
                            gw.Write((uint)s.Length);
                        }
                    }
                }
            #endregion

            WinFormsUtil.Alert("GARC Shuffled!");
        }