// 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); }
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); }
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); } }
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); }