Esempio n. 1
0
        internal static SARC analyzeSARC(string path)
        {
            SARC sarc = new SARC
            {
                FileName  = Path.GetFileNameWithoutExtension(path),
                FilePath  = Path.GetDirectoryName(path),
                Extension = Path.GetExtension(path)
            };
            BinaryReader br = new BinaryReader(File.OpenRead(path));

            sarc.valid     = true;
            sarc.Signature = new string(br.ReadChars(4));
            if (sarc.Signature != "SARC")
            {
                sarc.valid = false;
                return(sarc);
            }
            sarc.HeaderSize = br.ReadUInt16();
            sarc.Endianness = br.ReadUInt16();
            sarc.FileSize   = br.ReadUInt32();
            sarc.DataOffset = br.ReadUInt32();
            sarc.Unknown    = br.ReadUInt32();
            sarc.SFat       = new SFAT {
                Signature = new string(br.ReadChars(4))
            };
            if (sarc.SFat.Signature != "SFAT")
            {
                sarc.valid = false;
                return(sarc);
            }
            sarc.SFat.HeaderSize = br.ReadUInt16();
            sarc.SFat.EntryCount = br.ReadUInt16();
            sarc.SFat.HashMult   = br.ReadUInt32();
            sarc.SFat.Entries    = new List <SFATEntry>();
            for (int i = 0; i < sarc.SFat.EntryCount; i++)
            {
                SFATEntry s = new SFATEntry
                {
                    FileNameHash   = br.ReadUInt32(),
                    FileNameOffset = br.ReadUInt32(),
                    FileDataStart  = br.ReadUInt32(),
                    FileDataEnd    = br.ReadUInt32()
                };
                sarc.SFat.Entries.Add(s);
            }
            sarc.SFnt = new SFNT {
                Signature = new string(br.ReadChars(4))
            };
            if (sarc.SFnt.Signature != "SFNT")
            {
                sarc.valid = false;
                return(sarc);
            }
            sarc.SFnt.HeaderSize   = br.ReadUInt16();
            sarc.SFnt.Unknown      = br.ReadUInt16();
            sarc.SFnt.StringOffset = (uint)br.BaseStream.Position;
            return(sarc);
        }
