Ejemplo n.º 1
0
 public static GarcDef UnpackGarc(byte[] data)
 {
     using (var ms = new MemoryStream(data))
     {
         return(GarcUtil.UnpackGarc(ms));
     }
 }
Ejemplo n.º 2
0
        public virtual Task Read(byte[] data)
        {
            this.Data = data;
            this.Def  = GarcUtil.UnpackGarc(data);

            return(Task.CompletedTask);
        }
Ejemplo n.º 3
0
        public override async Task SaveFile()
        {
            byte[][] data = await Task.WhenAll(this.storage.Select((s, i) => s.Save()));

            var memGarc = await GarcUtil.PackGarc(data, this.Def.Version, (int)this.Def.ContentPadToNearest);

            this.Def  = memGarc.Def;
            this.Data = memGarc.Data;
        }
Ejemplo n.º 4
0
        public static async Task <GarcDef> UnpackGarc(string path)
        {
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                using (var ms = new MemoryStream())
                {
                    await fs.CopyToAsync(ms);

                    return(GarcUtil.UnpackGarc(ms));
                }
        }
Ejemplo n.º 5
0
        public virtual async Task SetFiles(byte[][] files)
        {
            if (files == null || files.Length != this.FileCount)
            {
                throw new ArgumentException();
            }

            var memGarc = await GarcUtil.PackGarc(files, this.Def.Version, (int)this.Def.ContentPadToNearest);

            this.Def  = memGarc.Def;
            this.Data = memGarc.Data;
        }
Ejemplo n.º 6
0
        public virtual async Task SaveFile()
        {
            byte[][] data = new byte[this.FileCount][];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = await this.GetFile(i);
            }

            var memGarc = await GarcUtil.PackGarc(data, this.Def.Version, (int)this.Def.ContentPadToNearest);

            this.Def  = memGarc.Def;
            this.Data = memGarc.Data;
        }
Ejemplo n.º 7
0
        public static async Task <bool> GarcUnpack(string garcPath, string outPath, bool skipDecompression, bool supress = false)
        {
            if (!File.Exists(garcPath) && !supress)
            {
                return(false);
            }

            // Unpack the GARC
            GarcDef garc = await GarcUtil.UnpackGarc(garcPath);

            const string ext       = "bin";       // Default Extension Name
            int          fileCount = garc.Fatb.FileCount;
            string       format    = "D" + Math.Ceiling(Math.Log10(fileCount));

            if (outPath == "gametext")
            {
                format = "D3";
            }

            using (BinaryReader br = new BinaryReader(File.OpenRead(garcPath)))
            {
                // Create Extraction folder if it does not exist.
                if (!Directory.Exists(outPath))
                {
                    Directory.CreateDirectory(outPath);
                }

                // Pull out all the files
                for (int o = 0; o < garc.Fato.EntryCount; o++)
                {
                    var entry = garc.Fatb.Entries[o];
                    // Set Entry File Name
                    string fileName = o.ToString(format);

                    #region OutDirectory Determination

                    string parentFolder = entry.IsFolder
                                                                                          ? Path.Combine(outPath, fileName)
                                                                                          : outPath;
                    if (entry.IsFolder)                       // Process Folder
                    {
                        Directory.CreateDirectory(parentFolder);
                    }

                    #endregion

                    uint vector = entry.Vector;
                    for (int i = 0; i < 32; i++)                       // For each bit in vector
                    {
                        var subEntry = entry.SubEntries[i];
                        if (!subEntry.Exists)
                        {
                            continue;
                        }

                        // Seek to Offset
                        br.BaseStream.Position = subEntry.Start + garc.DataOffset;

                        // Check if Compressed
                        bool compressed = false;
                        if (!skipDecompression)
                        {
                            try
                            {
                                compressed = (byte)br.PeekChar() == 0x11;
                            }
                            catch
                            {
                                // ignored
                            }
                        }

                        // Write File
                        string fileOut = Path.Combine(parentFolder, (entry.IsFolder
                                                                                                                                                   ? i.ToString("00")
                                                                                                                                                   : fileName) + "." + ext);
                        using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(fileOut)))
                        {
                            // Write out the data for the file
                            br.BaseStream.Position = subEntry.Start + garc.DataOffset;
                            bw.Write(br.ReadBytes(subEntry.Length));
                        }
                        if (compressed)

                        #region Decompression

                        {
                            string decout = Path.Combine(Path.GetDirectoryName(fileOut), "dec_" + Path.GetFileName(fileOut));
                            try
                            {
                                await Lzss.Decompress(fileOut, decout);

                                try
                                {
                                    File.Delete(fileOut);
                                }
                                catch (Exception)
                                {
                                    // ignored
                                }
                            }
                            catch
                            {
                                // File is really not encrypted.
                                try
                                {
                                    File.Delete(decout);
                                }
                                catch (Exception)
                                {
                                    // ignored
                                }
                            }
                        }

                        #endregion

                        if ((vector >>= 1) == 0)
                        {
                            break;
                        }
                    }
                }
            }

            return(true);
        }