Beispiel #1
0
        // Unpacking
        internal static string unpackDARC(string path, string outFolder = null, bool delete = true)
        {
            int  extracted = 0;
            int  folder    = 0;
            DARC darc      = analyze(path);

            if (!darc.valid)
            {
                return("Not a DARC?");
            }

            for (int i = 0; i < darc.Files.Files.Count; i++)
            {
                if (darc.Files.Files[i].Folder > 0)
                {
                    folder++;
                }
                else
                {
                    extracted++;
                    string dir = outFolder ?? Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + darc.FileName + Path.DirectorySeparatorChar;
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    using (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);
                        File.WriteAllBytes(Path.Combine(dir, darc.Files.FileNames[i]), fileBuffer);
                    }
                }
            }
            if (delete)
            {
                File.Delete(path); // File is unpacked.
            }
            // Debug info string:
            string s = "";

            s += "Header Offset: " + darc.HeaderOffset + Environment.NewLine;
            s += "File Count: " + darc.Files.Files.Count + Environment.NewLine;
            s += "Extracted " + extracted + " files";
            s += folder > 0 ? ", did not extract " + folder + " folders." : ".";
            return(s);
        }
        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            if (compressed)
            {
                WinFormsUtil.Alert("Recompressing may take some time...", "Don't panic if the Progress Bar doesn't move!");
            }
            // Write darcs
            for (int i = 0; i < darcs.Length; i++)
            {
                var data = File.ReadAllBytes(usedFiles[i]);
                int pos  = 0;
                while (BitConverter.ToUInt32(data, pos) != 0x63726164)
                {
                    pos += 4;
                    if (pos >= data.Length)
                    {
                        return;
                    }
                }
                byte[] preData  = data.Take(pos).ToArray();
                byte[] darcData = DARC.SetDARC(darcs[i]);
                byte[] newData  = preData.Concat(darcData).ToArray();

                byte[] oldDarc = File.ReadAllBytes(usedFiles[i]);
                if (newData.SequenceEqual(oldDarc)) // if same, just continue.
                {
                    if (compressed)
                    {
                        File.Delete(usedFiles[i]); // Use old compressed file (speedup)
                    }
                }
                else // File is different, replace and allow repacking to compress.
                {
                    if (compressed)
                    {
                        File.Delete(files[darcFiles[i]]);      // delete the old compressed file
                    }
                    File.WriteAllBytes(usedFiles[i], newData); // write the new edited (uncompressed) file
                }
            }
        }
        public TitleScreenEditor6()
        {
            InitializeComponent();
            AllowDrop           = true;
            DragEnter          += TC_Main_DragEnter;
            DragDrop           += TC_Main_DragDrop;
            PB_Image.AllowDrop  = true;
            PB_Image.DragEnter += TC_Main_DragEnter;
            PB_Image.DragDrop  += TC_Main_DragDrop;

            // Add tooltip to image
            new ToolTip().SetToolTip(PB_Image, "Click to toggle Green Screen\nRightClick for I/O\nCTRL+Click for Copy->Clipboard.");

            // Add context menus
            ContextMenuStrip  mnu  = new ContextMenuStrip();
            ToolStripMenuItem mnuR = new ToolStripMenuItem("Replace with...");
            ToolStripMenuItem mnuS = new ToolStripMenuItem("Save as...");

            // Assign event handlers
            mnuR.Click += ClickOpen;
            mnuS.Click += ClickSave;
            // Add to main context menu
            mnu.Items.AddRange(new ToolStripItem[] { mnuR, mnuS, });

            // Assign
            PB_Image.ContextMenuStrip = mnu;

            // Set up languages
            string[] languages = (Main.Config.ORAS ? new[] { "JP1" } : Array.Empty <string>()).Concat(new[] { "DE", "ES", "FR", "IT", "JP", "KO", "EN" }).ToArray();
            string[] games     = Main.Config.ORAS ? new[] { "OR", "AS" } : new[] { "X", "Y" };
            for (int i = 0; i < darcs.Length / 2; i++)
            {
                CB_DARC.Items.Add($"{games[0]} - {languages[i]}");
            }
            for (int i = darcs.Length / 2; i < darcs.Length; i++)
            {
                CB_DARC.Items.Add($"{games[1]} - {languages[i - (darcs.Length/2)]}");
            }

            // Load darcs
            for (int i = 0; i < darcs.Length; i++)
            {
                // Get DARC name and assign the decompressed name
                usedFiles[i] = "titlescreen\\" + (compressed ? "dec_" : "") + Path.GetFileName(files[darcFiles[i]]);
                if (compressed) // Decompress file (XY does not compress)
                {
                    LZSS.Decompress(files[darcFiles[i]], usedFiles[i]);
                }
                // Read decompressed file
                var data = File.ReadAllBytes(usedFiles[i]);

                // Find darc data offset (ignore header)
                int pos = 0;
                while (BitConverter.ToUInt32(data, pos) != 0x63726164)
                {
                    pos += 4;
                    if (pos >= data.Length)
                    {
                        throw new Exception("Invalid DARC?\n\n" + usedFiles[i]);
                    }
                }
                var darcData = data.Skip(pos).ToArray();
                darcs[i] = new DARC(darcData);
            }

            CB_DARC.SelectedIndex = CB_DARC.Items.Count - 1; // last (english game2)
        }
