Ejemplo n.º 1
0
        internal static string Interpret(string path)
        {
            string fn = Path.GetFileName(path);

            if (fn == "save0.bin" || fn == "save1.bin" || fn == "save2.bin")
            {
                return(FixMajoraChecksum(path));
            }
            if (fn.StartsWith("message") && (fn.EndsWith("_US.bin") || fn.EndsWith("_UK.bin")))
            {
                return(ParseShuffleText(path));
            }
            DARC       darc  = analyze(path);
            FARC       farc  = new FARC();
            GAR        gar   = new GAR();
            SARC       sarc  = new SARC();
            ShuffleARC sharc = new ShuffleARC();

            if (!darc.valid)
            {
                farc = analyzeFARC(path);
            }
            if (!farc.valid)
            {
                gar = analyzeGAR(path);
            }
            if (!gar.valid)
            {
                sharc = AnalyzeShuffle(path);
            }
            if (!sharc.valid)
            {
                sarc = analyzeSARC(path);
            }

            string ret = "";

            if (darc.valid)
            {
                ret += "Header Offset: " + darc.HeaderOffset + Environment.NewLine + "File Count: " + darc.Files.Files.Count + Environment.NewLine;
                int extracted = 0;
                int folder    = 0;
                for (int i = 0; i < darc.Files.Files.Count; i++)
                {
                    if (darc.Files.Files[i].Folder > 0)
                    {
                        folder++;
                    }
                    else
                    {
                        extracted++;
                        string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + darc.FileName + Path.DirectorySeparatorChar;
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        string outPath = dir + darc.Files.FileNames[i];
                        var    fs      = File.OpenRead(path);
                        fs.Seek(darc.Files.Files[i].Offset, SeekOrigin.Begin);
                        byte[] fileBuffer = new byte[darc.Files.Files[i].Length];
                        fs.Read(fileBuffer, 0, fileBuffer.Length);
                        fs.Close();
                        File.WriteAllBytes(outPath, fileBuffer);
                    }
                }
                ret += "Extracted " + extracted + " files";
                if (folder > 0)
                {
                    ret += ", did not extract " + folder + " folders";
                }
                ret += "." + Environment.NewLine + "Open a .DARC/.SARC/.FARC/.GAR/Shuffle Archive file (or drag/drop).";
            }
            else if (farc.valid)
            {
                ret += "Header Offset: " + farc.HeaderOffset + Environment.NewLine;
                int extracted = 0;
                for (int i = 0; i < farc.Files.Files.Count; i++)
                {
                    extracted++;
                    string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + "FARC_" + farc.FileName + Path.DirectorySeparatorChar;
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string outPath = dir + farc.Files.FileNames[i];
                    var    fs      = File.OpenRead(farc.FilePath + "\\" + farc.FileName + farc.Extension);
                    fs.Seek(farc.Files.Files[i].Offset + farc.DataOffset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[farc.Files.Files[i].Length];
                    fs.Read(fileBuffer, 0, fileBuffer.Length);
                    fs.Close();
                    File.WriteAllBytes(outPath, fileBuffer);
                }
                ret += "Extracted " + extracted + " files";
                ret += "." + Environment.NewLine + ".DARC/.FARC/.SARC/.GAR/Shuffle Archive file (or drag/drop).";
            }
            else if (gar.valid)
            {
                ret += "New GAR with " + gar.FileCount + " files." + Environment.NewLine;
                string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + gar.FileName + Path.DirectorySeparatorChar;
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                for (int i = 0; i < gar.FileCount; i++)
                {
                    var fs = File.OpenRead(path);
                    fs.Seek(gar.Files[i].Offset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[gar.Files[i].Length];
                    fs.Read(fileBuffer, 0, fileBuffer.Length);
                    fs.Close();
                    File.WriteAllBytes(dir + gar.Files[i].NameWithExtension, fileBuffer);
                    ret += "Extracted " + gar.Files[i].NameWithExtension + " (Offset: " + gar.Files[i].Offset.ToString("X8") + ", Len: " + gar.Files[i].Length.ToString("X8") + ")." + Environment.NewLine;
                }
                ret += Environment.NewLine;
            }
            else if (sharc.valid)
            {
                UnpackShuffleARC(path, sharc, ret);
            }
            else if (sarc.Valid)
            {
                UnpackSARC(sarc);
            }
            else
            {
                ret = "Not a valid .DARC/.FARC/.SARC/.GAR/Shuffle Archive file";
            }
            return(ret);
        }
Ejemplo n.º 2
0
        internal static GAR analyzeGAR(string path)
        {
            GAR gar = new GAR
            {
                FileName  = Path.GetFileNameWithoutExtension(path),
                FilePath  = Path.GetDirectoryName(path),
                Extension = Path.GetExtension(path)
            };
            BinaryReader br  = new BinaryReader(File.OpenRead(path));
            long         len = br.BaseStream.Length;

            gar.Magic      = br.ReadUInt32();
            gar.FileLength = br.ReadUInt32();
            if (gar.Magic != 0x02524147 || gar.FileLength != len)
            {
                gar.valid = false;
                return(gar);
            }
            gar.valid             = true;
            gar.Unknown           = br.ReadUInt32();
            gar.HeaderLength      = br.ReadUInt32();
            gar.FileMetaOffset    = br.ReadUInt32();
            gar.FileOffsetsOffset = br.ReadUInt32();
            br.BaseStream.Seek(0x1C, SeekOrigin.Current);
            gar.FileCountOffset = br.ReadUInt32();
            gar.CTXBOffset      = br.ReadUInt32();
            br.BaseStream.Seek(gar.FileOffsetsOffset, SeekOrigin.Begin);
            gar.DataOffset = br.ReadUInt32();
            gar.FileCount  = (gar.DataOffset - gar.FileOffsetsOffset) / 4;
            br.BaseStream.Seek(gar.FileMetaOffset, SeekOrigin.Begin);
            gar.Files = new List <GARFile>();
            for (int i = 0; i < gar.FileCount; i++)
            {
                GARFile gf = new GARFile
                {
                    Length    = br.ReadUInt32(),
                    NOffset   = br.ReadUInt32(),
                    NWEOffset = br.ReadUInt32()
                };
                gar.Files.Add(gf);
            }
            for (int i = 0; i < gar.FileCount; i++)
            {
                br.BaseStream.Seek(gar.Files[i].NOffset, SeekOrigin.Begin);
                StringBuilder sb = new StringBuilder();
                for (char c = br.ReadChar(); c != (char)0; c = br.ReadChar())
                {
                    sb.Append(c);
                }
                gar.Files[i].Name = sb.ToString();
                br.BaseStream.Seek(gar.Files[i].NWEOffset, SeekOrigin.Begin);
                sb = new StringBuilder();
                for (char c = br.ReadChar(); c != (char)0; c = br.ReadChar())
                {
                    sb.Append(c);
                }
                gar.Files[i].NameWithExtension = sb.ToString();
            }
            br.BaseStream.Seek(gar.FileOffsetsOffset, SeekOrigin.Begin);
            if (gar.Files.Count > 0)
            {
                gar.Files[0].Offset = gar.DataOffset;
                br.ReadUInt32();
            }
            for (int i = 1; i < gar.FileCount; i++)
            {
                gar.Files[i].Offset = br.ReadUInt32();
            }
            return(gar);
        }
Ejemplo n.º 3
0
        internal static string Interpret(string path)
        {
            string fn = Path.GetFileName(path);

            if (fn == "save0.bin" || fn == "save1.bin" || fn == "save2.bin")
            {
                return(FixMajoraChecksum(path));
            }
            if (fn.StartsWith("message") && (fn.EndsWith("_US.bin") || fn.EndsWith("_UK.bin")))
            {
                return(ParseShuffleText(path));
            }
            DARC       darc  = analyze(path);
            FARC       farc  = new FARC();
            GAR        gar   = new GAR();
            SARC       sarc  = new SARC();
            ShuffleARC sharc = new ShuffleARC();

            if (!darc.valid)
            {
                farc = analyzeFARC(path);
            }
            if (!farc.valid)
            {
                gar = analyzeGAR(path);
            }
            if (!gar.valid)
            {
                sharc = AnalyzeShuffle(path);
            }
            if (!sharc.valid)
            {
                sarc = analyzeSARC(path);
            }

            string ret = "";

            if (darc.valid)
            {
                ret += "Header Offset: " + darc.HeaderOffset + Environment.NewLine + "File Count: " + darc.Files.Files.Count + Environment.NewLine;
                int extracted = 0;
                int folder    = 0;
                for (int i = 0; i < darc.Files.Files.Count; i++)
                {
                    if (darc.Files.Files[i].Folder > 0)
                    {
                        folder++;
                    }
                    else
                    {
                        extracted++;
                        string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + darc.FileName + Path.DirectorySeparatorChar;
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        string outPath = dir + darc.Files.FileNames[i];
                        var    fs      = File.OpenRead(path);
                        fs.Seek(darc.Files.Files[i].Offset, SeekOrigin.Begin);
                        byte[] fileBuffer = new byte[darc.Files.Files[i].Length];
                        fs.Read(fileBuffer, 0, fileBuffer.Length);
                        fs.Close();
                        File.WriteAllBytes(outPath, fileBuffer);
                    }
                }
                ret += "Extracted " + extracted + " files";
                if (folder > 0)
                {
                    ret += ", did not extract " + folder + " folders";
                }
                ret += "." + Environment.NewLine + "Open a .DARC/.SARC/.FARC/.GAR/Shuffle Archive file (or drag/drop).";
            }
            else if (farc.valid)
            {
                ret += "Header Offset: " + farc.HeaderOffset + Environment.NewLine;
                int extracted = 0;
                for (int i = 0; i < farc.Files.Files.Count; i++)
                {
                    extracted++;
                    string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + "FARC_" + farc.FileName + Path.DirectorySeparatorChar;
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string outPath = dir + farc.Files.FileNames[i];
                    var    fs      = File.OpenRead(farc.FilePath + "\\" + farc.FileName + farc.Extension);
                    fs.Seek(farc.Files.Files[i].Offset + farc.DataOffset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[farc.Files.Files[i].Length];
                    fs.Read(fileBuffer, 0, fileBuffer.Length);
                    fs.Close();
                    File.WriteAllBytes(outPath, fileBuffer);
                }
                ret += "Extracted " + extracted + " files";
                ret += "." + Environment.NewLine + ".DARC/.FARC/.SARC/.GAR/Shuffle Archive file (or drag/drop).";
            }
            else if (gar.valid)
            {
                ret += "New GAR with " + gar.FileCount + " files." + Environment.NewLine;
                string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + gar.FileName + Path.DirectorySeparatorChar;
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                for (int i = 0; i < gar.FileCount; i++)
                {
                    var fs = File.OpenRead(path);
                    fs.Seek(gar.Files[i].Offset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[gar.Files[i].Length];
                    fs.Read(fileBuffer, 0, fileBuffer.Length);
                    fs.Close();
                    File.WriteAllBytes(dir + gar.Files[i].NameWithExtension, fileBuffer);
                    ret += "Extracted " + gar.Files[i].NameWithExtension + " (Offset: " + gar.Files[i].Offset.ToString("X8") + ", Len: " + gar.Files[i].Length.ToString("X8") + ")." + Environment.NewLine;
                }
                ret += Environment.NewLine;
            }
            else if (sharc.valid)
            {
                ret += "New Shuffle Archive with " + sharc.FileCount + " files." + Environment.NewLine;
                string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + sharc.FileName + "_" + Path.DirectorySeparatorChar;
                if (!Directory.Exists(dir))
                {
                    Console.WriteLine("Making dir: " + dir);
                    Directory.CreateDirectory(dir);
                }

                string diglen = "".PadLeft((int)(Math.Log10(sharc.FileCount) + 1), '0');
                for (int i = 0; i < sharc.FileCount; i++)
                {
                    var fs = File.OpenRead(path);
                    fs.Seek(sharc.Files[i].Offset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[sharc.Files[i].Length];
                    fs.Read(fileBuffer, 0, fileBuffer.Length);
                    fs.Close();
                    uint check = 0;
                    for (int j = 0; j < fileBuffer.Length; j += 4)
                    {
                        check += BitConverter.ToUInt32(fileBuffer, j);
                    }
                    Console.WriteLine(i.ToString(diglen) + ": " + check.ToString("X8"));
                    File.WriteAllBytes(dir + i.ToString(diglen) + ".zip", fileBuffer);
                    ret += "Extracted " + i.ToString(diglen) + " (Offset: " + sharc.Files[i].Offset.ToString("X8") + ", Len: " + sharc.Files[i].Length.ToString("X8") + ")." + Environment.NewLine;
                }
                ret += Environment.NewLine;
            }
            else if (sarc.valid)
            {
                ret = "New SARC with " + sarc.SFat.EntryCount + " files." + Environment.NewLine;
                string dir = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + sarc.FileName + "_" + Path.DirectorySeparatorChar;
                if (!Directory.Exists(dir))
                {
                    Console.WriteLine("Making dir: " + dir);
                    Directory.CreateDirectory(dir);
                }

                foreach (SFATEntry t in sarc.SFat.Entries)
                {
                    var  fs      = File.OpenRead(path);
                    uint FileLen = t.FileDataEnd - t.FileDataStart;
                    fs.Seek(t.FileDataStart + sarc.DataOffset, SeekOrigin.Begin);
                    byte[] fileBuffer = new byte[FileLen];
                    fs.Read(fileBuffer, 0, (int)FileLen);
                    fs.Seek(sarc.SFnt.StringOffset, SeekOrigin.Begin);
                    fs.Seek((t.FileNameOffset & 0x00FFFFFF) * 4, SeekOrigin.Current);
                    StringBuilder sb = new StringBuilder();
                    for (char c = (char)fs.ReadByte(); c != 0; c = (char)fs.ReadByte())
                    {
                        sb.Append(c);
                    }
                    string FileName = sb.ToString().Replace('/', Path.DirectorySeparatorChar);
                    fs.Close();
                    string FileDir = Path.GetDirectoryName(dir + FileName) + Path.DirectorySeparatorChar;
                    if (!Directory.Exists(FileDir))
                    {
                        Console.WriteLine("Making dir: " + FileDir);
                        Directory.CreateDirectory(FileDir);
                    }
                    File.WriteAllBytes(dir + FileName, fileBuffer);
                }
            }
            else
            {
                ret = "Not a valid .DARC/.FARC/.SARC/.GAR/Shuffle Archive file";
            }
            return(ret);
        }