Esempio n. 2
0
        internal static void OpenARC(string path, ProgressBar pBar1, bool recursing = false)
        {
            string newFolder = "";

            try
            {
                // Pre-check file length to see if it is at least valid.
                FileInfo fi = new FileInfo(path);
                if (fi.Length > (long)2 * (1 << 30))
                {
                    WinFormsUtil.Error("File is too big!"); return;
                }                                                                                      // 2 GB
                string folderPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));

                byte[] first4 = new byte[4];
                try
                {
                    using var fs = new FileStream(path, FileMode.Open);
                    using var bw = new BinaryReader(fs);
                    first4       = bw.ReadBytes(4);
                }
                catch (Exception e)
                {
                    WinFormsUtil.Error("Cannot open file!", e.ToString());
                }

                // Determine if it is a DARC or a Mini
                // Check if Mini first
                string fx = fi.Length > 10 * (1 << 20) ? null : Mini.GetIsMini(path); // no mini is above 10MB
                if (fx != null)                                                       // Is Mini Packed File
                {
                    newFolder = folderPath + "_" + fx;
                    // Fetch Mini File Contents
                    Mini.UnpackMini(path, fx, newFolder, false);
                    // Recurse throught the extracted contents if they extract successfully
                    if (Directory.Exists(newFolder))
                    {
                        foreach (string file in Directory.GetFiles(newFolder))
                        {
                            OpenARC(file, pBar1, true);
                        }
                        BatchRenameExtension(newFolder);
                    }
                }
                else if (first4.SequenceEqual(BitConverter.GetBytes(0x54594C41))) // ALYT
                {
                    if (threads > 0)
                    {
                        WinFormsUtil.Alert("Please wait for all operations to finish first."); return;
                    }
                    new Thread(() =>
                    {
                        Interlocked.Increment(ref threads);
                        var alyt = new ALYT(File.ReadAllBytes(path));
                        var sarc = new SARC(alyt.Data) // rip out sarc
                        {
                            FileName = Path.GetFileNameWithoutExtension(path) + "_sarc",
                            FilePath = Path.GetDirectoryName(path)
                        };
                        if (!sarc.Valid)
                        {
                            return;
                        }
                        var files = sarc.Dump();
                        foreach (var _ in files)
                        {
                            // openARC(file, pBar1, true);
                        }
                        Interlocked.Decrement(ref threads);
                    }).Start();
                }
                else if (first4.SequenceEqual(BitConverter.GetBytes(0x47415243))) // GARC
                {
                    if (threads > 0)
                    {
                        WinFormsUtil.Alert("Please wait for all operations to finish first."); return;
                    }
                    bool SkipDecompression = ModifierKeys == Keys.Control;
                    new Thread(() =>
                    {
                        Interlocked.Increment(ref threads);
                        bool r = GarcUtil.UnpackGARC(path, folderPath + "_g", SkipDecompression, pBar1);
                        Interlocked.Decrement(ref threads);
                        if (r)
                        {
                            BatchRenameExtension(newFolder);
                        }
                        else
                        {
                            WinFormsUtil.Alert("Unpacking failed."); return;
                        }
                        System.Media.SystemSounds.Asterisk.Play();
                    }).Start();
                }
                else if (ARC.Analyze(path).valid) // DARC
                {
                    var data = File.ReadAllBytes(path);
                    int pos  = 0;
                    while (BitConverter.ToUInt32(data, pos) != 0x63726164)
                    {
                        pos += 4;
                        if (pos >= data.Length)
                        {
                            return;
                        }
                    }
                    var darcData = data.Skip(pos).ToArray();
                    newFolder = folderPath + "_d";
                    bool r = Core.CTR.DARC.Darc2files(darcData, newFolder);
                    if (!r)
                    {
                        WinFormsUtil.Alert("Unpacking failed.");
                    }
                }
                else if (ARC.AnalyzeSARC(path).Valid)
                {
                    var sarc = ARC.AnalyzeSARC(path);
                    Console.WriteLine($"New SARC with {sarc.SFAT.EntryCount} files.");
                    foreach (var _ in sarc.Dump(path))
                    {
                    }
                }
                else if (!recursing)
                {
                    WinFormsUtil.Alert("File is not a darc or a mini packed file:" + Environment.NewLine + path);
                }
            }
            catch (Exception e)
            {
                if (!recursing)
                {
                    WinFormsUtil.Error("File error:" + Environment.NewLine + path, e.ToString());
                }
                threads = 0;
            }
        }
Esempio 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;
        }
Esempio n. 4
0
 internal static SARC analyzeSARC(string path)
 {
     SARC sarc = new SARC
     {
         FileName = Path.GetFileNameWithoutExtension(path),
         FilePath = Path.GetDirectoryName(path),
         Extension = Path.GetExtension(path)
     };
     BinaryReader br = new BinaryReader(File.OpenRead(path));
     sarc.valid = true;
     sarc.Signature = new string(br.ReadChars(4));
     if (sarc.Signature != "SARC")
     {
         sarc.valid = false;
         return sarc;
     }
     sarc.HeaderSize = br.ReadUInt16();
     sarc.Endianness = br.ReadUInt16();
     sarc.FileSize = br.ReadUInt32();
     sarc.DataOffset = br.ReadUInt32();
     sarc.Unknown = br.ReadUInt32();
     sarc.SFat = new SFAT { Signature = new string(br.ReadChars(4)) };
     if (sarc.SFat.Signature != "SFAT")
     {
         sarc.valid = false;
         return sarc;
     }
     sarc.SFat.HeaderSize = br.ReadUInt16();
     sarc.SFat.EntryCount = br.ReadUInt16();
     sarc.SFat.HashMult = br.ReadUInt32();
     sarc.SFat.Entries = new List<SFATEntry>();
     for (int i = 0; i < sarc.SFat.EntryCount; i++)
     {
         SFATEntry s = new SFATEntry
         {
             FileNameHash = br.ReadUInt32(),
             FileNameOffset = br.ReadUInt32(),
             FileDataStart = br.ReadUInt32(),
             FileDataEnd = br.ReadUInt32()
         };
         sarc.SFat.Entries.Add(s);
     }
     sarc.SFnt = new SFNT { Signature = new string(br.ReadChars(4)) };
     if (sarc.SFnt.Signature != "SFNT")
     {
         sarc.valid = false;
         return sarc;
     }
     sarc.SFnt.HeaderSize = br.ReadUInt16();
     sarc.SFnt.Unknown = br.ReadUInt16();
     sarc.SFnt.StringOffset = (uint)br.BaseStream.Position;
     return sarc;
 }
Esempio n. 5
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);
        }