Beispiel #4
0
        internal static DARC analyze(string path)
        {
            DARC darc = new DARC
            {
                FileName = Path.GetFileNameWithoutExtension(path),
                FilePath = Path.GetDirectoryName(path),
                Extension = Path.GetExtension(path)
            };
            using (BinaryReader br = new BinaryReader(File.OpenRead(path))) {
            long len = br.BaseStream.Length;
            darc.Magic = br.ReadUInt32();
            UInt32 m = darc.Magic;
            darc.HeaderOffset = 0;
            while (m != 0x63726164 && darc.HeaderOffset < len - 4)
            {
                m = br.ReadUInt32();
                darc.HeaderOffset += 4;
            }
            if (darc.HeaderOffset >= len - 4)
            {
                darc.valid = false;
                return darc;
            }
            darc.Magic = m;
            darc.valid = true;
            darc.BOM = br.ReadUInt16();
            darc.HeaderLength = br.ReadUInt16();
            darc.Unknown = br.ReadUInt32();
            darc.totalLength = br.ReadUInt32();
            darc.TableOffset = br.ReadUInt32(); //from start of file
            darc.TableOffset += darc.HeaderOffset;
            darc.TableLength = br.ReadUInt32();
            darc.DataOffset = br.ReadUInt32();
            FileTable ft = new FileTable();
            br.BaseStream.Seek(darc.TableOffset + 8, SeekOrigin.Begin);
            int count = br.ReadByte();
            ft.Files = new List<DarcFile>();
            ft.FileNames = new List<string>();
            br.BaseStream.Seek(darc.TableOffset, SeekOrigin.Begin);
            for (int i = 0; i < count; i++)
            {
                DarcFile file = new DarcFile
                {
                    NameOffset = br.ReadUInt16(),
                    Parent = br.ReadByte(),
                    Folder = br.ReadByte(),
                    Offset = br.ReadUInt32() + darc.HeaderOffset,
                    Length = br.ReadUInt32()
                };
                DarcFile f2 = file;
                ft.Files.Add(f2);
            }

            uint NameTableOffset = (uint)br.BaseStream.Position;
            for (int i = 0; i < ft.Files.Count; i++)
            {
                br.BaseStream.Seek(NameTableOffset + ft.Files[i].NameOffset, SeekOrigin.Begin);
                MemoryStream stream = new MemoryStream();
                for (byte fb = br.ReadByte(), sb = br.ReadByte();
                    fb != 0 || sb != 0;
                    fb = br.ReadByte(), sb = br.ReadByte())
                {
                    stream.WriteByte(fb);
                    stream.WriteByte(sb);
                }
                ft.FileNames.Add(Encoding.Unicode.GetString(stream.ToArray()));
                stream.Close();
            }
            darc.Files = ft;
            darc.FileName = Path.GetFileNameWithoutExtension(path);
            darc.FilePath = Path.GetDirectoryName(path);
            darc.Extension = Path.GetExtension(path);
            return darc;
            }
        }
Beispiel #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);
        }
Beispiel #6
0
        internal static DARC analyze(string path)
        {
            DARC darc = new DARC
            {
                FileName  = Path.GetFileNameWithoutExtension(path),
                FilePath  = Path.GetDirectoryName(path),
                Extension = Path.GetExtension(path)
            };

            using (BinaryReader br = new BinaryReader(File.OpenRead(path))) {
                long len = br.BaseStream.Length;
                darc.Magic = br.ReadUInt32();
                uint m = darc.Magic;
                darc.HeaderOffset = 0;
                while (m != 0x63726164 && darc.HeaderOffset < len - 4)
                {
                    m = br.ReadUInt32();
                    darc.HeaderOffset += 4;
                }
                if (darc.HeaderOffset >= len - 4)
                {
                    darc.valid = false;
                    return(darc);
                }
                darc.Magic        = m;
                darc.valid        = true;
                darc.BOM          = br.ReadUInt16();
                darc.HeaderLength = br.ReadUInt16();
                darc.Unknown      = br.ReadUInt32();
                darc.totalLength  = br.ReadUInt32();
                darc.TableOffset  = br.ReadUInt32(); //from start of file
                darc.TableOffset += darc.HeaderOffset;
                darc.TableLength  = br.ReadUInt32();
                darc.DataOffset   = br.ReadUInt32();
                FileTable ft = new FileTable();
                br.BaseStream.Seek(darc.TableOffset + 8, SeekOrigin.Begin);
                int count = br.ReadByte();
                ft.Files     = new List <DarcFile>();
                ft.FileNames = new List <string>();
                br.BaseStream.Seek(darc.TableOffset, SeekOrigin.Begin);
                for (int i = 0; i < count; i++)
                {
                    DarcFile file = new DarcFile
                    {
                        NameOffset = br.ReadUInt16(),
                        Parent     = br.ReadByte(),
                        Folder     = br.ReadByte(),
                        Offset     = br.ReadUInt32() + darc.HeaderOffset,
                        Length     = br.ReadUInt32()
                    };
                    DarcFile f2 = file;
                    ft.Files.Add(f2);
                }

                uint NameTableOffset = (uint)br.BaseStream.Position;
                for (int i = 0; i < ft.Files.Count; i++)
                {
                    br.BaseStream.Seek(NameTableOffset + ft.Files[i].NameOffset, SeekOrigin.Begin);
                    MemoryStream stream = new MemoryStream();
                    for (byte fb = br.ReadByte(), sb = br.ReadByte();
                         fb != 0 || sb != 0;
                         fb = br.ReadByte(), sb = br.ReadByte())
                    {
                        stream.WriteByte(fb);
                        stream.WriteByte(sb);
                    }
                    ft.FileNames.Add(Encoding.Unicode.GetString(stream.ToArray()));
                    stream.Close();
                }
                darc.Files     = ft;
                darc.FileName  = Path.GetFileNameWithoutExtension(path);
                darc.FilePath  = Path.GetDirectoryName(path);
                darc.Extension = Path.GetExtension(path);
                return(darc);
            }
        }